Skip to content

Commit

Permalink
[easy] group all the builder methods together (#455)
Browse files Browse the repository at this point in the history
All the builders should be together.
  • Loading branch information
ttung authored Aug 20, 2018
1 parent a7e9e8a commit 1b63fdd
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions starfish/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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

0 comments on commit 1b63fdd

Please sign in to comment.