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.18it/s]
 11%|█▏        | Fitting GeneralizingEstimator : 4/35 [00:00<00:00,   59.39it/s]
 17%|█▋        | Fitting GeneralizingEstimator : 6/35 [00:00<00:00,   59.34it/s]
 23%|██▎       | Fitting GeneralizingEstimator : 8/35 [00:00<00:00,   59.29it/s]
 31%|███▏      | Fitting GeneralizingEstimator : 11/35 [00:00<00:00,   65.79it/s]
 40%|████      | Fitting GeneralizingEstimator : 14/35 [00:00<00:00,   70.12it/s]
 49%|████▊     | Fitting GeneralizingEstimator : 17/35 [00:00<00:00,   73.15it/s]
 57%|█████▋    | Fitting GeneralizingEstimator : 20/35 [00:00<00:00,   75.41it/s]
 63%|██████▎   | Fitting GeneralizingEstimator : 22/35 [00:00<00:00,   73.16it/s]
 71%|███████▏  | Fitting GeneralizingEstimator : 25/35 [00:00<00:00,   75.04it/s]
 77%|███████▋  | Fitting GeneralizingEstimator : 27/35 [00:00<00:00,   73.18it/s]
 83%|████████▎ | Fitting GeneralizingEstimator : 29/35 [00:00<00:00,   71.62it/s]
 91%|█████████▏| Fitting GeneralizingEstimator : 32/35 [00:00<00:00,   73.35it/s]
 97%|█████████▋| Fitting GeneralizingEstimator : 34/35 [00:00<00:00,   71.94it/s]
