From bd10811aee63acf8368945966a1c1b624d41ec3f Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Wed, 1 Mar 2017 14:28:44 +0100 Subject: [PATCH] Add unit tests for opening files and axis sizes --- nd2reader/reader.py | 7 +++---- tests/test_reader.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 tests/test_reader.py diff --git a/nd2reader/reader.py b/nd2reader/reader.py index ba65de4..9386cd0 100644 --- a/nd2reader/reader.py +++ b/nd2reader/reader.py @@ -6,7 +6,6 @@ class ND2Reader(FramesSequenceND): """ PIMS wrapper for the ND2 parser """ - def __init__(self, filename): self.filename = filename @@ -45,7 +44,8 @@ class ND2Reader(FramesSequenceND): :param i: :return: """ - return self._parser.get_image(i) + c_name = self.metadata["channels"][0] + return self._parser.get_image_by_attributes(i, 0, c_name, 0, self.metadata["height"], self.metadata["width"]) def get_frame_2D(self, c, t, z): """ @@ -56,8 +56,7 @@ class ND2Reader(FramesSequenceND): :return: """ c_name = self.metadata["channels"][c] - - return self._parser.get_image_by_attributes(t, 0, c_name, z, self.metadata["width"], self.metadata["height"]) + return self._parser.get_image_by_attributes(t, 0, c_name, z, self.metadata["height"], self.metadata["width"]) @property def pixel_type(self): diff --git a/tests/test_reader.py b/tests/test_reader.py new file mode 100644 index 0000000..6e08aae --- /dev/null +++ b/tests/test_reader.py @@ -0,0 +1,42 @@ +import unittest + +from nd2reader.reader import ND2Reader + + +class TestReader(unittest.TestCase): + + def setUp(self): + self.files = [ + './test_data/data001.nd2', + './test_data/data002.nd2' + ] + + def test_sizes_data_001(self): + with ND2Reader(self.files[0]) as reader: + self.assertEqual(reader.sizes['x'], 128) + self.assertEqual(reader.sizes['y'], 128) + self.assertEqual(reader.sizes['t'], 982) + self.assertEqual(reader.sizes['c'], 1) + self.assertEqual(reader.sizes['z'], 1) + + def test_frame_size_data_001(self): + with ND2Reader(self.files[0]) as reader: + frame = reader[13] + self.assertEqual(frame.shape[0], 128) + self.assertEqual(frame.shape[1], 128) + + def test_sizes_data_002(self): + with ND2Reader(self.files[1]) as reader: + self.assertEqual(reader.sizes['x'], 512) + self.assertEqual(reader.sizes['y'], 256) + self.assertEqual(reader.sizes['t'], 78) + self.assertEqual(reader.sizes['c'], 1) + self.assertEqual(reader.sizes['z'], 1) + + def test_frame_size_data_002(self): + with ND2Reader(self.files[1]) as reader: + frame = reader[13] + self.assertEqual(frame.shape[1], 512) + self.assertEqual(frame.shape[0], 256) + +