def analysis(name, typ, condition=None, query=None, title=None): """Wrapper to ensure that we attribute the same function for each type of analyses: e.g. categorical, regression, circular regression.""" # Define univariate analysis erf_function = None # Default is fast_mannwhitneyu # /!\ for categorical analyses, the contrast is min(y) - max(y) # e.g. target_present==False - target_present==True if typ == 'categorize': # estimator is normalization + l2 Logistic Regression clf = make_pipeline( StandardScaler(), force_predict(LogisticRegression(class_weight='balanced'), axis=1)) scorer = scorer_auc chance = .5 elif typ == 'regress': # estimator is normalization + l2 Ridge clf = make_pipeline(StandardScaler(), Ridge()) scorer = scorer_spearman chance = 0. elif typ == 'circ_regress': # estimator is normalization + l2 Logistic Regression on cos and sin clf = make_pipeline(StandardScaler(), PolarRegression(Ridge())) scorer = scorer_angle chance = 0. # The univariate analysis needs a different scorer erf_function = scorer_circlin if condition is None: condition = name return dict(name=name, condition=condition, query=query, clf=clf, scorer=scorer, chance=chance, erf_function=erf_function, cv=8, typ=typ, title=title, single_trial=True)
def quick_score(X, y, clf=None, scorer=None): from sklearn.cross_validation import KFold regression = (len(np.unique(y)) > 2) & isinstance(y[0], float) if scorer is None: scorer = scorer_spearman if regression else scorer_auc if clf is None: clf = RidgeCV(alphas=[(2 * C) ** -1 for C in [1e-4, 1e-2, 1]])\ if regression else force_predict(LogisticRegression(), axis=1) sel = np.where(~np.isnan(y))[0] X = X[sel, :, :] y = y[sel] epochs = mat2mne(X, sfreq=100) clf = make_pipeline(StandardScaler(), clf) cv = KFold(len(y), 5) if regression else None gat = GeneralizationAcrossTime(clf=clf, n_jobs=-1, scorer=scorer, cv=cv) gat.fit(epochs, y) gat.score(epochs, y) return gat
# Compute time frequency decomposition X = mne.time_frequency.tfr_array_morlet(epochs.get_data(), sfreq=epochs.info['sfreq'], freqs=freqs, output='power', n_cycles=n_cycles) n_epochs, n_channels, n_freqs, n_times = X.shape X = X.reshape(n_epochs, n_channels, -1) # collapse freqs and time # Run decoding on TFR output for analysis in analyses: fname = results_folder +\ '%s_tf_scores_%s_%s.npy' % (subject, 'Cue', analysis) y = np.array(events_behavior[analysis]) clf = make_pipeline( StandardScaler(), force_predict(LogisticRegression(), 'predict_proba', axis=1)) scorer = scorer_auc kwargs = dict() le = preprocessing.LabelEncoder() le.fit(y) y = le.transform(y) sel = np.where(y != 0)[0] td = SlidingEstimator(clf, scoring=make_scorer(scorer), n_jobs=24, **kwargs) td.fit(X[sel], y[sel]) scores = cross_val_multiscore(td, X[sel], y[sel], cv=StratifiedKFold(12)) scores = scores.mean(axis=0) scores = np.reshape(scores, (n_freqs, n_times)) # Save cross validated scores
np.random.RandomState(1) order = np.arange(len(epochs)) np.random.shuffle(order) epochs = epochs[order] # decimate data decim = 2 epochs.decimate(decim) # only keep data > 0 ms & < 400 ms epochs.crop(-.100, .750) ###################################################################### # Decoding # set up a classifier based on a regularized Logistic Regression clf = LogisticRegression(C=1) # force the classifer to output a probabilistic prediction clf = force_predict(clf, axis=1) # insert a z-score normalization step before the classification clf = make_pipeline(StandardScaler(), clf) # initialize the GAT object gat = GeneralizationAcrossTime(clf=clf, scorer=scorer_auc, n_jobs=-1, cv=10) # select the trials where a target is presented for contrast in ['HL', 'EU', 'PR']: epochs_ = concatenate_epochs((epochs[contrast[0]], epochs[contrast[1]])) y = np.hstack((np.zeros(len(epochs[contrast[0]])), np.ones(len(epochs[contrast[1]])))) gat.fit(epochs_, y=y) fname = op.join(data_path, 's%i_%s_fit.pkl' % (subject, contrast)) with open(fname, 'wb') as f: