{
  "nbformat": 4,
  "nbformat_minor": 0,
  "cells": [
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n#       Feature Evolution and Distribution\n\n\nThis example shows how to plot feature evolution, or the distribution of a feature over trials\nat a certain time point.\n\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "# import matplotlib\n# matplotlib.use('TkAgg')\nfrom os.path import isdir, join\nimport sab_dataset\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 = sab_dataset.load_sab_dataset(join(sab_dataset_dirpath, sab_dataset_filename))"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Downsample the data\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "rec_dataset.downsample(2)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Construct the features from the SabDataset object - Select only 'hits' and 'correct rejects' trials and keep only\n2 electrodes of interest :\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "time_features = rec_dataset.create_features(trial_sel=(rec_dataset.hits | rec_dataset.correct_rejects))\nprint(time_features)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Extract features, if called without any parameter, the function return the possible feature to extract\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "time_features.extract_feature()"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Extract the discrete wavelet transform coefficients, The new features are automatically added\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "time_features.extract_feature('dwt')\nprint(time_features)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Plot the evolution of feature 19 :\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "time_features.plot_feature_erp(feature_pos=2)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Plot the evolution of feature DWT 16-32 Hz for channel TB'10-TB'11 :\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "time_features.plot_feature_erp(feature_type='DWT 16-32', feature_channame='EEG TP\\'3-TP\\'4')"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Plot the distribution of a feature at a certain time point. The 'time_points' parameter must be passed and contain\nonly 1 time point\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "time_point_sel = time_features.time2sample(0.55)\ntime_features.plot_feature_distribution(time_points=time_point_sel, feature_pos=10)"
      ],
      "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"
    }
  }
}