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
meg_path = data_path / 'MEG' / 'sample'
raw_fname = meg_path / 'sample_audvis_filt-0-40_raw.fif'
events_fname = meg_path / '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')
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)

[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
[Parallel(n_jobs=1)]: Done   1 out of   1 | elapsed:    0.0s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   2 out of   2 | elapsed:    0.0s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   3 out of   3 | elapsed:    0.0s remaining:    0.0s
[Parallel(n_jobs=1)]: Done   4 out of   4 | elapsed:    0.0s remaining:    0.0s
[Parallel(n_jobs=1)]: Done 366 out of 366 | elapsed:    0.7s finished

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='liblinear')  # liblinear is faster than lbfgs
)
time_gen = GeneralizingEstimator(clf, scoring='roc_auc', n_jobs=None,
                                 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)
  0%|          | Fitting GeneralizingEstimator : 0/35 [00:00<?,       ?it/s]
  6%|5         | Fitting GeneralizingEstimator : 2/35 [00:00<00:00,   58.42it/s]
 11%|#1        | Fitting GeneralizingEstimator : 4/35 [00:00<00:00,   58.79it/s]
 17%|#7        | Fitting GeneralizingEstimator : 6/35 [00:00<00:00,   58.93it/s]
 23%|##2       | Fitting GeneralizingEstimator : 8/35 [00:00<00:00,   58.94it/s]
 29%|##8       | Fitting GeneralizingEstimator : 10/35 [00:00<00:00,   58.94it/s]
 37%|###7      | Fitting GeneralizingEstimator : 13/35 [00:00<00:00,   64.51it/s]
 46%|####5     | Fitting GeneralizingEstimator : 16/35 [00:00<00:00,   68.47it/s]
 51%|#####1    | Fitting GeneralizingEstimator : 18/35 [00:00<00:00,   67.09it/s]
 60%|######    | Fitting GeneralizingEstimator : 21/35 [00:00<00:00,   69.84it/s]
 66%|######5   | Fitting GeneralizingEstimator : 23/35 [00:00<00:00,   68.50it/s]
 71%|#######1  | Fitting GeneralizingEstimator : 25/35 [00:00<00:00,   67.41it/s]
 74%|#######4  | Fitting GeneralizingEstimator : 26/35 [00:00<00:00,   63.28it/s]
 83%|########2 | Fitting GeneralizingEstimator : 29/35 [00:00<00:00,   65.87it/s]
 89%|########8 | Fitting GeneralizingEstimator : 31/35 [00:00<00:00,   65.21it/s]
 97%|#########7| Fitting GeneralizingEstimator : 34/35 [00:00<00:00,   67.38it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   67.29it/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)
  0%|          | Scoring GeneralizingEstimator : 0/1225 [00:00<?,       ?it/s]
  1%|          | Scoring GeneralizingEstimator : 11/1225 [00:00<00:04,  272.10it/s]
  2%|1         | Scoring GeneralizingEstimator : 21/1225 [00:00<00:04,  282.54it/s]
  3%|2         | Scoring GeneralizingEstimator : 36/1225 [00:00<00:03,  335.80it/s]
  4%|3         | Scoring GeneralizingEstimator : 48/1225 [00:00<00:03,  340.25it/s]
  5%|4         | Scoring GeneralizingEstimator : 59/1225 [00:00<00:03,  336.89it/s]
  6%|5         | Scoring GeneralizingEstimator : 72/1225 [00:00<00:03,  345.56it/s]
  7%|6         | Scoring GeneralizingEstimator : 85/1225 [00:00<00:03,  351.78it/s]
  8%|8         | Scoring GeneralizingEstimator : 98/1225 [00:00<00:03,  356.65it/s]
  9%|9         | Scoring GeneralizingEstimator : 111/1225 [00:00<00:03,  360.10it/s]
 10%|#         | Scoring GeneralizingEstimator : 125/1225 [00:00<00:03,  366.52it/s]
 11%|#1        | Scoring GeneralizingEstimator : 138/1225 [00:00<00:02,  368.38it/s]
 12%|#2        | Scoring GeneralizingEstimator : 152/1225 [00:00<00:02,  373.32it/s]
 13%|#3        | Scoring GeneralizingEstimator : 165/1225 [00:00<00:02,  374.42it/s]
 15%|#4        | Scoring GeneralizingEstimator : 178/1225 [00:00<00:02,  375.28it/s]
 16%|#5        | Scoring GeneralizingEstimator : 190/1225 [00:00<00:02,  373.39it/s]
 16%|#6        | Scoring GeneralizingEstimator : 201/1225 [00:00<00:02,  369.06it/s]
 17%|#7        | Scoring GeneralizingEstimator : 213/1225 [00:00<00:02,  367.77it/s]
 18%|#8        | Scoring GeneralizingEstimator : 225/1225 [00:00<00:02,  366.43it/s]
 19%|#9        | Scoring GeneralizingEstimator : 237/1225 [00:00<00:02,  365.54it/s]
 20%|##        | Scoring GeneralizingEstimator : 248/1225 [00:00<00:02,  362.37it/s]
 21%|##1       | Scoring GeneralizingEstimator : 259/1225 [00:00<00:02,  356.75it/s]
 22%|##1       | Scoring GeneralizingEstimator : 269/1225 [00:00<00:02,  352.17it/s]
 23%|##3       | Scoring GeneralizingEstimator : 283/1225 [00:00<00:02,  356.51it/s]
 24%|##4       | Scoring GeneralizingEstimator : 296/1225 [00:00<00:02,  358.38it/s]
 25%|##5       | Scoring GeneralizingEstimator : 308/1225 [00:00<00:02,  358.05it/s]
 26%|##6       | Scoring GeneralizingEstimator : 324/1225 [00:00<00:02,  365.59it/s]
 28%|##7       | Scoring GeneralizingEstimator : 339/1225 [00:00<00:02,  370.67it/s]
 29%|##8       | Scoring GeneralizingEstimator : 352/1225 [00:00<00:02,  371.22it/s]
 30%|##9       | Scoring GeneralizingEstimator : 367/1225 [00:00<00:02,  375.79it/s]
 31%|###1      | Scoring GeneralizingEstimator : 383/1225 [00:01<00:02,  381.74it/s]
 33%|###2      | Scoring GeneralizingEstimator : 401/1225 [00:01<00:02,  391.15it/s]
 34%|###4      | Scoring GeneralizingEstimator : 420/1225 [00:01<00:02,  401.70it/s]
 36%|###5      | Scoring GeneralizingEstimator : 439/1225 [00:01<00:01,  411.48it/s]
 37%|###7      | Scoring GeneralizingEstimator : 456/1225 [00:01<00:01,  416.97it/s]
 39%|###8      | Scoring GeneralizingEstimator : 472/1225 [00:01<00:01,  420.36it/s]
 40%|####      | Scoring GeneralizingEstimator : 490/1225 [00:01<00:01,  426.96it/s]
 41%|####1     | Scoring GeneralizingEstimator : 506/1225 [00:01<00:01,  429.52it/s]
 43%|####2     | Scoring GeneralizingEstimator : 523/1225 [00:01<00:01,  433.76it/s]
 44%|####4     | Scoring GeneralizingEstimator : 539/1225 [00:01<00:01,  435.99it/s]
 45%|####5     | Scoring GeneralizingEstimator : 553/1225 [00:01<00:01,  434.68it/s]
 46%|####6     | Scoring GeneralizingEstimator : 567/1225 [00:01<00:01,  433.51it/s]
 47%|####7     | Scoring GeneralizingEstimator : 581/1225 [00:01<00:01,  432.28it/s]
 48%|####8     | Scoring GeneralizingEstimator : 593/1225 [00:01<00:01,  427.88it/s]
 49%|####9     | Scoring GeneralizingEstimator : 606/1225 [00:01<00:01,  425.39it/s]
 51%|#####     | Scoring GeneralizingEstimator : 622/1225 [00:01<00:01,  428.01it/s]
 52%|#####1    | Scoring GeneralizingEstimator : 636/1225 [00:01<00:01,  427.21it/s]
 53%|#####3    | Scoring GeneralizingEstimator : 650/1225 [00:01<00:01,  426.44it/s]
 54%|#####4    | Scoring GeneralizingEstimator : 664/1225 [00:01<00:01,  425.79it/s]
 55%|#####5    | Scoring GeneralizingEstimator : 679/1225 [00:01<00:01,  426.73it/s]
 57%|#####6    | Scoring GeneralizingEstimator : 695/1225 [00:01<00:01,  429.28it/s]
 58%|#####7    | Scoring GeneralizingEstimator : 710/1225 [00:01<00:01,  429.94it/s]
 59%|#####9    | Scoring GeneralizingEstimator : 725/1225 [00:01<00:01,  430.62it/s]
 60%|######    | Scoring GeneralizingEstimator : 740/1225 [00:01<00:01,  431.11it/s]
 62%|######1   | Scoring GeneralizingEstimator : 756/1225 [00:01<00:01,  433.28it/s]
 63%|######2   | Scoring GeneralizingEstimator : 770/1225 [00:01<00:01,  432.17it/s]
 64%|######3   | Scoring GeneralizingEstimator : 783/1225 [00:01<00:01,  428.86it/s]
 65%|######5   | Scoring GeneralizingEstimator : 797/1225 [00:01<00:00,  428.11it/s]
 66%|######6   | Scoring GeneralizingEstimator : 811/1225 [00:01<00:00,  427.41it/s]
 67%|######7   | Scoring GeneralizingEstimator : 823/1225 [00:02<00:00,  423.56it/s]
 69%|######8   | Scoring GeneralizingEstimator : 840/1225 [00:02<00:00,  427.67it/s]
 70%|#######   | Scoring GeneralizingEstimator : 859/1225 [00:02<00:00,  434.60it/s]
 72%|#######1  | Scoring GeneralizingEstimator : 879/1225 [00:02<00:00,  442.70it/s]
 73%|#######3  | Scoring GeneralizingEstimator : 898/1225 [00:02<00:00,  448.81it/s]
 75%|#######4  | Scoring GeneralizingEstimator : 918/1225 [00:02<00:00,  456.21it/s]
 76%|#######6  | Scoring GeneralizingEstimator : 937/1225 [00:02<00:00,  461.56it/s]
 78%|#######8  | Scoring GeneralizingEstimator : 958/1225 [00:02<00:00,  469.79it/s]
 80%|#######9  | Scoring GeneralizingEstimator : 977/1225 [00:02<00:00,  474.45it/s]
 81%|########1 | Scoring GeneralizingEstimator : 994/1225 [00:02<00:00,  475.74it/s]
 82%|########2 | Scoring GeneralizingEstimator : 1009/1225 [00:02<00:00,  473.83it/s]
 84%|########3 | Scoring GeneralizingEstimator : 1023/1225 [00:02<00:00,  470.42it/s]
 85%|########4 | Scoring GeneralizingEstimator : 1037/1225 [00:02<00:00,  467.43it/s]
 86%|########5 | Scoring GeneralizingEstimator : 1050/1225 [00:02<00:00,  462.65it/s]
 87%|########6 | Scoring GeneralizingEstimator : 1062/1225 [00:02<00:00,  457.17it/s]
 88%|########7 | Scoring GeneralizingEstimator : 1073/1225 [00:02<00:00,  450.35it/s]
 89%|########8 | Scoring GeneralizingEstimator : 1085/1225 [00:02<00:00,  445.38it/s]
 90%|########9 | Scoring GeneralizingEstimator : 1100/1225 [00:02<00:00,  445.31it/s]
 91%|######### | Scoring GeneralizingEstimator : 1113/1225 [00:02<00:00,  442.12it/s]
 92%|#########2| Scoring GeneralizingEstimator : 1131/1225 [00:02<00:00,  446.64it/s]
 94%|#########3| Scoring GeneralizingEstimator : 1149/1225 [00:02<00:00,  450.90it/s]
 95%|#########5| Scoring GeneralizingEstimator : 1164/1225 [00:02<00:00,  450.41it/s]
 96%|#########6| Scoring GeneralizingEstimator : 1179/1225 [00:02<00:00,  450.08it/s]
 98%|#########7| Scoring GeneralizingEstimator : 1198/1225 [00:02<00:00,  455.75it/s]
 99%|#########9| Scoring GeneralizingEstimator : 1217/1225 [00:02<00:00,  461.17it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  431.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()
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 8.383 seconds)

Estimated memory usage: 129 MB

Gallery generated by Sphinx-Gallery