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
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 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., 30., 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 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 366 out of 366 | elapsed:    0.6s finished

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(),
             y=epochs['Left'].events[:, 2] > 2)
  0%|          | Fitting GeneralizingEstimator : 0/35 [00:00<?,       ?it/s]
  6%|5         | Fitting GeneralizingEstimator : 2/35 [00:00<00:00,   58.28it/s]
 11%|#1        | Fitting GeneralizingEstimator : 4/35 [00:00<00:00,   58.70it/s]
 20%|##        | Fitting GeneralizingEstimator : 7/35 [00:00<00:00,   69.20it/s]
 29%|##8       | Fitting GeneralizingEstimator : 10/35 [00:00<00:00,   74.51it/s]
 40%|####      | Fitting GeneralizingEstimator : 14/35 [00:00<00:00,   84.19it/s]
 49%|####8     | Fitting GeneralizingEstimator : 17/35 [00:00<00:00,   85.10it/s]
 60%|######    | Fitting GeneralizingEstimator : 21/35 [00:00<00:00,   90.59it/s]
 66%|######5   | Fitting GeneralizingEstimator : 23/35 [00:00<00:00,   85.96it/s]
 74%|#######4  | Fitting GeneralizingEstimator : 26/35 [00:00<00:00,   86.32it/s]
 83%|########2 | Fitting GeneralizingEstimator : 29/35 [00:00<00:00,   86.63it/s]
 91%|#########1| Fitting GeneralizingEstimator : 32/35 [00:00<00:00,   86.87it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   88.23it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   87.01it/s]

Score on the epochs where the stimulus was presented to the right.

