Decoding sensor space dataΒΆ

Decoding, a.k.a MVPA or supervised machine learning applied to MEG data in sensor space. Here the classifier is applied to every time point.

# Authors: Alexandre Gramfort <>
#          Jean-Remi King <>
# License: BSD (3-clause)

import matplotlib.pyplot as plt

import mne
from mne import io
from mne.datasets import sample
from mne.decoding import TimeDecoding


data_path = sample.data_path()


Set parameters

raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
tmin, tmax = -0.2, 0.5
event_id = dict(aud_l=1, vis_l=3)

# Setup for reading the raw data
raw = io.Raw(raw_fname, preload=True)
raw.filter(2, None, method='iir')  # replace baselining with high-pass
events = mne.read_events(event_fname)

# Set up pick list: EEG + MEG - bad channels (modify to your needs)['bads'] += ['MEG 2443', 'EEG 053']  # bads + 2 more
picks = mne.pick_types(, meg='grad', eeg=False, stim=True, eog=True,

# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
                    picks=picks, baseline=None, preload=True,
                    reject=dict(grad=4000e-13, eog=150e-6))

epochs_list = [epochs[k] for k in event_id]
data_picks = mne.pick_types(, meg=True, exclude='bads')

Setup decoding: default is linear SVC

td = TimeDecoding(predict_mode='cross-validation', n_jobs=1)
# Fit
# Compute accuracy
# Plot scores across time
td.plot(title='Sensor space decoding')

Script output:

not vectorizing predictions across testing times, using a time window with length > 1

Total running time of the script: (0 minutes 3.985 seconds)

Download Python source code: