Browse Source

add script for nix generation and validation

Julia Sprenger 2 years ago
parent
commit
bfca652a82
1 changed files with 98 additions and 0 deletions
  1. 98 0
      code/convert_to_nix.py

+ 98 - 0
code/convert_to_nix.py

@@ -0,0 +1,98 @@
+"""
+This script loads a complete session in the blackrock format and converts it to a single nix file
+"""
+import os
+
+import neo
+from neo.test.tools import (assert_same_sub_schema,
+                            assert_same_annotations,
+                            assert_same_array_annotations)
+from reachgraspio import reachgraspio
+
+
+# Choose which session you want to convert into a nix file
+# session = "l101210-001"
+session = "i140703-001"
+
+dataset_dir = '../datasets'
+session_path = f'{dataset_dir}/{session}'
+
+##### LOAD BLACKROCK FILES ############
+session = reachgraspio.ReachGraspIO(
+    filename=session_path,
+    odml_directory=dataset_dir,
+    verbose=False)
+
+block = session.read_block(lazy=False, load_waveforms=True)
+
+
+##### SAVE NIX FILE ###################
+nix_filename = session_path + '.nix'
+if os.path.exists(nix_filename):
+    print('Nix file already exists and will not be overwritten.')
+else:
+    with neo.NixIO(nix_filename) as io:
+        print(f'Saving nix file at {nix_filename}')
+        io.write_block(block)
+
+##### VALIDATION OF FILE CONTENT ######
+with neo.NixIO(nix_filename, mode='ro') as io:
+    blocks = io.read_all_blocks()
+    assert len(blocks) == 1
+    block_new = blocks[0]
+
+    for seg_old, seg_new in zip(block.segments, block_new.segments):
+        for anasig_old, anasig_new in zip(seg_old.analogsignals, seg_new.analogsignals):
+            # ignoring differences in the file_origin attribute
+            anasig_old.file_origin = anasig_new.file_origin
+            # ignoring nix_name annotation generated by nix file
+            anasig_new.annotations.pop('nix_name')
+
+            assert_same_sub_schema(anasig_old, anasig_new)
+            assert_same_annotations(anasig_old, anasig_new)
+            assert_same_array_annotations(anasig_old, anasig_new)
+            del anasig_old
+        print(f'AnalogSignals are equivalent.')
+        
+        for st_old, st_new in zip(seg_old.spiketrains, seg_new.spiketrains):
+            # ignoring differences in the file_origin attribute
+            st_old.file_origin = st_new.file_origin
+            # ignoring nix_name annotation generated by nix file
+            st_new.annotations.pop('nix_name')
+
+            assert_same_sub_schema(st_old, st_new)
+            assert_same_annotations(st_old, st_new)
+            assert_same_array_annotations(st_old, st_new)
+            del st_old
+        print(f'Spiketrains are equivalent.')
+        
+        for ev_old, ev_new in zip(seg_old.events, seg_new.events):
+            # ignoring differences in the file_origin attribute
+            ev_old.file_origin = ev_new.file_origin
+            # ignoring nix_name annotation generated by nix file
+            ev_new.annotations.pop('nix_name')
+            # ignore list-array type changes
+            if 'color_codes' in ev_old.annotations:
+                ev_old.annotations['color_codes'] = list(ev_old.annotations['color_codes'])
+
+            assert_same_sub_schema(ev_old, ev_new)
+            assert_same_annotations(ev_old, ev_new)
+            assert_same_array_annotations(ev_old, ev_new)
+            del ev_old
+        print(f'Events are equivalent.')
+        
+        for ep_old, ep_new in zip(seg_old.epochs, seg_new.epochs):
+            # ignoring differences in the file_origin attribute
+            ep_old.file_origin = ep_new.file_origin
+            # ignoring nix_name annotation generated by nix file
+            ep_new.annotations.pop('nix_name')
+
+            assert_same_sub_schema(ep_old, ep_new)
+            assert_same_annotations(ep_old, ep_new)
+            assert_same_array_annotations(ep_old, ep_new)
+            del ep_old
+        print(f'Epochs are equivalent.')
+            
+            
+
+