{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Read various format into SpikeInterface\n\nSpikeInterface can read various format of \"recording\" (traces) and \"sorting\" (spike train) data.\n\nInternally, to read different formats, SpikeInterface either uses:\n  * a wrapper to the [neo](https://github.com/NeuralEnsemble/python-neo) rawio classes\n  * or a direct implementation\n\nNote that:\n\n  * file formats contain a \"recording\", a \"sorting\",  or \"both\"\n  * file formats can be file-based (NWB, ...)  or folder based (SpikeGLX, OpenEphys, ...)\n\nIn this example we demonstrate how to read different file formats into SI\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nimport spikeinterface as si\nimport spikeinterface.extractors as se"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's download some datasets in different formats from the\n[ephy_testing_data](https://gin.g-node.org/NeuralEnsemble/ephy_testing_data) repo:\n  * MEArec: an simulator format which is hdf5-based. It contains both a \"recording\" and a \"sorting\" in the same file.\n  * Spike2: file from spike2 devices. It contains \"recording\" information only.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "spike2_file_path = si.download_dataset(remote_path='spike2/130322-1LY.smr')\nprint(spike2_file_path)\n\nmearec_folder_path = si.download_dataset(remote_path='mearec/mearec_test_10s.h5')\nprint(mearec_folder_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now that we have downloaded the files let's load them into SI.\n\nThe :py:func:`~spikeinterface.extractors.read_spike2` function returns one object,\na :py:class:`~spikeinterface.core.BaseRecording`.\n\nNote that internally this file contains 2 data streams ('0' and '1'), so we need to specify which one we\nwant to retrieve ('0' in our case):\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "recording = se.read_spike2(spike2_file_path, stream_id='0')\nprint(recording)\nprint(type(recording))\nprint(isinstance(recording, si.BaseRecording))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The :py:func:`~spikeinterface.extractors.read_spike2`` function is equivalent to instantiating a\n:py:class:`~spikeinterface.extractors.Spike2RecordingExtractor` object:\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "recording = se.Spike2RecordingExtractor(spike2_file_path, stream_id='0')\nprint(recording)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The :py:func:`~spikeinterface.extractors.read_mearec` function returns two objects,\na :py:class:`~spikeinterface.core.BaseRecording` and a :py:class:`~spikeinterface.core.BaseSorting`:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "recording, sorting = se.read_mearec(mearec_folder_path)\nprint(recording)\nprint(type(recording))\nprint()\nprint(sorting)\nprint(type(sorting))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The :py:func:`~spikeinterface.extractors.read_mearec` function is equivalent to:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "recording = se.MEArecRecordingExtractor(mearec_folder_path)\nsorting = se.MEArecSortingExtractor(mearec_folder_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "SI objects (:py:class:`~spikeinterface.core.BaseRecording` and :py:class:`~spikeinterface.core.BaseSorting`) object\ncan be plotted quickly with the :py:mod:`spikeinterface.widgets` submodule:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import spikeinterface.widgets as sw\n\nw_ts = sw.plot_timeseries(recording, time_range=(0, 5))\nw_rs = sw.plot_rasters(sorting, time_range=(0, 5))\n\nplt.show()"
      ]
    }
  ],
  "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
}