From 343a39dd9dcd62495d1afedb79ba5f7db8244047 Mon Sep 17 00:00:00 2001 From: Jim Rybarski Date: Sat, 12 Dec 2015 21:52:17 -0600 Subject: [PATCH] fixes #133. The index assigned to images produced by `select` was wrong. --- functional_tests/FYLM141111001.py | 5 +++++ nd2reader/driver/v3.py | 10 +++++++--- nd2reader/interface.py | 11 +++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/functional_tests/FYLM141111001.py b/functional_tests/FYLM141111001.py index ffffbdb..cc3b410 100644 --- a/functional_tests/FYLM141111001.py +++ b/functional_tests/FYLM141111001.py @@ -133,8 +133,13 @@ class FunctionalTests(unittest.TestCase): filter_images.append(image) if len(filter_images) == len(manual_images): break + self.assertEqual(len(manual_images), len(filter_images)) + self.assertGreater(len(manual_images), 0) for a, b in zip(manual_images, filter_images): self.assertTrue(np.array_equal(a, b)) + self.assertEqual(a.index, b.index) + self.assertEqual(a.field_of_view, b.field_of_view) + self.assertEqual(a.channel, b.channel) def test_filter_order_all(self): # If we select every possible image using select(), we should just get every image in order diff --git a/nd2reader/driver/v3.py b/nd2reader/driver/v3.py index 3f3ce99..dbb22d3 100644 --- a/nd2reader/driver/v3.py +++ b/nd2reader/driver/v3.py @@ -24,6 +24,12 @@ class V3Driver(object): self._label_map = label_map self._file_handle = file_handle + def calculate_image_properties(self, index): + field_of_view = self._calculate_field_of_view(index) + channel = self._calculate_channel(index) + z_level = self._calculate_z_level(index) + return field_of_view, channel, z_level + def get_image(self, index): """ Creates an Image object and adds its metadata, based on the index (which is simply the order in which the image was acquired). May return None if the ND2 contains @@ -35,10 +41,8 @@ class V3Driver(object): :rtype: Image or None """ + field_of_view, channel, z_level = self.calculate_image_properties(index) channel_offset = index % len(self._metadata.channels) - field_of_view = self._calculate_field_of_view(index) - channel = self._calculate_channel(index) - z_level = self._calculate_z_level(index) image_group_number = int(index / len(self._metadata.channels)) frame_number = self._calculate_frame_number(image_group_number, field_of_view, z_level) try: diff --git a/nd2reader/interface.py b/nd2reader/interface.py index c67353f..d47cd63 100644 --- a/nd2reader/interface.py +++ b/nd2reader/interface.py @@ -77,12 +77,11 @@ class Nd2(object): z_levels = self._to_list(z_levels, self.z_levels) for frame in self.frames: - for f in fields_of_view: - for z in z_levels: - for c in channels: - image = self.get_image(frame, f, c, z) - if image is not None: - yield image + field_of_view, channel, z_level = self._parser.driver.calculate_image_properties(frame) + if field_of_view in fields_of_view and channel in channels and z_level in z_levels: + image = self._parser.driver.get_image(frame) + if image is not None: + yield image @property def height(self):