def get_scores_from_gat(epochs, seed): from sklearn.svm import LinearSVC X_train, X_test, y_train, y_test = train_test_split(epochs.get_data(), epochs.events[:, 2] == 2, test_size=0.2, random_state=seed) clf = make_pipeline(StandardScaler(), LinearSVC(random_state=0, tol=1e-5, penalty='l2')) # clf = time_gen = GeneralizingEstimator(clf, scoring='roc_auc', n_jobs=-2) time_gen.fit(X_train, y_train) scores = time_gen.score(X_test, y_test) print( 'Units with highest weights of a classifier trained to predict subject' 's number:') print([(i, j) for (i, j) in zip( np.transpose( np.argsort( np.negative( np.abs(time_gen.estimators_[1]._final_estimator.coef_)))) [0:20], np.transpose( np.sort( np.negative( np.abs(time_gen.estimators_[1]._final_estimator.coef_)))) [0:20])]) return time_gen, scores
def train_test(X_train, y_train, X_test, y_test, clf=clf, scoring=scoring, n_jobs=n_jobs): # train and test time_gen = GeneralizingEstimator(clf, scoring=scoring, n_jobs=n_jobs) time_gen.fit(X=X_train, y=y_train) return time_gen.score(X=X_test, y=y_test)
############################################################################### # We will train the classifier on all left visual vs auditory trials # and test on all right visual vs auditory trials. clf = make_pipeline(StandardScaler(), LogisticRegression(solver='lbfgs')) time_gen = GeneralizingEstimator(clf, scoring='roc_auc', n_jobs=1, verbose=True) # Fit classifiers on the epochs where the stimulus was presented to the left. # Note that the experimental condition y indicates auditory or visual time_gen.fit(X=epochs['Left'].get_data(), y=epochs['Left'].events[:, 2] > 2) ############################################################################### # Score on the epochs where the stimulus was presented to the right. scores = time_gen.score(X=epochs['Right'].get_data(), y=epochs['Right'].events[:, 2] > 2) ############################################################################### # Plot fig, ax = plt.subplots(1) im = ax.matshow(scores, vmin=0, vmax=1., cmap='RdBu_r', origin='lower', extent=epochs.times[[0, -1, 0, -1]]) ax.axhline(0., color='k') ax.axvline(0., color='k') ax.xaxis.set_ticks_position('bottom') ax.set_xlabel('Testing Time (s)') ax.set_ylabel('Training Time (s)')
tmax=tmax, preload=True, baseline=(None, 0), decim=10) epochs.pick_types(meg=True, ref_meg=False) # Loop across analysis for analysis in analyses: fname = results_folder +\ '%s_scores_%s_%s.npy' % (subject, 'Cue', analysis) # define to-be-predicted values y = np.array(events_behavior[analysis]) clf = make_pipeline(StandardScaler(), LinearModel(LogisticRegression())) kwargs = dict() le = LabelEncoder() le.fit(y) y = le.transform(y) sel = np.where(y != 0)[0] # Run decoding cv = StratifiedKFold(12) scores = list() X = epochs._data gat = GeneralizingEstimator(clf, scoring='roc_auc', n_jobs=24, **kwargs) for train, test in cv.split(X[sel], y[sel]): gat.fit(X[sel][train], y[sel][train]) score = gat.score(X[sel][test], y[sel][test]) scores.append(score) scores = np.mean(scores, axis=0) # keep scores np.save(fname, np.array(scores))
############################################################################### # We will train the classifier on all left visual vs auditory trials # and test on all right visual vs auditory trials. clf = make_pipeline(StandardScaler(), LogisticRegression()) time_gen = GeneralizingEstimator(clf, scoring='roc_auc', n_jobs=1, verbose=True) # Fit classifiers on the epochs where the stimulus was presented to the left. # Note that the experimental condition y indicates auditory or visual time_gen.fit(X=epochs['Left'].get_data(), y=epochs['Left'].events[:, 2] > 2) ############################################################################### # Score on the epochs where the stimulus was presented to the right. scores = time_gen.score(X=epochs['Right'].get_data(), y=epochs['Right'].events[:, 2] > 2) ############################################################################### # Plot fig, ax = plt.subplots(1) im = ax.matshow(scores, vmin=0, vmax=1., cmap='RdBu_r', origin='lower', extent=epochs.times[[0, -1, 0, -1]]) ax.axhline(0., color='k') ax.axvline(0., color='k') ax.xaxis.set_ticks_position('bottom') ax.set_xlabel('Testing Time (s)') ax.set_ylabel('Training Time (s)') ax.set_title('Generalization across time and condition') plt.colorbar(im, ax=ax) plt.show()
n_jobs=24, **kwargs) y = np.array(y, dtype=float) # only consider non NaN values if ('cue_side' in analysis or 'cue_type' in analysis): sel = np.where(y != 0)[0] else: sel = np.where(~np.isnan(y))[0] # Run decoding cv = StratifiedKFold(12) scores = list() patterns = list() filters = list() for train, test in cv.split(X[sel], y[sel]): clf.fit(X[sel][train], y[sel][train]) score = clf.score(X[sel][test], y[sel][test]) scores.append(score) patterns.append(get_coef(clf, 'patterns_', inverse_transform=True)) filters.append(get_coef(clf, 'filters_', inverse_transform=True)) scores = np.mean(scores, axis=0) patterns = np.mean(patterns, axis=0) filters = np.mean(filters, axis=0) # save cross-validated scores, patterns and filters fname = results_folder +\ '%s_scores_%s_%s.npy' % (subject, epoch_type, analysis) np.save(fname, np.array(scores)) fname = results_folder +\ '%s_patterns_%s_%s.npy' % (subject, epoch_type, analysis) np.save(fname, np.array(patterns)) fname = results_folder +\ '%s_filters_%s_%s.npy' % (subject, epoch_type, analysis)
X2, y2 = get_Xy_balanced(sleep_epochs, contrast2, n_sample=nsample) X3, y3 = get_Xy_balanced(sleep_epochs, contrast3, n_sample=nsample) X4, y4 = get_Xy_balanced(sleep_epochs, contrast4, n_sample=nsample) X5, y5 = get_Xy_balanced(sleep_epochs, contrast5, n_sample=nsample) del wake_epochs del sleep_epochs clf = GeneralizingEstimator(make_pipeline( StandardScaler(), LogisticRegression(max_iter=4000)), scoring='accuracy', n_jobs=6) # clf = GeneralizingEstimator(make_pipeline(StandardScaler(), SVC(kernel='linear')), # scoring='accuracy', n_jobs=6) cv = StratifiedKFold(n_splits=2, shuffle=True) scores1, scores2, scores3, scores4, scores5 = [[] for i in range(5)] for train_idx, test_idx in cv.split(X1, y1): clf.fit(X1[train_idx], y=y1[train_idx]) scores1.append(clf.score(X1[test_idx], y=y1[test_idx])) scores2.append(clf.score(X2, y=y2)) scores3.append(clf.score(X3, y=y3)) scores4.append(clf.score(X4, y=y4)) scores5.append(clf.score(X5, y=y5)) results = [scores1, scores2, scores3, scores4, scores5] results = [results[i] for i in saveorder] pickle_save(os.path.join(save_path, sbj + '.p'), results)
time_gen = GeneralizingEstimator(clf, n_jobs=1, scoring='roc_auc') # # We will train the classifier on all stim face vs house trials # and test on all images face vs house trials. #le = LabelEncoder() # train on stim time_gen.fit(X=np.array(mean_train1 + mean_train2), y=np.array([0] * len(mean_train1) + [1] * len(mean_train2))) # score on imagery scores = time_gen.score(X=np.array(mean_test1 + mean_test2), y=np.array([0] * len(mean_test1) + [1] * len(mean_test2))) # let's save the scores now fname_td = os.path.join( results_path, '%s-causal-highpass-2Hz-temp-gene-across-conditions-stim_vs_imag-ave4trials.mat' % (subject)) savemat(fname_td, { 'scores': scores, 'times': epochs.times }) #, 'perm_scores': permutation_scores, 'pval' : pvalue}) # Here we go parallel inside the :class:`mne.decoding.SlidingEstimator` # so we don't dispatch manually to multiple jobs.
decim = 2 epochs.decimate(decim) # We will train the classifier on all stim face vs house trials # and test on all images face vs house trials. clf = make_pipeline(StandardScaler(), LogisticRegression()) time_gen = GeneralizingEstimator(clf, scoring='roc_auc', n_jobs=6) le = LabelEncoder() # train on stim time_gen.fit(X=epochs['stim'].get_data(), y=le.fit_transform(epochs['stim'].events[:, 2])) # score on imagery scores = time_gen.score(X=epochs['imag'].get_data(), y=le.fit_transform(epochs['imag'].events[:, 2])) # Plot fig, ax = plt.subplots(1) im = ax.matshow(scores, vmin=0, vmax=1., cmap='RdBu_r', origin='lower', extent=epochs.times[[0, -1, 0, -1]]) ax.axhline(0., color='k') ax.axvline(0., color='k') ax.xaxis.set_ticks_position('bottom') ax.set_xlabel('Testing Time (s)') ax.set_ylabel('Training Time (s)') ax.set_title('Generalization across time and condition')
# only consider trials with correct fixation sel = np.where(events['is_eye_fixed'] == 1)[0] y_train = y_train[sel] y_test = y_test[sel] X = np.concatenate((X0, X1, X2), axis=2) X = X[sel] # only consider non NaN values # Run decoding accross condition cv = StratifiedKFold(7) scores = list() scs = list() if np.isnan(y_train).any(): sel = np.where(~np.isnan(y_train))[0] for train, test in cv.split(X[sel], y_train[sel]): gat.fit(X[sel][train], y_train[sel][train]) score = gat.score(X[sel][test], y_test[sel][test]) sc = gat.score(X[sel][test], y_train[sel][test]) # test on same scores.append(score) scs.append(sc) scores = np.mean(scores, axis=0) scs = np.mean(scs, axis=0) else: for train, test in cv.split(X, y_train): y_te = y_test[test] X_te = X[test] y_te = y_te[np.where(~np.isnan(y_te))[0]] X_te = X_te[np.where(~np.isnan(y_te))[0]] y_tr = y_train[train] X_tr = X[train] y_tr = y_tr[np.where(~np.isnan(y_tr))[0]] X_tr = X_tr[np.where(~np.isnan(y_tr))[0]]
X = epochs._data[sel] le = LabelEncoder() le.fit(y_con) y_con = le.transform(y_con) sel = np.where(y_con != 0)[0] y_con = y_con[sel] X_con = epochs_con._data[sel] # Define estimators depending on the analysis clf = make_pipeline(StandardScaler(), LinearModel(LogisticRegression())) kwargs = dict() est = GeneralizingEstimator(clf, scoring='roc_auc', n_jobs=24, **kwargs) # Run decoding cv = StratifiedKFold(12) scores = list() scores_con = list() for train, test in cv.split(X, y): est.fit(X[train], y[train]) # train during WM task score = est.score(X[test], y[test]) # test during WM task score_con = est.score(X_con, y_con) # test during control task scores.append(score) scores_con.append(score_con) scores = np.mean(scores, axis=0) scores_con = np.mean(scores_con, axis=0) # save cross-validated scores fname = results_folder +\ '%s_scores_%s.npy' % (subject, analysis) np.save(fname, np.array(scores)) fname = results_folder +\ '%s_scores_%s_con.npy' % (subject, analysis) np.save(fname, np.array(scores_con))
assert all(Y > 0) print(list(Y).count(1), list(Y).count(2)) # clf = make_pipeline(StandardScaler(), LinearSVC(class_weight='balanced')) clf = make_pipeline(LinearSVC(class_weight='balanced')) time_gen = GeneralizingEstimator(clf, scoring='roc_auc', n_jobs=-1, verbose=True) cv = StratifiedKFold(n_splits=5, random_state=0, shuffle=True) #cv = StratifiedShuffleSplit(n_splits=20, random_state=0) scores = [] for i, (train, test) in enumerate(cv.split(X, Y)): time_gen.fit(X[train], Y[train]) scores.append(time_gen.score(X[test], Y[test])) # list (len=#cv-splits) of sublists (len=#timepoints): curr_weights_clf = np.asarray([ np.squeeze(w._final_estimator.coef_) for w in time_gen.estimators_ ]) weights_clf[v + 1]['splits'].append(curr_weights_clf) weights_clf[v + 1]['mean'] = np.mean(np.asarray(weights_clf[v + 1]['splits']), axis=0) weights_clf[v + 1]['std'] = np.std(np.asarray(weights_clf[v + 1]['splits']), axis=0) ######## # PLOT #