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.

164 lines
6.6 KiB

  1. """
  2. These tests require that you have a specific ND2 file created by the developer of nd2reader. You will never need to
  3. run them unless you're Jim Rybarski.
  4. """
  5. from nd2reader import Nd2
  6. import numpy as np
  7. from datetime import datetime
  8. import unittest
  9. class FunctionalTests(unittest.TestCase):
  10. def setUp(self):
  11. self.nd2 = Nd2("/var/nd2s/FYLM-141111-001.nd2")
  12. def tearDown(self):
  13. self.nd2.close()
  14. def test_shape(self):
  15. self.assertEqual(self.nd2.height, 1280)
  16. self.assertEqual(self.nd2.width, 800)
  17. def test_date(self):
  18. self.assertEqual(self.nd2.date, datetime(2014, 11, 11, 15, 59, 19))
  19. # def test_length(self):
  20. # # This will fail until we address issue #59
  21. # self.assertEqual(len(self.nd2), 17808)
  22. def test_frames(self):
  23. self.assertEqual(len(self.nd2.frames), 636)
  24. def test_fovs(self):
  25. self.assertEqual(len(self.nd2.fields_of_view), 8)
  26. def test_channels(self):
  27. self.assertTupleEqual(tuple(sorted(self.nd2.channels)), ('BF', 'GFP'))
  28. def test_z_levels(self):
  29. self.assertTupleEqual(tuple(self.nd2.z_levels), (0, 1, 2))
  30. def test_image(self):
  31. image = self.nd2[14]
  32. self.assertEqual(image.field_of_view, 2)
  33. self.assertEqual(image.frame_number, 0)
  34. self.assertAlmostEqual(image.timestamp, 19.0340758)
  35. self.assertEqual(image.channel, 'BF')
  36. self.assertEqual(image.z_level, 1)
  37. self.assertEqual(image.height, self.nd2.height)
  38. self.assertEqual(image.width, self.nd2.width)
  39. def test_last_image(self):
  40. image = self.nd2[30526]
  41. self.assertEqual(image.frame_number, 635)
  42. def test_bad_image(self):
  43. image = self.nd2[13]
  44. self.assertIsNone(image)
  45. def test_iteration(self):
  46. images = [image for image in self.nd2[:10]]
  47. self.assertEqual(len(images), 10)
  48. def test_iteration_step(self):
  49. images = [image for image in self.nd2[:10:2]]
  50. self.assertEqual(len(images), 5)
  51. def test_iteration_backwards(self):
  52. images = [image for image in self.nd2[:10:-1]]
  53. self.assertEqual(len(images), 10)
  54. def test_get_image_by_attribute_ok(self):
  55. image = self.nd2.get_image(4, 0, "GFP", 1)
  56. self.assertIsNotNone(image)
  57. image = self.nd2.get_image(4, 0, "BF", 0)
  58. self.assertIsNotNone(image)
  59. image = self.nd2.get_image(4, 0, "BF", 1)
  60. self.assertIsNotNone(image)
  61. def test_images(self):
  62. self.assertTupleEqual((self.nd2[0].z_level, self.nd2[0].channel), (0, 'BF'))
  63. self.assertIsNone(self.nd2[1])
  64. self.assertTupleEqual((self.nd2[2].z_level, self.nd2[2].channel), (1, 'BF'))
  65. self.assertTupleEqual((self.nd2[3].z_level, self.nd2[3].channel), (1, 'GFP'))
  66. self.assertTupleEqual((self.nd2[4].z_level, self.nd2[4].channel), (2, 'BF'))
  67. self.assertIsNone(self.nd2[5])
  68. self.assertTupleEqual((self.nd2[6].z_level, self.nd2[6].channel), (0, 'BF'))
  69. self.assertIsNone(self.nd2[7])
  70. self.assertTupleEqual((self.nd2[8].z_level, self.nd2[8].channel), (1, 'BF'))
  71. self.assertTupleEqual((self.nd2[9].z_level, self.nd2[9].channel), (1, 'GFP'))
  72. self.assertTupleEqual((self.nd2[10].z_level, self.nd2[10].channel), (2, 'BF'))
  73. self.assertIsNone(self.nd2[11])
  74. self.assertTupleEqual((self.nd2[12].z_level, self.nd2[12].channel), (0, 'BF'))
  75. self.assertIsNone(self.nd2[13])
  76. self.assertTupleEqual((self.nd2[14].z_level, self.nd2[14].channel), (1, 'BF'))
  77. self.assertTupleEqual((self.nd2[15].z_level, self.nd2[15].channel), (1, 'GFP'))
  78. self.assertTupleEqual((self.nd2[16].z_level, self.nd2[16].channel), (2, 'BF'))
  79. self.assertIsNone(self.nd2[17])
  80. self.assertTupleEqual((self.nd2[18].z_level, self.nd2[18].channel), (0, 'BF'))
  81. self.assertIsNone(self.nd2[19])
  82. self.assertIsNone(self.nd2[47])
  83. self.assertTupleEqual((self.nd2[48].z_level, self.nd2[48].channel), (0, 'BF'))
  84. self.assertIsNone(self.nd2[49])
  85. self.assertTupleEqual((self.nd2[50].z_level, self.nd2[50].channel), (1, 'BF'))
  86. self.assertIsNone(self.nd2[51])
  87. self.assertTupleEqual((self.nd2[52].z_level, self.nd2[52].channel), (2, 'BF'))
  88. self.assertIsNone(self.nd2[53])
  89. self.assertTupleEqual((self.nd2[54].z_level, self.nd2[54].channel), (0, 'BF'))
  90. def test_get_image_by_attribute_none(self):
  91. # Should handle missing images without an exception
  92. image = self.nd2.get_image(4, 0, "GFP", 0)
  93. self.assertIsNone(image)
  94. def test_index(self):
  95. # Do indexes get added to images properly?
  96. for n, image in enumerate(self.nd2):
  97. if image is not None:
  98. self.assertEqual(n, image.index)
  99. if n > 50:
  100. break
  101. def test_filter(self):
  102. # If we take the first 20 GFP images, they should be identical to the first 20 items iterated from filter()
  103. # if we set our criteria to just "GFP"
  104. manual_images = []
  105. for _, image in zip(range(20), self.nd2):
  106. if image is not None and image.channel == 'GFP':
  107. manual_images.append(image)
  108. filter_images = []
  109. for image in self.nd2.filter(channels='GFP'):
  110. filter_images.append(image)
  111. if len(filter_images) == len(manual_images):
  112. break
  113. for a, b in zip(manual_images, filter_images):
  114. self.assertTrue(np.array_equal(a, b))
  115. def test_filter_order_all(self):
  116. # If we select every possible image using filter(), we should just get every image in order
  117. n = 0
  118. for image in self.nd2.filter(channels=['BF', 'GFP'], z_levels=[0, 1, 2], fields_of_view=list(range(8))):
  119. while True:
  120. indexed_image = self.nd2[n]
  121. if indexed_image is not None:
  122. break
  123. n += 1
  124. self.assertTrue(np.array_equal(image, indexed_image))
  125. n += 1
  126. if n > 100:
  127. break
  128. def test_filter_order_subset(self):
  129. # Test that images are always yielded in increasing order. This guarantees that no matter what subset of images
  130. # we're filtering, we still get them in the chronological order they were acquired
  131. n = -1
  132. for image in self.nd2.filter(channels='BF', z_levels=[0, 1], fields_of_view=[1, 2, 4]):
  133. self.assertGreater(image.index, n)
  134. self.assertEqual(image.channel, 'BF')
  135. self.assertIn(image.field_of_view, (1, 2, 4))
  136. self.assertIn(image.z_level, (0, 1))
  137. n = image.index
  138. if n > 100:
  139. break