100%|██████████| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   73.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 : 11/1225 [00:00<00:03,  320.54it/s]
  2%|▏         | Scoring GeneralizingEstimator : 26/1225 [00:00<00:03,  383.12it/s]
  3%|▎         | Scoring GeneralizingEstimator : 41/1225 [00:00<00:02,  402.12it/s]
  5%|▍         | Scoring GeneralizingEstimator : 57/1225 [00:00<00:02,  420.95it/s]
  6%|▌         | Scoring GeneralizingEstimator : 72/1225 [00:00<00:02,  425.53it/s]
  7%|▋         | Scoring GeneralizingEstimator : 88/1225 [00:00<00:02,  434.51it/s]
  8%|▊         | Scoring GeneralizingEstimator : 103/1225 [00:00<00:02,  435.21it/s]
 10%|▉         | Scoring GeneralizingEstimator : 119/1225 [00:00<00:02,  440.83it/s]
 11%|█         | Scoring GeneralizingEstimator : 134/1225 [00:00<00:02,  441.28it/s]
 12%|█▏        | Scoring GeneralizingEstimator : 150/1225 [00:00<00:02,  445.21it/s]
 14%|█▎        | Scoring GeneralizingEstimator : 166/1225 [00:00<00:02,  447.91it/s]
 15%|█▍        | Scoring GeneralizingEstimator : 181/1225 [00:00<00:02,  447.26it/s]
 16%|█▌        | Scoring GeneralizingEstimator : 197/1225 [00:00<00:02,  450.03it/s]
 17%|█▋        | Scoring GeneralizingEstimator : 212/1225 [00:00<00:02,  448.85it/s]
 19%|█▊        | Scoring GeneralizingEstimator : 227/1225 [00:00<00:02,  448.45it/s]
 20%|█▉        | Scoring GeneralizingEstimator : 243/1225 [00:00<00:02,  450.22it/s]
 21%|██        | Scoring GeneralizingEstimator : 259/1225 [00:00<00:02,  452.15it/s]
 22%|██▏       | Scoring GeneralizingEstimator : 274/1225 [00:00<00:02,  450.97it/s]
 24%|██▎       | Scoring GeneralizingEstimator : 290/1225 [00:00<00:02,  452.66it/s]
 25%|██▍       | Scoring GeneralizingEstimator : 306/1225 [00:00<00:02,  454.28it/s]
 26%|██▋       | Scoring GeneralizingEstimator : 322/1225 [00:00<00:01,  455.56it/s]
 28%|██▊       | Scoring GeneralizingEstimator : 338/1225 [00:00<00:01,  456.82it/s]
 29%|██▉       | Scoring GeneralizingEstimator : 354/1225 [00:00<00:01,  457.95it/s]
 30%|███       | Scoring GeneralizingEstimator : 370/1225 [00:00<00:01,  458.74it/s]
 32%|███▏      | Scoring GeneralizingEstimator : 386/1225 [00:00<00:01,  459.51it/s]
 33%|███▎      | Scoring GeneralizingEstimator : 401/1225 [00:00<00:01,  458.19it/s]
 34%|███▍      | Scoring GeneralizingEstimator : 417/1225 [00:00<00:01,  458.93it/s]
 35%|███▌      | Scoring GeneralizingEstimator : 433/1225 [00:00<00:01,  459.59it/s]
 37%|███▋      | Scoring GeneralizingEstimator : 448/1225 [00:00<00:01,  458.47it/s]
 38%|███▊      | Scoring GeneralizingEstimator : 464/1225 [00:01<00:01,  459.46it/s]
 39%|███▉      | Scoring GeneralizingEstimator : 480/1225 [00:01<00:01,  459.80it/s]
 40%|████      | Scoring GeneralizingEstimator : 495/1225 [00:01<00:01,  458.79it/s]
 42%|████▏     | Scoring GeneralizingEstimator : 509/1225 [00:01<00:01,  456.05it/s]
 43%|████▎     | Scoring GeneralizingEstimator : 523/1225 [00:01<00:01,  453.47it/s]
 44%|████▍     | Scoring GeneralizingEstimator : 538/1225 [00:01<00:01,  452.61it/s]
 45%|████▍     | Scoring GeneralizingEstimator : 551/1225 [00:01<00:01,  448.49it/s]
 46%|████▌     | Scoring GeneralizingEstimator : 560/1225 [00:01<00:01,  436.71it/s]
 47%|████▋     | Scoring GeneralizingEstimator : 571/1225 [00:01<00:01,  429.84it/s]
 48%|████▊     | Scoring GeneralizingEstimator : 585/1225 [00:01<00:01,  426.00it/s]
 49%|████▉     | Scoring GeneralizingEstimator : 599/1225 [00:01<00:01,  425.01it/s]
 50%|████▉     | Scoring GeneralizingEstimator : 611/1225 [00:01<00:01,  420.87it/s]
 51%|█████     | Scoring GeneralizingEstimator : 624/1225 [00:01<00:01,  418.74it/s]
 52%|█████▏    | Scoring GeneralizingEstimator : 638/1225 [00:01<00:01,  418.46it/s]
 53%|█████▎    | Scoring GeneralizingEstimator : 653/1225 [00:01<00:01,  419.75it/s]
 54%|█████▍    | Scoring GeneralizingEstimator : 667/1225 [00:01<00:01,  419.35it/s]
 56%|█████▌    | Scoring GeneralizingEstimator : 682/1225 [00:01<00:01,  420.71it/s]
 57%|█████▋    | Scoring GeneralizingEstimator : 696/1225 [00:01<00:01,  420.02it/s]
 58%|█████▊    | Scoring GeneralizingEstimator : 707/1225 [00:01<00:01,  414.77it/s]
 59%|█████▊    | Scoring GeneralizingEstimator : 718/1225 [00:01<00:01,  409.75it/s]
 60%|█████▉    | Scoring GeneralizingEstimator : 732/1225 [00:01<00:01,  409.81it/s]
 61%|██████    | Scoring GeneralizingEstimator : 746/1225 [00:01<00:01,  410.07it/s]
 62%|██████▏   | Scoring GeneralizingEstimator : 760/1225 [00:01<00:01,  410.03it/s]
 63%|██████▎   | Scoring GeneralizingEstimator : 773/1225 [00:01<00:01,  408.46it/s]
 64%|██████▍   | Scoring GeneralizingEstimator : 787/1225 [00:01<00:01,  408.80it/s]
 65%|██████▌   | Scoring GeneralizingEstimator : 802/1225 [00:01<00:01,  410.62it/s]
 66%|██████▋   | Scoring GeneralizingEstimator : 814/1225 [00:01<00:01,  407.45it/s]
 68%|██████▊   | Scoring GeneralizingEstimator : 828/1225 [00:01<00:00,  407.81it/s]
 69%|██████▉   | Scoring GeneralizingEstimator : 843/1225 [00:01<00:00,  409.30it/s]
 70%|██████▉   | Scoring GeneralizingEstimator : 855/1225 [00:02<00:00,  406.37it/s]
 71%|███████   | Scoring GeneralizingEstimator : 870/1225 [00:02<00:00,  408.36it/s]
 72%|███████▏  | Scoring GeneralizingEstimator : 884/1225 [00:02<00:00,  408.24it/s]
 73%|███████▎  | Scoring GeneralizingEstimator : 898/1225 [00:02<00:00,  408.53it/s]
 74%|███████▍  | Scoring GeneralizingEstimator : 911/1225 [00:02<00:00,  407.09it/s]
 76%|███████▌  | Scoring GeneralizingEstimator : 926/1225 [00:02<00:00,  408.62it/s]
 77%|███████▋  | Scoring GeneralizingEstimator : 941/1225 [00:02<00:00,  410.45it/s]
 78%|███████▊  | Scoring GeneralizingEstimator : 954/1225 [00:02<00:00,  409.03it/s]
 79%|███████▉  | Scoring GeneralizingEstimator : 970/1225 [00:02<00:00,  412.27it/s]
 80%|████████  | Scoring GeneralizingEstimator : 984/1225 [00:02<00:00,  412.15it/s]
 82%|████████▏ | Scoring GeneralizingEstimator : 999/1225 [00:02<00:00,  413.70it/s]
 83%|████████▎ | Scoring GeneralizingEstimator : 1014/1225 [00:02<00:00,  415.27it/s]
 84%|████████▍ | Scoring GeneralizingEstimator : 1027/1225 [00:02<00:00,  413.44it/s]
 85%|████████▌ | Scoring GeneralizingEstimator : 1043/1225 [00:02<00:00,  416.52it/s]
 86%|████████▋ | Scoring GeneralizingEstimator : 1058/1225 [00:02<00:00,  417.74it/s]
 88%|████████▊ | Scoring GeneralizingEstimator : 1073/1225 [00:02<00:00,  418.96it/s]
 89%|████████▉ | Scoring GeneralizingEstimator : 1088/1225 [00:02<00:00,  420.26it/s]
 90%|█████████ | Scoring GeneralizingEstimator : 1103/1225 [00:02<00:00,  421.49it/s]
 91%|█████████▏| Scoring GeneralizingEstimator : 1118/1225 [00:02<00:00,  422.65it/s]
 92%|█████████▏| Scoring GeneralizingEstimator : 1133/1225 [00:02<00:00,  423.76it/s]
 94%|█████████▎| Scoring GeneralizingEstimator : 1148/1225 [00:02<00:00,  424.80it/s]
 95%|█████████▍| Scoring GeneralizingEstimator : 1163/1225 [00:02<00:00,  425.70it/s]
 96%|█████████▌| Scoring GeneralizingEstimator : 1179/1225 [00:02<00:00,  428.15it/s]
 97%|█████████▋| Scoring GeneralizingEstimator : 1193/1225 [00:02<00:00,  427.10it/s]
 98%|█████████▊| Scoring GeneralizingEstimator : 1205/1225 [00:02<00:00,  423.50it/s]
 99%|█████████▉| Scoring GeneralizingEstimator : 1217/1225 [00:02<00:00,  420.04it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  421.89it/s]
100%|██████████| Scoring GeneralizingEstimator : 1225/1225 [00:02<00:00,  425.93it/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.898 seconds)

Estimated memory usage: 129 MB

Gallery generated by Sphinx-Gallery