|
| 1 | +""" |
| 2 | +=============================================== |
| 3 | +Examples of analysis of a Dreyer2023 A dataset. |
| 4 | +=============================================== |
| 5 | +
|
| 6 | +This example shows how to plot Dreyer2023A Left-Right Imagery ROC AUC scores |
| 7 | +obtained with CSP+LDA pipeline versus demographic information of the examined |
| 8 | +subjects (gender and age) and experimenters (gender). |
| 9 | +
|
| 10 | +To reduce computational time, the example is provided for four subjects. |
| 11 | +
|
| 12 | +""" |
| 13 | + |
| 14 | +# Authors: Sara Sedlar <[email protected]> |
| 15 | +# Sylvain Chevallier <[email protected]> |
| 16 | +# License: BSD (3-clause) |
| 17 | + |
| 18 | +import matplotlib.patches as mpatches |
| 19 | +import matplotlib.pyplot as plt |
| 20 | +import seaborn as sb |
| 21 | +from pyriemann.estimation import Covariances |
| 22 | +from pyriemann.spatialfilters import CSP |
| 23 | +from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA |
| 24 | +from sklearn.pipeline import make_pipeline |
| 25 | + |
| 26 | +from moabb.datasets import Dreyer2023A |
| 27 | +from moabb.evaluations import WithinSessionEvaluation |
| 28 | +from moabb.paradigms import MotorImagery |
| 29 | + |
| 30 | + |
| 31 | +######################################################################################## |
| 32 | +# 1. Defining dataset, selecting subject for analysis and getting data |
| 33 | +dreyer2023 = Dreyer2023A() |
| 34 | +dreyer2023.subject_list = [1, 5, 7, 35] |
| 35 | +dreyer2023.get_data() |
| 36 | +######################################################################################## |
| 37 | +# 2. Defining MotorImagery paradigm and CSP+LDA pipeline |
| 38 | +paradigm = MotorImagery() |
| 39 | +pipelines = {} |
| 40 | +pipelines["CSP+LDA"] = make_pipeline( |
| 41 | + Covariances(estimator="oas"), CSP(nfilter=6), LDA(solver="lsqr", shrinkage="auto") |
| 42 | +) |
| 43 | +######################################################################################## |
| 44 | +# 3. Within session evaluation of the pipeline |
| 45 | +evaluation = WithinSessionEvaluation( |
| 46 | + paradigm=paradigm, datasets=[dreyer2023], suffix="examples", overwrite=False |
| 47 | +) |
| 48 | +results = evaluation.process(pipelines) |
| 49 | + |
| 50 | +######################################################################################## |
| 51 | +# 4. Loading dataset info and concatenation with the obtained results |
| 52 | +info = dreyer2023.get_subject_info().rename(columns={"score": "score_MR"}) |
| 53 | +# Creating a new column with subject's age |
| 54 | +info["Age"] = 2019 - info["Birth_year"] |
| 55 | +# Casting to int for merging |
| 56 | +info["subject"] = info["SUJ_ID"].astype(int) |
| 57 | +results["subject"] = results["subject"].astype(int) |
| 58 | + |
| 59 | +results_info = results.merge(info, on="subject", how="left") |
| 60 | + |
| 61 | +######################################################################################## |
| 62 | +######################################################################################## |
| 63 | +# 5.1 Plotting subject AUC ROC scores vs subject's gender |
| 64 | +fig, ax = plt.subplots(nrows=2, ncols=2, facecolor="white", figsize=[16, 8], sharey=True) |
| 65 | +fig.subplots_adjust(wspace=0.0, hspace=0.5) |
| 66 | +sb.boxplot( |
| 67 | + data=results_info, y="score", x="SUJ_gender", ax=ax[0, 0], palette="Set1", width=0.3 |
| 68 | +) |
| 69 | +sb.stripplot( |
| 70 | + data=results_info, |
| 71 | + y="score", |
| 72 | + x="SUJ_gender", |
| 73 | + ax=ax[0, 0], |
| 74 | + palette="Set1", |
| 75 | + linewidth=1, |
| 76 | + edgecolor="k", |
| 77 | + size=3, |
| 78 | + alpha=0.3, |
| 79 | + zorder=1, |
| 80 | +) |
| 81 | +ax[0, 0].set_title("AUC ROC scores vs. subject gender") |
| 82 | +ax[0, 0].set_xticklabels(["Man", "Woman"]) |
| 83 | +ax[0, 0].set_ylabel("ROC AUC") |
| 84 | +ax[0, 0].set_xlabel(None) |
| 85 | +ax[0, 0].set_ylim(0.3, 1) |
| 86 | +######################################################################################## |
| 87 | +# 5.2 Plotting subject AUC ROC scores vs subjects's age per gender |
| 88 | +sb.regplot( |
| 89 | + data=results_info[results_info["SUJ_gender"] == 1][["score", "Age"]].astype( |
| 90 | + "float32" |
| 91 | + ), |
| 92 | + y="score", |
| 93 | + x="Age", |
| 94 | + ax=ax[0, 1], |
| 95 | + scatter_kws={"color": "#e41a1c", "alpha": 0.5}, |
| 96 | + line_kws={"color": "#e41a1c"}, |
| 97 | +) |
| 98 | +sb.regplot( |
| 99 | + data=results_info[results_info["SUJ_gender"] == 2][["score", "Age"]].astype( |
| 100 | + "float32" |
| 101 | + ), |
| 102 | + y="score", |
| 103 | + x="Age", |
| 104 | + ax=ax[0, 1], |
| 105 | + scatter_kws={"color": "#377eb8", "alpha": 0.5}, |
| 106 | + line_kws={"color": "#377eb8"}, |
| 107 | +) |
| 108 | +ax[0, 1].set_title("AUC ROC scores vs. subject age per gender") |
| 109 | +ax[0, 1].set_ylabel(None) |
| 110 | +ax[0, 1].set_xlabel(None) |
| 111 | +ax[0, 1].legend( |
| 112 | + handles=[ |
| 113 | + mpatches.Patch(color="#e41a1c", label="Man"), |
| 114 | + mpatches.Patch(color="#377eb8", label="Woman"), |
| 115 | + ] |
| 116 | +) |
| 117 | +######################################################################################## |
| 118 | +# 5.3 Plotting subject AUC ROC scores vs experimenter's gender |
| 119 | +sb.boxplot( |
| 120 | + data=results_info, y="score", x="EXP_gender", ax=ax[1, 0], palette="Set1", width=0.3 |
| 121 | +) |
| 122 | +sb.stripplot( |
| 123 | + data=results_info, |
| 124 | + y="score", |
| 125 | + x="EXP_gender", |
| 126 | + ax=ax[1, 0], |
| 127 | + palette="Set1", |
| 128 | + linewidth=1, |
| 129 | + edgecolor="k", |
| 130 | + size=3, |
| 131 | + alpha=0.3, |
| 132 | + zorder=1, |
| 133 | +) |
| 134 | +ax[1, 0].set_title("AUC ROC scores vs. experimenter gender") |
| 135 | +ax[1, 0].set_xticklabels(["Man", "Woman"]) |
| 136 | +ax[1, 0].set_ylabel("ROC AUC") |
| 137 | +ax[1, 0].set_xlabel(None) |
| 138 | +ax[1, 0].set_ylim(0.3, 1) |
| 139 | +######################################################################################## |
| 140 | +# 5.4 Plotting subject AUC ROC scores vs subject's age |
| 141 | +sb.regplot( |
| 142 | + data=results_info[["score", "Age"]].astype("float32"), |
| 143 | + y="score", |
| 144 | + x="Age", |
| 145 | + ax=ax[1, 1], |
| 146 | + scatter_kws={"color": "black", "alpha": 0.5}, |
| 147 | + line_kws={"color": "black"}, |
| 148 | +) |
| 149 | +ax[1, 1].set_title("AUC ROC scores vs. subject age") |
| 150 | +ax[1, 1].set_ylabel(None) |
| 151 | +plt.show() |
| 152 | +######################################################################################## |
| 153 | +# 5.5 Obtained results for four selected subjects correspond to the following figure. |
| 154 | +# |
| 155 | +# .. image:: ../images/Dreyer_clf_scores_vs_subj_info/4_selected_subjects.png |
| 156 | +# :align: center |
| 157 | +# :alt: 4_selected_subjects |
| 158 | + |
| 159 | +######################################################################################## |
| 160 | +# Obtained results for all subjects correspond to the following figure. |
| 161 | +# |
| 162 | +# .. image:: ../images/Dreyer_clf_scores_vs_subj_info/all_subjects.png |
| 163 | +# :align: center |
| 164 | +# :alt: all_subjects |
0 commit comments