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_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_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
pipelines = {} pipelines["AM+LDA"] = make_pipeline(LogVariance(), LDA()) parameters = {"C": np.logspace(-2, 2, 10)} clf = GridSearchCV(SVC(kernel="linear"), parameters) pipe = make_pipeline(LogVariance(), clf) pipelines["AM+SVM"] = pipe ############################################################################## # Datasets # ----------------- # # Datasets can be specified in many ways: Each paradigm has a property # 'datasets' which returns the datasets that are appropriate for that paradigm print(LeftRightImagery().datasets) ########################################################################## # Or you can run a search through the available datasets: print(utils.dataset_search(paradigm="imagery", min_subjects=6)) ########################################################################## # Or you can simply make your own list (which we do here due to computational # constraints) dataset = BNCI2014001() dataset.subject_list = dataset.subject_list[:2] datasets = [dataset] ########################################################################## # Paradigm
# Datasets # -------- # # Select datasets for motor imagery datasets = [Zhou2016(), BNCI2014001()] ############################################################################## # Paradigm # -------- # # Restrict further analysis to specified channels, here C3, C4, and Cz. # Also, use a specific resampling. In this example, all datasets are # set to 200 Hz. paradigm = LeftRightImagery(channels=['C3', 'C4', 'Cz'], resample=200.) ############################################################################## # Evaluation # ---------- # # The evaluation is conducted on with CSP+LDA, only on the 3 electrodes, with # a sampling rate of 200 Hz. evaluation = WithinSessionEvaluation(paradigm=paradigm, datasets=datasets) csp_lda = make_pipeline(CSP(n_components=2), LDA()) ts_lr = make_pipeline(Covariances(estimator='oas'), TangentSpace(metric='riemann'), LR(C=1.0)) results = evaluation.process({'csp+lda': csp_lda, 'ts+lr': ts_lr}) print(results.head())
pipelines['RG + LR'] = make_pipeline(Covariances(), TangentSpace(), LogisticRegression()) ############################################################################## # Evaluation # ---------- # # We define the paradigm (LeftRightImagery) and the dataset (BNCI2014001). # The evaluation will return a dataframe containing a single AUC score for # each subject / session of the dataset, and for each pipeline. # # Results are saved into the database, so that if you add a new pipeline, it # will not run again the evaluation unless a parameter has changed. Results can # be overwrited if necessary. paradigm = LeftRightImagery() datasets = [BNCI2014001c()] overwrite = False # set to True if we want to overwrite cached results evaluation = CrossSessionEvaluation(paradigm=paradigm, datasets=datasets, suffix='examples', overwrite=overwrite) results = evaluation.process(pipelines) print(results.head()) ############################################################################## # Plot Results # ---------------- #
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)
# # Load 2 subjects of BNCI 2014-004 and Zhou2016 datasets, with 2 session each subj = [1, 2] datasets = [Zhou2016(), BNCI2014001()] for d in datasets: d.subject_list = subj ############################################################################### # Choose Paradigm # --------------- # # We select the paradigm MI, applying a bandpass filter (8-35 Hz) on # the data and we will keep only left- and right-hand motor imagery paradigm = LeftRightImagery(fmin=8, fmax=35) ############################################################################## # Create Pipelines # ---------------- # # Use the Common Spatial Patterns with 8 components and a Linear Discriminant # Analysis classifier. pipeline = {} pipeline["CSP+LDA"] = make_pipeline(CSP(n_components=8), LDA()) ############################################################################## # Get Data (optional) # ------------------- #
# Since two different preprocessing will be applied, we have two different # paradigm objects. We have to make sure their filter matchs so the comparison # will be fair. # # The first one is a standard `LeftRightImagery` with a 8 to 35 Hz broadband # filter. # # The second is a `FilterBankLeftRightImagery` with a bank of 6 filter, ranging # from 8 to 35 Hz. datasets = [BNCI2014001()] overwrite = False # set to True if we want to overwrite cached results # broadband filters filters = [[8, 35]] paradigm = LeftRightImagery(filters=filters) evaluation = CrossSessionEvaluation(paradigm=paradigm, datasets=datasets, suffix='examples', overwrite=overwrite) results = evaluation.process(pipelines) # cashed results might return other pipelines results = results[results.pipeline == 'CSP + LDA'] # bank of 6 filter, by 4 Hz increment filters = [[8, 12], [12, 16], [16, 20], [20, 24], [24, 28], [28, 35]] paradigm = FilterBankLeftRightImagery() evaluation = CrossSessionEvaluation(paradigm=paradigm, datasets=datasets, suffix='examples',
# paradigm objects. We have to make sure their filter matchs so the comparison # will be fair. # # The first one is a standard `LeftRightImagery` with a 8 to 35 Hz broadband # filter. # # The second is a `FilterBankLeftRightImagery` with a bank of 6 filter, ranging # from 8 to 35 Hz. datasets = [BNCI2014001()] overwrite = False # set to True if we want to overwrite cached results # broadband filters fmin = 8 fmax = 35 paradigm = LeftRightImagery(fmin=fmin, fmax=fmax) evaluation = CrossSessionEvaluation(paradigm=paradigm, datasets=datasets, suffix='examples', overwrite=overwrite) results = evaluation.process(pipelines) # bank of 6 filter, by 4 Hz increment filters = [[8, 12], [12, 16], [16, 20], [20, 24], [24, 28], [28, 35]] paradigm = FilterBankLeftRightImagery(filters=filters) evaluation = CrossSessionEvaluation(paradigm=paradigm, datasets=datasets, suffix='examples', overwrite=overwrite) results_fb = evaluation.process(pipelines_fb)
def test_LeftRightImagery_badevents(self): paradigm = LeftRightImagery() # does not accept dataset with bad event dataset = FakeDataset(paradigm="imagery") self.assertRaises(AssertionError, paradigm.get_data, dataset)
# Datasets # -------- # # Select datasets for motor imagery datasets = [Zhou2016(), BNCI2014001()] ############################################################################## # Paradigm # -------- # # Restrict further analysis to specified channels, here C3, C4, and Cz. # Also, use a specific resampling. In this example, all datasets are # set to 200 Hz. paradigm = LeftRightImagery(channels=["C3", "C4", "Cz"], resample=200.0) ############################################################################## # Evaluation # ---------- # # The evaluation is conducted on with CSP+LDA, only on the 3 electrodes, with # a sampling rate of 200 Hz. evaluation = WithinSessionEvaluation(paradigm=paradigm, datasets=datasets) csp_lda = make_pipeline(CSP(n_components=2), LDA()) ts_lr = make_pipeline(Covariances(estimator="oas"), TangentSpace(metric="riemann"), LR(C=1.0)) results = evaluation.process({"csp+lda": csp_lda, "ts+lr": ts_lr}) print(results.head())