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.
Current compensation grade : 0
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,   29.03it/s]
  6%|5         | Fitting GeneralizingEstimator : 2/35 [00:00<00:01,   28.84it/s]
  9%|8         | Fitting GeneralizingEstimator : 3/35 [00:00<00:01,   28.65it/s]
 14%|#4        | Fitting GeneralizingEstimator : 5/35 [00:00<00:01,   29.13it/s]
 20%|##        | Fitting GeneralizingEstimator : 7/35 [00:00<00:00,   29.89it/s]
 23%|##2       | Fitting GeneralizingEstimator : 8/35 [00:00<00:00,   29.88it/s]
 29%|##8       | Fitting GeneralizingEstimator : 10/35 [00:00<00:00,   30.62it/s]
 34%|###4      | Fitting GeneralizingEstimator : 12/35 [00:00<00:00,   31.38it/s]
 37%|###7      | Fitting GeneralizingEstimator : 13/35 [00:00<00:00,   31.20it/s]
 49%|####8     | Fitting GeneralizingEstimator : 17/35 [00:00<00:00,   32.28it/s]
 51%|#####1    | Fitting GeneralizingEstimator : 18/35 [00:00<00:00,   32.10it/s]
 60%|######    | Fitting GeneralizingEstimator : 21/35 [00:00<00:00,   32.93it/s]
 63%|######2   | Fitting GeneralizingEstimator : 22/35 [00:00<00:00,   31.96it/s]
 71%|#######1  | Fitting GeneralizingEstimator : 25/35 [00:00<00:00,   32.91it/s]
 77%|#######7  | Fitting GeneralizingEstimator : 27/35 [00:00<00:00,   33.05it/s]
 83%|########2 | Fitting GeneralizingEstimator : 29/35 [00:00<00:00,   33.43it/s]
 86%|########5 | Fitting GeneralizingEstimator : 30/35 [00:00<00:00,   32.99it/s]
 91%|#########1| Fitting GeneralizingEstimator : 32/35 [00:00<00:00,   33.74it/s]
 97%|#########7| Fitting GeneralizingEstimator : 34/35 [00:00<00:00,   34.07it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   45.70it/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 : 16/1225 [00:00<00:02,  467.35it/s]
  2%|2         | Scoring GeneralizingEstimator : 26/1225 [00:00<00:02,  454.00it/s]
  4%|3         | Scoring GeneralizingEstimator : 46/1225 [00:00<00:02,  459.26it/s]
  5%|5         | Scoring GeneralizingEstimator : 66/1225 [00:00<00:02,  464.47it/s]
  6%|6         | Scoring GeneralizingEstimator : 76/1225 [00:00<00:02,  451.35it/s]
  8%|7         | Scoring GeneralizingEstimator : 97/1225 [00:00<00:02,  457.40it/s]
 10%|9         | Scoring GeneralizingEstimator : 117/1225 [00:00<00:02,  462.67it/s]
 10%|#         | Scoring GeneralizingEstimator : 127/1225 [00:00<00:02,  449.88it/s]
 12%|#2        | Scoring GeneralizingEstimator : 148/1225 [00:00<00:02,  456.14it/s]
 14%|#3        | Scoring GeneralizingEstimator : 167/1225 [00:00<00:02,  460.51it/s]
 14%|#4        | Scoring GeneralizingEstimator : 177/1225 [00:00<00:02,  447.87it/s]
 16%|#6        | Scoring GeneralizingEstimator : 199/1225 [00:00<00:02,  454.91it/s]
 18%|#7        | Scoring GeneralizingEstimator : 217/1225 [00:00<00:02,  458.27it/s]
 19%|#8        | Scoring GeneralizingEstimator : 227/1225 [00:00<00:02,  445.94it/s]
 20%|##        | Scoring GeneralizingEstimator : 249/1225 [00:00<00:02,  452.99it/s]
 21%|##1       | Scoring GeneralizingEstimator : 261/1225 [00:00<00:02,  446.79it/s]
 22%|##2       | Scoring GeneralizingEstimator : 270/1225 [00:00<00:02,  431.97it/s]
 24%|##3       | Scoring GeneralizingEstimator : 291/1225 [00:00<00:02,  438.58it/s]
 25%|##4       | Scoring GeneralizingEstimator : 306/1225 [00:00<00:02,  438.81it/s]
 26%|##5       | Scoring GeneralizingEstimator : 315/1225 [00:00<00:02,  424.88it/s]
 28%|##7       | Scoring GeneralizingEstimator : 338/1225 [00:00<00:02,  432.96it/s]
 29%|##8       | Scoring GeneralizingEstimator : 352/1225 [00:00<00:02,  431.90it/s]
 30%|##9       | Scoring GeneralizingEstimator : 362/1225 [00:00<00:02,  422.16it/s]
 31%|###1      | Scoring GeneralizingEstimator : 385/1225 [00:00<00:01,  430.33it/s]
 32%|###2      | Scoring GeneralizingEstimator : 398/1225 [00:00<00:01,  427.78it/s]
 33%|###3      | Scoring GeneralizingEstimator : 408/1225 [00:00<00:01,  418.13it/s]
 35%|###4      | Scoring GeneralizingEstimator : 428/1225 [00:00<00:01,  424.29it/s]
 36%|###5      | Scoring GeneralizingEstimator : 439/1225 [00:00<00:01,  417.91it/s]
 37%|###6      | Scoring GeneralizingEstimator : 449/1225 [00:00<00:01,  409.35it/s]
 38%|###8      | Scoring GeneralizingEstimator : 469/1225 [00:01<00:01,  415.70it/s]
 39%|###9      | Scoring GeneralizingEstimator : 480/1225 [00:01<00:01,  409.41it/s]
 40%|####      | Scoring GeneralizingEstimator : 491/1225 [00:01<00:01,  403.91it/s]
 42%|####2     | Scoring GeneralizingEstimator : 515/1225 [00:01<00:01,  412.76it/s]
 43%|####2     | Scoring GeneralizingEstimator : 526/1225 [00:01<00:01,  407.34it/s]
 44%|####4     | Scoring GeneralizingEstimator : 539/1225 [00:01<00:01,  406.08it/s]
 46%|####6     | Scoring GeneralizingEstimator : 564/1225 [00:01<00:01,  415.48it/s]
 47%|####6     | Scoring GeneralizingEstimator : 575/1225 [00:01<00:01,  409.81it/s]
 48%|####8     | Scoring GeneralizingEstimator : 590/1225 [00:01<00:01,  411.35it/s]
 50%|#####     | Scoring GeneralizingEstimator : 616/1225 [00:01<00:01,  421.16it/s]
 51%|#####1    | Scoring GeneralizingEstimator : 626/1225 [00:01<00:01,  412.19it/s]
 52%|#####2    | Scoring GeneralizingEstimator : 641/1225 [00:01<00:01,  413.64it/s]
 54%|#####4    | Scoring GeneralizingEstimator : 666/1225 [00:01<00:01,  422.91it/s]
 55%|#####5    | Scoring GeneralizingEstimator : 676/1225 [00:01<00:01,  413.80it/s]
 56%|#####6    | Scoring GeneralizingEstimator : 692/1225 [00:01<00:01,  416.40it/s]
 58%|#####8    | Scoring GeneralizingEstimator : 716/1225 [00:01<00:01,  425.13it/s]
 59%|#####9    | Scoring GeneralizingEstimator : 726/1225 [00:01<00:01,  415.84it/s]
 61%|######    | Scoring GeneralizingEstimator : 743/1225 [00:01<00:01,  419.43it/s]
 63%|######2   | Scoring GeneralizingEstimator : 767/1225 [00:01<00:01,  428.04it/s]
 63%|######3   | Scoring GeneralizingEstimator : 777/1225 [00:01<00:01,  418.57it/s]
 65%|######4   | Scoring GeneralizingEstimator : 795/1225 [00:01<00:01,  423.00it/s]
 67%|######6   | Scoring GeneralizingEstimator : 818/1225 [00:01<00:00,  431.18it/s]
 68%|######7   | Scoring GeneralizingEstimator : 828/1225 [00:01<00:00,  421.43it/s]
 69%|######9   | Scoring GeneralizingEstimator : 846/1225 [00:01<00:00,  425.75it/s]
 71%|#######   | Scoring GeneralizingEstimator : 868/1225 [00:01<00:00,  433.23it/s]
 72%|#######1  | Scoring GeneralizingEstimator : 878/1225 [00:01<00:00,  423.03it/s]
 73%|#######3  | Scoring GeneralizingEstimator : 897/1225 [00:01<00:00,  428.18it/s]
 75%|#######4  | Scoring GeneralizingEstimator : 918/1225 [00:01<00:00,  434.81it/s]
 76%|#######5  | Scoring GeneralizingEstimator : 929/1225 [00:01<00:00,  427.38it/s]
 77%|#######7  | Scoring GeneralizingEstimator : 949/1225 [00:02<00:00,  433.37it/s]
 79%|#######9  | Scoring GeneralizingEstimator : 969/1225 [00:02<00:00,  439.26it/s]
 80%|#######9  | Scoring GeneralizingEstimator : 979/1225 [00:02<00:00,  428.73it/s]
 82%|########1 | Scoring GeneralizingEstimator : 1000/1225 [00:02<00:00,  435.43it/s]
 83%|########3 | Scoring GeneralizingEstimator : 1019/1225 [00:02<00:00,  439.95it/s]
 84%|########4 | Scoring GeneralizingEstimator : 1029/1225 [00:02<00:00,  429.38it/s]
 86%|########5 | Scoring GeneralizingEstimator : 1051/1225 [00:02<00:00,  436.59it/s]
 87%|########7 | Scoring GeneralizingEstimator : 1069/1225 [00:02<00:00,  440.47it/s]
 88%|########8 | Scoring GeneralizingEstimator : 1079/1225 [00:02<00:00,  429.39it/s]
 90%|########9 | Scoring GeneralizingEstimator : 1101/1225 [00:02<00:00,  436.72it/s]
 91%|#########1| Scoring GeneralizingEstimator : 1117/1225 [00:02<00:00,  438.37it/s]
 92%|#########2| Scoring GeneralizingEstimator : 1127/1225 [00:02<00:00,  427.93it/s]
 94%|#########4| Scoring GeneralizingEstimator : 1152/1225 [00:02<00:00,  436.98it/s]
 95%|#########5| Scoring GeneralizingEstimator : 1168/1225 [00:02<00:00,  438.68it/s]
 96%|#########6| Scoring GeneralizingEstimator : 1179/1225 [00:02<00:00,  430.83it/s]
 98%|#########8| Scoring GeneralizingEstimator : 1204/1225 [00:02<00:00,  439.86it/s]
100%|#########9| Scoring GeneralizingEstimator : 1219/1225 [00:02<00:00,  439.49it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  433.13it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  477.13it/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 8.368 seconds)

Estimated memory usage: 128 MB

Gallery generated by Sphinx-Gallery