Skip to content

Instantly share code, notes, and snippets.

View kingjr's full-sized avatar

Jean-Rémi KING kingjr

View GitHub Profile
@kingjr
kingjr / plr_proba_classifier.py
Created June 5, 2015 13:20
ad hoc: Scaled Logistic Regression with probabilistic output
# ad hoc: Scaled Logistic Regression with probabilistic output
class force_predict(object):
def __init__(self, clf, mode='predict_proba', axis=0):
self._mode = mode
self._axis = axis
self._clf = clf
def fit(self, X, y, **kwargs):
self._clf.fit(X, y, **kwargs)
@kingjr
kingjr / TFCE_stats_gat.py
Created June 5, 2015 13:27
Example TFCE stats for GAT
import matplotlib.pyplot as plt
from mne.stats import spatio_temporal_cluster_1samp_test
# Gather all scores
scores = np.array([gat.scores_ for gat in gat_list])
gat_mean = copy.deepcopy(gat_list[0])
gat_mean.scores_ = np.mean(scores, axis=0)
# STATS
chance = 0.5 # chance level; if it's an AUC, it has to be .5
@kingjr
kingjr / decod_source.py
Created June 8, 2015 15:04
decoding_source
"""
=============================================================
Decoding source space data with ROI - sliding window approach
=============================================================
Decoding, a.k.a MVPA or supervised machine learning applied to MEG
data in source space on the left cortical surface. Here f-test feature
selection is employed to confine the classification to the potentially
relevant features. The classifier then is trained to selected features of
epochs in source space. A different classifier is trained i) in each
@kingjr
kingjr / gat_source.py
Created June 8, 2015 17:10
Example for Chris: Decoding source space data with ROI
"""
======================================================
Example for Chris: Decoding source space data with ROI
======================================================
"""
# Author: Jean-Remi King <jeanremi.king@gmail.com
#
# License: BSD-Simplified
@kingjr
kingjr / Large_sliding_window.py
Last active August 29, 2015 14:23
Compare Riemann to classic in a sliding window context
import numpy as np
import mne
from mne import io
from mne.datasets import sample
data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
raw = io.Raw(raw_fname, preload=True)
raw.filter(1, 20, method='iir')
events = mne.read_events(event_fname)
@kingjr
kingjr / test_auc_bias.py
Created June 18, 2015 20:51
AUC underestimation: check whether empirical ROC AUC leads to underestimates
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_auc_score, roc_curve
def roc(n=1e4, bit=1.):
X = np.random.randn(n)
y = np.zeros(n)
y[:n/2] = 1
X[y==1] += bit
@kingjr
kingjr / example_parallel_pairwise.py
Created June 19, 2015 00:21
To perform parallel operation on pairwise matrices
def pairwise(X, Y, func, n_jobs=-1):
from mne.parallel import parallel_func
if X.shape != Y.shape:
raise ValueError('X and Y must have identical shapes')
parallel, p_pairwise, n_jobs = parallel_func(_pairwise, n_jobs)
dims = X.shape
X = np.reshape(X, [dims[0], np.prod(dims[1:])])
Y = np.reshape(Y, [dims[0], np.prod(dims[1:])])
n_cols = X.shape[1]
n_chunks = min(n_cols, n_jobs)
@kingjr
kingjr / share_clim.py
Created June 20, 2015 12:33
Share color limits across axes
def share_clim(axes, clim=None):
"""Share clim across multiple axes
Parameters
----------
axes : plt.axes
clim : np.array | list, shape(2,), optional
Defaults is min and max across axes.clim.
"""
# Find min max of clims
if clim is None:
@kingjr
kingjr / subscore_taj.py
Last active August 29, 2015 14:25
subscore TAJ
import numpy as np
import mne
from mne.decoding import GeneralizationAcrossTime as GAT
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.cross_validation import StratifiedKFold
from meeg_preprocessing.utils import setup_provenance
import numpy as np
import mne
from mne.decoding import GeneralizationAcrossTime as GAT
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.cross_validation import StratifiedKFold
from meeg_preprocessing.utils import setup_provenance