scores = time_gen.score(X=epochs['Right'].get_data(),
                        y=epochs['Right'].events[:, 2] > 2)
  0%|          | Scoring GeneralizingEstimator : 0/1225 [00:00<?,       ?it/s]
  1%|1         | Scoring GeneralizingEstimator : 17/1225 [00:00<00:02,  496.82it/s]
  3%|3         | Scoring GeneralizingEstimator : 40/1225 [00:00<00:02,  589.59it/s]
  5%|5         | Scoring GeneralizingEstimator : 62/1225 [00:00<00:01,  610.09it/s]
  7%|6         | Scoring GeneralizingEstimator : 85/1225 [00:00<00:01,  629.04it/s]
  9%|8         | Scoring GeneralizingEstimator : 107/1225 [00:00<00:01,  633.53it/s]
 11%|#         | Scoring GeneralizingEstimator : 130/1225 [00:00<00:01,  642.70it/s]
 12%|#2        | Scoring GeneralizingEstimator : 153/1225 [00:00<00:01,  649.08it/s]
 14%|#4        | Scoring GeneralizingEstimator : 176/1225 [00:00<00:01,  653.98it/s]
 16%|#6        | Scoring GeneralizingEstimator : 198/1225 [00:00<00:01,  653.34it/s]
 18%|#8        | Scoring GeneralizingEstimator : 221/1225 [00:00<00:01,  655.99it/s]
 20%|#9        | Scoring GeneralizingEstimator : 244/1225 [00:00<00:01,  658.70it/s]
 22%|##1       | Scoring GeneralizingEstimator : 267/1225 [00:00<00:01,  660.58it/s]
 24%|##3       | Scoring GeneralizingEstimator : 290/1225 [00:00<00:01,  662.45it/s]
 26%|##5       | Scoring GeneralizingEstimator : 313/1225 [00:00<00:01,  664.24it/s]
 27%|##7       | Scoring GeneralizingEstimator : 336/1225 [00:00<00:01,  665.91it/s]
 29%|##9       | Scoring GeneralizingEstimator : 359/1225 [00:00<00:01,  667.27it/s]
 31%|###1      | Scoring GeneralizingEstimator : 381/1225 [00:00<00:01,  665.88it/s]
 33%|###3      | Scoring GeneralizingEstimator : 405/1225 [00:00<00:01,  669.55it/s]
 35%|###5      | Scoring GeneralizingEstimator : 429/1225 [00:00<00:01,  672.93it/s]
 37%|###6      | Scoring GeneralizingEstimator : 453/1225 [00:00<00:01,  675.87it/s]
 39%|###8      | Scoring GeneralizingEstimator : 476/1225 [00:00<00:01,  676.35it/s]
 41%|####      | Scoring GeneralizingEstimator : 500/1225 [00:00<00:01,  678.88it/s]
 43%|####2     | Scoring GeneralizingEstimator : 523/1225 [00:00<00:01,  678.64it/s]
 45%|####4     | Scoring GeneralizingEstimator : 547/1225 [00:00<00:00,  680.87it/s]
 47%|####6     | Scoring GeneralizingEstimator : 570/1225 [00:00<00:00,  680.81it/s]
 48%|####8     | Scoring GeneralizingEstimator : 594/1225 [00:00<00:00,  682.81it/s]
 50%|#####     | Scoring GeneralizingEstimator : 618/1225 [00:00<00:00,  684.57it/s]
 52%|#####2    | Scoring GeneralizingEstimator : 641/1225 [00:00<00:00,  684.22it/s]
 54%|#####4    | Scoring GeneralizingEstimator : 665/1225 [00:00<00:00,  685.69it/s]
 56%|#####6    | Scoring GeneralizingEstimator : 689/1225 [00:01<00:00,  687.32it/s]
 58%|#####8    | Scoring GeneralizingEstimator : 713/1225 [00:01<00:00,  688.62it/s]
 60%|######    | Scoring GeneralizingEstimator : 736/1225 [00:01<00:00,  687.96it/s]
 62%|######2   | Scoring GeneralizingEstimator : 760/1225 [00:01<00:00,  689.29it/s]
 64%|######4   | Scoring GeneralizingEstimator : 784/1225 [00:01<00:00,  690.57it/s]
 66%|######5   | Scoring GeneralizingEstimator : 808/1225 [00:01<00:00,  691.79it/s]
 68%|######7   | Scoring GeneralizingEstimator : 831/1225 [00:01<00:00,  690.87it/s]
 70%|######9   | Scoring GeneralizingEstimator : 855/1225 [00:01<00:00,  691.92it/s]
 72%|#######1  | Scoring GeneralizingEstimator : 879/1225 [00:01<00:00,  693.08it/s]
 74%|#######3  | Scoring GeneralizingEstimator : 903/1225 [00:01<00:00,  694.05it/s]
 76%|#######5  | Scoring GeneralizingEstimator : 926/1225 [00:01<00:00,  692.89it/s]
 78%|#######7  | Scoring GeneralizingEstimator : 950/1225 [00:01<00:00,  693.86it/s]
 79%|#######9  | Scoring GeneralizingEstimator : 973/1225 [00:01<00:00,  693.06it/s]
 81%|########1 | Scoring GeneralizingEstimator : 997/1225 [00:01<00:00,  693.97it/s]
 83%|########3 | Scoring GeneralizingEstimator : 1020/1225 [00:01<00:00,  693.29it/s]
 85%|########5 | Scoring GeneralizingEstimator : 1043/1225 [00:01<00:00,  692.42it/s]
 87%|########7 | Scoring GeneralizingEstimator : 1067/1225 [00:01<00:00,  693.39it/s]
 89%|########8 | Scoring GeneralizingEstimator : 1090/1225 [00:01<00:00,  692.65it/s]
 91%|######### | Scoring GeneralizingEstimator : 1114/1225 [00:01<00:00,  693.61it/s]
 93%|#########2| Scoring GeneralizingEstimator : 1138/1225 [00:01<00:00,  694.53it/s]
 95%|#########4| Scoring GeneralizingEstimator : 1162/1225 [00:01<00:00,  695.23it/s]
 97%|#########6| Scoring GeneralizingEstimator : 1186/1225 [00:01<00:00,  696.01it/s]
 99%|#########8| Scoring GeneralizingEstimator : 1209/1225 [00:01<00:00,  695.15it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:01<00:00,  696.66it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:01<00:00,  687.41it/s]

Plot

fig, ax = plt.subplots(1)
im = ax.matshow(scores, vmin=0, vmax=1., cmap='RdBu_r', origin='lower',
                extent=epochs.times[[0, -1, 0, -1]])
ax.axhline(0., color='k')
ax.axvline(0., color='k')
ax.xaxis.set_ticks_position('bottom')
ax.set_xlabel('Testing Time (s)')
ax.set_ylabel('Training Time (s)')
ax.set_title('Generalization across time and condition')
plt.colorbar(im, ax=ax)
plt.show()
Generalization across time and condition

References#

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

Estimated memory usage: 128 MB

Gallery generated by Sphinx-Gallery

On this page