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.

References

1

King & Dehaene (2014) ‘Characterizing the dynamics of mental representations: the Temporal Generalization method’, Trends In Cognitive Sciences, 18(4), 203-210. doi: 10.1016/j.tics.2014.01.002.

# 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,   20.99it/s]
  9%|8         | Fitting GeneralizingEstimator : 3/35 [00:00<00:01,   21.58it/s]
 14%|#4        | Fitting GeneralizingEstimator : 5/35 [00:00<00:01,   22.16it/s]
 23%|##2       | Fitting GeneralizingEstimator : 8/35 [00:00<00:01,   22.93it/s]
 26%|##5       | Fitting GeneralizingEstimator : 9/35 [00:00<00:01,   22.95it/s]
 34%|###4      | Fitting GeneralizingEstimator : 12/35 [00:00<00:00,   23.83it/s]
 43%|####2     | Fitting GeneralizingEstimator : 15/35 [00:00<00:00,   24.68it/s]
 51%|#####1    | Fitting GeneralizingEstimator : 18/35 [00:00<00:00,   25.50it/s]
 57%|#####7    | Fitting GeneralizingEstimator : 20/35 [00:00<00:00,   26.25it/s]
 63%|######2   | Fitting GeneralizingEstimator : 22/35 [00:00<00:00,   26.99it/s]
 69%|######8   | Fitting GeneralizingEstimator : 24/35 [00:00<00:00,   27.74it/s]
 74%|#######4  | Fitting GeneralizingEstimator : 26/35 [00:00<00:00,   28.49it/s]
 77%|#######7  | Fitting GeneralizingEstimator : 27/35 [00:00<00:00,   28.33it/s]
 83%|########2 | Fitting GeneralizingEstimator : 29/35 [00:00<00:00,   29.04it/s]
 91%|#########1| Fitting GeneralizingEstimator : 32/35 [00:00<00:00,   29.92it/s]
 97%|#########7| Fitting GeneralizingEstimator : 34/35 [00:00<00:00,   30.68it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   31.37it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   54.23it/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%|          | Scoring GeneralizingEstimator : 10/1225 [00:00<00:04,  286.46it/s]
  2%|2         | Scoring GeneralizingEstimator : 27/1225 [00:00<00:04,  292.73it/s]
  4%|3         | Scoring GeneralizingEstimator : 43/1225 [00:00<00:03,  298.43it/s]
  5%|4         | Scoring GeneralizingEstimator : 61/1225 [00:00<00:03,  305.13it/s]
  6%|6         | Scoring GeneralizingEstimator : 75/1225 [00:00<00:03,  309.20it/s]
  8%|7         | Scoring GeneralizingEstimator : 95/1225 [00:00<00:03,  316.71it/s]
  9%|9         | Scoring GeneralizingEstimator : 115/1225 [00:00<00:03,  324.22it/s]
 11%|#1        | Scoring GeneralizingEstimator : 135/1225 [00:00<00:03,  331.72it/s]
 13%|#2        | Scoring GeneralizingEstimator : 156/1225 [00:00<00:03,  339.65it/s]
 14%|#4        | Scoring GeneralizingEstimator : 174/1225 [00:00<00:03,  345.87it/s]
 16%|#5        | Scoring GeneralizingEstimator : 193/1225 [00:00<00:02,  352.67it/s]
 17%|#7        | Scoring GeneralizingEstimator : 214/1225 [00:00<00:02,  360.33it/s]
 19%|#9        | Scoring GeneralizingEstimator : 235/1225 [00:00<00:02,  368.01it/s]
 21%|##        | Scoring GeneralizingEstimator : 256/1225 [00:00<00:02,  375.67it/s]
 22%|##2       | Scoring GeneralizingEstimator : 275/1225 [00:00<00:02,  382.03it/s]
 24%|##4       | Scoring GeneralizingEstimator : 295/1225 [00:00<00:02,  388.89it/s]
 26%|##5       | Scoring GeneralizingEstimator : 315/1225 [00:00<00:02,  395.62it/s]
 27%|##7       | Scoring GeneralizingEstimator : 336/1225 [00:00<00:02,  402.91it/s]
 29%|##9       | Scoring GeneralizingEstimator : 358/1225 [00:00<00:02,  410.74it/s]
 31%|###       | Scoring GeneralizingEstimator : 379/1225 [00:00<00:02,  417.85it/s]
 33%|###2      | Scoring GeneralizingEstimator : 400/1225 [00:00<00:01,  424.76it/s]
 34%|###4      | Scoring GeneralizingEstimator : 421/1225 [00:00<00:01,  431.61it/s]
 36%|###6      | Scoring GeneralizingEstimator : 442/1225 [00:00<00:01,  438.30it/s]
 38%|###7      | Scoring GeneralizingEstimator : 463/1225 [00:00<00:01,  444.80it/s]
 39%|###9      | Scoring GeneralizingEstimator : 483/1225 [00:00<00:01,  450.35it/s]
 41%|####      | Scoring GeneralizingEstimator : 502/1225 [00:00<00:01,  454.88it/s]
 43%|####2     | Scoring GeneralizingEstimator : 521/1225 [00:00<00:01,  459.28it/s]
 44%|####4     | Scoring GeneralizingEstimator : 541/1225 [00:00<00:01,  464.48it/s]
 46%|####5     | Scoring GeneralizingEstimator : 558/1225 [00:00<00:01,  466.25it/s]
 47%|####7     | Scoring GeneralizingEstimator : 576/1225 [00:01<00:01,  469.01it/s]
 49%|####8     | Scoring GeneralizingEstimator : 597/1225 [00:01<00:01,  474.78it/s]
 50%|#####     | Scoring GeneralizingEstimator : 618/1225 [00:01<00:01,  480.45it/s]
 52%|#####2    | Scoring GeneralizingEstimator : 640/1225 [00:01<00:01,  486.83it/s]
 54%|#####3    | Scoring GeneralizingEstimator : 659/1225 [00:01<00:01,  490.07it/s]
 55%|#####5    | Scoring GeneralizingEstimator : 679/1225 [00:01<00:01,  494.06it/s]
 57%|#####6    | Scoring GeneralizingEstimator : 696/1225 [00:01<00:01,  494.51it/s]
 58%|#####7    | Scoring GeneralizingEstimator : 710/1225 [00:01<00:01,  489.66it/s]
 60%|#####9    | Scoring GeneralizingEstimator : 730/1225 [00:01<00:01,  493.84it/s]
 61%|######1   | Scoring GeneralizingEstimator : 751/1225 [00:01<00:00,  498.84it/s]
 63%|######3   | Scoring GeneralizingEstimator : 774/1225 [00:01<00:00,  505.60it/s]
 65%|######4   | Scoring GeneralizingEstimator : 796/1225 [00:01<00:00,  511.22it/s]
 67%|######6   | Scoring GeneralizingEstimator : 819/1225 [00:01<00:00,  517.55it/s]
 69%|######8   | Scoring GeneralizingEstimator : 840/1225 [00:01<00:00,  521.79it/s]
 70%|#######   | Scoring GeneralizingEstimator : 859/1225 [00:01<00:00,  523.54it/s]
 72%|#######1  | Scoring GeneralizingEstimator : 880/1225 [00:01<00:00,  527.49it/s]
 74%|#######3  | Scoring GeneralizingEstimator : 901/1225 [00:01<00:00,  531.39it/s]
 75%|#######5  | Scoring GeneralizingEstimator : 922/1225 [00:01<00:00,  535.21it/s]
 77%|#######7  | Scoring GeneralizingEstimator : 944/1225 [00:01<00:00,  539.78it/s]
 79%|#######8  | Scoring GeneralizingEstimator : 965/1225 [00:01<00:00,  543.34it/s]
 80%|########  | Scoring GeneralizingEstimator : 986/1225 [00:01<00:00,  546.78it/s]
 82%|########2 | Scoring GeneralizingEstimator : 1007/1225 [00:01<00:00,  550.11it/s]
 84%|########3 | Scoring GeneralizingEstimator : 1028/1225 [00:01<00:00,  553.17it/s]
 86%|########5 | Scoring GeneralizingEstimator : 1050/1225 [00:01<00:00,  557.39it/s]
 87%|########7 | Scoring GeneralizingEstimator : 1071/1225 [00:01<00:00,  560.13it/s]
 89%|########9 | Scoring GeneralizingEstimator : 1092/1225 [00:01<00:00,  562.92it/s]
 91%|######### | Scoring GeneralizingEstimator : 1110/1225 [00:01<00:00,  561.02it/s]
 92%|#########2| Scoring GeneralizingEstimator : 1129/1225 [00:01<00:00,  560.87it/s]
 94%|#########3| Scoring GeneralizingEstimator : 1150/1225 [00:01<00:00,  563.45it/s]
 96%|#########5| Scoring GeneralizingEstimator : 1171/1225 [00:02<00:00,  566.01it/s]
 97%|#########7| Scoring GeneralizingEstimator : 1192/1225 [00:02<00:00,  567.97it/s]
 99%|#########9| Scoring GeneralizingEstimator : 1213/1225 [00:02<00:00,  570.28it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  571.07it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  586.00it/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

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

Estimated memory usage: 128 MB

Gallery generated by Sphinx-Gallery