{
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3",
      "language": "python"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "name": "python",
      "version": "3.5.2",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "file_extension": ".py",
      "mimetype": "text/x-python"
    }
  },
  "nbformat_minor": 0,
  "nbformat": 4,
  "cells": [
    {
      "source": [
        "%matplotlib inline"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "\n# Neo All - example 1\n\n\nThis example shows how to use create a NeoAll instance.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>NeoAll instances are created from the SpykingCircus files. See `here <https://spyking-circus.readthedocs.io/en/latest/advanced/files.html>`_\n    for details about these results files. NeoAll needs at least three arguments to be initiated :\n\n    * spikesorting_dirpath : the path of the directory containng the spyking circus result file\n    * spikesorting_filename : the name of the Spiking Circus result file\n    * probe_filepath : the path to the Spiking Circus probe file</p></div>\n\n\nFor instance, the Spyking Cirucs files may look like these :\n\n![](./../../_static/images/spykingcircus_files.png)\n\n\nDetails of the files :\n * The *000_AA.prb* file is the probe file, it contains information about how contacts are organized on the electrodes, see `here <https://spyking-circus.readthedocs.io/en/latest/code/probe.html>`_ for more info. This file is needed for launching the spike-sorting. It is used in NeoAll for creating channel indexes and knowing the number of tetrodes.\n * The *.result.hdf5* file is used to store the spike times for each unit, at the end of the spike sorting procedure.\n * The *.template.hdf5* file contains the preffered contact of each unit.\n * In order to generate the units template, three files are used : *.basis.hdf5*, *.templates.hdf5* and *.clusters.hdf5* files. Unit template is computed using the :func:`~spykingcircus_utils.get_templates_from_spykingcircus_file` function.\n\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "First import the neoStructures :\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "from neoStructures import *\nimport matplotlib.pyplot as plt\nfrom os.path import isdir, join"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "Import the data and create the NeoAll instance\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "data_dir = join('pySpikeAnalysis', 'sample_data') if isdir('pySpikeAnalysis') else join('..', '..', 'pySpikeAnalysis', 'sample_data')\nspykingcircus_dir = r'SpykingCircus_results'\nprobe_filename = r'000_AA.prb'\nresults_filename = r'spykingcircusres'\n\nneoAll = NeoAll(join(data_dir, spykingcircus_dir), results_filename, join(data_dir, probe_filename), save_fig=0)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "See information about NeoAll\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "print(neoAll)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "neoAll contains a list of Neo Segments (1 segment per unit)\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "print(type(neoAll.segments[0]))"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "Computing the mean firing rate is easy, e.g. for unit 3:\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "mean_spkrate_3 = len(neoAll.segments[3].spiketrains[0]) / (neoAll.segments[3].t_stop - neoAll.segments[3].t_start)\nprint(mean_spkrate_3)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "The method :func:`neoStructures.NeoAll.plot_spikerate_evolution` can be used to visualize the firing-rate over time\nThe firing rate is computed by kernel convolution. The kernel is gaussian and the standard deviation can be set using\nthe sigma_gauss_kernel parameter.\nThe package `Elephant <http://elephant.readthedocs.io/en/latest/index.html>`_ is used for the estimation.\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_spikerate_evolution(unit_pos=3, sigma_gauss_kernel=30*s)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "The shape of each unit can be plotted using the plot_unit_shape method :\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_unit_shape(2)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "As well as the ISI (Inter-Spike Interval), e.g. for unit 22 :\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_isi(unit_pos=22)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "Zoom on the x-origin and increase the number of bins for the histogram\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_isi(22, bin_duration_ms=0.1, tmax_ms=15)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    }
  ]
}