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.

240 lines
9.1 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. from skimage import io
  7. import numpy as np
  8. from datetime import datetime
  9. import unittest
  10. import time
  11. class FYLM141111Tests(unittest.TestCase):
  12. def setUp(self):
  13. self.nd2 = Nd2("/var/nd2s/FYLM-141111-001.nd2")
  14. def tearDown(self):
  15. self.nd2.close()
  16. def test_shape(self):
  17. self.assertEqual(self.nd2.height, 1280)
  18. self.assertEqual(self.nd2.width, 800)
  19. def test_date(self):
  20. self.assertEqual(self.nd2.date, datetime(2014, 11, 11, 15, 59, 19))
  21. @unittest.skip("This will fail until we address issue #59")
  22. def test_length(self):
  23. self.assertEqual(len(self.nd2), 17808)
  24. def test_frames(self):
  25. self.assertEqual(len(self.nd2.frames), 636)
  26. def test_fovs(self):
  27. self.assertEqual(len(self.nd2.fields_of_view), 8)
  28. def test_channels(self):
  29. self.assertTupleEqual(tuple(sorted(self.nd2.channels)), ('', 'GFP'))
  30. def test_z_levels(self):
  31. self.assertTupleEqual(tuple(self.nd2.z_levels), (0, 1, 2))
  32. def test_pixel_size(self):
  33. self.assertGreater(self.nd2.pixel_microns, 0.0)
  34. def test_image(self):
  35. image = self.nd2[14]
  36. self.assertEqual(image.field_of_view, 2)
  37. self.assertEqual(image.frame_number, 0)
  38. self.assertAlmostEqual(image.timestamp, 19.0340758)
  39. self.assertEqual(image.channel, '')
  40. self.assertEqual(image.z_level, 1)
  41. self.assertEqual(image.height, self.nd2.height)
  42. self.assertEqual(image.width, self.nd2.width)
  43. def test_last_image(self):
  44. image = self.nd2[30526]
  45. self.assertEqual(image.frame_number, 635)
  46. def test_bad_image(self):
  47. image = self.nd2[13]
  48. self.assertIsNone(image)
  49. def test_iteration(self):
  50. images = [image for image in self.nd2[:10]]
  51. self.assertEqual(len(images), 10)
  52. def test_iteration_step(self):
  53. images = [image for image in self.nd2[:10:2]]
  54. self.assertEqual(len(images), 5)
  55. def test_iteration_backwards(self):
  56. images = [image for image in self.nd2[:10:-1]]
  57. self.assertEqual(len(images), 10)
  58. def test_get_image_by_attribute_ok(self):
  59. image = self.nd2.get_image(4, 0, 'GFP', 1)
  60. self.assertIsNotNone(image)
  61. image = self.nd2.get_image(4, 0, '', 0)
  62. self.assertIsNotNone(image)
  63. image = self.nd2.get_image(4, 0, '', 1)
  64. self.assertIsNotNone(image)
  65. def test_images(self):
  66. self.assertTupleEqual((self.nd2[0].z_level, self.nd2[0].channel), (0, ''))
  67. self.assertIsNone(self.nd2[1])
  68. self.assertTupleEqual((self.nd2[2].z_level, self.nd2[2].channel), (1, ''))
  69. self.assertTupleEqual((self.nd2[3].z_level, self.nd2[3].channel), (1, 'GFP'))
  70. self.assertTupleEqual((self.nd2[4].z_level, self.nd2[4].channel), (2, ''))
  71. self.assertIsNone(self.nd2[5])
  72. self.assertTupleEqual((self.nd2[6].z_level, self.nd2[6].channel), (0, ''))
  73. self.assertIsNone(self.nd2[7])
  74. self.assertTupleEqual((self.nd2[8].z_level, self.nd2[8].channel), (1, ''))
  75. self.assertTupleEqual((self.nd2[9].z_level, self.nd2[9].channel), (1, 'GFP'))
  76. self.assertTupleEqual((self.nd2[10].z_level, self.nd2[10].channel), (2, ''))
  77. self.assertIsNone(self.nd2[11])
  78. self.assertTupleEqual((self.nd2[12].z_level, self.nd2[12].channel), (0, ''))
  79. self.assertIsNone(self.nd2[13])
  80. self.assertTupleEqual((self.nd2[14].z_level, self.nd2[14].channel), (1, ''))
  81. self.assertTupleEqual((self.nd2[15].z_level, self.nd2[15].channel), (1, 'GFP'))
  82. self.assertTupleEqual((self.nd2[16].z_level, self.nd2[16].channel), (2, ''))
  83. self.assertIsNone(self.nd2[17])
  84. self.assertTupleEqual((self.nd2[18].z_level, self.nd2[18].channel), (0, ''))
  85. self.assertIsNone(self.nd2[19])
  86. self.assertIsNone(self.nd2[47])
  87. self.assertTupleEqual((self.nd2[48].z_level, self.nd2[48].channel), (0, ''))
  88. self.assertIsNone(self.nd2[49])
  89. self.assertTupleEqual((self.nd2[50].z_level, self.nd2[50].channel), (1, ''))
  90. self.assertIsNone(self.nd2[51])
  91. self.assertTupleEqual((self.nd2[52].z_level, self.nd2[52].channel), (2, ''))
  92. self.assertIsNone(self.nd2[53])
  93. self.assertTupleEqual((self.nd2[54].z_level, self.nd2[54].channel), (0, ''))
  94. def test_get_image_by_attribute_none(self):
  95. # Should handle missing images without an exception
  96. image = self.nd2.get_image(4, 0, 'GFP', 0)
  97. self.assertIsNone(image)
  98. def test_index(self):
  99. # Do indexes get added to images properly?
  100. for n, image in enumerate(self.nd2):
  101. if image is not None:
  102. self.assertEqual(n, image.index)
  103. if n > 50:
  104. break
  105. def test_select(self):
  106. # If we take the first 20 GFP images, they should be identical to the first 20 items iterated from select()
  107. # if we set our criteria to just 'GFP'
  108. manual_images = []
  109. for _, image in zip(range(20), self.nd2):
  110. if image is not None and image.channel == 'GFP':
  111. manual_images.append(image)
  112. filter_images = []
  113. for image in self.nd2.select(channels='GFP'):
  114. filter_images.append(image)
  115. if len(filter_images) == len(manual_images):
  116. break
  117. self.assertEqual(len(manual_images), len(filter_images))
  118. self.assertGreater(len(manual_images), 0)
  119. for a, b in zip(manual_images, filter_images):
  120. self.assertTrue(np.array_equal(a, b))
  121. self.assertEqual(a.index, b.index)
  122. self.assertEqual(a.field_of_view, b.field_of_view)
  123. self.assertEqual(a.channel, b.channel)
  124. def test_select_order_all(self):
  125. # If we select every possible image using select(), we should just get every image in order
  126. n = 0
  127. for image in self.nd2.select(channels=['', 'GFP'], z_levels=[0, 1, 2], fields_of_view=list(range(8))):
  128. while True:
  129. indexed_image = self.nd2[n]
  130. if indexed_image is not None:
  131. break
  132. n += 1
  133. self.assertTrue(np.array_equal(image, indexed_image))
  134. n += 1
  135. if n > 100:
  136. break
  137. def test_select_order_subset(self):
  138. # Test that images are always yielded in increasing order. This guarantees that no matter what subset of images
  139. # we're filtering, we still get them in the chronological order they were acquired
  140. n = -1
  141. for image in self.nd2.select(channels='', z_levels=[0, 1], fields_of_view=[1, 2, 4]):
  142. self.assertGreater(image.index, n)
  143. self.assertEqual(image.channel, '')
  144. self.assertIn(image.field_of_view, (1, 2, 4))
  145. self.assertIn(image.z_level, (0, 1))
  146. n = image.index
  147. if n > 100:
  148. break
  149. def test_select_start(self):
  150. count = 0
  151. for _ in self.nd2.select(channels='GFP', start=29000):
  152. count += 1
  153. self.assertEqual(127, count)
  154. def test_select_stop(self):
  155. count = 0
  156. for _ in self.nd2.select(channels='GFP', stop=20):
  157. count += 1
  158. self.assertEqual(count, 3)
  159. def test_select_start_stop(self):
  160. count = 0
  161. for _ in self.nd2.select(channels='GFP', start=10, stop=20):
  162. count += 1
  163. self.assertEqual(count, 1)
  164. def test_select_start_stop_brightfield(self):
  165. count = 0
  166. for _ in self.nd2.select(channels='', start=10, stop=20):
  167. count += 1
  168. self.assertEqual(count, 5)
  169. def test_select_faster(self):
  170. select_count = 0
  171. select_start = time.time()
  172. for i in self.nd2.select(channels='GFP', start=10, stop=50):
  173. if i is not None and i.channel == 'GFP':
  174. select_count += 1
  175. select_duration = time.time() - select_start
  176. direct_count = 0
  177. direct_start = time.time()
  178. for i in self.nd2[10:50]:
  179. if i is not None and i.channel == 'GFP':
  180. direct_count += 1
  181. direct_duration = time.time() - direct_start
  182. self.assertEqual(select_count, direct_count)
  183. self.assertGreater(direct_duration, select_duration)
  184. def test_pixel_microns(self):
  185. self.assertEqual(round(self.nd2.pixel_microns, 2), 0.22)
  186. def test_numpy_operations(self):
  187. # just to make sure we can do this kind of thing and get scalars
  188. self.assertTrue(0 < np.mean(self.nd2[0]) < np.sum(self.nd2[0]))
  189. def test_numpy_mean(self):
  190. # make sure we get the right value and type
  191. expected_mean = 17513.053581054686
  192. mean = np.mean(self.nd2[0])
  193. self.assertEqual(type(mean), np.float64)
  194. self.assertAlmostEqual(expected_mean, mean)
  195. def test_subtract_images(self):
  196. # just to prove we can really treat Image like an array
  197. diff = self.nd2[0] - self.nd2[2]
  198. self.assertTrue(np.any(diff))
  199. def test_show(self):
  200. io.imshow(self.nd2[0])
  201. io.show()
  202. self.assertTrue(True)