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, 55.06it/s]
11%|█▏ | Fitting GeneralizingEstimator : 4/35 [00:00<00:00, 57.19it/s]
17%|█▋ | Fitting GeneralizingEstimator : 6/35 [00:00<00:00, 57.96it/s]
23%|██▎ | Fitting GeneralizingEstimator : 8/35 [00:00<00:00, 58.34it/s]
31%|███▏ | Fitting GeneralizingEstimator : 11/35 [00:00<00:00, 64.98it/s]
40%|████ | Fitting GeneralizingEstimator : 14/35 [00:00<00:00, 69.44it/s]
46%|████▌ | Fitting GeneralizingEstimator : 16/35 [00:00<00:00, 67.79it/s]
54%|█████▍ | Fitting GeneralizingEstimator : 19/35 [00:00<00:00, 70.88it/s]
60%|██████ | Fitting GeneralizingEstimator : 21/35 [00:00<00:00, 69.35it/s]
69%|██████▊ | Fitting GeneralizingEstimator : 24/35 [00:00<00:00, 71.12it/s]
74%|███████▍ | Fitting GeneralizingEstimator : 26/35 [00:00<00:00, 69.79it/s]
80%|████████ | Fitting GeneralizingEstimator : 28/35 [00:00<00:00, 68.67it/s]
89%|████████▊ | Fitting GeneralizingEstimator : 31/35 [00:00<00:00, 70.57it/s]
94%|█████████▍| Fitting GeneralizingEstimator : 33/35 [00:00<00:00, 69.49it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 71.50it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 70.46it/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, 304.23it/s]
2%|▏ | Scoring GeneralizingEstimator : 23/1225 [00:00<00:03, 329.46it/s]
3%|▎ | Scoring GeneralizingEstimator : 34/1225 [00:00<00:03, 328.23it/s]
4%|▍ | Scoring GeneralizingEstimator : 46/1225 [00:00<00:03, 335.29it/s]
5%|▍ | Scoring GeneralizingEstimator : 57/1225 [00:00<00:03, 333.38it/s]
6%|▌ | Scoring GeneralizingEstimator : 69/1225 [00:00<00:03, 337.75it/s]
7%|▋ | Scoring GeneralizingEstimator : 80/1225 [00:00<00:03, 335.85it/s]
7%|▋ | Scoring GeneralizingEstimator : 91/1225 [00:00<00:03, 334.46it/s]
8%|▊ | Scoring GeneralizingEstimator : 102/1225 [00:00<00:03, 332.79it/s]
9%|▉ | Scoring GeneralizingEstimator : 113/1225 [00:00<00:03, 330.78it/s]
10%|█ | Scoring GeneralizingEstimator : 125/1225 [00:00<00:03, 333.22it/s]
11%|█ | Scoring GeneralizingEstimator : 136/1225 [00:00<00:03, 332.44it/s]
12%|█▏ | Scoring GeneralizingEstimator : 147/1225 [00:00<00:03, 331.86it/s]
13%|█▎ | Scoring GeneralizingEstimator : 159/1225 [00:00<00:03, 333.44it/s]
14%|█▍ | Scoring GeneralizingEstimator : 170/1225 [00:00<00:03, 332.76it/s]
15%|█▍ | Scoring GeneralizingEstimator : 182/1225 [00:00<00:03, 334.85it/s]
16%|█▌ | Scoring GeneralizingEstimator : 194/1225 [00:00<00:03, 334.27it/s]
17%|█▋ | Scoring GeneralizingEstimator : 205/1225 [00:00<00:03, 333.21it/s]
18%|█▊ | Scoring GeneralizingEstimator : 216/1225 [00:00<00:03, 332.70it/s]
19%|█▊ | Scoring GeneralizingEstimator : 227/1225 [00:00<00:03, 332.16it/s]
20%|█▉ | Scoring GeneralizingEstimator : 239/1225 [00:00<00:02, 333.65it/s]
20%|██ | Scoring GeneralizingEstimator : 250/1225 [00:00<00:02, 333.13it/s]
21%|██▏ | Scoring GeneralizingEstimator : 261/1225 [00:00<00:02, 332.66it/s]
22%|██▏ | Scoring GeneralizingEstimator : 272/1225 [00:00<00:02, 332.22it/s]
23%|██▎ | Scoring GeneralizingEstimator : 283/1225 [00:00<00:02, 331.82it/s]
24%|██▍ | Scoring GeneralizingEstimator : 294/1225 [00:00<00:02, 331.47it/s]
25%|██▍ | Scoring GeneralizingEstimator : 306/1225 [00:00<00:02, 332.30it/s]
26%|██▌ | Scoring GeneralizingEstimator : 316/1225 [00:00<00:02, 329.96it/s]
27%|██▋ | Scoring GeneralizingEstimator : 327/1225 [00:00<00:02, 329.52it/s]
28%|██▊ | Scoring GeneralizingEstimator : 338/1225 [00:01<00:02, 329.37it/s]
29%|██▊ | Scoring GeneralizingEstimator : 350/1225 [00:01<00:02, 329.38it/s]
29%|██▉ | Scoring GeneralizingEstimator : 361/1225 [00:01<00:02, 327.45it/s]
30%|███ | Scoring GeneralizingEstimator : 372/1225 [00:01<00:02, 327.43it/s]
31%|███▏ | Scoring GeneralizingEstimator : 383/1225 [00:01<00:02, 326.15it/s]
32%|███▏ | Scoring GeneralizingEstimator : 395/1225 [00:01<00:02, 327.79it/s]
33%|███▎ | Scoring GeneralizingEstimator : 406/1225 [00:01<00:02, 327.73it/s]
34%|███▍ | Scoring GeneralizingEstimator : 417/1225 [00:01<00:02, 327.63it/s]
35%|███▍ | Scoring GeneralizingEstimator : 428/1225 [00:01<00:02, 327.53it/s]
36%|███▌ | Scoring GeneralizingEstimator : 440/1225 [00:01<00:02, 329.16it/s]
37%|███▋ | Scoring GeneralizingEstimator : 452/1225 [00:01<00:02, 330.65it/s]
38%|███▊ | Scoring GeneralizingEstimator : 463/1225 [00:01<00:02, 330.39it/s]
39%|███▉ | Scoring GeneralizingEstimator : 475/1225 [00:01<00:02, 330.68it/s]
40%|███▉ | Scoring GeneralizingEstimator : 487/1225 [00:01<00:02, 332.12it/s]
41%|████ | Scoring GeneralizingEstimator : 498/1225 [00:01<00:02, 331.67it/s]
42%|████▏ | Scoring GeneralizingEstimator : 510/1225 [00:01<00:02, 332.96it/s]
43%|████▎ | Scoring GeneralizingEstimator : 521/1225 [00:01<00:02, 332.58it/s]
44%|████▎ | Scoring GeneralizingEstimator : 533/1225 [00:01<00:02, 333.87it/s]
44%|████▍ | Scoring GeneralizingEstimator : 545/1225 [00:01<00:02, 335.09it/s]
45%|████▌ | Scoring GeneralizingEstimator : 557/1225 [00:01<00:01, 334.76it/s]
46%|████▋ | Scoring GeneralizingEstimator : 569/1225 [00:01<00:01, 335.93it/s]
47%|████▋ | Scoring GeneralizingEstimator : 580/1225 [00:01<00:01, 335.20it/s]
48%|████▊ | Scoring GeneralizingEstimator : 591/1225 [00:01<00:01, 334.71it/s]
49%|████▉ | Scoring GeneralizingEstimator : 603/1225 [00:01<00:01, 335.69it/s]
50%|█████ | Scoring GeneralizingEstimator : 614/1225 [00:01<00:01, 335.20it/s]
51%|█████ | Scoring GeneralizingEstimator : 625/1225 [00:01<00:01, 334.67it/s]
52%|█████▏ | Scoring GeneralizingEstimator : 637/1225 [00:01<00:01, 335.81it/s]
53%|█████▎ | Scoring GeneralizingEstimator : 648/1225 [00:01<00:01, 335.31it/s]
54%|█████▍ | Scoring GeneralizingEstimator : 659/1225 [00:01<00:01, 334.83it/s]
55%|█████▍ | Scoring GeneralizingEstimator : 671/1225 [00:02<00:01, 334.31it/s]
56%|█████▌ | Scoring GeneralizingEstimator : 682/1225 [00:02<00:01, 333.87it/s]
57%|█████▋ | Scoring GeneralizingEstimator : 693/1225 [00:02<00:01, 333.44it/s]
58%|█████▊ | Scoring GeneralizingEstimator : 705/1225 [00:02<00:01, 334.63it/s]
58%|█████▊ | Scoring GeneralizingEstimator : 716/1225 [00:02<00:01, 334.18it/s]
59%|█████▉ | Scoring GeneralizingEstimator : 727/1225 [00:02<00:01, 333.78it/s]
60%|██████ | Scoring GeneralizingEstimator : 739/1225 [00:02<00:01, 334.72it/s]
61%|██████ | Scoring GeneralizingEstimator : 750/1225 [00:02<00:01, 334.32it/s]
62%|██████▏ | Scoring GeneralizingEstimator : 761/1225 [00:02<00:01, 333.93it/s]
63%|██████▎ | Scoring GeneralizingEstimator : 767/1225 [00:02<00:01, 325.97it/s]
63%|██████▎ | Scoring GeneralizingEstimator : 774/1225 [00:02<00:01, 319.68it/s]
64%|██████▍ | Scoring GeneralizingEstimator : 784/1225 [00:02<00:01, 315.38it/s]
65%|██████▍ | Scoring GeneralizingEstimator : 791/1225 [00:02<00:01, 309.96it/s]
65%|██████▌ | Scoring GeneralizingEstimator : 799/1225 [00:02<00:01, 306.32it/s]
66%|██████▌ | Scoring GeneralizingEstimator : 811/1225 [00:02<00:01, 307.63it/s]
67%|██████▋ | Scoring GeneralizingEstimator : 822/1225 [00:02<00:01, 308.43it/s]
68%|██████▊ | Scoring GeneralizingEstimator : 833/1225 [00:02<00:01, 309.30it/s]
69%|██████▉ | Scoring GeneralizingEstimator : 844/1225 [00:02<00:01, 310.15it/s]
70%|██████▉ | Scoring GeneralizingEstimator : 856/1225 [00:02<00:01, 312.26it/s]
71%|███████ | Scoring GeneralizingEstimator : 867/1225 [00:02<00:01, 312.99it/s]
72%|███████▏ | Scoring GeneralizingEstimator : 879/1225 [00:02<00:01, 313.98it/s]
73%|███████▎ | Scoring GeneralizingEstimator : 890/1225 [00:02<00:01, 314.61it/s]
74%|███████▎ | Scoring GeneralizingEstimator : 901/1225 [00:02<00:01, 315.22it/s]
74%|███████▍ | Scoring GeneralizingEstimator : 912/1225 [00:02<00:00, 315.76it/s]
75%|███████▌ | Scoring GeneralizingEstimator : 923/1225 [00:02<00:00, 316.09it/s]
76%|███████▌ | Scoring GeneralizingEstimator : 934/1225 [00:02<00:00, 316.43it/s]
77%|███████▋ | Scoring GeneralizingEstimator : 945/1225 [00:02<00:00, 316.86it/s]
78%|███████▊ | Scoring GeneralizingEstimator : 955/1225 [00:02<00:00, 315.86it/s]
79%|███████▉ | Scoring GeneralizingEstimator : 966/1225 [00:02<00:00, 315.90it/s]
80%|███████▉ | Scoring GeneralizingEstimator : 977/1225 [00:03<00:00, 316.44it/s]
81%|████████ | Scoring GeneralizingEstimator : 989/1225 [00:03<00:00, 318.44it/s]
82%|████████▏ | Scoring GeneralizingEstimator : 1000/1225 [00:03<00:00, 318.83it/s]
83%|████████▎ | Scoring GeneralizingEstimator : 1014/1225 [00:03<00:00, 320.58it/s]
84%|████████▎ | Scoring GeneralizingEstimator : 1025/1225 [00:03<00:00, 320.62it/s]
84%|████████▍ | Scoring GeneralizingEstimator : 1033/1225 [00:03<00:00, 315.44it/s]
85%|████████▍ | Scoring GeneralizingEstimator : 1040/1225 [00:03<00:00, 310.07it/s]
85%|████████▌ | Scoring GeneralizingEstimator : 1046/1225 [00:03<00:00, 301.43it/s]
86%|████████▌ | Scoring GeneralizingEstimator : 1055/1225 [00:03<00:00, 299.72it/s]
87%|████████▋ | Scoring GeneralizingEstimator : 1066/1225 [00:03<00:00, 301.02it/s]
88%|████████▊ | Scoring GeneralizingEstimator : 1077/1225 [00:03<00:00, 301.76it/s]
88%|████████▊ | Scoring GeneralizingEstimator : 1084/1225 [00:03<00:00, 297.14it/s]
89%|████████▉ | Scoring GeneralizingEstimator : 1089/1225 [00:03<00:00, 289.81it/s]
90%|████████▉ | Scoring GeneralizingEstimator : 1099/1225 [00:03<00:00, 290.17it/s]
91%|█████████ | Scoring GeneralizingEstimator : 1110/1225 [00:03<00:00, 291.97it/s]
92%|█████████▏| Scoring GeneralizingEstimator : 1121/1225 [00:03<00:00, 293.58it/s]
92%|█████████▏| Scoring GeneralizingEstimator : 1133/1225 [00:03<00:00, 296.59it/s]
93%|█████████▎| Scoring GeneralizingEstimator : 1144/1225 [00:03<00:00, 298.05it/s]
94%|█████████▍| Scoring GeneralizingEstimator : 1156/1225 [00:03<00:00, 300.50it/s]
95%|█████████▌| Scoring GeneralizingEstimator : 1167/1225 [00:03<00:00, 301.75it/s]
96%|█████████▌| Scoring GeneralizingEstimator : 1178/1225 [00:03<00:00, 302.98it/s]
97%|█████████▋| Scoring GeneralizingEstimator : 1190/1225 [00:03<00:00, 305.25it/s]
98%|█████████▊| Scoring GeneralizingEstimator : 1201/1225 [00:03<00:00, 306.27it/s]
99%|█████████▉| Scoring GeneralizingEstimator : 1214/1225 [00:03<00:00, 310.14it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00, 312.99it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00, 319.75it/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 6.104 seconds)