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.

113 lines
5.8 KiB

  1. from nd2reader.parser import Nd2Parser
  2. import unittest
  3. class MockNd2Parser(object):
  4. def __init__(self, channels, fields_of_view, z_levels):
  5. self.channels = channels
  6. self.fields_of_view = fields_of_view
  7. self.z_levels = z_levels
  8. class TestNd2Parser(unittest.TestCase):
  9. def test_calculate_field_of_view_simple(self):
  10. """ With a single field of view, the field of view should always be the same number (0). """
  11. nd2 = MockNd2Parser([''], [0], [0])
  12. for frame_number in range(1000):
  13. result = Nd2Parser._calculate_field_of_view(nd2, frame_number)
  14. self.assertEqual(result, 0)
  15. def test_calculate_field_of_view_two_channels(self):
  16. nd2 = MockNd2Parser(['', 'GFP'], [0], [0])
  17. for frame_number in range(1000):
  18. result = Nd2Parser._calculate_field_of_view(nd2, frame_number)
  19. self.assertEqual(result, 0)
  20. def test_calculate_field_of_view_three_channels(self):
  21. nd2 = MockNd2Parser(['', 'GFP', 'dsRed'], [0], [0])
  22. for frame_number in range(1000):
  23. result = Nd2Parser._calculate_field_of_view(nd2, frame_number)
  24. self.assertEqual(result, 0)
  25. def test_calculate_field_of_view_two_fovs(self):
  26. nd2 = MockNd2Parser([''], [0, 1], [0])
  27. for frame_number in range(1000):
  28. result = Nd2Parser._calculate_field_of_view(nd2, frame_number)
  29. self.assertEqual(result, frame_number % 2)
  30. def test_calculate_field_of_view_two_fovs_two_zlevels(self):
  31. nd2 = MockNd2Parser([''], [0, 1], [0, 1])
  32. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 0), 0)
  33. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 1), 0)
  34. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 2), 1)
  35. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 3), 1)
  36. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 4), 0)
  37. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 5), 0)
  38. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 6), 1)
  39. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 7), 1)
  40. def test_calculate_field_of_view_two_everything(self):
  41. nd2 = MockNd2Parser(['', 'GFP'], [0, 1], [0, 1])
  42. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 0), 0)
  43. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 1), 0)
  44. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 2), 0)
  45. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 3), 0)
  46. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 4), 1)
  47. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 5), 1)
  48. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 6), 1)
  49. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, 7), 1)
  50. def test_calculate_field_of_view_7c2f2z(self):
  51. nd2 = MockNd2Parser(['', 'GFP', 'dsRed', 'dTomato', 'lulzBlue', 'jimbotronPurple', 'orange'], [0, 1], [0, 1])
  52. for i in range(14):
  53. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 0)
  54. for i in range(14, 28):
  55. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 1)
  56. for i in range(28, 42):
  57. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 0)
  58. for i in range(42, 56):
  59. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 1)
  60. for i in range(56, 70):
  61. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 0)
  62. for i in range(70, 84):
  63. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 1)
  64. def test_calculate_field_of_view_2c3f5z(self):
  65. """ All prime numbers to elucidate any errors that won't show up when numbers are multiples of each other """
  66. nd2 = MockNd2Parser(['', 'GFP'], [0, 1, 2], [0, 1, 2, 3, 4])
  67. for i in range(10):
  68. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 0)
  69. for i in range(10, 20):
  70. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 1)
  71. for i in range(20, 30):
  72. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 2)
  73. for i in range(30, 40):
  74. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 0)
  75. for i in range(40, 50):
  76. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 1)
  77. for i in range(50, 60):
  78. self.assertEqual(Nd2Parser._calculate_field_of_view(nd2, i), 2)
  79. def test_calculate_channel_simple(self):
  80. nd2 = MockNd2Parser(['GFP'], [0], [0])
  81. for i in range(1000):
  82. self.assertEqual(Nd2Parser._calculate_channel(nd2, i), 'GFP')
  83. def test_calculate_channel(self):
  84. nd2 = MockNd2Parser(['', 'GFP', 'dsRed', 'dTomato', 'lulzBlue', 'jimbotronPurple', 'orange'], [0], [0])
  85. for i in range(1000):
  86. for n, channel in enumerate(['', 'GFP', 'dsRed', 'dTomato', 'lulzBlue', 'jimbotronPurple', 'orange'], start=i*7):
  87. self.assertEqual(Nd2Parser._calculate_channel(nd2, n), channel)
  88. def test_calculate_channel_7c2fov1z(self):
  89. nd2 = MockNd2Parser(['', 'GFP', 'dsRed', 'dTomato', 'lulzBlue', 'jimbotronPurple', 'orange'], [0, 1], [0])
  90. for i in range(1000):
  91. for n, channel in enumerate(['', 'GFP', 'dsRed', 'dTomato', 'lulzBlue', 'jimbotronPurple', 'orange'], start=i*7):
  92. self.assertEqual(Nd2Parser._calculate_channel(nd2, n), channel)
  93. def test_calculate_channel_ludicrous_values(self):
  94. nd2 = MockNd2Parser(['', 'GFP', 'dsRed', 'dTomato', 'lulzBlue', 'jimbotronPurple', 'orange'], list(range(31)), list(range(17)))
  95. for i in range(1000):
  96. for n, channel in enumerate(['', 'GFP', 'dsRed', 'dTomato', 'lulzBlue', 'jimbotronPurple', 'orange'], start=i*7):
  97. self.assertEqual(Nd2Parser._calculate_channel(nd2, n), channel)