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 sec)
[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]
3%|2 | Fitting GeneralizingEstimator : 1/35 [00:00<00:01, 29.01it/s]
11%|#1 | Fitting GeneralizingEstimator : 4/35 [00:00<00:00, 59.19it/s]
17%|#7 | Fitting GeneralizingEstimator : 6/35 [00:00<00:00, 59.22it/s]
23%|##2 | Fitting GeneralizingEstimator : 8/35 [00:00<00:00, 59.20it/s]
31%|###1 | Fitting GeneralizingEstimator : 11/35 [00:00<00:00, 65.70it/s]
40%|#### | Fitting GeneralizingEstimator : 14/35 [00:00<00:00, 70.04it/s]
49%|####8 | Fitting GeneralizingEstimator : 17/35 [00:00<00:00, 73.15it/s]
57%|#####7 | Fitting GeneralizingEstimator : 20/35 [00:00<00:00, 75.49it/s]
66%|######5 | Fitting GeneralizingEstimator : 23/35 [00:00<00:00, 77.30it/s]
71%|#######1 | Fitting GeneralizingEstimator : 25/35 [00:00<00:00, 75.04it/s]
80%|######## | Fitting GeneralizingEstimator : 28/35 [00:00<00:00, 76.64it/s]
86%|########5 | Fitting GeneralizingEstimator : 30/35 [00:00<00:00, 74.74it/s]
94%|#########4| Fitting GeneralizingEstimator : 33/35 [00:00<00:00, 76.17it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 76.74it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00, 75.39it/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]
1%|1 | Scoring GeneralizingEstimator : 13/1225 [00:00<00:03, 379.13it/s]
3%|2 | Scoring GeneralizingEstimator : 32/1225 [00:00<00:02, 470.70it/s]
4%|4 | Scoring GeneralizingEstimator : 51/1225 [00:00<00:02, 502.56it/s]
5%|5 | Scoring GeneralizingEstimator : 66/1225 [00:00<00:02, 486.29it/s]
7%|6 | Scoring GeneralizingEstimator : 80/1225 [00:00<00:02, 470.08it/s]
8%|8 | Scoring GeneralizingEstimator : 98/1225 [00:00<00:02, 481.43it/s]
10%|9 | Scoring GeneralizingEstimator : 117/1225 [00:00<00:02, 494.66it/s]
11%|#1 | Scoring GeneralizingEstimator : 135/1225 [00:00<00:02, 498.94it/s]
13%|#2 | Scoring GeneralizingEstimator : 154/1225 [00:00<00:02, 507.37it/s]
14%|#4 | Scoring GeneralizingEstimator : 173/1225 [00:00<00:02, 513.93it/s]
16%|#5 | Scoring GeneralizingEstimator : 192/1225 [00:00<00:01, 519.31it/s]
17%|#7 | Scoring GeneralizingEstimator : 211/1225 [00:00<00:01, 523.96it/s]
19%|#8 | Scoring GeneralizingEstimator : 231/1225 [00:00<00:01, 530.27it/s]
20%|## | Scoring GeneralizingEstimator : 249/1225 [00:00<00:01, 530.20it/s]
22%|##1 | Scoring GeneralizingEstimator : 268/1225 [00:00<00:01, 533.08it/s]
24%|##3 | Scoring GeneralizingEstimator : 288/1225 [00:00<00:01, 538.34it/s]
25%|##5 | Scoring GeneralizingEstimator : 307/1225 [00:00<00:01, 539.68it/s]
26%|##6 | Scoring GeneralizingEstimator : 323/1225 [00:00<00:01, 533.83it/s]
28%|##7 | Scoring GeneralizingEstimator : 339/1225 [00:00<00:01, 528.74it/s]
29%|##9 | Scoring GeneralizingEstimator : 358/1225 [00:00<00:01, 531.35it/s]
31%|### | Scoring GeneralizingEstimator : 378/1225 [00:00<00:01, 535.90it/s]
32%|###2 | Scoring GeneralizingEstimator : 397/1225 [00:00<00:01, 537.76it/s]
34%|###3 | Scoring GeneralizingEstimator : 412/1225 [00:00<00:01, 530.96it/s]
35%|###4 | Scoring GeneralizingEstimator : 425/1225 [00:00<00:01, 520.65it/s]
36%|###5 | Scoring GeneralizingEstimator : 438/1225 [00:00<00:01, 511.25it/s]
37%|###7 | Scoring GeneralizingEstimator : 456/1225 [00:00<00:01, 512.61it/s]
39%|###8 | Scoring GeneralizingEstimator : 475/1225 [00:00<00:01, 515.86it/s]
40%|#### | Scoring GeneralizingEstimator : 493/1225 [00:00<00:01, 516.99it/s]
42%|####1 | Scoring GeneralizingEstimator : 512/1225 [00:00<00:01, 519.85it/s]
43%|####3 | Scoring GeneralizingEstimator : 531/1225 [00:01<00:01, 522.15it/s]
45%|####4 | Scoring GeneralizingEstimator : 551/1225 [00:01<00:01, 526.39it/s]
47%|####6 | Scoring GeneralizingEstimator : 570/1225 [00:01<00:01, 528.59it/s]
48%|####7 | Scoring GeneralizingEstimator : 587/1225 [00:01<00:01, 527.10it/s]
49%|####9 | Scoring GeneralizingEstimator : 604/1225 [00:01<00:01, 525.69it/s]
51%|##### | Scoring GeneralizingEstimator : 622/1225 [00:01<00:01, 526.00it/s]
52%|#####2 | Scoring GeneralizingEstimator : 641/1225 [00:01<00:01, 527.89it/s]
54%|#####3 | Scoring GeneralizingEstimator : 660/1225 [00:01<00:01, 529.87it/s]
55%|#####5 | Scoring GeneralizingEstimator : 679/1225 [00:01<00:01, 531.40it/s]
57%|#####6 | Scoring GeneralizingEstimator : 698/1225 [00:01<00:00, 533.02it/s]
58%|#####8 | Scoring GeneralizingEstimator : 716/1225 [00:01<00:00, 532.91it/s]
60%|###### | Scoring GeneralizingEstimator : 735/1225 [00:01<00:00, 534.59it/s]
62%|######1 | Scoring GeneralizingEstimator : 754/1225 [00:01<00:00, 536.19it/s]
63%|######3 | Scoring GeneralizingEstimator : 773/1225 [00:01<00:00, 537.67it/s]
65%|######4 | Scoring GeneralizingEstimator : 791/1225 [00:01<00:00, 537.41it/s]
66%|######6 | Scoring GeneralizingEstimator : 809/1225 [00:01<00:00, 536.89it/s]
68%|######7 | Scoring GeneralizingEstimator : 827/1225 [00:01<00:00, 536.45it/s]
69%|######8 | Scoring GeneralizingEstimator : 845/1225 [00:01<00:00, 536.12it/s]
71%|####### | Scoring GeneralizingEstimator : 864/1225 [00:01<00:00, 537.57it/s]
72%|#######2 | Scoring GeneralizingEstimator : 882/1225 [00:01<00:00, 537.25it/s]
74%|#######3 | Scoring GeneralizingEstimator : 901/1225 [00:01<00:00, 538.12it/s]
75%|#######5 | Scoring GeneralizingEstimator : 921/1225 [00:01<00:00, 540.74it/s]
77%|#######6 | Scoring GeneralizingEstimator : 941/1225 [00:01<00:00, 543.37it/s]
78%|#######8 | Scoring GeneralizingEstimator : 960/1225 [00:01<00:00, 544.29it/s]
80%|#######9 | Scoring GeneralizingEstimator : 978/1225 [00:01<00:00, 543.36it/s]
81%|########1 | Scoring GeneralizingEstimator : 998/1225 [00:01<00:00, 545.79it/s]
83%|########3 | Scoring GeneralizingEstimator : 1018/1225 [00:01<00:00, 547.95it/s]
85%|########4 | Scoring GeneralizingEstimator : 1038/1225 [00:01<00:00, 549.85it/s]
86%|########6 | Scoring GeneralizingEstimator : 1057/1225 [00:01<00:00, 550.32it/s]
88%|########7 | Scoring GeneralizingEstimator : 1076/1225 [00:02<00:00, 550.83it/s]
89%|########9 | Scoring GeneralizingEstimator : 1094/1225 [00:02<00:00, 549.67it/s]
91%|######### | Scoring GeneralizingEstimator : 1113/1225 [00:02<00:00, 549.88it/s]
92%|#########2| Scoring GeneralizingEstimator : 1132/1225 [00:02<00:00, 550.09it/s]
94%|#########4| Scoring GeneralizingEstimator : 1152/1225 [00:02<00:00, 552.04it/s]
96%|#########5| Scoring GeneralizingEstimator : 1171/1225 [00:02<00:00, 552.32it/s]
97%|#########7| Scoring GeneralizingEstimator : 1191/1225 [00:02<00:00, 554.24it/s]
99%|#########8| Scoring GeneralizingEstimator : 1210/1225 [00:02<00:00, 554.27it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00, 555.92it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00, 540.28it/s]
Plot
fig, ax = plt.subplots(1)
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('Testing Time (s)')
ax.set_ylabel('Training Time (s)')
ax.set_title('Generalization across time and condition')
plt.colorbar(im, ax=ax)
plt.show()

References#
Total running time of the script: ( 0 minutes 7.364 seconds)
Estimated memory usage: 128 MB