Within Session P300#

This example shows how to perform a within session analysis on three different P300 datasets.

We will compare two pipelines :

  • Riemannian geometry

  • XDAWN with Linear Discriminant Analysis

We will use the P300 paradigm, which uses the AUC as metric.

# Authors: Pedro Rodrigues <pedro.rodrigues01@gmail.com>
#
# License: BSD (3-clause)

import warnings

import matplotlib.pyplot as plt
import seaborn as sns
from mne.decoding import Vectorizer
from pyriemann.estimation import Xdawn, XdawnCovariances
from pyriemann.tangentspace import TangentSpace
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.pipeline import make_pipeline

import moabb
from moabb.datasets import BNCI2014_009
from moabb.evaluations import WithinSessionEvaluation
from moabb.paradigms import P300

getting rid of the warnings about the future

warnings.simplefilter(action="ignore", category=FutureWarning)
warnings.simplefilter(action="ignore", category=RuntimeWarning)

moabb.set_log_level("info")

Create Pipelines#

Pipelines must be a dict of sklearn pipeline transformer.

pipelines = {}

We have to do this because the classes are called ‘Target’ and ‘NonTarget’ but the evaluation function uses a LabelEncoder, transforming them to 0 and 1

labels_dict = {"Target": 1, "NonTarget": 0}

pipelines["RG+LDA"] = make_pipeline(
    XdawnCovariances(
        nfilter=2, classes=[labels_dict["Target"]], estimator="lwf", xdawn_estimator="scm"
    ),
    TangentSpace(),
    LDA(solver="lsqr", shrinkage="auto"),
)

pipelines["Xdw+LDA"] = make_pipeline(
    Xdawn(nfilter=2, estimator="scm"), Vectorizer(), LDA(solver="lsqr", shrinkage="auto")
)

Evaluation#

We define the paradigm (P300) and use all three datasets available for it. The evaluation will return a DataFrame containing a single AUC score for each subject / session of the dataset, and for each pipeline.

Results are saved into the database, so that if you add a new pipeline, it will not run again the evaluation unless a parameter has changed. Results can be overwritten if necessary.

paradigm = P300(resample=128)
dataset = BNCI2014_009()
dataset.subject_list = dataset.subject_list[:2]
datasets = [dataset]
overwrite = True  # set to True if we want to overwrite cached results
evaluation = WithinSessionEvaluation(
    paradigm=paradigm, datasets=datasets, suffix="examples", overwrite=overwrite
)
results = evaluation.process(pipelines)
BNCI2014-009-WithinSession:   0%|          | 0/2 [00:00<?, ?it/s]

  0%|                                              | 0.00/18.5M [00:00<?, ?B/s]

  0%|                                     | 8.19k/18.5M [00:00<05:36, 55.1kB/s]

  0%|                                      | 48.1k/18.5M [00:00<01:42, 180kB/s]

  1%|▎                                      | 128k/18.5M [00:00<00:53, 341kB/s]

  2%|▌                                      | 289k/18.5M [00:00<00:28, 630kB/s]

  3%|█▏                                    | 608k/18.5M [00:00<00:15, 1.17MB/s]

  7%|██▍                                  | 1.24M/18.5M [00:00<00:07, 2.20MB/s]

 14%|█████                                | 2.52M/18.5M [00:01<00:03, 4.27MB/s]

 27%|██████████                           | 5.06M/18.5M [00:01<00:01, 8.31MB/s]

 42%|███████████████▍                     | 7.71M/18.5M [00:01<00:00, 11.2MB/s]

 54%|████████████████████                 | 10.0M/18.5M [00:01<00:00, 12.5MB/s]

 67%|████████████████████████▊            | 12.4M/18.5M [00:01<00:00, 13.6MB/s]

 80%|█████████████████████████████▊       | 14.9M/18.5M [00:01<00:00, 14.5MB/s]

 93%|██████████████████████████████████▌  | 17.3M/18.5M [00:01<00:00, 15.0MB/s]

  0%|                                              | 0.00/18.5M [00:00<?, ?B/s]
100%|█████████████████████████████████████| 18.5M/18.5M [00:00<00:00, 50.4GB/s]

BNCI2014-009-WithinSession:  50%|█████     | 1/2 [00:10<00:10, 10.80s/it]

  0%|                                              | 0.00/18.5M [00:00<?, ?B/s]

  0%|                                      | 16.4k/18.5M [00:00<02:48, 110kB/s]

  0%|▏                                     | 64.5k/18.5M [00:00<01:18, 235kB/s]

  1%|▎                                      | 161k/18.5M [00:00<00:43, 421kB/s]

  2%|▋                                      | 352k/18.5M [00:00<00:23, 759kB/s]

  4%|█▌                                    | 736k/18.5M [00:00<00:12, 1.41MB/s]

  8%|███                                  | 1.51M/18.5M [00:00<00:06, 2.69MB/s]

 17%|██████                               | 3.06M/18.5M [00:01<00:02, 5.19MB/s]

 28%|██████████▍                          | 5.23M/18.5M [00:01<00:01, 8.14MB/s]

 38%|██████████████▏                      | 7.08M/18.5M [00:01<00:01, 9.44MB/s]

 49%|██████████████████▏                  | 9.13M/18.5M [00:01<00:00, 10.7MB/s]

 60%|██████████████████████               | 11.1M/18.5M [00:01<00:00, 11.4MB/s]

 70%|██████████████████████████           | 13.1M/18.5M [00:01<00:00, 12.0MB/s]

 81%|█████████████████████████████▉       | 15.0M/18.5M [00:01<00:00, 12.3MB/s]

 92%|█████████████████████████████████▉   | 17.0M/18.5M [00:02<00:00, 12.5MB/s]

  0%|                                              | 0.00/18.5M [00:00<?, ?B/s]
100%|█████████████████████████████████████| 18.5M/18.5M [00:00<00:00, 82.8GB/s]

BNCI2014-009-WithinSession: 100%|██████████| 2/2 [00:21<00:00, 10.92s/it]
BNCI2014-009-WithinSession: 100%|██████████| 2/2 [00:21<00:00, 10.90s/it]

Plot Results#

Here we plot the results to compare the two pipelines

fig, ax = plt.subplots(facecolor="white", figsize=[8, 4])

sns.stripplot(
    data=results,
    y="score",
    x="pipeline",
    ax=ax,
    jitter=True,
    alpha=0.5,
    zorder=1,
    palette="Set1",
)
sns.pointplot(data=results, y="score", x="pipeline", ax=ax, palette="Set1")

ax.set_ylabel("ROC AUC")
ax.set_ylim(0.5, 1)

plt.show()
plot within session p300

Total running time of the script: (0 minutes 24.390 seconds)

Estimated memory usage: 388 MB

Gallery generated by Sphinx-Gallery