Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix converter for ndtiff using multicam #218

Draft
wants to merge 1 commit into
base: ndtiff-pixel-size-fallback
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions iohub/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def __init__(
# for `get_num_positions()`
self.p = self.reader.num_positions
self.t = self.reader.frames
self.cams = self.reader.cameras # multicam
self.c = self.reader.channels
self.z = self.reader.slices
self.y = self.reader.height
Expand Down Expand Up @@ -474,7 +475,7 @@ def _convert_ndtiff(self):
)
try:
ndtiff_pos_idx, *_ = self.reader._check_coordinates(
ndtiff_pos_idx, 0, 0, 0
ndtiff_pos_idx, 0, 0, 0, 0
)
except ValueError:
# Log warning and continue if some positions were not
Expand All @@ -489,13 +490,18 @@ def _convert_ndtiff(self):
zarr_pos_name = self.zarr_position_names[p_idx]
zarr_arr = self.writer[zarr_pos_name]["0"]

for t_idx, c_idx in product(
for t_idx, c_idx, cam_idx in product(
range(self.t),
range(self.c),
range(len(self.cams)),
bar_format=bar_format_time_channel,
position=1,
leave=False,
):
if self.reader.cameras is not None:
c_idx = c_idx // len(self.cams)
ndtiff_cam_str = self.reader.cameras[cam_idx]

ndtiff_channel_idx = (
self.reader.channel_names[c_idx]
if self.reader.str_channel_axis
Expand All @@ -507,9 +513,10 @@ def _convert_ndtiff(self):
_,
ndtiff_t_idx,
ndtiff_channel_idx,
ndtiff_cam_str,
ndtiff_z_idx,
) = self.reader._check_coordinates(
ndtiff_pos_idx, t_idx, ndtiff_channel_idx, 0
ndtiff_pos_idx, t_idx, ndtiff_channel_idx, 0, 0
)
# Log warning and continue if some T/C were not acquired in the
# dataset
Expand All @@ -518,6 +525,7 @@ def _convert_ndtiff(self):
time=ndtiff_t_idx,
channel=ndtiff_channel_idx,
z=ndtiff_z_idx,
camera=ndtiff_cam_str,
):
logging.warning(
f"Cannot load data at timepoint {t_idx}, channel "
Expand All @@ -540,6 +548,7 @@ def _convert_ndtiff(self):
ndtiff_pos_idx,
ndtiff_t_idx,
ndtiff_channel_idx,
ndtiff_cam_str,
z_idx,
)
# T/C/Z
Expand Down
73 changes: 57 additions & 16 deletions iohub/ndtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ def __init__(self, data_path: str):
self.frames = (
len(self._axes["time"]) if "time" in self._axes.keys() else 1
)
self.slices = len(self._axes["z"]) if "z" in self._axes.keys() else 1
self.cameras = (
self._axes["camera"] if "camera" in self._axes.keys() else []
)
self.channels = (
len(self._axes["channel"]) if "channel" in self._axes.keys() else 1
)
self.slices = len(self._axes["z"]) if "z" in self._axes.keys() else 1

self.height = self.dataset.image_height
self.width = self.dataset.image_width
self.dtype = self.dataset.dtype
Expand All @@ -37,20 +41,29 @@ def __init__(self, data_path: str):
self.z_step_size = self.mm_meta["Summary"]["z-step_um"]
self.xy_pixel_size = self.mm_meta["Summary"].get("PixelSize_um", 1.0)

if len(self.cameras) > 1:
channel_names = []
self.channels = self.channels * len(self.cameras)
for chan_name in self.channel_names:
for cam in self.cameras:
channel_names.append(chan_name + "_" + cam)
self.channel_names = channel_names

def _get_summary_metadata(self):
pm_metadata = self.dataset.summary_metadata
pm_metadata["MicroManagerVersion"] = "pycromanager"
pm_metadata["Positions"] = self.get_num_positions()
img_metadata = self.get_image_metadata(0, 0, 0, 0)

