def score_ensemble_org(settings, subject_target, ntop): dataset = settings['dataset'] paradigm = settings['paradigm'] session = settings['session'] storage = settings['storage'] filepath = '../results/' + dataset + '/TL_intra-subject_scores.pkl' acc_intra_dict = joblib.load(filepath) scores = [] subject_sources = [] for subject in settings['subject_list']: if subject == subject_target: continue else: scores.append(acc_intra_dict[subject]) subject_sources.append(subject) scores = np.array(scores) subject_sources = np.array(subject_sources) idx_sort = scores.argsort()[::-1] scores = scores[idx_sort] subject_sources = subject_sources[idx_sort] subject_sources_ntop = subject_sources[:ntop] # get the geometric means for each subject (each class and also the center) filename = '../results/' + dataset + '/subject_means.pkl' subj_means = joblib.load(filename) # get the data for the target subject target_org = GD.get_dataset(dataset, subject_target, session, storage) if paradigm == 'MI': # things here are only implemented for MI for now target_org['covs'] = Covariances(estimator='oas').fit_transform( target_org['signals']) target_org['labels'] = target_org['labels'] ncovs = settings['ncovs_list'][0] nrzt = 10 score_rzt = 0.0 for rzt in range(nrzt): # split randomly the target dataset target_org_train, target_org_test = get_target_split_motorimagery( target_org, ncovs) covs_train_target = target_org_train['covs'] labs_train_target = target_org_train['labels'] M1_target = mean_riemann( covs_train_target[labs_train_target == 'left_hand']) M2_target = mean_riemann( covs_train_target[labs_train_target == 'right_hand']) covs_train_target = np.stack([M1_target, M2_target]) labs_train_target = np.array(['left_hand', 'right_hand']) clf = [] for subj_source in subject_sources_ntop: M1_source = subj_means[subj_source]['left_hand'] M2_source = subj_means[subj_source]['right_hand'] covs_train_source = np.stack([M1_source, M2_source]) labs_train_source = np.array(['left_hand', 'right_hand']) covs_train = np.concatenate([covs_train_source, covs_train_target]) labs_train = np.concatenate([labs_train_source, labs_train_target]) clfi = MDM() # problems here when using integer instead of floats on the sample_weight clfi.fit(covs_train, labs_train, sample_weight=np.array( [200.0, 200.0, 2.0 * ncovs, 2.0 * ncovs])) clf.append(clfi) covs_test = target_org_test['covs'] labs_test = target_org_test['labels'] ypred = [] for clfi in clf: yi = clfi.predict(covs_test) ypred.append(yi) ypred = np.array(ypred) majorvoting = [] for j in range(ypred.shape[1]): ypredj = ypred[:, j] values_unique, values_count = np.unique(ypredj, return_counts=True) majorvoting.append(values_unique[np.argmax(values_count)]) majorvoting = np.array(majorvoting) score_rzt = score_rzt + np.mean(majorvoting == labs_test) score = score_rzt / nrzt return score
from tqdm import tqdm from collections import OrderedDict # get the functions from RPA package import rpa.transfer_learning as TL import rpa.diffusion_map as DM import rpa.get_dataset as GD # get the dataset datafolder = '../datasets/' paradigm = 'motorimagery' target = GD.get_dataset(datafolder, paradigm, subject=1) source = GD.get_dataset(datafolder, paradigm, subject=2) # instantiate the Riemannian classifier to use clf = MDM() # create a scores dictionary methods_list = ['org', 'rct', 'rpa', 'clb'] scores = OrderedDict() for method in methods_list: scores[method] = [] nrzt = 5 for _ in tqdm(range(nrzt)): # get the split for the source and target dataset source_org, target_org_train, target_org_test = TL.get_sourcetarget_split(source, target, ncovs_train=10) # get the score with the original dataset scores['org'].append(TL.get_score_transferlearning(clf, source_org, target_org_train, target_org_test))
def score_pooling_rot(settings, subject_target, ntop): dataset = settings['dataset'] paradigm = settings['paradigm'] session = settings['session'] storage = settings['storage'] filepath = '../results/' + dataset + '/TL_intra-subject_scores.pkl' acc_intra_dict = joblib.load(filepath) scores = [] subject_sources = [] for subject in settings['subject_list']: if subject == subject_target: continue else: scores.append(acc_intra_dict[subject]) subject_sources.append(subject) scores = np.array(scores) subject_sources = np.array(subject_sources) idx_sort = scores.argsort()[::-1] scores = scores[idx_sort] subject_sources = subject_sources[idx_sort] subject_sources_ntop = subject_sources[:ntop] # get the geometric means for each subject (each class and also the center) filename = '../results/' + dataset + '/subject_means.pkl' subj_means = joblib.load(filename) # get the data for the target subject target_org = GD.get_dataset(dataset, subject_target, session, storage) if paradigm == 'MI': # things here are only implemented for MI for now target_org['covs'] = Covariances(estimator='oas').fit_transform( target_org['signals']) target_org['labels'] = target_org['labels'] ncovs = settings['ncovs_list'][0] score_rzt = 0.0 nrzt = 10 for rzt in range(nrzt): # split randomly the target dataset target_org_train, target_org_test = get_target_split_motorimagery( target_org, ncovs) # get the data from the sources and pool it all together class_mean_1 = [] class_mean_2 = [] for subj_source in subject_sources_ntop: MC_source = subj_means[subj_source]['center'] M1_source = subj_means[subj_source]['left_hand'] M2_source = subj_means[subj_source]['right_hand'] M1_source_rct = np.dot(invsqrtm(MC_source), np.dot(M1_source, invsqrtm(MC_source))) class_mean_1.append(M1_source_rct) M2_source_rct = np.dot(invsqrtm(MC_source), np.dot(M2_source, invsqrtm(MC_source))) class_mean_2.append(M2_source_rct) class_mean_1_source = np.stack(class_mean_1) class_mean_2_source = np.stack(class_mean_2) covs_train_source = np.concatenate( [class_mean_1_source, class_mean_2_source]) labs_train_source = np.concatenate([ len(class_mean_1_source) * ['left_hand'], len(class_mean_2_source) * ['right_hand'] ]) # re-center data for the target covs_train_target = target_org['covs'] MC_target = mean_riemann(covs_train_target) labs_train_target = target_org['labels'] class_mean_1_target = mean_riemann( covs_train_target[labs_train_target == 'left_hand']) class_mean_1_target_rct = np.dot( invsqrtm(MC_target), np.dot(class_mean_1_target, invsqrtm(MC_target))) class_mean_2_target = mean_riemann( covs_train_target[labs_train_target == 'right_hand']) class_mean_2_target_rct = np.dot( invsqrtm(MC_target), np.dot(class_mean_2_target, invsqrtm(MC_target))) # rotate the source matrices for each subject with respect to the target class_mean_1_source_rot = [] class_mean_2_source_rot = [] for Mi1, Mi2 in zip(class_mean_1_source, class_mean_2_source): M = [class_mean_1_target_rct, class_mean_2_target_rct] Mtilde = [Mi1, Mi2] U = manifoptim.get_rotation_matrix(M, Mtilde) Mi1_rot = np.dot(U, np.dot(Mi1, U.T)) class_mean_1_source_rot.append(Mi1_rot) Mi2_rot = np.dot(U, np.dot(Mi2, U.T)) class_mean_2_source_rot.append(Mi2_rot) class_mean_1_source = np.stack(class_mean_1_source_rot) class_mean_2_source = np.stack(class_mean_2_source_rot) covs_train_source = np.concatenate( [class_mean_1_source, class_mean_2_source]) covs_train_target = np.stack( [class_mean_1_target_rct, class_mean_2_target_rct]) labs_train_target = np.array(['left_hand', 'right_hand']) covs_train = np.concatenate([covs_train_source, covs_train_target]) labs_train = np.concatenate([labs_train_source, labs_train_target]) covs_test = target_org_test['covs'] labs_test = target_org_test['labels'] # do the classification clf = MDM() clf.fit(covs_train, labs_train) score_rzt = score_rzt + clf.score(covs_test, labs_test) score = score_rzt / nrzt return score
epochs = Epochs( raw, events, event_ids, tmin, tmin + wl, proj=True, picks=picks, preload=True, baseline=None, verbose=False, ) X = epochs.get_data() y = np.array([0 if ev == 2 else 1 for ev in epochs.events[:, -1]]) for est in estimators: clf = make_pipeline(Covariances(estimator=est), MDM()) try: score = cross_val_score(clf, X, y, cv=cv, scoring=sc) dfa += [dict(estimator=est, wlen=wl, accuracy=sc) for sc in score] except ValueError: print(f"{est}: {wl} is not sufficent to estimate a SPD matrix") dfa += [dict(estimator=est, wlen=wl, accuracy=np.nan)] * n_splits dfa = pd.DataFrame(dfa) ############################################################################### fig, ax = plt.subplots(figsize=(6, 4)) sns.lineplot( data=dfa, x="wlen", y="accuracy",
import matplotlib.pyplot as plt from collections import OrderedDict from moabb.datasets.bnci import BNCI2014001 from moabb.datasets.alex_mi import AlexMI from moabb.datasets.physionet_mi import PhysionetMI datasets = [ AlexMI(with_rest=True), BNCI2014001(), PhysionetMI(with_rest=True, feets=False) ] pipelines = OrderedDict() pipelines['MDM'] = make_pipeline(Covariances('oas'), MDM()) pipelines['TS'] = make_pipeline(Covariances('oas'), TSclassifier()) pipelines['CSP+LDA'] = make_pipeline(Covariances('oas'), CSP(8), LDA()) context = MotorImageryMultiClasses(datasets=datasets, pipelines=pipelines) results = context.evaluate(verbose=True) for p in results.keys(): results[p].to_csv('../../results/MotorImagery/MultiClass/%s.csv' % p) results = pd.concat(results.values()) print(results.groupby('Pipeline').mean()) res = results.pivot(values='Score', columns='Pipeline') sns.lmplot(data=res, x='CSP+LDA', y='TS', fit_reg=False)
TSC_record = [] CSP_lr_record = [] CSP_svm_record = [] for fold in range(1, 6): train = cov_data_bad[index[bad_subject_index] != fold] train_CSP = epochs_data_train_bad[index[bad_subject_index] != fold] train_label = labels_bad[index[bad_subject_index] != fold] test = cov_data_bad[index[bad_subject_index] == fold] test_CSP = epochs_data_train_bad[index[bad_subject_index] == fold] test_label = labels_bad[index[bad_subject_index] == fold] box_length = np.sum([index[bad_subject_index] == fold]) mdm = MDM(metric=dict(mean='riemann', distance='riemann')) mdm.fit(train, train_label) pred = mdm.predict(test) print('MDM: {:4f}'.format(np.sum(pred == test_label) / box_length)) MDM_record.append(np.sum(pred == test_label) / box_length) print('-----------------------------------------') Fgmdm = FgMDM(metric=dict(mean='riemann', distance='riemann')) Fgmdm.fit(train, train_label) pred = Fgmdm.predict(test) print('FGMDM: {:4f}'.format( np.sum(pred == test_label) / box_length))
# avoid classification of evoked responses by using epochs that start 1s after # cue onset. tmin, tmax = 1., 2. event_id = dict(hands=2, feet=3) subjects = range(1, 110) # There is subject where MNE can read the file subject_to_remove = [88, 89, 92, 100] for s in subject_to_remove: if s in subjects: subjects.remove(s) runs = [6, 10, 14] # motor imagery: hands vs feet classifiers = { 'mdm': make_pipeline(Covariances(), MDM(metric='riemann')), 'fgmdm': make_pipeline(Covariances(), FgMDM(metric='riemann')), 'tsLR': make_pipeline(Covariances(), TangentSpace(), LogisticRegression()), 'csp': make_pipeline(CSP(n_components=4, reg=None, log=True), LDA()) } # cross validation results = np.zeros((len(subjects), len(classifiers))) for s, subject in enumerate(subjects): print('Processing Subject %s' % (subject)) raw_files = [ read_raw_edf(f, preload=True, verbose=False) for f in eegbci.load_data(subject, runs)
labels = epochs.events[:, -1] evoked = epochs.average() ############################################################################### # Decoding with Xdawn + MDM n_components = 3 # pick some components # Define a monte-carlo cross-validation generator (reduce variance): cv = KFold(len(labels), 10, shuffle=True, random_state=42) pr = np.zeros(len(labels)) epochs_data = epochs.get_data() print('Multiclass classification with XDAWN + MDM') clf = make_pipeline(XdawnCovariances(n_components), MDM()) for train_idx, test_idx in cv: y_train, y_test = labels[train_idx], labels[test_idx] clf.fit(epochs_data[train_idx], y_train) pr[test_idx] = clf.predict(epochs_data[test_idx]) print(classification_report(labels, pr)) ############################################################################### # plot the spatial patterns xd = XdawnCovariances(n_components) xd.fit(epochs_data, labels) evoked.data = xd.Xd_.patterns_.T