# bine022/eegpt architecture

> Architecture diagram of the GitHub repository bine022/eegpt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bine022/eegpt
- Repository: https://github.com/bine022/eegpt

## Overview

##### Main Components
- Data Preparation and Datasets:
– Represent the datasets folder split into two parts:
- Pretrain datasets: “datasets/pretrain”, showing the script (prepare_pretrain_dataset.py) and its README.
- Downstream datasets: “datasets/downstream”, with corresponding preparation scripts and README.
– Include preprocessing for additional EEG datasets (TUAB, TUEV) from “downstream_tueg” components.
- Pretraining Pipeline:
– Show the “pretrain” directory that contains configuration (configs.py), model definition (modeling_pretraining.py), engine (engine_pretraining.py), and the run script (run_pretraining.py).
– Indicate that this module implements the mask-based dual self-supervised learning with spatio-temporal representation alignment.
- Downstream Task Pipelines:
– Group the various downstream scripts (e.g., finetuning and linear probing scripts like finetune_BENDR_PhysioP300.py, linear_probe_EEGPT_BCIC2A.py) from the “downstream” directory.
– Highlight the downstream data processing submodule (“downstream/Data_process”) that handles EEG data reading, transformation, and utility functions.
- Modular Models and Extensions:
– Indicate the “downstream/Modules” folder structured into various submodules:
- Specific model implementations (EEGPT_mcae*, dn3 extensions, LaBraM, BIOT, etc.).
- The “dn3” submodules: This contains configuration, training utilities, data pipelines, metrics, and transformation utilities forming an encapsulated framework.
- The “Transformers” directory for supplementary transformer-based components (e.g. positional embeddings).
- External Dependencies / Pretrained Models:
– Clearly show how these checkpoints feed into downstream modules (for example, the checkpoint used with EEGPTClassifier).
- Downstream TUAB/TUEV:
– Note that it includes its own dataset maker scripts, network modules, and dedicated finetuning scripts.

##### Relationships and Interactions
– From “Datasets” (various EEG data sources) → Data preprocessing modules (in “datasets” and “downstream/Data_process”).
– Processed EEG data → Pretraining Pipeline: Feed into transformer-based self-supervised training.
– Output pretrained transformer model (saved as a checkpoint) → Downstream fine-tuning modules (various finetuning scripts and modules in “downstream/Modules/models”).
– Alternate flow from data → Downstream tasks directly, if “linear_probe” scripts are executed.
- Interactions between modular components:
– Downstream modules interact with the pre-trained weights (model loading in EEGPTClassifier) and with additional models (BENDR, BIOT, LaBraM).
- External integration points for pretrained model files (illustrate external storage sources feeding into the system’s “checkpoint” and model directories).

