test_reachgraspio.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import sys
  2. from os.path import join, realpath, dirname
  3. sys.path.insert(0, realpath(join(dirname(__file__), '..')))
  4. import numpy as np
  5. import unittest
  6. import reachgraspio.reachgraspio as rg
  7. rio = rg.ReachGraspIO("../../datasets_blackrock/i140703-001",
  8. odml_directory="../../datasets_blackrock",
  9. nsx_to_load='all')
  10. block1 = rio.read_block(lazy=True)
  11. rio = rg.ReachGraspIO("../../datasets_blackrock/l101210-001",
  12. odml_directory="../../datasets_blackrock",
  13. nsx_to_load='all')
  14. block2 = rio.read_block(lazy=True)
  15. class RGIOTestCase(unittest.TestCase):
  16. def setUp(self):
  17. self.blocks = [block1, block2]
  18. def test_group_units_present(self):
  19. for block in self.blocks:
  20. unit_groups = [g for g in block.groups if 'Unit' in g.name]
  21. self.assertGreater(len(unit_groups), 0)
  22. self.assertGreaterEqual(len(unit_groups), len(block.segments[0].spiketrains))
  23. def test_channel_infos_present(self):
  24. for block in self.blocks:
  25. for seg in block.segments:
  26. for anasig in seg.analogsignals:
  27. if anasig.annotations['neural_signal']:
  28. self.assertIn('connector_aligned_ids', anasig.array_annotations)
  29. self.assertIn('coordinates_x', anasig.array_annotations)
  30. self.assertIn('coordinates_y', anasig.array_annotations)
  31. def test_group_unit_annotations(self):
  32. for block in self.blocks:
  33. for group in block.groups:
  34. if 'Unit' in group.name:
  35. self.assertIn('unit_id', group.annotations)
  36. self.assertIn('connector_aligned_id', group.annotations)
  37. self.assertIn('sua', group.annotations)
  38. self.assertIn('mua', group.annotations)
  39. self.assertIn('noise', group.annotations)
  40. # To be investigated
  41. # if group.annotations['unit_id'] > 0:
  42. # print(group.annotations)
  43. # self.assertIn('spike_duration', group.annotations)
  44. # self.assertIn('spike_amplitude', group.annotations)
  45. # self.assertIn('spike_count', group.annotations)
  46. def test_group_unit_linking(self):
  47. for block in self.blocks:
  48. # run this test only if neuronal signals are present
  49. if not [a for seg in block.segments for a in seg.analogsignals if
  50. a.annotations['neural_signal']]:
  51. return
  52. for group in block.groups:
  53. if 'Unit' in group.name:
  54. asig_annotations = group.channelviews[0].obj.array_annotations
  55. idx = group.channelviews[0].index
  56. self.assertEqual(group.annotations['channel_id'],
  57. asig_annotations['channel_ids'][idx])
  58. def test_consecutive_trial_ids(self):
  59. for block in self.blocks:
  60. for seg in block.segments:
  61. for ev in seg.events:
  62. if 'trial_id' in ev.array_annotations:
  63. # exclude invalid trial_ids:
  64. trial_ids = ev.array_annotations['trial_id']
  65. valid_ids = trial_ids[trial_ids != -1]
  66. self.assertTrue(all(np.diff(valid_ids) >= 0))
  67. def test_rejection_annotations_present(self):
  68. for block in self.blocks:
  69. for seg in block.segments:
  70. for anasig in seg.analogsignals:
  71. if anasig.annotations['neural_signal']:
  72. for key in ['file_origin', 'connector_ID', 'connector_pinID',
  73. 'nsx_hi_freq_order', 'nsx_lo_freq_order', 'nsx_hi_freq_type',
  74. 'nsx_lo_freq_type', 'description', 'nsx',
  75. 'electrode_reject_IFC', 'electrode_reject_LFC',
  76. 'electrode_reject_HFC']:
  77. self.assertIn(key, anasig.array_annotations)
  78. for st in seg.spiketrains:
  79. for key in ['electrode_reject_IFC', 'electrode_reject_LFC',
  80. 'electrode_reject_HFC','nev_dig_factor', 'nb_sorted_units',
  81. 'nev_hi_freq_order', 'nev_hi_freq_type', 'nev_lo_freq_order',
  82. 'nev_lo_freq_type']:
  83. self.assertIn(key, st.annotations)
  84. def test_event_annotations(self):
  85. for block in self.blocks:
  86. for seg in block.segments:
  87. for ev in seg.events:
  88. if ev.name in ['DigitalTrialEvents', 'AnalogTrialEvents', 'TrialEvents']:
  89. for key in ['trial_event_labels', 'trial_timestamp_id', 'trial_id',
  90. 'belongs_to_trialtype', 'performance_in_trial',
  91. 'performance_in_trial_str', 'trial_reject_HFC', 'trial_reject_LFC',
  92. 'trial_reject_IFC']:
  93. self.assertIn(key, ev.array_annotations)
  94. ev_names = [ev.name for ev in seg.events]
  95. for key in ['AnalogTrialEvents', 'DigitalTrialEvents', 'TrialEvents']:
  96. self.assertIn(key, ev_names)
  97. def test_block_annotation(self):
  98. for block in self.blocks:
  99. self.assertIn('conditions', block.annotations)
  100. self.assertGreater(len(block.annotations['conditions']), 0)
  101. def test_connector_aligned_ids_coordinates(self):
  102. for block in self.blocks:
  103. for seg in block.segments:
  104. for anasig in seg.analogsignals:
  105. if not anasig.annotations['neural_signal']:
  106. continue # only check annotations of neural signals
  107. for coords in ['coordinates_x', 'coordinates_y']:
  108. if coords in anasig.array_annotations:
  109. self.assertEqual(len(np.unique(anasig.array_annotations[coords])), 10)
  110. self.assertIn('connector_aligned_ids', anasig.array_annotations)
  111. def suite():
  112. suite = unittest.makeSuite(RGIOTestCase, 'test')
  113. return suite
  114. if __name__ == "__main__":
  115. runner = unittest.TextTestRunner(verbosity=2)
  116. runner.run(suite())