Examples of analysis of a Dreyer2023 A dataset.#

This example shows how to plot Dreyer2023A Left-Right Imagery ROC AUC scores obtained with CSP+LDA pipeline versus demographic information of the examined subjects (gender and age) and experimenters (gender).

To reduce computational time, the example is provided for four subjects.

# Authors: Sara Sedlar <sara.sedlar@gmail.com>
#          Sylvain Chevallier <sylvain.chevallier@universite-paris-saclay.fr>
# License: BSD (3-clause)

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import seaborn as sb
from pyriemann.estimation import Covariances
from pyriemann.spatialfilters import CSP
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.pipeline import make_pipeline

from moabb.datasets import Dreyer2023A
from moabb.evaluations import WithinSessionEvaluation
from moabb.paradigms import MotorImagery
  1. Defining dataset, selecting subject for analysis and getting data

  0%|                                              | 0.00/5.84k [00:00<?, ?B/s]
  0%|                                              | 0.00/5.84k [00:00<?, ?B/s]
100%|█████████████████████████████████████| 5.84k/5.84k [00:00<00:00, 50.4MB/s]

0it [00:00, ?it/s]

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

  0%|                                                | 0.00/698 [00:00<?, ?B/s]
100%|█████████████████████████████████████████| 698/698 [00:00<00:00, 6.68MB/s]

1it [00:01,  1.71s/it]

  0%|                                              | 0.00/89.0k [00:00<?, ?B/s]

  0%|                                              | 0.00/89.0k [00:00<?, ?B/s]
100%|██████████████████████████████████████| 89.0k/89.0k [00:00<00:00, 353MB/s]

2it [00:03,  1.56s/it]

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

  0%|                                                | 0.00/303 [00:00<?, ?B/s]
100%|█████████████████████████████████████████| 303/303 [00:00<00:00, 2.79MB/s]

3it [00:04,  1.51s/it]

  0%|                                               | 0.00/485k [00:00<?, ?B/s]

 37%|██████████████▎                        | 177k/485k [00:00<00:00, 1.76MB/s]

  0%|                                               | 0.00/485k [00:00<?, ?B/s]
100%|███████████████████████████████████████| 485k/485k [00:00<00:00, 1.97GB/s]

4it [00:06,  1.55s/it]

  0%|                                              | 0.00/1.92k [00:00<?, ?B/s]

  0%|                                              | 0.00/1.92k [00:00<?, ?B/s]
100%|█████████████████████████████████████| 1.92k/1.92k [00:00<00:00, 19.1MB/s]

5it [00:08,  1.77s/it]

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

  0%|                                                | 0.00/788 [00:00<?, ?B/s]
100%|█████████████████████████████████████████| 788/788 [00:00<00:00, 8.37MB/s]

6it [00:09,  1.60s/it]

  0%|                                              | 0.00/2.22k [00:00<?, ?B/s]

  0%|                                              | 0.00/2.22k [00:00<?, ?B/s]
100%|█████████████████████████████████████| 2.22k/2.22k [00:00<00:00, 23.0MB/s]

7it [00:11,  1.63s/it]

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

 14%|█████▏                                | 189k/1.39M [00:00<00:00, 1.88MB/s]

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

8it [00:12,  1.63s/it]

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

  0%|                                      | 179k/72.6M [00:00<00:41, 1.76MB/s]

  3%|█                                    | 2.15M/72.6M [00:00<00:05, 12.2MB/s]

 19%|███████                              | 13.8M/72.6M [00:00<00:00, 59.4MB/s]

 31%|███████████▋                         | 22.8M/72.6M [00:00<00:00, 71.6MB/s]

 46%|████████████████▉                    | 33.1M/72.6M [00:00<00:00, 82.9MB/s]

 57%|█████████████████████▏               | 41.4M/72.6M [00:00<00:00, 80.3MB/s]

 69%|█████████████████████████▌           | 50.1M/72.6M [00:00<00:00, 82.3MB/s]

 84%|███████████████████████████████      | 60.8M/72.6M [00:00<00:00, 66.8MB/s]

100%|████████████████████████████████████▊| 72.3M/72.6M [00:01<00:00, 78.7MB/s]

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

9it [00:16,  2.07s/it]
9it [00:16,  1.78s/it]

0it [00:00, ?it/s]

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

  0%|                                      | 122k/65.0M [00:00<00:53, 1.21MB/s]

  1%|▍                                     | 654k/65.0M [00:00<00:17, 3.61MB/s]

  5%|█▉                                   | 3.44M/65.0M [00:00<00:04, 14.6MB/s]

 18%|██████▌                              | 11.6M/65.0M [00:00<00:01, 40.8MB/s]

 30%|███████████▏                         | 19.7M/65.0M [00:00<00:00, 55.6MB/s]

 40%|██████████████▋                      | 25.8M/65.0M [00:00<00:00, 57.3MB/s]

 52%|███████████████████                  | 33.6M/65.0M [00:00<00:00, 60.9MB/s]

 64%|███████████████████████▋             | 41.7M/65.0M [00:00<00:00, 64.0MB/s]

 76%|████████████████████████████         | 49.2M/65.0M [00:00<00:00, 67.5MB/s]

 92%|██████████████████████████████████▏  | 60.1M/65.0M [00:01<00:00, 76.7MB/s]

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

