{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n# XDAWN Decoding From EEG data\n\n\nERP decoding with Xdawn. For each event type, a set of spatial Xdawn filters\nare trained and applied on the signal. Channels are concatenated and rescaled\nto create features vectors that will be fed into a Logistic Regression.\n\nReferences\n----------\n\n.. [1] Rivet, B., Souloumiac, A., Attina, V., & Gibert, G. (2009). xDAWN\n algorithm to enhance evoked potentials: application to brain-computer\n interface. Biomedical Engineering, IEEE Transactions on, 56(8),\n 2035-2043.\n.. [2] Rivet, B., Cecotti, H., Souloumiac, A., Maby, E., & Mattout, J. (2011,\n August). Theoretical analysis of xDAWN algorithm: application to an\n efficient sensor selection in a P300 BCI. In Signal Processing\n Conference, 2011 19th European (pp. 1382-1386). IEEE.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Authors: Alexandre Barachant \n#\n# License: BSD (3-clause)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.cross_validation import StratifiedKFold\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.preprocessing import MinMaxScaler\n\nfrom mne import io, pick_types, read_events, Epochs\nfrom mne.datasets import sample\nfrom mne.preprocessing import Xdawn\nfrom mne.decoding import Vectorizer\nfrom mne.viz import tight_layout\n\n\nprint(__doc__)\n\ndata_path = sample.data_path()" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Set parameters and read data\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'\nevent_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'\ntmin, tmax = -0.1, 0.3\nevent_id = dict(aud_l=1, aud_r=2, vis_l=3, vis_r=4)\n\n# Setup for reading the raw data\nraw = io.read_raw_fif(raw_fname, preload=True)\nraw.filter(1, 20)\nevents = read_events(event_fname)\n\npicks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False,\n exclude='bads')\n\nepochs = Epochs(raw, events, event_id, tmin, tmax, proj=False,\n picks=picks, baseline=None, preload=True,\n verbose=False)\n\n# Create classification pipeline\nclf = make_pipeline(Xdawn(n_components=3),\n Vectorizer(),\n MinMaxScaler(),\n LogisticRegression(penalty='l1'))\n\n# Get the labels\nlabels = epochs.events[:, -1]\n\n# Cross validator\ncv = StratifiedKFold(y=labels, n_folds=10, shuffle=True, random_state=42)\n\n# Do cross-validation\npreds = np.empty(len(labels))\nfor train, test in cv:\n clf.fit(epochs[train], labels[train])\n preds[test] = clf.predict(epochs[test])\n\n# Classification report\ntarget_names = ['aud_l', 'aud_r', 'vis_l', 'vis_r']\nreport = classification_report(labels, preds, target_names=target_names)\nprint(report)\n\n# Normalized confusion matrix\ncm = confusion_matrix(labels, preds)\ncm_normalized = cm.astype(float) / cm.sum(axis=1)[:, np.newaxis]\n\n# Plot confusion matrix\nplt.imshow(cm_normalized, interpolation='nearest', cmap=plt.cm.Blues)\nplt.title('Normalized Confusion matrix')\nplt.colorbar()\ntick_marks = np.arange(len(target_names))\nplt.xticks(tick_marks, target_names, rotation=45)\nplt.yticks(tick_marks, target_names)\ntight_layout()\nplt.ylabel('True label')\nplt.xlabel('Predicted label')\nplt.show()" ], "outputs": [], "metadata": { "collapsed": false } } ], "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.13", "pygments_lexer": "ipython2", "codemirror_mode": { "version": 2, "name": "ipython" } } } }