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)
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]
6%|▌ | Fitting GeneralizingEstimator : 2/35 [00:00<00:00, 54.33it/s]
11%|█▏ | Fitting GeneralizingEstimator : 4/35 [00:00<00:00, 56.80it/s]
17%|█▋ | Fitting GeneralizingEstimator : 6/35 [00:00<00:00, 57.65it/s]
23%|██▎ | Fitting GeneralizingEstimator : 8/35 [00:00<00:00, 58.02it/s]
29%|██▊ | Fitting GeneralizingEstimator : 10/35 [00:00<00:00, 58.33it/s]
37%|███▋ | Fitting GeneralizingEstimator : 13/35 [00:00<00:00, 64.06it/s]
46%|████▌ | Fitting GeneralizingEstimator : 16/35 [00:00<00:00, 68.14it/s]
51%|█████▏ | Fitting GeneralizingEstimator : 18/35 [00:00<00:00, 66.84it/s]
60%|██████ | Fitting GeneralizingEstimator : 21/35 [00:00<00:00, 69.81it/s]
66%|██████▌ | Fitting GeneralizingEstimator : 23/35 [00:00<00:00, 67.93it/s]
71%|███████▏ | Fitting GeneralizingEstimator : 25/35 [00:00<00:00, 66.95it/s]
77%|███████▋ | Fitting GeneralizingEstimator : 27/35 [00:00<00:00, 66.13it/s]
86%|████████▌ | Fitting GeneralizingEstimator : 30/35 [00:00<00:00, 68.46it/s]
91%|█████████▏| Fitting GeneralizingEstimator : 32/35 [00:00<00:00, 67.59it/s]
97%|█████████▋| Fitting GeneralizingEstimator : 34/35 [00:00<00:00, 66.83it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 68.05it/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 : 10/1225 [00:00<00:04, 271.49it/s]
2%|▏ | Scoring GeneralizingEstimator : 21/1225 [00:00<00:04, 298.31it/s]
3%|▎ | Scoring GeneralizingEstimator : 32/1225 [00:00<00:03, 307.71it/s]
4%|▎ | Scoring GeneralizingEstimator : 43/1225 [00:00<00:03, 312.28it/s]
4%|▍ | Scoring GeneralizingEstimator : 54/1225 [00:00<00:03, 315.25it/s]
5%|▌ | Scoring GeneralizingEstimator : 66/1225 [00:00<00:03, 322.24it/s]
6%|▋ | Scoring GeneralizingEstimator : 77/1225 [00:00<00:03, 322.91it/s]
7%|▋ | Scoring GeneralizingEstimator : 89/1225 [00:00<00:03, 327.83it/s]
8%|▊ | Scoring GeneralizingEstimator : 101/1225 [00:00<00:03, 331.46it/s]
9%|▉ | Scoring GeneralizingEstimator : 112/1225 [00:00<00:03, 330.76it/s]
10%|█ | Scoring GeneralizingEstimator : 123/1225 [00:00<00:03, 330.27it/s]
11%|█ | Scoring GeneralizingEstimator : 137/1225 [00:00<00:03, 336.46it/s]
12%|█▏ | Scoring GeneralizingEstimator : 151/1225 [00:00<00:03, 344.13it/s]
13%|█▎ | Scoring GeneralizingEstimator : 164/1225 [00:00<00:03, 348.16it/s]
15%|█▍ | Scoring GeneralizingEstimator : 178/1225 [00:00<00:02, 354.30it/s]
16%|█▌ | Scoring GeneralizingEstimator : 191/1225 [00:00<00:02, 356.84it/s]
17%|█▋ | Scoring GeneralizingEstimator : 205/1225 [00:00<00:02, 361.78it/s]
18%|█▊ | Scoring GeneralizingEstimator : 219/1225 [00:00<00:02, 366.16it/s]
19%|█▉ | Scoring GeneralizingEstimator : 233/1225 [00:00<00:02, 369.81it/s]
20%|██ | Scoring GeneralizingEstimator : 246/1225 [00:00<00:02, 371.09it/s]
21%|██ | Scoring GeneralizingEstimator : 259/1225 [00:00<00:02, 372.24it/s]
22%|██▏ | Scoring GeneralizingEstimator : 272/1225 [00:00<00:02, 372.34it/s]
23%|██▎ | Scoring GeneralizingEstimator : 284/1225 [00:00<00:02, 371.24it/s]
24%|██▍ | Scoring GeneralizingEstimator : 297/1225 [00:00<00:02, 372.28it/s]
25%|██▌ | Scoring GeneralizingEstimator : 310/1225 [00:00<00:02, 373.26it/s]
26%|██▋ | Scoring GeneralizingEstimator : 322/1225 [00:00<00:02, 370.49it/s]
27%|██▋ | Scoring GeneralizingEstimator : 333/1225 [00:00<00:02, 367.34it/s]
28%|██▊ | Scoring GeneralizingEstimator : 345/1225 [00:00<00:02, 366.60it/s]
29%|██▉ | Scoring GeneralizingEstimator : 358/1225 [00:00<00:02, 367.86it/s]
30%|███ | Scoring GeneralizingEstimator : 371/1225 [00:01<00:02, 368.91it/s]
31%|███▏ | Scoring GeneralizingEstimator : 383/1225 [00:01<00:02, 367.77it/s]
32%|███▏ | Scoring GeneralizingEstimator : 393/1225 [00:01<00:02, 362.83it/s]
33%|███▎ | Scoring GeneralizingEstimator : 406/1225 [00:01<00:02, 364.18it/s]
34%|███▍ | Scoring GeneralizingEstimator : 419/1225 [00:01<00:02, 365.37it/s]
35%|███▌ | Scoring GeneralizingEstimator : 433/1225 [00:01<00:02, 366.62it/s]
36%|███▋ | Scoring GeneralizingEstimator : 445/1225 [00:01<00:02, 365.98it/s]
37%|███▋ | Scoring GeneralizingEstimator : 458/1225 [00:01<00:02, 367.09it/s]
38%|███▊ | Scoring GeneralizingEstimator : 471/1225 [00:01<00:02, 368.22it/s]
39%|███▉ | Scoring GeneralizingEstimator : 482/1225 [00:01<00:02, 365.61it/s]
40%|████ | Scoring GeneralizingEstimator : 492/1225 [00:01<00:02, 361.69it/s]
41%|████ | Scoring GeneralizingEstimator : 504/1225 [00:01<00:01, 361.19it/s]
42%|████▏ | Scoring GeneralizingEstimator : 517/1225 [00:01<00:01, 362.50it/s]
43%|████▎ | Scoring GeneralizingEstimator : 530/1225 [00:01<00:01, 362.59it/s]
44%|████▍ | Scoring GeneralizingEstimator : 539/1225 [00:01<00:01, 357.33it/s]
45%|████▌ | Scoring GeneralizingEstimator : 552/1225 [00:01<00:01, 358.68it/s]
46%|████▌ | Scoring GeneralizingEstimator : 564/1225 [00:01<00:01, 358.56it/s]
47%|████▋ | Scoring GeneralizingEstimator : 577/1225 [00:01<00:01, 359.87it/s]
48%|████▊ | Scoring GeneralizingEstimator : 590/1225 [00:01<00:01, 361.23it/s]
49%|████▉ | Scoring GeneralizingEstimator : 599/1225 [00:01<00:01, 356.14it/s]
50%|████▉ | Scoring GeneralizingEstimator : 609/1225 [00:01<00:01, 344.66it/s]
50%|█████ | Scoring GeneralizingEstimator : 617/1225 [00:01<00:01, 337.39it/s]
51%|█████ | Scoring GeneralizingEstimator : 622/1225 [00:01<00:01, 327.62it/s]
52%|█████▏ | Scoring GeneralizingEstimator : 635/1225 [00:01<00:01, 330.67it/s]
53%|█████▎ | Scoring GeneralizingEstimator : 646/1225 [00:01<00:01, 328.96it/s]
53%|█████▎ | Scoring GeneralizingEstimator : 651/1225 [00:01<00:01, 319.70it/s]
53%|█████▎ | Scoring GeneralizingEstimator : 655/1225 [00:01<00:01, 309.37it/s]
54%|█████▍ | Scoring GeneralizingEstimator : 667/1225 [00:01<00:01, 311.76it/s]
55%|█████▌ | Scoring GeneralizingEstimator : 679/1225 [00:01<00:01, 313.86it/s]
56%|█████▋ | Scoring GeneralizingEstimator : 692/1225 [00:02<00:01, 316.14it/s]
57%|█████▋ | Scoring GeneralizingEstimator : 704/1225 [00:02<00:01, 318.19it/s]
59%|█████▊ | Scoring GeneralizingEstimator : 717/1225 [00:02<00:01, 321.39it/s]
60%|█████▉ | Scoring GeneralizingEstimator : 729/1225 [00:02<00:01, 323.18it/s]
60%|██████ | Scoring GeneralizingEstimator : 741/1225 [00:02<00:01, 324.54it/s]
61%|██████▏ | Scoring GeneralizingEstimator : 753/1225 [00:02<00:01, 325.92it/s]
62%|██████▏ | Scoring GeneralizingEstimator : 764/1225 [00:02<00:01, 325.92it/s]
63%|██████▎ | Scoring GeneralizingEstimator : 777/1225 [00:02<00:01, 328.03it/s]
64%|██████▍ | Scoring GeneralizingEstimator : 789/1225 [00:02<00:01, 329.45it/s]
65%|██████▌ | Scoring GeneralizingEstimator : 802/1225 [00:02<00:01, 332.30it/s]
67%|██████▋ | Scoring GeneralizingEstimator : 815/1225 [00:02<00:01, 334.99it/s]
68%|██████▊ | Scoring GeneralizingEstimator : 828/1225 [00:02<00:01, 337.55it/s]
69%|██████▊ | Scoring GeneralizingEstimator : 841/1225 [00:02<00:01, 339.98it/s]
70%|██████▉ | Scoring GeneralizingEstimator : 854/1225 [00:02<00:01, 342.26it/s]
71%|███████ | Scoring GeneralizingEstimator : 867/1225 [00:02<00:01, 344.45it/s]
72%|███████▏ | Scoring GeneralizingEstimator : 882/1225 [00:02<00:00, 348.26it/s]
73%|███████▎ | Scoring GeneralizingEstimator : 895/1225 [00:02<00:00, 350.17it/s]
74%|███████▍ | Scoring GeneralizingEstimator : 908/1225 [00:02<00:00, 351.94it/s]
75%|███████▌ | Scoring GeneralizingEstimator : 921/1225 [00:02<00:00, 353.66it/s]
76%|███████▋ | Scoring GeneralizingEstimator : 935/1225 [00:02<00:00, 356.68it/s]
77%|███████▋ | Scoring GeneralizingEstimator : 949/1225 [00:02<00:00, 359.57it/s]
79%|███████▊ | Scoring GeneralizingEstimator : 963/1225 [00:02<00:00, 362.38it/s]
80%|███████▉ | Scoring GeneralizingEstimator : 977/1225 [00:02<00:00, 364.84it/s]
81%|████████ | Scoring GeneralizingEstimator : 990/1225 [00:02<00:00, 365.80it/s]
82%|████████▏ | Scoring GeneralizingEstimator : 1004/1225 [00:02<00:00, 367.72it/s]
83%|████████▎ | Scoring GeneralizingEstimator : 1017/1225 [00:02<00:00, 368.62it/s]
84%|████████▍ | Scoring GeneralizingEstimator : 1030/1225 [00:02<00:00, 369.48it/s]
85%|████████▌ | Scoring GeneralizingEstimator : 1044/1225 [00:02<00:00, 371.64it/s]
86%|████████▋ | Scoring GeneralizingEstimator : 1057/1225 [00:02<00:00, 372.35it/s]
87%|████████▋ | Scoring GeneralizingEstimator : 1071/1225 [00:03<00:00, 373.61it/s]
88%|████████▊ | Scoring GeneralizingEstimator : 1084/1225 [00:03<00:00, 374.20it/s]
90%|████████▉ | Scoring GeneralizingEstimator : 1097/1225 [00:03<00:00, 374.79it/s]
91%|█████████ | Scoring GeneralizingEstimator : 1111/1225 [00:03<00:00, 376.58it/s]
92%|█████████▏| Scoring GeneralizingEstimator : 1124/1225 [00:03<00:00, 376.94it/s]
93%|█████████▎| Scoring GeneralizingEstimator : 1138/1225 [00:03<00:00, 377.32it/s]
94%|█████████▍| Scoring GeneralizingEstimator : 1151/1225 [00:03<00:00, 377.66it/s]
95%|█████████▌| Scoring GeneralizingEstimator : 1164/1225 [00:03<00:00, 378.07it/s]
96%|█████████▌| Scoring GeneralizingEstimator : 1177/1225 [00:03<00:00, 378.44it/s]
97%|█████████▋| Scoring GeneralizingEstimator : 1191/1225 [00:03<00:00, 380.12it/s]
98%|█████████▊| Scoring GeneralizingEstimator : 1204/1225 [00:03<00:00, 380.26it/s]
99%|█████████▉| Scoring GeneralizingEstimator : 1217/1225 [00:03<00:00, 380.54it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00, 382.24it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00, 359.12it/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.864 seconds)