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
# Copyright the MNE-Python contributors.
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

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(copy=False), y=epochs["Left"].events[:, 2] > 2)
  0%|          | Fitting GeneralizingEstimator : 0/35 [00:00<?,       ?it/s]
  3%|▎         | Fitting GeneralizingEstimator : 1/35 [00:00<00:01,   29.01it/s]
  9%|▊         | Fitting GeneralizingEstimator : 3/35 [00:00<00:00,   44.27it/s]
 14%|█▍        | Fitting GeneralizingEstimator : 5/35 [00:00<00:00,   49.37it/s]
 20%|██        | Fitting GeneralizingEstimator : 7/35 [00:00<00:00,   51.79it/s]
 26%|██▌       | Fitting GeneralizingEstimator : 9/35 [00:00<00:00,   53.41it/s]
 31%|███▏      | Fitting GeneralizingEstimator : 11/35 [00:00<00:00,   54.48it/s]
 40%|████      | Fitting GeneralizingEstimator : 14/35 [00:00<00:00,   60.11it/s]
 49%|████▊     | Fitting GeneralizingEstimator : 17/35 [00:00<00:00,   64.30it/s]
 54%|█████▍    | Fitting GeneralizingEstimator : 19/35 [00:00<00:00,   63.60it/s]
 63%|██████▎   | Fitting GeneralizingEstimator : 22/35 [00:00<00:00,   66.54it/s]
 69%|██████▊   | Fitting GeneralizingEstimator : 24/35 [00:00<00:00,   65.65it/s]
 74%|███████▍  | Fitting GeneralizingEstimator : 26/35 [00:00<00:00,   64.93it/s]
 80%|████████  | Fitting GeneralizingEstimator : 28/35 [00:00<00:00,   64.33it/s]
 86%|████████▌ | Fitting GeneralizingEstimator : 30/35 [00:00<00:00,   63.82it/s]
 94%|█████████▍| Fitting GeneralizingEstimator : 33/35 [00:00<00:00,   66.12it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   66.27it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   64.84it/s]

Score on the epochs where the stimulus was presented to the right.

scores = time_gen.score(
    X=epochs["Right"].get_data(copy=False), y=epochs["Right"].events[:, 2] > 2
)
  0%|          | Scoring GeneralizingEstimator : 0/1225 [00:00<?,       ?it/s]
  0%|          | Scoring GeneralizingEstimator : 5/1225 [00:00<00:08,  145.03it/s]
  1%|          | Scoring GeneralizingEstimator : 13/1225 [00:00<00:06,  190.85it/s]
  2%|▏         | Scoring GeneralizingEstimator : 20/1225 [00:00<00:06,  194.93it/s]
  2%|▏         | Scoring GeneralizingEstimator : 27/1225 [00:00<00:06,  197.89it/s]
  3%|▎         | Scoring GeneralizingEstimator : 35/1225 [00:00<00:05,  205.71it/s]
  3%|▎         | Scoring GeneralizingEstimator : 42/1225 [00:00<00:05,  205.81it/s]
  4%|▍         | Scoring GeneralizingEstimator : 49/1225 [00:00<00:05,  205.82it/s]
  5%|▍         | Scoring GeneralizingEstimator : 56/1225 [00:00<00:05,  205.62it/s]
  5%|▌         | Scoring GeneralizingEstimator : 64/1225 [00:00<00:05,  209.75it/s]
  6%|▌         | Scoring GeneralizingEstimator : 71/1225 [00:00<00:05,  208.97it/s]
  6%|▋         | Scoring GeneralizingEstimator : 79/1225 [00:00<00:05,  212.00it/s]
  7%|▋         | Scoring GeneralizingEstimator : 87/1225 [00:00<00:05,  214.63it/s]
  8%|▊         | Scoring GeneralizingEstimator : 95/1225 [00:00<00:05,  216.32it/s]
  8%|▊         | Scoring GeneralizingEstimator : 102/1225 [00:00<00:05,  215.25it/s]
  9%|▉         | Scoring GeneralizingEstimator : 110/1225 [00:00<00:05,  216.98it/s]
 10%|▉         | Scoring GeneralizingEstimator : 117/1225 [00:00<00:05,  216.02it/s]
 10%|█         | Scoring GeneralizingEstimator : 124/1225 [00:00<00:05,  215.22it/s]
 11%|█         | Scoring GeneralizingEstimator : 132/1225 [00:00<00:05,  216.98it/s]
 11%|█▏        | Scoring GeneralizingEstimator : 139/1225 [00:00<00:05,  216.13it/s]
 12%|█▏        | Scoring GeneralizingEstimator : 150/1225 [00:00<00:04,  224.54it/s]
 13%|█▎        | Scoring GeneralizingEstimator : 157/1225 [00:00<00:04,  223.09it/s]
 13%|█▎        | Scoring GeneralizingEstimator : 165/1225 [00:00<00:04,  223.90it/s]
 14%|█▍        | Scoring GeneralizingEstimator : 173/1225 [00:00<00:04,  224.43it/s]
 15%|█▍        | Scoring GeneralizingEstimator : 180/1225 [00:00<00:04,  223.03it/s]
 15%|█▌        | Scoring GeneralizingEstimator : 188/1225 [00:00<00:04,  223.74it/s]
 16%|█▌        | Scoring GeneralizingEstimator : 195/1225 [00:00<00:04,  222.57it/s]
 17%|█▋        | Scoring GeneralizingEstimator : 203/1225 [00:00<00:04,  223.43it/s]
 17%|█▋        | Scoring GeneralizingEstimator : 211/1225 [00:00<00:04,  224.16it/s]
 18%|█▊        | Scoring GeneralizingEstimator : 219/1225 [00:00<00:04,  224.78it/s]
 18%|█▊        | Scoring GeneralizingEstimator : 226/1225 [00:01<00:04,  223.33it/s]
 19%|█▉        | Scoring GeneralizingEstimator : 234/1225 [00:01<00:04,  224.17it/s]
 20%|█▉        | Scoring GeneralizingEstimator : 241/1225 [00:01<00:04,  223.07it/s]
 20%|██        | Scoring GeneralizingEstimator : 248/1225 [00:01<00:04,  221.79it/s]
 21%|██        | Scoring GeneralizingEstimator : 255/1225 [00:01<00:04,  220.62it/s]
 21%|██▏       | Scoring GeneralizingEstimator : 263/1225 [00:01<00:04,  221.56it/s]
 22%|██▏       | Scoring GeneralizingEstimator : 270/1225 [00:01<00:04,  220.59it/s]
 23%|██▎       | Scoring GeneralizingEstimator : 278/1225 [00:01<00:04,  221.39it/s]
 23%|██▎       | Scoring GeneralizingEstimator : 285/1225 [00:01<00:04,  220.43it/s]
 24%|██▍       | Scoring GeneralizingEstimator : 292/1225 [00:01<00:04,  219.52it/s]
 24%|██▍       | Scoring GeneralizingEstimator : 300/1225 [00:01<00:04,  220.12it/s]
 25%|██▌       | Scoring GeneralizingEstimator : 307/1225 [00:01<00:04,  219.16it/s]
 26%|██▌       | Scoring GeneralizingEstimator : 315/1225 [00:01<00:04,  219.82it/s]
 26%|██▋       | Scoring GeneralizingEstimator : 322/1225 [00:01<00:04,  218.96it/s]
 27%|██▋       | Scoring GeneralizingEstimator : 329/1225 [00:01<00:04,  218.23it/s]
 28%|██▊       | Scoring GeneralizingEstimator : 337/1225 [00:01<00:04,  219.20it/s]
 28%|██▊       | Scoring GeneralizingEstimator : 344/1225 [00:01<00:04,  218.25it/s]
 29%|██▊       | Scoring GeneralizingEstimator : 352/1225 [00:01<00:03,  219.20it/s]
 29%|██▉       | Scoring GeneralizingEstimator : 359/1225 [00:01<00:03,  218.47it/s]
 30%|██▉       | Scoring GeneralizingEstimator : 366/1225 [00:01<00:03,  217.83it/s]
 31%|███       | Scoring GeneralizingEstimator : 374/1225 [00:01<00:03,  218.76it/s]
 31%|███       | Scoring GeneralizingEstimator : 382/1225 [00:01<00:03,  219.59it/s]
 32%|███▏      | Scoring GeneralizingEstimator : 392/1225 [00:01<00:03,  223.63it/s]
 33%|███▎      | Scoring GeneralizingEstimator : 402/1225 [00:01<00:03,  227.44it/s]
 34%|███▎      | Scoring GeneralizingEstimator : 412/1225 [00:01<00:03,  230.96it/s]
 34%|███▍      | Scoring GeneralizingEstimator : 422/1225 [00:01<00:03,  234.33it/s]
 35%|███▌      | Scoring GeneralizingEstimator : 434/1225 [00:01<00:03,  240.64it/s]
 36%|███▋      | Scoring GeneralizingEstimator : 445/1225 [00:01<00:03,  244.95it/s]
 37%|███▋      | Scoring GeneralizingEstimator : 457/1225 [00:01<00:03,  250.68it/s]
 38%|███▊      | Scoring GeneralizingEstimator : 470/1225 [00:02<00:02,  257.51it/s]
 39%|███▉      | Scoring GeneralizingEstimator : 481/1225 [00:02<00:02,  261.00it/s]
 40%|████      | Scoring GeneralizingEstimator : 490/1225 [00:02<00:02,  261.23it/s]
 41%|████      | Scoring GeneralizingEstimator : 500/1225 [00:02<00:02,  262.95it/s]
 42%|████▏     | Scoring GeneralizingEstimator : 509/1225 [00:02<00:02,  263.14it/s]
 42%|████▏     | Scoring GeneralizingEstimator : 518/1225 [00:02<00:02,  263.30it/s]
 43%|████▎     | Scoring GeneralizingEstimator : 527/1225 [00:02<00:02,  263.39it/s]
 44%|████▎     | Scoring GeneralizingEstimator : 535/1225 [00:02<00:02,  261.90it/s]
 44%|████▍     | Scoring GeneralizingEstimator : 544/1225 [00:02<00:02,  262.07it/s]
 45%|████▌     | Scoring GeneralizingEstimator : 553/1225 [00:02<00:02,  262.18it/s]
 46%|████▌     | Scoring GeneralizingEstimator : 563/1225 [00:02<00:02,  263.60it/s]
 47%|████▋     | Scoring GeneralizingEstimator : 572/1225 [00:02<00:02,  263.62it/s]
 48%|████▊     | Scoring GeneralizingEstimator : 582/1225 [00:02<00:02,  265.24it/s]
 48%|████▊     | Scoring GeneralizingEstimator : 594/1225 [00:02<00:02,  269.64it/s]
 49%|████▉     | Scoring GeneralizingEstimator : 604/1225 [00:02<00:02,  270.93it/s]
 50%|█████     | Scoring GeneralizingEstimator : 613/1225 [00:02<00:02,  270.63it/s]
 51%|█████     | Scoring GeneralizingEstimator : 623/1225 [00:02<00:02,  271.89it/s]
 52%|█████▏    | Scoring GeneralizingEstimator : 632/1225 [00:02<00:02,  271.62it/s]
 52%|█████▏    | Scoring GeneralizingEstimator : 643/1225 [00:02<00:02,  274.15it/s]
 53%|█████▎    | Scoring GeneralizingEstimator : 653/1225 [00:02<00:02,  275.22it/s]
 54%|█████▍    | Scoring GeneralizingEstimator : 663/1225 [00:02<00:02,  276.12it/s]
 55%|█████▌    | Scoring GeneralizingEstimator : 675/1225 [00:02<00:01,  279.95it/s]
 56%|█████▌    | Scoring GeneralizingEstimator : 686/1225 [00:02<00:01,  282.21it/s]
 57%|█████▋    | Scoring GeneralizingEstimator : 696/1225 [00:02<00:01,  282.88it/s]
 58%|█████▊    | Scoring GeneralizingEstimator : 708/1225 [00:02<00:01,  286.46it/s]
 59%|█████▉    | Scoring GeneralizingEstimator : 720/1225 [00:02<00:01,  289.72it/s]
 60%|█████▉    | Scoring GeneralizingEstimator : 731/1225 [00:02<00:01,  291.48it/s]
 61%|██████    | Scoring GeneralizingEstimator : 744/1225 [00:02<00:01,  296.12it/s]
 62%|██████▏   | Scoring GeneralizingEstimator : 756/1225 [00:02<00:01,  299.03it/s]
 63%|██████▎   | Scoring GeneralizingEstimator : 768/1225 [00:02<00:01,  301.77it/s]
 64%|██████▎   | Scoring GeneralizingEstimator : 780/1225 [00:03<00:01,  304.06it/s]
 65%|██████▍   | Scoring GeneralizingEstimator : 792/1225 [00:03<00:01,  306.29it/s]
 66%|██████▌   | Scoring GeneralizingEstimator : 803/1225 [00:03<00:01,  307.22it/s]
 67%|██████▋   | Scoring GeneralizingEstimator : 815/1225 [00:03<00:01,  309.59it/s]
 68%|██████▊   | Scoring GeneralizingEstimator : 827/1225 [00:03<00:01,  311.68it/s]
 68%|██████▊   | Scoring GeneralizingEstimator : 839/1225 [00:03<00:01,  313.78it/s]
 69%|██████▉   | Scoring GeneralizingEstimator : 850/1225 [00:03<00:01,  314.36it/s]
 70%|███████   | Scoring GeneralizingEstimator : 862/1225 [00:03<00:01,  312.97it/s]
 71%|███████   | Scoring GeneralizingEstimator : 872/1225 [00:03<00:01,  312.10it/s]
 72%|███████▏  | Scoring GeneralizingEstimator : 883/1225 [00:03<00:01,  312.51it/s]
 73%|███████▎  | Scoring GeneralizingEstimator : 896/1225 [00:03<00:01,  315.89it/s]
 74%|███████▍  | Scoring GeneralizingEstimator : 908/1225 [00:03<00:00,  317.59it/s]
 75%|███████▌  | Scoring GeneralizingEstimator : 921/1225 [00:03<00:00,  320.79it/s]
 76%|███████▌  | Scoring GeneralizingEstimator : 933/1225 [00:03<00:00,  322.38it/s]
 77%|███████▋  | Scoring GeneralizingEstimator : 944/1225 [00:03<00:00,  322.19it/s]
 78%|███████▊  | Scoring GeneralizingEstimator : 955/1225 [00:03<00:00,  322.17it/s]
 79%|███████▉  | Scoring GeneralizingEstimator : 965/1225 [00:03<00:00,  320.67it/s]
 80%|███████▉  | Scoring GeneralizingEstimator : 976/1225 [00:03<00:00,  320.84it/s]
 81%|████████  | Scoring GeneralizingEstimator : 988/1225 [00:03<00:00,  322.42it/s]
 82%|████████▏ | Scoring GeneralizingEstimator : 1000/1225 [00:03<00:00,  323.72it/s]
 83%|████████▎ | Scoring GeneralizingEstimator : 1011/1225 [00:03<00:00,  323.37it/s]
 83%|████████▎ | Scoring GeneralizingEstimator : 1020/1225 [00:03<00:00,  320.57it/s]
 84%|████████▍ | Scoring GeneralizingEstimator : 1032/1225 [00:03<00:00,  322.27it/s]
 85%|████████▌ | Scoring GeneralizingEstimator : 1043/1225 [00:03<00:00,  322.37it/s]
 86%|████████▌ | Scoring GeneralizingEstimator : 1053/1225 [00:03<00:00,  321.04it/s]
 87%|████████▋ | Scoring GeneralizingEstimator : 1063/1225 [00:03<00:00,  319.52it/s]
 88%|████████▊ | Scoring GeneralizingEstimator : 1073/1225 [00:03<00:00,  318.35it/s]
 88%|████████▊ | Scoring GeneralizingEstimator : 1083/1225 [00:03<00:00,  317.19it/s]
 89%|████████▉ | Scoring GeneralizingEstimator : 1092/1225 [00:03<00:00,  314.62it/s]
 90%|████████▉ | Scoring GeneralizingEstimator : 1102/1225 [00:04<00:00,  312.71it/s]
 91%|█████████ | Scoring GeneralizingEstimator : 1114/1225 [00:04<00:00,  314.79it/s]
 92%|█████████▏| Scoring GeneralizingEstimator : 1125/1225 [00:04<00:00,  315.03it/s]
 93%|█████████▎| Scoring GeneralizingEstimator : 1135/1225 [00:04<00:00,  314.08it/s]
 93%|█████████▎| Scoring GeneralizingEstimator : 1144/1225 [00:04<00:00,  311.29it/s]
 94%|█████████▍| Scoring GeneralizingEstimator : 1153/1225 [00:04<00:00,  308.80it/s]
 95%|█████████▌| Scoring GeneralizingEstimator : 1164/1225 [00:04<00:00,  309.56it/s]
 96%|█████████▌| Scoring GeneralizingEstimator : 1177/1225 [00:04<00:00,  313.27it/s]
 97%|█████████▋| Scoring GeneralizingEstimator : 1188/1225 [00:04<00:00,  313.83it/s]
 98%|█████████▊| Scoring GeneralizingEstimator : 1197/1225 [00:04<00:00,  309.99it/s]
 99%|█████████▊| Scoring GeneralizingEstimator : 1208/1225 [00:04<00:00,  310.35it/s]
100%|█████████▉| Scoring GeneralizingEstimator : 1220/1225 [00:04<00:00,  312.32it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:04<00:00,  276.81it/s]

Plot

fig, ax = plt.subplots(layout="constrained")
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#

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

Estimated memory usage: 129 MB

Gallery generated by Sphinx-Gallery