Note
Go to the end to download the full example code
Decoding (MVPA)#
Design philosophy#
Decoding (a.k.a. MVPA) in MNE largely follows the machine learning API of the
scikit-learn package.
Each estimator implements fit
, transform
, fit_transform
, and
(optionally) inverse_transform
methods. For more details on this design,
visit scikit-learn. For additional theoretical insights into the decoding
framework in MNE [1].
For ease of comprehension, we will denote instantiations of the class using the same name as the class but in small caps instead of camel cases.
Let’s start by loading data for a simple two-class problem:
sphinx_gallery_thumbnail_number = 6
import numpy as np
import matplotlib.pyplot as plt
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
import mne
from mne.datasets import sample
from mne.decoding import (SlidingEstimator, GeneralizingEstimator, Scaler,
cross_val_multiscore, LinearModel, get_coef,
Vectorizer, CSP)
data_path = sample.data_path()
subjects_dir = data_path / 'subjects'
meg_path = data_path / 'MEG' / 'sample'
raw_fname = meg_path / 'sample_audvis_filt-0-40_raw.fif'
tmin, tmax = -0.200, 0.500
event_id = {'Auditory/Left': 1, 'Visual/Left': 3} # just use two
raw = mne.io.read_raw_fif(raw_fname)
raw.pick_types(meg='grad', stim=True, eog=True, exclude=())
# The subsequent decoding analyses only capture evoked responses, so we can
# low-pass the MEG data. Usually a value more like 40 Hz would be used,
# but here low-pass at 20 so we can more heavily decimate, and allow
# the example to run faster. The 2 Hz high-pass helps improve CSP.
raw.load_data().filter(2, 20)
events = mne.find_events(raw, 'STI 014')
# Set up bad channels (modify to your needs)
raw.info['bads'] += ['MEG 2443'] # bads + 2 more
# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=('grad', 'eog'), baseline=(None, 0.), preload=True,
reject=dict(grad=4000e-13, eog=150e-6), decim=3,
verbose='error')
epochs.pick_types(meg=True, exclude='bads') # remove stim and EOG
del raw
X = epochs.get_data() # MEG signals: n_epochs, n_meg_channels, n_times
y = epochs.events[:, 2] # target: auditory left vs visual left
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.
Removing projector <Projection | PCA-v1, active : False, n_channels : 102>
Removing projector <Projection | PCA-v2, active : False, n_channels : 102>
Removing projector <Projection | PCA-v3, active : False, n_channels : 102>
Removing projector <Projection | Average EEG reference, active : False, n_channels : 60>
Reading 0 ... 41699 = 0.000 ... 277.709 secs...
Filtering raw data in 1 contiguous segment
Setting up band-pass filter from 2 - 20 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: 2.00
- Lower transition bandwidth: 2.00 Hz (-6 dB cutoff frequency: 1.00 Hz)
- Upper passband edge: 20.00 Hz
- Upper transition bandwidth: 5.00 Hz (-6 dB cutoff frequency: 22.50 Hz)
- Filter length: 249 samples (1.658 sec)
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 4 out of 4 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 204 out of 204 | elapsed: 0.4s finished
319 events found
Event IDs: [ 1 2 3 4 5 32]
Transformation classes#
Scaler#
The mne.decoding.Scaler
will standardize the data based on channel
scales. In the simplest modes scalings=None
or scalings=dict(...)
,
each data channel type (e.g., mag, grad, eeg) is treated separately and
scaled by a constant. This is the approach used by e.g.,
mne.compute_covariance()
to standardize channel scales.
If scalings='mean'
or scalings='median'
, each channel is scaled using
empirical measures. Each channel is scaled independently by the mean and
standand deviation, or median and interquartile range, respectively, across
all epochs and time points during fit
(during training). The transform()
method is
called to transform data (training or test set) by scaling all time points
and epochs on a channel-by-channel basis. To perform both the fit
and
transform
operations in a single call, the
fit_transform()
method may be used. To invert the
transform, inverse_transform()
can be used. For
scalings='median'
, scikit-learn version 0.17+ is required.
Note
Using this class is different from directly applying
sklearn.preprocessing.StandardScaler
or
sklearn.preprocessing.RobustScaler
offered by
scikit-learn. These scale each classification feature, e.g.
each time point for each channel, with mean and standard
deviation computed across epochs, whereas
mne.decoding.Scaler
scales each channel using mean and
standard deviation computed across all of its time points
and epochs.
Vectorizer#
Scikit-learn API provides functionality to chain transformers and estimators
by using sklearn.pipeline.Pipeline
. We can construct decoding
pipelines and perform cross-validation and grid-search. However scikit-learn
transformers and estimators generally expect 2D data
(n_samples * n_features), whereas MNE transformers typically output data
with a higher dimensionality
(e.g. n_samples * n_channels * n_frequencies * n_times). A Vectorizer
therefore needs to be applied between the MNE and the scikit-learn steps
like:
# Uses all MEG sensors and time points as separate classification
# features, so the resulting filters used are spatio-temporal
clf = make_pipeline(
Scaler(epochs.info),
Vectorizer(),
LogisticRegression(solver='liblinear') # liblinear is faster than lbfgs
)
scores = cross_val_multiscore(clf, X, y, cv=5, n_jobs=None)
# Mean scores across cross-validation splits
score = np.mean(scores, axis=0)
print('Spatio-temporal: %0.1f%%' % (100 * score,))
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.2s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 0.3s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 0.4s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 4 out of 4 | elapsed: 0.6s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 5 out of 5 | elapsed: 0.7s finished
Spatio-temporal: 99.2%
PSDEstimator#
The mne.decoding.PSDEstimator
computes the power spectral density (PSD) using the multitaper
method. It takes a 3D array as input, converts it into 2D and computes the
PSD.
FilterEstimator#
The mne.decoding.FilterEstimator
filters the 3D epochs data.
Spatial filters#
Just like temporal filters, spatial filters provide weights to modify the data along the sensor dimension. They are popular in the BCI community because of their simplicity and ability to distinguish spatially-separated neural activity.
Common spatial pattern#
mne.decoding.CSP
is a technique to analyze multichannel data based
on recordings from two classes [2] (see also
https://en.wikipedia.org/wiki/Common_spatial_pattern).
Let \(X \in R^{C\times T}\) be a segment of data with \(C\) channels and \(T\) time points. The data at a single time point is denoted by \(x(t)\) such that \(X=[x(t), x(t+1), ..., x(t+T-1)]\). Common spatial pattern (CSP) finds a decomposition that projects the signal in the original sensor space to CSP space using the following transformation:
where each column of \(W \in R^{C\times C}\) is a spatial filter and each row of \(x_{CSP}\) is a CSP component. The matrix \(W\) is also called the de-mixing matrix in other contexts. Let \(\Sigma^{+} \in R^{C\times C}\) and \(\Sigma^{-} \in R^{C\times C}\) be the estimates of the covariance matrices of the two conditions. CSP analysis is given by the simultaneous diagonalization of the two covariance matrices
where \(\lambda^{C}\) is a diagonal matrix whose entries are the eigenvalues of the following generalized eigenvalue problem
Large entries in the diagonal matrix corresponds to a spatial filter which gives high variance in one class but low variance in the other. Thus, the filter facilitates discrimination between the two classes.
Note
The winning entry of the Grasp-and-lift EEG competition in Kaggle used
the CSP
implementation in MNE and was featured as
a script of the week.
We can use CSP with these data with:
csp = CSP(n_components=3, norm_trace=False)
clf_csp = make_pipeline(
csp,
LinearModel(LogisticRegression(solver='liblinear'))
)
scores = cross_val_multiscore(clf_csp, X, y, cv=5, n_jobs=None)
print('CSP: %0.1f%%' % (100 * scores.mean(),))
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
Computing rank from data with rank=None
Using tolerance 4e-11 (2.2e-16 eps * 203 dim * 8.9e+02 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
Computing rank from data with rank=None
Using tolerance 4.7e-11 (2.2e-16 eps * 203 dim * 1e+03 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.2s remaining: 0.0s
Computing rank from data with rank=None
Using tolerance 3.8e-11 (2.2e-16 eps * 203 dim * 8.5e+02 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
Computing rank from data with rank=None
Using tolerance 4.8e-11 (2.2e-16 eps * 203 dim * 1.1e+03 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 0.5s remaining: 0.0s
Computing rank from data with rank=None
Using tolerance 3.9e-11 (2.2e-16 eps * 203 dim * 8.6e+02 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
Computing rank from data with rank=None
Using tolerance 4.7e-11 (2.2e-16 eps * 203 dim * 1e+03 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
[Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 0.7s remaining: 0.0s
Computing rank from data with rank=None
Using tolerance 3.9e-11 (2.2e-16 eps * 203 dim * 8.6e+02 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
Computing rank from data with rank=None
Using tolerance 4.5e-11 (2.2e-16 eps * 203 dim * 1e+03 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
[Parallel(n_jobs=1)]: Done 4 out of 4 | elapsed: 0.9s remaining: 0.0s
Computing rank from data with rank=None
Using tolerance 3.8e-11 (2.2e-16 eps * 203 dim * 8.5e+02 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
Computing rank from data with rank=None
Using tolerance 4.7e-11 (2.2e-16 eps * 203 dim * 1e+03 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
[Parallel(n_jobs=1)]: Done 5 out of 5 | elapsed: 1.1s finished
CSP: 89.4%
Source power comodulation (SPoC)#
Source Power Comodulation (mne.decoding.SPoC
)
[3] identifies the composition of
orthogonal spatial filters that maximally correlate with a continuous target.
SPoC can be seen as an extension of the CSP where the target is driven by a continuous variable rather than a discrete variable. Typical applications include extraction of motor patterns using EMG power or audio patterns using sound envelope.
xDAWN#
mne.preprocessing.Xdawn
is a spatial filtering method designed to
improve the signal to signal + noise ratio (SSNR) of the ERP responses
[4]. Xdawn was originally
designed for P300 evoked potential by enhancing the target response with
respect to the non-target response. The implementation in MNE-Python is a
generalization to any type of ERP.
Effect-matched spatial filtering#
The result of mne.decoding.EMS
is a spatial filter at each time
point and a corresponding time course [5].
Intuitively, the result gives the similarity between the filter at
each time point and the data vector (sensors) at that time point.
Patterns vs. filters#
When interpreting the components of the CSP (or spatial filters in general), it is often more intuitive to think about how \(x(t)\) is composed of the different CSP components \(x_{CSP}(t)\). In other words, we can rewrite Equation (1) as follows:
The columns of the matrix \((W^{-1})^T\) are called spatial patterns. This is also called the mixing matrix. The example Linear classifier on sensor data with plot patterns and filters discusses the difference between patterns and filters.
These can be plotted with:
# Fit CSP on full data and plot
csp.fit(X, y)
csp.plot_patterns(epochs.info)
csp.plot_filters(epochs.info, scalings=1e-9)
Computing rank from data with rank=None
Using tolerance 4.3e-11 (2.2e-16 eps * 203 dim * 9.6e+02 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
Computing rank from data with rank=None
Using tolerance 5.2e-11 (2.2e-16 eps * 203 dim * 1.2e+03 max singular value)
Estimated rank (mag): 203
MAG: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
Decoding over time#
This strategy consists in fitting a multivariate predictive model on each
time instant and evaluating its performance at the same instant on new
epochs. The mne.decoding.SlidingEstimator
will take as input a
pair of features \(X\) and targets \(y\), where \(X\) has
more than 2 dimensions. For decoding over time the data \(X\)
is the epochs data of shape n_epochs × n_channels × n_times. As the
last dimension of \(X\) is the time, an estimator will be fit
on every time instant.
This approach is analogous to SlidingEstimator-based approaches in fMRI, where here we are interested in when one can discriminate experimental conditions and therefore figure out when the effect of interest happens.
When working with linear models as estimators, this approach boils down to estimating a discriminative spatial filter for each time instant.
Temporal decoding#
We’ll use a Logistic Regression for a binary classification as machine learning model.
# We will train the classifier on all left visual vs auditory trials on MEG
clf = make_pipeline(
StandardScaler(),
LogisticRegression(solver='liblinear')
)
time_decod = SlidingEstimator(
clf, n_jobs=None, scoring='roc_auc', verbose=True)
# here we use cv=3 just for speed
scores = cross_val_multiscore(time_decod, X, y, cv=3, n_jobs=None)
# Mean scores across cross-validation splits
scores = np.mean(scores, axis=0)
# Plot
fig, ax = plt.subplots()
ax.plot(epochs.times, scores, label='score')
ax.axhline(.5, color='k', linestyle='--', label='chance')
ax.set_xlabel('Times')
ax.set_ylabel('AUC') # Area Under the Curve
ax.legend()
ax.axvline(.0, color='k', linestyle='-')
ax.set_title('Sensor space decoding')
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
0%| | Fitting SlidingEstimator : 0/36 [00:00<?, ?it/s]
11%|#1 | Fitting SlidingEstimator : 4/36 [00:00<00:00, 115.58it/s]
22%|##2 | Fitting SlidingEstimator : 8/36 [00:00<00:00, 116.89it/s]
36%|###6 | Fitting SlidingEstimator : 13/36 [00:00<00:00, 127.04it/s]
53%|#####2 | Fitting SlidingEstimator : 19/36 [00:00<00:00, 140.20it/s]
67%|######6 | Fitting SlidingEstimator : 24/36 [00:00<00:00, 141.69it/s]
81%|######## | Fitting SlidingEstimator : 29/36 [00:00<00:00, 142.68it/s]
92%|#########1| Fitting SlidingEstimator : 33/36 [00:00<00:00, 138.63it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 136.71it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 136.36it/s]
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.1s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.1s finished
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.4s remaining: 0.0s
0%| | Fitting SlidingEstimator : 0/36 [00:00<?, ?it/s]
11%|#1 | Fitting SlidingEstimator : 4/36 [00:00<00:00, 117.75it/s]
25%|##5 | Fitting SlidingEstimator : 9/36 [00:00<00:00, 133.06it/s]
36%|###6 | Fitting SlidingEstimator : 13/36 [00:00<00:00, 127.91it/s]
53%|#####2 | Fitting SlidingEstimator : 19/36 [00:00<00:00, 141.30it/s]
69%|######9 | Fitting SlidingEstimator : 25/36 [00:00<00:00, 149.21it/s]
83%|########3 | Fitting SlidingEstimator : 30/36 [00:00<00:00, 148.90it/s]
97%|#########7| Fitting SlidingEstimator : 35/36 [00:00<00:00, 148.69it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 150.27it/s]
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.1s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.1s finished
[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 0.7s remaining: 0.0s
0%| | Fitting SlidingEstimator : 0/36 [00:00<?, ?it/s]
11%|#1 | Fitting SlidingEstimator : 4/36 [00:00<00:00, 118.00it/s]
22%|##2 | Fitting SlidingEstimator : 8/36 [00:00<00:00, 118.14it/s]
39%|###8 | Fitting SlidingEstimator : 14/36 [00:00<00:00, 138.44it/s]
56%|#####5 | Fitting SlidingEstimator : 20/36 [00:00<00:00, 148.55it/s]
69%|######9 | Fitting SlidingEstimator : 25/36 [00:00<00:00, 148.46it/s]
86%|########6 | Fitting SlidingEstimator : 31/36 [00:00<00:00, 153.89it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 157.82it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 155.53it/s]
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.1s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.1s finished
[Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 1.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 1.0s finished
You can retrieve the spatial filters and spatial patterns if you explicitly use a LinearModel
clf = make_pipeline(
StandardScaler(),
LinearModel(LogisticRegression(solver='liblinear'))
)
time_decod = SlidingEstimator(
clf, n_jobs=None, scoring='roc_auc', verbose=True)
time_decod.fit(X, y)
coef = get_coef(time_decod, 'patterns_', inverse_transform=True)
evoked_time_gen = mne.EvokedArray(coef, epochs.info, tmin=epochs.times[0])
joint_kwargs = dict(ts_args=dict(time_unit='s'),
topomap_args=dict(time_unit='s'))
evoked_time_gen.plot_joint(times=np.arange(0., .500, .100), title='patterns',
**joint_kwargs)
0%| | Fitting SlidingEstimator : 0/36 [00:00<?, ?it/s]
6%|5 | Fitting SlidingEstimator : 2/36 [00:00<00:00, 58.02it/s]
14%|#3 | Fitting SlidingEstimator : 5/36 [00:00<00:00, 73.53it/s]
22%|##2 | Fitting SlidingEstimator : 8/36 [00:00<00:00, 78.82it/s]
31%|### | Fitting SlidingEstimator : 11/36 [00:00<00:00, 81.47it/s]
42%|####1 | Fitting SlidingEstimator : 15/36 [00:00<00:00, 89.11it/s]
53%|#####2 | Fitting SlidingEstimator : 19/36 [00:00<00:00, 94.10it/s]
61%|######1 | Fitting SlidingEstimator : 22/36 [00:00<00:00, 93.21it/s]
69%|######9 | Fitting SlidingEstimator : 25/36 [00:00<00:00, 92.46it/s]
78%|#######7 | Fitting SlidingEstimator : 28/36 [00:00<00:00, 91.94it/s]
83%|########3 | Fitting SlidingEstimator : 30/36 [00:00<00:00, 87.85it/s]
92%|#########1| Fitting SlidingEstimator : 33/36 [00:00<00:00, 87.92it/s]
97%|#########7| Fitting SlidingEstimator : 35/36 [00:00<00:00, 84.77it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 87.61it/s]
No projector specified for this dataset. Please consider the method self.add_proj.
Temporal generalization#
Temporal generalization is an extension of the decoding over time approach. It consists in evaluating whether the model estimated at a particular time instant accurately predicts any other time instant. It is analogous to transferring a trained model to a distinct learning problem, where the problems correspond to decoding the patterns of brain activity recorded at distinct time instants.
The object to for Temporal generalization is
mne.decoding.GeneralizingEstimator
. It expects as input \(X\)
and \(y\) (similarly to SlidingEstimator
) but
generates predictions from each model for all time instants. The class
GeneralizingEstimator
is generic and will treat the
last dimension as the one to be used for generalization testing. For
convenience, here, we refer to it as different tasks. If \(X\)
corresponds to epochs data then the last dimension is time.
This runs the analysis used in [6] and further detailed in [7]:
# define the Temporal generalization object
time_gen = GeneralizingEstimator(clf, n_jobs=None, scoring='roc_auc',
verbose=True)
# again, cv=3 just for speed
scores = cross_val_multiscore(time_gen, X, y, cv=3, n_jobs=None)
# Mean scores across cross-validation splits
scores = np.mean(scores, axis=0)
# Plot the diagonal (it's exactly the same as the time-by-time decoding above)
fig, ax = plt.subplots()
ax.plot(epochs.times, np.diag(scores), label='score')
ax.axhline(.5, color='k', linestyle='--', label='chance')
ax.set_xlabel('Times')
ax.set_ylabel('AUC')
ax.legend()
ax.axvline(.0, color='k', linestyle='-')
ax.set_title('Decoding MEG sensors over time')
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
0%| | Fitting GeneralizingEstimator : 0/36 [00:00<?, ?it/s]
8%|8 | Fitting GeneralizingEstimator : 3/36 [00:00<00:00, 86.89it/s]
22%|##2 | Fitting GeneralizingEstimator : 8/36 [00:00<00:00, 117.20it/s]
33%|###3 | Fitting GeneralizingEstimator : 12/36 [00:00<00:00, 117.49it/s]
50%|##### | Fitting GeneralizingEstimator : 18/36 [00:00<00:00, 133.39it/s]
61%|######1 | Fitting GeneralizingEstimator : 22/36 [00:00<00:00, 130.05it/s]
72%|#######2 | Fitting GeneralizingEstimator : 26/36 [00:00<00:00, 127.79it/s]
83%|########3 | Fitting GeneralizingEstimator : 30/36 [00:00<00:00, 126.13it/s]
89%|########8 | Fitting GeneralizingEstimator : 32/36 [00:00<00:00, 110.36it/s]
100%|##########| Fitting GeneralizingEstimator : 36/36 [00:00<00:00, 112.43it/s]
100%|##########| Fitting GeneralizingEstimator : 36/36 [00:00<00:00, 113.90it/s]
0%| | Scoring GeneralizingEstimator : 0/1296 [00:00<?, ?it/s]
1%|1 | Scoring GeneralizingEstimator : 14/1296 [00:00<00:03, 410.74it/s]
2%|2 | Scoring GeneralizingEstimator : 30/1296 [00:00<00:02, 442.52it/s]
4%|3 | Scoring GeneralizingEstimator : 47/1296 [00:00<00:02, 463.31it/s]
5%|4 | Scoring GeneralizingEstimator : 63/1296 [00:00<00:02, 466.02it/s]
6%|6 | Scoring GeneralizingEstimator : 80/1296 [00:00<00:02, 472.98it/s]
7%|7 | Scoring GeneralizingEstimator : 97/1296 [00:00<00:02, 478.61it/s]
9%|8 | Scoring GeneralizingEstimator : 113/1296 [00:00<00:02, 477.35it/s]
10%|# | Scoring GeneralizingEstimator : 130/1296 [00:00<00:02, 479.34it/s]
11%|#1 | Scoring GeneralizingEstimator : 147/1296 [00:00<00:02, 482.34it/s]
13%|#2 | Scoring GeneralizingEstimator : 163/1296 [00:00<00:02, 480.51it/s]
14%|#3 | Scoring GeneralizingEstimator : 180/1296 [00:00<00:02, 482.75it/s]
15%|#5 | Scoring GeneralizingEstimator : 195/1296 [00:00<00:02, 477.19it/s]
17%|#6 | Scoring GeneralizingEstimator : 217/1296 [00:00<00:02, 494.66it/s]
19%|#8 | Scoring GeneralizingEstimator : 242/1296 [00:00<00:02, 516.76it/s]
20%|## | Scoring GeneralizingEstimator : 261/1296 [00:00<00:01, 520.80it/s]
22%|##1 | Scoring GeneralizingEstimator : 283/1296 [00:00<00:01, 530.86it/s]
23%|##3 | Scoring GeneralizingEstimator : 302/1296 [00:00<00:01, 533.29it/s]
24%|##4 | Scoring GeneralizingEstimator : 317/1296 [00:00<00:01, 525.41it/s]
26%|##5 | Scoring GeneralizingEstimator : 333/1296 [00:00<00:01, 521.21it/s]
27%|##7 | Scoring GeneralizingEstimator : 353/1296 [00:00<00:01, 526.69it/s]
29%|##8 | Scoring GeneralizingEstimator : 372/1296 [00:00<00:01, 529.10it/s]
30%|### | Scoring GeneralizingEstimator : 391/1296 [00:00<00:01, 531.36it/s]
31%|###1 | Scoring GeneralizingEstimator : 406/1296 [00:00<00:01, 524.91it/s]
32%|###2 | Scoring GeneralizingEstimator : 420/1296 [00:00<00:01, 516.69it/s]
33%|###3 | Scoring GeneralizingEstimator : 434/1296 [00:00<00:01, 509.42it/s]
35%|###4 | Scoring GeneralizingEstimator : 448/1296 [00:00<00:01, 502.95it/s]
36%|###5 | Scoring GeneralizingEstimator : 462/1296 [00:00<00:01, 496.56it/s]
37%|###6 | Scoring GeneralizingEstimator : 477/1296 [00:00<00:01, 492.81it/s]
38%|###7 | Scoring GeneralizingEstimator : 491/1296 [00:00<00:01, 487.60it/s]
39%|###8 | Scoring GeneralizingEstimator : 505/1296 [00:01<00:01, 482.64it/s]
40%|#### | Scoring GeneralizingEstimator : 520/1296 [00:01<00:01, 480.22it/s]
42%|####1 | Scoring GeneralizingEstimator : 544/1296 [00:01<00:01, 493.87it/s]
44%|####3 | Scoring GeneralizingEstimator : 566/1296 [00:01<00:01, 503.41it/s]
46%|####5 | Scoring GeneralizingEstimator : 590/1296 [00:01<00:01, 515.76it/s]
47%|####7 | Scoring GeneralizingEstimator : 613/1296 [00:01<00:01, 525.36it/s]
49%|####8 | Scoring GeneralizingEstimator : 633/1296 [00:01<00:01, 529.26it/s]
51%|##### | Scoring GeneralizingEstimator : 656/1296 [00:01<00:01, 537.55it/s]
52%|#####2 | Scoring GeneralizingEstimator : 678/1296 [00:01<00:01, 544.01it/s]
54%|#####3 | Scoring GeneralizingEstimator : 696/1296 [00:01<00:01, 543.14it/s]
55%|#####4 | Scoring GeneralizingEstimator : 712/1296 [00:01<00:01, 539.06it/s]
57%|#####6 | Scoring GeneralizingEstimator : 735/1296 [00:01<00:01, 546.63it/s]
58%|#####8 | Scoring GeneralizingEstimator : 752/1296 [00:01<00:01, 543.38it/s]
60%|#####9 | Scoring GeneralizingEstimator : 774/1296 [00:01<00:00, 549.19it/s]
61%|######1 | Scoring GeneralizingEstimator : 791/1296 [00:01<00:00, 545.99it/s]
62%|######2 | Scoring GeneralizingEstimator : 808/1296 [00:01<00:00, 543.65it/s]
64%|######3 | Scoring GeneralizingEstimator : 825/1296 [00:01<00:00, 540.77it/s]
65%|######4 | Scoring GeneralizingEstimator : 842/1296 [00:01<00:00, 538.43it/s]
67%|######6 | Scoring GeneralizingEstimator : 863/1296 [00:01<00:00, 542.85it/s]
68%|######8 | Scoring GeneralizingEstimator : 885/1296 [00:01<00:00, 548.13it/s]
70%|######9 | Scoring GeneralizingEstimator : 902/1296 [00:01<00:00, 545.66it/s]
71%|####### | Scoring GeneralizingEstimator : 918/1296 [00:01<00:00, 541.39it/s]
72%|#######2 | Scoring GeneralizingEstimator : 937/1296 [00:01<00:00, 541.95it/s]
74%|#######4 | Scoring GeneralizingEstimator : 963/1296 [00:01<00:00, 553.12it/s]
76%|#######6 | Scoring GeneralizingEstimator : 985/1296 [00:01<00:00, 558.06it/s]
78%|#######7 | Scoring GeneralizingEstimator : 1008/1296 [00:01<00:00, 564.42it/s]
79%|#######9 | Scoring GeneralizingEstimator : 1027/1296 [00:01<00:00, 563.97it/s]
81%|######## | Scoring GeneralizingEstimator : 1047/1296 [00:01<00:00, 565.40it/s]
82%|########2 | Scoring GeneralizingEstimator : 1066/1296 [00:01<00:00, 564.55it/s]
84%|########3 | Scoring GeneralizingEstimator : 1088/1296 [00:02<00:00, 568.87it/s]
86%|########5 | Scoring GeneralizingEstimator : 1109/1296 [00:02<00:00, 570.70it/s]
87%|########7 | Scoring GeneralizingEstimator : 1133/1296 [00:02<00:00, 577.28it/s]
89%|########9 | Scoring GeneralizingEstimator : 1154/1296 [00:02<00:00, 579.28it/s]
90%|######### | Scoring GeneralizingEstimator : 1172/1296 [00:02<00:00, 576.63it/s]
92%|#########1| Scoring GeneralizingEstimator : 1187/1296 [00:02<00:00, 569.11it/s]
93%|#########2| Scoring GeneralizingEstimator : 1203/1296 [00:02<00:00, 563.64it/s]
94%|#########4| Scoring GeneralizingEstimator : 1220/1296 [00:02<00:00, 560.43it/s]
95%|#########5| Scoring GeneralizingEstimator : 1237/1296 [00:02<00:00, 557.05it/s]
97%|#########6| Scoring GeneralizingEstimator : 1253/1296 [00:02<00:00, 552.68it/s]
98%|#########7| Scoring GeneralizingEstimator : 1270/1296 [00:02<00:00, 549.41it/s]
99%|#########9| Scoring GeneralizingEstimator : 1287/1296 [00:02<00:00, 546.95it/s]
100%|##########| Scoring GeneralizingEstimator : 1296/1296 [00:02<00:00, 546.87it/s]
100%|##########| Scoring GeneralizingEstimator : 1296/1296 [00:02<00:00, 538.31it/s]
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 2.8s remaining: 0.0s
0%| | Fitting GeneralizingEstimator : 0/36 [00:00<?, ?it/s]
8%|8 | Fitting GeneralizingEstimator : 3/36 [00:00<00:00, 88.11it/s]
19%|#9 | Fitting GeneralizingEstimator : 7/36 [00:00<00:00, 103.38it/s]
31%|### | Fitting GeneralizingEstimator : 11/36 [00:00<00:00, 108.59it/s]
42%|####1 | Fitting GeneralizingEstimator : 15/36 [00:00<00:00, 111.24it/s]
56%|#####5 | Fitting GeneralizingEstimator : 20/36 [00:00<00:00, 119.14it/s]
67%|######6 | Fitting GeneralizingEstimator : 24/36 [00:00<00:00, 118.94it/s]
78%|#######7 | Fitting GeneralizingEstimator : 28/36 [00:00<00:00, 118.81it/s]
89%|########8 | Fitting GeneralizingEstimator : 32/36 [00:00<00:00, 118.72it/s]
100%|##########| Fitting GeneralizingEstimator : 36/36 [00:00<00:00, 120.48it/s]
100%|##########| Fitting GeneralizingEstimator : 36/36 [00:00<00:00, 119.31it/s]
0%| | Scoring GeneralizingEstimator : 0/1296 [00:00<?, ?it/s]
1%|1 | Scoring GeneralizingEstimator : 14/1296 [00:00<00:03, 410.88it/s]
2%|2 | Scoring GeneralizingEstimator : 30/1296 [00:00<00:02, 442.32it/s]
3%|3 | Scoring GeneralizingEstimator : 45/1296 [00:00<00:02, 441.96it/s]
5%|4 | Scoring GeneralizingEstimator : 61/1296 [00:00<00:02, 450.15it/s]
6%|5 | Scoring GeneralizingEstimator : 76/1296 [00:00<00:02, 448.74it/s]
7%|6 | Scoring GeneralizingEstimator : 90/1296 [00:00<00:02, 441.12it/s]
8%|8 | Scoring GeneralizingEstimator : 106/1296 [00:00<00:02, 445.93it/s]
9%|9 | Scoring GeneralizingEstimator : 120/1296 [00:00<00:02, 440.96it/s]
10%|# | Scoring GeneralizingEstimator : 135/1296 [00:00<00:02, 440.86it/s]
11%|#1 | Scoring GeneralizingEstimator : 149/1296 [00:00<00:02, 436.65it/s]
13%|#2 | Scoring GeneralizingEstimator : 165/1296 [00:00<00:02, 440.24it/s]
14%|#3 | Scoring GeneralizingEstimator : 181/1296 [00:00<00:02, 443.66it/s]
15%|#5 | Scoring GeneralizingEstimator : 197/1296 [00:00<00:02, 446.00it/s]
17%|#6 | Scoring GeneralizingEstimator : 216/1296 [00:00<00:02, 455.34it/s]
18%|#8 | Scoring GeneralizingEstimator : 234/1296 [00:00<00:02, 462.01it/s]
20%|#9 | Scoring GeneralizingEstimator : 253/1296 [00:00<00:02, 470.64it/s]
21%|## | Scoring GeneralizingEstimator : 271/1296 [00:00<00:02, 475.82it/s]
22%|##2 | Scoring GeneralizingEstimator : 288/1296 [00:00<00:02, 476.75it/s]
24%|##3 | Scoring GeneralizingEstimator : 305/1296 [00:00<00:02, 478.66it/s]
25%|##5 | Scoring GeneralizingEstimator : 324/1296 [00:00<00:02, 484.99it/s]
26%|##6 | Scoring GeneralizingEstimator : 341/1296 [00:00<00:01, 486.21it/s]
28%|##7 | Scoring GeneralizingEstimator : 357/1296 [00:00<00:01, 485.22it/s]
29%|##8 | Scoring GeneralizingEstimator : 373/1296 [00:00<00:01, 483.43it/s]
30%|### | Scoring GeneralizingEstimator : 389/1296 [00:00<00:01, 482.49it/s]
31%|###1 | Scoring GeneralizingEstimator : 405/1296 [00:00<00:01, 481.60it/s]
32%|###2 | Scoring GeneralizingEstimator : 421/1296 [00:00<00:01, 480.93it/s]
34%|###3 | Scoring GeneralizingEstimator : 436/1296 [00:00<00:01, 478.29it/s]
35%|###4 | Scoring GeneralizingEstimator : 452/1296 [00:00<00:01, 477.49it/s]
36%|###6 | Scoring GeneralizingEstimator : 467/1296 [00:00<00:01, 475.26it/s]
37%|###7 | Scoring GeneralizingEstimator : 485/1296 [00:01<00:01, 478.84it/s]
39%|###8 | Scoring GeneralizingEstimator : 501/1296 [00:01<00:01, 478.13it/s]
40%|#### | Scoring GeneralizingEstimator : 523/1296 [00:01<00:01, 488.64it/s]
42%|####2 | Scoring GeneralizingEstimator : 545/1296 [00:01<00:01, 498.41it/s]
44%|####3 | Scoring GeneralizingEstimator : 568/1296 [00:01<00:01, 509.29it/s]
45%|####5 | Scoring GeneralizingEstimator : 588/1296 [00:01<00:01, 514.04it/s]
47%|####6 | Scoring GeneralizingEstimator : 607/1296 [00:01<00:01, 515.92it/s]
49%|####8 | Scoring GeneralizingEstimator : 631/1296 [00:01<00:01, 527.01it/s]
51%|##### | Scoring GeneralizingEstimator : 655/1296 [00:01<00:01, 536.94it/s]
52%|#####2 | Scoring GeneralizingEstimator : 679/1296 [00:01<00:01, 546.06it/s]
54%|#####4 | Scoring GeneralizingEstimator : 701/1296 [00:01<00:01, 550.97it/s]
56%|#####5 | Scoring GeneralizingEstimator : 720/1296 [00:01<00:01, 550.55it/s]
57%|#####6 | Scoring GeneralizingEstimator : 738/1296 [00:01<00:01, 549.37it/s]
58%|#####8 | Scoring GeneralizingEstimator : 756/1296 [00:01<00:00, 548.43it/s]
60%|#####9 | Scoring GeneralizingEstimator : 773/1296 [00:01<00:00, 545.70it/s]
61%|######1 | Scoring GeneralizingEstimator : 792/1296 [00:01<00:00, 545.95it/s]
63%|######2 | Scoring GeneralizingEstimator : 815/1296 [00:01<00:00, 553.34it/s]
64%|######4 | Scoring GeneralizingEstimator : 834/1296 [00:01<00:00, 553.71it/s]
66%|######5 | Scoring GeneralizingEstimator : 855/1296 [00:01<00:00, 557.18it/s]
68%|######7 | Scoring GeneralizingEstimator : 876/1296 [00:01<00:00, 560.38it/s]
69%|######8 | Scoring GeneralizingEstimator : 893/1296 [00:01<00:00, 556.68it/s]
70%|####### | Scoring GeneralizingEstimator : 913/1296 [00:01<00:00, 558.27it/s]
72%|#######1 | Scoring GeneralizingEstimator : 933/1296 [00:01<00:00, 559.29it/s]
73%|#######3 | Scoring GeneralizingEstimator : 952/1296 [00:01<00:00, 558.64it/s]
75%|#######5 | Scoring GeneralizingEstimator : 972/1296 [00:01<00:00, 560.25it/s]
77%|#######6 | Scoring GeneralizingEstimator : 992/1296 [00:01<00:00, 561.60it/s]
78%|#######8 | Scoring GeneralizingEstimator : 1014/1296 [00:01<00:00, 565.76it/s]
80%|#######9 | Scoring GeneralizingEstimator : 1034/1296 [00:01<00:00, 565.91it/s]
81%|########1 | Scoring GeneralizingEstimator : 1051/1296 [00:01<00:00, 562.25it/s]
82%|########2 | Scoring GeneralizingEstimator : 1067/1296 [00:02<00:00, 556.93it/s]
84%|########3 | Scoring GeneralizingEstimator : 1083/1296 [00:02<00:00, 552.37it/s]
85%|########4 | Scoring GeneralizingEstimator : 1099/1296 [00:02<00:00, 547.05it/s]
86%|########6 | Scoring GeneralizingEstimator : 1115/1296 [00:02<00:00, 543.04it/s]
87%|########7 | Scoring GeneralizingEstimator : 1131/1296 [00:02<00:00, 539.08it/s]
89%|########8 | Scoring GeneralizingEstimator : 1147/1296 [00:02<00:00, 535.58it/s]
90%|######### | Scoring GeneralizingEstimator : 1167/1296 [00:02<00:00, 535.24it/s]
92%|#########1| Scoring GeneralizingEstimator : 1186/1296 [00:02<00:00, 535.49it/s]
93%|#########3| Scoring GeneralizingEstimator : 1206/1296 [00:02<00:00, 537.92it/s]
94%|#########4| Scoring GeneralizingEstimator : 1223/1296 [00:02<00:00, 535.75it/s]
96%|#########5| Scoring GeneralizingEstimator : 1238/1296 [00:02<00:00, 531.05it/s]
97%|#########7| Scoring GeneralizingEstimator : 1259/1296 [00:02<00:00, 535.22it/s]
98%|#########8| Scoring GeneralizingEstimator : 1276/1296 [00:02<00:00, 533.59it/s]
100%|#########9| Scoring GeneralizingEstimator : 1294/1296 [00:02<00:00, 533.34it/s]
100%|##########| Scoring GeneralizingEstimator : 1296/1296 [00:02<00:00, 524.65it/s]
[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 5.6s remaining: 0.0s
0%| | Fitting GeneralizingEstimator : 0/36 [00:00<?, ?it/s]
11%|#1 | Fitting GeneralizingEstimator : 4/36 [00:00<00:00, 117.21it/s]
25%|##5 | Fitting GeneralizingEstimator : 9/36 [00:00<00:00, 132.91it/s]
36%|###6 | Fitting GeneralizingEstimator : 13/36 [00:00<00:00, 127.74it/s]
53%|#####2 | Fitting GeneralizingEstimator : 19/36 [00:00<00:00, 141.12it/s]
67%|######6 | Fitting GeneralizingEstimator : 24/36 [00:00<00:00, 142.58it/s]
81%|######## | Fitting GeneralizingEstimator : 29/36 [00:00<00:00, 143.60it/s]
94%|#########4| Fitting GeneralizingEstimator : 34/36 [00:00<00:00, 143.98it/s]
100%|##########| Fitting GeneralizingEstimator : 36/36 [00:00<00:00, 143.02it/s]
0%| | Scoring GeneralizingEstimator : 0/1296 [00:00<?, ?it/s]
1%|1 | Scoring GeneralizingEstimator : 15/1296 [00:00<00:02, 441.37it/s]
2%|2 | Scoring GeneralizingEstimator : 32/1296 [00:00<00:02, 470.98it/s]
4%|3 | Scoring GeneralizingEstimator : 48/1296 [00:00<00:02, 469.72it/s]
5%|4 | Scoring GeneralizingEstimator : 64/1296 [00:00<00:02, 470.60it/s]
6%|6 | Scoring GeneralizingEstimator : 80/1296 [00:00<00:02, 470.24it/s]
7%|7 | Scoring GeneralizingEstimator : 96/1296 [00:00<00:02, 470.69it/s]
9%|8 | Scoring GeneralizingEstimator : 112/1296 [00:00<00:02, 468.89it/s]
10%|9 | Scoring GeneralizingEstimator : 128/1296 [00:00<00:02, 469.34it/s]
11%|#1 | Scoring GeneralizingEstimator : 144/1296 [00:00<00:02, 469.48it/s]
12%|#2 | Scoring GeneralizingEstimator : 160/1296 [00:00<00:02, 468.81it/s]
14%|#3 | Scoring GeneralizingEstimator : 176/1296 [00:00<00:02, 469.22it/s]
15%|#4 | Scoring GeneralizingEstimator : 193/1296 [00:00<00:02, 472.90it/s]
16%|#6 | Scoring GeneralizingEstimator : 210/1296 [00:00<00:02, 475.61it/s]
18%|#7 | Scoring GeneralizingEstimator : 229/1296 [00:00<00:02, 482.21it/s]
19%|#9 | Scoring GeneralizingEstimator : 248/1296 [00:00<00:02, 488.22it/s]
21%|## | Scoring GeneralizingEstimator : 271/1296 [00:00<00:02, 505.10it/s]
22%|##2 | Scoring GeneralizingEstimator : 290/1296 [00:00<00:01, 509.72it/s]
24%|##3 | Scoring GeneralizingEstimator : 307/1296 [00:00<00:01, 509.13it/s]
25%|##5 | Scoring GeneralizingEstimator : 325/1296 [00:00<00:01, 510.93it/s]
27%|##7 | Scoring GeneralizingEstimator : 350/1296 [00:00<00:01, 528.48it/s]
29%|##8 | Scoring GeneralizingEstimator : 372/1296 [00:00<00:01, 537.53it/s]
30%|### | Scoring GeneralizingEstimator : 389/1296 [00:00<00:01, 534.69it/s]
32%|###1 | Scoring GeneralizingEstimator : 412/1296 [00:00<00:01, 544.84it/s]
33%|###3 | Scoring GeneralizingEstimator : 434/1296 [00:00<00:01, 551.69it/s]
35%|###5 | Scoring GeneralizingEstimator : 458/1296 [00:00<00:01, 561.18it/s]
36%|###6 | Scoring GeneralizingEstimator : 473/1296 [00:00<00:01, 552.41it/s]
38%|###7 | Scoring GeneralizingEstimator : 492/1296 [00:00<00:01, 552.85it/s]
39%|###9 | Scoring GeneralizingEstimator : 511/1296 [00:00<00:01, 553.42it/s]
41%|####1 | Scoring GeneralizingEstimator : 534/1296 [00:00<00:01, 561.11it/s]
43%|####2 | Scoring GeneralizingEstimator : 551/1296 [00:01<00:01, 556.55it/s]
44%|####3 | Scoring GeneralizingEstimator : 567/1296 [00:01<00:01, 551.23it/s]
45%|####5 | Scoring GeneralizingEstimator : 584/1296 [00:01<00:01, 547.59it/s]
46%|####6 | Scoring GeneralizingEstimator : 601/1296 [00:01<00:01, 544.82it/s]
48%|####7 | Scoring GeneralizingEstimator : 618/1296 [00:01<00:01, 542.34it/s]
49%|####9 | Scoring GeneralizingEstimator : 639/1296 [00:01<00:01, 546.79it/s]
51%|#####1 | Scoring GeneralizingEstimator : 662/1296 [00:01<00:01, 554.51it/s]
53%|#####2 | Scoring GeneralizingEstimator : 683/1296 [00:01<00:01, 558.02it/s]
54%|#####4 | Scoring GeneralizingEstimator : 706/1296 [00:01<00:01, 564.93it/s]
56%|#####6 | Scoring GeneralizingEstimator : 727/1296 [00:01<00:01, 567.37it/s]
58%|#####7 | Scoring GeneralizingEstimator : 749/1296 [00:01<00:00, 572.05it/s]
60%|#####9 | Scoring GeneralizingEstimator : 772/1296 [00:01<00:00, 577.91it/s]
61%|######1 | Scoring GeneralizingEstimator : 792/1296 [00:01<00:00, 577.87it/s]
63%|######2 | Scoring GeneralizingEstimator : 814/1296 [00:01<00:00, 581.81it/s]
64%|######4 | Scoring GeneralizingEstimator : 833/1296 [00:01<00:00, 580.62it/s]
66%|######6 | Scoring GeneralizingEstimator : 856/1296 [00:01<00:00, 585.87it/s]
68%|######7 | Scoring GeneralizingEstimator : 877/1296 [00:01<00:00, 586.96it/s]
69%|######9 | Scoring GeneralizingEstimator : 898/1296 [00:01<00:00, 588.04it/s]
71%|#######1 | Scoring GeneralizingEstimator : 921/1296 [00:01<00:00, 593.06it/s]
73%|#######2 | Scoring GeneralizingEstimator : 942/1296 [00:01<00:00, 594.43it/s]
74%|#######4 | Scoring GeneralizingEstimator : 960/1296 [00:01<00:00, 590.74it/s]
76%|#######5 | Scoring GeneralizingEstimator : 979/1296 [00:01<00:00, 588.69it/s]
77%|#######7 | Scoring GeneralizingEstimator : 999/1296 [00:01<00:00, 588.10it/s]
79%|#######8 | Scoring GeneralizingEstimator : 1023/1296 [00:01<00:00, 594.44it/s]
81%|######## | Scoring GeneralizingEstimator : 1045/1296 [00:01<00:00, 597.05it/s]
82%|########2 | Scoring GeneralizingEstimator : 1065/1296 [00:01<00:00, 595.96it/s]
84%|########3 | Scoring GeneralizingEstimator : 1084/1296 [00:01<00:00, 593.84it/s]
85%|########5 | Scoring GeneralizingEstimator : 1104/1296 [00:01<00:00, 593.73it/s]
86%|########6 | Scoring GeneralizingEstimator : 1121/1296 [00:01<00:00, 588.78it/s]
88%|########7 | Scoring GeneralizingEstimator : 1138/1296 [00:02<00:00, 583.54it/s]
89%|########9 | Scoring GeneralizingEstimator : 1157/1296 [00:02<00:00, 582.14it/s]
91%|######### | Scoring GeneralizingEstimator : 1175/1296 [00:02<00:00, 579.44it/s]
92%|#########1| Scoring GeneralizingEstimator : 1191/1296 [00:02<00:00, 573.72it/s]
94%|#########3| Scoring GeneralizingEstimator : 1212/1296 [00:02<00:00, 576.00it/s]
95%|#########5| Scoring GeneralizingEstimator : 1234/1296 [00:02<00:00, 579.78it/s]
97%|#########6| Scoring GeneralizingEstimator : 1256/1296 [00:02<00:00, 582.47it/s]
99%|#########8| Scoring GeneralizingEstimator : 1279/1296 [00:02<00:00, 587.13it/s]
100%|##########| Scoring GeneralizingEstimator : 1296/1296 [00:02<00:00, 591.47it/s]
100%|##########| Scoring GeneralizingEstimator : 1296/1296 [00:02<00:00, 568.50it/s]
[Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 8.1s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 8.1s finished
Plot the full (generalization) matrix:
fig, ax = plt.subplots(1, 1)
im = ax.imshow(scores, interpolation='lanczos', origin='lower', cmap='RdBu_r',
extent=epochs.times[[0, -1, 0, -1]], vmin=0., vmax=1.)
ax.set_xlabel('Testing Time (s)')
ax.set_ylabel('Training Time (s)')
ax.set_title('Temporal generalization')
ax.axvline(0, color='k')
ax.axhline(0, color='k')
cbar = plt.colorbar(im, ax=ax)
cbar.set_label('AUC')
Projecting sensor-space patterns to source space#
If you use a linear classifier (or regressor) for your data, you can also
project these to source space. For example, using our evoked_time_gen
from before:
cov = mne.compute_covariance(epochs, tmax=0.)
del epochs
fwd = mne.read_forward_solution(
meg_path / 'sample_audvis-meg-eeg-oct-6-fwd.fif')
inv = mne.minimum_norm.make_inverse_operator(
evoked_time_gen.info, fwd, cov, loose=0.)
stc = mne.minimum_norm.apply_inverse(evoked_time_gen, inv, 1. / 9., 'dSPM')
del fwd, inv
Computing rank from data with rank=None
Using tolerance 3.1e-10 (2.2e-16 eps * 203 dim * 7e+03 max singular value)
Estimated rank (grad): 203
GRAD: rank 203 computed from 203 data channels with 0 projectors
Reducing data rank from 203 -> 203
Estimating covariance using EMPIRICAL
Done.
Number of samples used : 1353
[done]
Reading forward solution from /home/circleci/mne_data/MNE-sample-data/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif...
Reading a source space...
Computing patch statistics...
Patch information added...
Distance information added...
[done]
Reading a source space...
Computing patch statistics...
Patch information added...
Distance information added...
[done]
2 source spaces read
Desired named matrix (kind = 3523) not available
Read MEG forward solution (7498 sources, 306 channels, free orientations)
Desired named matrix (kind = 3523) not available
Read EEG forward solution (7498 sources, 60 channels, free orientations)
Forward solutions combined: MEG, EEG
Source spaces transformed to the forward solution coordinate frame
Computing inverse operator with 203 channels.
203 out of 366 channels remain after picking
Selected 203 channels
Creating the depth weighting matrix...
203 planar channels
limit = 7262/7498 = 10.020866
scale = 2.58122e-08 exp = 0.8
Picked elements from a free-orientation depth-weighting prior into the fixed-orientation one
Average patch normals will be employed in the rotation to the local surface coordinates....
Converting to surface-based source orientations...
[done]
Whitening the forward solution.
Computing rank from covariance with rank=None
Using tolerance 1.6e-13 (2.2e-16 eps * 203 dim * 3.6 max singular value)
Estimated rank (grad): 203
GRAD: rank 203 computed from 203 data channels with 0 projectors
Setting small GRAD eigenvalues to zero (without PCA)
Creating the source covariance matrix
Adjusting source covariance matrix.
Computing SVD of whitened and weighted lead field matrix.
largest singular value = 3.91709
scaling factor to adjust the trace = 6.26373e+18 (nchan = 203 nzero = 0)
Preparing the inverse operator for use...
Scaled noise and source covariance from nave = 1 to nave = 1
Created the regularized inverter
The projection vectors do not apply to these channels.
Created the whitener using a noise covariance matrix with rank 203 (0 small eigenvalues omitted)
Computing noise-normalization factors (dSPM)...
[done]
Applying inverse operator to ""...
Picked 203 channels from the data
Computing inverse...
Eigenleads need to be weighted ...
Computing residual...
Explained 76.4% variance
dSPM...
[done]
And this can be visualized using stc.plot
:
brain = stc.plot(hemi='split', views=('lat', 'med'), initial_time=0.1,
subjects_dir=subjects_dir)
Using control points [1.98776221 2.41838256 8.06628582]
Source-space decoding#
Source space decoding is also possible, but because the number of features can be much larger than in the sensor space, univariate feature selection using ANOVA f-test (or some other metric) can be done to reduce the feature dimension. Interpreting decoding results might be easier in source space as compared to sensor space.
Exercise#
Explore other datasets from MNE (e.g. Face dataset from SPM to predict Face vs. Scrambled)
References#
Total running time of the script: ( 0 minutes 31.097 seconds)
Estimated memory usage: 130 MB