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.

79 lines
3.3 KiB

7 years ago
  1. import unittest
  2. import numpy as np
  3. import struct
  4. from pims import Frame
  5. from nd2reader2.artificial import ArtificialND2
  6. from nd2reader2.exceptions import EmptyFileError, InvalidFileType
  7. from nd2reader2.reader import ND2Reader
  8. from nd2reader2.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 cmp_two_readers(self, r1, r2):
  15. attributes = r1.data['image_attributes']['SLxImageAttributes']
  16. self.assertEqual(r2.metadata['width'], attributes['uiWidth'])
  17. self.assertEqual(r2.metadata['height'], attributes['uiHeight'])
  18. self.assertEqual(r2.metadata['width'], r2.sizes['x'])
  19. self.assertEqual(r2.metadata['height'], r2.sizes['y'])
  20. self.assertEqual(r2.pixel_type, np.float64)
  21. self.assertEqual(r2.iter_axes, ['t'])
  22. def test_init_and_init_axes(self):
  23. with ArtificialND2('test_data/test_nd2_reader.nd2') as artificial:
  24. with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
  25. self.cmp_two_readers(artificial, reader)
  26. def test_init_from_handler(self):
  27. with ArtificialND2('test_data/test_nd2_reader.nd2') as artificial:
  28. with open('test_data/test_nd2_reader.nd2', "rb") as FH:
  29. with ND2Reader(FH) as reader:
  30. self.cmp_two_readers(artificial, reader)
  31. def test_init_empty_file(self):
  32. with ArtificialND2('test_data/empty.nd2', skip_blocks=['label_map_marker']):
  33. with self.assertRaises(EmptyFileError) as exception:
  34. with ND2Reader('test_data/empty.nd2'):
  35. pass
  36. self.assertEqual(str(exception.exception), "No axes were found for this .nd2 file.")
  37. def test_get_parser(self):
  38. with ArtificialND2('test_data/test_nd2_reader.nd2') as _:
  39. with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
  40. self.assertIsInstance(reader.parser, Parser)
  41. def test_get_timesteps(self):
  42. with ArtificialND2('test_data/test_nd2_reader.nd2') as _:
  43. with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
  44. timesteps = reader.timesteps
  45. self.assertEquals(len(timesteps), 0)
  46. def test_get_frame_zero(self):
  47. # Best test we can do for now:
  48. # test everything up to the actual unpacking of the frame data
  49. with ArtificialND2('test_data/test_nd2_reader.nd2') as _:
  50. with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
  51. with self.assertRaises(struct.error) as exception:
  52. frame = reader[0]
  53. self.assertIn('unpack', str(exception.exception))
  54. def test_get_frame_2D(self):
  55. # Best test we can do for now:
  56. # test everything up to the actual unpacking of the frame data
  57. with ArtificialND2('test_data/test_nd2_reader.nd2') as _:
  58. with ND2Reader('test_data/test_nd2_reader.nd2') as reader:
  59. with self.assertRaises(struct.error) as exception:
  60. frame = reader.get_frame_2D(c=0, t=0, z=0, x=0, y=0, v=0)
  61. self.assertIn('unpack', str(exception.exception))