{
  "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 2 - Cross-Correlograms\n\n\nThis example shows how to compute and plot cross-correlograms of spiketrains from different units.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>The crosscorrelogram compares the output of 2 different neurons, it indicates the firing rate of one neuron\n          versus another. See `here <https://www.med.upenn.edu/mulab/crosscorrelation.html>`_ for more details</p></div>\n\n![](./../../_static/images/crosscorrelogram.png)\n\n\n\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "First import 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": [
        "Use :func:`neoStructures.NeoAll.plot_crosscorrelogram` to plot cross-correlogram between 2 units. The spiketrains are first converted into binned\nspiketrains before the computation of the cross-correlogram.\nThe package `Elephant <http://elephant.readthedocs.io/en/latest/index.html>`_ is used for the binning as well as for computing\nthe cross-correlogram.\nLet's compute the cross-correlogram between the first 2 units :\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_crosscorrelogram(0, 1)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "We can see from these cross-correlogram that the two units often fire together\nBin duration is set by default to 1ms but can be modified. The max_lag_time parameter sets the time limits of the\ncross-correlogram, its default value is set to 80 ms\nIt can be changed to zoom on the peak near the origin :\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_crosscorrelogram(0, 1, bin_time=1*ms, max_lag_time=25*ms)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "Some statistics can be computed, be setting the do_stat parameter to 1 : n_surrogates spike-trains are created\nin which a jitter is added to the time of the spikes. The jitter is computed from a normal distribution whose standard\ndeviation is fixed by the normal_dist_sd parameter. The 99% confidence interval computed from the jittered spiketrains\nis shown on top of the cross-correlogram.\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_crosscorrelogram(0, 1, do_stat=True, n_surrogates=20, normal_dist_sd=25*ms)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "If unit_pos_a and unit_pos_b parameters are equals, the autocorrelogram is computed.\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_crosscorrelogram(0, 0)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "Multiples cross-correlogram can be plot at the same time in multiple figures :\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_crosscorrelogram(0, [0, 1, 2])"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "Or in the same figure :\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_crosscorrelogram([0, 1, 2], [0, 1, 2], merge_plots=1)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "If same_yscale is True, the cross-correlograms are smoothed and the same y-scale is used.\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_crosscorrelogram([0, 1, 2], [0, 1, 2], merge_plots=1, same_yscale=1, fill_under_plot=1)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    }
  ]
}