From 05428dfa0e7458fab273ff3d9f6202fd45eb5616 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Wed, 11 Dec 2024 09:16:39 -0800 Subject: [PATCH] More robust calculation of "z-step_um" for ndtiff datasets (#263) use try/except when calculating pm_metadata["z-step_um"] --- iohub/ndtiff.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/iohub/ndtiff.py b/iohub/ndtiff.py index 23d72e6..904e644 100644 --- a/iohub/ndtiff.py +++ b/iohub/ndtiff.py @@ -140,19 +140,20 @@ def _get_summary_metadata(self): c_idx = self._ndtiff_channel_names[0] img_metadata = self.get_image_metadata(p_idx, 0, c_idx, 0) - pm_metadata["z-step_um"] = None - if "ZPosition_um_Intended" in img_metadata.keys(): + try: + z0 = self.get_image_metadata(p_idx, 0, c_idx, 0)[ + "ZPosition_um_Intended" + ] + z1 = self.get_image_metadata(p_idx, 0, c_idx, 1)[ + "ZPosition_um_Intended" + ] pm_metadata["z-step_um"] = np.around( - abs( - self.get_image_metadata(p_idx, 0, c_idx, 1)[ - "ZPosition_um_Intended" - ] - - self.get_image_metadata(p_idx, 0, c_idx, 0)[ - "ZPosition_um_Intended" - ] - ), - decimals=3, + abs(z1 - z0), decimals=3 ).astype(float) + # Will raise KeyError if dataset does not have z slices + # Will raise ValueError if dataset has only one z slice + except (KeyError, ValueError): + pm_metadata["z-step_um"] = None pm_metadata["StagePositions"] = [] if "position" in self._axes: