{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Append and/or concatenate segments\n\nSometimes a recording can be split in several subparts, for instance a baseline and an intervention.\n\nSimilarly to [NEO](https://github.com/NeuralEnsemble/python-neo) we define each subpart as a \"segment\".\n\nSpikeInterface has tools to manipulate these segments. There are two ways:\n\n  1. :py:func:`~spikeinterface.core.append_recordings()` and :py:func:`~spikeinterface.core.append_sortings()`\n\n  2. :py:func:`~spikeinterface.core.concatenate_recordings()`\n\nHere is the difference. Imagine we have 2 recordings with 2 and 3 segments respectively:\n\n  1. In case 1. (append) we will end up with one recording with 5 segments.\n  2. In case 2. (concatenate) we will end up with one recording with 1 \"big\" segment\n  that virtually concatenates all segments.\n\nHere is a short example.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\n\nfrom spikeinterface import NumpyRecording, NumpySorting\nfrom spikeinterface import append_recordings, concatenate_recordings"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First let's generate 2 recordings with 2 and 3 segments respectively:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sampling_frequency = 1000.\n\ntrace0 = np.zeros((150, 5), dtype='float32')\ntrace1 = np.zeros((100, 5), dtype='float32')\nrec0 = NumpyRecording([trace0, trace1], sampling_frequency)\nprint(rec0)\n\ntrace2 = np.zeros((50, 5), dtype='float32')\ntrace3 = np.zeros((200, 5), dtype='float32')\ntrace4 = np.zeros((120, 5), dtype='float32')\nrec1 = NumpyRecording([trace2, trace3, trace4], sampling_frequency)\nprint(rec1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's use the :py:func:`~spikeinterface.core.append_recordings()`:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "recording_list = [rec0, rec1]\nrec = append_recordings(recording_list)\nprint(rec)\nfor i in range(rec.get_num_segments()):\n    s = rec.get_num_samples(segment_index=i)\n    print(f'segment {i} num_samples {s}')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's use the :py:func:`~spikeinterface.core.concatenate_recordings()`:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "recording_list = [rec0, rec1]\nrec = concatenate_recordings(recording_list)\nprint(rec)\ns = rec.get_num_samples(segment_index=0)\nprint(f'segment {0} num_samples {s}')"
      ]
    }
  ],
  "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
}