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.16it/s]
  6%|5         | Fitting GeneralizingEstimator : 2/35 [00:00<00:01,   21.84it/s]
  9%|8         | Fitting GeneralizingEstimator : 3/35 [00:00<00:01,   23.97it/s]
 14%|#4        | Fitting GeneralizingEstimator : 5/35 [00:00<00:01,   22.10it/s]
 17%|#7        | Fitting GeneralizingEstimator : 6/35 [00:00<00:01,   18.03it/s]
 20%|##        | Fitting GeneralizingEstimator : 7/35 [00:00<00:01,   17.57it/s]
 23%|##2       | Fitting GeneralizingEstimator : 8/35 [00:00<00:01,   18.67it/s]
 26%|##5       | Fitting GeneralizingEstimator : 9/35 [00:00<00:01,   18.07it/s]
 31%|###1      | Fitting GeneralizingEstimator : 11/35 [00:00<00:01,   21.29it/s]
 34%|###4      | Fitting GeneralizingEstimator : 12/35 [00:00<00:01,   20.47it/s]
 40%|####      | Fitting GeneralizingEstimator : 14/35 [00:00<00:00,   23.15it/s]
 49%|####8     | Fitting GeneralizingEstimator : 17/35 [00:00<00:00,   24.36it/s]
 54%|#####4    | Fitting GeneralizingEstimator : 19/35 [00:00<00:00,   23.64it/s]
 57%|#####7    | Fitting GeneralizingEstimator : 20/35 [00:00<00:00,   22.84it/s]
 63%|######2   | Fitting GeneralizingEstimator : 22/35 [00:00<00:00,   24.66it/s]
 66%|######5   | Fitting GeneralizingEstimator : 23/35 [00:00<00:00,   23.77it/s]
 71%|#######1  | Fitting GeneralizingEstimator : 25/35 [00:01<00:00,   25.45it/s]
 74%|#######4  | Fitting GeneralizingEstimator : 26/35 [00:01<00:00,   25.65it/s]
 77%|#######7  | Fitting GeneralizingEstimator : 27/35 [00:01<00:00,   24.64it/s]
 80%|########  | Fitting GeneralizingEstimator : 28/35 [00:01<00:00,   23.88it/s]
 83%|########2 | Fitting GeneralizingEstimator : 29/35 [00:01<00:00,   24.11it/s]
 86%|########5 | Fitting GeneralizingEstimator : 30/35 [00:01<00:00,   23.38it/s]
 89%|########8 | Fitting GeneralizingEstimator : 31/35 [00:01<00:00,   23.63it/s]
 91%|#########1| Fitting GeneralizingEstimator : 32/35 [00:01<00:00,   22.96it/s]
 97%|#########7| Fitting GeneralizingEstimator : 34/35 [00:01<00:00,   24.49it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:01<00:00,   23.60it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:01<00:00,   23.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)

