MOABB (Mother of All BCI Benchmarks)

repository·develop·Indexed 21 days ago

https://github.com/neurotechx/moabb

A framework for building comprehensive, reproducible benchmarks for Brain-Computer Interface (BCI) algorithms using freely available EEG datasets. MOABB provides a unified interface for accessing datasets, defining paradigms (such as Motor Imagery, P300, SSVEP, and c-VEP), executing evaluations (within-session, cross-session, and cross-subject), and building scikit-learn compatible pipelines. It includes tools for statistical meta-analysis, data visualization, and a high-level benchmark module to automate the end-to-end workflow.

Tokens
12K
Snippets
38
Records
66
Agent score
76%

What's inside moabb

  1. Perform cross-subject transfer learning with CrossSubjectEvaluation

    develop

    MOABB 1.6 introduces support for cross-subject transfer learning via moabb.evaluations.CrossSubjectEvaluation. This is achieved using an optional target-calibration slice provided by the CrossSubjectSplitter.

    Key Parameters

    • calibration_size: A float in [0, 1] representing the fraction of each held-out subject/session pair set aside for adaptation. When calibration_size > 0, each fold is structured as (train, calibration, test).
    • calibration_labeled: A boolean. If True, the calibration set includes both X_target_labeled and y_target_labeled.
    • cs_mode: An argument using the moabb.evaluations.CrossSubjectMode enum to select predefined modes:
      • TRAIN_ONLY: No adaptation.
      • Unlabeled target adaptation at 20%, 50%, or 100%.
      • Labeled target calibration at 20% or 50%.
      • TRAIN_TRIALWISE: Scores one target trial at a time (prevents exploiting whole-block statistics).

    Data Flow

    Calibration trials are trial-disjoint from the training and test sets. Pipeline steps can opt-in to receive calibration data via set_fit_request using keys like X_target_unlabeled, X_target_labeled, y_target_labeled, or the subjects array.

    # Example conceptual usage of CrossSubjectEvaluation with calibration
    from moabb.evaluations import CrossSubjectEvaluation, CrossSubjectSplitter, CrossSubjectMode
    
    evaluation = CrossSubjectEvaluation(
        splitter=CrossSubjectSplitter(calibration_size=0.2, calibration_labeled=True),
        cs_mode=CrossSubjectMode.UNLABELED_20_PERCENT
    )
  2. Understand paradigm-specific definitions in MOABB

    develop

    Different BCI paradigms use different definitions for 'trials' and 'classes'. When analyzing dataset summaries, keep these definitions in mind:

    • Imagery (Motor/Speech): A Trial is one repetition of the task. Classes are the different imagery tasks (e.g., left hand vs right hand).
    • P300/ERP: A Trial is one flash. Classes are binary: target (the subject focused on the flashed key) or non-target.
    • SSVEP: A Trial is one symbol selection (which may include multiple flashes). Classes are the different stimulation frequencies.
    • c-VEP: A Trial is one symbol selection. Trial classes are the different symbols. Epoch classes are the possible intensities of the flashes (e.g., on/off).
  3. Understand the core concepts of MOABB

    develop

    MOABB is organized around four primary abstractions that define the BCI benchmarking workflow:

    1. Datasets: Abstract low-level data access. They handle downloading, local storage, and conversion of raw data into MNE raw objects. They support pooling recording sessions per subject or evaluating them separately.
    2. Paradigms: Define how raw data is converted into trials ready for decoding. The paradigm determines the necessary preprocessing and how data is structured (e.g., motor imagery vs. ERP).
    3. Evaluations: Define how to calculate generalization statistics (like AUC, f-score, or accuracy) from trials. Evaluations specify the splitting strategy, such as within-session, across-session, or across-subject accuracy.
    4. Pipelines: Define the sequence of steps required to obtain predictions. Pipelines are typically chains of scikit-learn compatible transformers ending with a scikit-learn compatible estimator.

    Additionally, MOABB provides statistical tools, visualization utilities, and a benchmark module that wraps these steps into a single function for quick execution.

  4. Perform benchmarking with Evaluations

    develop

    Evaluations in moabb.evaluations determine how performance metrics (AUC, accuracy, etc.) are calculated across different data splits.

    Key evaluation types include:

    • WithinSessionEvaluation: Accuracy within a single recording session.
    • CrossSessionEvaluation: Accuracy across different sessions of the same subject.
    • CrossSubjectEvaluation: Accuracy across different subjects (transfer learning).

    Evaluations use Splitters to manage data partitioning:

    • WithinSessionSplitter
    • WithinSubjectSplitter
    • CrossSessionSplitter
    • CrossSubjectSplitter

    For cross-subject protocols, you can use CrossSubjectMode to define what the estimator is allowed to see of the held-out target subject.

  5. Define BCI tasks using Paradigms

    develop

    A paradigm in moabb.paradigms dictates how raw data is transformed into usable trials.

    Common paradigm types include:

    • Imagery Paradigms: Covers motor imagery and imagined speech.
      • MotorImagery: The standard class for motor imagery.
      • SpeechImagery: A specialized class for imagined speech using a broadband 1-100 Hz filter.
      • Imagery: A thin alias for MotorImagery.
      • FilterBankMotorImagery: For filter-bank based approaches.
    • P300 Paradigms: For ERP-based tasks like P300.
    • SSVEP Paradigms: For steady-state visual evoked potentials (e.g., SSVEP, FilterBankSSVEP).
    • c-VEP Paradigms: For checkerboard VEPs (e.g., CVEP, FilterBankCVEP).
    • Resting State: Includes RestingStateToP300Adapter.
    • Fixed Interval Windows: For specific temporal windowing (e.g., FixedIntervalWindowsProcessing).
  6. Use Datasets to access EEG data

    develop

    Datasets in moabb.datasets provide a unified interface to various BCI datasets. They abstract the complexity of different file formats and download locations, returning data as MNE raw objects.

    Datasets are categorized by the type of BCI task:

    • Motor Imagery: Includes datasets like AlexMI, BNCI2014_001, PhysionetMI, etc.
    • Imagined Speech: Uses the imagery paradigm tag. Examples include AguileraRodriguez2025 and Nguyen2017_L.
    • ERP/P300: Includes BI2012, BNCI2014_008, and various ErpCore2021 datasets.
    • SSVEP: Includes Kalunga2016, Wang2016, and Lee2019_SSVEP.
    • c-VEP: Includes Thielen2015 and CastillosCVEP40.
    • Resting State: Includes Hinss2021 and Rodrigues2017.

    Specialized CompoundDataset objects (in moabb.datasets.compound_dataset) are also available for specific research setups.

  7. Build decoding workflows with Pipelines

    develop

    Pipelines in moabb.pipelines are chains of scikit-learn compatible transformers and estimators. They represent the complete end-to-end process from processed trials to predictions.

    Common components used in pipelines include:

    • Features: LogVariance, FM, ExtendedSSVEPSignal, StandardScaler_Epoch.
    • Preprocessing/Spatial Filters: TRCSP (Common Spatial Patterns variant).
    • Classifiers: SSVEP_CCA, SSVEP_TRCA, SSVEP_msetCCA, SSVEP_itCCA, SSVEP_eCCA.
  8. Merge datasets using Compound Datasets

    develop

    The moabb.datasets.compound_dataset module allows you to create Compound Datasets. These are datasets composed of subjects from other datasets. This is useful for:

    • Merging multiple different datasets into one.
    • Selecting a specific sample of subjects from a dataset (e.g., only subjects with high performance).
  9. Quickstart: Run a BCI algorithm benchmark

    develop

    This example demonstrates how to set up a basic benchmarking pipeline using MOABB. It involves defining a pipeline (using sklearn), selecting a dataset, defining a paradigm (e.g., frequency bands for imagery), and running a cross-session evaluation.

    import moabb
    from moabb.datasets import BNCI2014_001
    from moabb.evaluations import CrossSessionEvaluation
    from moabb.paradigms import LeftRightImagery
    from moabb.pipelines.features import LogVariance
    
    from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
    from sklearn.pipeline import make_pipeline
    
    # Set logging level
    moabb.set_log_level("info")
    
    # Define pipelines as a dictionary of sklearn-compatible pipelines
    pipelines = {"LogVar+LDA": make_pipeline(LogVariance(), LDA())}
    
    # Initialize dataset and limit subjects for testing
    dataset = BNCI2014_001()
    dataset.subject_list = dataset.subject_list[:2]
    
    # Define the paradigm (e.g., frequency range for Left/Right imagery)
    paradigm = LeftRightImagery(fmin=8, fmax=35)
    
    # Set up and run the evaluation
    evaluation = CrossSessionEvaluation(paradigm=paradigm, datasets=[dataset])
    results = evaluation.process(pipelines)
    
    print(results.head())