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.

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