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
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
events_fname = data_path + '/MEG/sample/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')

Out:

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)

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='lbfgs'))
time_gen = GeneralizingEstimator(clf, scoring='roc_auc', n_jobs=1,
                                 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)

Out:

  0%|          | Fitting GeneralizingEstimator : 0/35 [00:00<?,       ?it/s]
  3%|2         | Fitting GeneralizingEstimator : 1/35 [00:00<00:01,   28.77it/s]
  6%|5         | Fitting GeneralizingEstimator : 2/35 [00:00<00:01,   26.83it/s]
  9%|8         | Fitting GeneralizingEstimator : 3/35 [00:00<00:01,   27.39it/s]
 11%|#1        | Fitting GeneralizingEstimator : 4/35 [00:00<00:01,   18.21it/s]
 14%|#4        | Fitting GeneralizingEstimator : 5/35 [00:00<00:01,   17.84it/s]
 17%|#7        | Fitting GeneralizingEstimator : 6/35 [00:00<00:01,   19.27it/s]
 20%|##        | Fitting GeneralizingEstimator : 7/35 [00:00<00:01,   16.58it/s]
 23%|##2       | Fitting GeneralizingEstimator : 8/35 [00:00<00:01,   16.46it/s]
 26%|##5       | Fitting GeneralizingEstimator : 9/35 [00:00<00:01,   15.26it/s]
 29%|##8       | Fitting GeneralizingEstimator : 10/35 [00:00<00:01,   16.13it/s]
 31%|###1      | Fitting GeneralizingEstimator : 11/35 [00:00<00:01,   16.11it/s]
 37%|###7      | Fitting GeneralizingEstimator : 13/35 [00:00<00:01,   18.77it/s]
 43%|####2     | Fitting GeneralizingEstimator : 15/35 [00:00<00:01,   19.00it/s]
 46%|####5     | Fitting GeneralizingEstimator : 16/35 [00:00<00:01,   18.48it/s]
 51%|#####1    | Fitting GeneralizingEstimator : 18/35 [00:00<00:00,   20.55it/s]
 54%|#####4    | Fitting GeneralizingEstimator : 19/35 [00:00<00:00,   20.05it/s]
 57%|#####7    | Fitting GeneralizingEstimator : 20/35 [00:01<00:00,   20.51it/s]
 60%|######    | Fitting GeneralizingEstimator : 21/35 [00:01<00:00,   20.02it/s]
 66%|######5   | Fitting GeneralizingEstimator : 23/35 [00:01<00:00,   20.01it/s]
 69%|######8   | Fitting GeneralizingEstimator : 24/35 [00:01<00:00,   20.42it/s]
 71%|#######1  | Fitting GeneralizingEstimator : 25/35 [00:01<00:00,   20.00it/s]
 77%|#######7  | Fitting GeneralizingEstimator : 27/35 [00:01<00:00,   21.62it/s]
 80%|########  | Fitting GeneralizingEstimator : 28/35 [00:01<00:00,   20.27it/s]
 86%|########5 | Fitting GeneralizingEstimator : 30/35 [00:01<00:00,   20.35it/s]
 91%|#########1| Fitting GeneralizingEstimator : 32/35 [00:01<00:00,   20.13it/s]
 94%|#########4| Fitting GeneralizingEstimator : 33/35 [00:01<00:00,   19.25it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:01<00:00,   19.61it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:01<00:00,   19.53it/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)

