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.1s
[Parallel(n_jobs=1)]: Done 161 tasks | elapsed: 0.2s
[Parallel(n_jobs=1)]: Done 287 tasks | elapsed: 0.4s
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, 57.10it/s]
11%|█▏ | Fitting GeneralizingEstimator : 4/35 [00:00<00:00, 58.08it/s]
17%|█▋ | Fitting GeneralizingEstimator : 6/35 [00:00<00:00, 58.39it/s]
23%|██▎ | Fitting GeneralizingEstimator : 8/35 [00:00<00:00, 58.50it/s]
34%|███▍ | Fitting GeneralizingEstimator : 12/35 [00:00<00:00, 71.54it/s]
43%|████▎ | Fitting GeneralizingEstimator : 15/35 [00:00<00:00, 74.73it/s]
51%|█████▏ | Fitting GeneralizingEstimator : 18/35 [00:00<00:00, 76.70it/s]
57%|█████▋ | Fitting GeneralizingEstimator : 20/35 [00:00<00:00, 74.06it/s]
66%|██████▌ | Fitting GeneralizingEstimator : 23/35 [00:00<00:00, 75.99it/s]
74%|███████▍ | Fitting GeneralizingEstimator : 26/35 [00:00<00:00, 77.51it/s]
80%|████████ | Fitting GeneralizingEstimator : 28/35 [00:00<00:00, 75.41it/s]
89%|████████▊ | Fitting GeneralizingEstimator : 31/35 [00:00<00:00, 76.67it/s]
94%|█████████▍| Fitting GeneralizingEstimator : 33/35 [00:00<00:00, 74.84it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 76.82it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 75.77it/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, 321.99it/s]
2%|▏ | Scoring GeneralizingEstimator : 25/1225 [00:00<00:03, 367.45it/s]
3%|▎ | Scoring GeneralizingEstimator : 39/1225 [00:00<00:03, 383.63it/s]
4%|▍ | Scoring GeneralizingEstimator : 52/1225 [00:00<00:03, 383.62it/s]
5%|▍ | Scoring GeneralizingEstimator : 61/1225 [00:00<00:03, 357.69it/s]
6%|▌ | Scoring GeneralizingEstimator : 70/1225 [00:00<00:03, 340.59it/s]
7%|▋ | Scoring GeneralizingEstimator : 80/1225 [00:00<00:03, 333.24it/s]
8%|▊ | Scoring GeneralizingEstimator : 93/1225 [00:00<00:03, 340.80it/s]
9%|▊ | Scoring GeneralizingEstimator : 106/1225 [00:00<00:03, 346.63it/s]
10%|▉ | Scoring GeneralizingEstimator : 121/1225 [00:00<00:03, 358.55it/s]
11%|█ | Scoring GeneralizingEstimator : 135/1225 [00:00<00:02, 364.91it/s]
12%|█▏ | Scoring GeneralizingEstimator : 149/1225 [00:00<00:02, 370.19it/s]
13%|█▎ | Scoring GeneralizingEstimator : 164/1225 [00:00<00:02, 377.57it/s]
15%|█▍ | Scoring GeneralizingEstimator : 178/1225 [00:00<00:02, 380.98it/s]
16%|█▌ | Scoring GeneralizingEstimator : 193/1225 [00:00<00:02, 386.72it/s]
17%|█▋ | Scoring GeneralizingEstimator : 208/1225 [00:00<00:02, 391.23it/s]
18%|█▊ | Scoring GeneralizingEstimator : 221/1225 [00:00<00:02, 390.31it/s]
19%|█▉ | Scoring GeneralizingEstimator : 236/1225 [00:00<00:02, 394.75it/s]
20%|██ | Scoring GeneralizingEstimator : 251/1225 [00:00<00:02, 398.71it/s]
22%|██▏ | Scoring GeneralizingEstimator : 264/1225 [00:00<00:02, 397.32it/s]
23%|██▎ | Scoring GeneralizingEstimator : 279/1225 [00:00<00:02, 400.69it/s]
24%|██▍ | Scoring GeneralizingEstimator : 294/1225 [00:00<00:02, 403.31it/s]
25%|██▌ | Scoring GeneralizingEstimator : 308/1225 [00:00<00:02, 404.10it/s]
26%|██▋ | Scoring GeneralizingEstimator : 323/1225 [00:00<00:02, 406.96it/s]
28%|██▊ | Scoring GeneralizingEstimator : 337/1225 [00:00<00:02, 407.23it/s]
29%|██▊ | Scoring GeneralizingEstimator : 351/1225 [00:00<00:02, 407.60it/s]
30%|██▉ | Scoring GeneralizingEstimator : 366/1225 [00:00<00:02, 409.83it/s]
31%|███ | Scoring GeneralizingEstimator : 381/1225 [00:00<00:02, 411.72it/s]
32%|███▏ | Scoring GeneralizingEstimator : 395/1225 [00:00<00:02, 411.77it/s]
33%|███▎ | Scoring GeneralizingEstimator : 410/1225 [00:01<00:01, 413.71it/s]
35%|███▍ | Scoring GeneralizingEstimator : 424/1225 [00:01<00:01, 413.63it/s]
36%|███▌ | Scoring GeneralizingEstimator : 438/1225 [00:01<00:01, 413.48it/s]
37%|███▋ | Scoring GeneralizingEstimator : 452/1225 [00:01<00:01, 413.45it/s]
38%|███▊ | Scoring GeneralizingEstimator : 466/1225 [00:01<00:01, 413.53it/s]
39%|███▉ | Scoring GeneralizingEstimator : 480/1225 [00:01<00:01, 413.62it/s]
40%|████ | Scoring GeneralizingEstimator : 493/1225 [00:01<00:01, 411.80it/s]
41%|████▏ | Scoring GeneralizingEstimator : 508/1225 [00:01<00:01, 413.66it/s]
43%|████▎ | Scoring GeneralizingEstimator : 523/1225 [00:01<00:01, 415.41it/s]
44%|████▍ | Scoring GeneralizingEstimator : 537/1225 [00:01<00:01, 415.36it/s]
45%|████▍ | Scoring GeneralizingEstimator : 551/1225 [00:01<00:01, 415.26it/s]
46%|████▌ | Scoring GeneralizingEstimator : 566/1225 [00:01<00:01, 416.75it/s]
47%|████▋ | Scoring GeneralizingEstimator : 580/1225 [00:01<00:01, 416.53it/s]
49%|████▊ | Scoring GeneralizingEstimator : 595/1225 [00:01<00:01, 417.89it/s]
50%|████▉ | Scoring GeneralizingEstimator : 608/1225 [00:01<00:01, 416.03it/s]
51%|█████ | Scoring GeneralizingEstimator : 622/1225 [00:01<00:01, 415.53it/s]
52%|█████▏ | Scoring GeneralizingEstimator : 637/1225 [00:01<00:01, 417.06it/s]
53%|█████▎ | Scoring GeneralizingEstimator : 652/1225 [00:01<00:01, 418.38it/s]
54%|█████▍ | Scoring GeneralizingEstimator : 667/1225 [00:01<00:01, 419.58it/s]
56%|█████▌ | Scoring GeneralizingEstimator : 682/1225 [00:01<00:01, 420.56it/s]
57%|█████▋ | Scoring GeneralizingEstimator : 696/1225 [00:01<00:01, 419.62it/s]
58%|█████▊ | Scoring GeneralizingEstimator : 710/1225 [00:01<00:01, 419.28it/s]
59%|█████▉ | Scoring GeneralizingEstimator : 722/1225 [00:01<00:01, 415.71it/s]
60%|██████ | Scoring GeneralizingEstimator : 735/1225 [00:01<00:01, 413.77it/s]
61%|██████ | Scoring GeneralizingEstimator : 748/1225 [00:01<00:01, 412.26it/s]
62%|██████▏ | Scoring GeneralizingEstimator : 761/1225 [00:01<00:01, 410.78it/s]
63%|██████▎ | Scoring GeneralizingEstimator : 774/1225 [00:01<00:01, 409.13it/s]
64%|██████▍ | Scoring GeneralizingEstimator : 789/1225 [00:01<00:01, 411.00it/s]
65%|██████▌ | Scoring GeneralizingEstimator : 802/1225 [00:01<00:01, 409.29it/s]
67%|██████▋ | Scoring GeneralizingEstimator : 816/1225 [00:02<00:00, 409.52it/s]
68%|██████▊ | Scoring GeneralizingEstimator : 829/1225 [00:02<00:00, 408.09it/s]
69%|██████▊ | Scoring GeneralizingEstimator : 841/1225 [00:02<00:00, 405.28it/s]
70%|██████▉ | Scoring GeneralizingEstimator : 853/1225 [00:02<00:00, 402.71it/s]
71%|███████ | Scoring GeneralizingEstimator : 865/1225 [00:02<00:00, 400.03it/s]
72%|███████▏ | Scoring GeneralizingEstimator : 879/1225 [00:02<00:00, 400.56it/s]
73%|███████▎ | Scoring GeneralizingEstimator : 892/1225 [00:02<00:00, 399.64it/s]
74%|███████▍ | Scoring GeneralizingEstimator : 906/1225 [00:02<00:00, 400.13it/s]
75%|███████▌ | Scoring GeneralizingEstimator : 920/1225 [00:02<00:00, 400.89it/s]
76%|███████▌ | Scoring GeneralizingEstimator : 934/1225 [00:02<00:00, 401.49it/s]
77%|███████▋ | Scoring GeneralizingEstimator : 948/1225 [00:02<00:00, 402.18it/s]
79%|███████▊ | Scoring GeneralizingEstimator : 963/1225 [00:02<00:00, 404.31it/s]
80%|███████▉ | Scoring GeneralizingEstimator : 978/1225 [00:02<00:00, 406.18it/s]
81%|████████ | Scoring GeneralizingEstimator : 992/1225 [00:02<00:00, 406.31it/s]
82%|████████▏ | Scoring GeneralizingEstimator : 1006/1225 [00:02<00:00, 406.69it/s]
83%|████████▎ | Scoring GeneralizingEstimator : 1019/1225 [00:02<00:00, 405.26it/s]
84%|████████▍ | Scoring GeneralizingEstimator : 1033/1225 [00:02<00:00, 405.76it/s]
85%|████████▌ | Scoring GeneralizingEstimator : 1046/1225 [00:02<00:00, 404.70it/s]
86%|████████▋ | Scoring GeneralizingEstimator : 1058/1225 [00:02<00:00, 401.89it/s]
88%|████████▊ | Scoring GeneralizingEstimator : 1073/1225 [00:02<00:00, 403.99it/s]
89%|████████▊ | Scoring GeneralizingEstimator : 1086/1225 [00:02<00:00, 402.81it/s]
90%|████████▉ | Scoring GeneralizingEstimator : 1100/1225 [00:02<00:00, 403.37it/s]
91%|█████████ | Scoring GeneralizingEstimator : 1112/1225 [00:02<00:00, 400.96it/s]
92%|█████████▏| Scoring GeneralizingEstimator : 1124/1225 [00:02<00:00, 398.61it/s]
93%|█████████▎| Scoring GeneralizingEstimator : 1136/1225 [00:02<00:00, 396.39it/s]
94%|█████████▎| Scoring GeneralizingEstimator : 1146/1225 [00:02<00:00, 391.13it/s]
94%|█████████▍| Scoring GeneralizingEstimator : 1157/1225 [00:02<00:00, 387.68it/s]
95%|█████████▌| Scoring GeneralizingEstimator : 1168/1225 [00:02<00:00, 384.44it/s]
96%|█████████▌| Scoring GeneralizingEstimator : 1179/1225 [00:02<00:00, 381.41it/s]
97%|█████████▋| Scoring GeneralizingEstimator : 1189/1225 [00:02<00:00, 376.88it/s]
98%|█████████▊| Scoring GeneralizingEstimator : 1202/1225 [00:03<00:00, 377.24it/s]
99%|█████████▉| Scoring GeneralizingEstimator : 1213/1225 [00:03<00:00, 374.46it/s]
100%|█████████▉| Scoring GeneralizingEstimator : 1224/1225 [00:03<00:00, 371.87it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00, 395.76it/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 8.135 seconds)
Estimated memory usage: 32 MB