Skip to content

Commit 73c10c3

Browse files
authored
More robust conversion (#315)
* iterate over P index to find first frame with metadata * don't error out if all positions at a given timepoint are missing * style * move ndtiff index parsing internally * style * better error message * update docstring
1 parent 74d2b9f commit 73c10c3

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

iohub/ndtiff.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import warnings
55
from json import JSONDecodeError
66
from pathlib import Path
7-
from typing import Any, Iterable, Literal
7+
from typing import Iterable, Literal
88

99
import numpy as np
1010
from natsort import natsorted
@@ -45,7 +45,7 @@ def __getitem__(
4545
def xdata(self) -> DataArray:
4646
return self._xdata
4747

48-
def frame_metadata(self, t: int, c: int, z: int) -> dict[str, Any]:
48+
def frame_metadata(self, t: int, c: int, z: int) -> dict | None:
4949
return self.parent.get_image_metadata(self._position, t, c, z)
5050

5151

@@ -143,9 +143,12 @@ def _get_summary_metadata(self):
143143
pm_metadata["MicroManagerVersion"] = "pycromanager"
144144
pm_metadata["Positions"] = len(self)
145145

146-
p_idx = self._all_position_keys[0]
147146
c_idx = self._ndtiff_channel_names[0]
148-
img_metadata = self.get_image_metadata(p_idx, 0, c_idx, 0)
147+
# Get the first position index that has metadata
148+
for p_idx in self._all_position_keys:
149+
img_metadata = self.get_image_metadata(p_idx, 0, c_idx, 0)
150+
if img_metadata is not None:
151+
break
149152

150153
try:
151154
z0 = self.get_image_metadata(p_idx, 0, c_idx, 0)[
@@ -316,7 +319,7 @@ def xdata(self) -> XDataset:
316319

317320
def get_image_metadata(
318321
self, p: int | str, t: int, c: int | str, z: int
319-
) -> dict:
322+
) -> dict | None:
320323
"""Return image plane metadata at the requested PTCZ coordinates
321324
322325
Parameters
@@ -332,14 +335,20 @@ def get_image_metadata(
332335
333336
Returns
334337
-------
335-
dict
336-
image plane metadata
338+
dict | None
339+
Image plane metadata. None if not available.
337340
"""
338341
metadata = None
339342
if not self.str_position_axis and isinstance(p, str):
340343
if p.isdigit():
341344
p = int(p)
342-
p, t, c, z = self._check_coordinates(p, t, c, z)
345+
try:
346+
p, t, c, z = self._check_coordinates(p, t, c, z)
347+
except ValueError as e:
348+
_logger.debug(
349+
f"Error checking coordinates at position {p}, time {t}, "
350+
f"channel {c}, z {z}: {e}"
351+
)
343352
if self.dataset.has_image(position=p, time=t, channel=c, z=z):
344353
try:
345354
metadata = self.dataset.read_metadata(

0 commit comments

Comments
 (0)