9it [00:02,  3.19it/s]
9it [00:02,  3.19it/s]

0it [00:00, ?it/s]

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

  0%|                                      | 166k/64.3M [00:00<00:38, 1.65MB/s]

  2%|▋                                    | 1.13M/64.3M [00:00<00:09, 6.34MB/s]

 13%|████▋                                | 8.21M/64.3M [00:00<00:01, 35.8MB/s]

 26%|█████████▌                           | 16.7M/64.3M [00:00<00:00, 54.8MB/s]

 37%|█████████████▋                       | 23.8M/64.3M [00:00<00:00, 60.8MB/s]

 54%|███████████████████▊                 | 34.4M/64.3M [00:00<00:00, 76.1MB/s]

 65%|████████████████████████▏            | 42.0M/64.3M [00:00<00:00, 74.8MB/s]

 81%|██████████████████████████████       | 52.3M/64.3M [00:00<00:00, 81.5MB/s]

 99%|████████████████████████████████████▊| 64.0M/64.3M [00:00<00:00, 92.3MB/s]

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

9it [00:02,  3.36it/s]
9it [00:02,  3.36it/s]

0it [00:00, ?it/s]

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

  0%|                                      | 166k/67.2M [00:00<00:40, 1.66MB/s]

  2%|▊                                    | 1.46M/67.2M [00:00<00:07, 8.30MB/s]

 16%|█████▉                               | 10.8M/67.2M [00:00<00:01, 47.3MB/s]

 24%|████████▊                            | 16.1M/67.2M [00:00<00:01, 49.3MB/s]

 31%|███████████▌                         | 21.0M/67.2M [00:00<00:00, 47.5MB/s]

 44%|████████████████▏                    | 29.3M/67.2M [00:00<00:00, 59.3MB/s]

 54%|███████████████████▉                 | 36.2M/67.2M [00:00<00:00, 62.3MB/s]

 64%|███████████████████████▊             | 43.3M/67.2M [00:00<00:00, 65.1MB/s]

 81%|██████████████████████████████       | 54.5M/67.2M [00:00<00:00, 63.6MB/s]

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

9it [00:02,  3.44it/s]
9it [00:02,  3.44it/s]

{1: {'0': {'0R1acquisition': <RawEDF | sub-01_task-R1acquisition_eeg.edf, 32 x 230400 (450.0 s), ~56.3 MiB, data loaded>, '1R2acquisition': <RawEDF | sub-01_task-R2acquisition_eeg.edf, 32 x 230400 (450.0 s), ~56.3 MiB, data loaded>, '2R3online': <RawEDF | sub-01_task-R3online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '3R4online': <RawEDF | sub-01_task-R4online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '4R5online': <RawEDF | sub-01_task-R5online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '5R6online': <RawEDF | sub-01_task-R6online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>}}, 5: {'0': {'0R1acquisition': <RawEDF | sub-05_task-R1acquisition_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '1R2acquisition': <RawEDF | sub-05_task-R2acquisition_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '2R3online': <RawEDF | sub-05_task-R3online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '3R4online': <RawEDF | sub-05_task-R4online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '4R5online': <RawEDF | sub-05_task-R5online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '5R6online': <RawEDF | sub-05_task-R6online_eeg.edf, 32 x 234496 (458.0 s), ~57.3 MiB, data loaded>}}, 7: {'0': {'0R1acquisition': <RawEDF | sub-07_task-R1acquisition_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '1R2acquisition': <RawEDF | sub-07_task-R2acquisition_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '2R3online': <RawEDF | sub-07_task-R3online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '3R4online': <RawEDF | sub-07_task-R4online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '4R5online': <RawEDF | sub-07_task-R5online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '5R6online': <RawEDF | sub-07_task-R6online_eeg.edf, 32 x 230400 (450.0 s), ~56.3 MiB, data loaded>}}, 35: {'0': {'0R1acquisition': <RawEDF | sub-35_task-R1acquisition_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '1R2acquisition': <RawEDF | sub-35_task-R2acquisition_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '2R3online': <RawEDF | sub-35_task-R3online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '3R4online': <RawEDF | sub-35_task-R4online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '4R5online': <RawEDF | sub-35_task-R5online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>, '5R6online': <RawEDF | sub-35_task-R6online_eeg.edf, 32 x 230912 (451.0 s), ~56.4 MiB, data loaded>}}}
  1. Defining MotorImagery paradigm and CSP+LDA pipeline

paradigm = MotorImagery()
pipelines = {}
pipelines["CSP+LDA"] = make_pipeline(
    Covariances(estimator="oas"), CSP(nfilter=6), LDA(solver="lsqr", shrinkage="auto")
)
  1. Within session evaluation of the pipeline