img_metadata = self.get_image_metadata(0, 0, 0, 0, 0)

pm_metadata["z-step_um"] = None
if "ZPosition_um_Intended" in img_metadata.keys():
pm_metadata["z-step_um"] = np.around(
abs(
self.get_image_metadata(0, 0, 0, 1)[
self.get_image_metadata(0, 0, 0, 0, 1)[
"ZPosition_um_Intended"
]
- self.get_image_metadata(0, 0, 0, 0)[
- self.get_image_metadata(0, 0, 0, 0, 0)[
"ZPosition_um_Intended"
]
),
Expand All @@ -61,7 +74,7 @@ def _get_summary_metadata(self):
if "position" in self._axes:
for position in self._axes["position"]:
position_metadata = {}
img_metadata = self.get_image_metadata(position, 0, 0, 0)
img_metadata = self.get_image_metadata(position, 0, 0, 0, 0)

if img_metadata is not None and all(
key in img_metadata.keys()
Expand Down Expand Up @@ -102,15 +115,20 @@ def str_channel_axis(self) -> bool:
return self._str_channel_axis

def _check_coordinates(
self, p: Union[int, str], t: int, c: Union[int, str], z: int
self,
p: Union[int, str],
t: int,
c: Union[int, str],
camera: Union[int, str],
z: int,
):
"""
Check that the (p, t, c, z) coordinates are part of the ndtiff dataset.
Replace coordinates with None or string values in specific cases - see
below
"""
coords = [p, t, c, z]
axes = ("position", "time", "channel", "z")
coords = [p, t, c, camera, z]
axes = ("position", "time", "channel", "camera", "z")

for i, axis in enumerate(axes):
coord = coords[i]
Expand Down Expand Up @@ -172,7 +190,12 @@ def get_num_positions(self) -> int:
)

def get_image(
self, p: Union[int, str], t: int, c: Union[int, str], z: int
self,
p: Union[int, str],
t: int,
c: Union[int, str],
camera: Union[int, str],
z: int,
) -> np.ndarray:
"""return the image at the provided PTCZ coordinates

Expand All @@ -194,10 +217,20 @@ def get_image(
"""

image = None
p, t, c, z = self._check_coordinates(p, t, c, z)
p, t, c, camera, z = self._check_coordinates(
p,
t,
c,
camera,
z,
)

if self.dataset.has_image(position=p, time=t, channel=c, z=z):
image = self.dataset.read_image(position=p, time=t, channel=c, z=z)
if self.dataset.has_image(
position=p, time=t, channel=c, camera=camera, z=z
):
image = self.dataset.read_image(
position=p, time=t, channel=c, camera=camera, z=z
)

return image

Expand Down Expand Up @@ -238,6 +271,7 @@ def get_zarr(self, position: Union[int, str]) -> zarr.array:
position = None

da = self.dataset.as_array(position=position)

shape = (
self.frames,
self.channels,
Expand Down Expand Up @@ -265,7 +299,12 @@ def get_array(self, position: Union[int, str]) -> np.ndarray:
return np.asarray(self.get_zarr(position))

def get_image_metadata(
self, p: Union[int, str], t: int, c: Union[int, str], z: int
self,
p: Union[int, str],
t: int,
c: Union[int, str],
z: int,
camera: Union[int, str],
) -> dict:
"""Return image plane metadata at the requested PTCZ coordinates

Expand All @@ -286,11 +325,13 @@ def get_image_metadata(
image plane metadata
"""
metadata = None
p, t, c, z = self._check_coordinates(p, t, c, z)
p, t, c, camera, z = self._check_coordinates(p, t, c, z, camera)

if self.dataset.has_image(position=p, time=t, channel=c, z=z):
if self.dataset.has_image(
position=p, time=t, channel=c, z=z, camera=camera
):
metadata = self.dataset.read_metadata(
position=p, time=t, channel=c, z=z
position=p, time=t, channel=c, z=z, camera=camera
)

return metadata