{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\nWhat is Deconvolution?\n======================\nNeuroscientists (amongst others) are often interested in time series that are derived\nfrom neural activity, such as fMRI BOLD and pupil dilation. However, for some classes\nof data (notably, pupil dilation and fMRI BOLD), neural activity gets temporally delayed and \ndispersed. This means that if the time series is related to some behavioral events that \nare close together in time, these event-related responses will contaminate each other.\n\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "from nideconv import simulate\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style('white')\nsns.set_context('notebook')"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "Simulate data\n-------------\nHere we simulate fMRI data with a \"cue - stimulus\" design.\nThere are four cues and stimulus pairs.\nThe cue is always followed by a stimulus in 1, 2, 3, or 4 seconds.\nThe cue leads to a small de-activation (0.5 % signal change), the stimulus a\nslight activation (1.0 % signal change)\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "cue_onsets = [5, 15, 25, 35]\nstim_onsets = [6, 17, 28, 39]\n\ncue_pars = {'name':'cue',\n            'mu_group':-.5,\n            'std_group':0,\n            'onsets':cue_onsets}\n\nstim_pars = {'name':'stim',\n             'mu_group':1,\n             'std_group':0,\n             'onsets':stim_onsets}\n\nconditions = [cue_pars,\n              stim_pars]\n\ndata, onsets, parameters = simulate.simulate_fmri_experiment(conditions,\n                                                             run_duration=60,\n                                                             noise_level=0.1)"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "Plot simulated data\n-------------------\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "data.plot()\nsns.despine()\n\nfor onset in cue_onsets:\n    l1 =plt.axvline(onset, c='r')\n\nfor onset in stim_onsets:\n    l2 =plt.axvline(onset, c='g')\n\nplt.legend([l1, l2], ['Cue', 'Stimulus'])\nplt.gcf().set_size_inches(10, 4)"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "Underlying data-generating model\n--------------------------------\nBecause we simulated the data, we know that the event-related responses should\nexactly follow the *canonical Hemodynamic Response Function* [1]_are\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "from nideconv.utils import double_gamma_with_d\nimport numpy as np\n\nplt.figure(figsize=(12, 4))\n\nt = np.linspace(0, 20, 100)\nax1 = plt.subplot(121)\nplt.title('Ground truth cue-related response')\nplt.plot(t, double_gamma_with_d(t) * -.5,\n         c=sns.color_palette()[0])\nplt.xlabel('Time since event (s)')\nplt.ylabel('Percent signal change')\nplt.axhline(0, c='k', ls='--')\n\nplt.subplot(122, sharey=ax1)\nplt.title('Ground truth stimulus-related response')\nplt.plot(t, double_gamma_with_d(t),\n         c=sns.color_palette()[1])\nplt.axhline(0, c='k', ls='--')\nplt.xlabel('Time since event (s)')\nplt.ylabel('Percent signal change')\n\nsns.despine()"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "Naive approach: epoched averaging\n---------------------------------\nA simple approach that is more appropriate for fast electrphysiological signals\nlike EEG and MEG would be to select little chunks of the time series,\ncorresponding to the onset of our events-of-interest and the first 20 seconds\n(\"epoching\").\n\nnideconv\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "source": [
        "References\n-----------\n.. [1] Glover et al., 1999\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }
  ], 
  "metadata": {
    "kernelspec": {
      "display_name": "Python 2", 
      "name": "python2", 
      "language": "python"
    }, 
    "language_info": {
      "mimetype": "text/x-python", 
      "nbconvert_exporter": "python", 
      "name": "python", 
      "file_extension": ".py", 
      "version": "2.7.12", 
      "pygments_lexer": "ipython2", 
      "codemirror_mode": {
        "version": 2, 
        "name": "ipython"
      }
    }
  }
}