| """ |
| ============================================== |
| Time-Resolved Decoding with SlidingEstimator |
| ============================================== |
| |
| This example shows how to perform time-resolved decoding of EEG signals using |
| :class:`mne.decoding.SlidingEstimator`. Instead of reducing the entire trial to |
| a single score, a SlidingEstimator fits an independent classifier at each time |
| point, revealing *when* during a trial the neural signal carries information |
| about the mental state. |
| |
| This approach is a natural alternative to pseudo-online evaluation (using |
| overlapping windows): rather than simulating an online scenario by slicing |
| the raw signal with a sliding window, we directly assess decoding accuracy |
| at each sample of the already-epoched trial. |
| |
| We use the BNCI2014-001 motor-imagery dataset (left- vs right-hand) and apply |
| a logistic-regression classifier wrapped in a SlidingEstimator. For each |
| subject the score is evaluated via stratified 5-fold cross-validation using |
| :func:`mne.decoding.cross_val_multiscore`, and the results are averaged across |
| subjects and visualised as a time course. |
| """ |
|
|
| |
| |
| |
| |
|
|
| import warnings |
|
|
| import matplotlib.pyplot as plt |
| import numpy as np |
| from mne.decoding import SlidingEstimator, cross_val_multiscore |
| from scipy.stats import ttest_1samp |
| from sklearn.linear_model import LogisticRegression |
| from sklearn.pipeline import make_pipeline |
| from sklearn.preprocessing import StandardScaler |
|
|
| import moabb |
| from moabb.datasets import BNCI2014_001 |
| from moabb.paradigms import LeftRightImagery |
|
|
|
|
| moabb.set_log_level("info") |
| warnings.filterwarnings("ignore") |
|
|
| |
| |
| |
| |
| |
|
|
| dataset = BNCI2014_001() |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| paradigm = LeftRightImagery() |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| clf = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)) |
| sliding = SlidingEstimator(clf, scoring="roc_auc", n_jobs=1) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| all_scores = [] |
|
|
| for subject in dataset.subject_list: |
| epochs, y, meta = paradigm.get_data( |
| dataset=dataset, subjects=[subject], return_epochs=True |
| ) |
| X = epochs.get_data() |
|
|
| |
| scores = cross_val_multiscore(sliding, X, y, cv=5, n_jobs=1) |
| all_scores.append(scores.mean(axis=0)) |
|
|
| |
| all_scores = np.array(all_scores) |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| times = epochs.times |
| sfreq = epochs.info["sfreq"] |
| print(f"Sampling frequency: {sfreq} Hz, {len(times)} time points") |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| _, p_values = ttest_1samp(all_scores, 0.5, axis=0) |
| sig_mask = p_values < 0.05 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| mean_scores = all_scores.mean(axis=0) |
| sem_scores = all_scores.std(axis=0) / np.sqrt(len(dataset.subject_list)) |
|
|
| fig, ax = plt.subplots(figsize=(8, 4)) |
| ax.plot(times, mean_scores, label="Mean AUC across subjects", color="steelblue") |
| ax.fill_between( |
| times, |
| mean_scores - sem_scores, |
| mean_scores + sem_scores, |
| alpha=0.3, |
| color="steelblue", |
| label="\u00b1SEM", |
| ) |
| ax.axhline(0.5, linestyle="--", color="k", label="Chance level (AUC = 0.5)") |
| ax.axvline(times[0], linestyle=":", color="gray", label="MI onset") |
|
|
| |
| ax.fill_between( |
| times, |
| 0.0, |
| 0.03, |
| where=sig_mask, |
| color="tab:orange", |
| alpha=0.7, |
| label="p < 0.05 (uncorrected)", |
| transform=ax.get_xaxis_transform(), |
| ) |
|
|
| ax.set_xlabel("Time (s)") |
| ax.set_ylabel("AUC") |
| ax.set_title("Time-Resolved Decoding \u2013 Left vs. Right Motor Imagery\n(BNCI2014-001)") |
| ax.legend(loc="upper left", fontsize="small") |
| ax.set_xlim(times[0], times[-1]) |
| ax.set_ylim(0.4, 1.0) |
| plt.tight_layout() |
| plt.show() |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| fig, ax = plt.subplots(figsize=(8, 4)) |
| im = ax.imshow( |
| all_scores, |
| aspect="auto", |
| origin="lower", |
| extent=[times[0], times[-1], 0.5, len(dataset.subject_list) + 0.5], |
| cmap="RdBu_r", |
| vmin=0.3, |
| vmax=0.7, |
| ) |
| ax.set_xlabel("Time (s)") |
| ax.set_ylabel("Subject") |
| ax.set_yticks(range(1, len(dataset.subject_list) + 1)) |
| ax.set_title("Per-Subject Time-Resolved AUC\n(BNCI2014-001)") |
| ax.axvline(times[0], linestyle=":", color="k", linewidth=0.8) |
| ax.set_xlim(times[0], times[-1]) |
| fig.colorbar(im, ax=ax, label="AUC") |
| plt.tight_layout() |
| plt.show() |
|
|