Out:

  0%|          | Scoring GeneralizingEstimator : 0/1225 [00:00<?,       ?it/s]
  1%|1         | Scoring GeneralizingEstimator : 15/1225 [00:00<00:02,  439.00it/s]
  2%|1         | Scoring GeneralizingEstimator : 22/1225 [00:00<00:03,  320.39it/s]
  3%|2         | Scoring GeneralizingEstimator : 36/1225 [00:00<00:03,  353.44it/s]
  3%|3         | Scoring GeneralizingEstimator : 38/1225 [00:00<00:05,  231.18it/s]
  4%|4         | Scoring GeneralizingEstimator : 50/1225 [00:00<00:04,  254.81it/s]
  5%|4         | Scoring GeneralizingEstimator : 56/1225 [00:00<00:05,  208.28it/s]
  6%|5         | Scoring GeneralizingEstimator : 69/1225 [00:00<00:04,  231.26it/s]
  6%|6         | Scoring GeneralizingEstimator : 76/1225 [00:00<00:05,  204.68it/s]
  7%|7         | Scoring GeneralizingEstimator : 88/1225 [00:00<00:05,  220.12it/s]
  8%|7         | Scoring GeneralizingEstimator : 95/1225 [00:00<00:05,  200.11it/s]
  9%|8         | Scoring GeneralizingEstimator : 108/1225 [00:00<00:05,  215.90it/s]
  9%|9         | Scoring GeneralizingEstimator : 114/1225 [00:00<00:05,  197.09it/s]
 11%|#         | Scoring GeneralizingEstimator : 130/1225 [00:00<00:05,  217.60it/s]
 11%|#1        | Scoring GeneralizingEstimator : 140/1225 [00:00<00:05,  208.89it/s]
 12%|#2        | Scoring GeneralizingEstimator : 150/1225 [00:00<00:05,  214.66it/s]
 13%|#2        | Scoring GeneralizingEstimator : 154/1225 [00:00<00:05,  195.97it/s]
 13%|#3        | Scoring GeneralizingEstimator : 165/1225 [00:00<00:05,  203.88it/s]
 14%|#3        | Scoring GeneralizingEstimator : 171/1225 [00:00<00:05,  191.22it/s]
 15%|#5        | Scoring GeneralizingEstimator : 184/1225 [00:00<00:05,  202.20it/s]
 16%|#5        | Scoring GeneralizingEstimator : 191/1225 [00:00<00:05,  192.04it/s]
 17%|#6        | Scoring GeneralizingEstimator : 204/1225 [00:00<00:05,  202.24it/s]
 17%|#7        | Scoring GeneralizingEstimator : 209/1225 [00:01<00:05,  189.80it/s]
 18%|#8        | Scoring GeneralizingEstimator : 223/1225 [00:01<00:04,  201.16it/s]
 19%|#8        | Scoring GeneralizingEstimator : 230/1225 [00:01<00:05,  192.11it/s]
 20%|##        | Scoring GeneralizingEstimator : 247/1225 [00:01<00:04,  207.14it/s]
 21%|##        | Scoring GeneralizingEstimator : 254/1225 [00:01<00:04,  197.66it/s]
 22%|##2       | Scoring GeneralizingEstimator : 271/1225 [00:01<00:04,  211.73it/s]
 23%|##2       | Scoring GeneralizingEstimator : 278/1225 [00:01<00:04,  202.74it/s]
 24%|##3       | Scoring GeneralizingEstimator : 291/1225 [00:01<00:04,  210.85it/s]
 24%|##4       | Scoring GeneralizingEstimator : 297/1225 [00:01<00:04,  200.85it/s]
 25%|##5       | Scoring GeneralizingEstimator : 311/1225 [00:01<00:04,  210.12it/s]
 26%|##5       | Scoring GeneralizingEstimator : 317/1225 [00:01<00:04,  200.28it/s]
 27%|##7       | Scoring GeneralizingEstimator : 333/1225 [00:01<00:04,  211.85it/s]
 28%|##7       | Scoring GeneralizingEstimator : 341/1225 [00:01<00:04,  204.58it/s]
 29%|##9       | Scoring GeneralizingEstimator : 357/1225 [00:01<00:04,  215.67it/s]
 30%|##9       | Scoring GeneralizingEstimator : 365/1225 [00:01<00:04,  208.26it/s]
 31%|###1      | Scoring GeneralizingEstimator : 383/1225 [00:01<00:03,  221.42it/s]
 32%|###1      | Scoring GeneralizingEstimator : 390/1225 [00:01<00:03,  212.43it/s]
 33%|###3      | Scoring GeneralizingEstimator : 406/1225 [00:01<00:03,  222.80it/s]
 34%|###3      | Scoring GeneralizingEstimator : 414/1225 [00:01<00:03,  215.09it/s]
 35%|###4      | Scoring GeneralizingEstimator : 426/1225 [00:01<00:03,  220.54it/s]
 35%|###5      | Scoring GeneralizingEstimator : 432/1225 [00:02<00:03,  210.89it/s]
 37%|###6      | Scoring GeneralizingEstimator : 449/1225 [00:02<00:03,  222.15it/s]
 37%|###7      | Scoring GeneralizingEstimator : 455/1225 [00:02<00:03,  212.49it/s]
 38%|###8      | Scoring GeneralizingEstimator : 467/1225 [00:02<00:03,  217.80it/s]
 39%|###8      | Scoring GeneralizingEstimator : 473/1225 [00:02<00:03,  208.65it/s]
 40%|###9      | Scoring GeneralizingEstimator : 485/1225 [00:02<00:03,  214.15it/s]
 40%|####      | Scoring GeneralizingEstimator : 492/1225 [00:02<00:03,  213.88it/s]
 41%|####1     | Scoring GeneralizingEstimator : 505/1225 [00:02<00:03,  212.28it/s]
 42%|####1     | Scoring GeneralizingEstimator : 510/1225 [00:02<00:03,  202.87it/s]
 43%|####2     | Scoring GeneralizingEstimator : 523/1225 [00:02<00:03,  209.55it/s]
 43%|####3     | Scoring GeneralizingEstimator : 529/1225 [00:02<00:03,  201.17it/s]
 44%|####4     | Scoring GeneralizingEstimator : 542/1225 [00:02<00:03,  207.79it/s]
 45%|####4     | Scoring GeneralizingEstimator : 548/1225 [00:02<00:03,  199.66it/s]
 46%|####6     | Scoring GeneralizingEstimator : 565/1225 [00:02<00:03,  210.65it/s]
 47%|####6     | Scoring GeneralizingEstimator : 572/1225 [00:02<00:03,  210.52it/s]
 47%|####6     | Scoring GeneralizingEstimator : 573/1225 [00:02<00:03,  203.76it/s]
 48%|####8     | Scoring GeneralizingEstimator : 592/1225 [00:02<00:02,  217.27it/s]
 49%|####9     | Scoring GeneralizingEstimator : 601/1225 [00:02<00:02,  211.73it/s]
 51%|#####     | Scoring GeneralizingEstimator : 619/1225 [00:02<00:02,  223.68it/s]
 51%|#####1    | Scoring GeneralizingEstimator : 625/1225 [00:02<00:02,  221.88it/s]
 51%|#####1    | Scoring GeneralizingEstimator : 626/1225 [00:02<00:02,  214.47it/s]
 52%|#####2    | Scoring GeneralizingEstimator : 643/1225 [00:02<00:02,  225.63it/s]
 53%|#####3    | Scoring GeneralizingEstimator : 650/1225 [00:03<00:02,  217.35it/s]
 54%|#####4    | Scoring GeneralizingEstimator : 667/1225 [00:03<00:02,  228.31it/s]
 55%|#####5    | Scoring GeneralizingEstimator : 675/1225 [00:03<00:02,  220.43it/s]
 56%|#####6    | Scoring GeneralizingEstimator : 689/1225 [00:03<00:02,  227.75it/s]
 57%|#####6    | Scoring GeneralizingEstimator : 695/1225 [00:03<00:02,  217.84it/s]
 58%|#####8    | Scoring GeneralizingEstimator : 711/1225 [00:03<00:02,  227.35it/s]
 59%|#####8    | Scoring GeneralizingEstimator : 720/1225 [00:03<00:02,  220.87it/s]
 60%|#####9    | Scoring GeneralizingEstimator : 732/1225 [00:03<00:02,  225.82it/s]
 60%|######    | Scoring GeneralizingEstimator : 739/1225 [00:03<00:02,  217.23it/s]
 61%|######1   | Scoring GeneralizingEstimator : 751/1225 [00:03<00:02,  222.29it/s]
 62%|######1   | Scoring GeneralizingEstimator : 759/1225 [00:03<00:02,  215.22it/s]
 63%|######2   | Scoring GeneralizingEstimator : 771/1225 [00:03<00:02,  220.32it/s]
 63%|######3   | Scoring GeneralizingEstimator : 777/1225 [00:03<00:02,  211.25it/s]
 64%|######4   | Scoring GeneralizingEstimator : 788/1225 [00:03<00:02,  215.39it/s]
 65%|######4   | Scoring GeneralizingEstimator : 793/1225 [00:03<00:02,  205.65it/s]
 66%|######5   | Scoring GeneralizingEstimator : 807/1225 [00:03<00:01,  213.12it/s]
 67%|######6   | Scoring GeneralizingEstimator : 815/1225 [00:03<00:01,  206.79it/s]
 68%|######7   | Scoring GeneralizingEstimator : 832/1225 [00:03<00:01,  217.39it/s]
 69%|######8   | Scoring GeneralizingEstimator : 841/1225 [00:03<00:01,  211.82it/s]
 70%|#######   | Scoring GeneralizingEstimator : 858/1225 [00:03<00:01,  222.21it/s]
 71%|#######   | Scoring GeneralizingEstimator : 866/1225 [00:04<00:01,  215.25it/s]
 72%|#######2  | Scoring GeneralizingEstimator : 883/1225 [00:04<00:01,  225.42it/s]
 73%|#######2  | Scoring GeneralizingEstimator : 890/1225 [00:04<00:01,  217.27it/s]
 74%|#######4  | Scoring GeneralizingEstimator : 907/1225 [00:04<00:01,  227.36it/s]
 75%|#######4  | Scoring GeneralizingEstimator : 915/1225 [00:04<00:01,  220.17it/s]
 76%|#######6  | Scoring GeneralizingEstimator : 932/1225 [00:04<00:01,  230.11it/s]
 77%|#######6  | Scoring GeneralizingEstimator : 941/1225 [00:04<00:01,  223.72it/s]
 78%|#######8  | Scoring GeneralizingEstimator : 957/1225 [00:04<00:01,  232.49it/s]
 79%|#######8  | Scoring GeneralizingEstimator : 963/1225 [00:04<00:01,  222.95it/s]
 80%|#######9  | Scoring GeneralizingEstimator : 976/1225 [00:04<00:01,  228.55it/s]
 80%|########  | Scoring GeneralizingEstimator : 981/1225 [00:04<00:01,  218.31it/s]
 81%|########1 | Scoring GeneralizingEstimator : 994/1225 [00:04<00:01,  224.11it/s]
 82%|########1 | Scoring GeneralizingEstimator : 1000/1225 [00:04<00:01,  222.44it/s]
 83%|########2 | Scoring GeneralizingEstimator : 1013/1225 [00:04<00:00,  220.40it/s]
 83%|########3 | Scoring GeneralizingEstimator : 1019/1225 [00:04<00:00,  212.00it/s]
 85%|########4 | Scoring GeneralizingEstimator : 1037/1225 [00:04<00:00,  223.11it/s]
 85%|########5 | Scoring GeneralizingEstimator : 1045/1225 [00:04<00:00,  216.29it/s]
 87%|########6 | Scoring GeneralizingEstimator : 1061/1225 [00:04<00:00,  225.14it/s]
 87%|########7 | Scoring GeneralizingEstimator : 1068/1225 [00:04<00:00,  217.25it/s]
 88%|########8 | Scoring GeneralizingEstimator : 1083/1225 [00:04<00:00,  225.08it/s]
 89%|########9 | Scoring GeneralizingEstimator : 1091/1225 [00:05<00:00,  218.15it/s]
 90%|######### | Scoring GeneralizingEstimator : 1105/1225 [00:05<00:00,  224.89it/s]
 91%|######### | Scoring GeneralizingEstimator : 1112/1225 [00:05<00:00,  224.24it/s]
 91%|######### | Scoring GeneralizingEstimator : 1113/1225 [00:05<00:00,  217.31it/s]
 92%|#########2| Scoring GeneralizingEstimator : 1131/1225 [00:05<00:00,  228.68it/s]
 93%|#########2| Scoring GeneralizingEstimator : 1139/1225 [00:05<00:00,  221.61it/s]
 94%|#########3| Scoring GeneralizingEstimator : 1151/1225 [00:05<00:00,  226.39it/s]
 94%|#########4| Scoring GeneralizingEstimator : 1157/1225 [00:05<00:00,  217.04it/s]
 96%|#########5| Scoring GeneralizingEstimator : 1175/1225 [00:05<00:00,  228.30it/s]
 96%|#########6| Scoring GeneralizingEstimator : 1181/1225 [00:05<00:00,  218.70it/s]
 98%|#########7| Scoring GeneralizingEstimator : 1198/1225 [00:05<00:00,  228.80it/s]
 98%|#########8| Scoring GeneralizingEstimator : 1204/1225 [00:05<00:00,  219.37it/s]
 99%|#########9| Scoring GeneralizingEstimator : 1218/1225 [00:05<00:00,  226.20it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:05<00:00,  218.45it/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 17.991 seconds)

Estimated memory usage: 129 MB

Gallery generated by Sphinx-Gallery