Browse Source

Added missing annotations to offline filtered LFP in lilou

Michael Denker 11 months ago
parent
commit
60939eb88f
1 changed files with 29 additions and 9 deletions
  1. 29 9
      code/convert_to_nix.py

+ 29 - 9
code/convert_to_nix.py

@@ -2,6 +2,7 @@
 This script loads a complete session in the blackrock format and converts it to a single nix file
 """
 import os
+import copy
 
 import numpy as np
 import quantities as pq
@@ -16,8 +17,8 @@ from reachgraspio import reachgraspio
 
 
 # Choose which session you want to convert into a nix file
-session = "i140703-001"
-# session = "l101210-001"
+# session = "i140703-001"
+session = "l101210-001"
 
 # Input data. i.e., original Blackrock files and odML
 dataset_dir = '../datasets_blackrock'
@@ -51,11 +52,10 @@ nsx_to_anasig_name = {2: 'LFP signal (online filtered)',
                       5: 'raw signal',
                       6: 'raw signal'}
 
-# this factor was experimentally determined as an approximate shift introduced
-# by the online filtering. Here, we integrate this shift such that the offline
-# filtered signal is aligned to the online filtered signal (if that were
-# available)
-time_shift_factor = -0.42*pq.ms
+# this factor was heuristically determined as an approximate shift introduced
+# by the online filtering. Here, the offline filtering does not introduce a
+# noticable shift, so we set it to zero.
+time_shift_factor = 0*pq.ms
 
 filtered_anasig = None
 raw_anasig = None
@@ -94,7 +94,11 @@ if filtered_anasig is None:
             filter_function='sosfiltfilt',
             order=4)
 
-        downsampled_signal=filtered_signal.downsample(30).time_shift(time_shift_factor)
+        # For other filters that may introduce a time shift, here would be the
+        # place to correct for this shift using:
+        # ... .time_shift(time_shift_factor)
+        # Downsampling 30-fold here to get to 1000Hz from 30kHz
+        downsampled_signal=filtered_signal.downsample(30)
 
         # first run? Create a new Analogsignal
         if f == 0:
@@ -104,6 +108,22 @@ if filtered_anasig is None:
                 t_start=downsampled_signal.t_start,
                 sampling_rate=downsampled_signal.sampling_rate)
 
+            # add missing annotations (decision not to put nsx2, since this
+            # signal is not in the original files
+            offline_filtered_anasig.annotate(
+                neural_signal=True,
+                filter_shift_correction=time_shift_factor,
+                nsx=-1,
+                stream_id='',
+            )
+
+            # all array annotations of the raw signal also apply to the filtered
+            # signal
+            # offline_filtered_anasig.array_annotations = copy.copy(
+            #     raw_anasig.array_annotations)
+            offline_filtered_anasig.array_annotate(
+                **raw_anasig.array_annotations)
+
         offline_filtered_anasig[:, f] = downsampled_signal
 
     if 'nsx' in anasig.annotations:
@@ -116,7 +136,7 @@ if filtered_anasig is None:
                            "performed off-line during post-processing"
 
     # Attach all offline filtered LFPs to the segment of data
-    block.segments[0].analogsignals.append(offline_filtered_anasig)
+    block.segments[0].analogsignals.insert(0, offline_filtered_anasig)
 
 
 ##### SAVE NIX FILE ###################