def test_LeftRightImagery_paradigm(self): # with a good dataset paradigm = LeftRightImagery() dataset = FakeDataset(event_list=["left_hand", "right_hand"], paradigm="imagery") X, labels, metadata = paradigm.get_data(dataset, subjects=[1]) self.assertEqual(len(np.unique(labels)), 2) self.assertEqual(list(np.unique(labels)), ["left_hand", "right_hand"]) # should return epochs epochs, _, _ = paradigm.get_data(dataset, subjects=[1], return_epochs=True) self.assertIsInstance(epochs, BaseEpochs)
def test_LeftRightImagery_paradigm(self): # with a good dataset paradigm = LeftRightImagery() dataset = FakeDataset(event_list=['left_hand', 'right_hand']) X, labels, metadata = paradigm.get_data(dataset, subjects=[1]) self.assertEqual(len(np.unique(labels)), 2) self.assertEqual(list(np.unique(labels)), ['left_hand', 'right_hand'])
def test_leftright_paradigm(self): # we cant pass event to this class paradigm = LeftRightImagery() self.assertRaises(ValueError, LeftRightImagery, events=['a']) # does not accept dataset with bad event dataset = FakeDataset() self.assertRaises(AssertionError, paradigm.get_data, dataset) # with a good dataset dataset = FakeDataset(event_list=['left_hand', 'right_hand']) X, labels, metadata = paradigm.get_data(dataset, subjects=[1]) self.assertEquals(len(np.unique(labels)), 2) self.assertEquals(list(np.unique(labels)), ['left_hand', 'right_hand'])
def get_cho_train_val_test(subject_id): paradigm = LeftRightImagery() dataset = Cho2017() subjects = [subject_id] X, y, metadata = paradigm.get_data(dataset=dataset, subjects=subjects) y = np.where(y == 'left_hand', 0, y) y = np.where(y == 'right_hand', 1, y) X_train_val, X_test, y_train_val, y_test = train_test_split( X, y, test_size=0.3) # test = 30%, same as paper X_train, X_val, y_train, y_val = train_test_split( X_train_val, y_train_val, test_size=global_vars.get('valid_set_fraction')) train_set, valid_set, test_set = makeDummySignalTargets( X_train, y_train, X_val, y_val, X_test, y_test) return train_set, valid_set, test_set
def get_bci_iv_2b_train_val_test(subject_id): paradigm = LeftRightImagery() dataset = BNCI2014004() subjects = [subject_id] X, y, metadata = paradigm.get_data(dataset=dataset, subjects=subjects) y = np.where(y == 'left_hand', 0, y) y = np.where(y == 'right_hand', 1, y) train_indexes = np.logical_or(metadata.values[:, 1] == 'session_0', metadata.values[:, 1] == 'session_1', metadata.values[:, 1] == 'session_2') test_indexes = np.logical_or(metadata.values[:, 1] == 'session_3', metadata.values[:, 1] == 'session_4') X_train_val = X[train_indexes] y_train_val = y[train_indexes] X_test = X[test_indexes] y_test = y[test_indexes] X_train, X_val, y_train, y_val = train_test_split( X_train_val, y_train_val, test_size=global_vars.get('valid_set_fraction')) train_set, valid_set, test_set = makeDummySignalTargets( X_train, y_train, X_val, y_val, X_test, y_test) return train_set, valid_set, test_set
from pyriemann.classification import MDM from sklearn.externals import joblib from sklearn.model_selection import StratifiedKFold from sklearn.metrics import roc_auc_score from pyriemann.estimation import Covariances from riemann_lab import power_means import moabb from moabb.datasets import BNCI2014001 from moabb.paradigms import LeftRightImagery # load the data subject = 1 dataset = BNCI2014001() paradigm = LeftRightImagery() X, labels, meta = paradigm.get_data(dataset, subjects=[subject]) X = X[meta['session'] == 'session_E'] covs = Covariances(estimator='oas').fit_transform(X) labs = labels[meta['session'] == 'session_E'] # define the pipelines for classification -- MDM and MeansField classifier pipelines = {} pipelines['MDM'] = MDM() plist = [1.00, 0.75, 0.50, 0.25, 0.10, 0.01, -0.01, -0.10, -0.25, -0.50, -0.75, -1.00] pipelines['MeansField'] = power_means.MeanFieldClassifier(plist=plist) # perform the KFold cross-validation procedure with stratified segments # (same proportion of labels form each class on every fold) n_splits = 5 kf = StratifiedKFold(n_splits) scores = {}