{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Handle time information\n\nBy default, SpikeInterface assumes that a recording is uniformly sampled and it starts at 0 seconds.\nHowever, in some cases there could be a different start time or even some missing frames in the recording.\n\nThis notebook shows how to handle time information in SpikeInterface recording and sorting objects.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from spikeinterface.extractors import toy_example"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First let's generate toy example with a single segment:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rec, sort = toy_example(num_segments=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generally, the time information would be automaticall loaded when reading a \nrecording. \nHowever, sometimes we might need to add a time vector externally.\nFor example, now let's create a time vector by getting the default times and \nadding 5 s:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "default_times = rec.get_times()\nprint(default_times[:10])\nnew_times = default_times + 5"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can now set the new time vector with the :code:`set_times()` function.\nAdditionally, we can register to recording object to the sorting one so that \ntime information can be accessed by the sorting object as well (note that this \nlink is lost in case the sorting object is saved to disk!):\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rec.set_times(new_times)\nsort.register_recording(rec)\n\n# print new times\nprint(rec.get_times()[:10])\n\n# print spike times (internally uses registered recording times)\nspike_times0 = sort.get_unit_spike_train(sort.unit_ids[0], return_times=True)\nprint(spike_times0[:10])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "While here we have shown how to set times only for a mono-segment recording,\ntimes can also be handled in multi-segment recordings (using the \n:code:`segment_index` argument when calling :code:`set_times()`).\n\nFinally, you you run spike sorting through :code:`spikeinterface`, the recording\nis automatically registered to the output sorting object!\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}