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
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.14it/s]
  6%|5         | Fitting GeneralizingEstimator : 2/35 [00:00<00:01,   23.79it/s]
 11%|#1        | Fitting GeneralizingEstimator : 4/35 [00:00<00:00,   34.45it/s]
 14%|#4        | Fitting GeneralizingEstimator : 5/35 [00:00<00:01,   22.58it/s]
 17%|#7        | Fitting GeneralizingEstimator : 6/35 [00:00<00:01,   23.60it/s]
 20%|##        | Fitting GeneralizingEstimator : 7/35 [00:00<00:01,   23.58it/s]
 23%|##2       | Fitting GeneralizingEstimator : 8/35 [00:00<00:01,   21.03it/s]
 29%|##8       | Fitting GeneralizingEstimator : 10/35 [00:00<00:01,   24.77it/s]
 31%|###1      | Fitting GeneralizingEstimator : 11/35 [00:00<00:01,   23.17it/s]
 40%|####      | Fitting GeneralizingEstimator : 14/35 [00:00<00:00,   28.61it/s]
 43%|####2     | Fitting GeneralizingEstimator : 15/35 [00:00<00:00,   26.64it/s]
 49%|####8     | Fitting GeneralizingEstimator : 17/35 [00:00<00:00,   28.78it/s]
 51%|#####1    | Fitting GeneralizingEstimator : 18/35 [00:00<00:00,   27.21it/s]
 57%|#####7    | Fitting GeneralizingEstimator : 20/35 [00:00<00:00,   29.02it/s]
 60%|######    | Fitting GeneralizingEstimator : 21/35 [00:00<00:00,   27.63it/s]
 69%|######8   | Fitting GeneralizingEstimator : 24/35 [00:00<00:00,   30.95it/s]
 77%|#######7  | Fitting GeneralizingEstimator : 27/35 [00:00<00:00,   30.67it/s]
 83%|########2 | Fitting GeneralizingEstimator : 29/35 [00:01<00:00,   29.56it/s]
 86%|########5 | Fitting GeneralizingEstimator : 30/35 [00:01<00:00,   28.25it/s]
 94%|#########4| Fitting GeneralizingEstimator : 33/35 [00:01<00:00,   30.58it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:01<00:00,   29.54it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:01<00:00,   28.91it/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]
  2%|1         | Scoring GeneralizingEstimator : 20/1225 [00:00<00:02,  585.90it/s]
  3%|2         | Scoring GeneralizingEstimator : 32/1225 [00:00<00:02,  467.57it/s]
  4%|3         | Scoring GeneralizingEstimator : 47/1225 [00:00<00:03,  335.89it/s]
  5%|5         | Scoring GeneralizingEstimator : 63/1225 [00:00<00:03,  364.88it/s]
  6%|6         | Scoring GeneralizingEstimator : 74/1225 [00:00<00:03,  304.57it/s]
  7%|7         | Scoring GeneralizingEstimator : 90/1225 [00:00<00:03,  328.27it/s]
  8%|8         | Scoring GeneralizingEstimator : 100/1225 [00:00<00:03,  288.41it/s]
  9%|9         | Scoring GeneralizingEstimator : 115/1225 [00:00<00:03,  304.89it/s]
 10%|#         | Scoring GeneralizingEstimator : 126/1225 [00:00<00:03,  279.99it/s]
 12%|#1        | Scoring GeneralizingEstimator : 141/1225 [00:00<00:03,  294.54it/s]
 12%|#2        | Scoring GeneralizingEstimator : 152/1225 [00:00<00:03,  274.66it/s]
 14%|#3        | Scoring GeneralizingEstimator : 166/1225 [00:00<00:03,  285.37it/s]
 14%|#4        | Scoring GeneralizingEstimator : 177/1225 [00:00<00:03,  269.06it/s]
 16%|#5        | Scoring GeneralizingEstimator : 191/1225 [00:00<00:03,  278.94it/s]
 16%|#6        | Scoring GeneralizingEstimator : 198/1225 [00:00<00:03,  274.03it/s]
 17%|#7        | Scoring GeneralizingEstimator : 211/1225 [00:00<00:03,  263.94it/s]
 18%|#7        | Scoring GeneralizingEstimator : 218/1225 [00:00<00:04,  246.71it/s]
 19%|#9        | Scoring GeneralizingEstimator : 235/1225 [00:00<00:03,  261.40it/s]
 20%|##        | Scoring GeneralizingEstimator : 247/1225 [00:00<00:03,  252.89it/s]
 22%|##1       | Scoring GeneralizingEstimator : 267/1225 [00:00<00:03,  271.08it/s]
 23%|##2       | Scoring GeneralizingEstimator : 279/1225 [00:01<00:03,  262.20it/s]
 24%|##4       | Scoring GeneralizingEstimator : 298/1225 [00:01<00:03,  277.37it/s]
 25%|##5       | Scoring GeneralizingEstimator : 309/1225 [00:01<00:03,  266.97it/s]
 27%|##6       | Scoring GeneralizingEstimator : 327/1225 [00:01<00:03,  279.88it/s]
 28%|##7       | Scoring GeneralizingEstimator : 337/1225 [00:01<00:03,  268.12it/s]
 29%|##8       | Scoring GeneralizingEstimator : 351/1225 [00:01<00:03,  274.95it/s]
 29%|##9       | Scoring GeneralizingEstimator : 357/1225 [00:01<00:03,  258.77it/s]
 30%|###       | Scoring GeneralizingEstimator : 369/1225 [00:01<00:03,  263.10it/s]
 31%|###       | Scoring GeneralizingEstimator : 377/1225 [00:01<00:03,  251.01it/s]
 32%|###1      | Scoring GeneralizingEstimator : 391/1225 [00:01<00:03,  258.14it/s]
 33%|###2      | Scoring GeneralizingEstimator : 399/1225 [00:01<00:03,  246.59it/s]
 34%|###4      | Scoring GeneralizingEstimator : 418/1225 [00:01<00:03,  259.87it/s]
 35%|###4      | Scoring GeneralizingEstimator : 427/1225 [00:01<00:03,  250.22it/s]
 36%|###6      | Scoring GeneralizingEstimator : 446/1225 [00:01<00:02,  263.16it/s]
 37%|###7      | Scoring GeneralizingEstimator : 457/1225 [00:01<00:03,  255.52it/s]
 39%|###8      | Scoring GeneralizingEstimator : 477/1225 [00:01<00:02,  269.15it/s]
 40%|###9      | Scoring GeneralizingEstimator : 487/1225 [00:01<00:02,  260.09it/s]
 41%|####1     | Scoring GeneralizingEstimator : 503/1225 [00:01<00:02,  268.57it/s]
 42%|####1     | Scoring GeneralizingEstimator : 512/1225 [00:01<00:02,  258.50it/s]
 43%|####2     | Scoring GeneralizingEstimator : 526/1225 [00:01<00:02,  264.57it/s]
 44%|####3     | Scoring GeneralizingEstimator : 537/1225 [00:02<00:02,  257.25it/s]
 45%|####4     | Scoring GeneralizingEstimator : 551/1225 [00:02<00:02,  263.23it/s]
 46%|####5     | Scoring GeneralizingEstimator : 561/1225 [00:02<00:02,  254.82it/s]
 47%|####7     | Scoring GeneralizingEstimator : 576/1225 [00:02<00:02,  262.01it/s]
 48%|####7     | Scoring GeneralizingEstimator : 583/1225 [00:02<00:02,  250.28it/s]
 49%|####9     | Scoring GeneralizingEstimator : 603/1225 [00:02<00:02,  263.11it/s]
 50%|####9     | Scoring GeneralizingEstimator : 612/1225 [00:02<00:02,  254.52it/s]
 51%|#####     | Scoring GeneralizingEstimator : 623/1225 [00:02<00:02,  257.11it/s]
 51%|#####1    | Scoring GeneralizingEstimator : 629/1225 [00:02<00:02,  254.06it/s]
 51%|#####1    | Scoring GeneralizingEstimator : 630/1225 [00:02<00:02,  245.38it/s]
 52%|#####2    | Scoring GeneralizingEstimator : 643/1225 [00:02<00:02,  250.70it/s]
 53%|#####2    | Scoring GeneralizingEstimator : 649/1225 [00:02<00:02,  247.80it/s]
 53%|#####3    | Scoring GeneralizingEstimator : 650/1225 [00:02<00:02,  239.10it/s]
 54%|#####4    | Scoring GeneralizingEstimator : 667/1225 [00:02<00:02,  249.61it/s]
 55%|#####5    | Scoring GeneralizingEstimator : 674/1225 [00:02<00:02,  239.63it/s]
 56%|#####6    | Scoring GeneralizingEstimator : 692/1225 [00:02<00:02,  251.11it/s]
 57%|#####7    | Scoring GeneralizingEstimator : 700/1225 [00:02<00:02,  241.59it/s]
 58%|#####8    | Scoring GeneralizingEstimator : 716/1225 [00:02<00:02,  250.56it/s]
 59%|#####9    | Scoring GeneralizingEstimator : 726/1225 [00:02<00:02,  243.23it/s]
 60%|######    | Scoring GeneralizingEstimator : 738/1225 [00:02<00:01,  247.48it/s]
 61%|######    | Scoring GeneralizingEstimator : 745/1225 [00:02<00:02,  237.12it/s]
 62%|######1   | Scoring GeneralizingEstimator : 756/1225 [00:02<00:01,  240.40it/s]
 62%|######2   | Scoring GeneralizingEstimator : 764/1225 [00:03<00:01,  231.77it/s]
 64%|######4   | Scoring GeneralizingEstimator : 784/1225 [00:03<00:01,  245.20it/s]
 65%|######4   | Scoring GeneralizingEstimator : 794/1225 [00:03<00:01,  247.11it/s]
 65%|######4   | Scoring GeneralizingEstimator : 795/1225 [00:03<00:01,  238.74it/s]
 66%|######6   | Scoring GeneralizingEstimator : 814/1225 [00:03<00:01,  251.19it/s]
 67%|######7   | Scoring GeneralizingEstimator : 823/1225 [00:03<00:01,  243.19it/s]
 68%|######8   | Scoring GeneralizingEstimator : 834/1225 [00:03<00:01,  246.27it/s]
 69%|######8   | Scoring GeneralizingEstimator : 841/1225 [00:03<00:01,  236.11it/s]
 70%|#######   | Scoring GeneralizingEstimator : 860/1225 [00:03<00:01,  248.43it/s]
 71%|#######   | Scoring GeneralizingEstimator : 869/1225 [00:03<00:01,  240.33it/s]
 72%|#######2  | Scoring GeneralizingEstimator : 884/1225 [00:03<00:01,  247.91it/s]
 73%|#######3  | Scoring GeneralizingEstimator : 895/1225 [00:03<00:01,  242.09it/s]
 74%|#######4  | Scoring GeneralizingEstimator : 909/1225 [00:03<00:01,  248.45it/s]
 75%|#######5  | Scoring GeneralizingEstimator : 921/1225 [00:03<00:01,  243.71it/s]
 76%|#######6  | Scoring GeneralizingEstimator : 936/1225 [00:03<00:01,  251.04it/s]
 77%|#######7  | Scoring GeneralizingEstimator : 946/1225 [00:03<00:01,  244.02it/s]
 78%|#######8  | Scoring GeneralizingEstimator : 960/1225 [00:03<00:01,  250.17it/s]
 79%|#######9  | Scoring GeneralizingEstimator : 969/1225 [00:03<00:01,  242.24it/s]
 80%|########  | Scoring GeneralizingEstimator : 986/1225 [00:03<00:00,  251.66it/s]
 81%|########1 | Scoring GeneralizingEstimator : 996/1225 [00:03<00:00,  244.70it/s]
 83%|########2 | Scoring GeneralizingEstimator : 1011/1225 [00:03<00:00,  251.85it/s]
 83%|########3 | Scoring GeneralizingEstimator : 1018/1225 [00:04<00:00,  241.75it/s]
 85%|########4 | Scoring GeneralizingEstimator : 1037/1225 [00:04<00:00,  253.22it/s]
 85%|########5 | Scoring GeneralizingEstimator : 1046/1225 [00:04<00:00,  245.21it/s]
 87%|########7 | Scoring GeneralizingEstimator : 1066/1225 [00:04<00:00,  257.53it/s]
 88%|########8 | Scoring GeneralizingEstimator : 1079/1225 [00:04<00:00,  253.37it/s]
 90%|########9 | Scoring GeneralizingEstimator : 1099/1225 [00:04<00:00,  265.38it/s]
 91%|######### | Scoring GeneralizingEstimator : 1109/1225 [00:04<00:00,  257.60it/s]
 92%|#########2| Scoring GeneralizingEstimator : 1129/1225 [00:04<00:00,  269.39it/s]
 93%|#########2| Scoring GeneralizingEstimator : 1138/1225 [00:04<00:00,  260.36it/s]
 95%|#########4| Scoring GeneralizingEstimator : 1158/1225 [00:04<00:00,  272.03it/s]
 95%|#########5| Scoring GeneralizingEstimator : 1169/1225 [00:04<00:00,  264.86it/s]
 97%|#########6| Scoring GeneralizingEstimator : 1188/1225 [00:04<00:00,  275.28it/s]
 98%|#########7| Scoring GeneralizingEstimator : 1197/1225 [00:04<00:00,  265.89it/s]
 99%|#########9| Scoring GeneralizingEstimator : 1214/1225 [00:04<00:00,  274.18it/s]
100%|#########9| Scoring GeneralizingEstimator : 1222/1225 [00:04<00:00,  263.76it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:04<00:00,  258.17it/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 10.753 seconds)

Estimated memory usage: 128 MB

Gallery generated by Sphinx-Gallery