From b0f22eb83b3111ddc23de88deb1e0799c62e043d Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Wed, 15 Mar 2017 16:59:33 +0100 Subject: [PATCH] Very basic Reader test --- nd2reader/reader.py | 22 +++++++++++++++++----- tests/test_reader.py | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/nd2reader/reader.py b/nd2reader/reader.py index 24a102e..d48d157 100644 --- a/nd2reader/reader.py +++ b/nd2reader/reader.py @@ -108,15 +108,27 @@ class ND2Reader(FramesSequenceND): """ return self._dtype + def _get_metadata_property(self, key, default=None): + if self.metadata is None: + return default + + if key not in self.metadata: + return default + + if self.metadata[key] is None: + return default + + return self.metadata[key] + def _setup_axes(self): """Setup the xyctz axes, iterate over t axis by default """ - self._init_axis('x', self.metadata["width"]) - self._init_axis('y', self.metadata["height"]) - self._init_axis('c', len(self.metadata["channels"])) - self._init_axis('t', len(self.metadata["frames"])) - self._init_axis('z', len(self.metadata["z_levels"])) + self._init_axis('x', self._get_metadata_property("width", default=0)) + self._init_axis('y', self._get_metadata_property("height", default=0)) + self._init_axis('c', len(self._get_metadata_property("channels", default=[]))) + self._init_axis('t', len(self._get_metadata_property("frames", default=[]))) + self._init_axis('z', len(self._get_metadata_property("z_levels", default=[]))) # provide the default self.iter_axes = 't' diff --git a/tests/test_reader.py b/tests/test_reader.py index e183096..73fa31e 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -1,8 +1,30 @@ import unittest +from os import path +import numpy as np +from nd2reader.artificial import ArtificialND2 from nd2reader.reader import ND2Reader class TestReader(unittest.TestCase): + def create_test_nd2(self): + with ArtificialND2(self.test_file) as artificial: + artificial.close() + + def setUp(self): + dir_path = path.dirname(path.realpath(__file__)) + self.test_file = path.join(dir_path, 'test_data/test.nd2') + + def test_can_open_test_file(self): + self.create_test_nd2() + with ND2Reader(self.test_file) as reader: + self.assertEqual(reader.filename, self.test_file) + self.assertEqual(reader.pixel_type, np.float64) + self.assertEqual(reader.sizes['x'], 0) + self.assertEqual(reader.sizes['y'], 0) + self.assertEqual(reader.sizes['z'], 1) + self.assertEqual(reader.sizes['c'], 0) + self.assertEqual(reader.sizes['t'], 1) + def test_extension(self): self.assertTrue('nd2' in ND2Reader.class_exts())