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.

81 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
5 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_parse_z_levels(self):
  27. # smokescreen test to check if the fallback to z_coordinates is working
  28. # for details, see RawMetadata._parse_z_levels()
  29. dimension_text = self.metadata._parse_dimension_text()
  30. z_levels = self.metadata._parse_dimension(r""".*?Z\((\d+)\).*?""", dimension_text)
  31. z_coords = self.metadata._parse_z_coordinates()
  32. self.assertEqual(len(dimension_text), 0)
  33. self.assertEqual(len(z_levels), 0)
  34. self.assertEqual(len(self.metadata._parse_z_levels()), len(z_coords))
  35. def test_dict(self):
  36. self.assertTrue(type(self.metadata.__dict__) is dict)
  37. def test_parsed_metadata_has_all_keys(self):
  38. metadata = self.metadata.get_parsed_metadata()
  39. self.assertTrue(type(metadata) is dict)
  40. required_keys = ["height", "width", "date", "fields_of_view", "frames", "z_levels", "total_images_per_channel",
  41. "channels", "pixel_microns"]
  42. for required in required_keys:
  43. self.assertTrue(required in metadata)
  44. def test_cached_metadata(self):
  45. metadata_one = self.metadata.get_parsed_metadata()
  46. metadata_two = self.metadata.get_parsed_metadata()
  47. self.assertEqual(metadata_one, metadata_two)
  48. def test_pfs_status(self):
  49. self.assertEqual(self.file_data['pfs_status'], self.metadata.pfs_status[0])
  50. def _assert_dicts_equal(self, parsed_dict, original_dict):
  51. for attribute in original_dict.keys():
  52. parsed_key = six.b(attribute)
  53. self.assertIn(parsed_key, parsed_dict.keys())
  54. if isinstance(parsed_dict[parsed_key], dict):
  55. self._assert_dicts_equal(parsed_dict[parsed_key], original_dict[attribute])
  56. else:
  57. self.assertEqual(parsed_dict[parsed_key], original_dict[attribute])
  58. def test_image_attributes(self):
  59. parsed_dict = self.metadata.image_attributes
  60. self._assert_dicts_equal(parsed_dict, self.file_data['image_attributes'])
  61. def test_color_channels(self):
  62. parsed_channels = self.metadata.get_parsed_metadata()['channels']
  63. self.assertEquals(parsed_channels, ['TRITC'])