evaluation = WithinSessionEvaluation(
    paradigm=paradigm, datasets=[dreyer2023], suffix="examples", overwrite=False
)
results = evaluation.process(pipelines)
0it [00:00, ?it/s]
9it [00:00, 11015.10it/s]

0it [00:00, ?it/s]
9it [00:00, 15860.81it/s]

0it [00:00, ?it/s]
9it [00:00, 15702.47it/s]

0it [00:00, ?it/s]
9it [00:00, 17142.93it/s]
  1. Loading dataset info and concatenation with the obtained results

info = dreyer2023.get_subject_info().rename(columns={"score": "score_MR"})
# Creating a new column with subject's age
info["Age"] = 2019 - info["Birth_year"]
# Casting to int for merging
info["subject"] = info["SUJ_ID"].astype(int)
results["subject"] = results["subject"].astype(int)

results_info = results.merge(info, on="subject", how="left")
  0%|                                              | 0.00/19.8k [00:00<?, ?B/s]
  0%|                                              | 0.00/19.8k [00:00<?, ?B/s]
100%|██████████████████████████████████████| 19.8k/19.8k [00:00<00:00, 110MB/s]

5.1 Plotting subject AUC ROC scores vs subject’s gender

fig, ax = plt.subplots(nrows=2, ncols=2, facecolor="white", figsize=[16, 8], sharey=True)
fig.subplots_adjust(wspace=0.0, hspace=0.5)
sb.boxplot(
    data=results_info, y="score", x="SUJ_gender", ax=ax[0, 0], palette="Set1", width=0.3
)
sb.stripplot(
    data=results_info,
    y="score",
    x="SUJ_gender",
    ax=ax[0, 0],
    palette="Set1",
    linewidth=1,
    edgecolor="k",
    size=3,
    alpha=0.3,
    zorder=1,
)
ax[0, 0].set_title("AUC ROC scores vs. subject gender")
ax[0, 0].set_xticklabels(["Man", "Woman"])
ax[0, 0].set_ylabel("ROC AUC")
ax[0, 0].set_xlabel(None)
ax[0, 0].set_ylim(0.3, 1)
AUC ROC scores vs. subject gender
(0.3, 1.0)

5.2 Plotting subject AUC ROC scores vs subjects’s age per gender

sb.regplot(
    data=results_info[results_info["SUJ_gender"] == 1][["score", "Age"]].astype(
        "float32"
    ),
    y="score",
    x="Age",
    ax=ax[0, 1],
    scatter_kws={"color": "#e41a1c", "alpha": 0.5},
    line_kws={"color": "#e41a1c"},
)
sb.regplot(
    data=results_info[results_info["SUJ_gender"] == 2][["score", "Age"]].astype(
        "float32"
    ),
    y="score",
    x="Age",
    ax=ax[0, 1],
    scatter_kws={"color": "#377eb8", "alpha": 0.5},
    line_kws={"color": "#377eb8"},
)
ax[0, 1].set_title("AUC ROC scores vs. subject age per gender")
ax[0, 1].set_ylabel(None)
ax[0, 1].set_xlabel(None)
ax[0, 1].legend(
    handles=[
        mpatches.Patch(color="#e41a1c", label="Man"),
        mpatches.Patch(color="#377eb8", label="Woman"),
    ]
)
<matplotlib.legend.Legend object at 0x7f4516a61350>

5.3 Plotting subject AUC ROC scores vs experimenter’s gender

sb.boxplot(
    data=results_info, y="score", x="EXP_gender", ax=ax[1, 0], palette="Set1", width=0.3
)
sb.stripplot(
    data=results_info,
    y="score",
    x="EXP_gender",
    ax=ax[1, 0],
    palette="Set1",
    linewidth=1,
    edgecolor="k",
    size=3,
    alpha=0.3,
    zorder=1,
)
ax[1, 0].set_title("AUC ROC scores vs. experimenter gender")
ax[1, 0].set_xticklabels(["Man", "Woman"])
ax[1, 0].set_ylabel("ROC AUC")
ax[1, 0].set_xlabel(None)
ax[1, 0].set_ylim(0.3, 1)
(0.3, 1.0)

5.4 Plotting subject AUC ROC scores vs subject’s age

sb.regplot(
    data=results_info[["score", "Age"]].astype("float32"),
    y="score",
    x="Age",
    ax=ax[1, 1],
    scatter_kws={"color": "black", "alpha": 0.5},
    line_kws={"color": "black"},
)
ax[1, 1].set_title("AUC ROC scores vs. subject age")
ax[1, 1].set_ylabel(None)
plt.show()

5.5 Obtained results for four selected subjects correspond to the following figure.#

4_selected_subjects

Obtained results for all subjects correspond to the following figure.

all_subjects

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

Estimated memory usage: 3147 MB

Gallery generated by Sphinx-Gallery