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.20it/s]
 11%|#1        | Fitting GeneralizingEstimator : 4/35 [00:00<00:00,   58.60it/s]
 14%|#4        | Fitting GeneralizingEstimator : 5/35 [00:00<00:00,   48.48it/s]
 23%|##2       | Fitting GeneralizingEstimator : 8/35 [00:00<00:00,   59.31it/s]
 29%|##8       | Fitting GeneralizingEstimator : 10/35 [00:00<00:00,   59.29it/s]
 37%|###7      | Fitting GeneralizingEstimator : 13/35 [00:00<00:00,   64.87it/s]
 46%|####5     | Fitting GeneralizingEstimator : 16/35 [00:00<00:00,   68.83it/s]
 54%|#####4    | Fitting GeneralizingEstimator : 19/35 [00:00<00:00,   71.80it/s]
 63%|######2   | Fitting GeneralizingEstimator : 22/35 [00:00<00:00,   74.12it/s]
 69%|######8   | Fitting GeneralizingEstimator : 24/35 [00:00<00:00,   72.28it/s]
 77%|#######7  | Fitting GeneralizingEstimator : 27/35 [00:00<00:00,   74.19it/s]
 83%|########2 | Fitting GeneralizingEstimator : 29/35 [00:00<00:00,   72.56it/s]
 91%|#########1| Fitting GeneralizingEstimator : 32/35 [00:00<00:00,   74.24it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   77.35it/s]