Out:

  0%|          | Scoring GeneralizingEstimator : 0/1225 [00:00<?,       ?it/s]
  2%|1         | Scoring GeneralizingEstimator : 21/1225 [00:00<00:01,  616.96it/s]
  3%|2         | Scoring GeneralizingEstimator : 33/1225 [00:00<00:02,  482.11it/s]
  4%|4         | Scoring GeneralizingEstimator : 55/1225 [00:00<00:02,  391.92it/s]
  5%|5         | Scoring GeneralizingEstimator : 64/1225 [00:00<00:03,  364.50it/s]
  6%|6         | Scoring GeneralizingEstimator : 75/1225 [00:00<00:03,  294.58it/s]
  7%|7         | Scoring GeneralizingEstimator : 87/1225 [00:00<00:03,  302.55it/s]
  8%|7         | Scoring GeneralizingEstimator : 93/1225 [00:00<00:04,  259.86it/s]
  9%|8         | Scoring GeneralizingEstimator : 106/1225 [00:00<00:04,  272.91it/s]
  9%|9         | Scoring GeneralizingEstimator : 116/1225 [00:00<00:04,  248.52it/s]
 11%|#         | Scoring GeneralizingEstimator : 129/1225 [00:00<00:04,  260.23it/s]
 11%|#         | Scoring GeneralizingEstimator : 134/1225 [00:00<00:04,  233.92it/s]
 12%|#2        | Scoring GeneralizingEstimator : 152/1225 [00:00<00:04,  256.59it/s]
 13%|#3        | Scoring GeneralizingEstimator : 162/1225 [00:00<00:04,  242.75it/s]
 14%|#4        | Scoring GeneralizingEstimator : 174/1225 [00:00<00:04,  250.17it/s]
 15%|#4        | Scoring GeneralizingEstimator : 183/1225 [00:00<00:04,  236.31it/s]
 16%|#6        | Scoring GeneralizingEstimator : 197/1225 [00:00<00:04,  247.25it/s]
 17%|#6        | Scoring GeneralizingEstimator : 207/1225 [00:00<00:04,  236.39it/s]
 18%|#8        | Scoring GeneralizingEstimator : 226/1225 [00:00<00:03,  255.06it/s]
 19%|#9        | Scoring GeneralizingEstimator : 234/1225 [00:00<00:04,  240.94it/s]
 20%|##        | Scoring GeneralizingEstimator : 251/1225 [00:00<00:03,  255.02it/s]
 21%|##1       | Scoring GeneralizingEstimator : 260/1225 [00:01<00:03,  242.44it/s]
 23%|##2       | Scoring GeneralizingEstimator : 278/1225 [00:01<00:03,  257.06it/s]
 23%|##3       | Scoring GeneralizingEstimator : 286/1225 [00:01<00:03,  244.86it/s]
 25%|##4       | Scoring GeneralizingEstimator : 306/1225 [00:01<00:03,  261.64it/s]
 26%|##5       | Scoring GeneralizingEstimator : 315/1225 [00:01<00:03,  249.00it/s]
 27%|##7       | Scoring GeneralizingEstimator : 333/1225 [00:01<00:03,  262.09it/s]
 28%|##7       | Scoring GeneralizingEstimator : 339/1225 [00:01<00:03,  247.18it/s]
 29%|##9       | Scoring GeneralizingEstimator : 357/1225 [00:01<00:03,  259.88it/s]
 30%|##9       | Scoring GeneralizingEstimator : 365/1225 [00:01<00:03,  248.21it/s]
 31%|###1      | Scoring GeneralizingEstimator : 385/1225 [00:01<00:03,  263.09it/s]
 32%|###2      | Scoring GeneralizingEstimator : 392/1225 [00:01<00:03,  250.21it/s]
 33%|###3      | Scoring GeneralizingEstimator : 405/1225 [00:01<00:03,  255.85it/s]
 34%|###3      | Scoring GeneralizingEstimator : 411/1225 [00:01<00:03,  243.80it/s]
 35%|###4      | Scoring GeneralizingEstimator : 424/1225 [00:01<00:03,  249.50it/s]
 35%|###5      | Scoring GeneralizingEstimator : 430/1225 [00:01<00:03,  237.12it/s]
 36%|###6      | Scoring GeneralizingEstimator : 447/1225 [00:01<00:03,  247.89it/s]
 37%|###7      | Scoring GeneralizingEstimator : 456/1225 [00:01<00:03,  239.27it/s]
 39%|###8      | Scoring GeneralizingEstimator : 472/1225 [00:01<00:03,  248.59it/s]
 39%|###9      | Scoring GeneralizingEstimator : 479/1225 [00:01<00:03,  237.76it/s]
 41%|####      | Scoring GeneralizingEstimator : 498/1225 [00:01<00:02,  250.45it/s]
 41%|####1     | Scoring GeneralizingEstimator : 506/1225 [00:02<00:02,  240.79it/s]
 43%|####2     | Scoring GeneralizingEstimator : 522/1225 [00:02<00:02,  249.76it/s]
 43%|####3     | Scoring GeneralizingEstimator : 531/1225 [00:02<00:02,  242.45it/s]
 45%|####4     | Scoring GeneralizingEstimator : 546/1225 [00:02<00:02,  249.99it/s]
 45%|####5     | Scoring GeneralizingEstimator : 553/1225 [00:02<00:02,  237.81it/s]
 47%|####6     | Scoring GeneralizingEstimator : 571/1225 [00:02<00:02,  248.88it/s]
 47%|####7     | Scoring GeneralizingEstimator : 577/1225 [00:02<00:02,  237.21it/s]
 49%|####8     | Scoring GeneralizingEstimator : 599/1225 [00:02<00:02,  252.56it/s]
 49%|####9     | Scoring GeneralizingEstimator : 605/1225 [00:02<00:02,  242.11it/s]
 51%|#####     | Scoring GeneralizingEstimator : 623/1225 [00:02<00:02,  252.78it/s]
 52%|#####1    | Scoring GeneralizingEstimator : 631/1225 [00:02<00:02,  242.47it/s]
 53%|#####3    | Scoring GeneralizingEstimator : 651/1225 [00:02<00:02,  255.13it/s]
 54%|#####3    | Scoring GeneralizingEstimator : 657/1225 [00:02<00:02,  245.80it/s]
 55%|#####4    | Scoring GeneralizingEstimator : 673/1225 [00:02<00:02,  254.11it/s]
 56%|#####5    | Scoring GeneralizingEstimator : 684/1225 [00:02<00:02,  245.85it/s]
 57%|#####7    | Scoring GeneralizingEstimator : 701/1225 [00:02<00:02,  254.98it/s]
 58%|#####7    | Scoring GeneralizingEstimator : 710/1225 [00:02<00:02,  246.90it/s]
 60%|#####9    | Scoring GeneralizingEstimator : 730/1225 [00:02<00:01,  259.20it/s]
 60%|######    | Scoring GeneralizingEstimator : 737/1225 [00:02<00:01,  248.69it/s]
 61%|######1   | Scoring GeneralizingEstimator : 751/1225 [00:02<00:01,  254.51it/s]
 62%|######1   | Scoring GeneralizingEstimator : 755/1225 [00:03<00:01,  242.33it/s]
 63%|######3   | Scoring GeneralizingEstimator : 774/1225 [00:03<00:01,  253.70it/s]
 64%|######3   | Scoring GeneralizingEstimator : 783/1225 [00:03<00:01,  245.67it/s]
 65%|######5   | Scoring GeneralizingEstimator : 802/1225 [00:03<00:01,  256.86it/s]
 66%|######6   | Scoring GeneralizingEstimator : 811/1225 [00:03<00:01,  248.66it/s]
 68%|######7   | Scoring GeneralizingEstimator : 828/1225 [00:03<00:01,  257.60it/s]
 68%|######8   | Scoring GeneralizingEstimator : 837/1225 [00:03<00:01,  249.38it/s]
 70%|######9   | Scoring GeneralizingEstimator : 852/1225 [00:03<00:01,  256.20it/s]
 70%|#######   | Scoring GeneralizingEstimator : 859/1225 [00:03<00:01,  246.04it/s]
 72%|#######1  | Scoring GeneralizingEstimator : 877/1225 [00:03<00:01,  256.06it/s]
 72%|#######2  | Scoring GeneralizingEstimator : 885/1225 [00:03<00:01,  245.97it/s]
 74%|#######3  | Scoring GeneralizingEstimator : 903/1225 [00:03<00:01,  255.86it/s]
 74%|#######4  | Scoring GeneralizingEstimator : 911/1225 [00:03<00:01,  248.11it/s]
 76%|#######5  | Scoring GeneralizingEstimator : 928/1225 [00:03<00:01,  256.98it/s]
 77%|#######6  | Scoring GeneralizingEstimator : 938/1225 [00:03<00:01,  249.70it/s]
 78%|#######8  | Scoring GeneralizingEstimator : 956/1225 [00:03<00:01,  259.48it/s]
 79%|#######8  | Scoring GeneralizingEstimator : 965/1225 [00:03<00:01,  252.27it/s]
 80%|########  | Scoring GeneralizingEstimator : 980/1225 [00:03<00:00,  258.91it/s]
 81%|########  | Scoring GeneralizingEstimator : 992/1225 [00:03<00:00,  251.74it/s]
 82%|########2 | Scoring GeneralizingEstimator : 1008/1225 [00:03<00:00,  259.34it/s]
 83%|########2 | Scoring GeneralizingEstimator : 1012/1225 [00:04<00:00,  248.15it/s]
 84%|########3 | Scoring GeneralizingEstimator : 1023/1225 [00:04<00:00,  250.77it/s]
 84%|########4 | Scoring GeneralizingEstimator : 1032/1225 [00:04<00:00,  242.19it/s]
 85%|########5 | Scoring GeneralizingEstimator : 1045/1225 [00:04<00:00,  247.00it/s]
 86%|########5 | Scoring GeneralizingEstimator : 1051/1225 [00:04<00:00,  244.56it/s]
 87%|########6 | Scoring GeneralizingEstimator : 1065/1225 [00:04<00:00,  241.39it/s]
 87%|########7 | Scoring GeneralizingEstimator : 1069/1225 [00:04<00:00,  231.34it/s]
 88%|########8 | Scoring GeneralizingEstimator : 1082/1225 [00:04<00:00,  236.58it/s]
 89%|########9 | Scoring GeneralizingEstimator : 1093/1225 [00:04<00:00,  230.10it/s]
 91%|######### | Scoring GeneralizingEstimator : 1112/1225 [00:04<00:00,  241.37it/s]
 91%|#########1| Scoring GeneralizingEstimator : 1118/1225 [00:04<00:00,  231.54it/s]
 93%|#########2| Scoring GeneralizingEstimator : 1137/1225 [00:04<00:00,  242.82it/s]
 93%|#########3| Scoring GeneralizingEstimator : 1144/1225 [00:04<00:00,  233.82it/s]
 95%|#########4| Scoring GeneralizingEstimator : 1163/1225 [00:04<00:00,  245.02it/s]
 96%|#########5| Scoring GeneralizingEstimator : 1170/1225 [00:04<00:00,  235.88it/s]
 97%|#########7| Scoring GeneralizingEstimator : 1191/1225 [00:04<00:00,  249.06it/s]
 98%|#########7| Scoring GeneralizingEstimator : 1199/1225 [00:04<00:00,  240.62it/s]
 99%|#########9| Scoring GeneralizingEstimator : 1213/1225 [00:04<00:00,  246.47it/s]
 99%|#########9| Scoring GeneralizingEstimator : 1218/1225 [00:04<00:00,  235.30it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:04<00:00,  246.88it/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()
Generalization across time and condition

References

1

Jean-Rémi King and Stanislas Dehaene. Characterizing the dynamics of mental representations: the temporal generalization method. Trends in Cognitive Sciences, 18(4):203–210, 2014. doi:10.1016/j.tics.2014.01.002.

Total running time of the script: ( 0 minutes 12.616 seconds)

Estimated memory usage: 128 MB

Gallery generated by Sphinx-Gallery