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,   23.90it/s]
  9%|8         | Fitting GeneralizingEstimator : 3/35 [00:00<00:01,   26.32it/s]
 11%|#1        | Fitting GeneralizingEstimator : 4/35 [00:00<00:01,   27.07it/s]
 14%|#4        | Fitting GeneralizingEstimator : 5/35 [00:00<00:01,   23.13it/s]
 17%|#7        | Fitting GeneralizingEstimator : 6/35 [00:00<00:01,   23.95it/s]
 20%|##        | Fitting GeneralizingEstimator : 7/35 [00:00<00:01,   21.98it/s]
 23%|##2       | Fitting GeneralizingEstimator : 8/35 [00:00<00:01,   22.63it/s]
 26%|##5       | Fitting GeneralizingEstimator : 9/35 [00:00<00:01,   21.37it/s]
 31%|###1      | Fitting GeneralizingEstimator : 11/35 [00:00<00:00,   24.82it/s]
 34%|###4      | Fitting GeneralizingEstimator : 12/35 [00:00<00:00,   23.31it/s]
 40%|####      | Fitting GeneralizingEstimator : 14/35 [00:00<00:00,   25.94it/s]
 43%|####2     | Fitting GeneralizingEstimator : 15/35 [00:00<00:00,   24.61it/s]
 49%|####8     | Fitting GeneralizingEstimator : 17/35 [00:00<00:00,   26.82it/s]
 51%|#####1    | Fitting GeneralizingEstimator : 18/35 [00:00<00:00,   25.54it/s]
 60%|######    | Fitting GeneralizingEstimator : 21/35 [00:00<00:00,   29.43it/s]
 63%|######2   | Fitting GeneralizingEstimator : 22/35 [00:00<00:00,   27.89it/s]
 66%|######5   | Fitting GeneralizingEstimator : 23/35 [00:00<00:00,   27.96it/s]
 69%|######8   | Fitting GeneralizingEstimator : 24/35 [00:00<00:00,   26.60it/s]
 71%|#######1  | Fitting GeneralizingEstimator : 25/35 [00:00<00:00,   26.76it/s]
 74%|#######4  | Fitting GeneralizingEstimator : 26/35 [00:01<00:00,   25.54it/s]
 77%|#######7  | Fitting GeneralizingEstimator : 27/35 [00:01<00:00,   25.74it/s]
 80%|########  | Fitting GeneralizingEstimator : 28/35 [00:01<00:00,   24.76it/s]
 83%|########2 | Fitting GeneralizingEstimator : 29/35 [00:01<00:00,   24.96it/s]
 86%|########5 | Fitting GeneralizingEstimator : 30/35 [00:01<00:00,   24.09it/s]
 89%|########8 | Fitting GeneralizingEstimator : 31/35 [00:01<00:00,   24.26it/s]
 91%|#########1| Fitting GeneralizingEstimator : 32/35 [00:01<00:00,   23.54it/s]
 97%|#########7| Fitting GeneralizingEstimator : 34/35 [00:01<00:00,   25.16it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:01<00:00,   24.27it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:01<00:00,   24.73it/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 : 17/1225 [00:00<00:02,  497.22it/s]
  3%|2         | Scoring GeneralizingEstimator : 35/1225 [00:00<00:02,  513.12it/s]
  5%|4         | Scoring GeneralizingEstimator : 56/1225 [00:00<00:03,  378.00it/s]
  7%|6         | Scoring GeneralizingEstimator : 80/1225 [00:00<00:02,  444.48it/s]
  7%|7         | Scoring GeneralizingEstimator : 91/1225 [00:00<00:03,  363.14it/s]
  9%|8         | Scoring GeneralizingEstimator : 108/1225 [00:00<00:02,  382.01it/s]
  9%|9         | Scoring GeneralizingEstimator : 116/1225 [00:00<00:03,  321.67it/s]
 11%|#1        | Scoring GeneralizingEstimator : 136/1225 [00:00<00:03,  349.85it/s]
 12%|#1        | Scoring GeneralizingEstimator : 145/1225 [00:00<00:03,  314.53it/s]
 14%|#3        | Scoring GeneralizingEstimator : 169/1225 [00:00<00:03,  349.11it/s]
 15%|#4        | Scoring GeneralizingEstimator : 179/1225 [00:00<00:03,  318.87it/s]
 17%|#6        | Scoring GeneralizingEstimator : 203/1225 [00:00<00:02,  348.51it/s]
 18%|#7        | Scoring GeneralizingEstimator : 216/1225 [00:00<00:03,  327.96it/s]
 20%|#9        | Scoring GeneralizingEstimator : 240/1225 [00:00<00:02,  353.88it/s]
 21%|##        | Scoring GeneralizingEstimator : 252/1225 [00:00<00:02,  332.73it/s]
 23%|##2       | Scoring GeneralizingEstimator : 277/1225 [00:00<00:02,  357.77it/s]
 24%|##3       | Scoring GeneralizingEstimator : 289/1225 [00:00<00:02,  337.91it/s]
 26%|##5       | Scoring GeneralizingEstimator : 313/1225 [00:00<00:02,  359.21it/s]
 26%|##6       | Scoring GeneralizingEstimator : 323/1225 [00:00<00:02,  337.24it/s]
 28%|##8       | Scoring GeneralizingEstimator : 347/1225 [00:00<00:02,  357.28it/s]
 29%|##9       | Scoring GeneralizingEstimator : 359/1225 [00:01<00:02,  339.61it/s]
 31%|###1      | Scoring GeneralizingEstimator : 382/1225 [00:01<00:02,  356.85it/s]
 32%|###2      | Scoring GeneralizingEstimator : 393/1225 [00:01<00:02,  338.99it/s]
 34%|###3      | Scoring GeneralizingEstimator : 416/1225 [00:01<00:02,  355.55it/s]
 35%|###4      | Scoring GeneralizingEstimator : 427/1225 [00:01<00:02,  338.34it/s]
 37%|###6      | Scoring GeneralizingEstimator : 448/1225 [00:01<00:02,  351.53it/s]
 37%|###7      | Scoring GeneralizingEstimator : 455/1225 [00:01<00:02,  329.99it/s]
 38%|###8      | Scoring GeneralizingEstimator : 468/1225 [00:01<00:02,  332.44it/s]
 39%|###8      | Scoring GeneralizingEstimator : 475/1225 [00:01<00:02,  313.17it/s]
 40%|####      | Scoring GeneralizingEstimator : 494/1225 [00:01<00:02,  323.97it/s]
 41%|####1     | Scoring GeneralizingEstimator : 504/1225 [00:01<00:02,  309.65it/s]
 42%|####2     | Scoring GeneralizingEstimator : 518/1225 [00:01<00:02,  313.93it/s]
 43%|####2     | Scoring GeneralizingEstimator : 526/1225 [00:01<00:02,  298.54it/s]
 44%|####4     | Scoring GeneralizingEstimator : 539/1225 [00:01<00:02,  302.03it/s]
 44%|####4     | Scoring GeneralizingEstimator : 544/1225 [00:01<00:02,  284.37it/s]
 46%|####5     | Scoring GeneralizingEstimator : 560/1225 [00:01<00:02,  291.74it/s]
 46%|####6     | Scoring GeneralizingEstimator : 565/1225 [00:01<00:02,  275.33it/s]
 48%|####7     | Scoring GeneralizingEstimator : 583/1225 [00:01<00:02,  285.51it/s]
 48%|####8     | Scoring GeneralizingEstimator : 591/1225 [00:01<00:02,  273.10it/s]
 50%|####9     | Scoring GeneralizingEstimator : 608/1225 [00:01<00:02,  282.12it/s]
 50%|#####     | Scoring GeneralizingEstimator : 616/1225 [00:02<00:02,  270.04it/s]
 52%|#####1    | Scoring GeneralizingEstimator : 631/1225 [00:02<00:02,  276.73it/s]
 52%|#####2    | Scoring GeneralizingEstimator : 638/1225 [00:02<00:02,  264.15it/s]
 54%|#####4    | Scoring GeneralizingEstimator : 662/1225 [00:02<00:02,  281.11it/s]
 55%|#####4    | Scoring GeneralizingEstimator : 671/1225 [00:02<00:02,  270.58it/s]
 56%|#####6    | Scoring GeneralizingEstimator : 690/1225 [00:02<00:01,  281.49it/s]
 57%|#####7    | Scoring GeneralizingEstimator : 701/1225 [00:02<00:01,  273.22it/s]
 59%|#####9    | Scoring GeneralizingEstimator : 726/1225 [00:02<00:01,  290.60it/s]
 60%|######    | Scoring GeneralizingEstimator : 735/1225 [00:02<00:01,  279.59it/s]
 62%|######1   | Scoring GeneralizingEstimator : 758/1225 [00:02<00:01,  294.37it/s]
 63%|######2   | Scoring GeneralizingEstimator : 766/1225 [00:02<00:01,  282.15it/s]
 64%|######4   | Scoring GeneralizingEstimator : 788/1225 [00:02<00:01,  295.62it/s]
 65%|######5   | Scoring GeneralizingEstimator : 799/1225 [00:02<00:01,  286.57it/s]
 67%|######6   | Scoring GeneralizingEstimator : 815/1225 [00:02<00:01,  293.36it/s]
 67%|######7   | Scoring GeneralizingEstimator : 822/1225 [00:02<00:01,  280.25it/s]
 68%|######8   | Scoring GeneralizingEstimator : 839/1225 [00:02<00:01,  288.29it/s]
 69%|######9   | Scoring GeneralizingEstimator : 847/1225 [00:02<00:01,  276.69it/s]
 71%|#######   | Scoring GeneralizingEstimator : 864/1225 [00:02<00:01,  284.69it/s]
 71%|#######1  | Scoring GeneralizingEstimator : 873/1225 [00:02<00:01,  274.55it/s]
 73%|#######3  | Scoring GeneralizingEstimator : 897/1225 [00:02<00:01,  290.10it/s]
 74%|#######4  | Scoring GeneralizingEstimator : 907/1225 [00:03<00:01,  280.61it/s]
 76%|#######5  | Scoring GeneralizingEstimator : 928/1225 [00:03<00:01,  292.67it/s]
 77%|#######6  | Scoring GeneralizingEstimator : 940/1225 [00:03<00:01,  284.89it/s]
 79%|#######8  | Scoring GeneralizingEstimator : 965/1225 [00:03<00:00,  300.91it/s]
 80%|#######9  | Scoring GeneralizingEstimator : 975/1225 [00:03<00:00,  290.95it/s]
 81%|########1 | Scoring GeneralizingEstimator : 997/1225 [00:03<00:00,  303.65it/s]
 82%|########2 | Scoring GeneralizingEstimator : 1006/1225 [00:03<00:00,  292.34it/s]
 84%|########4 | Scoring GeneralizingEstimator : 1031/1225 [00:03<00:00,  308.08it/s]
 85%|########5 | Scoring GeneralizingEstimator : 1043/1225 [00:03<00:00,  299.50it/s]
 87%|########6 | Scoring GeneralizingEstimator : 1065/1225 [00:03<00:00,  311.68it/s]
 88%|########7 | Scoring GeneralizingEstimator : 1075/1225 [00:03<00:00,  311.07it/s]
 88%|########7 | Scoring GeneralizingEstimator : 1076/1225 [00:03<00:00,  300.84it/s]
 90%|########9 | Scoring GeneralizingEstimator : 1100/1225 [00:03<00:00,  315.76it/s]
 91%|######### | Scoring GeneralizingEstimator : 1109/1225 [00:03<00:00,  303.94it/s]
 92%|#########2| Scoring GeneralizingEstimator : 1133/1225 [00:03<00:00,  318.74it/s]
 93%|#########3| Scoring GeneralizingEstimator : 1143/1225 [00:03<00:00,  317.88it/s]
 95%|#########5| Scoring GeneralizingEstimator : 1164/1225 [00:03<00:00,  317.85it/s]
 96%|#########6| Scoring GeneralizingEstimator : 1176/1225 [00:03<00:00,  308.36it/s]
 98%|#########7| Scoring GeneralizingEstimator : 1200/1225 [00:03<00:00,  322.77it/s]
 99%|#########8| Scoring GeneralizingEstimator : 1210/1225 [00:03<00:00,  310.95it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00,  320.16it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00,  308.76it/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 11.272 seconds)

Estimated memory usage: 129 MB

Gallery generated by Sphinx-Gallery