test_reachgraspio.py 4.8 KB

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