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.2s
[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]
  6%|▌         | Fitting GeneralizingEstimator : 2/35 [00:00<00:00,   58.35it/s]
 14%|█▍        | Fitting GeneralizingEstimator : 5/35 [00:00<00:00,   73.87it/s]
 20%|██        | Fitting GeneralizingEstimator : 7/35 [00:00<00:00,   68.86it/s]
 31%|███▏      | Fitting GeneralizingEstimator : 11/35 [00:00<00:00,   82.25it/s]
 43%|████▎     | Fitting GeneralizingEstimator : 15/35 [00:00<00:00,   90.29it/s]
 51%|█████▏    | Fitting GeneralizingEstimator : 18/35 [00:00<00:00,   90.05it/s]
 63%|██████▎   | Fitting GeneralizingEstimator : 22/35 [00:00<00:00,   94.80it/s]
 71%|███████▏  | Fitting GeneralizingEstimator : 25/35 [00:00<00:00,   93.95it/s]
 80%|████████  | Fitting GeneralizingEstimator : 28/35 [00:00<00:00,   93.29it/s]
 89%|████████▊ | Fitting GeneralizingEstimator : 31/35 [00:00<00:00,   92.78it/s]
 97%|█████████▋| Fitting GeneralizingEstimator : 34/35 [00:00<00:00,   92.35it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   93.60it/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 : 13/1225 [00:00<00:03,  378.00it/s]
  2%|▏         | Scoring GeneralizingEstimator : 30/1225 [00:00<00:02,  441.53it/s]
  4%|▍         | Scoring GeneralizingEstimator : 47/1225 [00:00<00:02,  461.27it/s]
  5%|▌         | Scoring GeneralizingEstimator : 63/1225 [00:00<00:02,  465.11it/s]
  7%|▋         | Scoring GeneralizingEstimator : 80/1225 [00:00<00:02,  472.80it/s]
  8%|▊         | Scoring GeneralizingEstimator : 97/1225 [00:00<00:02,  478.90it/s]
  9%|▉         | Scoring GeneralizingEstimator : 114/1225 [00:00<00:02,  482.70it/s]
 11%|█         | Scoring GeneralizingEstimator : 131/1225 [00:00<00:02,  484.83it/s]
 12%|█▏        | Scoring GeneralizingEstimator : 148/1225 [00:00<00:02,  486.89it/s]
 13%|█▎        | Scoring GeneralizingEstimator : 165/1225 [00:00<00:02,  488.32it/s]
 15%|█▍        | Scoring GeneralizingEstimator : 182/1225 [00:00<00:02,  489.37it/s]
 16%|█▌        | Scoring GeneralizingEstimator : 198/1225 [00:00<00:02,  487.77it/s]
 18%|█▊        | Scoring GeneralizingEstimator : 215/1225 [00:00<00:02,  489.00it/s]
 19%|█▉        | Scoring GeneralizingEstimator : 232/1225 [00:00<00:02,  489.95it/s]
 20%|██        | Scoring GeneralizingEstimator : 249/1225 [00:00<00:01,  490.57it/s]
 22%|██▏       | Scoring GeneralizingEstimator : 266/1225 [00:00<00:01,  491.80it/s]
 23%|██▎       | Scoring GeneralizingEstimator : 283/1225 [00:00<00:01,  492.89it/s]
 24%|██▍       | Scoring GeneralizingEstimator : 300/1225 [00:00<00:01,  493.48it/s]
 26%|██▌       | Scoring GeneralizingEstimator : 316/1225 [00:00<00:01,  491.97it/s]
 27%|██▋       | Scoring GeneralizingEstimator : 333/1225 [00:00<00:01,  492.99it/s]
 29%|██▊       | Scoring GeneralizingEstimator : 350/1225 [00:00<00:01,  493.86it/s]
 30%|██▉       | Scoring GeneralizingEstimator : 367/1225 [00:00<00:01,  494.34it/s]
 31%|███▏      | Scoring GeneralizingEstimator : 384/1225 [00:00<00:01,  494.84it/s]
 33%|███▎      | Scoring GeneralizingEstimator : 400/1225 [00:00<00:01,  493.49it/s]
 34%|███▍      | Scoring GeneralizingEstimator : 417/1225 [00:00<00:01,  494.24it/s]
 35%|███▌      | Scoring GeneralizingEstimator : 434/1225 [00:00<00:01,  494.93it/s]
 37%|███▋      | Scoring GeneralizingEstimator : 451/1225 [00:00<00:01,  495.61it/s]
 38%|███▊      | Scoring GeneralizingEstimator : 468/1225 [00:00<00:01,  495.93it/s]
 40%|███▉      | Scoring GeneralizingEstimator : 485/1225 [00:00<00:01,  496.13it/s]
 41%|████      | Scoring GeneralizingEstimator : 501/1225 [00:01<00:01,  494.76it/s]
 42%|████▏     | Scoring GeneralizingEstimator : 518/1225 [00:01<00:01,  495.32it/s]
 44%|████▎     | Scoring GeneralizingEstimator : 535/1225 [00:01<00:01,  495.32it/s]
 45%|████▌     | Scoring GeneralizingEstimator : 552/1225 [00:01<00:01,  495.51it/s]
 46%|████▋     | Scoring GeneralizingEstimator : 569/1225 [00:01<00:01,  495.55it/s]
 48%|████▊     | Scoring GeneralizingEstimator : 586/1225 [00:01<00:01,  496.01it/s]
 49%|████▉     | Scoring GeneralizingEstimator : 603/1225 [00:01<00:01,  496.50it/s]
 51%|█████     | Scoring GeneralizingEstimator : 620/1225 [00:01<00:01,  497.01it/s]
 52%|█████▏    | Scoring GeneralizingEstimator : 637/1225 [00:01<00:01,  497.20it/s]
 53%|█████▎    | Scoring GeneralizingEstimator : 654/1225 [00:01<00:01,  497.44it/s]
 55%|█████▍    | Scoring GeneralizingEstimator : 671/1225 [00:01<00:01,  497.59it/s]
 56%|█████▌    | Scoring GeneralizingEstimator : 688/1225 [00:01<00:01,  497.79it/s]
 57%|█████▋    | Scoring GeneralizingEstimator : 704/1225 [00:01<00:01,  496.32it/s]
 59%|█████▉    | Scoring GeneralizingEstimator : 721/1225 [00:01<00:01,  496.63it/s]
 60%|██████    | Scoring GeneralizingEstimator : 738/1225 [00:01<00:00,  496.63it/s]
 62%|██████▏   | Scoring GeneralizingEstimator : 755/1225 [00:01<00:00,  496.69it/s]
 63%|██████▎   | Scoring GeneralizingEstimator : 772/1225 [00:01<00:00,  496.56it/s]
 64%|██████▍   | Scoring GeneralizingEstimator : 789/1225 [00:01<00:00,  496.61it/s]
 66%|██████▌   | Scoring GeneralizingEstimator : 806/1225 [00:01<00:00,  496.71it/s]
 67%|██████▋   | Scoring GeneralizingEstimator : 823/1225 [00:01<00:00,  496.80it/s]
 69%|██████▊   | Scoring GeneralizingEstimator : 840/1225 [00:01<00:00,  496.79it/s]
 70%|██████▉   | Scoring GeneralizingEstimator : 857/1225 [00:01<00:00,  496.69it/s]
 71%|███████▏  | Scoring GeneralizingEstimator : 874/1225 [00:01<00:00,  496.76it/s]
 73%|███████▎  | Scoring GeneralizingEstimator : 891/1225 [00:01<00:00,  497.09it/s]
 74%|███████▍  | Scoring GeneralizingEstimator : 908/1225 [00:01<00:00,  497.49it/s]
 75%|███████▌  | Scoring GeneralizingEstimator : 924/1225 [00:01<00:00,  496.01it/s]
 77%|███████▋  | Scoring GeneralizingEstimator : 941/1225 [00:01<00:00,  496.06it/s]
 78%|███████▊  | Scoring GeneralizingEstimator : 958/1225 [00:01<00:00,  496.53it/s]
 80%|███████▉  | Scoring GeneralizingEstimator : 975/1225 [00:01<00:00,  496.91it/s]
 81%|████████  | Scoring GeneralizingEstimator : 992/1225 [00:02<00:00,  497.36it/s]
 82%|████████▏ | Scoring GeneralizingEstimator : 1008/1225 [00:02<00:00,  496.17it/s]
 84%|████████▎ | Scoring GeneralizingEstimator : 1025/1225 [00:02<00:00,  496.30it/s]
 85%|████████▌ | Scoring GeneralizingEstimator : 1042/1225 [00:02<00:00,  496.63it/s]
 86%|████████▋ | Scoring GeneralizingEstimator : 1059/1225 [00:02<00:00,  497.09it/s]
 88%|████████▊ | Scoring GeneralizingEstimator : 1076/1225 [00:02<00:00,  497.28it/s]
 89%|████████▉ | Scoring GeneralizingEstimator : 1093/1225 [00:02<00:00,  497.35it/s]
 91%|█████████ | Scoring GeneralizingEstimator : 1110/1225 [00:02<00:00,  497.47it/s]
 92%|█████████▏| Scoring GeneralizingEstimator : 1126/1225 [00:02<00:00,  496.34it/s]
 93%|█████████▎| Scoring GeneralizingEstimator : 1143/1225 [00:02<00:00,  496.55it/s]
 95%|█████████▍| Scoring GeneralizingEstimator : 1160/1225 [00:02<00:00,  496.65it/s]
 96%|█████████▌| Scoring GeneralizingEstimator : 1177/1225 [00:02<00:00,  496.75it/s]
 97%|█████████▋| Scoring GeneralizingEstimator : 1194/1225 [00:02<00:00,  497.12it/s]
 99%|█████████▉| Scoring GeneralizingEstimator : 1211/1225 [00:02<00:00,  497.51it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  497.77it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  495.67it/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 6.848 seconds)

Estimated memory usage: 175 MB

Gallery generated by Sphinx-Gallery