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
Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/checkouts/0.98.0/examples/modules_gallery/core/plot_6_handle_times.py", line 10, in <module>
    from spikeinterface.extractors import toy_example
  File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.98.0/lib/python3.9/site-packages/spikeinterface/extractors/__init__.py", line 1, in <module>
    from .extractorlist import *
  File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.98.0/lib/python3.9/site-packages/spikeinterface/extractors/extractorlist.py", line 15, in <module>
    from .neoextractors import *
  File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.98.0/lib/python3.9/site-packages/spikeinterface/extractors/neoextractors/__init__.py", line 1, in <module>
    from .alphaomega import AlphaOmegaRecordingExtractor, AlphaOmegaEventExtractor, read_alphaomega, read_alphaomega_event
  File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.98.0/lib/python3.9/site-packages/spikeinterface/extractors/neoextractors/alphaomega.py", line 3, in <module>
    from .neobaseextractor import NeoBaseRecordingExtractor, NeoBaseEventExtractor
  File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.98.0/lib/python3.9/site-packages/spikeinterface/extractors/neoextractors/neobaseextractor.py", line 332, in <module>
    class NeoBaseSortingExtractor(_NeoBaseExtractor, BaseSorting):
  File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.98.0/lib/python3.9/site-packages/spikeinterface/extractors/neoextractors/neobaseextractor.py", line 480, in NeoBaseSortingExtractor
    def _infer_t_start_from_signal_stream(self, segment_index: int, stream_id: Optional[str] = None) -> float | None:
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

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

rec, sort = toy_example(num_segments=1)

Generally, the time information would be automaticall loaded when reading a recording. However, sometimes we might need to add a time vector externally. For example, now 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

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])

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.002 seconds)

Gallery generated by Sphinx-Gallery