| """ |
| ================================ |
| Hinss2021 classification example |
| ================================ |
| |
| This example shows how to use the Hinss2021 dataset |
| with the resting state paradigm. |
| |
| In this example, we aim to determine the most effective channel selection strategy |
| for the :class:`moabb.datasets.Hinss2021` dataset. |
| The pipelines under consideration are: |
| |
| - `Xdawn` |
| - Electrode selection based on time epochs data |
| - Electrode selection based on covariance matrices |
| |
| """ |
|
|
| |
|
|
| import warnings |
|
|
| import numpy as np |
| import seaborn as sns |
| from matplotlib import pyplot as plt |
| from pyriemann.channelselection import ElectrodeSelection |
| from pyriemann.estimation import Covariances |
| from pyriemann.spatialfilters import Xdawn |
| from pyriemann.tangentspace import TangentSpace |
| from sklearn.base import TransformerMixin |
| from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA |
| from sklearn.pipeline import make_pipeline |
|
|
| from moabb import set_log_level |
| from moabb.datasets import Hinss2021 |
| from moabb.evaluations import CrossSessionEvaluation |
| from moabb.paradigms import RestingStateToP300Adapter |
|
|
|
|
| |
| warnings.simplefilter(action="ignore", category=FutureWarning) |
| warnings.simplefilter(action="ignore", category=RuntimeWarning) |
|
|
| set_log_level("info") |
|
|
|
|
| |
| |
| |
| |
| |
| |
|
|
|
|
| class EpochSelectChannel(TransformerMixin): |
| """Select channels based on covariance information.""" |
|
|
| def __init__(self, n_chan, cov_est): |
| self._chs_idx = None |
| self.n_chan = n_chan |
| self.cov_est = cov_est |
|
|
| def fit(self, X, _y=None): |
| |
| covs = Covariances(estimator=self.cov_est).fit_transform(X) |
| |
| m = np.mean(covs, axis=0) |
| |
| indices = np.unravel_index( |
| np.argpartition(m, -self.n_chan, axis=None)[-self.n_chan :], m.shape |
| ) |
| |
| self._chs_idx = np.unique(indices) |
| return self |
|
|
| def transform(self, X): |
| return X[:, self._chs_idx, :] |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| events = {"easy": 2, "diff": 3} |
| |
| paradigm = RestingStateToP300Adapter(events=events, tmin=0, tmax=0.5) |
| |
| datasets = [Hinss2021()] |
|
|
| |
| |
| n__subjects = 2 |
| title = "Datasets: " |
| for dataset in datasets: |
| title = title + " " + dataset.code |
| dataset.subject_list = dataset.subject_list[:n__subjects] |
|
|
| |
| |
| |
| |
| |
|
|
| pipelines = {} |
|
|
| pipelines["Xdawn+Cov+TS+LDA"] = make_pipeline( |
| Xdawn(nfilter=4), Covariances(estimator="lwf"), TangentSpace(), LDA() |
| ) |
|
|
| pipelines["Cov+ElSel+TS+LDA"] = make_pipeline( |
| Covariances(estimator="lwf"), ElectrodeSelection(nelec=8), TangentSpace(), LDA() |
| ) |
|
|
| |
| |
| pipelines["ElSel+Cov+TS+LDA"] = make_pipeline( |
| EpochSelectChannel(n_chan=8, cov_est="lwf"), |
| Covariances(estimator="lwf"), |
| TangentSpace(), |
| LDA(), |
| ) |
|
|
| |
| |
| |
| |
| |
|
|
| |
| evaluation = CrossSessionEvaluation(paradigm=paradigm, datasets=datasets, overwrite=False) |
|
|
| results = evaluation.process(pipelines) |
|
|
| |
| |
| |
|
|
| print("Averaging the session performance:") |
| print(results.groupby("pipeline")[["score", "time"]].mean()) |
|
|
| |
| |
| |
| |
| |
|
|
|
|
| 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").set( |
| title=title |
| ) |
|
|
| ax.set_ylabel("ROC AUC") |
| ax.set_ylim(0.3, 1) |
|
|
| plt.show() |
|
|
| |
| |
| |
| |
| |
| |
|
|