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, 56.19it/s]
11%|█▏ | Fitting GeneralizingEstimator : 4/35 [00:00<00:00, 57.81it/s]
17%|█▋ | Fitting GeneralizingEstimator : 6/35 [00:00<00:00, 58.37it/s]
23%|██▎ | Fitting GeneralizingEstimator : 8/35 [00:00<00:00, 58.64it/s]
31%|███▏ | Fitting GeneralizingEstimator : 11/35 [00:00<00:00, 65.31it/s]
40%|████ | Fitting GeneralizingEstimator : 14/35 [00:00<00:00, 69.78it/s]
49%|████▊ | Fitting GeneralizingEstimator : 17/35 [00:00<00:00, 72.97it/s]
57%|█████▋ | Fitting GeneralizingEstimator : 20/35 [00:00<00:00, 75.12it/s]
63%|██████▎ | Fitting GeneralizingEstimator : 22/35 [00:00<00:00, 73.01it/s]
71%|███████▏ | Fitting GeneralizingEstimator : 25/35 [00:00<00:00, 74.62it/s]
77%|███████▋ | Fitting GeneralizingEstimator : 27/35 [00:00<00:00, 72.87it/s]
83%|████████▎ | Fitting GeneralizingEstimator : 29/35 [00:00<00:00, 71.42it/s]
91%|█████████▏| Fitting GeneralizingEstimator : 32/35 [00:00<00:00, 73.23it/s]
97%|█████████▋| Fitting GeneralizingEstimator : 34/35 [00:00<00:00, 71.90it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 73.30it/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, 327.92it/s]
2%|▏ | Scoring GeneralizingEstimator : 23/1225 [00:00<00:03, 326.85it/s]
3%|▎ | Scoring GeneralizingEstimator : 35/1225 [00:00<00:03, 336.54it/s]
4%|▍ | Scoring GeneralizingEstimator : 47/1225 [00:00<00:03, 341.86it/s]
5%|▍ | Scoring GeneralizingEstimator : 59/1225 [00:00<00:03, 344.97it/s]
6%|▌ | Scoring GeneralizingEstimator : 71/1225 [00:00<00:03, 346.47it/s]
7%|▋ | Scoring GeneralizingEstimator : 82/1225 [00:00<00:03, 341.97it/s]
8%|▊ | Scoring GeneralizingEstimator : 93/1225 [00:00<00:03, 339.32it/s]
9%|▊ | Scoring GeneralizingEstimator : 105/1225 [00:00<00:03, 341.37it/s]
10%|▉ | Scoring GeneralizingEstimator : 117/1225 [00:00<00:03, 343.28it/s]
11%|█ | Scoring GeneralizingEstimator : 130/1225 [00:00<00:03, 345.14it/s]
12%|█▏ | Scoring GeneralizingEstimator : 141/1225 [00:00<00:03, 343.17it/s]
12%|█▏ | Scoring GeneralizingEstimator : 153/1225 [00:00<00:03, 344.46it/s]
13%|█▎ | Scoring GeneralizingEstimator : 165/1225 [00:00<00:03, 345.55it/s]
14%|█▍ | Scoring GeneralizingEstimator : 177/1225 [00:00<00:03, 345.92it/s]
15%|█▌ | Scoring GeneralizingEstimator : 188/1225 [00:00<00:03, 344.08it/s]
16%|█▋ | Scoring GeneralizingEstimator : 200/1225 [00:00<00:02, 345.10it/s]
17%|█▋ | Scoring GeneralizingEstimator : 212/1225 [00:00<00:02, 345.97it/s]
18%|█▊ | Scoring GeneralizingEstimator : 224/1225 [00:00<00:02, 346.56it/s]
19%|█▉ | Scoring GeneralizingEstimator : 236/1225 [00:00<00:02, 347.35it/s]
20%|██ | Scoring GeneralizingEstimator : 248/1225 [00:00<00:02, 347.13it/s]
21%|██ | Scoring GeneralizingEstimator : 260/1225 [00:00<00:02, 347.84it/s]
22%|██▏ | Scoring GeneralizingEstimator : 271/1225 [00:00<00:02, 346.32it/s]
23%|██▎ | Scoring GeneralizingEstimator : 282/1225 [00:00<00:02, 344.93it/s]
24%|██▍ | Scoring GeneralizingEstimator : 295/1225 [00:00<00:02, 345.50it/s]
25%|██▌ | Scoring GeneralizingEstimator : 307/1225 [00:00<00:02, 346.25it/s]
26%|██▌ | Scoring GeneralizingEstimator : 319/1225 [00:00<00:02, 346.74it/s]
27%|██▋ | Scoring GeneralizingEstimator : 330/1225 [00:00<00:02, 345.34it/s]
28%|██▊ | Scoring GeneralizingEstimator : 342/1225 [00:00<00:02, 346.05it/s]
29%|██▉ | Scoring GeneralizingEstimator : 354/1225 [00:01<00:02, 346.49it/s]
29%|██▉ | Scoring GeneralizingEstimator : 361/1225 [00:01<00:02, 336.49it/s]
30%|███ | Scoring GeneralizingEstimator : 371/1225 [00:01<00:02, 334.09it/s]
31%|███▏ | Scoring GeneralizingEstimator : 383/1225 [00:01<00:02, 335.41it/s]
32%|███▏ | Scoring GeneralizingEstimator : 395/1225 [00:01<00:02, 336.67it/s]
33%|███▎ | Scoring GeneralizingEstimator : 408/1225 [00:01<00:02, 339.56it/s]
34%|███▍ | Scoring GeneralizingEstimator : 418/1225 [00:01<00:02, 336.98it/s]
35%|███▍ | Scoring GeneralizingEstimator : 425/1225 [00:01<00:02, 329.31it/s]
36%|███▌ | Scoring GeneralizingEstimator : 438/1225 [00:01<00:02, 332.60it/s]
37%|███▋ | Scoring GeneralizingEstimator : 451/1225 [00:01<00:02, 335.64it/s]
38%|███▊ | Scoring GeneralizingEstimator : 462/1225 [00:01<00:02, 332.50it/s]
38%|███▊ | Scoring GeneralizingEstimator : 469/1225 [00:01<00:02, 325.49it/s]
39%|███▉ | Scoring GeneralizingEstimator : 479/1225 [00:01<00:02, 323.91it/s]
40%|████ | Scoring GeneralizingEstimator : 492/1225 [00:01<00:02, 327.30it/s]
41%|████ | Scoring GeneralizingEstimator : 504/1225 [00:01<00:02, 328.91it/s]
42%|████▏ | Scoring GeneralizingEstimator : 517/1225 [00:01<00:02, 332.01it/s]
43%|████▎ | Scoring GeneralizingEstimator : 529/1225 [00:01<00:02, 333.28it/s]
44%|████▍ | Scoring GeneralizingEstimator : 540/1225 [00:01<00:02, 332.94it/s]
45%|████▌ | Scoring GeneralizingEstimator : 554/1225 [00:01<00:01, 337.15it/s]
46%|████▋ | Scoring GeneralizingEstimator : 567/1225 [00:01<00:01, 339.81it/s]
47%|████▋ | Scoring GeneralizingEstimator : 580/1225 [00:01<00:01, 342.31it/s]
48%|████▊ | Scoring GeneralizingEstimator : 593/1225 [00:01<00:01, 344.39it/s]
49%|████▉ | Scoring GeneralizingEstimator : 605/1225 [00:01<00:01, 344.93it/s]
50%|█████ | Scoring GeneralizingEstimator : 617/1225 [00:01<00:01, 344.60it/s]
51%|█████▏ | Scoring GeneralizingEstimator : 629/1225 [00:01<00:01, 343.70it/s]
52%|█████▏ | Scoring GeneralizingEstimator : 640/1225 [00:01<00:01, 342.81it/s]
53%|█████▎ | Scoring GeneralizingEstimator : 653/1225 [00:01<00:01, 345.10it/s]
54%|█████▍ | Scoring GeneralizingEstimator : 666/1225 [00:01<00:01, 347.08it/s]
55%|█████▌ | Scoring GeneralizingEstimator : 677/1225 [00:01<00:01, 346.02it/s]
56%|█████▌ | Scoring GeneralizingEstimator : 688/1225 [00:02<00:01, 345.02it/s]
57%|█████▋ | Scoring GeneralizingEstimator : 702/1225 [00:02<00:01, 348.63it/s]
58%|█████▊ | Scoring GeneralizingEstimator : 715/1225 [00:02<00:01, 350.54it/s]
59%|█████▉ | Scoring GeneralizingEstimator : 728/1225 [00:02<00:01, 352.09it/s]
60%|██████ | Scoring GeneralizingEstimator : 741/1225 [00:02<00:01, 353.78it/s]
62%|██████▏ | Scoring GeneralizingEstimator : 754/1225 [00:02<00:01, 355.09it/s]
63%|██████▎ | Scoring GeneralizingEstimator : 767/1225 [00:02<00:01, 356.66it/s]
64%|██████▍ | Scoring GeneralizingEstimator : 782/1225 [00:02<00:01, 359.64it/s]
65%|██████▍ | Scoring GeneralizingEstimator : 794/1225 [00:02<00:01, 359.46it/s]
66%|██████▌ | Scoring GeneralizingEstimator : 808/1225 [00:02<00:01, 360.77it/s]
67%|██████▋ | Scoring GeneralizingEstimator : 821/1225 [00:02<00:01, 362.02it/s]
68%|██████▊ | Scoring GeneralizingEstimator : 832/1225 [00:02<00:01, 360.23it/s]
69%|██████▉ | Scoring GeneralizingEstimator : 843/1225 [00:02<00:01, 358.52it/s]
70%|██████▉ | Scoring GeneralizingEstimator : 855/1225 [00:02<00:01, 358.23it/s]
71%|███████ | Scoring GeneralizingEstimator : 868/1225 [00:02<00:00, 359.66it/s]
72%|███████▏ | Scoring GeneralizingEstimator : 881/1225 [00:02<00:00, 360.99it/s]
73%|███████▎ | Scoring GeneralizingEstimator : 892/1225 [00:02<00:00, 359.19it/s]
74%|███████▍ | Scoring GeneralizingEstimator : 904/1225 [00:02<00:00, 358.98it/s]
75%|███████▍ | Scoring GeneralizingEstimator : 917/1225 [00:02<00:00, 360.27it/s]
76%|███████▌ | Scoring GeneralizingEstimator : 931/1225 [00:02<00:00, 361.91it/s]
77%|███████▋ | Scoring GeneralizingEstimator : 944/1225 [00:02<00:00, 363.11it/s]
78%|███████▊ | Scoring GeneralizingEstimator : 957/1225 [00:02<00:00, 364.27it/s]
79%|███████▉ | Scoring GeneralizingEstimator : 970/1225 [00:02<00:00, 365.38it/s]
80%|████████ | Scoring GeneralizingEstimator : 983/1225 [00:02<00:00, 366.44it/s]
81%|████████▏ | Scoring GeneralizingEstimator : 996/1225 [00:02<00:00, 367.43it/s]
82%|████████▏ | Scoring GeneralizingEstimator : 1008/1225 [00:02<00:00, 366.71it/s]
83%|████████▎ | Scoring GeneralizingEstimator : 1021/1225 [00:02<00:00, 367.65it/s]
84%|████████▍ | Scoring GeneralizingEstimator : 1034/1225 [00:02<00:00, 367.31it/s]
85%|████████▌ | Scoring GeneralizingEstimator : 1047/1225 [00:02<00:00, 368.00it/s]
86%|████████▋ | Scoring GeneralizingEstimator : 1058/1225 [00:03<00:00, 365.91it/s]
87%|████████▋ | Scoring GeneralizingEstimator : 1069/1225 [00:03<00:00, 363.94it/s]
88%|████████▊ | Scoring GeneralizingEstimator : 1083/1225 [00:03<00:00, 365.01it/s]
89%|████████▉ | Scoring GeneralizingEstimator : 1096/1225 [00:03<00:00, 366.01it/s]
90%|█████████ | Scoring GeneralizingEstimator : 1108/1225 [00:03<00:00, 365.44it/s]
91%|█████████▏| Scoring GeneralizingEstimator : 1119/1225 [00:03<00:00, 363.48it/s]
92%|█████████▏| Scoring GeneralizingEstimator : 1131/1225 [00:03<00:00, 363.12it/s]
93%|█████████▎| Scoring GeneralizingEstimator : 1144/1225 [00:03<00:00, 364.20it/s]
94%|█████████▍| Scoring GeneralizingEstimator : 1156/1225 [00:03<00:00, 363.82it/s]
95%|█████████▌| Scoring GeneralizingEstimator : 1168/1225 [00:03<00:00, 363.46it/s]
96%|█████████▋| Scoring GeneralizingEstimator : 1180/1225 [00:03<00:00, 363.11it/s]
97%|█████████▋| Scoring GeneralizingEstimator : 1194/1225 [00:03<00:00, 365.66it/s]
99%|█████████▊| Scoring GeneralizingEstimator : 1207/1225 [00:03<00:00, 366.69it/s]
100%|█████████▉| Scoring GeneralizingEstimator : 1221/1225 [00:03<00:00, 367.33it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00, 354.27it/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.917 seconds)