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.

146 lines
5.6 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
  1. """
  2. Tests on ND2s that have 1 or 2 cycles only. This is unlike the ND2s I work with typically, which are all done over very long periods of time.
  3. """
  4. from nd2reader import Nd2
  5. import numpy as np
  6. import unittest
  7. class Monocycle1Tests(unittest.TestCase):
  8. def setUp(self):
  9. self.nd2 = Nd2("/var/nd2s/simone1.nd2")
  10. def tearDown(self):
  11. self.nd2.close()
  12. def test_channels(self):
  13. self.assertListEqual(self.nd2.channels, ['Cy3Narrow', 'TxRed-modified', 'FITC', 'DAPI'])
  14. def test_pixel_size(self):
  15. self.assertGreater(self.nd2.pixel_microns, 0.0)
  16. def test_select(self):
  17. manual_images = []
  18. for _, image in zip(range(20), self.nd2):
  19. if image is not None and image.channel == 'FITC':
  20. manual_images.append(image)
  21. filter_images = []
  22. for image in self.nd2.select(channels='FITC'):
  23. filter_images.append(image)
  24. if len(filter_images) == len(manual_images):
  25. break
  26. self.assertEqual(len(manual_images), len(filter_images))
  27. self.assertGreater(len(manual_images), 0)
  28. for a, b in zip(manual_images, filter_images):
  29. self.assertTrue(np.array_equal(a, b))
  30. self.assertEqual(a.index, b.index)
  31. self.assertEqual(a.field_of_view, b.field_of_view)
  32. self.assertEqual(a.channel, b.channel)
  33. def test_select_order_all(self):
  34. # If we select every possible image using select(), we should just get every image in order
  35. n = 0
  36. for image in self.nd2.select(channels=['Cy3Narrow', 'DAPI', 'FITC', 'TxRed-modified'],
  37. z_levels=list(range(35)),
  38. fields_of_view=list(range(5))):
  39. while True:
  40. indexed_image = self.nd2[n]
  41. if indexed_image is not None:
  42. break
  43. n += 1
  44. self.assertTrue(np.array_equal(image, indexed_image))
  45. n += 1
  46. if n > 100:
  47. # Quit after the first hundred images just to save time.
  48. # If there's a problem, we'll have seen it by now.
  49. break
  50. def test_select_order_subset(self):
  51. # Test that images are always yielded in increasing order. This guarantees that no matter what subset of images
  52. # we're filtering, we still get them in the chronological order they were acquired
  53. n = -1
  54. for image in self.nd2.select(channels='FITC',
  55. z_levels=[0, 1],
  56. fields_of_view=[1, 2, 4]):
  57. self.assertGreater(image.index, n)
  58. self.assertEqual(image.channel, 'FITC')
  59. self.assertIn(image.field_of_view, (1, 2, 4))
  60. self.assertIn(image.z_level, (0, 1))
  61. n = image.index
  62. if n > 100:
  63. break
  64. class Monocycle2Tests(unittest.TestCase):
  65. def setUp(self):
  66. self.nd2 = Nd2("/var/nd2s/hawkjo.nd2")
  67. def tearDown(self):
  68. self.nd2.close()
  69. def test_pixel_size(self):
  70. self.assertGreater(round(self.nd2.pixel_microns, 2), 0.26)
  71. def test_select(self):
  72. manual_images = []
  73. for _, image in zip(range(20), self.nd2):
  74. if image is not None and image.channel == 'HHQ 500 LP 1':
  75. manual_images.append(image)
  76. filter_images = []
  77. for image in self.nd2.select(channels='HHQ 500 LP 1'):
  78. filter_images.append(image)
  79. if len(filter_images) == len(manual_images):
  80. break
  81. self.assertEqual(len(manual_images), len(filter_images))
  82. self.assertGreater(len(manual_images), 0)
  83. for a, b in zip(manual_images, filter_images):
  84. self.assertTrue(np.array_equal(a, b))
  85. self.assertEqual(a.index, b.index)
  86. self.assertEqual(a.field_of_view, b.field_of_view)
  87. self.assertEqual(a.channel, b.channel)
  88. def test_select_order_all(self):
  89. # If we select every possible image using select(), we should just get every image in order
  90. n = 0
  91. for image in self.nd2.select(channels=['HHQ 500 LP 1', 'HHQ 500 LP 2'],
  92. z_levels=[0],
  93. fields_of_view=list(range(100))):
  94. while True:
  95. indexed_image = self.nd2[n]
  96. if indexed_image is not None:
  97. break
  98. n += 1
  99. self.assertTrue(np.array_equal(image, indexed_image))
  100. n += 1
  101. if n > 100:
  102. # Quit after the first hundred images just to save time.
  103. # If there's a problem, we'll have seen it by now.
  104. break
  105. def test_select_order_subset(self):
  106. # Test that images are always yielded in increasing order. This guarantees that no matter what subset of images
  107. # we're filtering, we still get them in the chronological order they were acquired
  108. n = -1
  109. for image in self.nd2.select(channels='HHQ 500 LP 2',
  110. z_levels=[0],
  111. fields_of_view=[1, 2, 4]):
  112. self.assertGreater(image.index, n)
  113. self.assertEqual(image.channel, 'HHQ 500 LP 2')
  114. self.assertIn(image.field_of_view, (1, 2, 4))
  115. self.assertEqual(image.z_level, 0)
  116. n = image.index
  117. if n > 100:
  118. break
  119. class OneTests(unittest.TestCase):
  120. def test_opens(self):
  121. # just testing that this doesn't throw an exception
  122. nd2 = Nd2("/var/nd2s/001.nd2")
  123. self.assertIsNotNone(nd2)
  124. nd2.close()