def erp_cov_vr_pc(X_training, labels_training, X_test, labels_test, class_name, class_info): # estimate the extended ERP covariance matrices with Xdawn erpc = ERPCovariances(classes=[class_info[class_name]], estimator='lwf') erpc.fit(X_training, labels_training) covs_training = erpc.transform(X_training) covs_test = erpc.transform(X_test) # get the AUC for the classification clf = MDM() clf.fit(covs_training, labels_training) labels_pred = clf.predict(covs_test) return roc_auc_score(labels_test, labels_pred)
def get_sourcetarget_split_p300(source, target, ncovs_train): X_source = source['epochs'] y_source = source['labels'].flatten() covs_source = ERPCovariances(classes=[2], estimator='lwf').fit_transform( X_source, y_source) source = {} source['covs'] = covs_source source['labels'] = y_source X_target = target['epochs'] y_target = target['labels'].flatten() sel = np.arange(len(y_target)) np.random.shuffle(sel) X_target = X_target[sel] y_target = y_target[sel] idx_erps = np.where(y_target == 2)[0][:ncovs_train] idx_rest = np.where( y_target == 1)[0][:ncovs_train * 5] # because there's one ERP in every 6 flashes idx_train = np.concatenate([idx_erps, idx_rest]) idx_test = np.array( [i for i in range(len(y_target)) if i not in idx_train]) erp = ERPCovariances(classes=[2], estimator='lwf') erp.fit(X_target[idx_train], y_target[idx_train]) target_train = {} covs_target_train = erp.transform(X_target[idx_train]) y_target_train = y_target[idx_train] target_train['covs'] = covs_target_train target_train['labels'] = y_target_train target_test = {} covs_target_test = erp.transform(X_target[idx_test]) y_target_test = y_target[idx_test] target_test['covs'] = covs_target_test target_test['labels'] = y_target_test return source, target_train, target_test
def test_from_cross_template_P300(template_path, subject_path, test_chnames, flashmode='RoCo', nb_targets=180, visu=False): T = 0 NT = 1 ERP = np.load(template_path + 'ERP_Array.npy') Centroids_List = np.load(template_path + 'Centroids_List.npy') mu_TNT = np.load(template_path + 'rTNT_mu.npy') sigma_TNT = np.load(template_path + 'rTNT_var.npy') data, labels, event = get_data_from_csv_EIV( myfilename=subject_path + '-signals.csv', markersfile=subject_path + 'markers.csv', chnames=test_chnames) erp = ERPCovariances() erp.P = ERP erp.estimator = 'cov' X = erp.transform(data) train_NaiveBayes = R_TNT_NaiveBayes(targets=[ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_' ], mu_TNT=mu_TNT, sigma_TNT=sigma_TNT, class_prior=None) dist = [ np.array([distance(x, Centroids_List[l]) for i, x in enumerate(X)]) for l in [T, NT] ] r_TNT = np.array(np.log(dist[0] / dist[1])) mean, var = test_loop_P300(r_TNT_test=r_TNT, y_test=labels, e_test=event, train_NaiveBayes=train_NaiveBayes, T=0, NT=1, flashmode=flashmode, visu=visu, nb_targets=nb_targets) return mean, var
def distribution_single_from_template(test_sub, test_sess, template_path, database): x_test, y_test, e_test, t_test, _, _ = database.subjects[ test_sub].get_data([test_sess]) ERP_train = np.load(template_path + 'ERP_Array.npy') erp_train = ERPCovariances(estimator='cov') erp_train.P = ERP_train X_test = erp_train.transform(x_test) centroids_train = np.load(template_path + 'Centroids_List.npy') erp_train = ERPCovariances(estimator='cov') erp_train.P = ERP_train r_TNT_test = predict_R_TNT(X=X_test, centroids_list=centroids_train) return r_TNT_test, y_test
def predict_ERP_centroids(x, y, metric='riemann', ERP_bloc=None, T=0, NT=1): """Helper to predict the r_TNT for a new set of trials. Parameters ---------- x : ndarray, shape (n_trials, n_channels, n_times) y : ndarray, shape (,n_trials) ERP_bloc : list with 0 or 1 for the class in the ERP Returns ------- erp : the ERPCovariance object with erp.P an ndarray, shape (n_channels*len(ERP_bloc), n_times) centroids : list of the two centers of classe which are both ndarray, shape (n_channels*len(ERP_bloc), n_channels*len(ERP_bloc)) X : ndarray, shape (n_trials, n_channels*len(ERP_bloc), n_channels*len(ERP_bloc)), the set of super covariance matrices of set signals given in input """ classes = [T, NT] erp = ERPCovariances(classes=ERP_bloc, estimator='cov') erp.fit(X=x, y=y) X = erp.transform(X=x) centroids = [ mean_covariance(X[y == l, :, :], metric=metric) for l in classes ] return erp, centroids, X
kf = KFold(n_splits = 6) repetitions = [1, 2] auc = [] blocks = np.arange(1, 12+1) for train_idx, test_idx in kf.split(np.arange(12)): # split in training and testing blocks X_training, labels_training, _ = get_block_repetition(X, labels, meta, blocks[train_idx], repetitions) X_test, labels_test, _ = get_block_repetition(X, labels, meta, blocks[test_idx], repetitions) # estimate the extended ERP covariance matrices with Xdawn dict_labels = {'Target':1, 'NonTarget':0} erpc = ERPCovariances(classes=[dict_labels['Target']], estimator='lwf') erpc.fit(X_training, labels_training) covs_training = erpc.transform(X_training) covs_test = erpc.transform(X_test) # get the AUC for the classification clf = MDM() clf.fit(covs_training, labels_training) labels_pred = clf.predict(covs_test) auc.append(roc_auc_score(labels_test, labels_pred)) # stock scores scores_subject.append(np.mean(auc)) scores.append(scores_subject) # print results df[tmax] = pd.DataFrame(scores, columns=['subject', 'VR', 'PC'])