100%|##########| Fitting GeneralizingEstimator : 35/35 [00:00<00:00,   75.07it/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 : 15/1225 [00:00<00:02,  435.32it/s]
  3%|2         | Scoring GeneralizingEstimator : 36/1225 [00:00<00:02,  529.25it/s]
  5%|4         | Scoring GeneralizingEstimator : 56/1225 [00:00<00:02,  550.92it/s]
  6%|6         | Scoring GeneralizingEstimator : 76/1225 [00:00<00:02,  561.73it/s]
  8%|7         | Scoring GeneralizingEstimator : 94/1225 [00:00<00:02,  555.31it/s]
  9%|9         | Scoring GeneralizingEstimator : 114/1225 [00:00<00:01,  561.85it/s]
 11%|#1        | Scoring GeneralizingEstimator : 135/1225 [00:00<00:01,  571.74it/s]
 13%|#2        | Scoring GeneralizingEstimator : 156/1225 [00:00<00:01,  579.11it/s]
 14%|#4        | Scoring GeneralizingEstimator : 177/1225 [00:00<00:01,  584.52it/s]
 16%|#6        | Scoring GeneralizingEstimator : 197/1225 [00:00<00:01,  585.47it/s]
 18%|#7        | Scoring GeneralizingEstimator : 218/1225 [00:00<00:01,  589.52it/s]
 20%|#9        | Scoring GeneralizingEstimator : 239/1225 [00:00<00:01,  592.77it/s]
 21%|##1       | Scoring GeneralizingEstimator : 261/1225 [00:00<00:01,  598.53it/s]
 23%|##3       | Scoring GeneralizingEstimator : 282/1225 [00:00<00:01,  600.81it/s]
 25%|##4       | Scoring GeneralizingEstimator : 302/1225 [00:00<00:01,  599.89it/s]
 26%|##6       | Scoring GeneralizingEstimator : 322/1225 [00:00<00:01,  598.87it/s]
 28%|##8       | Scoring GeneralizingEstimator : 343/1225 [00:00<00:01,  600.80it/s]
 30%|##9       | Scoring GeneralizingEstimator : 364/1225 [00:00<00:01,  602.61it/s]
 31%|###1      | Scoring GeneralizingEstimator : 385/1225 [00:00<00:01,  604.22it/s]
 33%|###3      | Scoring GeneralizingEstimator : 405/1225 [00:00<00:01,  602.64it/s]
 35%|###4      | Scoring GeneralizingEstimator : 427/1225 [00:00<00:01,  606.34it/s]
 37%|###6      | Scoring GeneralizingEstimator : 448/1225 [00:00<00:01,  607.41it/s]
 38%|###8      | Scoring GeneralizingEstimator : 470/1225 [00:00<00:01,  610.63it/s]
 40%|####      | Scoring GeneralizingEstimator : 491/1225 [00:00<00:01,  611.33it/s]
 42%|####1     | Scoring GeneralizingEstimator : 513/1225 [00:00<00:01,  614.03it/s]
 44%|####3     | Scoring GeneralizingEstimator : 534/1225 [00:00<00:01,  614.34it/s]
 45%|####5     | Scoring GeneralizingEstimator : 556/1225 [00:00<00:01,  616.38it/s]
 47%|####7     | Scoring GeneralizingEstimator : 578/1225 [00:00<00:01,  618.57it/s]
 49%|####8     | Scoring GeneralizingEstimator : 600/1225 [00:00<00:01,  620.74it/s]
 51%|#####     | Scoring GeneralizingEstimator : 622/1225 [00:01<00:00,  622.68it/s]
 52%|#####2    | Scoring GeneralizingEstimator : 643/1225 [00:01<00:00,  622.55it/s]
 54%|#####4    | Scoring GeneralizingEstimator : 665/1225 [00:01<00:00,  624.32it/s]
 56%|#####6    | Scoring GeneralizingEstimator : 686/1225 [00:01<00:00,  623.95it/s]
 58%|#####7    | Scoring GeneralizingEstimator : 708/1225 [00:01<00:00,  625.63it/s]
 60%|#####9    | Scoring GeneralizingEstimator : 729/1225 [00:01<00:00,  625.35it/s]
 61%|######1   | Scoring GeneralizingEstimator : 750/1225 [00:01<00:00,  624.97it/s]
 63%|######3   | Scoring GeneralizingEstimator : 772/1225 [00:01<00:00,  626.49it/s]
 65%|######4   | Scoring GeneralizingEstimator : 794/1225 [00:01<00:00,  627.99it/s]
 67%|######6   | Scoring GeneralizingEstimator : 816/1225 [00:01<00:00,  629.35it/s]
 68%|######8   | Scoring GeneralizingEstimator : 838/1225 [00:01<00:00,  630.59it/s]
 70%|#######   | Scoring GeneralizingEstimator : 859/1225 [00:01<00:00,  629.94it/s]
 72%|#######1  | Scoring GeneralizingEstimator : 881/1225 [00:01<00:00,  631.01it/s]
 74%|#######3  | Scoring GeneralizingEstimator : 902/1225 [00:01<00:00,  630.38it/s]
 75%|#######5  | Scoring GeneralizingEstimator : 924/1225 [00:01<00:00,  631.54it/s]
 77%|#######7  | Scoring GeneralizingEstimator : 946/1225 [00:01<00:00,  632.52it/s]
 79%|#######9  | Scoring GeneralizingEstimator : 968/1225 [00:01<00:00,  633.45it/s]
 81%|########  | Scoring GeneralizingEstimator : 989/1225 [00:01<00:00,  632.84it/s]
 83%|########2 | Scoring GeneralizingEstimator : 1011/1225 [00:01<00:00,  633.90it/s]
 84%|########4 | Scoring GeneralizingEstimator : 1033/1225 [00:01<00:00,  634.49it/s]
 86%|########6 | Scoring GeneralizingEstimator : 1055/1225 [00:01<00:00,  635.02it/s]
 88%|########7 | Scoring GeneralizingEstimator : 1076/1225 [00:01<00:00,  634.17it/s]
 90%|########9 | Scoring GeneralizingEstimator : 1098/1225 [00:01<00:00,  635.03it/s]
 91%|#########1| Scoring GeneralizingEstimator : 1119/1225 [00:01<00:00,  634.02it/s]
 93%|#########3| Scoring GeneralizingEstimator : 1141/1225 [00:01<00:00,  634.56it/s]
 95%|#########4| Scoring GeneralizingEstimator : 1163/1225 [00:01<00:00,  635.44it/s]
 97%|#########6| Scoring GeneralizingEstimator : 1185/1225 [00:01<00:00,  636.22it/s]
 98%|#########8| Scoring GeneralizingEstimator : 1206/1225 [00:01<00:00,  635.50it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:01<00:00,  636.78it/s]
100%|##########| Scoring GeneralizingEstimator : 1225/1225 [00:01<00:00,  625.10it/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.573 seconds)

Estimated memory usage: 128 MB

Gallery generated by Sphinx-Gallery

On this page