def test_Xdawncovariances(): """Test fit ERPCovariances""" x = np.random.randn(10, 3, 100) labels = np.array([0, 1]).repeat(5) cov = XdawnCovariances() cov.fit_transform(x, labels) assert_equal(cov.get_params(), dict(nfilter=4, applyfilters=True, classes=None, estimator='scm', xdawn_estimator='scm'))
def test_Xdawncovariances(): """Test fit ERPCovariances""" x = np.random.randn(10, 3, 100) labels = np.array([0, 1]).repeat(5) cov = XdawnCovariances() cov.fit_transform(x, labels) assert_equal(cov.get_params(), dict(nfilter=4, applyfilters=True, classes=None, estimator='scm', xdawn_estimator='scm', baseline_cov=None))
def test_xdawn_covariances_applyfilters(rndstate, get_labels): n_classes, nfilter = 2, 2 n_matrices, n_channels, n_times = 4, 6, 100 x = rndstate.randn(n_matrices, n_channels, n_times) labels = get_labels(n_matrices, n_classes) cov = XdawnCovariances(nfilter=nfilter, applyfilters=False) covmats = cov.fit_transform(x, labels) covsize = n_classes * nfilter + n_channels assert covmats.shape == (n_matrices, covsize, covsize) assert is_spsd(covmats)
def test_xdawn_covariances_nfilter(nfilter, rndstate, get_labels): """Test fit XdawnCovariances""" n_classes, n_matrices, n_channels, n_times = 2, 4, 8, 100 x = rndstate.randn(n_matrices, n_channels, n_times) labels = get_labels(n_matrices, n_classes) cov = XdawnCovariances(nfilter=nfilter) covmats = cov.fit_transform(x, labels) assert cov.get_params() == dict( nfilter=nfilter, applyfilters=True, classes=None, estimator="scm", xdawn_estimator="scm", baseline_cov=None, ) covsize = 2 * (n_classes * nfilter) assert covmats.shape == (n_matrices, covsize, covsize) assert is_spsd(covmats)
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) scores.append(clf.score(epochs_data[test_idx], y_test)) # Printing the results class_balance = np.mean(labels == labels[0]) class_balance = max(class_balance, 1. - class_balance) print("Classification accuracy: %f / Chance level: %f" % (np.mean(scores), class_balance)) # spatial patterns xd = XdawnCovariances(n_components) Cov = xd.fit_transform(epochs_data,labels) evoked.data = xd.Xd._patterns.T evoked.times = np.arange(evoked.data.shape[0]) evoked.plot_topomap(times=[0, 1, n_components, n_components+1], ch_type='grad', colorbar=False, size=1.5) # prototyped covariance matrices mdm = MDM() mdm.fit(Cov,labels) fig,axe = plt.subplots(1,2) axe[0].matshow(mdm.covmeans[0]) axe[0].set_title('Class 1 covariance matrix') axe[1].matshow(mdm.covmeans[1]) axe[1].set_title('Class 2 covariance matrix') plt.show()
subject_dir_list = test_list_np[i] subject_epoch = np.empty((0, 56, 260), float) for j in range(5): subject_dir = subject_dir_list[j] data = epoching('./data/test/' + subject_dir) subject_epoch = np.vstack((subject_epoch, data)) subject_epoch = np.reshape(subject_epoch, (1, 340, 56, 260)) test_data_list = np.vstack((test_data_list, subject_epoch)) print('Epoched training data shape: ' + str(train_data_list.shape)) print('Epoched testing data shape: ' + str(test_data_list.shape)) ########################## apply data preprocessing ############################ y_train = pd.read_csv('TrainLabels.csv')['Prediction'].values y_test = np.reshape(pd.read_csv('true_labels.csv', header=None).values, 3400) XC = XdawnCovariances(nfilter=5) output_train = XC.fit_transform( np.reshape(train_data_list, (16 * 340, 56, 260)), y_train) X_train = TangentSpace(metric='riemann').fit_transform(output_train) output_test = XC.fit_transform(np.reshape(test_data_list, (10 * 340, 56, 260)), y_test) X_test = TangentSpace(metric='riemann').fit_transform(output_test) print('Preprocessed training data shape: ' + str(X_train.shape)) print('Preprocessed testing data shape: ' + str(X_test.shape)) ############################## save data to disk ############################### np.save('./data/train_data_56_260_1_40Hz.npy', train_data_list) np.save('./data/test_data_56_260_1_40Hz.npy', test_data_list) np.save('./data/X_train', X_train) np.save('./data/X_test', X_test)
print('Multiclass classification with XDAWN + FgMDM') clf = Pipeline([('COV', XdawnCovariances(n_components)), ('MDM', FgMDM())]) 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) print confusion_matrix(labels, pr) # spatial patterns xd = XdawnCovariances(n_components) Cov = xd.fit_transform(epochs_data, labels) evoked.data = xd.Xd._patterns.T evoked.times = np.arange(evoked.data.shape[0]) evoked.plot_topomap( times=[0, n_components, 2 * n_components, 3 * n_components], ch_type='grad', colorbar=False, size=1.5) # prototyped covariance matrices mdm = MDM() mdm.fit(Cov, labels) fig, axe = plt.subplots(1, 4) axe[0].matshow(mdm.covmeans[0]) axe[0].set_title('Class 1 covariance matrix')
def test_Xdawncovariances(): """Test fit ERPCovariances""" x = np.random.randn(10, 3, 100) labels = np.array([0, 1]).repeat(5) cov = XdawnCovariances() cov.fit_transform(x, labels)
subject_dir_list = test_list_np[testing_participant_id] subject_epoch = np.empty((0, len(channels), epoch_len), float) for trial_id in range(trial_per_subj): subject_dir = subject_dir_list[trial_id] data = generate_epoch('FeedBackEvent', './data/test/'+subject_dir, channels, fs, lowcut, highcut, epoch_s, epoch_e, bl_s, bl_e) subject_epoch = np.vstack((subject_epoch, data)) subject_epoch = np.reshape( subject_epoch, (1, stimulus_per_subj, len(channels), epoch_len)) test_data_list = np.vstack((test_data_list, subject_epoch)) print('Epoched testing data shape: ' + str(test_data_list.shape)) # ########################## apply data preprocessing ############################ y_train = pd.read_csv('data/TrainLabels.csv')['Prediction'].values XC = XdawnCovariances(nfilter=5) X_train = XC.fit_transform(np.reshape( train_data_list, (total_training_participant*stimulus_per_subj, len(channels), epoch_len)), y_train) X_train = TangentSpace(metric='riemann').fit_transform(X_train) X_test = XC.transform(np.reshape( test_data_list, (10*stimulus_per_subj, len(channels), epoch_len))) X_test = TangentSpace(metric='riemann').transform(X_test) print('Preprocessed training data shape: ' + str(X_train.shape)) print('Preprocessed testing data shape: ' + str(X_test.shape)) # ############################## save data to disk ############################### np.save('./data/train_data.npy', train_data_list) np.save('./data/test_data.npy', test_data_list) np.save('./data/X_train', X_train) np.save('./data/X_test', X_test)
def test_Xdawncovariances(): """Test fit ERPCovariances""" x = np.random.randn(10,3,100) labels = np.array([0,1]).repeat(5) cov = XdawnCovariances() cov.fit_transform(x,labels)