From 9c957a1d0a8413a177a5fb50493e842e789d7ce3 Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Thu, 15 Jun 2017 15:04:07 +0200 Subject: [PATCH] Only setup certain axes if size > 1 --- nd2reader/reader.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/nd2reader/reader.py b/nd2reader/reader.py index 95b00cf..6a46f11 100644 --- a/nd2reader/reader.py +++ b/nd2reader/reader.py @@ -61,7 +61,11 @@ class ND2Reader(FramesSequenceND): numpy.ndarray: The requested frame """ - c_name = self.metadata["channels"][c] + try: + c_name = self.metadata["channels"][c] + except KeyError: + c_name = self.metadata["channels"][0] + x = self.metadata["width"] if x <= 0 else x y = self.metadata["height"] if y <= 0 else y return self._parser.get_image_by_attributes(t, 0, c_name, z, y, x) @@ -103,9 +107,9 @@ class ND2Reader(FramesSequenceND): """ self._init_axis_if_exists('x', self._get_metadata_property("width", default=0)) self._init_axis_if_exists('y', self._get_metadata_property("height", default=0)) - self._init_axis_if_exists('c', len(self._get_metadata_property("channels", default=[]))) - self._init_axis_if_exists('t', len(self._get_metadata_property("frames", default=[]))) - self._init_axis_if_exists('z', len(self._get_metadata_property("z_levels", default=[]))) + self._init_axis_if_exists('c', len(self._get_metadata_property("channels", default=[])), min_size=2) + self._init_axis_if_exists('t', len(self._get_metadata_property("frames", default=[])), min_size=2) + self._init_axis_if_exists('z', len(self._get_metadata_property("z_levels", default=[])), min_size=2) if len(self.sizes) == 0: raise EmptyFileError("No axes were found for this .nd2 file.") @@ -113,8 +117,8 @@ class ND2Reader(FramesSequenceND): # provide the default self.iter_axes = self._guess_default_iter_axis() - def _init_axis_if_exists(self, axis, size): - if size > 0: + def _init_axis_if_exists(self, axis, size, min_size=1): + if size >= min_size: self._init_axis(axis, size) def _guess_default_iter_axis(self):