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-Remi 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.2s
[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, 29.10it/s]
9%|▊ | Fitting GeneralizingEstimator : 3/35 [00:00<00:00, 43.51it/s]
14%|█▍ | Fitting GeneralizingEstimator : 5/35 [00:00<00:00, 48.93it/s]
20%|██ | Fitting GeneralizingEstimator : 7/35 [00:00<00:00, 51.59it/s]
29%|██▊ | Fitting GeneralizingEstimator : 10/35 [00:00<00:00, 58.79it/s]
37%|███▋ | Fitting GeneralizingEstimator : 13/35 [00:00<00:00, 64.37it/s]
46%|████▌ | Fitting GeneralizingEstimator : 16/35 [00:00<00:00, 68.24it/s]
54%|█████▍ | Fitting GeneralizingEstimator : 19/35 [00:00<00:00, 70.98it/s]
60%|██████ | Fitting GeneralizingEstimator : 21/35 [00:00<00:00, 69.44it/s]
69%|██████▊ | Fitting GeneralizingEstimator : 24/35 [00:00<00:00, 71.83it/s]
74%|███████▍ | Fitting GeneralizingEstimator : 26/35 [00:00<00:00, 70.41it/s]
80%|████████ | Fitting GeneralizingEstimator : 28/35 [00:00<00:00, 69.22it/s]
89%|████████▊ | Fitting GeneralizingEstimator : 31/35 [00:00<00:00, 70.79it/s]
94%|█████████▍| Fitting GeneralizingEstimator : 33/35 [00:00<00:00, 69.69it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 70.61it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 69.38it/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 : 12/1225 [00:00<00:03, 330.41it/s]
2%|▏ | Scoring GeneralizingEstimator : 22/1225 [00:00<00:03, 313.46it/s]
3%|▎ | Scoring GeneralizingEstimator : 34/1225 [00:00<00:03, 327.66it/s]
4%|▍ | Scoring GeneralizingEstimator : 46/1225 [00:00<00:03, 335.07it/s]
5%|▍ | Scoring GeneralizingEstimator : 58/1225 [00:00<00:03, 339.72it/s]
6%|▌ | Scoring GeneralizingEstimator : 70/1225 [00:00<00:03, 342.82it/s]
7%|▋ | Scoring GeneralizingEstimator : 82/1225 [00:00<00:03, 344.92it/s]
8%|▊ | Scoring GeneralizingEstimator : 94/1225 [00:00<00:03, 327.57it/s]
8%|▊ | Scoring GeneralizingEstimator : 104/1225 [00:00<00:03, 323.49it/s]
9%|▉ | Scoring GeneralizingEstimator : 115/1225 [00:00<00:03, 323.55it/s]
10%|█ | Scoring GeneralizingEstimator : 127/1225 [00:00<00:03, 327.21it/s]
11%|█▏ | Scoring GeneralizingEstimator : 139/1225 [00:00<00:03, 330.25it/s]
12%|█▏ | Scoring GeneralizingEstimator : 151/1225 [00:00<00:03, 332.85it/s]
13%|█▎ | Scoring GeneralizingEstimator : 163/1225 [00:00<00:03, 335.00it/s]
14%|█▍ | Scoring GeneralizingEstimator : 175/1225 [00:00<00:03, 336.83it/s]
15%|█▌ | Scoring GeneralizingEstimator : 187/1225 [00:00<00:03, 338.38it/s]
16%|█▌ | Scoring GeneralizingEstimator : 199/1225 [00:00<00:03, 339.89it/s]
17%|█▋ | Scoring GeneralizingEstimator : 211/1225 [00:00<00:02, 341.22it/s]
18%|█▊ | Scoring GeneralizingEstimator : 223/1225 [00:00<00:02, 342.33it/s]
19%|█▉ | Scoring GeneralizingEstimator : 235/1225 [00:00<00:02, 343.41it/s]
20%|██ | Scoring GeneralizingEstimator : 247/1225 [00:00<00:02, 344.36it/s]
21%|██ | Scoring GeneralizingEstimator : 259/1225 [00:00<00:02, 345.14it/s]
22%|██▏ | Scoring GeneralizingEstimator : 272/1225 [00:00<00:02, 345.95it/s]
23%|██▎ | Scoring GeneralizingEstimator : 284/1225 [00:00<00:02, 346.60it/s]
24%|██▍ | Scoring GeneralizingEstimator : 295/1225 [00:00<00:02, 345.21it/s]
25%|██▌ | Scoring GeneralizingEstimator : 308/1225 [00:00<00:02, 347.78it/s]
26%|██▌ | Scoring GeneralizingEstimator : 319/1225 [00:00<00:02, 346.34it/s]
27%|██▋ | Scoring GeneralizingEstimator : 331/1225 [00:00<00:02, 346.98it/s]
28%|██▊ | Scoring GeneralizingEstimator : 343/1225 [00:00<00:02, 347.50it/s]
29%|██▉ | Scoring GeneralizingEstimator : 355/1225 [00:01<00:02, 347.99it/s]
30%|███ | Scoring GeneralizingEstimator : 368/1225 [00:01<00:02, 348.66it/s]
31%|███ | Scoring GeneralizingEstimator : 380/1225 [00:01<00:02, 349.09it/s]
32%|███▏ | Scoring GeneralizingEstimator : 392/1225 [00:01<00:02, 349.48it/s]
33%|███▎ | Scoring GeneralizingEstimator : 405/1225 [00:01<00:02, 351.56it/s]
34%|███▍ | Scoring GeneralizingEstimator : 416/1225 [00:01<00:02, 341.97it/s]
35%|███▍ | Scoring GeneralizingEstimator : 426/1225 [00:01<00:02, 339.32it/s]
35%|███▌ | Scoring GeneralizingEstimator : 432/1225 [00:01<00:02, 330.09it/s]
36%|███▌ | Scoring GeneralizingEstimator : 439/1225 [00:01<00:02, 322.59it/s]
37%|███▋ | Scoring GeneralizingEstimator : 448/1225 [00:01<00:02, 319.40it/s]
38%|███▊ | Scoring GeneralizingEstimator : 460/1225 [00:01<00:02, 321.19it/s]
39%|███▊ | Scoring GeneralizingEstimator : 473/1225 [00:01<00:02, 324.78it/s]
40%|███▉ | Scoring GeneralizingEstimator : 485/1225 [00:01<00:02, 326.45it/s]
41%|████ | Scoring GeneralizingEstimator : 498/1225 [00:01<00:02, 329.70it/s]
42%|████▏ | Scoring GeneralizingEstimator : 510/1225 [00:01<00:02, 331.14it/s]
43%|████▎ | Scoring GeneralizingEstimator : 523/1225 [00:01<00:02, 333.97it/s]
44%|████▎ | Scoring GeneralizingEstimator : 535/1225 [00:01<00:02, 335.15it/s]
45%|████▍ | Scoring GeneralizingEstimator : 549/1225 [00:01<00:01, 338.52it/s]
46%|████▌ | Scoring GeneralizingEstimator : 561/1225 [00:01<00:01, 339.47it/s]
47%|████▋ | Scoring GeneralizingEstimator : 576/1225 [00:01<00:01, 345.09it/s]
48%|████▊ | Scoring GeneralizingEstimator : 591/1225 [00:01<00:01, 350.38it/s]
49%|████▉ | Scoring GeneralizingEstimator : 606/1225 [00:01<00:01, 355.41it/s]
51%|█████ | Scoring GeneralizingEstimator : 621/1225 [00:01<00:01, 360.15it/s]
52%|█████▏ | Scoring GeneralizingEstimator : 637/1225 [00:01<00:01, 365.17it/s]
53%|█████▎ | Scoring GeneralizingEstimator : 652/1225 [00:01<00:01, 369.37it/s]
55%|█████▍ | Scoring GeneralizingEstimator : 668/1225 [00:01<00:01, 374.91it/s]
56%|█████▌ | Scoring GeneralizingEstimator : 682/1225 [00:01<00:01, 376.95it/s]
57%|█████▋ | Scoring GeneralizingEstimator : 697/1225 [00:01<00:01, 380.52it/s]
58%|█████▊ | Scoring GeneralizingEstimator : 711/1225 [00:01<00:01, 382.30it/s]
59%|█████▉ | Scoring GeneralizingEstimator : 724/1225 [00:02<00:01, 382.41it/s]
60%|██████ | Scoring GeneralizingEstimator : 738/1225 [00:02<00:01, 383.51it/s]
61%|██████▏ | Scoring GeneralizingEstimator : 752/1225 [00:02<00:01, 385.13it/s]
63%|██████▎ | Scoring GeneralizingEstimator : 766/1225 [00:02<00:01, 386.70it/s]
64%|██████▎ | Scoring GeneralizingEstimator : 779/1225 [00:02<00:01, 386.66it/s]
65%|██████▍ | Scoring GeneralizingEstimator : 792/1225 [00:02<00:01, 384.78it/s]
66%|██████▌ | Scoring GeneralizingEstimator : 805/1225 [00:02<00:01, 384.83it/s]
67%|██████▋ | Scoring GeneralizingEstimator : 819/1225 [00:02<00:01, 386.40it/s]
68%|██████▊ | Scoring GeneralizingEstimator : 833/1225 [00:02<00:01, 387.91it/s]
69%|██████▉ | Scoring GeneralizingEstimator : 847/1225 [00:02<00:00, 389.24it/s]
70%|███████ | Scoring GeneralizingEstimator : 859/1225 [00:02<00:00, 387.51it/s]
71%|███████ | Scoring GeneralizingEstimator : 871/1225 [00:02<00:00, 385.82it/s]
72%|███████▏ | Scoring GeneralizingEstimator : 886/1225 [00:02<00:00, 388.86it/s]
74%|███████▎ | Scoring GeneralizingEstimator : 901/1225 [00:02<00:00, 391.64it/s]
75%|███████▍ | Scoring GeneralizingEstimator : 915/1225 [00:02<00:00, 392.86it/s]
76%|███████▌ | Scoring GeneralizingEstimator : 930/1225 [00:02<00:00, 393.82it/s]
77%|███████▋ | Scoring GeneralizingEstimator : 944/1225 [00:02<00:00, 394.92it/s]
78%|███████▊ | Scoring GeneralizingEstimator : 958/1225 [00:02<00:00, 395.91it/s]
79%|███████▉ | Scoring GeneralizingEstimator : 971/1225 [00:02<00:00, 395.40it/s]
80%|████████ | Scoring GeneralizingEstimator : 984/1225 [00:02<00:00, 394.93it/s]
81%|████████▏ | Scoring GeneralizingEstimator : 996/1225 [00:02<00:00, 392.84it/s]
82%|████████▏ | Scoring GeneralizingEstimator : 1009/1225 [00:02<00:00, 392.47it/s]
84%|████████▎ | Scoring GeneralizingEstimator : 1024/1225 [00:02<00:00, 395.08it/s]
85%|████████▍ | Scoring GeneralizingEstimator : 1038/1225 [00:02<00:00, 396.12it/s]
86%|████████▌ | Scoring GeneralizingEstimator : 1051/1225 [00:02<00:00, 394.88it/s]
87%|████████▋ | Scoring GeneralizingEstimator : 1062/1225 [00:02<00:00, 391.44it/s]
88%|████████▊ | Scoring GeneralizingEstimator : 1077/1225 [00:02<00:00, 394.12it/s]
89%|████████▉ | Scoring GeneralizingEstimator : 1091/1225 [00:02<00:00, 395.15it/s]
90%|█████████ | Scoring GeneralizingEstimator : 1108/1225 [00:02<00:00, 399.41it/s]
92%|█████████▏| Scoring GeneralizingEstimator : 1121/1225 [00:03<00:00, 398.74it/s]
93%|█████████▎| Scoring GeneralizingEstimator : 1135/1225 [00:03<00:00, 399.44it/s]
94%|█████████▍| Scoring GeneralizingEstimator : 1149/1225 [00:03<00:00, 400.23it/s]
95%|█████████▍| Scoring GeneralizingEstimator : 1163/1225 [00:03<00:00, 401.02it/s]
96%|█████████▌| Scoring GeneralizingEstimator : 1175/1225 [00:03<00:00, 398.69it/s]
97%|█████████▋| Scoring GeneralizingEstimator : 1187/1225 [00:03<00:00, 396.53it/s]
98%|█████████▊| Scoring GeneralizingEstimator : 1201/1225 [00:03<00:00, 395.88it/s]
99%|█████████▉| Scoring GeneralizingEstimator : 1215/1225 [00:03<00:00, 396.78it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00, 398.31it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00, 373.36it/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.806 seconds)