You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
3.0 KiB

8 years ago
  1. import unittest
  2. import numpy as np
  3. import struct
  4. from pims import Frame
  5. from nd2reader.artificial import ArtificialND2
  6. from nd2reader.exceptions import EmptyFileError, InvalidFileType
  7. from nd2reader.reader import ND2Reader
  8. from nd2reader.parser import Parser
  9. class TestReader(unittest.TestCase):
  10. def test_invalid_file_extension(self):
  11. self.assertRaises(InvalidFileType, lambda: ND2Reader('test_data/invalid_extension_file.inv'))
  12. def test_extension(self):
  13. self.assertTrue('nd2' in ND2Reader.class_exts())
  14. def test_init_and_init_axes(self):
  15. with ArtificialND2('test_data/test_nd2_reader.nd2') as artificial:
  16. with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
  17. attributes = artificial.data['image_attributes']['SLxImageAttributes']
  18. self.assertEqual(reader.metadata['width'], attributes['uiWidth'])
  19. self.assertEqual(reader.metadata['height'], attributes['uiHeight'])
  20. self.assertEqual(reader.metadata['width'], reader.sizes['x'])
  21. self.assertEqual(reader.metadata['height'], reader.sizes['y'])
  22. self.assertEqual(reader.pixel_type, np.float64)
  23. self.assertEqual(reader.iter_axes, ['t'])
  24. def test_init_empty_file(self):
  25. with ArtificialND2('test_data/empty.nd2', skip_blocks=['label_map_marker']):
  26. with self.assertRaises(EmptyFileError) as exception:
  27. with ND2Reader('test_data/empty.nd2'):
  28. pass
  29. self.assertEqual(str(exception.exception), "No axes were found for this .nd2 file.")
  30. def test_get_parser(self):
  31. with ArtificialND2('test_data/test_nd2_reader.nd2') as _:
  32. with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
  33. self.assertIsInstance(reader.parser, Parser)
  34. def test_get_timesteps(self):
  35. with ArtificialND2('test_data/test_nd2_reader.nd2') as _:
  36. with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
  37. timesteps = reader.timesteps
  38. self.assertEquals(len(timesteps), 0)
  39. def test_get_frame_zero(self):
  40. # Best test we can do for now:
  41. # test everything up to the actual unpacking of the frame data
  42. with ArtificialND2('test_data/test_nd2_reader.nd2') as _:
  43. with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
  44. with self.assertRaises(struct.error) as exception:
  45. frame = reader[0]
  46. self.assertIn('unpack', str(exception.exception))
  47. def test_get_frame_2D(self):
  48. # Best test we can do for now:
  49. # test everything up to the actual unpacking of the frame data
  50. with ArtificialND2('test_data/test_nd2_reader.nd2') as _:
  51. with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
  52. with self.assertRaises(struct.error) as exception:
  53. frame = reader.get_frame_2D(c=0, t=0, z=0, x=0, y=0, v=0)
  54. self.assertIn('unpack', str(exception.exception))