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]
  6%|5         | Fitting GeneralizingEstimator : 2/35 [00:00<00:01,   24.55it/s]
  9%|8         | Fitting GeneralizingEstimator : 3/35 [00:00<00:01,   16.37it/s]
 11%|#1        | Fitting GeneralizingEstimator : 4/35 [00:00<00:01,   18.19it/s]
 14%|#4        | Fitting GeneralizingEstimator : 5/35 [00:00<00:01,   19.86it/s]
 17%|#7        | Fitting GeneralizingEstimator : 6/35 [00:00<00:01,   15.41it/s]
 20%|##        | Fitting GeneralizingEstimator : 7/35 [00:00<00:01,   16.62it/s]
 23%|##2       | Fitting GeneralizingEstimator : 8/35 [00:00<00:01,   17.76it/s]
 26%|##5       | Fitting GeneralizingEstimator : 9/35 [00:00<00:01,   17.26it/s]
 31%|###1      | Fitting GeneralizingEstimator : 11/35 [00:00<00:01,   17.97it/s]
 34%|###4      | Fitting GeneralizingEstimator : 12/35 [00:00<00:01,   17.60it/s]
 40%|####      | Fitting GeneralizingEstimator : 14/35 [00:00<00:01,   20.04it/s]
 43%|####2     | Fitting GeneralizingEstimator : 15/35 [00:00<00:01,   19.61it/s]
 51%|#####1    | Fitting GeneralizingEstimator : 18/35 [00:00<00:00,   23.51it/s]
 60%|######    | Fitting GeneralizingEstimator : 21/35 [00:00<00:00,   24.49it/s]
 66%|######5   | Fitting GeneralizingEstimator : 23/35 [00:01<00:00,   23.45it/s]
 69%|######8   | Fitting GeneralizingEstimator : 24/35 [00:01<00:00,   23.16it/s]
 74%|#######4  | Fitting GeneralizingEstimator : 26/35 [00:01<00:00,   24.76it/s]
 80%|########  | Fitting GeneralizingEstimator : 28/35 [00:01<00:00,   24.16it/s]
 83%|########2 | Fitting GeneralizingEstimator : 29/35 [00:01<00:00,   24.38it/s]
 86%|########5 | Fitting GeneralizingEstimator : 30/35 [00:01<00:00,   24.21it/s]
 89%|########8 | Fitting GeneralizingEstimator : 31/35 [00:01<00:00,   22.99it/s]
 94%|#########4| Fitting GeneralizingEstimator : 33/35 [00:01<00:00,   24.42it/s]
 97%|#########7| Fitting GeneralizingEstimator : 34/35 [00:01<00:00,   22.74it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:01<00:00,   23.02it/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 : 18/1225 [00:00<00:02,  526.85it/s]
  3%|2         | Scoring GeneralizingEstimator : 36/1225 [00:00<00:03,  326.68it/s]
  5%|4         | Scoring GeneralizingEstimator : 56/1225 [00:00<00:02,  392.31it/s]
  5%|5         | Scoring GeneralizingEstimator : 66/1225 [00:00<00:03,  310.62it/s]
  7%|6         | Scoring GeneralizingEstimator : 85/1225 [00:00<00:03,  349.29it/s]
  8%|7         | Scoring GeneralizingEstimator : 93/1225 [00:00<00:03,  294.32it/s]
  9%|9         | Scoring GeneralizingEstimator : 111/1225 [00:00<00:03,  321.60it/s]
 10%|9         | Scoring GeneralizingEstimator : 121/1225 [00:00<00:03,  289.03it/s]
 11%|#1        | Scoring GeneralizingEstimator : 140/1225 [00:00<00:03,  314.24it/s]
 12%|#2        | Scoring GeneralizingEstimator : 148/1225 [00:00<00:03,  283.34it/s]
 14%|#3        | Scoring GeneralizingEstimator : 166/1225 [00:00<00:03,  303.09it/s]
 14%|#4        | Scoring GeneralizingEstimator : 175/1225 [00:00<00:03,  279.56it/s]
 16%|#5        | Scoring GeneralizingEstimator : 193/1225 [00:00<00:03,  297.27it/s]
 16%|#6        | Scoring GeneralizingEstimator : 200/1225 [00:00<00:03,  273.06it/s]
 18%|#7        | Scoring GeneralizingEstimator : 219/1225 [00:00<00:03,  291.50it/s]
 19%|#8        | Scoring GeneralizingEstimator : 227/1225 [00:00<00:03,  271.68it/s]
 20%|##        | Scoring GeneralizingEstimator : 246/1225 [00:00<00:03,  288.65it/s]
 21%|##        | Scoring GeneralizingEstimator : 255/1225 [00:00<00:03,  272.21it/s]
 22%|##2       | Scoring GeneralizingEstimator : 274/1225 [00:00<00:03,  288.13it/s]
 23%|##3       | Scoring GeneralizingEstimator : 282/1225 [00:01<00:03,  271.19it/s]
 24%|##4       | Scoring GeneralizingEstimator : 300/1225 [00:01<00:03,  284.67it/s]
 25%|##5       | Scoring GeneralizingEstimator : 309/1225 [00:01<00:03,  270.36it/s]
 27%|##6       | Scoring GeneralizingEstimator : 328/1225 [00:01<00:03,  284.76it/s]
 27%|##7       | Scoring GeneralizingEstimator : 336/1225 [00:01<00:03,  269.63it/s]
 28%|##8       | Scoring GeneralizingEstimator : 349/1225 [00:01<00:03,  275.07it/s]
 29%|##8       | Scoring GeneralizingEstimator : 354/1225 [00:01<00:03,  257.35it/s]
 30%|##9       | Scoring GeneralizingEstimator : 367/1225 [00:01<00:03,  263.14it/s]
 30%|###       | Scoring GeneralizingEstimator : 373/1225 [00:01<00:03,  248.33it/s]
 32%|###1      | Scoring GeneralizingEstimator : 386/1225 [00:01<00:03,  254.29it/s]
 32%|###2      | Scoring GeneralizingEstimator : 392/1225 [00:01<00:03,  240.70it/s]
 33%|###3      | Scoring GeneralizingEstimator : 405/1225 [00:01<00:03,  246.82it/s]
 34%|###3      | Scoring GeneralizingEstimator : 411/1225 [00:01<00:03,  234.18it/s]
 35%|###4      | Scoring GeneralizingEstimator : 424/1225 [00:01<00:03,  240.42it/s]
 35%|###5      | Scoring GeneralizingEstimator : 429/1225 [00:01<00:03,  227.38it/s]
 36%|###6      | Scoring GeneralizingEstimator : 442/1225 [00:01<00:03,  233.76it/s]
 36%|###6      | Scoring GeneralizingEstimator : 447/1225 [00:01<00:03,  221.49it/s]
 38%|###7      | Scoring GeneralizingEstimator : 460/1225 [00:01<00:03,  227.99it/s]
 38%|###8      | Scoring GeneralizingEstimator : 466/1225 [00:01<00:03,  217.54it/s]
 39%|###9      | Scoring GeneralizingEstimator : 479/1225 [00:01<00:03,  224.07it/s]
 40%|###9      | Scoring GeneralizingEstimator : 485/1225 [00:02<00:03,  214.10it/s]
 41%|####      | Scoring GeneralizingEstimator : 498/1225 [00:02<00:03,  220.69it/s]
 41%|####1     | Scoring GeneralizingEstimator : 503/1225 [00:02<00:03,  209.99it/s]
 42%|####2     | Scoring GeneralizingEstimator : 516/1225 [00:02<00:03,  216.63it/s]
 43%|####2     | Scoring GeneralizingEstimator : 521/1225 [00:02<00:03,  206.33it/s]
 44%|####3     | Scoring GeneralizingEstimator : 534/1225 [00:02<00:03,  213.06it/s]
 44%|####4     | Scoring GeneralizingEstimator : 541/1225 [00:02<00:03,  205.26it/s]
 45%|####5     | Scoring GeneralizingEstimator : 554/1225 [00:02<00:03,  211.97it/s]
 46%|####5     | Scoring GeneralizingEstimator : 560/1225 [00:02<00:03,  203.34it/s]
 47%|####6     | Scoring GeneralizingEstimator : 573/1225 [00:02<00:03,  210.07it/s]
 47%|####7     | Scoring GeneralizingEstimator : 578/1225 [00:02<00:03,  200.49it/s]
 48%|####8     | Scoring GeneralizingEstimator : 591/1225 [00:02<00:03,  207.22it/s]
 49%|####8     | Scoring GeneralizingEstimator : 597/1225 [00:02<00:03,  199.04it/s]
 50%|####9     | Scoring GeneralizingEstimator : 610/1225 [00:02<00:02,  205.77it/s]
 50%|#####     | Scoring GeneralizingEstimator : 616/1225 [00:02<00:03,  197.75it/s]
 51%|#####1    | Scoring GeneralizingEstimator : 629/1225 [00:02<00:02,  204.49it/s]
 52%|#####1    | Scoring GeneralizingEstimator : 634/1225 [00:02<00:03,  195.55it/s]
 53%|#####2    | Scoring GeneralizingEstimator : 647/1225 [00:02<00:02,  202.36it/s]
 53%|#####3    | Scoring GeneralizingEstimator : 653/1225 [00:02<00:02,  194.66it/s]
 54%|#####4    | Scoring GeneralizingEstimator : 666/1225 [00:02<00:02,  201.44it/s]
 55%|#####4    | Scoring GeneralizingEstimator : 672/1225 [00:03<00:02,  193.84it/s]
 56%|#####5    | Scoring GeneralizingEstimator : 685/1225 [00:03<00:02,  200.56it/s]
 56%|#####6    | Scoring GeneralizingEstimator : 691/1225 [00:03<00:02,  193.13it/s]
 57%|#####7    | Scoring GeneralizingEstimator : 704/1225 [00:03<00:02,  199.91it/s]
 58%|#####7    | Scoring GeneralizingEstimator : 710/1225 [00:03<00:02,  192.46it/s]
 59%|#####9    | Scoring GeneralizingEstimator : 723/1225 [00:03<00:02,  199.24it/s]
 60%|#####9    | Scoring GeneralizingEstimator : 729/1225 [00:03<00:02,  191.88it/s]
 61%|######    | Scoring GeneralizingEstimator : 742/1225 [00:03<00:02,  198.65it/s]
 61%|######    | Scoring GeneralizingEstimator : 747/1225 [00:03<00:02,  196.83it/s]
 62%|######2   | Scoring GeneralizingEstimator : 760/1225 [00:03<00:02,  196.81it/s]
 63%|######2   | Scoring GeneralizingEstimator : 766/1225 [00:03<00:02,  189.66it/s]
 64%|######3   | Scoring GeneralizingEstimator : 779/1225 [00:03<00:02,  196.43it/s]
 64%|######4   | Scoring GeneralizingEstimator : 784/1225 [00:03<00:02,  194.69it/s]
 65%|######5   | Scoring GeneralizingEstimator : 797/1225 [00:03<00:02,  194.56it/s]
 66%|######5   | Scoring GeneralizingEstimator : 803/1225 [00:03<00:02,  187.92it/s]
 67%|######6   | Scoring GeneralizingEstimator : 816/1225 [00:03<00:02,  194.74it/s]
 67%|######7   | Scoring GeneralizingEstimator : 821/1225 [00:03<00:02,  186.79it/s]
 68%|######8   | Scoring GeneralizingEstimator : 834/1225 [00:03<00:02,  193.62it/s]
 69%|######8   | Scoring GeneralizingEstimator : 840/1225 [00:03<00:02,  186.72it/s]
 70%|######9   | Scoring GeneralizingEstimator : 853/1225 [00:03<00:01,  193.48it/s]
 70%|#######   | Scoring GeneralizingEstimator : 858/1225 [00:04<00:01,  185.75it/s]
 71%|#######1  | Scoring GeneralizingEstimator : 871/1225 [00:04<00:01,  192.62it/s]
 72%|#######1  | Scoring GeneralizingEstimator : 877/1225 [00:04<00:01,  185.87it/s]
 73%|#######2  | Scoring GeneralizingEstimator : 889/1225 [00:04<00:01,  191.66it/s]
 73%|#######2  | Scoring GeneralizingEstimator : 894/1225 [00:04<00:01,  184.00it/s]
 74%|#######4  | Scoring GeneralizingEstimator : 907/1225 [00:04<00:01,  190.91it/s]
 75%|#######4  | Scoring GeneralizingEstimator : 913/1225 [00:04<00:01,  184.24it/s]
 76%|#######5  | Scoring GeneralizingEstimator : 926/1225 [00:04<00:01,  191.14it/s]
 76%|#######6  | Scoring GeneralizingEstimator : 932/1225 [00:04<00:01,  184.49it/s]
 77%|#######7  | Scoring GeneralizingEstimator : 945/1225 [00:04<00:01,  191.37it/s]
 78%|#######7  | Scoring GeneralizingEstimator : 951/1225 [00:04<00:01,  184.69it/s]
 79%|#######8  | Scoring GeneralizingEstimator : 964/1225 [00:04<00:01,  191.57it/s]
 79%|#######9  | Scoring GeneralizingEstimator : 969/1225 [00:04<00:01,  183.90it/s]
 80%|########  | Scoring GeneralizingEstimator : 982/1225 [00:04<00:01,  190.79it/s]
 81%|########  | Scoring GeneralizingEstimator : 988/1225 [00:04<00:01,  184.18it/s]
 82%|########1 | Scoring GeneralizingEstimator : 1001/1225 [00:04<00:01,  191.06it/s]
 82%|########2 | Scoring GeneralizingEstimator : 1007/1225 [00:04<00:01,  184.42it/s]
 83%|########3 | Scoring GeneralizingEstimator : 1020/1225 [00:04<00:01,  191.28it/s]
 84%|########3 | Scoring GeneralizingEstimator : 1026/1225 [00:04<00:01,  184.64it/s]
 85%|########4 | Scoring GeneralizingEstimator : 1039/1225 [00:04<00:00,  191.49it/s]
 85%|########5 | Scoring GeneralizingEstimator : 1045/1225 [00:05<00:00,  184.83it/s]
 86%|########6 | Scoring GeneralizingEstimator : 1058/1225 [00:05<00:00,  191.68it/s]
 87%|########6 | Scoring GeneralizingEstimator : 1064/1225 [00:05<00:00,  185.02it/s]
 88%|########7 | Scoring GeneralizingEstimator : 1077/1225 [00:05<00:00,  191.86it/s]
 88%|########8 | Scoring GeneralizingEstimator : 1083/1225 [00:05<00:00,  185.18it/s]
 89%|########9 | Scoring GeneralizingEstimator : 1096/1225 [00:05<00:00,  192.01it/s]
 90%|########9 | Scoring GeneralizingEstimator : 1102/1225 [00:05<00:00,  185.33it/s]
 91%|#########1| Scoring GeneralizingEstimator : 1115/1225 [00:05<00:00,  192.15it/s]
 92%|#########1| Scoring GeneralizingEstimator : 1121/1225 [00:05<00:00,  185.45it/s]
 93%|#########2| Scoring GeneralizingEstimator : 1134/1225 [00:05<00:00,  192.24it/s]
 93%|#########3| Scoring GeneralizingEstimator : 1140/1225 [00:05<00:00,  185.58it/s]
 94%|#########4| Scoring GeneralizingEstimator : 1153/1225 [00:05<00:00,  192.38it/s]
 95%|#########4| Scoring GeneralizingEstimator : 1159/1225 [00:05<00:00,  185.67it/s]
 96%|#########5| Scoring GeneralizingEstimator : 1172/1225 [00:05<00:00,  192.46it/s]
 96%|#########6| Scoring GeneralizingEstimator : 1178/1225 [00:05<00:00,  185.78it/s]
 97%|#########7| Scoring GeneralizingEstimator : 1191/1225 [00:05<00:00,  192.53it/s]
 98%|#########7| Scoring GeneralizingEstimator : 1196/1225 [00:05<00:00,  184.87it/s]
 99%|#########8| Scoring GeneralizingEstimator : 1209/1225 [00:05<00:00,  191.66it/s]
 99%|#########9| Scoring GeneralizingEstimator : 1215/1225 [00:05<00:00,  185.05it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:05<00:00,  190.45it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:05<00:00,  206.40it/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 13.008 seconds)

Estimated memory usage: 128 MB

Gallery generated by Sphinx-Gallery