##### Architectural Patterns and Design Principles
- Modularity: The project is separated into distinct folders for data preparation, pretraining, and downstream tasks. Within each, components are modularized (e.g., separating data process, training configurations, model definitions, etc.).
- Pipeline-based design: There is a clear preparation → pretraining → finetuning pipeline.
- Plugin for multiple models: The use of directories such as “BIOT”, “LaBraM”, and “EEGPT” indicates a design meant to handle several variations of transformer-based models.
- Clear separation of concerns: The pretraining is isolated from downstream tasks, and both have their dedicated configuration and execution scripts.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Preparation and Datasets
    subgraph "1. Data Preparation and Datasets"
        pretrain_data["Pretrain: datasets/pretrain<br>prepare_pretrain_dataset.py, readme.md"]:::data
        downstream_data["Downstream: datasets/downstream<br>prepare_PhysioNetP300.py, prepare_sleep.py, readme.md"]:::data
        process_data["Data Process: downstream/Data_process<br>EEG_Dataset.py, LoadData.py, process_function.py, utils.py"]:::data
    end

    %% Pretraining Pipeline
    subgraph "2. Pretraining Pipeline"
        pretrain_module["Pretrain Module: pretrain<br>configs.py, engine_pretraining.py, modeling_pretraining.py, run_pretraining.py, utils.py"]:::pretrain
    end

    %% Downstream Task Pipelines
    subgraph "3. Downstream Task Pipelines"
        downstream_scripts["Downstream Scripts: downstream<br>finetune_BENDR_PhysioP300.py, linear_probe_EEGPT_BCIC2A.py, ..."]:::downstream
    end

    %% Modular Models and Extensions
    subgraph "4. Modular Models and Extensions"
        eegpt_models["EEGPT Models: downstream/Modules/models<br>EEGPT_mcae.py, EEGPT_mcae_finetune.py, dn3_ext.py"]:::modular
        biot_module["BIOT Module: downstream/Modules/BIOT<br>biot.py"]:::modular
        labram_module["LaBraM Module: downstream/Modules/LaBraM<br>modeling_finetune.py"]:::modular
        dn3_modules["DN3 Submodules: downstream/Modules/dn3<br>(configuratron, data, metrics, trainable, transforms, utils.py)"]:::modular
        transformers["Transformers Components: downstream/Modules/Transformers<br>pos_embed.py"]:::modular
        network_utils["Network Utilities: downstream/Modules/Network<br>utils.py"]:::modular
    end

    %% External Pretrained Models
    subgraph "5. External Dependencies / Pretrained Models"
        external_models["Pretrained Models"]:::external
    end

    %% Downstream TUAB/TUEV
    subgraph "6. Downstream TUAB/TUEV"
        tuab_tuev["Downstream TUAB/TUEV: downstream_tueg<br>(make_TUAB.py, make_TUEV.py, finetune_TUAB_EEGPT.sh, finetune_TUEV_EEGPT.sh, optim_factory.py, utils.py)"]:::downstream
    end

    %% Relationships and Data Flow
    pretrain_data -->|"feeds"| pretrain_module
    downstream_data -->|"feeds"| downstream_scripts
    process_data -->|"feeds"| downstream_scripts
    process_data -->|"feeds"| tuab_tuev

    pretrain_module -->|"checkpoint"| downstream_scripts
    pretrain_module -->|"checkpoint"| tuab_tuev

    downstream_scripts -->|"integrates"| eegpt_models
    downstream_scripts -->|"integrates"| biot_module
    downstream_scripts -->|"integrates"| labram_module
    downstream_scripts -->|"integrates"| dn3_modules
    downstream_scripts -->|"integrates"| transformers
    downstream_scripts -->|"integrates"| network_utils

    external_models -->|"supplies"| eegpt_models

    %% Styling Classes
    classDef data fill:#ffccff,stroke:#333,stroke-width:2px;
    classDef pretrain fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef downstream fill:#ccccff,stroke:#333,stroke-width:2px;
    classDef modular fill:#ffffcc,stroke:#333,stroke-width:2px;
    classDef external fill:#ccffff,stroke:#333,stroke-width:2px;

    %% Click Events
    click pretrain_data "https://github.com/bine022/eegpt/tree/main/datasets/pretrain"
    click downstream_data "https://github.com/bine022/eegpt/tree/main/datasets/downstream"
    click process_data "https://github.com/bine022/eegpt/tree/main/downstream/Data_process"
    click pretrain_module "https://github.com/bine022/eegpt/tree/main/pretrain"
    click downstream_scripts "https://github.com/bine022/eegpt/tree/main/downstream"
    click eegpt_models "https://github.com/bine022/eegpt/tree/main/downstream/Modules/models"
    click biot_module "https://github.com/bine022/eegpt/tree/main/downstream/Modules/BIOT"
    click labram_module "https://github.com/bine022/eegpt/tree/main/downstream/Modules/LaBraM"
    click dn3_modules "https://github.com/bine022/eegpt/tree/main/downstream/Modules/dn3"
    click transformers "https://github.com/bine022/eegpt/tree/main/downstream/Modules/Transformers"
    click network_utils "https://github.com/bine022/eegpt/tree/main/downstream/Modules/Network"
    click tuab_tuev "https://github.com/bine022/eegpt/tree/main/downstream_tueg"
```
