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

6 years ago
6 years ago
6 years ago
6 years ago
  1. import unittest
  2. import six
  3. from nd2reader.artificial import ArtificialND2
  4. from nd2reader.label_map import LabelMap
  5. from nd2reader.raw_metadata import RawMetadata
  6. from nd2reader.common_raw_metadata import parse_roi_shape, parse_roi_type, parse_dimension_text_line
  7. class TestRawMetadata(unittest.TestCase):
  8. def setUp(self):
  9. self.nd2 = ArtificialND2('test_data/test_nd2_raw_metadata001.nd2')
  10. self.raw_text, self.locations, self.file_data = self.nd2.raw_text, self.nd2.locations, self.nd2.data
  11. self.label_map = LabelMap(self.raw_text)
  12. self.metadata = RawMetadata(self.nd2.file_handle, self.label_map)
  13. def test_parse_roi_shape(self):
  14. self.assertEqual(parse_roi_shape(3), 'rectangle')
  15. self.assertEqual(parse_roi_shape(9), 'circle')
  16. self.assertIsNone(parse_roi_shape(-1))
  17. def test_parse_roi_type(self):
  18. self.assertEqual(parse_roi_type(3), 'reference')
  19. self.assertEqual(parse_roi_type(2), 'background')
  20. self.assertEqual(parse_roi_type(4), 'stimulation')
  21. self.assertIsNone(parse_roi_type(-1))
  22. def test_parse_dimension_text(self):
  23. line = six.b('Metadata:\r\nDimensions: T(443) x \xce\xbb(1)\r\nCamera Name: Andor Zyla VSC-01537')
  24. self.assertEqual(parse_dimension_text_line(line), six.b('Dimensions: T(443) x \xce\xbb(1)'))
  25. self.assertIsNone(parse_dimension_text_line(six.b('Dim: nothing')))
  26. def test_dict(self):
  27. self.assertTrue(type(self.metadata.__dict__) is dict)
  28. def test_parsed_metadata_has_all_keys(self):
  29. metadata = self.metadata.get_parsed_metadata()
  30. self.assertTrue(type(metadata) is dict)
  31. required_keys = ["height", "width", "date", "fields_of_view", "frames", "z_levels", "total_images_per_channel",
  32. "channels", "pixel_microns"]
  33. for required in required_keys:
  34. self.assertTrue(required in metadata)
  35. def test_cached_metadata(self):
  36. metadata_one = self.metadata.get_parsed_metadata()
  37. metadata_two = self.metadata.get_parsed_metadata()
  38. self.assertEqual(metadata_one, metadata_two)
  39. def test_pfs_status(self):
  40. self.assertEqual(self.file_data['pfs_status'], self.metadata.pfs_status[0])
  41. def _assert_dicts_equal(self, parsed_dict, original_dict):
  42. for attribute in original_dict.keys():
  43. parsed_key = six.b(attribute)
  44. self.assertIn(parsed_key, parsed_dict.keys())
  45. if isinstance(parsed_dict[parsed_key], dict):
  46. self._assert_dicts_equal(parsed_dict[parsed_key], original_dict[attribute])
  47. else:
  48. self.assertEqual(parsed_dict[parsed_key], original_dict[attribute])
  49. def test_image_attributes(self):
  50. parsed_dict = self.metadata.image_attributes
  51. self._assert_dicts_equal(parsed_dict, self.file_data['image_attributes'])
  52. def test_color_channels(self):
  53. parsed_channels = self.metadata.get_parsed_metadata()['channels']
  54. self.assertEquals(parsed_channels, ['TRITC'])