test_reachgraspio.py 6.2 KB

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