Braindece API Reference#
Models#
Model zoo availables in braindecode. The models are implemented as
PyTorch
torch.nn.Modules
and can be used for various EEG decoding ways tasks.
All the models have the convention of having the signal related parameters named the same way, following the braindecode’s standards:
n_outputs
: Number of labels or outputs of the model.n_chans
: Number of EEG channels.n_times
: Number of time points of the input window.input_window_seconds
: Length of the input window in seconds.sfreq
: Sampling frequency of the EEG recordings.chs_info
: Information about each individual EEG channel. Refer tomne.Info["chs"]
.
All the models assume that the input data is a 3D tensor of shape
(batch_size, n_chans, n_times)
, and some models also accept a 4D tensor of shape
(batch_size, n_chans, n_times, n_epochs)
, in case of cropped model.
All the models are implemented as subclasses of EEGModuleMixin
, which is a
base class for all EEG models in Braindecode. The EEGModuleMixin
class
provides a common interface for all EEG models and derivate variables names if necessary.
braindecode.models.base
:
|
Mixin class for all EEG models in braindecode. |
braindecode.models
:
|
ATCNet model from Altaheri et al. (2022) [R2ecdb73d6ab9-1]. |
|
AttentionBaseNet from Wimpff M et al. (2023) [R523d6c831d64-Martin2023]. |
|
Braindecode TCN from Gemein, L et al (2020) [Rd56781dc6fcb-gemein2020]. |
|
BIOT from Yang et al. (2023) [R606e26b38fe6-Yang2023]. |
|
Contrast with the World Representation ContraWR from Yang et al (2021) [Ra71465cb6797-Yang2021]. |
|
CTNet from Zhao, W et al (2024) [Rc7f1d6cec70c-ctnet]. |
|
Deep ConvNet model from Schirrmeister et al (2017) [Rb8ef6c2733ce-Schirrmeister2017]. |
|
Sleep staging architecture from Supratak et al. (2017) [R28b528aca953-Supratak2017]. |
|
EEG Conformer from Song et al. (2022) from [Rd6c0fefc356a-song2022]. |
|
EEG Inception for ERP-based from Santamaria-Vazquez et al (2020) [R37c4761d4e92-santamaria2020]. |
|
EEG Inception for Motor Imagery, as proposed in Zhang et al. (2021) [Rc36ed781f4f5-1]. |
|
EEG-ITNet from Salami, et al (2022) [R7fe571f46200-Salami2022] |
|
EEGMiner from Ludwig et al (2024) [R66a8789ab6ed-eegminer]. |
|
EEGNet model from Lawhern et al. 2016 from [Rcbe3da2652d4-EEGNet]. |
|
EEGNet v4 model from Lawhern et al. (2018) [Rbd7837e27d7f-EEGNet4]. |
|
EEGNeX model from Chen et al. (2024) [R60f8df15fd80-eegnex]. |
|
EEGResNet from Schirrmeister et al. 2017 [R625b4a01fbf7-Schirrmeister2017]. |
|
EEGSimpleConv from Ouahidi, YE et al. (2023) [R5661533ddc63-Yassine2023]. |
|
EEGTCNet model from Ingolfsson et al. (2020) [Rc25b3d8a3a40-ingolfsson2020]. |
|
FBCNet from Mane, R et al (2021) [R9769c9f8e3f7-fbcnet2021]. |
|
LightConvNet from Ma, X et al (2023) [R501137d6e8c9-lightconvnet]. |
|
FBMSNet from Liu et al (2022) [Re7850041dabd-fbmsnet]. |
|
IFNetV2 from Wang J et al (2023) [Rd9f3b242e751-ifnet]. |
|
Labram from Jiang, W B et al (2024) [Rb5cdfc6ea4fe-Jiang2024]. |
|
MSVTNet model from Liu K et al (2024) from [R0733e66fed6d-msvt2024]. |
|
SCCNet from Wei, C S (2019) [Rbd95e5cdbbde-sccnet]. |
|
Shallow ConvNet model from Schirrmeister et al (2017) [R9432a19f6121-Schirrmeister2017]. |
|
Architecture introduced in signal-JEPA for self-supervised pre-training, Guetschel, P et al (2024) [R62b31c4b9b52-1] |
|
Contextual downstream architecture introduced in signal-JEPA Guetschel, P et al (2024) [Rb25bb9e753a8-1]. |
|
Post-local downstream architecture introduced in signal-JEPA Guetschel, P et al (2024) [R29e8e87440e5-1]. |
|
Pre-local downstream architecture introduced in signal-JEPA Guetschel, P et al (2024) [R795e75e58da6-1]. |
|
Sinc-ShallowNet from Borra, D et al (2020) [R4fd1ba6a7153-borra2020]. |
|
Sleep staging architecture from Blanco et al. (2020) from [Rb3eee9d9e81a-Blanco2020]. |
|
Sleep staging architecture from Chambon et al. (2018) [R89163c5eab6a-Chambon2018]. |
|
Sleep Staging Architecture from Eldele et al. (2021) [R63c3502458ce-Eldele2021]. |
|
Seizures, Periodic and Rhythmic pattern Continuum Neural Network (SPaRCNet) from Jing et al. (2023) [Rf8eed20f8ca2-jing2023]. |
|
Synchronization Network (SyncNet) from Li, Y et al (2017) [R5cdbe961b734-Li2017]. |
|
Thinker Invariance DenseNet model from Kostas et al. (2020) [Re74dd80418c9-TIDNet]. |
|
TSception model from Ding et al. (2020) from [R422d465e7195-ding2020]. |
|
Sleep staging architecture from Perslev et al. (2021) [R58a5e8182f0a-1]. |
Modules#
braindecode.modules
:
This module contains the building blocks for Braindecode models. It contains activation functions, convolutional layers, attention mechanisms, filter banks, and other utilities.
Activation#
These modules wrap specialized activation functions—e.g., safe logarithms for numerical stability.
braindecode.modules.activation
:
|
Logarithm activation function. |
|
Safe logarithm activation function module. |
Attention#
These modules implement various attention mechanisms, including multi’head attention and squeeze and excitation layers.
braindecode.modules.attention
:
|
Attention Mechanism from [R4b3f3c7c2fe2-Wu2023]. |
|
Convolutional Block Attention Module from [R8389a3af090c-Woo2018]. |
|
Efficient Channel Attention [R18399df6eede-Wang2021]. |
|
Frequency Channel Attention Networks from [R7a6c40ff4f0b-Qin2021]. |
|
Gated Channel Transformation from [R977e9a24fcb7-Yang2020]. |
|
Attention module from [R94e1b7c716d1-Lee2019]. |
|
Modification of CAT without the convolutional layer from [R1d30135f2545-Wu2023]. |
|
Context Encoding for Semantic Segmentation from [R13609999c674-Zhang2018]. |
|
Gather-Excite Networks from [Re3d24ebdda8b-Hu2018b]. |
|
Global Second-order Pooling Convolutional Networks from [Reb98cd67024f-Gao2018]. |
|
|
|
Squeeze-and-Excitation Networks from [R0b6baaf6da0f-Hu2018]. |
Blocks#
These modules are specialized building blocks for neural networks, including multi’layer perceptrons (MLPs) and inception blocks.
braindecode.modules.blocks
:
|
Multilayer Perceptron (MLP) with GELU activation and optional dropout. |
|
|
|
Inception block module. |
Convolution#
These modules implement constraints convolutional layers, including depthwise convolutions and causal convolutions. They also include convolutional layers with constraints and pooling layers.
braindecode.modules.convolution
:
|
Compute average pooling using a convolution, to have the dilation parameter. |
|
Causal 1-dimensional convolution |
|
Merged convolutional layer for temporal and spatial convs in Deep4/ShallowFBCSP |
|
|
|
Depthwise convolution layer. |
Filter#
These modules implement Filter Bank as Layer and generalizer Gaussian layer.
braindecode.modules.filter
:
|
Apply multiple band-pass filters to generate multiview signal representation. |
|
Generalized Gaussian Filter from Ludwig et al (2024) [Raf65b68c9f5f-eegminer]. |
Layers#
These modules implement various types of layers, including dropout layers, normalization layers, and time’distributed layers. They also include layers for handling different input shapes and dimensions.
braindecode.modules.layers
:
|
|
|
Drop paths, also known as Stochastic Depth, per sample. |
|
|
|
Apply module on multiple windows. |
Linear#
These modules implement linear layers with various constraints and initializations. They include linear layers with max’norm constraints and linear layers with specific initializations.
braindecode.modules.linear
:
|
Linear layer with max-norm constraint on the weights. |
|
Linear layer with MaxNorm constraining on weights. |
Stats#
These modules implement statistical layers, including layers for calculating the mean, standard deviation, and variance of input data. They also include layers for calculating the log power and log variance of input data. Mostly used on FilterBank models.
braindecode.modules.stats
:
|
Generic layer to compute a statistical function along a specified dimension. |
Generic layer to compute a statistical function along a specified dimension. |
|
Generic layer to compute a statistical function along a specified dimension. |
|
Generic layer to compute a statistical function along a specified dimension. |
|
Generic layer to compute a statistical function along a specified dimension. |
|
Generic layer to compute a statistical function along a specified dimension. |
|
Generic layer to compute a statistical function along a specified dimension. |
Utilities#
These modules implement various utility functions and classes for change to cropped model.
braindecode.modules.util
:
|
Aggregate predicted probabilities with self-ensembling. |
Wrappers#
These modules implement wrappers for various types of models, including wrappers for models with multiple outputs and wrappers for models with intermediate outputs.
braindecode.modules.wrapper
:
|
Compute given expression on forward pass. |
|
Wraps network model such that outputs of intermediate layers can be returned. |
Functional#
braindecode.functional
:
The functional module contains various functions that can be used like functional API.
|
Drop paths (Stochastic Depth) per sample. |
|
Initialize parameters of all modules by initializing weights with glorot uniform/xavier initialization, and setting biases to zero. Weights from batch norm layers are set to 1. |
|
Compute the Hilbert transform using PyTorch, separating the real and imaginary parts. |
|
|
|
Compute the Phase Locking Value (PLV) metric in the time domain. |
|
Recaling the l-th transformer layer. |
|
Prevents |
|
Datasets#
braindecode.datasets
:
Pytorch Datasets structure for common EEG datasets, and function to create the dataset from several different data formats. The options available are: Numpy Arrays, MNE Raw and MNE Epochs.
Base classes#
|
A base class for concatenated datasets. |
|
Returns samples from an mne.io.Raw object along with a target. |
|
Returns windows from an mne.Epochs object along with a target. |
|
Dataset for loading BIDS. |
|
Experimental dataset for loading |
Common Datasets#
|
BCI competition IV dataset 4. |
|
BNCI 2014-001 Motor Imagery dataset. |
|
High-gamma dataset described in Schirrmeister et al. 2017. |
|
A class for moabb datasets. |
|
The NMT Scalp EEG Dataset. |
|
Sleep Physionet dataset. |
|
Physionet Challenge 2018 polysomnography dataset. |
|
Temple University Hospital (TUH) EEG Corpus (www.isip.piconepress.com/projects/tuh_eeg/html/downloads.shtml#c_tueg). |
|
Temple University Hospital (TUH) Abnormal EEG Corpus. |
Dataset Builders Functions#
Functions to create datasets from different data formats
|
Create a BaseConcatDataset of WindowsDatasets from X and y to be used for decoding with skorch and braindecode, where X is a list of pre-cut trials and y are corresponding targets. |
|
Create WindowsDatasets from mne.RawArrays |
|
Create WindowsDatasets from mne.Epochs |
Preprocessing#
braindecode.preprocessing
:
|
Create windows based on events in mne.Raw. |
|
Windower that creates sliding windows. |
|
|
|
Perform exponential moving demeanining. |
|
Perform exponential moving standardization. |
|
Applies multiple bandpass filters to the signals in raw. |
|
Apply preprocessors to a concat dataset. |
|
Preprocessor for an MNE Raw or Epochs object. |
|
Resample an array. |
|
Drop channel(s). |
|
Specify which reference to use for EEG data. |
|
Filter a subset of channels/vertices. |
|
Pick a subset of channels. |
|
Crop raw data file. |
Data Utils#
braindecode.datautil
:
|
|
|
Load a stored BaseConcatDataset of BaseDatasets or WindowsDatasets from files. |
Samplers#
Samplers that can used to sample EEG data for training and testing and to create batches of data, used on Self’Supervised Learning and other tasks.
braindecode.samplers
:
|
Base sampler simplifying sampling from recordings. |
|
Base sampler simplifying sampling from recordings in distributed setting. |
|
Sample sequences of consecutive windows. |
|
Sample examples for the relative positioning task from [R0467437a2408-Banville2020]. |
|
Sample examples for the relative positioning task from [Rc4a232d8b33d-Banville2020] in distributed mode. |
|
Balanced sampling of sequences of consecutive windows with categorical targets. |
Augmentation#
The augmentation module follow the pytorch transforms API. It contains transformations that can be applied to EEG data. The transformations can be used to augment the data during training, which can help improve the performance of the model. The transformations can be applied to the data in a variety of ways, including time’domain transformations, frequency’domain transformations, and spatial transformations.
braindecode.augmentation
:
|
Basic transform class used for implementing data augmentation operations. |
|
Identity transform. |
|
Transform composition. |
|
A base dataloader class customized to applying augmentation Transforms. |
|
Flip the time axis of each input with a given probability. |
|
Flip the sign axis of each input with a given probability. |
|
FT surrogate augmentation of a single EEG channel, as proposed in [Ra7c6c14d9bd9-1]. |
|
Randomly shuffle channels in EEG data matrix. |
|
Randomly set channels to flat signal. |
|
Randomly add white noise to all channels. |
|
Permute EEG channels inverting left and right-side sensors. |
|
Smoothly replace a randomly chosen contiguous part of all channels by zeros. |
|
Apply a band-stop filter with desired bandwidth at a randomly selected frequency position between 0 and |
|
Add a random shift in the frequency domain to all channels. |
|
Interpolates EEG signals over sensors rotated around the desired axis with an angle sampled uniformly between |
|
Interpolates EEG signals over sensors rotated around the Z axis with an angle sampled uniformly between |
|
Interpolates EEG signals over sensors rotated around the Y axis with an angle sampled uniformly between |
|
Interpolates EEG signals over sensors rotated around the X axis with an angle sampled uniformly between |
|
Implements Iterator for Mixup for EEG data. |
|
Segmentation Reconstruction from Lotte (2015) [R78e7a66c7d6f-Lotte2015]. |
|
MaskEncoding from [R9102599ed233-1]. |
The functional augmentation API contains the same transformations as the transforms API, but they are implemented as functions.
braindecode.augmentation.functional
:
|
Identity operation. |
|
Flip the time axis of each input. |
|
Flip the sign axis of each input. |
|
FT surrogate augmentation of a single EEG channel, as proposed in [R52a4658fffa7-1]. |
|
Randomly set channels to flat signal. |
|
Randomly shuffle channels in EEG data matrix. |
|
Permute EEG channels according to fixed permutation matrix. |
|
Randomly add white Gaussian noise to all channels. |
|
Smoothly replace a contiguous part of all channels by zeros. |
|
Apply a band-stop filter with desired bandwidth at the desired frequency position. |
|
Adds a shift in the frequency domain to all channels. |
|
Interpolates EEG signals over sensors rotated around the desired axis with the desired angle. |
|
Mixes two channels of EEG data. |
|
Segment and reconstruct EEG data from [Rc19448ba78ac-1]. |
|
Mark encoding from Ding et al. (2024) from [Re49696d5b28b-ding2024]. |
Classifier#
Skorch wrapper for braindecode models. The skorch wrapper allows to use braindecode models with scikit’learn API.
braindecode.classifier
:
|
Classifier that does not assume softmax activation. |
Regressor#
Skorch wrapper for braindecode models focus on regression tasks. The skorch wrapper allows to use braindecode models with scikit’learn API.
braindecode.regressor
:
|
Regressor that calls loss function directly. |
Training#
Training module contains functions and classes for training and evaluating EEG models. It is inside the Classifier and Regressor skorch classes, and it is used to train the models and evaluate their performance.
braindecode.training
:
|
Compute Loss after averaging predictions across time. |
|
Compute Loss between timeseries targets and predictions. |
|
Class to compute scores for trials from a model that predicts (super)crops. |
|
Class to compute scores for trials from a model that predicts (super)crops with time series target. |
|
Epoch Scoring class that recomputes predictions after the epoch on the training in validation mode. |
|
Implements loss for Mixup for EEG data. |
|
Assigning window predictions to trials while removing duplicate predictions. |
|
Create trialwise predictions and optionally also return trialwise labels from cropped dataset given module. |
Utils#
Functions available in braindecode util module.
braindecode.util
:
|
Set seeds for python random module numpy.random and torch. |
Visualization#
Visualization module contains functions for visualizing EEG data, including plotting the confusion matrix and computing amplitude gradients. The visualization module is useful for understanding the performance of the model and for interpreting the results.
braindecode.visualization
:
|
|
|
Generates a confusion matrix with additional precision and sensitivity metrics as in [R8046536b33dd-1]. |