convert_to_nix.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """
  2. This script loads a complete session in the blackrock format and converts it to a single nix file
  3. """
  4. import os
  5. import neo
  6. from neo.test.tools import (assert_same_sub_schema,
  7. assert_same_annotations,
  8. assert_same_array_annotations)
  9. from reachgraspio import reachgraspio
  10. # Choose which session you want to convert into a nix file
  11. # session = "l101210-001"
  12. session = "i140703-001"
  13. dataset_dir = '../datasets'
  14. session_path = f'{dataset_dir}/{session}'
  15. ##### LOAD BLACKROCK FILES ############
  16. session = reachgraspio.ReachGraspIO(
  17. filename=session_path,
  18. odml_directory=dataset_dir,
  19. verbose=False)
  20. block = session.read_block(lazy=False, load_waveforms=True)
  21. ##### SAVE NIX FILE ###################
  22. nix_filename = session_path + '.nix'
  23. if os.path.exists(nix_filename):
  24. print('Nix file already exists and will not be overwritten.')
  25. else:
  26. with neo.NixIO(nix_filename) as io:
  27. print(f'Saving nix file at {nix_filename}')
  28. io.write_block(block)
  29. ##### VALIDATION OF FILE CONTENT ######
  30. with neo.NixIO(nix_filename, mode='ro') as io:
  31. blocks = io.read_all_blocks()
  32. assert len(blocks) == 1
  33. block_new = blocks[0]
  34. for seg_old, seg_new in zip(block.segments, block_new.segments):
  35. for anasig_old, anasig_new in zip(seg_old.analogsignals, seg_new.analogsignals):
  36. # ignoring differences in the file_origin attribute
  37. anasig_old.file_origin = anasig_new.file_origin
  38. # ignoring nix_name annotation generated by nix file
  39. anasig_new.annotations.pop('nix_name')
  40. assert_same_sub_schema(anasig_old, anasig_new)
  41. assert_same_annotations(anasig_old, anasig_new)
  42. assert_same_array_annotations(anasig_old, anasig_new)
  43. del anasig_old
  44. print(f'AnalogSignals are equivalent.')
  45. for st_old, st_new in zip(seg_old.spiketrains, seg_new.spiketrains):
  46. # ignoring differences in the file_origin attribute
  47. st_old.file_origin = st_new.file_origin
  48. # ignoring nix_name annotation generated by nix file
  49. st_new.annotations.pop('nix_name')
  50. assert_same_sub_schema(st_old, st_new)
  51. assert_same_annotations(st_old, st_new)
  52. assert_same_array_annotations(st_old, st_new)
  53. del st_old
  54. print(f'Spiketrains are equivalent.')
  55. for ev_old, ev_new in zip(seg_old.events, seg_new.events):
  56. # ignoring differences in the file_origin attribute
  57. ev_old.file_origin = ev_new.file_origin
  58. # ignoring nix_name annotation generated by nix file
  59. ev_new.annotations.pop('nix_name')
  60. # ignore list-array type changes
  61. if 'color_codes' in ev_old.annotations:
  62. ev_old.annotations['color_codes'] = list(ev_old.annotations['color_codes'])
  63. assert_same_sub_schema(ev_old, ev_new)
  64. assert_same_annotations(ev_old, ev_new)
  65. assert_same_array_annotations(ev_old, ev_new)
  66. del ev_old
  67. print(f'Events are equivalent.')
  68. for ep_old, ep_new in zip(seg_old.epochs, seg_new.epochs):
  69. # ignoring differences in the file_origin attribute
  70. ep_old.file_origin = ep_new.file_origin
  71. # ignoring nix_name annotation generated by nix file
  72. ep_new.annotations.pop('nix_name')
  73. assert_same_sub_schema(ep_old, ep_new)
  74. assert_same_annotations(ep_old, ep_new)
  75. assert_same_array_annotations(ep_old, ep_new)
  76. del ep_old
  77. print(f'Epochs are equivalent.')