Skip to content
Snippets Groups Projects
Commit c14c2ad9 authored by Alán Muñoz's avatar Alán Muñoz
Browse files

change(vis_tools): refactor _overlay_mask_tile

parent 76f293b9
No related branches found
No related tags found
No related merge requests found
...@@ -41,7 +41,7 @@ def get_tiles_at_times( ...@@ -41,7 +41,7 @@ def get_tiles_at_times(
Parameters Parameters
---------- ----------
image_path : str image_path : str
hdf5 location hdf5 index
timepoints : t.List[int] timepoints : t.List[int]
list of timepoints to fetch list of timepoints to fetch
tile_reduction : t.Union[int, t.List[int], str, t.Callable] tile_reduction : t.Union[int, t.List[int], str, t.Callable]
...@@ -164,60 +164,84 @@ def crop_mask(img: np.ndarray, mask: np.ndarray): ...@@ -164,60 +164,84 @@ def crop_mask(img: np.ndarray, mask: np.ndarray):
return img return img
def overlay_masks_tiles( def _sample_n_tiles_masks(
image_path: str, image_path: str,
results_path: str, results_path: str,
masks: np.ndarray, n: int,
locations: t.Tuple[t.Tuple[int], t.Tuple[int], t.Tuple[int]], seed: int = 0,
interval=None,
as_generator=False,
) -> t.Tuple[t.Tuple, t.Tuple[np.ndarray, np.ndarray]]:
cells = Cells(results_path)
indices, masks = cells._sample_masks(n, seed=seed, interval=interval)
processed_tiles, cropped_masks = _overlay_masks_tiles(
image_path,
results_path,
masks,
[indices[i] for i in (0, 2)],
as_generator=as_generator,
)
return indices, (processed_tiles, cropped_masks)
def _overlay_mask_tile(
image_path: str,
results_path: str,
mask: np.ndarray,
index: t.Tuple[int, int, int],
bg_channel: int = 0, bg_channel: int = 0,
fg_channel: int = 1, fg_channel: int = 1,
reduce_z: t.Union[None, t.Callable] = np.max, reduce_z: t.Union[None, t.Callable] = np.max,
as_generator: bool = False,
) -> t.Tuple[np.ndarray, np.ndarray]: ) -> t.Tuple[np.ndarray, np.ndarray]:
"""
Return a tuplw with two channels
"""
tcs = np.stack( tc = np.stack(
[ [
[ fetch_tc(image_path, results_path, index[1], i)
fetch_tc(image_path, results_path, tp, i) for i in (bg_channel, fg_channel)
for i in (bg_channel, fg_channel)
]
for tp in locations[1]
] ]
) # Returns TC(tile)ZYX ) # Returns C(tile)ZYX
tiles = np.stack( tiles = tc[:, index[0]].astype(float)
[tcs[i, :, tile].astype(float) for i, tile in enumerate(locations[0])]
)
reduced_z = ( reduced_z = (
reduce_z(tiles, axis=2) if reduce_z else concatenate_dims(tiles, 2, -2) reduce_z(tiles, axis=1) if reduce_z else concatenate_dims(tiles, 1, -2)
) )
repeated_mask = np.stack( repeated_mask = tile_like(mask, reduced_z[0])
[tile_like(mask, reduced_z[0, 0]) for mask in masks]
)
cropped_fg = np.stack( cropped_fg = crop_mask(reduced_z[1], repeated_mask)
[crop_mask(c, mask) for mask, c in zip(repeated_mask, reduced_z[:, 1])]
)
return reduced_z[:, 0], cropped_fg return reduced_z[0], cropped_fg
def _sample_n_tiles_masks( def _overlay_masks_tiles(
image_path: str, image_path: str,
results_path: str, results_path: str,
n: int, masks: np.ndarray,
seed: int = 0, indices: t.Tuple[t.Tuple[int], t.Tuple[int], t.Tuple[int]],
interval=None, bg_channel: int = 0,
) -> t.Tuple[t.Tuple, t.Tuple[np.ndarray, np.ndarray]]: fg_channel: int = 1,
reduce_z: t.Union[None, t.Callable] = np.max,
as_generator: bool = False,
) -> t.Tuple[np.ndarray, np.ndarray]:
cells = Cells(results_path) tmp = [
locations, masks = cells._sample_masks(n, seed=seed, interval=interval) _overlay_mask_tile(
image_path,
results_path,
mask,
index,
bg_channel,
fg_channel,
reduce_z,
)
for mask, index in zip(masks, zip(*indices))
]
processed_tiles, cropped_masks = overlay_masks_tiles( return [np.stack(x) for x in zip(*tmp)]
image_path,
results_path,
masks,
[locations[i] for i in (0, 2)],
)
return locations, (processed_tiles, cropped_masks)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment