{
  "nbformat": 4,
  "nbformat_minor": 0,
  "cells": [
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n#  Inter-Trial Phase Clustering\n\n\nThis example shows how to compute and plot the ITPC : Inter-Trial Phase Coherence from a SabDataset instance\n\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "# import matplotlib\n# matplotlib.use('TkAgg')\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom os.path import isdir, join\nplt.rcParams['image.cmap'] = 'viridis'\nfrom sab_dataset import *\nimport seaborn as sns\nsns.set()\nsns.set_context('paper')"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Load the data : sab dataset\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "sab_dataset_dirpath = join('pySAB', 'sample_data') if isdir('pySAB') else join('..', '..', 'pySAB', 'sample_data')\nsab_dataset_filename = 'sab_dataset_rec_subject_id_040119_1153.p'\nrec_dataset = load_sab_dataset(join(sab_dataset_dirpath, sab_dataset_filename))"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Print dataset information\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "print(rec_dataset)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Downsample the dataset\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "rec_dataset.downsample(2)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Select filter center frequencies and filter bandwidth\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "n_filters = 30\nfilt_cf = np.logspace(np.log10(3), np.log10(70), n_filters)\nfilt_bw = np.logspace(np.log10(1.5), np.log10(20), n_filters)\nf = plt.figure()\nax = f.add_subplot(111)\nax.scatter(np.arange(n_filters), filt_cf, zorder=2)\nax.vlines(np.arange(n_filters), filt_cf-filt_bw/2, filt_cf+filt_bw/2, zorder=1)\nax.set(title='Filters center frequency and bandwidth', xlabel='Filter index', ylabel='(Hz)')\nplt.legend(['Center frequency', 'Bandwidth'])"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Compute and plot the ITPC of channel 4, for hits trials\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "rec_dataset.plot_itpc(4, rec_dataset.hits, filt_cf, filt_bw, n_monte_carlo=1, contour_plot=1)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    }
  ],
  "metadata": {
    "language_info": {
      "version": "3.5.2",
      "name": "python",
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "codemirror_mode": {
        "version": 3,
        "name": "ipython"
      },
      "file_extension": ".py",
      "pygments_lexer": "ipython3"
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3",
      "language": "python"
    }
  }
}