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]
 11%|█▏        | Fitting GeneralizingEstimator : 4/35 [00:00<00:00,   59.25it/s]
 17%|█▋        | Fitting GeneralizingEstimator : 6/35 [00:00<00:00,   59.24it/s]
 23%|██▎       | Fitting GeneralizingEstimator : 8/35 [00:00<00:00,   59.22it/s]
 31%|███▏      | Fitting GeneralizingEstimator : 11/35 [00:00<00:00,   65.58it/s]
 40%|████      | Fitting GeneralizingEstimator : 14/35 [00:00<00:00,   69.93it/s]
 49%|████▊     | Fitting GeneralizingEstimator : 17/35 [00:00<00:00,   72.84it/s]
 54%|█████▍    | Fitting GeneralizingEstimator : 19/35 [00:00<00:00,   70.79it/s]
 63%|██████▎   | Fitting GeneralizingEstimator : 22/35 [00:00<00:00,   73.18it/s]
 69%|██████▊   | Fitting GeneralizingEstimator : 24/35 [00:00<00:00,   71.44it/s]
 77%|███████▋  | Fitting GeneralizingEstimator : 27/35 [00:00<00:00,   73.43it/s]
 83%|████████▎ | Fitting GeneralizingEstimator : 29/35 [00:00<00:00,   71.87it/s]
 91%|█████████▏| Fitting GeneralizingEstimator : 32/35 [00:00<00:00,   73.58it/s]
 97%|█████████▋| Fitting GeneralizingEstimator : 34/35 [00:00<00:00,   72.17it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   72.45it/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 : 11/1225 [00:00<00:03,  320.43it/s]
  2%|▏         | Scoring GeneralizingEstimator : 25/1225 [00:00<00:03,  365.41it/s]
  3%|▎         | Scoring GeneralizingEstimator : 40/1225 [00:00<00:03,  391.43it/s]
  4%|▍         | Scoring GeneralizingEstimator : 55/1225 [00:00<00:02,  403.69it/s]
  6%|▌         | Scoring GeneralizingEstimator : 69/1225 [00:00<00:02,  405.85it/s]
  7%|▋         | Scoring GeneralizingEstimator : 84/1225 [00:00<00:02,  413.13it/s]
  8%|▊         | Scoring GeneralizingEstimator : 98/1225 [00:00<00:02,  412.57it/s]
  9%|▉         | Scoring GeneralizingEstimator : 112/1225 [00:00<00:02,  412.71it/s]
 10%|█         | Scoring GeneralizingEstimator : 127/1225 [00:00<00:02,  416.96it/s]
 11%|█▏        | Scoring GeneralizingEstimator : 140/1225 [00:00<00:02,  413.05it/s]
 13%|█▎        | Scoring GeneralizingEstimator : 155/1225 [00:00<00:02,  416.41it/s]
 14%|█▍        | Scoring GeneralizingEstimator : 170/1225 [00:00<00:02,  419.47it/s]
 15%|█▌        | Scoring GeneralizingEstimator : 185/1225 [00:00<00:02,  421.69it/s]
 16%|█▋        | Scoring GeneralizingEstimator : 201/1225 [00:00<00:02,  426.05it/s]
 18%|█▊        | Scoring GeneralizingEstimator : 217/1225 [00:00<00:02,  430.30it/s]
 19%|█▊        | Scoring GeneralizingEstimator : 229/1225 [00:00<00:02,  423.01it/s]
 20%|█▉        | Scoring GeneralizingEstimator : 243/1225 [00:00<00:02,  422.27it/s]
 21%|██        | Scoring GeneralizingEstimator : 257/1225 [00:00<00:02,  421.45it/s]
 22%|██▏       | Scoring GeneralizingEstimator : 273/1225 [00:00<00:02,  425.28it/s]
 24%|██▎       | Scoring GeneralizingEstimator : 288/1225 [00:00<00:02,  426.75it/s]
 25%|██▍       | Scoring GeneralizingEstimator : 303/1225 [00:00<00:02,  428.05it/s]
 26%|██▌       | Scoring GeneralizingEstimator : 318/1225 [00:00<00:02,  429.26it/s]
 27%|██▋       | Scoring GeneralizingEstimator : 331/1225 [00:00<00:02,  425.95it/s]
 28%|██▊       | Scoring GeneralizingEstimator : 345/1225 [00:00<00:02,  424.63it/s]
 29%|██▉       | Scoring GeneralizingEstimator : 360/1225 [00:00<00:02,  425.49it/s]
 30%|███       | Scoring GeneralizingEstimator : 373/1225 [00:00<00:02,  422.63it/s]
 32%|███▏      | Scoring GeneralizingEstimator : 387/1225 [00:00<00:01,  421.70it/s]
 33%|███▎      | Scoring GeneralizingEstimator : 399/1225 [00:00<00:01,  417.36it/s]
 34%|███▎      | Scoring GeneralizingEstimator : 412/1225 [00:00<00:01,  415.02it/s]
 35%|███▍      | Scoring GeneralizingEstimator : 424/1225 [00:01<00:01,  411.14it/s]
 36%|███▌      | Scoring GeneralizingEstimator : 438/1225 [00:01<00:01,  411.36it/s]
 37%|███▋      | Scoring GeneralizingEstimator : 452/1225 [00:01<00:01,  411.25it/s]
 38%|███▊      | Scoring GeneralizingEstimator : 465/1225 [00:01<00:01,  409.35it/s]
 39%|███▉      | Scoring GeneralizingEstimator : 479/1225 [00:01<00:01,  409.65it/s]
 40%|████      | Scoring GeneralizingEstimator : 493/1225 [00:01<00:01,  409.96it/s]
 41%|████▏     | Scoring GeneralizingEstimator : 507/1225 [00:01<00:01,  409.77it/s]
 42%|████▏     | Scoring GeneralizingEstimator : 519/1225 [00:01<00:01,  406.52it/s]
 43%|████▎     | Scoring GeneralizingEstimator : 532/1225 [00:01<00:01,  405.23it/s]
 44%|████▍     | Scoring GeneralizingEstimator : 544/1225 [00:01<00:01,  402.29it/s]
 45%|████▌     | Scoring GeneralizingEstimator : 556/1225 [00:01<00:01,  399.60it/s]
 46%|████▋     | Scoring GeneralizingEstimator : 569/1225 [00:01<00:01,  398.79it/s]
 48%|████▊     | Scoring GeneralizingEstimator : 583/1225 [00:01<00:01,  399.66it/s]
 49%|████▊     | Scoring GeneralizingEstimator : 597/1225 [00:01<00:01,  399.82it/s]
 50%|████▉     | Scoring GeneralizingEstimator : 611/1225 [00:01<00:01,  400.57it/s]
 51%|█████     | Scoring GeneralizingEstimator : 625/1225 [00:01<00:01,  400.93it/s]
 52%|█████▏    | Scoring GeneralizingEstimator : 638/1225 [00:01<00:01,  399.64it/s]
 53%|█████▎    | Scoring GeneralizingEstimator : 650/1225 [00:01<00:01,  397.12it/s]
 54%|█████▍    | Scoring GeneralizingEstimator : 662/1225 [00:01<00:01,  394.87it/s]
 55%|█████▌    | Scoring GeneralizingEstimator : 675/1225 [00:01<00:01,  394.25it/s]
 56%|█████▌    | Scoring GeneralizingEstimator : 689/1225 [00:01<00:01,  394.69it/s]
 57%|█████▋    | Scoring GeneralizingEstimator : 702/1225 [00:01<00:01,  393.60it/s]
 58%|█████▊    | Scoring GeneralizingEstimator : 714/1225 [00:01<00:01,  391.48it/s]
 59%|█████▉    | Scoring GeneralizingEstimator : 726/1225 [00:01<00:01,  389.40it/s]
 60%|██████    | Scoring GeneralizingEstimator : 740/1225 [00:01<00:01,  390.53it/s]
 62%|██████▏   | Scoring GeneralizingEstimator : 754/1225 [00:01<00:01,  391.59it/s]
 63%|██████▎   | Scoring GeneralizingEstimator : 767/1225 [00:01<00:01,  391.18it/s]
 64%|██████▎   | Scoring GeneralizingEstimator : 779/1225 [00:01<00:01,  389.27it/s]
 65%|██████▍   | Scoring GeneralizingEstimator : 792/1225 [00:01<00:01,  389.05it/s]
 66%|██████▌   | Scoring GeneralizingEstimator : 805/1225 [00:02<00:01,  388.79it/s]
 67%|██████▋   | Scoring GeneralizingEstimator : 817/1225 [00:02<00:01,  386.74it/s]
 68%|██████▊   | Scoring GeneralizingEstimator : 829/1225 [00:02<00:01,  384.81it/s]
 69%|██████▊   | Scoring GeneralizingEstimator : 841/1225 [00:02<00:01,  383.26it/s]
 70%|██████▉   | Scoring GeneralizingEstimator : 854/1225 [00:02<00:00,  383.31it/s]
 71%|███████   | Scoring GeneralizingEstimator : 866/1225 [00:02<00:00,  381.63it/s]
 72%|███████▏  | Scoring GeneralizingEstimator : 876/1225 [00:02<00:00,  376.81it/s]
 73%|███████▎  | Scoring GeneralizingEstimator : 889/1225 [00:02<00:00,  377.12it/s]
 74%|███████▎  | Scoring GeneralizingEstimator : 902/1225 [00:02<00:00,  377.50it/s]
 75%|███████▍  | Scoring GeneralizingEstimator : 914/1225 [00:02<00:00,  376.13it/s]
 76%|███████▌  | Scoring GeneralizingEstimator : 927/1225 [00:02<00:00,  376.53it/s]
 77%|███████▋  | Scoring GeneralizingEstimator : 940/1225 [00:02<00:00,  376.60it/s]
 78%|███████▊  | Scoring GeneralizingEstimator : 952/1225 [00:02<00:00,  375.40it/s]
 79%|███████▉  | Scoring GeneralizingEstimator : 965/1225 [00:02<00:00,  375.41it/s]
 80%|███████▉  | Scoring GeneralizingEstimator : 979/1225 [00:02<00:00,  377.42it/s]
 81%|████████  | Scoring GeneralizingEstimator : 993/1225 [00:02<00:00,  379.12it/s]
 82%|████████▏ | Scoring GeneralizingEstimator : 1007/1225 [00:02<00:00,  380.57it/s]
 83%|████████▎ | Scoring GeneralizingEstimator : 1021/1225 [00:02<00:00,  382.18it/s]
 84%|████████▍ | Scoring GeneralizingEstimator : 1034/1225 [00:02<00:00,  382.15it/s]
 86%|████████▌ | Scoring GeneralizingEstimator : 1049/1225 [00:02<00:00,  385.19it/s]
 87%|████████▋ | Scoring GeneralizingEstimator : 1063/1225 [00:02<00:00,  386.34it/s]
 88%|████████▊ | Scoring GeneralizingEstimator : 1076/1225 [00:02<00:00,  385.96it/s]
 89%|████████▉ | Scoring GeneralizingEstimator : 1090/1225 [00:02<00:00,  387.30it/s]
 90%|█████████ | Scoring GeneralizingEstimator : 1103/1225 [00:02<00:00,  387.11it/s]
 91%|█████████ | Scoring GeneralizingEstimator : 1117/1225 [00:02<00:00,  388.39it/s]
 92%|█████████▏| Scoring GeneralizingEstimator : 1127/1225 [00:02<00:00,  383.77it/s]
 93%|█████████▎| Scoring GeneralizingEstimator : 1140/1225 [00:02<00:00,  383.64it/s]
 94%|█████████▍| Scoring GeneralizingEstimator : 1154/1225 [00:02<00:00,  384.98it/s]
 95%|█████████▌| Scoring GeneralizingEstimator : 1164/1225 [00:02<00:00,  380.34it/s]
 96%|█████████▌| Scoring GeneralizingEstimator : 1179/1225 [00:02<00:00,  383.47it/s]
 97%|█████████▋| Scoring GeneralizingEstimator : 1194/1225 [00:03<00:00,  386.39it/s]
 99%|█████████▉| Scoring GeneralizingEstimator : 1210/1225 [00:03<00:00,  390.53it/s]
100%|█████████▉| Scoring GeneralizingEstimator : 1224/1225 [00:03<00:00,  391.66it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:03<00:00,  395.31it/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 8.154 seconds)

Estimated memory usage: 129 MB

Gallery generated by Sphinx-Gallery