Append and/or concatenate segments

Sometimes a recording can be split into several subparts, for instance a baseline and an intervention.

Similarly to NEO we define each subpart as a “segment”.

SpikeInterface has tools to interact with these segments. There are two ways:

Here is the difference. Imagine we have 2 recordings with 2 and 3 segments respectively:

  1. In case 1. (append) we will end up with one recording with 5 segments.

  2. In case 2. (concatenate) we will end up with one recording with 1 “big” segment that virtually concatenates all segments.

Here is a short example.

import matplotlib.pyplot as plt
import numpy as np


from spikeinterface import NumpyRecording, NumpySorting
from spikeinterface import append_recordings, concatenate_recordings

First let’s generate 2 recordings with 2 and 3 segments respectively:

sampling_frequency = 1000.0

trace0 = np.zeros((150, 5), dtype="float32")
trace1 = np.zeros((100, 5), dtype="float32")
rec0 = NumpyRecording([trace0, trace1], sampling_frequency)
print(rec0)

trace2 = np.zeros((50, 5), dtype="float32")
trace3 = np.zeros((200, 5), dtype="float32")
trace4 = np.zeros((120, 5), dtype="float32")
rec1 = NumpyRecording([trace2, trace3, trace4], sampling_frequency)
print(rec1)
NumpyRecording: 5 channels - 1.0kHz - 2 segments - 250 samples - 0.25s (250.00 ms) - float32 dtype
                4.88 KiB
Segments:
Samples:   150 | 100
Durations: 0.15s (150.00 ms) | 0.10s (100.00 ms)
Memory:    2.93 KiB | 1.95 KiB
NumpyRecording: 5 channels - 1.0kHz - 3 segments - 370 samples - 0.37s (370.00 ms) - float32 dtype
                7.23 KiB
Segments:
Samples:   50 | 200 | 120
Durations: 0.05s (50.00 ms) | 0.20s (200.00 ms) | 0.12s (120.00 ms)
Memory:    1000.00 B | 3.91 KiB | 2.34 KiB

Let’s use the append_recordings():

recording_list = [rec0, rec1]
rec = append_recordings(recording_list)
print(rec)
for i in range(rec.get_num_segments()):
    s = rec.get_num_samples(segment_index=i)
    print(f"segment {i} num_samples {s}")
AppendSegmentRecording: 5 channels - 1.0kHz - 5 segments - 620 samples - 0.62s (620.00 ms)
                        float32 dtype - 12.11 KiB
Segments:
Samples:   150 | 100 | 50 | 200 | 120
Durations: 0.15s (150.00 ms) | 0.10s (100.00 ms) | 0.05s (50.00 ms) | 0.20s (200.00 ms) | 0.12s (120.00 ms)
Memory:    2.93 KiB | 1.95 KiB | 1000.00 B | 3.91 KiB | 2.34 KiB
segment 0 num_samples 150
segment 1 num_samples 100
segment 2 num_samples 50
segment 3 num_samples 200
segment 4 num_samples 120

Let’s use the concatenate_recordings():

recording_list = [rec0, rec1]
rec = concatenate_recordings(recording_list)
print(rec)
s = rec.get_num_samples(segment_index=0)
print(f"segment {0} num_samples {s}")
ConcatenateSegmentRecording: 5 channels - 1.0kHz - 1 segments - 620 samples - 0.62s (620.00 ms)
                             float32 dtype - 12.11 KiB
segment 0 num_samples 620

Total running time of the script: (0 minutes 0.003 seconds)

Gallery generated by Sphinx-Gallery