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.

218 lines
8.3 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  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 time
  10. class FYLM141111Tests(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. @unittest.skip("This will fail until we address issue #59")
  21. def test_length(self):
  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)), ('', 'GFP'))
  29. def test_z_levels(self):
  30. self.assertTupleEqual(tuple(self.nd2.z_levels), (0, 1, 2))
  31. def test_pixel_size(self):
  32. self.assertGreater(self.nd2.pixel_microns, 0.0)
  33. def test_image(self):
  34. image = self.nd2[14]
  35. self.assertEqual(image.field_of_view, 2)
  36. self.assertEqual(image.frame_number, 0)
  37. self.assertAlmostEqual(image.timestamp, 19.0340758)
  38. self.assertEqual(image.channel, '')
  39. self.assertEqual(image.z_level, 1)
  40. self.assertEqual(image.height, self.nd2.height)
  41. self.assertEqual(image.width, self.nd2.width)
  42. def test_last_image(self):
  43. image = self.nd2[30526]
  44. self.assertEqual(image.frame_number, 635)
  45. def test_bad_image(self):
  46. image = self.nd2[13]
  47. self.assertIsNone(image)
  48. def test_iteration(self):
  49. images = [image for image in self.nd2[:10]]
  50. self.assertEqual(len(images), 10)
  51. def test_iteration_step(self):
  52. images = [image for image in self.nd2[:10:2]]
  53. self.assertEqual(len(images), 5)
  54. def test_iteration_backwards(self):
  55. images = [image for image in self.nd2[:10:-1]]
  56. self.assertEqual(len(images), 10)
  57. def test_get_image_by_attribute_ok(self):
  58. image = self.nd2.get_image(4, 0, 'GFP', 1)
  59. self.assertIsNotNone(image)
  60. image = self.nd2.get_image(4, 0, '', 0)
  61. self.assertIsNotNone(image)
  62. image = self.nd2.get_image(4, 0, '', 1)
  63. self.assertIsNotNone(image)
  64. def test_images(self):
  65. self.assertTupleEqual((self.nd2[0].z_level, self.nd2[0].channel), (0, ''))
  66. self.assertIsNone(self.nd2[1])
  67. self.assertTupleEqual((self.nd2[2].z_level, self.nd2[2].channel), (1, ''))
  68. self.assertTupleEqual((self.nd2[3].z_level, self.nd2[3].channel), (1, 'GFP'))
  69. self.assertTupleEqual((self.nd2[4].z_level, self.nd2[4].channel), (2, ''))
  70. self.assertIsNone(self.nd2[5])
  71. self.assertTupleEqual((self.nd2[6].z_level, self.nd2[6].channel), (0, ''))
  72. self.assertIsNone(self.nd2[7])
  73. self.assertTupleEqual((self.nd2[8].z_level, self.nd2[8].channel), (1, ''))
  74. self.assertTupleEqual((self.nd2[9].z_level, self.nd2[9].channel), (1, 'GFP'))
  75. self.assertTupleEqual((self.nd2[10].z_level, self.nd2[10].channel), (2, ''))
  76. self.assertIsNone(self.nd2[11])
  77. self.assertTupleEqual((self.nd2[12].z_level, self.nd2[12].channel), (0, ''))
  78. self.assertIsNone(self.nd2[13])
  79. self.assertTupleEqual((self.nd2[14].z_level, self.nd2[14].channel), (1, ''))
  80. self.assertTupleEqual((self.nd2[15].z_level, self.nd2[15].channel), (1, 'GFP'))
  81. self.assertTupleEqual((self.nd2[16].z_level, self.nd2[16].channel), (2, ''))
  82. self.assertIsNone(self.nd2[17])
  83. self.assertTupleEqual((self.nd2[18].z_level, self.nd2[18].channel), (0, ''))
  84. self.assertIsNone(self.nd2[19])
  85. self.assertIsNone(self.nd2[47])
  86. self.assertTupleEqual((self.nd2[48].z_level, self.nd2[48].channel), (0, ''))
  87. self.assertIsNone(self.nd2[49])
  88. self.assertTupleEqual((self.nd2[50].z_level, self.nd2[50].channel), (1, ''))
  89. self.assertIsNone(self.nd2[51])
  90. self.assertTupleEqual((self.nd2[52].z_level, self.nd2[52].channel), (2, ''))
  91. self.assertIsNone(self.nd2[53])
  92. self.assertTupleEqual((self.nd2[54].z_level, self.nd2[54].channel), (0, ''))
  93. def test_get_image_by_attribute_none(self):
  94. # Should handle missing images without an exception
  95. image = self.nd2.get_image(4, 0, 'GFP', 0)
  96. self.assertIsNone(image)
  97. def test_index(self):
  98. # Do indexes get added to images properly?
  99. for n, image in enumerate(self.nd2):
  100. if image is not None:
  101. self.assertEqual(n, image.index)
  102. if n > 50:
  103. break
  104. def test_select(self):
  105. # If we take the first 20 GFP images, they should be identical to the first 20 items iterated from select()
  106. # if we set our criteria to just 'GFP'
  107. manual_images = []
  108. for _, image in zip(range(20), self.nd2):
  109. if image is not None and image.channel == 'GFP':
  110. manual_images.append(image)
  111. filter_images = []
  112. for image in self.nd2.select(channels='GFP'):
  113. filter_images.append(image)
  114. if len(filter_images) == len(manual_images):
  115. break
  116. self.assertEqual(len(manual_images), len(filter_images))
  117. self.assertGreater(len(manual_images), 0)
  118. for a, b in zip(manual_images, filter_images):
  119. self.assertTrue(np.array_equal(a, b))
  120. self.assertEqual(a.index, b.index)
  121. self.assertEqual(a.field_of_view, b.field_of_view)
  122. self.assertEqual(a.channel, b.channel)
  123. def test_select_order_all(self):
  124. # If we select every possible image using select(), we should just get every image in order
  125. n = 0
  126. for image in self.nd2.select(channels=['', 'GFP'], z_levels=[0, 1, 2], fields_of_view=list(range(8))):
  127. while True:
  128. indexed_image = self.nd2[n]
  129. if indexed_image is not None:
  130. break
  131. n += 1
  132. self.assertTrue(np.array_equal(image, indexed_image))
  133. n += 1
  134. if n > 100:
  135. break
  136. def test_select_order_subset(self):
  137. # Test that images are always yielded in increasing order. This guarantees that no matter what subset of images
  138. # we're filtering, we still get them in the chronological order they were acquired
  139. n = -1
  140. for image in self.nd2.select(channels='', z_levels=[0, 1], fields_of_view=[1, 2, 4]):
  141. self.assertGreater(image.index, n)
  142. self.assertEqual(image.channel, '')
  143. self.assertIn(image.field_of_view, (1, 2, 4))
  144. self.assertIn(image.z_level, (0, 1))
  145. n = image.index
  146. if n > 100:
  147. break
  148. def test_select_start(self):
  149. count = 0
  150. for _ in self.nd2.select(channels='GFP', start=29000):
  151. count += 1
  152. self.assertEqual(127, count)
  153. def test_select_stop(self):
  154. count = 0
  155. for _ in self.nd2.select(channels='GFP', stop=20):
  156. count += 1
  157. self.assertEqual(count, 3)
  158. def test_select_start_stop(self):
  159. count = 0
  160. for _ in self.nd2.select(channels='GFP', start=10, stop=20):
  161. count += 1
  162. self.assertEqual(count, 1)
  163. def test_select_start_stop_brightfield(self):
  164. count = 0
  165. for _ in self.nd2.select(channels='', start=10, stop=20):
  166. count += 1
  167. self.assertEqual(count, 5)
  168. def test_select_faster(self):
  169. select_count = 0
  170. select_start = time.time()
  171. for i in self.nd2.select(channels='GFP', start=10, stop=50):
  172. if i is not None and i.channel == 'GFP':
  173. select_count += 1
  174. select_duration = time.time() - select_start
  175. direct_count = 0
  176. direct_start = time.time()
  177. for i in self.nd2[10:50]:
  178. if i is not None and i.channel == 'GFP':
  179. direct_count += 1
  180. direct_duration = time.time() - direct_start
  181. self.assertEqual(select_count, direct_count)
  182. self.assertGreater(direct_duration, select_duration)
  183. def test_pixel_microns(self):
  184. self.assertEqual(round(self.nd2.pixel_microns, 2), 0.22)