From 1b63fddf685f0663fd413f5c7996b848d2be08fb Mon Sep 17 00:00:00 2001 From: Tony Tung Date: Mon, 20 Aug 2018 14:10:28 -0700 Subject: [PATCH] [easy] group all the builder methods together (#455) All the builders should be together. --- starfish/stack.py | 64 +++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/starfish/stack.py b/starfish/stack.py index 244084cbc..bc6b753b6 100644 --- a/starfish/stack.py +++ b/starfish/stack.py @@ -175,6 +175,38 @@ def from_path_or_url(cls, url_or_path: str) -> "ImageStack": _, relativeurl, baseurl = resolve_path_or_url(url_or_path) return cls.from_url(relativeurl, baseurl) + @classmethod + def from_numpy_array(cls, array: np.ndarray) -> "ImageStack": + """Create an ImageStack from a 5d numpy array with shape (n_round, n_ch, n_z, y, x) + + Parameters + ---------- + array : np.ndarray + 5-d tensor of shape (n_round, n_ch, n_z, y, x) + + Returns + ------- + ImageStack : + array data stored as an ImageStack + + """ + if len(array.shape) != 5: + raise ValueError('a 5-d tensor with shape (n_round, n_ch, n_z, y, x) must be provided.') + n_round, n_ch, n_z, height, width = array.shape + empty = cls.synthetic_stack( + num_round=n_round, num_ch=n_ch, num_z=n_z, tile_height=height, tile_width=width) + + # preserve original dtype + empty._data = empty._data.astype(array.dtype) + + for h in np.arange(n_round): + for c in np.arange(n_ch): + for z in np.arange(n_z): + view = array[h, c, z] + empty.set_slice({Indices.ROUND: h, Indices.CH: c, Indices.Z: z}, view) + + return empty + @property def numpy_array(self): """Retrieves the image data as a numpy array.""" @@ -1051,35 +1083,3 @@ def un_squeeze(self, stack): new_shape = (self.num_rounds, self.num_chs, self.num_zlayers) + self.tile_shape res = stack.reshape(new_shape) return res - - @classmethod - def from_numpy_array(cls, array: np.ndarray) -> "ImageStack": - """Create an ImageStack from a 5d numpy array with shape (n_round, n_ch, n_z, y, x) - - Parameters - ---------- - array : np.ndarray - 5-d tensor of shape (n_round, n_ch, n_z, y, x) - - Returns - ------- - ImageStack : - array data stored as an ImageStack - - """ - if len(array.shape) != 5: - raise ValueError('a 5-d tensor with shape (n_round, n_ch, n_z, y, x) must be provided.') - n_round, n_ch, n_z, height, width = array.shape - empty = cls.synthetic_stack( - num_round=n_round, num_ch=n_ch, num_z=n_z, tile_height=height, tile_width=width) - - # preserve original dtype - empty._data = empty._data.astype(array.dtype) - - for h in np.arange(n_round): - for c in np.arange(n_ch): - for z in np.arange(n_z): - view = array[h, c, z] - empty.set_slice({Indices.ROUND: h, Indices.CH: c, Indices.Z: z}, view) - - return empty