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,   20.03it/s]
  9%|8         | Fitting GeneralizingEstimator : 3/35 [00:00<00:01,   20.66it/s]
 14%|#4        | Fitting GeneralizingEstimator : 5/35 [00:00<00:01,   21.31it/s]
 20%|##        | Fitting GeneralizingEstimator : 7/35 [00:00<00:01,   21.88it/s]
 26%|##5       | Fitting GeneralizingEstimator : 9/35 [00:00<00:01,   22.58it/s]
 37%|###7      | Fitting GeneralizingEstimator : 13/35 [00:00<00:00,   23.52it/s]
 46%|####5     | Fitting GeneralizingEstimator : 16/35 [00:00<00:00,   24.37it/s]
 54%|#####4    | Fitting GeneralizingEstimator : 19/35 [00:00<00:00,   25.21it/s]
 60%|######    | Fitting GeneralizingEstimator : 21/35 [00:00<00:00,   25.95it/s]
 69%|######8   | Fitting GeneralizingEstimator : 24/35 [00:00<00:00,   26.80it/s]
 74%|#######4  | Fitting GeneralizingEstimator : 26/35 [00:00<00:00,   27.55it/s]
 80%|########  | Fitting GeneralizingEstimator : 28/35 [00:00<00:00,   28.31it/s]
 86%|########5 | Fitting GeneralizingEstimator : 30/35 [00:00<00:00,   29.07it/s]
 91%|#########1| Fitting GeneralizingEstimator : 32/35 [00:00<00:00,   29.83it/s]
 97%|#########7| Fitting GeneralizingEstimator : 34/35 [00:00<00:00,   30.59it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   60.64it/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,  528.60it/s]
  3%|3         | Scoring GeneralizingEstimator : 41/1225 [00:00<00:02,  534.57it/s]
  5%|5         | Scoring GeneralizingEstimator : 63/1225 [00:00<00:02,  539.40it/s]
  7%|7         | Scoring GeneralizingEstimator : 86/1225 [00:00<00:02,  545.05it/s]
  9%|8         | Scoring GeneralizingEstimator : 109/1225 [00:00<00:02,  550.58it/s]
 11%|#         | Scoring GeneralizingEstimator : 132/1225 [00:00<00:01,  555.92it/s]
 13%|#2        | Scoring GeneralizingEstimator : 155/1225 [00:00<00:01,  560.94it/s]
 15%|#4        | Scoring GeneralizingEstimator : 178/1225 [00:00<00:01,  565.95it/s]
 16%|#6        | Scoring GeneralizingEstimator : 201/1225 [00:00<00:01,  570.80it/s]
 18%|#8        | Scoring GeneralizingEstimator : 224/1225 [00:00<00:01,  575.41it/s]
 20%|##        | Scoring GeneralizingEstimator : 247/1225 [00:00<00:01,  579.92it/s]
 22%|##2       | Scoring GeneralizingEstimator : 270/1225 [00:00<00:01,  584.25it/s]
 24%|##3       | Scoring GeneralizingEstimator : 293/1225 [00:00<00:01,  588.48it/s]
 26%|##5       | Scoring GeneralizingEstimator : 316/1225 [00:00<00:01,  592.35it/s]
 28%|##7       | Scoring GeneralizingEstimator : 339/1225 [00:00<00:01,  596.22it/s]
 30%|##9       | Scoring GeneralizingEstimator : 362/1225 [00:00<00:01,  600.00it/s]
 31%|###1      | Scoring GeneralizingEstimator : 384/1225 [00:00<00:01,  602.24it/s]
 33%|###3      | Scoring GeneralizingEstimator : 407/1225 [00:00<00:01,  605.79it/s]
 35%|###5      | Scoring GeneralizingEstimator : 430/1225 [00:00<00:01,  609.10it/s]
 37%|###7      | Scoring GeneralizingEstimator : 454/1225 [00:00<00:01,  613.50it/s]
 39%|###8      | Scoring GeneralizingEstimator : 477/1225 [00:00<00:01,  616.55it/s]
 41%|####      | Scoring GeneralizingEstimator : 500/1225 [00:00<00:01,  619.51it/s]
 43%|####2     | Scoring GeneralizingEstimator : 523/1225 [00:00<00:01,  622.35it/s]
 45%|####4     | Scoring GeneralizingEstimator : 547/1225 [00:00<00:01,  626.24it/s]
 46%|####6     | Scoring GeneralizingEstimator : 569/1225 [00:00<00:01,  627.52it/s]
 48%|####8     | Scoring GeneralizingEstimator : 590/1225 [00:00<00:01,  627.20it/s]
 50%|####9     | Scoring GeneralizingEstimator : 612/1225 [00:00<00:00,  628.35it/s]
 52%|#####1    | Scoring GeneralizingEstimator : 634/1225 [00:00<00:00,  629.32it/s]
 54%|#####3    | Scoring GeneralizingEstimator : 656/1225 [00:00<00:00,  630.45it/s]
 55%|#####5    | Scoring GeneralizingEstimator : 677/1225 [00:01<00:00,  629.91it/s]
 57%|#####7    | Scoring GeneralizingEstimator : 699/1225 [00:01<00:00,  630.72it/s]
 59%|#####8    | Scoring GeneralizingEstimator : 721/1225 [00:01<00:00,  631.67it/s]
 61%|######    | Scoring GeneralizingEstimator : 742/1225 [00:01<00:00,  631.15it/s]
 62%|######2   | Scoring GeneralizingEstimator : 764/1225 [00:01<00:00,  632.13it/s]
 64%|######4   | Scoring GeneralizingEstimator : 787/1225 [00:01<00:00,  634.42it/s]
 66%|######6   | Scoring GeneralizingEstimator : 810/1225 [00:01<00:00,  636.64it/s]
 68%|######8   | Scoring GeneralizingEstimator : 833/1225 [00:01<00:00,  638.76it/s]
 70%|######9   | Scoring GeneralizingEstimator : 856/1225 [00:01<00:00,  640.81it/s]
 72%|#######1  | Scoring GeneralizingEstimator : 878/1225 [00:01<00:00,  641.07it/s]
 73%|#######3  | Scoring GeneralizingEstimator : 900/1225 [00:01<00:00,  641.41it/s]
 75%|#######5  | Scoring GeneralizingEstimator : 923/1225 [00:01<00:00,  643.09it/s]
 77%|#######7  | Scoring GeneralizingEstimator : 946/1225 [00:01<00:00,  644.86it/s]
 79%|#######9  | Scoring GeneralizingEstimator : 969/1225 [00:01<00:00,  646.62it/s]
 81%|########  | Scoring GeneralizingEstimator : 992/1225 [00:01<00:00,  647.89it/s]
 83%|########2 | Scoring GeneralizingEstimator : 1015/1225 [00:01<00:00,  649.46it/s]
 85%|########4 | Scoring GeneralizingEstimator : 1039/1225 [00:01<00:00,  652.24it/s]
 87%|########6 | Scoring GeneralizingEstimator : 1062/1225 [00:01<00:00,  653.66it/s]
 89%|########8 | Scoring GeneralizingEstimator : 1085/1225 [00:01<00:00,  654.95it/s]
 90%|######### | Scoring GeneralizingEstimator : 1108/1225 [00:01<00:00,  656.24it/s]
 92%|#########2| Scoring GeneralizingEstimator : 1130/1225 [00:01<00:00,  655.56it/s]
 94%|#########4| Scoring GeneralizingEstimator : 1153/1225 [00:01<00:00,  656.79it/s]
 96%|#########6| Scoring GeneralizingEstimator : 1177/1225 [00:01<00:00,  659.17it/s]
 98%|#########7| Scoring GeneralizingEstimator : 1200/1225 [00:01<00:00,  660.30it/s]
100%|#########9| Scoring GeneralizingEstimator : 1223/1225 [00:01<00:00,  661.31it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:01<00:00,  669.63it/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 5.809 seconds)

Estimated memory usage: 128 MB

Gallery generated by Sphinx-Gallery