{
  "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 4\n\n\nIn this example, we look at the single unit activity around interictal epileptic discharges (IEDs). To do this we will\nadd events and define epochs to the NeoAll instance.\n\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "from neoStructures import *\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom os.path import isdir, join, isfile\nimport seaborn as sns\nimport pickle\nsns.set()\nsns.set_context('paper')"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "In this example we will load the NeoAll instance from a pickle file\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "data_dir = join('pySpikeAnalysis', 'sample_data') if isdir('pySpikeAnalysis') else join('..', '..', 'pySpikeAnalysis', 'sample_data')\nneo_all_filename = r'neoall_071118_1132.p'\nif not isfile(join(data_dir, neo_all_filename)):\n    raise ValueError('Cannot find the file {}'.format(join(data_dir, neo_all_filename)))\nwith open(join(data_dir, neo_all_filename), 'rb') as f:\n    neoAll = pickle.load(f)"
      ],
      "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": [
        "Before creating epochs, we first need to create events. They can represents stimuli onset or epileptic events for\ninstance. In this example, each event correponds to an Interictal Epileptic Discharge. There are two ways to create\nevents, either by providing the event_filepath parameter when instanciating a NeoAll instance or by calling the\n:func:`neoStructures.NeoAll.read_event_file` method. The indexing of the csv file (i.e. to which field correspond each\ncolumn) is done in the **neoStructures_params.py** file.\nHere we can see that the neoAll already contains events :\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "print(neoAll.segments[0].events)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "We can access the times of the events and the name of the events :\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "print('Event name : {}'.format(neoAll.segments[0].events[0].name))\nprint('Event times : {}'.format(neoAll.segments[0].events[0].times))"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "Let's now define epochs on each segments (NeoAll class contains one Neo.segment for each unit), using the\n:func:`neoStructures.NeoAll.create_epochs_around_events`\nWe have to specify the epoch onset and duration (s), as well as the epochs name.\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "epoch_t_start = np.array([-0.500, -0.250, -0.050, 0.050, 0.250])\nepoch_duration = np.array([0.250, 0.200, 0.100, 0.200, 0.250])\nepoch_names = ['Pre-IED baseline', 'Pre-IED', 'IED', 'Slow wave', 'Post-IED']"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "We must specify around which event we want to create epochs\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.create_epochs_around_events('Pointe', time_offset=epoch_t_start, epoch_duration=epoch_duration,\n                                   epoch_names=epoch_names)"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "We can now study the behaviour of neurons on the different periods\nof the IEDs we have defined.\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "neoAll.plot_spikerate_change_ratio_on_epochs('IED', 'Pre-IED')"
      ],
      "execution_count": null,
      "outputs": [],
      "cell_type": "code",
      "metadata": {
        "collapsed": false
      }
    },
    {
      "source": [
        "We can see that some units start firing during the IED and are almost silent before, in the Pre-IED period. Further\nanalyses can be conducted using the NeoEpoch class which allows to plot rasterplot of the different units around\nthe events of interest.\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    }
  ]
}