Skip to content

Instantly share code, notes, and snippets.

@kingjr
Created March 31, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingjr/6a4cbfb51a5d7da6850a to your computer and use it in GitHub Desktop.
Save kingjr/6a4cbfb51a5d7da6850a to your computer and use it in GitHub Desktop.
# Authors: Jean-Remi King <jeanremi.king@gmail.com>
#
# License: BSD (3-clause)
""" This aims at showing how the MNE GAT object can be used to score different
subsets of data"""
import copy
import numpy as np
import mne
from mne.datasets import sample
from mne.decoding import GeneralizationAcrossTime
# Preprocess data
data_path = sample.data_path()
# Load and filter data, set up epochs
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
events_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
raw = mne.io.Raw(raw_fname, preload=True)
picks = mne.pick_types(raw.info, meg=True, exclude='bads') # Pick MEG channels
raw.filter(1, 30, method='fft') # Band pass filtering signals
events = mne.read_events(events_fname)
event_id = {'AudL': 1, 'AudR': 2, 'VisL': 3, 'VisR': 4}
decim = 2 # decimate to make the example faster to run
epochs = mne.Epochs(raw, events, event_id, -0.050, 0.400, proj=True,
picks=picks, baseline=None, preload=True,
reject=dict(mag=5e-12), decim=decim, verbose=False)
# We will train the classifier on all left visual vs auditory trials
# and test on all right visual vs auditory trials.
# In this case, because the test data is independent from the train data,
# we test the classifier of each fold and average the respective predictions.
# Define events of interest
triggers = epochs.events[:, 2]
viz_vs_auditory = np.in1d(triggers, (1, 2)).astype(int)
gat = GeneralizationAcrossTime(n_jobs=-1)
# fit on both left and right stim
viz_vs_auditory = 1.0 + (epochs.events[:, 2] > 2)
gat.fit(epochs, y=viz_vs_auditory)
gat.predict(epochs)
gat.score(y=viz_vs_auditory)
gat.plot()
# score left
right_left = epochs.events[:, 2] % 2
gat_ = copy.deepcopy(gat)
gat_.y_pred_ = gat.y_pred_[:, :, right_left == 0]
gat_.score(y=viz_vs_auditory[right_left == 0])
gat_.plot()
# score right
gat_ = copy.deepcopy(gat)
gat_.y_pred_ = gat.y_pred_[:, :, right_left == 1]
gat_.score(y=viz_vs_auditory[right_left == 1])
gat_.plot()
@kingjr
Copy link
Author

kingjr commented Mar 31, 2015

@chri4354 here's an example on how to get score on subsets of data.

you have to wait until this mne-tools/mne-python#1915 has been merged, and then update your mne directory:

cd ~/path/to/mne-ptyhon
git pull https://github.com/mne-tools/mne-python 

@kingjr
Copy link
Author

kingjr commented Mar 31, 2015

@cmoutard This example may be relevant to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment