Note
Go to the end to download the full example code.
Decoding sensor space data with generalization across time and conditions#
This example runs the analysis described in [1]. It illustrates how one can fit a linear classifier to identify a discriminatory topography at a given time instant and subsequently assess whether this linear model can accurately predict all of the time samples of a second set of conditions.
# Authors: Jean-Rémi King <jeanremi.king@gmail.com>
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
# Denis Engemann <denis.engemann@gmail.com>
#
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
import mne
from mne.datasets import sample
from mne.decoding import GeneralizingEstimator
print(__doc__)
# Preprocess data
data_path = sample.data_path()
# Load and filter data, set up epochs
meg_path = data_path / "MEG" / "sample"
raw_fname = meg_path / "sample_audvis_filt-0-40_raw.fif"
events_fname = meg_path / "sample_audvis_filt-0-40_raw-eve.fif"
raw = mne.io.read_raw_fif(raw_fname, preload=True)
picks = mne.pick_types(raw.info, meg=True, exclude="bads") # Pick MEG channels
raw.filter(1.0, 30.0, fir_design="firwin") # Band pass filtering signals
events = mne.read_events(events_fname)
event_id = {
"Auditory/Left": 1,
"Auditory/Right": 2,
"Visual/Left": 3,
"Visual/Right": 4,
}
tmin = -0.050
tmax = 0.400
# decimate to make the example faster to run, but then use verbose='error' in
# the Epochs constructor to suppress warning about decimation causing aliasing
decim = 2
epochs = mne.Epochs(
raw,
events,
event_id=event_id,
tmin=tmin,
tmax=tmax,
proj=True,
picks=picks,
baseline=None,
preload=True,
reject=dict(mag=5e-12),
decim=decim,
verbose="error",
)
Opening raw data file /home/circleci/mne_data/MNE-sample-data/MEG/sample/sample_audvis_filt-0-40_raw.fif...
Read a total of 4 projection items:
PCA-v1 (1 x 102) idle
PCA-v2 (1 x 102) idle
PCA-v3 (1 x 102) idle
Average EEG reference (1 x 60) idle
Range : 6450 ... 48149 = 42.956 ... 320.665 secs
Ready.
Reading 0 ... 41699 = 0.000 ... 277.709 secs...
Filtering raw data in 1 contiguous segment
Setting up band-pass filter from 1 - 30 Hz
FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal bandpass filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 1.00
- Lower transition bandwidth: 1.00 Hz (-6 dB cutoff frequency: 0.50 Hz)
- Upper passband edge: 30.00 Hz
- Upper transition bandwidth: 7.50 Hz (-6 dB cutoff frequency: 33.75 Hz)
- Filter length: 497 samples (3.310 s)
[Parallel(n_jobs=1)]: Done 17 tasks | elapsed: 0.0s
[Parallel(n_jobs=1)]: Done 71 tasks | elapsed: 0.1s
[Parallel(n_jobs=1)]: Done 161 tasks | elapsed: 0.3s
[Parallel(n_jobs=1)]: Done 287 tasks | elapsed: 0.6s
We will train the classifier on all left visual vs auditory trials and test on all right visual vs auditory trials.
clf = make_pipeline(
StandardScaler(),
LogisticRegression(solver="liblinear"), # liblinear is faster than lbfgs
)
time_gen = GeneralizingEstimator(clf, scoring="roc_auc", n_jobs=None, verbose=True)
# Fit classifiers on the epochs where the stimulus was presented to the left.
# Note that the experimental condition y indicates auditory or visual
time_gen.fit(X=epochs["Left"].get_data(copy=False), y=epochs["Left"].events[:, 2] > 2)
0%| | Fitting GeneralizingEstimator : 0/35 [00:00<?, ?it/s]
3%|▎ | Fitting GeneralizingEstimator : 1/35 [00:00<00:01, 28.54it/s]
11%|█▏ | Fitting GeneralizingEstimator : 4/35 [00:00<00:00, 58.99it/s]
17%|█▋ | Fitting GeneralizingEstimator : 6/35 [00:00<00:00, 59.14it/s]
23%|██▎ | Fitting GeneralizingEstimator : 8/35 [00:00<00:00, 59.23it/s]
29%|██▊ | Fitting GeneralizingEstimator : 10/35 [00:00<00:00, 59.29it/s]
37%|███▋ | Fitting GeneralizingEstimator : 13/35 [00:00<00:00, 64.62it/s]
46%|████▌ | Fitting GeneralizingEstimator : 16/35 [00:00<00:00, 68.66it/s]
54%|█████▍ | Fitting GeneralizingEstimator : 19/35 [00:00<00:00, 71.69it/s]
60%|██████ | Fitting GeneralizingEstimator : 21/35 [00:00<00:00, 70.04it/s]
69%|██████▊ | Fitting GeneralizingEstimator : 24/35 [00:00<00:00, 72.03it/s]
74%|███████▍ | Fitting GeneralizingEstimator : 26/35 [00:00<00:00, 70.58it/s]
80%|████████ | Fitting GeneralizingEstimator : 28/35 [00:00<00:00, 69.39it/s]
89%|████████▊ | Fitting GeneralizingEstimator : 31/35 [00:00<00:00, 71.36it/s]
94%|█████████▍| Fitting GeneralizingEstimator : 33/35 [00:00<00:00, 69.76it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 71.95it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 70.72it/s]
Score on the epochs where the stimulus was presented to the right.
scores = time_gen.score(
X=epochs["Right"].get_data(copy=False), y=epochs["Right"].events[:, 2] > 2
)
0%| | Scoring GeneralizingEstimator : 0/1225 [00:00<?, ?it/s]
1%| | Scoring GeneralizingEstimator : 11/1225 [00:00<00:03, 306.67it/s]
2%|▏ | Scoring GeneralizingEstimator : 24/1225 [00:00<00:03, 344.38it/s]
3%|▎ | Scoring GeneralizingEstimator : 36/1225 [00:00<00:03, 346.86it/s]
4%|▍ | Scoring GeneralizingEstimator : 48/1225 [00:00<00:03, 348.07it/s]
5%|▍ | Scoring GeneralizingEstimator : 60/1225 [00:00<00:03, 349.31it/s]
6%|▌ | Scoring GeneralizingEstimator : 72/1225 [00:00<00:03, 349.90it/s]
7%|▋ | Scoring GeneralizingEstimator : 84/1225 [00:00<00:03, 350.82it/s]
8%|▊ | Scoring GeneralizingEstimator : 96/1225 [00:00<00:03, 351.52it/s]
9%|▉ | Scoring GeneralizingEstimator : 110/1225 [00:00<00:03, 356.63it/s]
10%|▉ | Scoring GeneralizingEstimator : 122/1225 [00:00<00:03, 356.34it/s]
11%|█ | Scoring GeneralizingEstimator : 135/1225 [00:00<00:03, 359.44it/s]
12%|█▏ | Scoring GeneralizingEstimator : 147/1225 [00:00<00:03, 359.04it/s]
13%|█▎ | Scoring GeneralizingEstimator : 160/1225 [00:00<00:02, 361.68it/s]
14%|█▍ | Scoring GeneralizingEstimator : 173/1225 [00:00<00:02, 364.03it/s]
15%|█▌ | Scoring GeneralizingEstimator : 187/1225 [00:00<00:02, 368.65it/s]
16%|█▋ | Scoring GeneralizingEstimator : 202/1225 [00:00<00:02, 375.35it/s]
18%|█▊ | Scoring GeneralizingEstimator : 216/1225 [00:00<00:02, 378.84it/s]
18%|█▊ | Scoring GeneralizingEstimator : 225/1225 [00:00<00:02, 369.73it/s]
19%|█▉ | Scoring GeneralizingEstimator : 233/1225 [00:00<00:02, 358.73it/s]
20%|██ | Scoring GeneralizingEstimator : 247/1225 [00:00<00:02, 363.18it/s]
21%|██▏ | Scoring GeneralizingEstimator : 262/1225 [00:00<00:02, 368.57it/s]
23%|██▎ | Scoring GeneralizingEstimator : 276/1225 [00:00<00:02, 372.08it/s]
24%|██▎ | Scoring GeneralizingEstimator : 289/1225 [00:00<00:02, 371.89it/s]
24%|██▍ | Scoring GeneralizingEstimator : 300/1225 [00:00<00:02, 368.79it/s]
26%|██▌ | Scoring GeneralizingEstimator : 313/1225 [00:00<00:02, 369.92it/s]
27%|██▋ | Scoring GeneralizingEstimator : 326/1225 [00:00<00:02, 371.00it/s]
28%|██▊ | Scoring GeneralizingEstimator : 340/1225 [00:00<00:02, 373.90it/s]
29%|██▊ | Scoring GeneralizingEstimator : 352/1225 [00:00<00:02, 370.97it/s]
29%|██▉ | Scoring GeneralizingEstimator : 361/1225 [00:00<00:02, 362.57it/s]
30%|███ | Scoring GeneralizingEstimator : 368/1225 [00:01<00:02, 352.89it/s]
31%|███ | Scoring GeneralizingEstimator : 382/1225 [00:01<00:02, 356.79it/s]
32%|███▏ | Scoring GeneralizingEstimator : 394/1225 [00:01<00:02, 356.80it/s]
33%|███▎ | Scoring GeneralizingEstimator : 406/1225 [00:01<00:02, 356.77it/s]
34%|███▍ | Scoring GeneralizingEstimator : 421/1225 [00:01<00:02, 362.01it/s]
36%|███▌ | Scoring GeneralizingEstimator : 435/1225 [00:01<00:02, 365.14it/s]
37%|███▋ | Scoring GeneralizingEstimator : 448/1225 [00:01<00:02, 366.40it/s]
38%|███▊ | Scoring GeneralizingEstimator : 461/1225 [00:01<00:02, 367.57it/s]
39%|███▉ | Scoring GeneralizingEstimator : 475/1225 [00:01<00:02, 370.10it/s]
40%|███▉ | Scoring GeneralizingEstimator : 484/1225 [00:01<00:02, 363.93it/s]
41%|████ | Scoring GeneralizingEstimator : 497/1225 [00:01<00:01, 365.13it/s]
42%|████▏ | Scoring GeneralizingEstimator : 510/1225 [00:01<00:01, 366.33it/s]
43%|████▎ | Scoring GeneralizingEstimator : 525/1225 [00:01<00:01, 369.48it/s]
44%|████▍ | Scoring GeneralizingEstimator : 537/1225 [00:01<00:01, 368.76it/s]
45%|████▍ | Scoring GeneralizingEstimator : 549/1225 [00:01<00:01, 368.01it/s]
46%|████▌ | Scoring GeneralizingEstimator : 563/1225 [00:01<00:01, 370.62it/s]
47%|████▋ | Scoring GeneralizingEstimator : 571/1225 [00:01<00:01, 361.54it/s]
47%|████▋ | Scoring GeneralizingEstimator : 577/1225 [00:01<00:01, 351.50it/s]
48%|████▊ | Scoring GeneralizingEstimator : 588/1225 [00:01<00:01, 348.23it/s]
49%|████▊ | Scoring GeneralizingEstimator : 595/1225 [00:01<00:01, 340.76it/s]
49%|████▉ | Scoring GeneralizingEstimator : 602/1225 [00:01<00:01, 333.61it/s]
50%|█████ | Scoring GeneralizingEstimator : 615/1225 [00:01<00:01, 336.40it/s]
51%|█████ | Scoring GeneralizingEstimator : 627/1225 [00:01<00:01, 337.42it/s]
52%|█████▏ | Scoring GeneralizingEstimator : 639/1225 [00:01<00:01, 338.37it/s]
53%|█████▎ | Scoring GeneralizingEstimator : 653/1225 [00:01<00:01, 342.39it/s]
54%|█████▍ | Scoring GeneralizingEstimator : 666/1225 [00:01<00:01, 344.63it/s]
55%|█████▌ | Scoring GeneralizingEstimator : 676/1225 [00:01<00:01, 342.13it/s]
56%|█████▌ | Scoring GeneralizingEstimator : 682/1225 [00:01<00:01, 332.25it/s]
56%|█████▌ | Scoring GeneralizingEstimator : 689/1225 [00:01<00:01, 325.82it/s]
57%|█████▋ | Scoring GeneralizingEstimator : 697/1225 [00:02<00:01, 317.05it/s]
57%|█████▋ | Scoring GeneralizingEstimator : 704/1225 [00:02<00:01, 310.29it/s]
58%|█████▊ | Scoring GeneralizingEstimator : 713/1225 [00:02<00:01, 308.13it/s]
59%|█████▉ | Scoring GeneralizingEstimator : 726/1225 [00:02<00:01, 312.09it/s]
60%|██████ | Scoring GeneralizingEstimator : 737/1225 [00:02<00:01, 312.85it/s]
61%|██████ | Scoring GeneralizingEstimator : 748/1225 [00:02<00:01, 313.56it/s]
62%|██████▏ | Scoring GeneralizingEstimator : 759/1225 [00:02<00:01, 312.07it/s]
62%|██████▏ | Scoring GeneralizingEstimator : 765/1225 [00:02<00:01, 304.64it/s]
63%|██████▎ | Scoring GeneralizingEstimator : 772/1225 [00:02<00:01, 299.35it/s]
64%|██████▍ | Scoring GeneralizingEstimator : 784/1225 [00:02<00:01, 302.14it/s]
65%|██████▌ | Scoring GeneralizingEstimator : 797/1225 [00:02<00:01, 306.36it/s]
66%|██████▌ | Scoring GeneralizingEstimator : 811/1225 [00:02<00:01, 311.86it/s]
67%|██████▋ | Scoring GeneralizingEstimator : 823/1225 [00:02<00:01, 314.02it/s]
68%|██████▊ | Scoring GeneralizingEstimator : 836/1225 [00:02<00:01, 317.48it/s]
69%|██████▉ | Scoring GeneralizingEstimator : 849/1225 [00:02<00:01, 320.92it/s]
71%|███████ | Scoring GeneralizingEstimator : 864/1225 [00:02<00:01, 327.18it/s]
72%|███████▏ | Scoring GeneralizingEstimator : 879/1225 [00:02<00:01, 332.47it/s]
73%|███████▎ | Scoring GeneralizingEstimator : 892/1225 [00:02<00:00, 335.09it/s]
74%|███████▍ | Scoring GeneralizingEstimator : 906/1225 [00:02<00:00, 338.93it/s]
75%|███████▍ | Scoring GeneralizingEstimator : 916/1225 [00:02<00:00, 336.81it/s]
75%|███████▌ | Scoring GeneralizingEstimator : 924/1225 [00:02<00:00, 331.86it/s]
77%|███████▋ | Scoring GeneralizingEstimator : 938/1225 [00:02<00:00, 336.06it/s]
78%|███████▊ | Scoring GeneralizingEstimator : 952/1225 [00:02<00:00, 339.32it/s]
78%|███████▊ | Scoring GeneralizingEstimator : 961/1225 [00:02<00:00, 330.04it/s]
79%|███████▉ | Scoring GeneralizingEstimator : 968/1225 [00:02<00:00, 324.05it/s]
80%|███████▉ | Scoring GeneralizingEstimator : 978/1225 [00:02<00:00, 318.81it/s]
81%|████████ | Scoring GeneralizingEstimator : 991/1225 [00:02<00:00, 322.09it/s]
82%|████████▏ | Scoring GeneralizingEstimator : 1006/1225 [00:02<00:00, 328.11it/s]
83%|████████▎ | Scoring GeneralizingEstimator : 1019/1225 [00:03<00:00, 330.76it/s]
84%|████████▍ | Scoring GeneralizingEstimator : 1031/1225 [00:03<00:00, 331.87it/s]
85%|████████▌ | Scoring GeneralizingEstimator : 1043/1225 [00:03<00:00, 333.06it/s]
86%|████████▋ | Scoring GeneralizingEstimator : 1057/1225 [00:03<00:00, 337.11it/s]
87%|████████▋ | Scoring GeneralizingEstimator : 1069/1225 [00:03<00:00, 338.05it/s]
88%|████████▊ | Scoring GeneralizingEstimator : 1082/1225 [00:03<00:00, 340.37it/s]
89%|████████▉ | Scoring GeneralizingEstimator : 1092/1225 [00:03<00:00, 338.15it/s]
90%|█████████ | Scoring GeneralizingEstimator : 1103/1225 [00:03<00:00, 337.57it/s]
91%|█████████ | Scoring GeneralizingEstimator : 1116/1225 [00:03<00:00, 339.81it/s]
92%|█████████▏| Scoring GeneralizingEstimator : 1129/1225 [00:03<00:00, 341.99it/s]
93%|█████████▎| Scoring GeneralizingEstimator : 1142/1225 [00:03<00:00, 344.15it/s]
94%|█████████▍| Scoring GeneralizingEstimator : 1156/1225 [00:03<00:00, 347.66it/s]
95%|█████████▌| Scoring GeneralizingEstimator : 1169/1225 [00:03<00:00, 349.53it/s]
97%|█████████▋| Scoring GeneralizingEstimator : 1183/1225 [00:03<00:00, 352.74it/s]
98%|█████████▊| Scoring GeneralizingEstimator : 1196/1225 [00:03<00:00, 354.37it/s]
99%|█████████▉| Scoring GeneralizingEstimator : 1210/1225 [00:03<00:00, 357.42it/s]
100%|█████████▉| Scoring GeneralizingEstimator : 1224/1225 [00:03<00:00, 360.33it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00, 345.83it/s]
Plot
fig, ax = plt.subplots(layout="constrained")
im = ax.matshow(
scores,
vmin=0,
vmax=1.0,
cmap="RdBu_r",
origin="lower",
extent=epochs.times[[0, -1, 0, -1]],
)
ax.axhline(0.0, color="k")
ax.axvline(0.0, color="k")
ax.xaxis.set_ticks_position("bottom")
ax.set_xlabel(
'Condition: "Right"\nTesting Time (s)',
)
ax.set_ylabel('Condition: "Left"\nTraining Time (s)')
ax.set_title("Generalization across time and condition", fontweight="bold")
fig.colorbar(im, ax=ax, label="Performance (ROC AUC)")
plt.show()
References#
Total running time of the script: (0 minutes 5.936 seconds)