예제 #1
0
from sklearn.linear_model import LogisticRegression as LR

from mne.decoding import CSP
from pyriemann.estimation import Covariances
from pyriemann.tangentspace import TangentSpace

import matplotlib.pyplot as plt
import moabb.analysis.plotting as moabb_plt

##############################################################################
# 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
# ----------
#
# first pipeline is the CSP+LDA that we have seen in the previous parts. The
# other two pipelines rely on Riemannian geometry, using an SVM classification
# in the tangent space of the covariance matrices estimated from the EEG or a
# MDM classifier that works directly on covariance matrices.

pipelines = {}
pipelines["csp+lda"] = make_pipeline(CSP(n_components=8), LDA())
pipelines["tgsp+svm"] = make_pipeline(Covariances("oas"),
                                      TangentSpace(metric="riemann"),
                                      SVC(kernel="linear"))
pipelines["MDM"] = make_pipeline(Covariances("oas"), MDM(metric="riemann"))

##############################################################################
# The following lines go exactly as in the previous tutorial, where we end up
# obtaining a pandas dataframe containing the results of the evaluation.
datasets = [BNCI2014001(), Zhou2016()]
subj = [1, 2, 3]
for d in datasets:
    d.subject_list = subj
paradigm = LeftRightImagery()
evaluation = WithinSessionEvaluation(paradigm=paradigm,
                                     datasets=datasets,
                                     overwrite=False)
results = evaluation.process(pipelines)

##############################################################################
# As `overwrite` is set to False, the results from the previous tutorial are reused and
# only the new pipelines are evaluated. The results from "csp+lda" are not recomputed.
# The results are saved in ~/mne_data/results if the parameter `hdf5_path` is not set.

##############################################################################
# We instantiate the three different classiciation pipelines to be considered
# in the analysis. The object that gathers each pipeline is a dictionary. The
# first pipeline is the CSP+LDA that we have seen in the previous parts. The
# other two pipelines rely on Riemannian geometry, using an SVM classification
# in the tangent space of the covariance matrices estimated from the EEG or a
# MDM classifier that works directly on covariance matrices.
pipelines = {}
pipelines["csp+lda"] = make_pipeline(CSP(n_components=8), LDA())
pipelines["tgsp+svm"] = make_pipeline(Covariances('oas'),
                                      TangentSpace(metric='riemann'),
                                      SVC(kernel='linear'))
pipelines["MDM"] = make_pipeline(Covariances('oas'), MDM(metric='riemann'))

# The following lines go exactly as in the previous example, where we end up
# obtaining a pandas dataframe containing the results of the evaluation.
datasets = [BNCI2014001(), Weibo2014(), Zhou2016()]
paradigm = LeftRightImagery()
evaluation = WithinSessionEvaluation(paradigm=paradigm,
                                     datasets=datasets,
                                     overwrite=True)
results = evaluation.process(pipelines)
if not os.path.exists("./results"):
    os.mkdir("./results")
results.to_csv("./results/results_part2-3.csv")
results = pd.read_csv('./results/results_part2-3.csv')

##############################################################################
# Plotting Results
# ----------------
#
# The following plot shows a comparison of the three classification pipelines
예제 #4
0
from utilities import transfer_learning as TL
from sklearn.model_selection import KFold
from tqdm import tqdm

# setup the paradigm
paradigm = MotorImagery(events=['right_hand', 'feet'])
paradigm_name = 'MI'

# set the weights for each class in the dataset
weights_classes = {}
weights_classes['feet'] = 1
weights_classes['right_hand'] = 1

# get data from source
source = {}
dataset_source = Zhou2016()
subject_source = 1
raw_source = dataset_source._get_single_subject_data(
    subject_source)['session_0']['run_0']
raw_source.pick_types(eeg=True)
X, labels, meta = paradigm.get_data(dataset_source, subjects=[subject_source])
source['org'] = {}
source['org']['covs'] = Covariances(estimator='lwf').fit_transform(X[:, 2:, :])
source['org']['labels'] = labels
source['org']['chnames'] = [chi.upper() for chi in raw_source.ch_names[2:]]

# get data from target
target = {}
dataset_target = BNCI2015001()
subject_target = 1
raw_target = dataset_target._get_single_subject_data(