Note
Click here 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'
raw_fname = data_path + '/MEG/sample/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
Out:
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)
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='lbfgs'))
scores = cross_val_multiscore(clf, X, y, cv=5, n_jobs=1)
# Mean scores across cross-validation splits
score = np.mean(scores, axis=0)
print('Spatio-temporal: %0.1f%%' % (100 * score,))
Out:
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.
Examples
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='lbfgs')))
scores = cross_val_multiscore(clf_csp, X, y, cv=5, n_jobs=1)
print('CSP: %0.1f%%' % (100 * scores.mean(),))
Out:
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.
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.
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.
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.
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.
CSP: 86.2%
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.
Examples
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)
Out:
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 x n_channels x 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='lbfgs'))
time_decod = SlidingEstimator(clf, n_jobs=1, 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=1)
# 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')
Out:
0%| | Fitting SlidingEstimator : 0/36 [00:00<?, ?it/s]
3%|2 | Fitting SlidingEstimator : 1/36 [00:00<00:01, 23.64it/s]
8%|8 | Fitting SlidingEstimator : 3/36 [00:00<00:01, 25.21it/s]
14%|#3 | Fitting SlidingEstimator : 5/36 [00:00<00:00, 32.59it/s]
17%|#6 | Fitting SlidingEstimator : 6/36 [00:00<00:01, 27.29it/s]
22%|##2 | Fitting SlidingEstimator : 8/36 [00:00<00:00, 31.56it/s]
25%|##5 | Fitting SlidingEstimator : 9/36 [00:00<00:00, 28.08it/s]
31%|### | Fitting SlidingEstimator : 11/36 [00:00<00:00, 31.56it/s]
33%|###3 | Fitting SlidingEstimator : 12/36 [00:00<00:00, 28.47it/s]
42%|####1 | Fitting SlidingEstimator : 15/36 [00:00<00:00, 34.00it/s]
44%|####4 | Fitting SlidingEstimator : 16/36 [00:00<00:00, 30.99it/s]
53%|#####2 | Fitting SlidingEstimator : 19/36 [00:00<00:00, 35.53it/s]
58%|#####8 | Fitting SlidingEstimator : 21/36 [00:00<00:00, 34.80it/s]
67%|######6 | Fitting SlidingEstimator : 24/36 [00:00<00:00, 38.07it/s]
69%|######9 | Fitting SlidingEstimator : 25/36 [00:00<00:00, 35.63it/s]
81%|######## | Fitting SlidingEstimator : 29/36 [00:00<00:00, 40.83it/s]
83%|########3 | Fitting SlidingEstimator : 30/36 [00:00<00:00, 37.90it/s]
92%|#########1| Fitting SlidingEstimator : 33/36 [00:00<00:00, 40.84it/s]
94%|#########4| Fitting SlidingEstimator : 34/36 [00:00<00:00, 38.10it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 40.19it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 38.34it/s]
0%| | Fitting SlidingEstimator : 0/36 [00:00<?, ?it/s]
3%|2 | Fitting SlidingEstimator : 1/36 [00:00<00:01, 29.23it/s]
6%|5 | Fitting SlidingEstimator : 2/36 [00:00<00:01, 27.22it/s]
14%|#3 | Fitting SlidingEstimator : 5/36 [00:00<00:00, 44.90it/s]
19%|#9 | Fitting SlidingEstimator : 7/36 [00:00<00:00, 33.72it/s]
25%|##5 | Fitting SlidingEstimator : 9/36 [00:00<00:00, 32.83it/s]
31%|### | Fitting SlidingEstimator : 11/36 [00:00<00:00, 36.07it/s]
33%|###3 | Fitting SlidingEstimator : 12/36 [00:00<00:00, 31.83it/s]
44%|####4 | Fitting SlidingEstimator : 16/36 [00:00<00:00, 40.24it/s]
47%|####7 | Fitting SlidingEstimator : 17/36 [00:00<00:00, 36.09it/s]
58%|#####8 | Fitting SlidingEstimator : 21/36 [00:00<00:00, 42.90it/s]
61%|######1 | Fitting SlidingEstimator : 22/36 [00:00<00:00, 38.76it/s]
69%|######9 | Fitting SlidingEstimator : 25/36 [00:00<00:00, 42.37it/s]
75%|#######5 | Fitting SlidingEstimator : 27/36 [00:00<00:00, 40.90it/s]
83%|########3 | Fitting SlidingEstimator : 30/36 [00:00<00:00, 43.98it/s]
86%|########6 | Fitting SlidingEstimator : 31/36 [00:00<00:00, 40.59it/s]
94%|#########4| Fitting SlidingEstimator : 34/36 [00:00<00:00, 43.45it/s]
97%|#########7| Fitting SlidingEstimator : 35/36 [00:00<00:00, 40.35it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 40.93it/s]
0%| | Fitting SlidingEstimator : 0/36 [00:00<?, ?it/s]
8%|8 | Fitting SlidingEstimator : 3/36 [00:00<00:00, 73.69it/s]
11%|#1 | Fitting SlidingEstimator : 4/36 [00:00<00:00, 45.63it/s]
19%|#9 | Fitting SlidingEstimator : 7/36 [00:00<00:00, 57.33it/s]
22%|##2 | Fitting SlidingEstimator : 8/36 [00:00<00:00, 42.01it/s]
31%|### | Fitting SlidingEstimator : 11/36 [00:00<00:00, 49.39it/s]
33%|###3 | Fitting SlidingEstimator : 12/36 [00:00<00:00, 40.94it/s]
44%|####4 | Fitting SlidingEstimator : 16/36 [00:00<00:00, 50.22it/s]
50%|##### | Fitting SlidingEstimator : 18/36 [00:00<00:00, 46.34it/s]
61%|######1 | Fitting SlidingEstimator : 22/36 [00:00<00:00, 52.48it/s]
64%|######3 | Fitting SlidingEstimator : 23/36 [00:00<00:00, 46.99it/s]
75%|#######5 | Fitting SlidingEstimator : 27/36 [00:00<00:00, 52.88it/s]
78%|#######7 | Fitting SlidingEstimator : 28/36 [00:00<00:00, 47.39it/s]
89%|########8 | Fitting SlidingEstimator : 32/36 [00:00<00:00, 52.48it/s]
92%|#########1| Fitting SlidingEstimator : 33/36 [00:00<00:00, 47.67it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 51.43it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:00<00:00, 50.72it/s]
You can retrieve the spatial filters and spatial patterns if you explicitly use a LinearModel
clf = make_pipeline(StandardScaler(),
LinearModel(LogisticRegression(solver='lbfgs')))
time_decod = SlidingEstimator(clf, n_jobs=1, 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)
Out:
0%| | Fitting SlidingEstimator : 0/36 [00:00<?, ?it/s]
3%|2 | Fitting SlidingEstimator : 1/36 [00:00<00:01, 28.89it/s]
8%|8 | Fitting SlidingEstimator : 3/36 [00:00<00:00, 44.15it/s]
11%|#1 | Fitting SlidingEstimator : 4/36 [00:00<00:00, 32.20it/s]
17%|#6 | Fitting SlidingEstimator : 6/36 [00:00<00:00, 38.43it/s]
19%|#9 | Fitting SlidingEstimator : 7/36 [00:00<00:00, 30.92it/s]
25%|##5 | Fitting SlidingEstimator : 9/36 [00:00<00:00, 34.24it/s]
28%|##7 | Fitting SlidingEstimator : 10/36 [00:00<00:00, 29.05it/s]
36%|###6 | Fitting SlidingEstimator : 13/36 [00:00<00:00, 35.45it/s]
44%|####4 | Fitting SlidingEstimator : 16/36 [00:00<00:00, 35.29it/s]
50%|##### | Fitting SlidingEstimator : 18/36 [00:00<00:00, 34.20it/s]
58%|#####8 | Fitting SlidingEstimator : 21/36 [00:00<00:00, 38.12it/s]
61%|######1 | Fitting SlidingEstimator : 22/36 [00:00<00:00, 35.48it/s]
67%|######6 | Fitting SlidingEstimator : 24/36 [00:00<00:00, 37.09it/s]
69%|######9 | Fitting SlidingEstimator : 25/36 [00:00<00:00, 36.58it/s]
72%|#######2 | Fitting SlidingEstimator : 26/36 [00:00<00:00, 36.12it/s]
75%|#######5 | Fitting SlidingEstimator : 27/36 [00:00<00:00, 35.47it/s]
78%|#######7 | Fitting SlidingEstimator : 28/36 [00:00<00:00, 33.38it/s]
83%|########3 | Fitting SlidingEstimator : 30/36 [00:00<00:00, 34.84it/s]
86%|########6 | Fitting SlidingEstimator : 31/36 [00:00<00:00, 34.52it/s]
89%|########8 | Fitting SlidingEstimator : 32/36 [00:00<00:00, 34.17it/s]
94%|#########4| Fitting SlidingEstimator : 34/36 [00:00<00:00, 35.47it/s]
97%|#########7| Fitting SlidingEstimator : 35/36 [00:00<00:00, 35.14it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:01<00:00, 34.49it/s]
100%|##########| Fitting SlidingEstimator : 36/36 [00:01<00:00, 34.72it/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=1, scoring='roc_auc',
verbose=True)
# again, cv=3 just for speed
scores = cross_val_multiscore(time_gen, X, y, cv=3, n_jobs=1)
# 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')
Out:
0%| | Fitting GeneralizingEstimator : 0/36 [00:00<?, ?it/s]
6%|5 | Fitting GeneralizingEstimator : 2/36 [00:00<00:00, 58.40it/s]
14%|#3 | Fitting GeneralizingEstimator : 5/36 [00:00<00:00, 73.02it/s]
17%|#6 | Fitting GeneralizingEstimator : 6/36 [00:00<00:00, 41.98it/s]
25%|##5 | Fitting GeneralizingEstimator : 9/36 [00:00<00:00, 51.47it/s]
31%|### | Fitting GeneralizingEstimator : 11/36 [00:00<00:00, 45.28it/s]
39%|###8 | Fitting GeneralizingEstimator : 14/36 [00:00<00:00, 51.34it/s]
42%|####1 | Fitting GeneralizingEstimator : 15/36 [00:00<00:00, 43.25it/s]
50%|##### | Fitting GeneralizingEstimator : 18/36 [00:00<00:00, 46.95it/s]
53%|#####2 | Fitting GeneralizingEstimator : 19/36 [00:00<00:00, 42.20it/s]
58%|#####8 | Fitting GeneralizingEstimator : 21/36 [00:00<00:00, 43.49it/s]
61%|######1 | Fitting GeneralizingEstimator : 22/36 [00:00<00:00, 39.29it/s]
69%|######9 | Fitting GeneralizingEstimator : 25/36 [00:00<00:00, 43.07it/s]
72%|#######2 | Fitting GeneralizingEstimator : 26/36 [00:00<00:00, 39.26it/s]
81%|######## | Fitting GeneralizingEstimator : 29/36 [00:00<00:00, 42.45it/s]
83%|########3 | Fitting GeneralizingEstimator : 30/36 [00:00<00:00, 38.95it/s]
94%|#########4| Fitting GeneralizingEstimator : 34/36 [00:00<00:00, 43.83it/s]
97%|#########7| Fitting GeneralizingEstimator : 35/36 [00:00<00:00, 40.90it/s]
100%|##########| Fitting GeneralizingEstimator : 36/36 [00:00<00:00, 42.45it/s]
0%| | Scoring GeneralizingEstimator : 0/1296 [00:00<?, ?it/s]
1%|1 | Scoring GeneralizingEstimator : 15/1296 [00:00<00:05, 232.74it/s]
4%|3 | Scoring GeneralizingEstimator : 48/1296 [00:00<00:02, 496.70it/s]
6%|6 | Scoring GeneralizingEstimator : 84/1296 [00:00<00:01, 649.59it/s]
9%|8 | Scoring GeneralizingEstimator : 116/1296 [00:00<00:01, 715.58it/s]
12%|#1 | Scoring GeneralizingEstimator : 152/1296 [00:00<00:01, 782.00it/s]
15%|#4 | Scoring GeneralizingEstimator : 188/1296 [00:00<00:01, 829.03it/s]
17%|#7 | Scoring GeneralizingEstimator : 224/1296 [00:00<00:01, 864.21it/s]
20%|#9 | Scoring GeneralizingEstimator : 256/1296 [00:00<00:01, 875.28it/s]
22%|##2 | Scoring GeneralizingEstimator : 288/1296 [00:00<00:01, 882.52it/s]
24%|##4 | Scoring GeneralizingEstimator : 316/1296 [00:00<00:01, 875.50it/s]
26%|##6 | Scoring GeneralizingEstimator : 339/1296 [00:00<00:01, 853.98it/s]
29%|##8 | Scoring GeneralizingEstimator : 374/1296 [00:00<00:01, 871.93it/s]
31%|###1 | Scoring GeneralizingEstimator : 407/1296 [00:00<00:01, 881.37it/s]
34%|###3 | Scoring GeneralizingEstimator : 436/1296 [00:00<00:00, 879.14it/s]
36%|###6 | Scoring GeneralizingEstimator : 469/1296 [00:00<00:00, 887.65it/s]
39%|###8 | Scoring GeneralizingEstimator : 504/1296 [00:00<00:00, 899.59it/s]
42%|####1 | Scoring GeneralizingEstimator : 540/1296 [00:00<00:00, 913.25it/s]
44%|####4 | Scoring GeneralizingEstimator : 576/1296 [00:00<00:00, 925.26it/s]
47%|####7 | Scoring GeneralizingEstimator : 612/1296 [00:00<00:00, 936.25it/s]
50%|##### | Scoring GeneralizingEstimator : 648/1296 [00:00<00:00, 945.98it/s]
53%|#####2 | Scoring GeneralizingEstimator : 685/1296 [00:00<00:00, 956.47it/s]
56%|#####5 | Scoring GeneralizingEstimator : 721/1296 [00:00<00:00, 964.39it/s]
58%|#####8 | Scoring GeneralizingEstimator : 758/1296 [00:00<00:00, 972.64it/s]
61%|######1 | Scoring GeneralizingEstimator : 794/1296 [00:00<00:00, 978.96it/s]
64%|######4 | Scoring GeneralizingEstimator : 830/1296 [00:00<00:00, 984.31it/s]
67%|######6 | Scoring GeneralizingEstimator : 867/1296 [00:00<00:00, 991.66it/s]
70%|######9 | Scoring GeneralizingEstimator : 903/1296 [00:00<00:00, 996.59it/s]
73%|#######2 | Scoring GeneralizingEstimator : 940/1296 [00:00<00:00, 1002.42it/s]
75%|#######5 | Scoring GeneralizingEstimator : 977/1296 [00:01<00:00, 1007.64it/s]
78%|#######8 | Scoring GeneralizingEstimator : 1012/1296 [00:01<00:00, 1009.35it/s]
81%|######## | Scoring GeneralizingEstimator : 1049/1296 [00:01<00:00, 1014.19it/s]
84%|########3 | Scoring GeneralizingEstimator : 1086/1296 [00:01<00:00, 1019.13it/s]
87%|########6 | Scoring GeneralizingEstimator : 1122/1296 [00:01<00:00, 1021.82it/s]
89%|########9 | Scoring GeneralizingEstimator : 1159/1296 [00:01<00:00, 1026.00it/s]
92%|#########2| Scoring GeneralizingEstimator : 1196/1296 [00:01<00:00, 1030.05it/s]
95%|#########4| Scoring GeneralizingEstimator : 1231/1296 [00:01<00:00, 1030.28it/s]
98%|#########7| Scoring GeneralizingEstimator : 1266/1296 [00:01<00:00, 1030.45it/s]
100%|##########| Scoring GeneralizingEstimator : 1296/1296 [00:01<00:00, 1024.05it/s]
100%|##########| Scoring GeneralizingEstimator : 1296/1296 [00:01<00:00, 982.77it/s]
0%| | Fitting GeneralizingEstimator : 0/36 [00:00<?, ?it/s]
6%|5 | Fitting GeneralizingEstimator : 2/36 [00:00<00:00, 48.12it/s]
14%|#3 | Fitting GeneralizingEstimator : 5/36 [00:00<00:00, 66.81it/s]
17%|#6 | Fitting GeneralizingEstimator : 6/36 [00:00<00:00, 41.62it/s]
25%|##5 | Fitting GeneralizingEstimator : 9/36 [00:00<00:00, 51.32it/s]
28%|##7 | Fitting GeneralizingEstimator : 10/36 [00:00<00:00, 40.53it/s]
36%|###6 | Fitting GeneralizingEstimator : 13/36 [00:00<00:00, 47.14it/s]
39%|###8 | Fitting GeneralizingEstimator : 14/36 [00:00<00:00, 44.89it/s]
42%|####1 | Fitting GeneralizingEstimator : 15/36 [00:00<00:00, 42.93it/s]
53%|#####2 | Fitting GeneralizingEstimator : 19/36 [00:00<00:00, 51.22it/s]
56%|#####5 | Fitting GeneralizingEstimator : 20/36 [00:00<00:00, 44.92it/s]
67%|######6 | Fitting GeneralizingEstimator : 24/36 [00:00<00:00, 51.58it/s]
69%|######9 | Fitting GeneralizingEstimator : 25/36 [00:00<00:00, 45.84it/s]
78%|#######7 | Fitting GeneralizingEstimator : 28/36 [00:00<00:00, 49.13it/s]
83%|########3 | Fitting GeneralizingEstimator : 30/36 [00:00<00:00, 46.56it/s]
92%|#########1| Fitting GeneralizingEstimator : 33/36 [00:00<00:00, 49.44it/s]
94%|#########4| Fitting GeneralizingEstimator : 34/36 [00:00<00:00, 45.19it/s]
100%|##########| Fitting GeneralizingEstimator : 36/36 [00:00<00:00, 47.44it/s]
100%|##########| Fitting GeneralizingEstimator : 36/36 [00:00<00:00, 47.34it/s]
0%| | Scoring GeneralizingEstimator : 0/1296 [00:00<?, ?it/s]
1%|1 | Scoring GeneralizingEstimator : 19/1296 [00:00<00:04, 292.26it/s]
4%|4 | Scoring GeneralizingEstimator : 52/1296 [00:00<00:02, 533.24it/s]
7%|6 | Scoring GeneralizingEstimator : 88/1296 [00:00<00:01, 677.57it/s]
10%|9 | Scoring GeneralizingEstimator : 124/1296 [00:00<00:01, 763.21it/s]
12%|#2 | Scoring GeneralizingEstimator : 160/1296 [00:00<00:01, 818.26it/s]
15%|#5 | Scoring GeneralizingEstimator : 197/1296 [00:00<00:01, 863.11it/s]
18%|#8 | Scoring GeneralizingEstimator : 234/1296 [00:00<00:01, 896.35it/s]
21%|## | Scoring GeneralizingEstimator : 271/1296 [00:00<00:01, 922.71it/s]
24%|##3 | Scoring GeneralizingEstimator : 308/1296 [00:00<00:01, 943.85it/s]
27%|##6 | Scoring GeneralizingEstimator : 345/1296 [00:00<00:00, 960.48it/s]
29%|##9 | Scoring GeneralizingEstimator : 382/1296 [00:00<00:00, 974.77it/s]
32%|###1 | Scoring GeneralizingEstimator : 413/1296 [00:00<00:00, 968.75it/s]
35%|###4 | Scoring GeneralizingEstimator : 449/1296 [00:00<00:00, 978.00it/s]
37%|###6 | Scoring GeneralizingEstimator : 474/1296 [00:00<00:00, 955.50it/s]
38%|###8 | Scoring GeneralizingEstimator : 497/1296 [00:00<00:00, 930.93it/s]
40%|#### | Scoring GeneralizingEstimator : 522/1296 [00:00<00:00, 914.22it/s]
42%|####2 | Scoring GeneralizingEstimator : 546/1296 [00:00<00:00, 897.22it/s]
44%|####3 | Scoring GeneralizingEstimator : 569/1296 [00:00<00:00, 879.35it/s]
47%|####6 | Scoring GeneralizingEstimator : 604/1296 [00:00<00:00, 891.34it/s]
49%|####9 | Scoring GeneralizingEstimator : 639/1296 [00:00<00:00, 902.25it/s]
52%|#####1 | Scoring GeneralizingEstimator : 672/1296 [00:00<00:00, 907.35it/s]
54%|#####4 | Scoring GeneralizingEstimator : 706/1296 [00:00<00:00, 914.50it/s]
57%|#####7 | Scoring GeneralizingEstimator : 742/1296 [00:00<00:00, 925.18it/s]
60%|#####9 | Scoring GeneralizingEstimator : 776/1296 [00:00<00:00, 930.61it/s]
62%|######2 | Scoring GeneralizingEstimator : 807/1296 [00:00<00:00, 929.24it/s]
65%|######4 | Scoring GeneralizingEstimator : 838/1296 [00:00<00:00, 928.37it/s]
67%|######6 | Scoring GeneralizingEstimator : 862/1296 [00:00<00:00, 914.01it/s]
69%|######8 | Scoring GeneralizingEstimator : 889/1296 [00:00<00:00, 906.45it/s]
71%|####### | Scoring GeneralizingEstimator : 914/1296 [00:01<00:00, 894.61it/s]
72%|#######2 | Scoring GeneralizingEstimator : 939/1296 [00:01<00:00, 884.97it/s]
75%|#######4 | Scoring GeneralizingEstimator : 970/1296 [00:01<00:00, 886.54it/s]
77%|#######7 | Scoring GeneralizingEstimator : 1001/1296 [00:01<00:00, 888.27it/s]
80%|#######9 | Scoring GeneralizingEstimator : 1031/1296 [00:01<00:00, 887.93it/s]
82%|########2 | Scoring GeneralizingEstimator : 1063/1296 [00:01<00:00, 891.36it/s]
85%|########4 | Scoring GeneralizingEstimator : 1096/1296 [00:01<00:00, 896.33it/s]
87%|########6 | Scoring GeneralizingEstimator : 1124/1296 [00:01<00:00, 892.06it/s]
89%|########8 | Scoring GeneralizingEstimator : 1152/1296 [00:01<00:00, 887.72it/s]
92%|#########1| Scoring GeneralizingEstimator : 1189/1296 [00:01<00:00, 899.39it/s]
94%|#########4| Scoring GeneralizingEstimator : 1224/1296 [00:01<00:00, 906.83it/s]
97%|#########7| Scoring GeneralizingEstimator : 1259/1296 [00:01<00:00, 914.05it/s]
100%|#########9| Scoring GeneralizingEstimator : 1293/1296 [00:01<00:00, 919.17it/s]
100%|##########| Scoring GeneralizingEstimator : 1296/1296 [00:01<00:00, 908.38it/s]
0%| | Fitting GeneralizingEstimator : 0/36 [00:00<?, ?it/s]
6%|5 | Fitting GeneralizingEstimator : 2/36 [00:00<00:00, 57.51it/s]
11%|#1 | Fitting GeneralizingEstimator : 4/36 [00:00<00:01, 28.61it/s]
14%|#3 | Fitting GeneralizingEstimator : 5/36 [00:00<00:01, 24.01it/s]
19%|#9 | Fitting GeneralizingEstimator : 7/36 [00:00<00:01, 28.89it/s]
22%|##2 | Fitting GeneralizingEstimator : 8/36 [00:00<00:01, 26.04it/s]
28%|##7 | Fitting GeneralizingEstimator : 10/36 [00:00<00:00, 29.82it/s]
36%|###6 | Fitting GeneralizingEstimator : 13/36 [00:00<00:00, 29.90it/s]
39%|###8 | Fitting GeneralizingEstimator : 14/36 [00:00<00:00, 27.59it/s]
44%|####4 | Fitting GeneralizingEstimator : 16/36 [00:00<00:00, 29.89it/s]
47%|####7 | Fitting GeneralizingEstimator : 17/36 [00:00<00:00, 29.86it/s]
58%|#####8 | Fitting GeneralizingEstimator : 21/36 [00:00<00:00, 33.65it/s]
61%|######1 | Fitting GeneralizingEstimator : 22/36 [00:00<00:00, 31.75it/s]
69%|######9 | Fitting GeneralizingEstimator : 25/36 [00:00<00:00, 35.27it/s]
72%|#######2 | Fitting GeneralizingEstimator : 26/36 [00:00<00:00, 32.94it/s]
81%|######## | Fitting GeneralizingEstimator : 29/36 [00:00<00:00, 36.14it/s]
86%|########6 | Fitting GeneralizingEstimator : 31/36 [00:00<00:00, 35.59it/s]
94%|#########4| Fitting GeneralizingEstimator : 34/36 [00:00<00:00, 38.45it/s]
97%|#########7| Fitting GeneralizingEstimator : 35/36 [00:01<00:00, 36.12it/s]
100%|##########| Fitting GeneralizingEstimator : 36/36 [00:01<00:00, 35.57it/s]
0%| | Scoring GeneralizingEstimator : 0/1296 [00:00<?, ?it/s]
1%|1 | Scoring GeneralizingEstimator : 13/1296 [00:00<00:06, 201.31it/s]
3%|3 | Scoring GeneralizingEstimator : 44/1296 [00:00<00:02, 453.76it/s]
6%|5 | Scoring GeneralizingEstimator : 77/1296 [00:00<00:02, 595.30it/s]
8%|8 | Scoring GeneralizingEstimator : 106/1296 [00:00<00:01, 653.27it/s]
10%|# | Scoring GeneralizingEstimator : 132/1296 [00:00<00:01, 673.03it/s]
12%|#2 | Scoring GeneralizingEstimator : 157/1296 [00:00<00:01, 683.87it/s]
14%|#3 | Scoring GeneralizingEstimator : 179/1296 [00:00<00:01, 678.42it/s]
16%|#5 | Scoring GeneralizingEstimator : 202/1296 [00:00<00:01, 678.76it/s]
17%|#7 | Scoring GeneralizingEstimator : 225/1296 [00:00<00:01, 677.84it/s]
20%|#9 | Scoring GeneralizingEstimator : 254/1296 [00:00<00:01, 698.76it/s]
22%|##2 | Scoring GeneralizingEstimator : 290/1296 [00:00<00:01, 738.64it/s]
25%|##5 | Scoring GeneralizingEstimator : 325/1296 [00:00<00:01, 768.72it/s]
28%|##7 | Scoring GeneralizingEstimator : 362/1296 [00:00<00:01, 800.01it/s]
31%|### | Scoring GeneralizingEstimator : 399/1296 [00:00<00:01, 826.21it/s]
33%|###2 | Scoring GeneralizingEstimator : 423/1296 [00:00<00:01, 814.39it/s]
35%|###4 | Scoring GeneralizingEstimator : 452/1296 [00:00<00:01, 817.61it/s]
37%|###6 | Scoring GeneralizingEstimator : 477/1296 [00:00<00:01, 810.51it/s]
39%|###8 | Scoring GeneralizingEstimator : 501/1296 [00:00<00:00, 802.05it/s]
41%|####1 | Scoring GeneralizingEstimator : 534/1296 [00:00<00:00, 815.57it/s]
44%|####3 | Scoring GeneralizingEstimator : 569/1296 [00:00<00:00, 832.12it/s]
46%|####6 | Scoring GeneralizingEstimator : 601/1296 [00:00<00:00, 840.52it/s]
49%|####8 | Scoring GeneralizingEstimator : 630/1296 [00:00<00:00, 841.73it/s]
51%|##### | Scoring GeneralizingEstimator : 659/1296 [00:00<00:00, 842.36it/s]
53%|#####3 | Scoring GeneralizingEstimator : 691/1296 [00:00<00:00, 849.00it/s]
56%|#####5 | Scoring GeneralizingEstimator : 725/1296 [00:00<00:00, 859.65it/s]
58%|#####8 | Scoring GeneralizingEstimator : 753/1296 [00:00<00:00, 857.47it/s]
60%|###### | Scoring GeneralizingEstimator : 778/1296 [00:00<00:00, 849.78it/s]
62%|######2 | Scoring GeneralizingEstimator : 808/1296 [00:00<00:00, 851.31it/s]
65%|######4 | Scoring GeneralizingEstimator : 842/1296 [00:01<00:00, 860.94it/s]
67%|######7 | Scoring GeneralizingEstimator : 874/1296 [00:01<00:00, 866.03it/s]
70%|######9 | Scoring GeneralizingEstimator : 901/1296 [00:01<00:00, 861.82it/s]
71%|#######1 | Scoring GeneralizingEstimator : 924/1296 [00:01<00:00, 850.37it/s]
73%|#######2 | Scoring GeneralizingEstimator : 945/1296 [00:01<00:00, 836.57it/s]
74%|#######3 | Scoring GeneralizingEstimator : 957/1296 [00:01<00:00, 807.31it/s]
76%|#######5 | Scoring GeneralizingEstimator : 983/1296 [00:01<00:00, 805.08it/s]
78%|#######7 | Scoring GeneralizingEstimator : 1007/1296 [00:01<00:00, 799.43it/s]
80%|######## | Scoring GeneralizingEstimator : 1037/1296 [00:01<00:00, 804.47it/s]
82%|########2 | Scoring GeneralizingEstimator : 1069/1296 [00:01<00:00, 812.33it/s]
84%|########4 | Scoring GeneralizingEstimator : 1092/1296 [00:01<00:00, 804.75it/s]
86%|########6 | Scoring GeneralizingEstimator : 1117/1296 [00:01<00:00, 800.75it/s]
88%|########8 | Scoring GeneralizingEstimator : 1144/1296 [00:01<00:00, 800.60it/s]
91%|######### | Scoring GeneralizingEstimator : 1178/1296 [00:01<00:00, 812.09it/s]
94%|#########3| Scoring GeneralizingEstimator : 1212/1296 [00:01<00:00, 822.74it/s]
96%|#########5| Scoring GeneralizingEstimator : 1242/1296 [00:01<00:00, 825.70it/s]
98%|#########7| Scoring GeneralizingEstimator : 1269/1296 [00:01<00:00, 824.27it/s]
100%|#########9| Scoring GeneralizingEstimator : 1295/1296 [00:01<00:00, 821.29it/s]
100%|##########| Scoring GeneralizingEstimator : 1296/1296 [00:01<00:00, 812.69it/s]
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(
data_path + '/MEG/sample/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
Out:
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)
MEG and EEG forward solutions combined
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)
Out:
Using control points [2.01542836 2.45337108 8.12707429]
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.
Examples
Exercise¶
Explore other datasets from MNE (e.g. Face dataset from SPM to predict Face vs. Scrambled)
References¶
- 1
Jean-Rémi King, Laura Gwilliams, Chris Holdgraf, Jona Sassenhagen, Alexandre Barachant, Denis Engemann, Eric Larson, and Alexandre Gramfort. Encoding and decoding neuronal dynamics: methodological framework to uncover the algorithms of cognition. hal-01848442, 2018. URL: https://hal.archives-ouvertes.fr/hal-01848442.
- 2
Zoltan J. Koles. The quantitative extraction and topographic mapping of the abnormal components in the clinical EEG. Electroencephalography and Clinical Neurophysiology, 79(6):440–447, 1991. doi:10.1016/0013-4694(91)90163-X.
- 3
Sven Dähne, Frank C. Meinecke, Stefan Haufe, Johannes Höhne, Michael Tangermann, Klaus-Robert Müller, and Vadim V. Nikulin. SPoC: a novel framework for relating the amplitude of neuronal oscillations to behaviorally relevant parameters. NeuroImage, 86:111–122, 2014. doi:10.1016/j.neuroimage.2013.07.079.
- 4
Bertrand Rivet, Antoine Souloumiac, Virginie Attina, and Guillaume Gibert. xDAWN algorithm to enhance evoked potentials: application to brain–computer interface. IEEE Transactions on Biomedical Engineering, 56(8):2035–2043, 2009. doi:10.1109/TBME.2009.2012869.
- 5
Aaron Schurger, Sebastien Marti, and Stanislas Dehaene. Reducing multi-sensor data to a single time course that reveals experimental effects. BMC Neuroscience, 2013. doi:10.1186/1471-2202-14-122.
- 6
Jean-Rémi King, Alexandre Gramfort, Aaron Schurger, Lionel Naccache, and Stanislas Dehaene. Two distinct dynamic modes subtend the detection of unexpected sounds. PLoS ONE, 9(1):e85791, 2014. doi:10.1371/journal.pone.0085791.
- 7
Jean-Rémi King and Stanislas Dehaene. Characterizing the dynamics of mental representations: the temporal generalization method. Trends in Cognitive Sciences, 18(4):203–210, 2014. doi:10.1016/j.tics.2014.01.002.
Total running time of the script: ( 0 minutes 35.775 seconds)
Estimated memory usage: 77 MB