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.0, 30.0, 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 s)

[Parallel(n_jobs=1)]: Done  17 tasks      | elapsed:    0.0s
[Parallel(n_jobs=1)]: Done  71 tasks      | elapsed:    0.1s
[Parallel(n_jobs=1)]: Done 161 tasks      | elapsed:    0.3s
[Parallel(n_jobs=1)]: Done 287 tasks      | elapsed:    0.5s

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%|▌         | Fitting GeneralizingEstimator : 2/35 [00:00<00:00,   58.46it/s]
 11%|█▏        | Fitting GeneralizingEstimator : 4/35 [00:00<00:00,   58.77it/s]
 17%|█▋        | Fitting GeneralizingEstimator : 6/35 [00:00<00:00,   58.96it/s]
 26%|██▌       | Fitting GeneralizingEstimator : 9/35 [00:00<00:00,   66.94it/s]
 34%|███▍      | Fitting GeneralizingEstimator : 12/35 [00:00<00:00,   71.77it/s]
 43%|████▎     | Fitting GeneralizingEstimator : 15/35 [00:00<00:00,   74.97it/s]
 54%|█████▍    | Fitting GeneralizingEstimator : 19/35 [00:00<00:00,   82.13it/s]
 60%|██████    | Fitting GeneralizingEstimator : 21/35 [00:00<00:00,   78.73it/s]
 69%|██████▊   | Fitting GeneralizingEstimator : 24/35 [00:00<00:00,   80.07it/s]
 74%|███████▍  | Fitting GeneralizingEstimator : 26/35 [00:00<00:00,   77.47it/s]
 83%|████████▎ | Fitting GeneralizingEstimator : 29/35 [00:00<00:00,   78.74it/s]
 89%|████████▊ | Fitting GeneralizingEstimator : 31/35 [00:00<00:00,   76.62it/s]
 97%|█████████▋| Fitting GeneralizingEstimator : 34/35 [00:00<00:00,   77.87it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   78.11it/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 : 12/1225 [00:00<00:03,  349.59it/s]
  2%|▏         | Scoring GeneralizingEstimator : 28/1225 [00:00<00:02,  412.62it/s]
  4%|▎         | Scoring GeneralizingEstimator : 44/1225 [00:00<00:02,  433.74it/s]
  5%|▍         | Scoring GeneralizingEstimator : 59/1225 [00:00<00:02,  436.65it/s]
  6%|▌         | Scoring GeneralizingEstimator : 75/1225 [00:00<00:02,  444.77it/s]
  7%|▋         | Scoring GeneralizingEstimator : 90/1225 [00:00<00:02,  444.39it/s]
  9%|▊         | Scoring GeneralizingEstimator : 106/1225 [00:00<00:02,  449.12it/s]
 10%|▉         | Scoring GeneralizingEstimator : 121/1225 [00:00<00:02,  448.25it/s]
 11%|█         | Scoring GeneralizingEstimator : 137/1225 [00:00<00:02,  451.26it/s]
 12%|█▏        | Scoring GeneralizingEstimator : 152/1225 [00:00<00:02,  450.18it/s]
 14%|█▎        | Scoring GeneralizingEstimator : 168/1225 [00:00<00:02,  452.99it/s]
 15%|█▍        | Scoring GeneralizingEstimator : 183/1225 [00:00<00:02,  451.44it/s]
 16%|█▌        | Scoring GeneralizingEstimator : 199/1225 [00:00<00:02,  453.81it/s]
 18%|█▊        | Scoring GeneralizingEstimator : 215/1225 [00:00<00:02,  455.61it/s]
 19%|█▉        | Scoring GeneralizingEstimator : 230/1225 [00:00<00:02,  454.29it/s]
 20%|██        | Scoring GeneralizingEstimator : 246/1225 [00:00<00:02,  455.98it/s]
 21%|██▏       | Scoring GeneralizingEstimator : 262/1225 [00:00<00:02,  457.43it/s]
 23%|██▎       | Scoring GeneralizingEstimator : 278/1225 [00:00<00:02,  458.64it/s]
 24%|██▍       | Scoring GeneralizingEstimator : 294/1225 [00:00<00:02,  459.86it/s]
 25%|██▌       | Scoring GeneralizingEstimator : 309/1225 [00:00<00:01,  458.67it/s]
 27%|██▋       | Scoring GeneralizingEstimator : 325/1225 [00:00<00:01,  459.84it/s]
 28%|██▊       | Scoring GeneralizingEstimator : 340/1225 [00:00<00:01,  458.62it/s]
 29%|██▉       | Scoring GeneralizingEstimator : 356/1225 [00:00<00:01,  459.29it/s]
 30%|███       | Scoring GeneralizingEstimator : 371/1225 [00:00<00:01,  458.10it/s]
 31%|███▏      | Scoring GeneralizingEstimator : 385/1225 [00:00<00:01,  455.01it/s]
 33%|███▎      | Scoring GeneralizingEstimator : 399/1225 [00:00<00:01,  451.87it/s]
 34%|███▍      | Scoring GeneralizingEstimator : 414/1225 [00:00<00:01,  451.31it/s]
 35%|███▌      | Scoring GeneralizingEstimator : 429/1225 [00:00<00:01,  450.86it/s]
 36%|███▌      | Scoring GeneralizingEstimator : 444/1225 [00:00<00:01,  450.31it/s]
 38%|███▊      | Scoring GeneralizingEstimator : 460/1225 [00:01<00:01,  451.61it/s]
 39%|███▉      | Scoring GeneralizingEstimator : 476/1225 [00:01<00:01,  452.98it/s]
 40%|████      | Scoring GeneralizingEstimator : 491/1225 [00:01<00:01,  452.31it/s]
 41%|████▏     | Scoring GeneralizingEstimator : 507/1225 [00:01<00:01,  453.61it/s]
 43%|████▎     | Scoring GeneralizingEstimator : 524/1225 [00:01<00:01,  456.43it/s]
 44%|████▍     | Scoring GeneralizingEstimator : 540/1225 [00:01<00:01,  457.47it/s]
 45%|████▌     | Scoring GeneralizingEstimator : 556/1225 [00:01<00:01,  458.45it/s]
 47%|████▋     | Scoring GeneralizingEstimator : 572/1225 [00:01<00:01,  459.25it/s]
 48%|████▊     | Scoring GeneralizingEstimator : 588/1225 [00:01<00:01,  459.99it/s]
 49%|████▉     | Scoring GeneralizingEstimator : 604/1225 [00:01<00:01,  460.30it/s]
 51%|█████     | Scoring GeneralizingEstimator : 620/1225 [00:01<00:01,  460.97it/s]
 52%|█████▏    | Scoring GeneralizingEstimator : 636/1225 [00:01<00:01,  461.68it/s]
 53%|█████▎    | Scoring GeneralizingEstimator : 651/1225 [00:01<00:01,  460.31it/s]
 54%|█████▍    | Scoring GeneralizingEstimator : 667/1225 [00:01<00:01,  460.63it/s]
 56%|█████▌    | Scoring GeneralizingEstimator : 683/1225 [00:01<00:01,  461.36it/s]
 57%|█████▋    | Scoring GeneralizingEstimator : 699/1225 [00:01<00:01,  461.99it/s]
 58%|█████▊    | Scoring GeneralizingEstimator : 715/1225 [00:01<00:01,  462.67it/s]
 60%|█████▉    | Scoring GeneralizingEstimator : 731/1225 [00:01<00:01,  463.21it/s]
 61%|██████    | Scoring GeneralizingEstimator : 747/1225 [00:01<00:01,  463.68it/s]
 62%|██████▏   | Scoring GeneralizingEstimator : 764/1225 [00:01<00:00,  465.86it/s]
 64%|██████▎   | Scoring GeneralizingEstimator : 780/1225 [00:01<00:00,  466.32it/s]
 65%|██████▍   | Scoring GeneralizingEstimator : 796/1225 [00:01<00:00,  466.63it/s]
 66%|██████▋   | Scoring GeneralizingEstimator : 813/1225 [00:01<00:00,  468.63it/s]
 68%|██████▊   | Scoring GeneralizingEstimator : 829/1225 [00:01<00:00,  468.92it/s]
 69%|██████▉   | Scoring GeneralizingEstimator : 845/1225 [00:01<00:00,  468.55it/s]
 70%|███████   | Scoring GeneralizingEstimator : 862/1225 [00:01<00:00,  470.30it/s]
 72%|███████▏  | Scoring GeneralizingEstimator : 878/1225 [00:01<00:00,  470.42it/s]
 73%|███████▎  | Scoring GeneralizingEstimator : 895/1225 [00:01<00:00,  472.08it/s]
 74%|███████▍  | Scoring GeneralizingEstimator : 911/1225 [00:01<00:00,  472.11it/s]
 76%|███████▌  | Scoring GeneralizingEstimator : 926/1225 [00:02<00:00,  470.66it/s]
 77%|███████▋  | Scoring GeneralizingEstimator : 942/1225 [00:02<00:00,  470.81it/s]
 78%|███████▊  | Scoring GeneralizingEstimator : 958/1225 [00:02<00:00,  470.95it/s]
 80%|███████▉  | Scoring GeneralizingEstimator : 975/1225 [00:02<00:00,  472.42it/s]
 81%|████████  | Scoring GeneralizingEstimator : 991/1225 [00:02<00:00,  472.40it/s]
 82%|████████▏ | Scoring GeneralizingEstimator : 1007/1225 [00:02<00:00,  472.39it/s]
 84%|████████▎ | Scoring GeneralizingEstimator : 1023/1225 [00:02<00:00,  472.39it/s]
 85%|████████▍ | Scoring GeneralizingEstimator : 1038/1225 [00:02<00:00,  470.87it/s]
 86%|████████▌ | Scoring GeneralizingEstimator : 1055/1225 [00:02<00:00,  472.59it/s]
 87%|████████▋ | Scoring GeneralizingEstimator : 1071/1225 [00:02<00:00,  472.68it/s]
 89%|████████▊ | Scoring GeneralizingEstimator : 1087/1225 [00:02<00:00,  472.60it/s]
 90%|█████████ | Scoring GeneralizingEstimator : 1104/1225 [00:02<00:00,  474.16it/s]
 91%|█████████▏| Scoring GeneralizingEstimator : 1120/1225 [00:02<00:00,  474.17it/s]
 93%|█████████▎| Scoring GeneralizingEstimator : 1137/1225 [00:02<00:00,  475.60it/s]
 94%|█████████▍| Scoring GeneralizingEstimator : 1153/1225 [00:02<00:00,  475.21it/s]
 96%|█████████▌| Scoring GeneralizingEstimator : 1170/1225 [00:02<00:00,  476.69it/s]
 97%|█████████▋| Scoring GeneralizingEstimator : 1187/1225 [00:02<00:00,  477.95it/s]
 98%|█████████▊| Scoring GeneralizingEstimator : 1203/1225 [00:02<00:00,  477.39it/s]
100%|█████████▉| Scoring GeneralizingEstimator : 1219/1225 [00:02<00:00,  476.86it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  467.27it/s]

Plot

fig, ax = plt.subplots(constrained_layout=True)
im = ax.matshow(
    scores,
    vmin=0,
    vmax=1.0,
    cmap="RdBu_r",
    origin="lower",
    extent=epochs.times[[0, -1, 0, -1]],
)
ax.axhline(0.0, color="k")
ax.axvline(0.0, color="k")
ax.xaxis.set_ticks_position("bottom")
ax.set_xlabel(
    'Condition: "Right"\nTesting Time (s)',
)
ax.set_ylabel('Condition: "Left"\nTraining Time (s)')
ax.set_title("Generalization across time and condition", fontweight="bold")
fig.colorbar(im, ax=ax, label="Performance (ROC AUC)")
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 7.273 seconds)

Estimated memory usage: 129 MB

Gallery generated by Sphinx-Gallery