Skip to content

Instantly share code, notes, and snippets.

@kingjr
Last active August 29, 2015 14:07
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/a4ee6489057d1e86ddf1 to your computer and use it in GitHub Desktop.
Save kingjr/a4ee6489057d1e86ddf1 to your computer and use it in GitHub Desktop.
This example illustrate how a simple univariate analysis can be applied to performe a generalization across time analysis, and thus help the reader conceptualize what is at stake in the analysis
# Authors: Jean-Remi King <jeanremi.king@gmail.com>
"""
This example illustrate how a simple univariate analysis can be applied to
performe a generalization across time analysis, and thus help the reader
conceptualize what is at stake in the analysis"""
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline
from sklearn.feature_selection import SelectPercentile, f_regression
import mne
from mne.datasets import sample
from mne.decoding import GeneralizationAcrossTime, plot_decod, plot_gat
# --------------------------------------------------------------
# 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='iir') # Band pass filtering signals
events = mne.read_events(events_fname)
event_id = {'AudL': 1, 'AudR': 2, 'VisL': 3, 'VisR': 4}
decim = 3 # 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)
# Define events of interest
y_vis_audio = epochs.events[:, 2] <= 2
y_left_right = np.mod(epochs.events[:, 2], 2) == 1
# --------------------------------------------------------------
# 'CLASSIC' GAT
# --------------------------------------------------------------
gat = GeneralizationAcrossTime()
gat.fit(epochs, y=y_vis_audio)
gat.score(epochs, y=y_vis_audio)
plot_gat(gat) # plot full GAT matrix
# --------------------------------------------------------------
# Simplier 'univariate' equivalent:
# --------------------------------------------------------------
# Here I apply a correlation across sensors between the two averaged
# conditions. Note the diagonal artifact introduced by the lack of
# cross validation
from scipy.stats.stats import pearsonr
times = epochs.times
X = epochs.get_data()
X1 = np.mean(X[y_vis_audio, :, :], axis=0)
X2 = np.mean(X[-y_vis_audio, :, :], axis=0)
X_diff = X1 - X2
for t_train in range(len(times)):
for t_test in range(len(times)):
r_p = pearsonr(X_diff[:, t_train], X_diff[:, t_test])
gat.scores[t_train][t_test] = r_p[0]
plot_gat(gat, vmin=-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment