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.2s
[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,   28.93it/s]
  9%|▊         | Fitting GeneralizingEstimator : 3/35 [00:00<00:00,   44.19it/s]
 14%|█▍        | Fitting GeneralizingEstimator : 5/35 [00:00<00:00,   49.35it/s]
 20%|██        | Fitting GeneralizingEstimator : 7/35 [00:00<00:00,   51.92it/s]
 26%|██▌       | Fitting GeneralizingEstimator : 9/35 [00:00<00:00,   53.50it/s]
 34%|███▍      | Fitting GeneralizingEstimator : 12/35 [00:00<00:00,   60.11it/s]
 43%|████▎     | Fitting GeneralizingEstimator : 15/35 [00:00<00:00,   64.84it/s]
 51%|█████▏    | Fitting GeneralizingEstimator : 18/35 [00:00<00:00,   68.35it/s]
 57%|█████▋    | Fitting GeneralizingEstimator : 20/35 [00:00<00:00,   67.09it/s]
 66%|██████▌   | Fitting GeneralizingEstimator : 23/35 [00:00<00:00,   69.75it/s]
 71%|███████▏  | Fitting GeneralizingEstimator : 25/35 [00:00<00:00,   68.46it/s]
 74%|███████▍  | Fitting GeneralizingEstimator : 26/35 [00:00<00:00,   64.24it/s]
 80%|████████  | Fitting GeneralizingEstimator : 28/35 [00:00<00:00,   63.72it/s]
 86%|████████▌ | Fitting GeneralizingEstimator : 30/35 [00:00<00:00,   63.25it/s]
 91%|█████████▏| Fitting GeneralizingEstimator : 32/35 [00:00<00:00,   62.84it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   65.72it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   64.80it/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]
  1%|          | Scoring GeneralizingEstimator : 9/1225 [00:00<00:04,  263.23it/s]
  1%|▏         | Scoring GeneralizingEstimator : 17/1225 [00:00<00:04,  248.19it/s]
  2%|▏         | Scoring GeneralizingEstimator : 25/1225 [00:00<00:04,  243.55it/s]
  3%|▎         | Scoring GeneralizingEstimator : 34/1225 [00:00<00:04,  249.23it/s]
  3%|▎         | Scoring GeneralizingEstimator : 42/1225 [00:00<00:04,  246.38it/s]
  4%|▍         | Scoring GeneralizingEstimator : 50/1225 [00:00<00:04,  244.59it/s]
  5%|▍         | Scoring GeneralizingEstimator : 58/1225 [00:00<00:04,  243.14it/s]
  6%|▌         | Scoring GeneralizingEstimator : 69/1225 [00:00<00:04,  255.06it/s]
  6%|▋         | Scoring GeneralizingEstimator : 78/1225 [00:00<00:04,  256.36it/s]
  7%|▋         | Scoring GeneralizingEstimator : 87/1225 [00:00<00:04,  257.43it/s]
  8%|▊         | Scoring GeneralizingEstimator : 97/1225 [00:00<00:04,  261.68it/s]
  9%|▊         | Scoring GeneralizingEstimator : 105/1225 [00:00<00:04,  258.79it/s]
  9%|▉         | Scoring GeneralizingEstimator : 114/1225 [00:00<00:04,  259.37it/s]
 10%|█         | Scoring GeneralizingEstimator : 125/1225 [00:00<00:04,  265.11it/s]
 11%|█         | Scoring GeneralizingEstimator : 133/1225 [00:00<00:04,  262.13it/s]
 12%|█▏        | Scoring GeneralizingEstimator : 142/1225 [00:00<00:04,  262.02it/s]
 12%|█▏        | Scoring GeneralizingEstimator : 153/1225 [00:00<00:04,  266.85it/s]
 13%|█▎        | Scoring GeneralizingEstimator : 161/1225 [00:00<00:04,  264.26it/s]
 14%|█▍        | Scoring GeneralizingEstimator : 174/1225 [00:00<00:03,  273.42it/s]
 15%|█▌        | Scoring GeneralizingEstimator : 185/1225 [00:00<00:03,  277.34it/s]
 16%|█▌        | Scoring GeneralizingEstimator : 193/1225 [00:00<00:03,  274.18it/s]
 17%|█▋        | Scoring GeneralizingEstimator : 207/1225 [00:00<00:03,  284.40it/s]
 18%|█▊        | Scoring GeneralizingEstimator : 219/1225 [00:00<00:03,  289.29it/s]
 19%|█▊        | Scoring GeneralizingEstimator : 228/1225 [00:00<00:03,  287.55it/s]
 20%|█▉        | Scoring GeneralizingEstimator : 242/1225 [00:00<00:03,  296.19it/s]
 21%|██        | Scoring GeneralizingEstimator : 255/1225 [00:00<00:03,  302.04it/s]
 22%|██▏       | Scoring GeneralizingEstimator : 264/1225 [00:00<00:03,  299.61it/s]
 22%|██▏       | Scoring GeneralizingEstimator : 275/1225 [00:00<00:03,  301.19it/s]
 23%|██▎       | Scoring GeneralizingEstimator : 284/1225 [00:00<00:03,  298.93it/s]
 24%|██▍       | Scoring GeneralizingEstimator : 293/1225 [00:01<00:03,  296.54it/s]
 25%|██▍       | Scoring GeneralizingEstimator : 304/1225 [00:01<00:03,  298.25it/s]
 26%|██▌       | Scoring GeneralizingEstimator : 316/1225 [00:01<00:03,  301.72it/s]
 27%|██▋       | Scoring GeneralizingEstimator : 325/1225 [00:01<00:03,  299.31it/s]
 27%|██▋       | Scoring GeneralizingEstimator : 335/1225 [00:01<00:02,  299.09it/s]
 28%|██▊       | Scoring GeneralizingEstimator : 347/1225 [00:01<00:02,  302.31it/s]
 29%|██▉       | Scoring GeneralizingEstimator : 357/1225 [00:01<00:02,  301.88it/s]
 30%|███       | Scoring GeneralizingEstimator : 368/1225 [00:01<00:02,  302.77it/s]
 31%|███       | Scoring GeneralizingEstimator : 377/1225 [00:01<00:02,  300.38it/s]
 31%|███▏      | Scoring GeneralizingEstimator : 384/1225 [00:01<00:02,  295.00it/s]
 32%|███▏      | Scoring GeneralizingEstimator : 390/1225 [00:01<00:02,  288.16it/s]
 32%|███▏      | Scoring GeneralizingEstimator : 396/1225 [00:01<00:02,  281.42it/s]
 33%|███▎      | Scoring GeneralizingEstimator : 406/1225 [00:01<00:02,  282.17it/s]
 34%|███▍      | Scoring GeneralizingEstimator : 416/1225 [00:01<00:02,  282.91it/s]
 35%|███▍      | Scoring GeneralizingEstimator : 425/1225 [00:01<00:02,  281.96it/s]
 35%|███▌      | Scoring GeneralizingEstimator : 433/1225 [00:01<00:02,  279.41it/s]
 36%|███▌      | Scoring GeneralizingEstimator : 442/1225 [00:01<00:02,  278.68it/s]
 37%|███▋      | Scoring GeneralizingEstimator : 450/1225 [00:01<00:02,  276.22it/s]
 37%|███▋      | Scoring GeneralizingEstimator : 459/1225 [00:01<00:02,  275.62it/s]
 38%|███▊      | Scoring GeneralizingEstimator : 471/1225 [00:01<00:02,  279.85it/s]
 39%|███▉      | Scoring GeneralizingEstimator : 480/1225 [00:01<00:02,  278.19it/s]
 40%|███▉      | Scoring GeneralizingEstimator : 488/1225 [00:01<00:02,  275.92it/s]
 41%|████      | Scoring GeneralizingEstimator : 497/1225 [00:01<00:02,  275.43it/s]
 41%|████▏     | Scoring GeneralizingEstimator : 507/1225 [00:01<00:02,  275.42it/s]
 42%|████▏     | Scoring GeneralizingEstimator : 517/1225 [00:01<00:02,  276.38it/s]
 43%|████▎     | Scoring GeneralizingEstimator : 529/1225 [00:01<00:02,  280.46it/s]
 44%|████▍     | Scoring GeneralizingEstimator : 538/1225 [00:01<00:02,  279.69it/s]
 45%|████▍     | Scoring GeneralizingEstimator : 550/1225 [00:01<00:02,  283.62it/s]
 46%|████▌     | Scoring GeneralizingEstimator : 564/1225 [00:01<00:02,  290.44it/s]
 47%|████▋     | Scoring GeneralizingEstimator : 573/1225 [00:02<00:02,  288.92it/s]
 48%|████▊     | Scoring GeneralizingEstimator : 586/1225 [00:02<00:02,  293.83it/s]
 49%|████▉     | Scoring GeneralizingEstimator : 599/1225 [00:02<00:02,  297.22it/s]
 50%|████▉     | Scoring GeneralizingEstimator : 609/1225 [00:02<00:02,  295.26it/s]
 50%|█████     | Scoring GeneralizingEstimator : 618/1225 [00:02<00:02,  293.71it/s]
 51%|█████▏    | Scoring GeneralizingEstimator : 628/1225 [00:02<00:02,  293.80it/s]
 52%|█████▏    | Scoring GeneralizingEstimator : 637/1225 [00:02<00:02,  292.35it/s]
 53%|█████▎    | Scoring GeneralizingEstimator : 648/1225 [00:02<00:01,  293.90it/s]
 54%|█████▍    | Scoring GeneralizingEstimator : 659/1225 [00:02<00:01,  294.91it/s]
 55%|█████▍    | Scoring GeneralizingEstimator : 669/1225 [00:02<00:01,  294.56it/s]
 56%|█████▌    | Scoring GeneralizingEstimator : 681/1225 [00:02<00:01,  297.48it/s]
 56%|█████▋    | Scoring GeneralizingEstimator : 691/1225 [00:02<00:01,  297.33it/s]
 57%|█████▋    | Scoring GeneralizingEstimator : 702/1225 [00:02<00:01,  298.60it/s]
 58%|█████▊    | Scoring GeneralizingEstimator : 712/1225 [00:02<00:01,  298.26it/s]
 59%|█████▉    | Scoring GeneralizingEstimator : 720/1225 [00:02<00:01,  295.14it/s]
 60%|█████▉    | Scoring GeneralizingEstimator : 729/1225 [00:02<00:01,  293.65it/s]
 60%|██████    | Scoring GeneralizingEstimator : 739/1225 [00:02<00:01,  293.77it/s]
 61%|██████    | Scoring GeneralizingEstimator : 747/1225 [00:02<00:01,  290.75it/s]
 62%|██████▏   | Scoring GeneralizingEstimator : 757/1225 [00:02<00:01,  290.90it/s]
 63%|██████▎   | Scoring GeneralizingEstimator : 766/1225 [00:02<00:01,  289.61it/s]
 63%|██████▎   | Scoring GeneralizingEstimator : 775/1225 [00:02<00:01,  288.41it/s]
 64%|██████▍   | Scoring GeneralizingEstimator : 784/1225 [00:02<00:01,  287.26it/s]
 65%|██████▍   | Scoring GeneralizingEstimator : 795/1225 [00:02<00:01,  288.95it/s]
 66%|██████▌   | Scoring GeneralizingEstimator : 805/1225 [00:02<00:01,  289.22it/s]
 67%|██████▋   | Scoring GeneralizingEstimator : 818/1225 [00:02<00:01,  293.99it/s]
 68%|██████▊   | Scoring GeneralizingEstimator : 833/1225 [00:02<00:01,  301.30it/s]
 69%|██████▊   | Scoring GeneralizingEstimator : 842/1225 [00:02<00:01,  299.40it/s]
 70%|███████   | Scoring GeneralizingEstimator : 858/1225 [00:02<00:01,  307.94it/s]
 71%|███████▏  | Scoring GeneralizingEstimator : 875/1225 [00:02<00:01,  317.68it/s]
 72%|███████▏  | Scoring GeneralizingEstimator : 884/1225 [00:03<00:01,  315.11it/s]
 73%|███████▎  | Scoring GeneralizingEstimator : 896/1225 [00:03<00:01,  317.08it/s]
 74%|███████▍  | Scoring GeneralizingEstimator : 907/1225 [00:03<00:01,  317.51it/s]
 75%|███████▍  | Scoring GeneralizingEstimator : 916/1225 [00:03<00:00,  314.93it/s]
 76%|███████▌  | Scoring GeneralizingEstimator : 929/1225 [00:03<00:00,  318.38it/s]
 77%|███████▋  | Scoring GeneralizingEstimator : 940/1225 [00:03<00:00,  318.65it/s]
 77%|███████▋  | Scoring GeneralizingEstimator : 948/1225 [00:03<00:00,  314.44it/s]
 78%|███████▊  | Scoring GeneralizingEstimator : 961/1225 [00:03<00:00,  317.92it/s]
 80%|███████▉  | Scoring GeneralizingEstimator : 974/1225 [00:03<00:00,  321.10it/s]
 80%|████████  | Scoring GeneralizingEstimator : 982/1225 [00:03<00:00,  316.87it/s]
 81%|████████  | Scoring GeneralizingEstimator : 992/1225 [00:03<00:00,  315.80it/s]
 82%|████████▏ | Scoring GeneralizingEstimator : 1003/1225 [00:03<00:00,  313.15it/s]
 83%|████████▎ | Scoring GeneralizingEstimator : 1015/1225 [00:03<00:00,  315.18it/s]
 84%|████████▍ | Scoring GeneralizingEstimator : 1029/1225 [00:03<00:00,  320.09it/s]
 85%|████████▍ | Scoring GeneralizingEstimator : 1040/1225 [00:03<00:00,  320.15it/s]
 85%|████████▌ | Scoring GeneralizingEstimator : 1047/1225 [00:03<00:00,  312.67it/s]
 86%|████████▌ | Scoring GeneralizingEstimator : 1056/1225 [00:03<00:00,  310.34it/s]
 87%|████████▋ | Scoring GeneralizingEstimator : 1064/1225 [00:03<00:00,  306.73it/s]
 88%|████████▊ | Scoring GeneralizingEstimator : 1072/1225 [00:03<00:00,  303.24it/s]
 88%|████████▊ | Scoring GeneralizingEstimator : 1081/1225 [00:03<00:00,  301.37it/s]
 89%|████████▉ | Scoring GeneralizingEstimator : 1092/1225 [00:03<00:00,  302.51it/s]
 90%|████████▉ | Scoring GeneralizingEstimator : 1102/1225 [00:03<00:00,  302.13it/s]
 91%|█████████ | Scoring GeneralizingEstimator : 1113/1225 [00:03<00:00,  303.21it/s]
 92%|█████████▏| Scoring GeneralizingEstimator : 1123/1225 [00:03<00:00,  302.39it/s]
 93%|█████████▎| Scoring GeneralizingEstimator : 1134/1225 [00:03<00:00,  303.11it/s]
 93%|█████████▎| Scoring GeneralizingEstimator : 1143/1225 [00:03<00:00,  301.10it/s]
 94%|█████████▍| Scoring GeneralizingEstimator : 1152/1225 [00:03<00:00,  299.33it/s]
 95%|█████████▍| Scoring GeneralizingEstimator : 1163/1225 [00:03<00:00,  300.34it/s]
 96%|█████████▌| Scoring GeneralizingEstimator : 1173/1225 [00:03<00:00,  300.11it/s]
 96%|█████████▋| Scoring GeneralizingEstimator : 1182/1225 [00:04<00:00,  298.08it/s]
 97%|█████████▋| Scoring GeneralizingEstimator : 1191/1225 [00:04<00:00,  296.39it/s]
 98%|█████████▊| Scoring GeneralizingEstimator : 1204/1225 [00:04<00:00,  300.44it/s]
 99%|█████████▉| Scoring GeneralizingEstimator : 1214/1225 [00:04<00:00,  300.21it/s]
100%|█████████▉| Scoring GeneralizingEstimator : 1224/1225 [00:04<00:00,  300.01it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:04<00:00,  295.62it/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.800 seconds)

Estimated memory usage: 141 MB

Gallery generated by Sphinx-Gallery