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.22it/s]
  6%|5         | Fitting GeneralizingEstimator : 2/35 [00:00<00:01,   29.36it/s]
 14%|#4        | Fitting GeneralizingEstimator : 5/35 [00:00<00:00,   60.18it/s]
 17%|#7        | Fitting GeneralizingEstimator : 6/35 [00:00<00:01,   22.20it/s]
 26%|##5       | Fitting GeneralizingEstimator : 9/35 [00:00<00:00,   68.26it/s]
 34%|###4      | Fitting GeneralizingEstimator : 12/35 [00:00<00:00,   78.86it/s]
 43%|####2     | Fitting GeneralizingEstimator : 15/35 [00:00<00:00,   87.59it/s]
 49%|####8     | Fitting GeneralizingEstimator : 17/35 [00:00<00:00,   60.79it/s]
 57%|#####7    | Fitting GeneralizingEstimator : 20/35 [00:00<00:00,   68.92it/s]
 69%|######8   | Fitting GeneralizingEstimator : 24/35 [00:00<00:00,   91.49it/s]
 77%|#######7  | Fitting GeneralizingEstimator : 27/35 [00:00<00:00,   68.43it/s]
 83%|########2 | Fitting GeneralizingEstimator : 29/35 [00:00<00:00,   58.85it/s]
 91%|#########1| Fitting GeneralizingEstimator : 32/35 [00:00<00:00,   69.70it/s]
 97%|#########7| Fitting GeneralizingEstimator : 34/35 [00:00<00:00,   49.83it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   63.80it/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.48it/s]
  3%|2         | Scoring GeneralizingEstimator : 33/1225 [00:00<00:02,  450.30it/s]
  4%|3         | Scoring GeneralizingEstimator : 48/1225 [00:00<00:02,  443.68it/s]
  5%|5         | Scoring GeneralizingEstimator : 64/1225 [00:00<00:02,  468.99it/s]
  6%|6         | Scoring GeneralizingEstimator : 78/1225 [00:00<00:02,  420.01it/s]
  8%|7         | Scoring GeneralizingEstimator : 94/1225 [00:00<00:02,  467.26it/s]
 10%|9         | Scoring GeneralizingEstimator : 118/1225 [00:00<00:01,  675.36it/s]
 12%|#1        | Scoring GeneralizingEstimator : 141/1225 [00:00<00:01,  678.42it/s]
 13%|#3        | Scoring GeneralizingEstimator : 162/1225 [00:00<00:01,  626.37it/s]
 15%|#5        | Scoring GeneralizingEstimator : 184/1225 [00:00<00:01,  648.76it/s]
 17%|#6        | Scoring GeneralizingEstimator : 206/1225 [00:00<00:01,  649.64it/s]
 19%|#8        | Scoring GeneralizingEstimator : 229/1225 [00:00<00:01,  675.78it/s]
 21%|##        | Scoring GeneralizingEstimator : 253/1225 [00:00<00:01,  702.26it/s]
 23%|##2       | Scoring GeneralizingEstimator : 276/1225 [00:00<00:01,  680.14it/s]
 24%|##4       | Scoring GeneralizingEstimator : 299/1225 [00:00<00:01,  679.23it/s]
 26%|##6       | Scoring GeneralizingEstimator : 322/1225 [00:00<00:01,  681.52it/s]
 28%|##8       | Scoring GeneralizingEstimator : 345/1225 [00:00<00:01,  681.79it/s]
 30%|##9       | Scoring GeneralizingEstimator : 367/1225 [00:00<00:01,  651.65it/s]
 32%|###1      | Scoring GeneralizingEstimator : 390/1225 [00:00<00:01,  676.26it/s]
 34%|###3      | Scoring GeneralizingEstimator : 414/1225 [00:00<00:01,  706.95it/s]
 36%|###5      | Scoring GeneralizingEstimator : 438/1225 [00:00<00:01,  708.75it/s]
 38%|###7      | Scoring GeneralizingEstimator : 462/1225 [00:00<00:01,  709.33it/s]
 40%|###9      | Scoring GeneralizingEstimator : 486/1225 [00:00<00:01,  710.24it/s]
 42%|####1     | Scoring GeneralizingEstimator : 511/1225 [00:00<00:00,  738.04it/s]
 44%|####3     | Scoring GeneralizingEstimator : 535/1225 [00:00<00:00,  712.09it/s]
 46%|####5     | Scoring GeneralizingEstimator : 559/1225 [00:00<00:00,  710.39it/s]
 48%|####7     | Scoring GeneralizingEstimator : 583/1225 [00:00<00:00,  710.28it/s]
 50%|####9     | Scoring GeneralizingEstimator : 608/1225 [00:00<00:00,  737.01it/s]
 52%|#####1    | Scoring GeneralizingEstimator : 632/1225 [00:00<00:00,  713.62it/s]
 54%|#####3    | Scoring GeneralizingEstimator : 656/1225 [00:01<00:00,  708.81it/s]
 56%|#####5    | Scoring GeneralizingEstimator : 681/1225 [00:01<00:00,  736.76it/s]
 58%|#####7    | Scoring GeneralizingEstimator : 705/1225 [00:01<00:00,  712.86it/s]
 60%|#####9    | Scoring GeneralizingEstimator : 729/1225 [00:01<00:00,  708.89it/s]
 61%|######1   | Scoring GeneralizingEstimator : 753/1225 [00:01<00:00,  709.70it/s]
 64%|######3   | Scoring GeneralizingEstimator : 778/1225 [00:01<00:00,  737.61it/s]
 65%|######5   | Scoring GeneralizingEstimator : 802/1225 [00:01<00:00,  713.36it/s]
 67%|######7   | Scoring GeneralizingEstimator : 826/1225 [00:01<00:00,  709.34it/s]
 69%|######9   | Scoring GeneralizingEstimator : 851/1225 [00:01<00:00,  734.12it/s]
 71%|#######1  | Scoring GeneralizingEstimator : 875/1225 [00:01<00:00,  706.90it/s]
 73%|#######3  | Scoring GeneralizingEstimator : 899/1225 [00:01<00:00,  710.49it/s]
 75%|#######5  | Scoring GeneralizingEstimator : 924/1225 [00:01<00:00,  718.18it/s]
 77%|#######7  | Scoring GeneralizingEstimator : 948/1225 [00:01<00:00,  708.54it/s]
 79%|#######9  | Scoring GeneralizingEstimator : 972/1225 [00:01<00:00,  705.67it/s]
 81%|########1 | Scoring GeneralizingEstimator : 997/1225 [00:01<00:00,  737.47it/s]
 83%|########3 | Scoring GeneralizingEstimator : 1021/1225 [00:01<00:00,  713.24it/s]
 85%|########5 | Scoring GeneralizingEstimator : 1045/1225 [00:01<00:00,  707.39it/s]
 87%|########7 | Scoring GeneralizingEstimator : 1070/1225 [00:01<00:00,  735.22it/s]
 89%|########9 | Scoring GeneralizingEstimator : 1094/1225 [00:01<00:00,  712.28it/s]
 91%|#########1| Scoring GeneralizingEstimator : 1119/1225 [00:01<00:00,  732.96it/s]
 93%|#########3| Scoring GeneralizingEstimator : 1143/1225 [00:01<00:00,  712.38it/s]
 95%|#########5| Scoring GeneralizingEstimator : 1167/1225 [00:01<00:00,  710.19it/s]
 97%|#########7| Scoring GeneralizingEstimator : 1191/1225 [00:01<00:00,  709.71it/s]
 99%|#########9| Scoring GeneralizingEstimator : 1215/1225 [00:01<00:00,  711.20it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:01<00:00,  677.44it/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()
../../_images/sphx_glr_plot_decoding_time_generalization_conditions_001.png

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

Estimated memory usage: 8 MB

Gallery generated by Sphinx-Gallery