Skip to content

Commit

Permalink
correct channel selection error and add a tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain Chevallier committed Jul 13, 2020
1 parent 1ce83d8 commit 131aa0f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
7 changes: 4 additions & 3 deletions moabb/paradigms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ def process_raw(self, raw, dataset, return_epochs=False):
events = mne.find_events(raw, shortest_event=0, verbose=False)
else:
events, _ = mne.events_from_annotations(raw, verbose=False)
channels = () if self.channels is None else self.channels

# picks channels
picks = mne.pick_types(raw.info, eeg=True, stim=False,
include=channels)
if self.channels is None:
picks = mne.pick_types(raw.info, eeg=True, stim=False)
else:
picks = mne.pick_types(raw.info, stim=False, include=self.channels)

# pick events, based on event_id
try:
Expand Down
88 changes: 88 additions & 0 deletions tutorials/select_electrodes_resample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""
================================
Select electrodes and resampling
================================
Within paradigm, it is possible to restrict analysis only to a subset of
electrodes and to resample to a specific sampling rate. There is also a
utility function to select common electrodes shared between datasets.
This tutorial demonstrates how to use this functionality.
"""
# Authors: Sylvain Chevallier <[email protected]>
#
# License: BSD (3-clause)
from moabb.datasets import BNCI2014001, Zhou2016
from moabb.paradigms import LeftRightImagery
from moabb.evaluations import WithinSessionEvaluation
from moabb.datasets.utils import find_intersecting_channels

from sklearn.pipeline import make_pipeline
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
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
# ----------
#
# 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())

##############################################################################
# Electrode selection
# -------------------
#
# It is possible to select the electrodes that are shared by all datasets
# using the `find_intersecting_channels` function. Datasets that have 0
# overlap with others are discarded. It returns the set of common channels,
# as well as the list of datasets with valid channels.

electrodes, datasets = find_intersecting_channels(datasets)
evaluation = WithinSessionEvaluation(paradigm=paradigm,
datasets=datasets,
overwrite=True)
results = evaluation.process({'csp+lda': csp_lda, 'ts+lr': ts_lr})
print(results.head())

##############################################################################
# Plot results
# ------------
#
# Compare the obtained results with the two pipelines, CSP+LDA and logistic
# regression computed in the tangent space of the covariance matrices.

fig = moabb_plt.paired_plot(results, 'csp+lda', 'ts+lr')
plt.show()

0 comments on commit 131aa0f

Please sign in to comment.