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.4s

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.15it/s]
 11%|█▏        | Fitting GeneralizingEstimator : 4/35 [00:00<00:00,   59.43it/s]
 17%|█▋        | Fitting GeneralizingEstimator : 6/35 [00:00<00:00,   59.36it/s]
 23%|██▎       | Fitting GeneralizingEstimator : 8/35 [00:00<00:00,   59.32it/s]
 31%|███▏      | Fitting GeneralizingEstimator : 11/35 [00:00<00:00,   65.81it/s]
 43%|████▎     | Fitting GeneralizingEstimator : 15/35 [00:00<00:00,   75.66it/s]
 49%|████▊     | Fitting GeneralizingEstimator : 17/35 [00:00<00:00,   72.95it/s]
 57%|█████▋    | Fitting GeneralizingEstimator : 20/35 [00:00<00:00,   75.30it/s]
 66%|██████▌   | Fitting GeneralizingEstimator : 23/35 [00:00<00:00,   77.11it/s]
 74%|███████▍  | Fitting GeneralizingEstimator : 26/35 [00:00<00:00,   78.43it/s]
 80%|████████  | Fitting GeneralizingEstimator : 28/35 [00:00<00:00,   76.21it/s]
 89%|████████▊ | Fitting GeneralizingEstimator : 31/35 [00:00<00:00,   77.57it/s]
 94%|█████████▍| Fitting GeneralizingEstimator : 33/35 [00:00<00:00,   75.68it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   77.75it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   76.38it/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 : 12/1225 [00:00<00:03,  351.29it/s]
  2%|▏         | Scoring GeneralizingEstimator : 27/1225 [00:00<00:03,  398.17it/s]
  3%|▎         | Scoring GeneralizingEstimator : 42/1225 [00:00<00:02,  413.35it/s]
  4%|▍         | Scoring GeneralizingEstimator : 55/1225 [00:00<00:02,  405.29it/s]
  6%|▌         | Scoring GeneralizingEstimator : 70/1225 [00:00<00:02,  413.43it/s]
  7%|▋         | Scoring GeneralizingEstimator : 86/1225 [00:00<00:02,  424.69it/s]
  8%|▊         | Scoring GeneralizingEstimator : 101/1225 [00:00<00:02,  427.06it/s]
 10%|▉         | Scoring GeneralizingEstimator : 117/1225 [00:00<00:02,  433.90it/s]
 11%|█         | Scoring GeneralizingEstimator : 132/1225 [00:00<00:02,  434.58it/s]
 12%|█▏        | Scoring GeneralizingEstimator : 148/1225 [00:00<00:02,  439.09it/s]
 13%|█▎        | Scoring GeneralizingEstimator : 164/1225 [00:00<00:02,  442.25it/s]
 15%|█▍        | Scoring GeneralizingEstimator : 179/1225 [00:00<00:02,  441.70it/s]
 16%|█▌        | Scoring GeneralizingEstimator : 195/1225 [00:00<00:02,  444.97it/s]
 17%|█▋        | Scoring GeneralizingEstimator : 210/1225 [00:00<00:02,  444.66it/s]
 18%|█▊        | Scoring GeneralizingEstimator : 226/1225 [00:00<00:02,  447.39it/s]
 19%|█▉        | Scoring GeneralizingEstimator : 238/1225 [00:00<00:02,  439.15it/s]
 20%|██        | Scoring GeneralizingEstimator : 250/1225 [00:00<00:02,  431.12it/s]
 22%|██▏       | Scoring GeneralizingEstimator : 266/1225 [00:00<00:02,  434.61it/s]
 23%|██▎       | Scoring GeneralizingEstimator : 282/1225 [00:00<00:02,  437.59it/s]
 24%|██▍       | Scoring GeneralizingEstimator : 297/1225 [00:00<00:02,  438.04it/s]
 26%|██▌       | Scoring GeneralizingEstimator : 313/1225 [00:00<00:02,  440.54it/s]
 27%|██▋       | Scoring GeneralizingEstimator : 328/1225 [00:00<00:02,  440.70it/s]
 28%|██▊       | Scoring GeneralizingEstimator : 344/1225 [00:00<00:01,  443.02it/s]
 29%|██▉       | Scoring GeneralizingEstimator : 359/1225 [00:00<00:01,  442.95it/s]
 31%|███       | Scoring GeneralizingEstimator : 374/1225 [00:00<00:01,  443.00it/s]
 32%|███▏      | Scoring GeneralizingEstimator : 389/1225 [00:00<00:01,  442.87it/s]
 33%|███▎      | Scoring GeneralizingEstimator : 404/1225 [00:00<00:01,  442.92it/s]
 34%|███▍      | Scoring GeneralizingEstimator : 419/1225 [00:00<00:01,  442.97it/s]
 35%|███▌      | Scoring GeneralizingEstimator : 430/1225 [00:00<00:01,  435.38it/s]
 36%|███▌      | Scoring GeneralizingEstimator : 442/1225 [00:01<00:01,  430.31it/s]
 37%|███▋      | Scoring GeneralizingEstimator : 457/1225 [00:01<00:01,  430.90it/s]
 39%|███▊      | Scoring GeneralizingEstimator : 472/1225 [00:01<00:01,  431.64it/s]
 40%|███▉      | Scoring GeneralizingEstimator : 487/1225 [00:01<00:01,  432.03it/s]
 41%|████      | Scoring GeneralizingEstimator : 502/1225 [00:01<00:01,  432.59it/s]
 42%|████▏     | Scoring GeneralizingEstimator : 517/1225 [00:01<00:01,  433.08it/s]
 44%|████▎     | Scoring GeneralizingEstimator : 533/1225 [00:01<00:01,  434.96it/s]
 45%|████▍     | Scoring GeneralizingEstimator : 548/1225 [00:01<00:01,  435.52it/s]
 46%|████▌     | Scoring GeneralizingEstimator : 564/1225 [00:01<00:01,  437.46it/s]
 47%|████▋     | Scoring GeneralizingEstimator : 579/1225 [00:01<00:01,  437.76it/s]
 48%|████▊     | Scoring GeneralizingEstimator : 594/1225 [00:01<00:01,  438.05it/s]
 50%|████▉     | Scoring GeneralizingEstimator : 609/1225 [00:01<00:01,  438.21it/s]
 51%|█████     | Scoring GeneralizingEstimator : 625/1225 [00:01<00:01,  440.24it/s]
 52%|█████▏    | Scoring GeneralizingEstimator : 641/1225 [00:01<00:01,  441.79it/s]
 54%|█████▎    | Scoring GeneralizingEstimator : 657/1225 [00:01<00:01,  443.32it/s]
 55%|█████▍    | Scoring GeneralizingEstimator : 672/1225 [00:01<00:01,  443.22it/s]
 56%|█████▌    | Scoring GeneralizingEstimator : 688/1225 [00:01<00:01,  444.90it/s]
 57%|█████▋    | Scoring GeneralizingEstimator : 704/1225 [00:01<00:01,  446.14it/s]
 59%|█████▉    | Scoring GeneralizingEstimator : 720/1225 [00:01<00:01,  447.62it/s]
 60%|██████    | Scoring GeneralizingEstimator : 736/1225 [00:01<00:01,  448.92it/s]
 61%|██████▏   | Scoring GeneralizingEstimator : 752/1225 [00:01<00:01,  450.17it/s]
 63%|██████▎   | Scoring GeneralizingEstimator : 768/1225 [00:01<00:01,  451.41it/s]
 64%|██████▍   | Scoring GeneralizingEstimator : 784/1225 [00:01<00:00,  452.61it/s]
 65%|██████▌   | Scoring GeneralizingEstimator : 800/1225 [00:01<00:00,  453.73it/s]
 67%|██████▋   | Scoring GeneralizingEstimator : 816/1225 [00:01<00:00,  454.71it/s]
 68%|██████▊   | Scoring GeneralizingEstimator : 833/1225 [00:01<00:00,  457.01it/s]
 69%|██████▉   | Scoring GeneralizingEstimator : 849/1225 [00:01<00:00,  457.84it/s]
 71%|███████   | Scoring GeneralizingEstimator : 865/1225 [00:01<00:00,  458.60it/s]
 72%|███████▏  | Scoring GeneralizingEstimator : 881/1225 [00:01<00:00,  459.37it/s]
 73%|███████▎  | Scoring GeneralizingEstimator : 897/1225 [00:02<00:00,  459.65it/s]
 75%|███████▍  | Scoring GeneralizingEstimator : 913/1225 [00:02<00:00,  460.24it/s]
 76%|███████▌  | Scoring GeneralizingEstimator : 929/1225 [00:02<00:00,  460.85it/s]
 77%|███████▋  | Scoring GeneralizingEstimator : 945/1225 [00:02<00:00,  461.52it/s]
 78%|███████▊  | Scoring GeneralizingEstimator : 961/1225 [00:02<00:00,  462.02it/s]
 80%|███████▉  | Scoring GeneralizingEstimator : 977/1225 [00:02<00:00,  462.44it/s]
 81%|████████  | Scoring GeneralizingEstimator : 993/1225 [00:02<00:00,  463.06it/s]
 82%|████████▏ | Scoring GeneralizingEstimator : 1009/1225 [00:02<00:00,  463.63it/s]
 84%|████████▎ | Scoring GeneralizingEstimator : 1025/1225 [00:02<00:00,  464.01it/s]
 85%|████████▍ | Scoring GeneralizingEstimator : 1041/1225 [00:02<00:00,  464.40it/s]
 86%|████████▋ | Scoring GeneralizingEstimator : 1057/1225 [00:02<00:00,  464.76it/s]
 88%|████████▊ | Scoring GeneralizingEstimator : 1073/1225 [00:02<00:00,  465.19it/s]
 89%|████████▉ | Scoring GeneralizingEstimator : 1090/1225 [00:02<00:00,  467.18it/s]
 90%|█████████ | Scoring GeneralizingEstimator : 1106/1225 [00:02<00:00,  467.35it/s]
 92%|█████████▏| Scoring GeneralizingEstimator : 1122/1225 [00:02<00:00,  467.71it/s]
 93%|█████████▎| Scoring GeneralizingEstimator : 1138/1225 [00:02<00:00,  468.07it/s]
 94%|█████████▍| Scoring GeneralizingEstimator : 1154/1225 [00:02<00:00,  468.34it/s]
 96%|█████████▌| Scoring GeneralizingEstimator : 1170/1225 [00:02<00:00,  468.65it/s]
 97%|█████████▋| Scoring GeneralizingEstimator : 1186/1225 [00:02<00:00,  468.96it/s]
 98%|█████████▊| Scoring GeneralizingEstimator : 1202/1225 [00:02<00:00,  469.07it/s]
 99%|█████████▉| Scoring GeneralizingEstimator : 1218/1225 [00:02<00:00,  469.29it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  454.41it/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 7.556 seconds)

Estimated memory usage: 128 MB

Gallery generated by Sphinx-Gallery