Handle time information

By default, SpikeInterface assumes that a recording is uniformly sampled and it starts at 0 seconds. However, in some cases there could be a different start time or even some missing frames in the recording.

This notebook shows how to handle time information in SpikeInterface recording and sorting objects.

from spikeinterface.extractors import toy_example

First let’s generate a toy example with a single segment:

rec, sort = toy_example(num_segments=1)

Generally, the time information would be automatically loaded when reading a recording. However, sometimes we might need to add a time vector externally. For example, let’s create a time vector by getting the default times and adding 5 s:

default_times = rec.get_times()
print(default_times[:10])
new_times = default_times + 5
[0.00000000e+00 3.33333333e-05 6.66666667e-05 1.00000000e-04
 1.33333333e-04 1.66666667e-04 2.00000000e-04 2.33333333e-04
 2.66666667e-04 3.00000000e-04]

We can now set the new time vector with the set_times() function. Additionally, we can register to recording object to the sorting one so that time information can be accessed by the sorting object as well (note that this link is lost in case the sorting object is saved to disk!):

rec.set_times(new_times)
sort.register_recording(rec)

# print new times
print(rec.get_times()[:10])

# print spike times (internally uses registered recording times)
spike_times0 = sort.get_unit_spike_train(sort.unit_ids[0], return_times=True)
print(spike_times0[:10])
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/checkouts/latest/src/spikeinterface/core/baserecording.py:429: UserWarning: Setting times with Recording.set_times() is not recommended because times are not always propagated across preprocessingUse this carefully!
  warn(
[5.         5.00003333 5.00006667 5.0001     5.00013333 5.00016667
 5.0002     5.00023333 5.00026667 5.0003    ]
[5.11516667 5.1286     5.24403333 6.03073333 6.84833333 7.75933333
 8.5442     8.9273     9.3444     9.48433333]

While here we have shown how to set times only for a mono-segment recording, times can also be handled in multi-segment recordings (using the segment_index argument when calling set_times()).

Finally, you you run spike sorting through spikeinterface, the recording is automatically registered to the output sorting object!

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

Gallery generated by Sphinx-Gallery