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.

123 lines
5.1 KiB

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_select(self):
  13. # If we take the first 20 GFP images, they should be identical to the first 20 items iterated from select()
  14. # if we set our criteria to just "GFP"
  15. manual_images = []
  16. for _, image in zip(range(20), self.nd2):
  17. if image is not None and image.channel == 'FITC':
  18. manual_images.append(image)
  19. filter_images = []
  20. for image in self.nd2.select(channels='FITC'):
  21. filter_images.append(image)
  22. if len(filter_images) == len(manual_images):
  23. break
  24. self.assertEqual(len(manual_images), len(filter_images))
  25. self.assertGreater(len(manual_images), 0)
  26. for a, b in zip(manual_images, filter_images):
  27. self.assertTrue(np.array_equal(a, b))
  28. self.assertEqual(a.index, b.index)
  29. self.assertEqual(a.field_of_view, b.field_of_view)
  30. self.assertEqual(a.channel, b.channel)
  31. def test_select_order_all(self):
  32. # If we select every possible image using select(), we should just get every image in order
  33. n = 0
  34. for image in self.nd2.select(channels=['Cy3Narrow', 'DAPI', 'FITC', 'TxRed-modified'], z_levels=list(range(35)), fields_of_view=list(range(5))):
  35. while True:
  36. indexed_image = self.nd2[n]
  37. if indexed_image is not None:
  38. break
  39. n += 1
  40. self.assertTrue(np.array_equal(image, indexed_image))
  41. n += 1
  42. if n > 100:
  43. # Quit after the first hundred images just to save time. If there's a problem, we'll have seen it by now.
  44. break
  45. def test_select_order_subset(self):
  46. # Test that images are always yielded in increasing order. This guarantees that no matter what subset of images
  47. # we're filtering, we still get them in the chronological order they were acquired
  48. n = -1
  49. for image in self.nd2.select(channels='FITC', z_levels=[0, 1], fields_of_view=[1, 2, 4]):
  50. self.assertGreater(image.index, n)
  51. self.assertEqual(image.channel, 'FITC')
  52. self.assertIn(image.field_of_view, (1, 2, 4))
  53. self.assertIn(image.z_level, (0, 1))
  54. n = image.index
  55. if n > 100:
  56. break
  57. class Monocycle2Tests(unittest.TestCase):
  58. def setUp(self):
  59. self.nd2 = Nd2("/var/nd2s/hawkjo.nd2")
  60. def tearDown(self):
  61. self.nd2.close()
  62. def test_select(self):
  63. # If we take the first 20 HHQ 500 LP 1 images, they should be identical to the first 20 items iterated from select()
  64. # if we set our criteria to just "HHQ 500 LP 1"
  65. manual_images = []
  66. for _, image in zip(range(20), self.nd2):
  67. if image is not None and image.channel == 'HHQ 500 LP 1':
  68. manual_images.append(image)
  69. filter_images = []
  70. for image in self.nd2.select(channels='HHQ 500 LP 1'):
  71. filter_images.append(image)
  72. if len(filter_images) == len(manual_images):
  73. break
  74. self.assertEqual(len(manual_images), len(filter_images))
  75. self.assertGreater(len(manual_images), 0)
  76. for a, b in zip(manual_images, filter_images):
  77. self.assertTrue(np.array_equal(a, b))
  78. self.assertEqual(a.index, b.index)
  79. self.assertEqual(a.field_of_view, b.field_of_view)
  80. self.assertEqual(a.channel, b.channel)
  81. def test_select_order_all(self):
  82. # If we select every possible image using select(), we should just get every image in order
  83. n = 0
  84. for image in self.nd2.select(channels=['HHQ 500 LP 1', 'HHQ 500 LP 2'], z_levels=[0], fields_of_view=list(range(100))):
  85. while True:
  86. indexed_image = self.nd2[n]
  87. if indexed_image is not None:
  88. break
  89. n += 1
  90. self.assertTrue(np.array_equal(image, indexed_image))
  91. n += 1
  92. if n > 100:
  93. # Quit after the first hundred images just to save time. If there's a problem, we'll have seen it by now.
  94. break
  95. def test_select_order_subset(self):
  96. # Test that images are always yielded in increasing order. This guarantees that no matter what subset of images
  97. # we're filtering, we still get them in the chronological order they were acquired
  98. n = -1
  99. for image in self.nd2.select(channels='HHQ 500 LP 2', z_levels=[0], fields_of_view=[1, 2, 4]):
  100. self.assertGreater(image.index, n)
  101. self.assertEqual(image.channel, 'HHQ 500 LP 2')
  102. self.assertIn(image.field_of_view, (1, 2, 4))
  103. self.assertEqual(image.z_level, 0)
  104. n = image.index
  105. if n > 100:
  106. break