Browse Source

Test legacy Nd2, parser, reader fixes

master
Ruben Verweij 8 years ago
parent
commit
abeb67b39e
5 changed files with 73 additions and 24 deletions
  1. +7
    -9
      nd2reader/raw_metadata.py
  2. +1
    -0
      nd2reader/reader.py
  3. +38
    -0
      tests/test_legacy.py
  4. +5
    -5
      tests/test_parser.py
  5. +22
    -10
      tests/test_reader.py

+ 7
- 9
nd2reader/raw_metadata.py View File

@ -321,23 +321,21 @@ class RawMetadata(object):
"""Parse the metadata of the ND experiment
"""
self._metadata_parsed['experiment'] = {
'description': 'unknown',
'loops': []
}
if self.image_metadata is None or six.b('SLxExperiment') not in self.image_metadata:
return
raw_data = self.image_metadata[six.b('SLxExperiment')]
experimental_data = {
'description': 'unknown',
'loops': []
}
if six.b('wsApplicationDesc') in raw_data:
experimental_data['description'] = raw_data[six.b('wsApplicationDesc')].decode('utf8')
self._metadata_parsed['experiment']['description'] = raw_data[six.b('wsApplicationDesc')].decode('utf8')
if six.b('uLoopPars') in raw_data:
experimental_data['loops'] = self._parse_loop_data(raw_data[six.b('uLoopPars')])
self._metadata_parsed['experiment'] = experimental_data
self._metadata_parsed['experiment']['loops'] = self._parse_loop_data(raw_data[six.b('uLoopPars')])
@staticmethod
def _get_loops_from_data(loop_data):


+ 1
- 0
nd2reader/reader.py View File

@ -180,6 +180,7 @@ class ND2Reader(FramesSequenceND):
timesteps = np.array([])
current_time = 0.0
for loop in self.metadata['experiment']['loops']:
if loop['stimulation']:
continue


+ 38
- 0
tests/test_legacy.py View File

@ -0,0 +1,38 @@
import unittest
import warnings
from nd2reader.legacy import Nd2
from nd2reader.reader import ND2Reader
from nd2reader.artificial import ArtificialND2
class TestLegacy(unittest.TestCase):
def test_init(self):
with ArtificialND2('test_data/legacy.nd2'):
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
with Nd2('test_data/legacy.nd2') as reader:
self.assertIsInstance(reader.reader, ND2Reader)
self.assertTrue(issubclass(w[0].category, DeprecationWarning))
self.assertEquals(str(w[0].message), "The 'Nd2' class is deprecated, please consider using the new" +
" ND2Reader interface which uses pims.")
def test_misc(self):
with ArtificialND2('test_data/legacy.nd2'):
with Nd2('test_data/legacy.nd2') as reader:
representation = "\n".join(["<Deprecated ND2 %s>" % reader.reader.filename,
"Created: Unknown",
"Image size: %sx%s (HxW)" % (reader.height, reader.width),
"Frames: %s" % len(reader.frames),
"Channels: %s" % ", ".join(["%s" % str(channel) for channel
in reader.channels]),
"Fields of View: %s" % len(reader.fields_of_view),
"Z-Levels: %s" % len(reader.z_levels)
])
self.assertEquals(representation, str(reader))
# not implemented yet
self.assertEquals(reader.pixel_microns, None)
self.assertEquals(len(reader), 1)

+ 5
- 5
tests/test_parser.py View File

@ -25,8 +25,8 @@ class TestParser(unittest.TestCase):
self.assertTrue(parser.supported)
def test_cannot_open_wrong_version(self):
with ArtificialND2(self.test_file, version=('a', 'b')) as artificial:
artificial.close()
with open(self.test_file, 'rb') as fh:
self.assertRaises(InvalidVersionError, Parser, fh)
with ArtificialND2(self.test_file, version=('0', '0')) as _:
with open(self.test_file, 'rb') as fh:
with self.assertRaises(InvalidVersionError) as exception:
Parser(fh)
self.assertEqual(str(exception.exception), "No parser is available for that version.")

+ 22
- 10
tests/test_reader.py View File

@ -4,6 +4,7 @@ import numpy as np
from nd2reader.artificial import ArtificialND2
from nd2reader.exceptions import EmptyFileError
from nd2reader.reader import ND2Reader
from nd2reader.parser import Parser
class TestReader(unittest.TestCase):
@ -12,20 +13,31 @@ class TestReader(unittest.TestCase):
def test_init_and_init_axes(self):
with ArtificialND2('test_data/test_nd2_reader.nd2') as artificial:
reader = ND2Reader('test_data/test_nd2_reader.nd2')
with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
attributes = artificial.data['image_attributes']['SLxImageAttributes']
self.assertEqual(reader.metadata['width'], attributes['uiWidth'])
self.assertEqual(reader.metadata['height'], attributes['uiHeight'])
attributes = artificial.data['image_attributes']['SLxImageAttributes']
self.assertEqual(reader.metadata['width'], attributes['uiWidth'])
self.assertEqual(reader.metadata['height'], attributes['uiHeight'])
self.assertEqual(reader.metadata['width'], reader.sizes['x'])
self.assertEqual(reader.metadata['height'], reader.sizes['y'])
self.assertEqual(reader.metadata['width'], reader.sizes['x'])
self.assertEqual(reader.metadata['height'], reader.sizes['y'])
self.assertEqual(reader._dtype, np.float64)
self.assertEqual(reader.iter_axes, ['t'])
self.assertEqual(reader.pixel_type, np.float64)
self.assertEqual(reader.iter_axes, ['t'])
def test_init_empty_file(self):
with ArtificialND2('test_data/empty.nd2', skip_blocks=['label_map_marker']):
with self.assertRaises(EmptyFileError) as exception:
ND2Reader('test_data/empty.nd2')
with ND2Reader('test_data/empty.nd2'):
pass
self.assertEqual(str(exception.exception), "No axes were found for this .nd2 file.")
def test_get_parser(self):
with ArtificialND2('test_data/test_nd2_reader.nd2') as _:
with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
self.assertIsInstance(reader.parser, Parser)
def test_get_timesteps(self):
with ArtificialND2('test_data/test_nd2_reader.nd2') as _:
with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
timesteps = reader.timesteps
self.assertEquals(list(timesteps), [])

Loading…
Cancel
Save