{
  "nbformat": 4,
  "nbformat_minor": 0,
  "cells": [
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n# From SAB Dataset to Decoding\n\n\nThis example shows how to use the different modules of SAB_main from the creation of the SAB Dataset to decoding EEG\n\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "# import matplotlib\n# matplotlib.use('TkAgg')\nimport sab_dataset\nfrom timedecoder import *\nfrom sklearn import svm\nimport os\nimport seaborn as sns\nsns.set()\nsns.set_context('paper')\n\n\nsab_dataset_dirpath = os.path.join('pySAB', 'sample_data') if os.path.isdir('pySAB') else os.path.join('..', '..', 'pySAB', 'sample_data')\nsab_dataset_filename = 'sab_dataset_rec_subject_id_040119_1153.p'\n\nload_dataset = 1"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Dataset creation from the matlab dataset\nThis first step create the python dataset of the SAB data from the matlab dataset. All is needed is the path towards\nthe matlab dataset directory and the id of the subject.\nThe matlab directory must contain the variables EEGrec.mat, hits.mat, correctRejects.mat, falseAlarms.mat,\nomissions.mat, reactionTimes.mat\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "if not load_dataset:\n    sab_dataset = sab_dataset.SabDataset(sab_dataset_dirpath, 'subject_id')\n    ###########################################\n    # If you want to specify colors for the different conditions, use the color_dict argument. It must be a dictionnary\n    # with the condition names as keys and the colors as values. For instance :\n    # sab_dataset = sab_dataset.SabDataset('path_to_the_matlab_directory', 'subject_id',\n    #                          colors_dict={'hits': 'g', 'cr': 'r', 'omission': 'y', 'fa': 'm'})\n    # To save the newly created dataset use the save method which takes 2 arguments : the directory where the dataset\n    # will be saved and the filename. By default the filename will be similar to sab_dataset_rec_001AA_150218_1542.p\n    sab_dataset.save('path_to_save_the_dataset', 'sab_dataset_name.p')\nelse:\n    ###########################################\n    # If you have previously created the dataset, you can load it using the load_sab_dataset function. The function\n    # takes as argument the path of the file\n    sab_dataset = sab_dataset.load_sab_dataset(os.path.join(sab_dataset_dirpath, sab_dataset_filename))"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "You can print the informations of the dataset using the print function\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "print(sab_dataset)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "If needed you can downsample the data using the downsample method.\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "help(sab_dataset.downsample)\n\n# Downsample the dataset to 256Hz\nsab_dataset.downsample(2)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "TimeFeature Creation from the sab_dataset\nFor this step you just need to call the create_features method of SabDataset class which returns a TimeFeature instance\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "time_features = sab_dataset.create_features()"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "It is possible to select some channels, electrodes or trials using respectively the chan_sel, electrode_sel or trial_sel\narguments. See the help for more details :\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "help(sab_dataset.create_features)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "You can check the time_features information using again the print function\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "print(time_features)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "TimeDecoder Class.\nIn this step, the time_decoder instance is created from the TimeDecoder class. This class allow to run classification on\nthe time_features data.\nFirst we need to created a classifier to the time_decoder instance. It can be Support Vector Machine, LDA, ... as long\nas it support 2-classes classification with fit and predict methods\nIn this example, we use a SVM classifier, with C parameter set to 1\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "svm_clf = svm.SVC(kernel='linear', probability=True)\n\n# We can now create the time_decoder instance.\ntime_decoder = TimeDecoder(svm_clf)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Decoding.\nWe can now start to classify the EEG data (stored in time_features), given 2 conditions (e.g. 'hits' and 'Correct rejects')\nby using the decode method.\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "data, _, labels, _ = time_features.get_data(label_keys=[1, 2])\n# label_keys 1, 2 are the labels used for hits and correct_rejects respectively. You can see it with the label_dict argument\nprint(time_features.label_dict)\n# Run the decoding using multiples processors :\nscores = time_decoder.decode_mpver(data, labels, time_features.label_dict)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "You can select data using the arguments of get_data method (see help(time_features.get_data)).\nThe possible selection arguments are feature_pos, feature_type, feature_channame, label_keys and time_points.\nThe current time_features instance contains the following channels :\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "print(time_features.channel_names)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "If we want to select only some channel, you can specify it with the feature_channame argument :\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "data, _, labels, _ = time_features.get_data(feature_channame=['EEG TP\\'2-TP\\'3'], label_keys=[1, 2])\nscores = time_decoder.decode_mpver(data, labels, time_features.label_dict)"
      ],
      "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"
    }
  }
}