Last active
August 29, 2015 14:25
-
-
Save kingjr/e53e36a3f3c5486d2fb3 to your computer and use it in GitHub Desktop.
subscore TAJ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| report, run_id, results_dir, logger = setup_provenance( | |
| __file__, results_dir='results') | |
| conditions = ['LSGS', 'LSGD', 'LDGS', 'LDGD'] | |
| local_cond, global_cond = [ | |
| [['LSGS', 'LSGD'], ['LDGS', 'LDGD']], | |
| [['LSGS', 'LDGS'], ['LSGD', 'LDGD']]] | |
| 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) | |
| self._copyattr() | |
| def predict(self, X): | |
| if self._mode == 'predict_proba': | |
| return self._clf.predict_proba(X)[:, self._axis] | |
| elif self._mode == 'decision_function': | |
| distances = self._clf.decision_function(X) | |
| if len(distances.shape) > 1: | |
| return distances[:, self._axis] | |
| else: | |
| return distances | |
| else: | |
| return self._clf.predict(X) | |
| def get_params(self, deep=True): | |
| return dict(clf=self._clf, mode=self._mode, axis=self._axis) | |
| def _copyattr(self): | |
| for key, value in self._clf.__dict__.iteritems(): | |
| self.__setattr__(key, value) | |
| class force_weight(object): | |
| def __init__(self, clf, weights=None): | |
| self._clf = clf | |
| def fit(self, X, y): | |
| return self._clf.fit(X, np.array(y[:, 0], dtype=int), | |
| sample_weight=np.array(y[:, 1])) | |
| def predict(self, X): | |
| return self._clf.predict(X) | |
| def get_params(self, deep=True): | |
| return dict(clf=self._clf) | |
| # weighted probablistic linear classifier | |
| clf = make_pipeline(StandardScaler(), | |
| force_predict(force_weight(SVC( | |
| kernel='linear', probability=True)), axis=1)) | |
| results = list() | |
| for subject_name in ['TAJ20081223']: | |
| # Preproc | |
| epochs = mne.read_epochs('TAJ-epo.fif') | |
| this_epochs = epochs[conditions].crop(0.6, None) | |
| # Contrast definitions | |
| event_id = {v: k for k, v in this_epochs.event_id.items()} | |
| y_raw = [event_id[k] for k in this_epochs.events[:, 2]] | |
| sample_weight = [1. / y_raw.count(k) for k in y_raw] | |
| y_local = [int(v in local_cond[1]) for v in y_raw] | |
| y_global = [int(v in global_cond[1]) for v in y_raw] | |
| iter_contrast = [[y_local, y_global], ['local', 'global']] | |
| # from mne.decoding.time_gen import TimeDecoding | |
| # td = TimeDecoding(scorer=roc_auc_score, clf=clf, n_jobs=-1) | |
| # td.fit(this_epochs, y=y_local) | |
| # td.score(this_epochs, y=y_local) | |
| # td.plot | |
| # GAT | |
| cv = StratifiedKFold(y=y_raw, n_folds=5) # ensure full stratification | |
| gat = GAT(scorer=roc_auc_score, clf=clf, n_jobs=-1, cv=cv) | |
| gat.train_times = dict(step=.024) # = decim | |
| gat.test_times = dict(step=.024) | |
| for y_fit, names_fit in zip(*iter_contrast): | |
| gat.fit(this_epochs, y=np.c_[y_fit, sample_weight]) | |
| gat.predict(this_epochs) | |
| for y_score, names_score in zip(*iter_contrast): | |
| scores = gat.score(y=y_score) | |
| results.append({'from': names_fit, 'to': names_score, | |
| 'out': scores, 'subject': subject_name}) | |
| fig = gat.plot(show=False) | |
| report.add_figs_to_section( | |
| fig, 'fit: %s, score: %s' % (names_fit, names_score), subject_name) | |
| for condition in conditions: | |
| sel = [int(v in condition) for v in y_raw] | |
| y_pred = np.mean(np.array(gat.y_pred_)[:, :, sel, :], axis=2) | |
| results.append({'from': names_fit, 'to': condition, | |
| 'out': y_pred, 'subject': subject_name}) | |
| report.save() | |
| # ANOVA |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dengemann