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
import matplotlib.pyplot as plt
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
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., 30., 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)]: Using backend SequentialBackend with 1 concurrent workers.
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 4 out of 4 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 366 out of 366 | elapsed: 0.6s finished
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(),
y=epochs['Left'].events[:, 2] > 2)
0%| | Fitting GeneralizingEstimator : 0/35 [00:00<?, ?it/s]
6%|5 | Fitting GeneralizingEstimator : 2/35 [00:00<00:00, 57.50it/s]
11%|#1 | Fitting GeneralizingEstimator : 4/35 [00:00<00:00, 58.29it/s]
17%|#7 | Fitting GeneralizingEstimator : 6/35 [00:00<00:00, 58.59it/s]
23%|##2 | Fitting GeneralizingEstimator : 8/35 [00:00<00:00, 58.75it/s]
34%|###4 | Fitting GeneralizingEstimator : 12/35 [00:00<00:00, 71.83it/s]
43%|####2 | Fitting GeneralizingEstimator : 15/35 [00:00<00:00, 75.03it/s]
49%|####8 | Fitting GeneralizingEstimator : 17/35 [00:00<00:00, 72.42it/s]
57%|#####7 | Fitting GeneralizingEstimator : 20/35 [00:00<00:00, 74.84it/s]
66%|######5 | Fitting GeneralizingEstimator : 23/35 [00:00<00:00, 76.66it/s]
71%|#######1 | Fitting GeneralizingEstimator : 25/35 [00:00<00:00, 74.48it/s]
80%|######## | Fitting GeneralizingEstimator : 28/35 [00:00<00:00, 76.09it/s]
86%|########5 | Fitting GeneralizingEstimator : 30/35 [00:00<00:00, 74.24it/s]
94%|#########4| Fitting GeneralizingEstimator : 33/35 [00:00<00:00, 75.73it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 75.82it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 74.89it/s]
Score on the epochs where the stimulus was presented to the right.
scores = time_gen.score(X=epochs['Right'].get_data(),
y=epochs['Right'].events[:, 2] > 2)
0%| | Scoring GeneralizingEstimator : 0/1225 [00:00<?, ?it/s]
0%| | Scoring GeneralizingEstimator : 6/1225 [00:00<00:07, 173.39it/s]
1%|1 | Scoring GeneralizingEstimator : 14/1225 [00:00<00:05, 204.20it/s]
2%|1 | Scoring GeneralizingEstimator : 23/1225 [00:00<00:05, 224.26it/s]
3%|2 | Scoring GeneralizingEstimator : 32/1225 [00:00<00:05, 235.25it/s]
3%|3 | Scoring GeneralizingEstimator : 41/1225 [00:00<00:04, 242.00it/s]
4%|4 | Scoring GeneralizingEstimator : 50/1225 [00:00<00:04, 246.08it/s]
5%|4 | Scoring GeneralizingEstimator : 59/1225 [00:00<00:04, 249.31it/s]
6%|5 | Scoring GeneralizingEstimator : 68/1225 [00:00<00:04, 251.39it/s]
6%|6 | Scoring GeneralizingEstimator : 76/1225 [00:00<00:04, 248.95it/s]
7%|6 | Scoring GeneralizingEstimator : 85/1225 [00:00<00:04, 250.98it/s]
8%|7 | Scoring GeneralizingEstimator : 93/1225 [00:00<00:04, 249.22it/s]
8%|8 | Scoring GeneralizingEstimator : 102/1225 [00:00<00:04, 250.44it/s]
9%|9 | Scoring GeneralizingEstimator : 111/1225 [00:00<00:04, 252.08it/s]
10%|9 | Scoring GeneralizingEstimator : 119/1225 [00:00<00:04, 250.42it/s]
10%|# | Scoring GeneralizingEstimator : 128/1225 [00:00<00:04, 251.53it/s]
11%|#1 | Scoring GeneralizingEstimator : 138/1225 [00:00<00:04, 255.45it/s]
12%|#2 | Scoring GeneralizingEstimator : 148/1225 [00:00<00:04, 258.82it/s]
13%|#2 | Scoring GeneralizingEstimator : 159/1225 [00:00<00:04, 263.94it/s]
14%|#3 | Scoring GeneralizingEstimator : 169/1225 [00:00<00:03, 266.39it/s]
15%|#4 | Scoring GeneralizingEstimator : 178/1225 [00:00<00:03, 266.29it/s]
15%|#5 | Scoring GeneralizingEstimator : 188/1225 [00:00<00:03, 268.45it/s]
16%|#6 | Scoring GeneralizingEstimator : 198/1225 [00:00<00:03, 270.40it/s]
17%|#7 | Scoring GeneralizingEstimator : 209/1225 [00:00<00:03, 274.07it/s]
18%|#8 | Scoring GeneralizingEstimator : 222/1225 [00:00<00:03, 281.69it/s]
19%|#9 | Scoring GeneralizingEstimator : 238/1225 [00:00<00:03, 294.71it/s]
21%|## | Scoring GeneralizingEstimator : 252/1225 [00:00<00:03, 302.70it/s]
22%|##1 | Scoring GeneralizingEstimator : 267/1225 [00:00<00:03, 312.06it/s]
23%|##3 | Scoring GeneralizingEstimator : 283/1225 [00:00<00:02, 322.53it/s]
24%|##4 | Scoring GeneralizingEstimator : 298/1225 [00:00<00:02, 330.10it/s]
26%|##5 | Scoring GeneralizingEstimator : 315/1225 [00:01<00:02, 341.02it/s]
27%|##6 | Scoring GeneralizingEstimator : 330/1225 [00:01<00:02, 347.32it/s]
28%|##8 | Scoring GeneralizingEstimator : 348/1225 [00:01<00:02, 358.78it/s]
30%|##9 | Scoring GeneralizingEstimator : 365/1225 [00:01<00:02, 367.51it/s]
31%|###1 | Scoring GeneralizingEstimator : 382/1225 [00:01<00:02, 375.59it/s]
33%|###2 | Scoring GeneralizingEstimator : 400/1225 [00:01<00:02, 384.55it/s]
34%|###4 | Scoring GeneralizingEstimator : 417/1225 [00:01<00:02, 391.36it/s]
36%|###5 | Scoring GeneralizingEstimator : 435/1225 [00:01<00:01, 399.56it/s]
37%|###7 | Scoring GeneralizingEstimator : 454/1225 [00:01<00:01, 408.76it/s]
39%|###8 | Scoring GeneralizingEstimator : 472/1225 [00:01<00:01, 415.85it/s]
40%|#### | Scoring GeneralizingEstimator : 490/1225 [00:01<00:01, 422.48it/s]
42%|####1 | Scoring GeneralizingEstimator : 509/1225 [00:01<00:01, 430.43it/s]
43%|####3 | Scoring GeneralizingEstimator : 527/1225 [00:01<00:01, 436.20it/s]
45%|####4 | Scoring GeneralizingEstimator : 546/1225 [00:01<00:01, 443.28it/s]
46%|####6 | Scoring GeneralizingEstimator : 565/1225 [00:01<00:01, 449.86it/s]
48%|####7 | Scoring GeneralizingEstimator : 583/1225 [00:01<00:01, 454.44it/s]
49%|####9 | Scoring GeneralizingEstimator : 601/1225 [00:01<00:01, 458.74it/s]
51%|##### | Scoring GeneralizingEstimator : 620/1225 [00:01<00:01, 464.41it/s]
52%|#####2 | Scoring GeneralizingEstimator : 638/1225 [00:01<00:01, 468.13it/s]
53%|#####3 | Scoring GeneralizingEstimator : 655/1225 [00:01<00:01, 469.91it/s]
55%|#####4 | Scoring GeneralizingEstimator : 672/1225 [00:01<00:01, 471.68it/s]
56%|#####6 | Scoring GeneralizingEstimator : 690/1225 [00:01<00:01, 474.91it/s]
58%|#####7 | Scoring GeneralizingEstimator : 707/1225 [00:01<00:01, 476.31it/s]
59%|#####8 | Scoring GeneralizingEstimator : 721/1225 [00:01<00:01, 473.06it/s]
60%|###### | Scoring GeneralizingEstimator : 739/1225 [00:01<00:01, 475.83it/s]
62%|######1 | Scoring GeneralizingEstimator : 754/1225 [00:01<00:00, 473.96it/s]
63%|######2 | Scoring GeneralizingEstimator : 768/1225 [00:01<00:00, 470.79it/s]
64%|######3 | Scoring GeneralizingEstimator : 781/1225 [00:01<00:00, 466.32it/s]
65%|######4 | Scoring GeneralizingEstimator : 796/1225 [00:01<00:00, 465.07it/s]
66%|######6 | Scoring GeneralizingEstimator : 814/1225 [00:02<00:00, 468.57it/s]
68%|######8 | Scoring GeneralizingEstimator : 833/1225 [00:02<00:00, 473.51it/s]
70%|######9 | Scoring GeneralizingEstimator : 852/1225 [00:02<00:00, 478.18it/s]
71%|#######1 | Scoring GeneralizingEstimator : 870/1225 [00:02<00:00, 481.01it/s]
73%|#######2 | Scoring GeneralizingEstimator : 889/1225 [00:02<00:00, 484.97it/s]
74%|#######4 | Scoring GeneralizingEstimator : 907/1225 [00:02<00:00, 487.30it/s]
76%|#######5 | Scoring GeneralizingEstimator : 926/1225 [00:02<00:00, 491.12it/s]
77%|#######7 | Scoring GeneralizingEstimator : 944/1225 [00:02<00:00, 493.21it/s]
79%|#######8 | Scoring GeneralizingEstimator : 962/1225 [00:02<00:00, 494.60it/s]
80%|######## | Scoring GeneralizingEstimator : 980/1225 [00:02<00:00, 496.42it/s]
82%|########1 | Scoring GeneralizingEstimator : 999/1225 [00:02<00:00, 499.72it/s]
83%|########3 | Scoring GeneralizingEstimator : 1017/1225 [00:02<00:00, 500.98it/s]
84%|########4 | Scoring GeneralizingEstimator : 1035/1225 [00:02<00:00, 502.21it/s]
86%|########5 | Scoring GeneralizingEstimator : 1053/1225 [00:02<00:00, 503.32it/s]
87%|########7 | Scoring GeneralizingEstimator : 1071/1225 [00:02<00:00, 504.70it/s]
89%|########8 | Scoring GeneralizingEstimator : 1089/1225 [00:02<00:00, 506.19it/s]
90%|######### | Scoring GeneralizingEstimator : 1107/1225 [00:02<00:00, 507.44it/s]
92%|#########1| Scoring GeneralizingEstimator : 1124/1225 [00:02<00:00, 507.14it/s]
93%|#########3| Scoring GeneralizingEstimator : 1142/1225 [00:02<00:00, 508.43it/s]
95%|#########4| Scoring GeneralizingEstimator : 1158/1225 [00:02<00:00, 506.57it/s]
96%|#########5| Scoring GeneralizingEstimator : 1175/1225 [00:02<00:00, 506.33it/s]
97%|#########7| Scoring GeneralizingEstimator : 1194/1225 [00:02<00:00, 509.22it/s]
99%|#########9| Scoring GeneralizingEstimator : 1213/1225 [00:02<00:00, 511.61it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00, 513.80it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00, 441.73it/s]
Plot
fig, ax = plt.subplots(constrained_layout=True)
im = ax.matshow(scores, vmin=0, vmax=1., cmap='RdBu_r', origin='lower',
extent=epochs.times[[0, -1, 0, -1]])
ax.axhline(0., color='k')
ax.axvline(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.009 seconds)
Estimated memory usage: 128 MB