Note
Go to the end to download the full example code.
Tutorial 0: Getting Started#
This tutorial takes you through a basic working example of how to use this codebase, including all the different components, up to the results generation. If you’d like to know about the statistics and plotting, see the next tutorial.
# Authors: Vinay Jayaram <vinayjayaram13@gmail.com>
#
# License: BSD (3-clause)
Introduction#
To use the codebase you need an evaluation and a paradigm, some algorithms, and a list of datasets to run it all on. You can find those in the following submodules; detailed tutorials are given for each of them.
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.svm import SVC
If you would like to specify the logging level when it is running, you can use the standard python logging commands through the top-level moabb module
import moabb
from moabb.datasets import BNCI2014_001, utils
from moabb.evaluations import CrossSessionEvaluation
from moabb.paradigms import LeftRightImagery
from moabb.pipelines.features import LogVariance
In order to create pipelines within a script, you will likely need at least the make_pipeline function. They can also be specified via a .yml file. Here we will make a couple pipelines just for convenience
moabb.set_log_level("info")
Create pipelines#
We create two pipelines: channel-wise log variance followed by LDA, and channel-wise log variance followed by a cross-validated SVM (note that a cross-validation via scikit-learn cannot be described in a .yml file). For later in the process, the pipelines need to be in a dictionary where the key is the name of the pipeline and the value is the Pipeline object
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)
/home/runner/work/moabb/moabb/moabb/datasets/fake.py:92: RuntimeWarning: Setting non-standard config type: "MNE_DATASETS_FAKEDATASET-IMAGERY-10-2--60-60--120-120--FAKE1-FAKE2-FAKE3--C3-CZ-C4_PATH"
set_config(key, temp_dir)
/home/runner/work/moabb/moabb/moabb/datasets/fake.py:92: RuntimeWarning: Setting non-standard config type: "MNE_DATASETS_FAKEVIRTUALREALITYDATASET-P300-21-1--60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60-60--120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120-120--TARGET-NONTARGET--C3-CZ-C4_PATH"
set_config(key, temp_dir)
[<moabb.datasets.bnci.BNCI2014_001 object at 0x7fa536783790>, <moabb.datasets.bnci.BNCI2014_004 object at 0x7fa5367814b0>, <moabb.datasets.beetl.Beetl2021_A object at 0x7fa536781c60>, <moabb.datasets.beetl.Beetl2021_B object at 0x7fa536781e10>, <moabb.datasets.gigadb.Cho2017 object at 0x7fa536783d90>, <moabb.datasets.dreyer2023.Dreyer2023 object at 0x7fa536783f70>, <moabb.datasets.dreyer2023.Dreyer2023A object at 0x7fa536782e00>, <moabb.datasets.dreyer2023.Dreyer2023B object at 0x7fa536782260>, <moabb.datasets.dreyer2023.Dreyer2023C object at 0x7fa536782110>, <moabb.datasets.mpi_mi.GrosseWentrup2009 object at 0x7fa5377822f0>, <moabb.datasets.Lee2019.Lee2019_MI object at 0x7fa537780c10>, <moabb.datasets.liu2024.Liu2024 object at 0x7fa536783f40>, <moabb.datasets.physionet_mi.PhysionetMI object at 0x7fa536781390>, <moabb.datasets.schirrmeister2017.Schirrmeister2017 object at 0x7fa5367831f0>, <moabb.datasets.bbci_eeg_fnirs.Shin2017A object at 0x7fa537782980>, <moabb.datasets.stieger2021.Stieger2021 object at 0x7fa537448400>, <moabb.datasets.Weibo2014.Weibo2014 object at 0x7fa537449de0>, <moabb.datasets.Zhou2016.Zhou2016 object at 0x7fa53744ac20>]
Or you can run a search through the available datasets:
print(utils.dataset_search(paradigm="imagery", min_subjects=6))
[<moabb.datasets.alex_mi.AlexMI object at 0x7fa536783ca0>, <moabb.datasets.bnci.BNCI2014_001 object at 0x7fa536783790>, <moabb.datasets.bnci.BNCI2014_002 object at 0x7fa536783670>, <moabb.datasets.bnci.BNCI2014_004 object at 0x7fa536783b50>, <moabb.datasets.bnci.BNCI2015_001 object at 0x7fa5367810c0>, <moabb.datasets.bnci.BNCI2015_004 object at 0x7fa536780910>, <moabb.datasets.gigadb.Cho2017 object at 0x7fa536782140>, <moabb.datasets.dreyer2023.Dreyer2023 object at 0x7fa536782200>, <moabb.datasets.dreyer2023.Dreyer2023A object at 0x7fa536782d70>, <moabb.datasets.dreyer2023.Dreyer2023B object at 0x7fa536780d30>, <moabb.datasets.dreyer2023.Dreyer2023C object at 0x7fa536782ad0>, <moabb.datasets.fake.FakeDataset object at 0x7fa5367831f0>, <moabb.datasets.mpi_mi.GrosseWentrup2009 object at 0x7fa536780af0>, <moabb.datasets.Lee2019.Lee2019_MI object at 0x7fa536783eb0>, <moabb.datasets.liu2024.Liu2024 object at 0x7fa5367810f0>, <moabb.datasets.upper_limb.Ofner2017 object at 0x7fa536783e80>, <moabb.datasets.physionet_mi.PhysionetMI object at 0x7fa536781c30>, <moabb.datasets.schirrmeister2017.Schirrmeister2017 object at 0x7fa536781d80>, <moabb.datasets.bbci_eeg_fnirs.Shin2017A object at 0x7fa536780610>, <moabb.datasets.bbci_eeg_fnirs.Shin2017B object at 0x7fa536783400>, <moabb.datasets.stieger2021.Stieger2021 object at 0x7fa5367804f0>, <moabb.datasets.Weibo2014.Weibo2014 object at 0x7fa536783430>]
Or you can simply make your own list (which we do here due to computational constraints)
dataset = BNCI2014_001()
dataset.subject_list = dataset.subject_list[:2]
datasets = [dataset]
Paradigm#
Paradigms define the events, epoch time, bandpass, and other preprocessing parameters. They have defaults that you can read in the documentation, or you can simply set them as we do here. A single paradigm defines a method for going from continuous data to trial data of a fixed size. To learn more look at the tutorial Exploring Paradigms
Evaluation#
An evaluation defines how the training and test sets are chosen. This could be cross-validated within a single recording, or across days, or sessions, or subjects. This also is the correct place to specify multiple threads.
evaluation = CrossSessionEvaluation(
paradigm=paradigm, datasets=datasets, suffix="examples", overwrite=False
)
results = evaluation.process(pipelines)
/home/runner/work/moabb/moabb/moabb/analysis/results.py:95: RuntimeWarning: Setting non-standard config type: "MOABB_RESULTS"
set_config("MOABB_RESULTS", osp.join(osp.expanduser("~"), "mne_data"))
BNCI2014-001-CrossSession: 0%| | 0/2 [00:00<?, ?it/s]2025-10-06 20:22:19,067 INFO MainThread moabb.datasets.download MNE_DATA is not already configured. It will be set to default location in the home directory - /home/runner/mne_data
All datasets will be downloaded to this location, if anything is already downloaded, please move manually to this location
/home/runner/work/moabb/moabb/moabb/datasets/download.py:60: RuntimeWarning: Setting non-standard config type: "MNE_DATASETS_BNCI_PATH"
set_config(key, get_config("MNE_DATA"))
/home/runner/work/moabb/moabb/.venv/lib/python3.10/site-packages/urllib3/connectionpool.py:1064: InsecureRequestWarning: Unverified HTTPS request is being made to host 'lampx.tugraz.at'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
0%| | 0.00/42.8M [00:00<?, ?B/s]
0%| | 16.4k/42.8M [00:00<04:25, 161kB/s]
0%| | 56.3k/42.8M [00:00<02:22, 300kB/s]
0%|▏ | 144k/42.8M [00:00<01:16, 558kB/s]
1%|▎ | 337k/42.8M [00:00<00:39, 1.08MB/s]
2%|▋ | 705k/42.8M [00:00<00:21, 1.99MB/s]
3%|█▎ | 1.45M/42.8M [00:00<00:10, 3.78MB/s]
7%|██▌ | 2.94M/42.8M [00:00<00:05, 7.31MB/s]
12%|████▍ | 5.14M/42.8M [00:00<00:03, 11.8MB/s]
15%|█████▍ | 6.33M/42.8M [00:00<00:03, 11.4MB/s]
20%|███████▎ | 8.46M/42.8M [00:01<00:02, 14.3MB/s]
25%|█████████▎ | 10.8M/42.8M [00:01<00:01, 16.9MB/s]
30%|███████████▏ | 13.0M/42.8M [00:01<00:01, 18.4MB/s]
36%|█████████████▏ | 15.2M/42.8M [00:01<00:01, 19.4MB/s]
41%|███████████████▏ | 17.6M/42.8M [00:01<00:01, 20.5MB/s]
46%|█████████████████▏ | 19.9M/42.8M [00:01<00:01, 21.1MB/s]
53%|███████████████████▌ | 22.7M/42.8M [00:01<00:00, 23.0MB/s]
58%|█████████████████████▌ | 25.0M/42.8M [00:01<00:00, 19.0MB/s]
64%|███████████████████████▊ | 27.5M/42.8M [00:01<00:00, 20.5MB/s]
70%|█████████████████████████▉ | 30.0M/42.8M [00:02<00:00, 21.6MB/s]
75%|███████████████████████████▉ | 32.3M/42.8M [00:02<00:00, 21.6MB/s]
81%|█████████████████████████████▊ | 34.5M/42.8M [00:02<00:00, 21.6MB/s]
86%|███████████████████████████████▊ | 36.8M/42.8M [00:02<00:00, 22.0MB/s]
91%|█████████████████████████████████▊ | 39.1M/42.8M [00:02<00:00, 21.9MB/s]
96%|███████████████████████████████████▋ | 41.3M/42.8M [00:02<00:00, 21.8MB/s]
0%| | 0.00/42.8M [00:00<?, ?B/s]
100%|██████████████████████████████████████| 42.8M/42.8M [00:00<00:00, 162GB/s]
/home/runner/work/moabb/moabb/.venv/lib/python3.10/site-packages/urllib3/connectionpool.py:1064: InsecureRequestWarning: Unverified HTTPS request is being made to host 'lampx.tugraz.at'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
0%| | 0.00/43.8M [00:00<?, ?B/s]
0%| | 8.19k/43.8M [00:00<09:00, 80.9kB/s]
0%| | 56.3k/43.8M [00:00<02:20, 311kB/s]
0%|▏ | 153k/43.8M [00:00<01:12, 598kB/s]
1%|▎ | 344k/43.8M [00:00<00:39, 1.10MB/s]
2%|▋ | 736k/43.8M [00:00<00:20, 2.08MB/s]
3%|█▎ | 1.51M/43.8M [00:00<00:10, 3.95MB/s]
7%|██▌ | 3.06M/43.8M [00:00<00:05, 7.58MB/s]
13%|████▉ | 5.77M/43.8M [00:00<00:02, 13.6MB/s]
18%|██████▋ | 7.88M/43.8M [00:00<00:02, 15.8MB/s]
24%|████████▉ | 10.5M/43.8M [00:01<00:01, 18.8MB/s]
31%|███████████▍ | 13.5M/43.8M [00:01<00:01, 22.0MB/s]
38%|██████████████ | 16.7M/43.8M [00:01<00:01, 24.7MB/s]
45%|████████████████▊ | 19.8M/43.8M [00:01<00:00, 26.5MB/s]
52%|███████████████████▎ | 22.8M/43.8M [00:01<00:00, 27.2MB/s]
59%|█████████████████████▉ | 25.9M/43.8M [00:01<00:00, 28.2MB/s]
66%|████████████████████████▎ | 28.7M/43.8M [00:01<00:00, 24.3MB/s]
71%|██████████████████████████▍ | 31.3M/43.8M [00:01<00:00, 24.6MB/s]
77%|████████████████████████████▋ | 33.9M/43.8M [00:01<00:00, 25.0MB/s]
83%|██████████████████████████████▊ | 36.5M/43.8M [00:01<00:00, 25.4MB/s]
89%|█████████████████████████████████ | 39.2M/43.8M [00:02<00:00, 25.7MB/s]
96%|███████████████████████████████████▍ | 41.9M/43.8M [00:02<00:00, 26.1MB/s]
0%| | 0.00/43.8M [00:00<?, ?B/s]
100%|██████████████████████████████████████| 43.8M/43.8M [00:00<00:00, 122GB/s]
BNCI2014-001-CrossSession: 50%|█████ | 1/2 [00:12<00:12, 12.90s/it]/home/runner/work/moabb/moabb/.venv/lib/python3.10/site-packages/urllib3/connectionpool.py:1064: InsecureRequestWarning: Unverified HTTPS request is being made to host 'lampx.tugraz.at'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
0%| | 0.00/43.1M [00:00<?, ?B/s]
0%| | 16.4k/43.1M [00:00<04:29, 160kB/s]
0%| | 64.5k/43.1M [00:00<02:05, 342kB/s]
0%|▏ | 153k/43.1M [00:00<01:14, 578kB/s]
1%|▎ | 344k/43.1M [00:00<00:39, 1.09MB/s]
1%|▌ | 600k/43.1M [00:00<00:26, 1.60MB/s]
3%|█ | 1.24M/43.1M [00:00<00:13, 3.17MB/s]
6%|██▏ | 2.49M/43.1M [00:00<00:06, 6.11MB/s]
10%|███▊ | 4.42M/43.1M [00:00<00:03, 10.2MB/s]
15%|█████▌ | 6.52M/43.1M [00:00<00:02, 13.4MB/s]
20%|███████▍ | 8.72M/43.1M [00:01<00:02, 15.9MB/s]
26%|█████████▍ | 11.0M/43.1M [00:01<00:01, 17.9MB/s]
31%|███████████▌ | 13.5M/43.1M [00:01<00:01, 19.7MB/s]
37%|█████████████▌ | 15.8M/43.1M [00:01<00:01, 20.6MB/s]
42%|███████████████▋ | 18.2M/43.1M [00:01<00:01, 21.4MB/s]
48%|█████████████████▋ | 20.6M/43.1M [00:01<00:01, 22.0MB/s]
53%|███████████████████▋ | 22.9M/43.1M [00:01<00:00, 22.2MB/s]
59%|█████████████████████▋ | 25.3M/43.1M [00:01<00:00, 22.5MB/s]
64%|███████████████████████▋ | 27.6M/43.1M [00:01<00:00, 22.6MB/s]
70%|█████████████████████████▊ | 30.0M/43.1M [00:01<00:00, 22.8MB/s]
75%|███████████████████████████▊ | 32.4M/43.1M [00:02<00:00, 22.8MB/s]
81%|█████████████████████████████▊ | 34.8M/43.1M [00:02<00:00, 23.0MB/s]
86%|███████████████████████████████▊ | 37.1M/43.1M [00:02<00:00, 22.6MB/s]
91%|█████████████████████████████████▊ | 39.3M/43.1M [00:02<00:00, 18.5MB/s]
97%|███████████████████████████████████▊ | 41.7M/43.1M [00:02<00:00, 19.8MB/s]
0%| | 0.00/43.1M [00:00<?, ?B/s]
100%|██████████████████████████████████████| 43.1M/43.1M [00:00<00:00, 127GB/s]
/home/runner/work/moabb/moabb/.venv/lib/python3.10/site-packages/urllib3/connectionpool.py:1064: InsecureRequestWarning: Unverified HTTPS request is being made to host 'lampx.tugraz.at'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
0%| | 0.00/44.2M [00:00<?, ?B/s]
0%| | 8.19k/44.2M [00:00<09:05, 81.1kB/s]
0%| | 56.3k/44.2M [00:00<02:22, 311kB/s]
0%|▏ | 153k/44.2M [00:00<01:13, 598kB/s]
1%|▎ | 344k/44.2M [00:00<00:39, 1.10MB/s]
2%|▋ | 736k/44.2M [00:00<00:20, 2.08MB/s]
3%|█▎ | 1.51M/44.2M [00:00<00:10, 3.94MB/s]
7%|██▌ | 3.06M/44.2M [00:00<00:05, 7.57MB/s]
12%|████▍ | 5.32M/44.2M [00:00<00:03, 12.2MB/s]
17%|██████▍ | 7.69M/44.2M [00:00<00:02, 15.6MB/s]
23%|████████▌ | 10.2M/44.2M [00:01<00:01, 18.3MB/s]
29%|██████████▋ | 12.8M/44.2M [00:01<00:01, 20.5MB/s]
35%|████████████▊ | 15.4M/44.2M [00:01<00:01, 21.7MB/s]
41%|███████████████ | 17.9M/44.2M [00:01<00:01, 22.7MB/s]
46%|█████████████████▏ | 20.5M/44.2M [00:01<00:01, 23.4MB/s]
52%|███████████████████▏ | 22.9M/44.2M [00:01<00:00, 23.0MB/s]
57%|█████████████████████ | 25.2M/44.2M [00:01<00:00, 22.8MB/s]
62%|██████████████████████▉ | 27.5M/44.2M [00:01<00:00, 22.5MB/s]
67%|████████████████████████▊ | 29.7M/44.2M [00:01<00:00, 22.3MB/s]
72%|██████████████████████████▋ | 31.9M/44.2M [00:01<00:00, 22.1MB/s]
77%|████████████████████████████▋ | 34.2M/44.2M [00:02<00:00, 22.1MB/s]
83%|██████████████████████████████▋ | 36.7M/44.2M [00:02<00:00, 22.7MB/s]
89%|████████████████████████████████▊ | 39.3M/44.2M [00:02<00:00, 23.2MB/s]
95%|██████████████████████████████████▉ | 41.8M/44.2M [00:02<00:00, 23.6MB/s]
0%| | 0.00/44.2M [00:00<?, ?B/s]
100%|██████████████████████████████████████| 44.2M/44.2M [00:00<00:00, 135GB/s]
BNCI2014-001-CrossSession: 100%|██████████| 2/2 [00:24<00:00, 11.88s/it]
BNCI2014-001-CrossSession: 100%|██████████| 2/2 [00:24<00:00, 12.03s/it]
2025-10-06 20:22:42,888 INFO MainThread moabb.evaluations.base AM+LDA | BNCI2014-001 | 1 | 0train: Score 0.786
2025-10-06 20:22:43,004 INFO MainThread moabb.evaluations.base AM+LDA | BNCI2014-001 | 1 | 1test: Score 0.802
2025-10-06 20:22:43,118 INFO MainThread moabb.evaluations.base AM+SVM | BNCI2014-001 | 1 | 0train: Score 0.797
2025-10-06 20:22:43,250 INFO MainThread moabb.evaluations.base AM+SVM | BNCI2014-001 | 1 | 1test: Score 0.774
2025-10-06 20:22:43,379 INFO MainThread moabb.evaluations.base AM+LDA | BNCI2014-001 | 2 | 0train: Score 0.577
2025-10-06 20:22:43,491 INFO MainThread moabb.evaluations.base AM+LDA | BNCI2014-001 | 2 | 1test: Score 0.499
2025-10-06 20:22:43,606 INFO MainThread moabb.evaluations.base AM+SVM | BNCI2014-001 | 2 | 0train: Score 0.551
2025-10-06 20:22:43,732 INFO MainThread moabb.evaluations.base AM+SVM | BNCI2014-001 | 2 | 1test: Score 0.471
Results are returned as a pandas DataFrame, and from here you can do as you want with them
print(results.head())
score time samples ... n_sessions dataset pipeline
0 0.797068 0.250668 144.0 ... 2 BNCI2014-001 AM+SVM
1 0.773920 0.152906 144.0 ... 2 BNCI2014-001 AM+SVM
2 0.550733 0.247473 144.0 ... 2 BNCI2014-001 AM+SVM
3 0.471451 0.161849 144.0 ... 2 BNCI2014-001 AM+SVM
4 0.786458 0.062732 144.0 ... 2 BNCI2014-001 AM+LDA
[5 rows x 9 columns]
Total running time of the script: (0 minutes 26.381 seconds)
Estimated memory usage: 800 MB