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.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]
6%|▌ | Fitting GeneralizingEstimator : 2/35 [00:00<00:00, 55.59it/s]
11%|█▏ | Fitting GeneralizingEstimator : 4/35 [00:00<00:00, 57.19it/s]
17%|█▋ | Fitting GeneralizingEstimator : 6/35 [00:00<00:00, 57.94it/s]
23%|██▎ | Fitting GeneralizingEstimator : 8/35 [00:00<00:00, 58.33it/s]
31%|███▏ | Fitting GeneralizingEstimator : 11/35 [00:00<00:00, 65.04it/s]
40%|████ | Fitting GeneralizingEstimator : 14/35 [00:00<00:00, 69.54it/s]
49%|████▊ | Fitting GeneralizingEstimator : 17/35 [00:00<00:00, 72.77it/s]
57%|█████▋ | Fitting GeneralizingEstimator : 20/35 [00:00<00:00, 75.19it/s]
66%|██████▌ | Fitting GeneralizingEstimator : 23/35 [00:00<00:00, 76.62it/s]
74%|███████▍ | Fitting GeneralizingEstimator : 26/35 [00:00<00:00, 78.17it/s]
80%|████████ | Fitting GeneralizingEstimator : 28/35 [00:00<00:00, 76.02it/s]
89%|████████▊ | Fitting GeneralizingEstimator : 31/35 [00:00<00:00, 77.45it/s]
97%|█████████▋| Fitting GeneralizingEstimator : 34/35 [00:00<00:00, 78.64it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 77.68it/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 : 14/1225 [00:00<00:03, 398.93it/s]
2%|▏ | Scoring GeneralizingEstimator : 27/1225 [00:00<00:03, 392.72it/s]
3%|▎ | Scoring GeneralizingEstimator : 41/1225 [00:00<00:02, 400.67it/s]
5%|▍ | Scoring GeneralizingEstimator : 56/1225 [00:00<00:02, 412.71it/s]
6%|▌ | Scoring GeneralizingEstimator : 71/1225 [00:00<00:02, 419.96it/s]
7%|▋ | Scoring GeneralizingEstimator : 87/1225 [00:00<00:02, 424.20it/s]
8%|▊ | Scoring GeneralizingEstimator : 102/1225 [00:00<00:02, 427.49it/s]
10%|▉ | Scoring GeneralizingEstimator : 117/1225 [00:00<00:02, 430.04it/s]
11%|█ | Scoring GeneralizingEstimator : 132/1225 [00:00<00:02, 432.02it/s]
12%|█▏ | Scoring GeneralizingEstimator : 147/1225 [00:00<00:02, 433.73it/s]
13%|█▎ | Scoring GeneralizingEstimator : 163/1225 [00:00<00:02, 438.50it/s]
15%|█▍ | Scoring GeneralizingEstimator : 178/1225 [00:00<00:02, 439.24it/s]
16%|█▌ | Scoring GeneralizingEstimator : 193/1225 [00:00<00:02, 439.72it/s]
17%|█▋ | Scoring GeneralizingEstimator : 209/1225 [00:00<00:02, 441.41it/s]
18%|█▊ | Scoring GeneralizingEstimator : 224/1225 [00:00<00:02, 441.60it/s]
20%|█▉ | Scoring GeneralizingEstimator : 239/1225 [00:00<00:02, 441.96it/s]
21%|██ | Scoring GeneralizingEstimator : 253/1225 [00:00<00:02, 439.70it/s]
22%|██▏ | Scoring GeneralizingEstimator : 268/1225 [00:00<00:02, 440.12it/s]
23%|██▎ | Scoring GeneralizingEstimator : 284/1225 [00:00<00:02, 440.39it/s]
24%|██▍ | Scoring GeneralizingEstimator : 299/1225 [00:00<00:02, 440.75it/s]
26%|██▌ | Scoring GeneralizingEstimator : 314/1225 [00:00<00:02, 441.13it/s]
27%|██▋ | Scoring GeneralizingEstimator : 329/1225 [00:00<00:02, 441.46it/s]
28%|██▊ | Scoring GeneralizingEstimator : 344/1225 [00:00<00:01, 441.76it/s]
29%|██▉ | Scoring GeneralizingEstimator : 359/1225 [00:00<00:01, 442.02it/s]
31%|███ | Scoring GeneralizingEstimator : 374/1225 [00:00<00:01, 442.26it/s]
32%|███▏ | Scoring GeneralizingEstimator : 389/1225 [00:00<00:01, 442.50it/s]
33%|███▎ | Scoring GeneralizingEstimator : 404/1225 [00:00<00:01, 442.67it/s]
34%|███▍ | Scoring GeneralizingEstimator : 419/1225 [00:00<00:01, 442.81it/s]
35%|███▌ | Scoring GeneralizingEstimator : 434/1225 [00:00<00:01, 442.96it/s]
37%|███▋ | Scoring GeneralizingEstimator : 450/1225 [00:01<00:01, 443.68it/s]
38%|███▊ | Scoring GeneralizingEstimator : 466/1225 [00:01<00:01, 443.65it/s]
39%|███▉ | Scoring GeneralizingEstimator : 481/1225 [00:01<00:01, 443.70it/s]
40%|████ | Scoring GeneralizingEstimator : 496/1225 [00:01<00:01, 443.74it/s]
42%|████▏ | Scoring GeneralizingEstimator : 511/1225 [00:01<00:01, 443.80it/s]
43%|████▎ | Scoring GeneralizingEstimator : 526/1225 [00:01<00:01, 443.93it/s]
44%|████▍ | Scoring GeneralizingEstimator : 541/1225 [00:01<00:01, 444.03it/s]
45%|████▌ | Scoring GeneralizingEstimator : 556/1225 [00:01<00:01, 444.14it/s]
47%|████▋ | Scoring GeneralizingEstimator : 572/1225 [00:01<00:01, 445.88it/s]
48%|████▊ | Scoring GeneralizingEstimator : 587/1225 [00:01<00:01, 445.89it/s]
49%|████▉ | Scoring GeneralizingEstimator : 602/1225 [00:01<00:01, 445.90it/s]
50%|█████ | Scoring GeneralizingEstimator : 618/1225 [00:01<00:01, 446.03it/s]
52%|█████▏ | Scoring GeneralizingEstimator : 633/1225 [00:01<00:01, 446.01it/s]
53%|█████▎ | Scoring GeneralizingEstimator : 648/1225 [00:01<00:01, 445.97it/s]
54%|█████▍ | Scoring GeneralizingEstimator : 663/1225 [00:01<00:01, 445.92it/s]
55%|█████▌ | Scoring GeneralizingEstimator : 678/1225 [00:01<00:01, 445.88it/s]
57%|█████▋ | Scoring GeneralizingEstimator : 693/1225 [00:01<00:01, 445.87it/s]
58%|█████▊ | Scoring GeneralizingEstimator : 707/1225 [00:01<00:01, 444.23it/s]
59%|█████▉ | Scoring GeneralizingEstimator : 723/1225 [00:01<00:01, 445.93it/s]
60%|██████ | Scoring GeneralizingEstimator : 738/1225 [00:01<00:01, 444.07it/s]
62%|██████▏ | Scoring GeneralizingEstimator : 754/1225 [00:01<00:01, 444.09it/s]
63%|██████▎ | Scoring GeneralizingEstimator : 768/1225 [00:01<00:01, 442.57it/s]
64%|██████▍ | Scoring GeneralizingEstimator : 782/1225 [00:01<00:01, 441.17it/s]
65%|██████▍ | Scoring GeneralizingEstimator : 796/1225 [00:01<00:00, 439.71it/s]
66%|██████▌ | Scoring GeneralizingEstimator : 810/1225 [00:01<00:00, 438.48it/s]
67%|██████▋ | Scoring GeneralizingEstimator : 824/1225 [00:01<00:00, 437.30it/s]
68%|██████▊ | Scoring GeneralizingEstimator : 836/1225 [00:01<00:00, 432.73it/s]
69%|██████▉ | Scoring GeneralizingEstimator : 849/1225 [00:01<00:00, 430.18it/s]
70%|███████ | Scoring GeneralizingEstimator : 863/1225 [00:01<00:00, 429.42it/s]
72%|███████▏ | Scoring GeneralizingEstimator : 878/1225 [00:02<00:00, 429.40it/s]
73%|███████▎ | Scoring GeneralizingEstimator : 891/1225 [00:02<00:00, 427.11it/s]
74%|███████▎ | Scoring GeneralizingEstimator : 903/1225 [00:02<00:00, 423.42it/s]
75%|███████▍ | Scoring GeneralizingEstimator : 916/1225 [00:02<00:00, 421.44it/s]
76%|███████▌ | Scoring GeneralizingEstimator : 931/1225 [00:02<00:00, 422.63it/s]
77%|███████▋ | Scoring GeneralizingEstimator : 947/1225 [00:02<00:00, 424.52it/s]
78%|███████▊ | Scoring GeneralizingEstimator : 960/1225 [00:02<00:00, 422.47it/s]
80%|███████▉ | Scoring GeneralizingEstimator : 974/1225 [00:02<00:00, 422.11it/s]
81%|████████ | Scoring GeneralizingEstimator : 988/1225 [00:02<00:00, 421.78it/s]
82%|████████▏ | Scoring GeneralizingEstimator : 1001/1225 [00:02<00:00, 419.94it/s]
83%|████████▎ | Scoring GeneralizingEstimator : 1014/1225 [00:02<00:00, 416.60it/s]
84%|████████▍ | Scoring GeneralizingEstimator : 1028/1225 [00:02<00:00, 416.56it/s]
85%|████████▌ | Scoring GeneralizingEstimator : 1042/1225 [00:02<00:00, 416.38it/s]
86%|████████▌ | Scoring GeneralizingEstimator : 1056/1225 [00:02<00:00, 416.27it/s]
87%|████████▋ | Scoring GeneralizingEstimator : 1068/1225 [00:02<00:00, 413.21it/s]
88%|████████▊ | Scoring GeneralizingEstimator : 1081/1225 [00:02<00:00, 411.83it/s]
89%|████████▉ | Scoring GeneralizingEstimator : 1095/1225 [00:02<00:00, 412.02it/s]
91%|█████████ | Scoring GeneralizingEstimator : 1110/1225 [00:02<00:00, 413.74it/s]
92%|█████████▏| Scoring GeneralizingEstimator : 1124/1225 [00:02<00:00, 413.80it/s]
93%|█████████▎| Scoring GeneralizingEstimator : 1139/1225 [00:02<00:00, 414.52it/s]
94%|█████████▍| Scoring GeneralizingEstimator : 1153/1225 [00:02<00:00, 414.58it/s]
95%|█████████▌| Scoring GeneralizingEstimator : 1166/1225 [00:02<00:00, 413.08it/s]
96%|█████████▌| Scoring GeneralizingEstimator : 1178/1225 [00:02<00:00, 410.18it/s]
97%|█████████▋| Scoring GeneralizingEstimator : 1191/1225 [00:02<00:00, 408.91it/s]
98%|█████████▊| Scoring GeneralizingEstimator : 1206/1225 [00:02<00:00, 409.31it/s]
100%|█████████▉| Scoring GeneralizingEstimator : 1220/1225 [00:02<00:00, 409.64it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00, 426.74it/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()
![Generalization across time and condition](../../_images/sphx_glr_decoding_time_generalization_conditions_001.png)
References#
Total running time of the script: (0 minutes 5.328 seconds)