# wajtsq/brain4fms architecture

> Architecture diagram of the GitHub repository wajtsq/brain4fms, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wajtsq/brain4fms
- Repository: https://github.com/wajtsq/brain4fms

## Overview

Brain4FMs benchmarks self-supervised brain foundation models on EEG/iEEG. The main workflow prepares dataset-specific signals, loads standardized dataset/model adapters and pretrained checkpoints, then runs finetuning, testing, few-shot, or prototype evaluation. Its major branches include a broad model catalogue, dataset preprocessing and metadata, shared training/evaluation pipelines, and agent-assisted model/dataset integration. Source excerpts show selected preprocessing, adapter, and evaluation behavior; several branches are linked only where the README documents the relationship.

Main entry points: [`model`](https://github.com/wajtsq/brain4fms/tree/main/model) (Model adapters), [`pretrained_run.py`](https://github.com/wajtsq/brain4fms/blob/main/pretrained_run.py) (Run orchestration), [`pipeline/eval_epoch.py`](https://github.com/wajtsq/brain4fms/blob/main/pipeline/eval_epoch.py) (Evaluation epoch), [`update_skill`](https://github.com/wajtsq/brain4fms/tree/main/update_skill) (Agent-assisted integration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_prep["Data preparation"]
  node_preprocess["Dataset preprocessors"]
  node_signal_utils["Signal utilities<br/>[utils.py]"]
  node_cogitate["Cogitate iEEG<br/>[preprocess.py]"]
  node_swec["SWEC iEEG<br/>[preprocess.py]"]
end

subgraph group_data["Dataset interfaces"]
  node_prepared["Prepared signal groups"]
  node_dataset_meta["Dataset metadata<br/>[data_info.py]"]
  node_dataset_adapters["Dataset adapters"]
  node_neurolm_data["NeuroLM data adapter<br/>[NeuroLM_dataset.py]"]
end

subgraph group_models["Model catalogue"]
  node_model_config["Model configuration<br/>[model_config.py]"]
  node_model_adapters["Model adapters"]
  node_bendr["BENDR model<br/>[Bendr.py]"]
  node_brainbert["BrainBERT model<br/>[BrainBERT.py]"]
  node_mbrain["MBrain model<br/>[ssl_model.py]"]
  node_neurolm["NeuroLM model<br/>[model.py]"]
end

subgraph group_runtime["Benchmark runtime"]
  node_benchmark["Run orchestration<br/>[pretrained_run.py]"]
  node_evaluation["Evaluation epoch<br/>[eval_epoch.py]"]
  node_training["Training epochs<br/>[train_epoch.py]"]
  node_prototype["Prototype evaluation<br/>[prototype_epoch.py]"]
  node_metrics["Metrics and utilities<br/>[metrics.py]"]
end

subgraph group_extend["Integration tools"]
  node_integration["Agent-assisted integration"]
  node_dataset_updater["Dataset updater<br/>[dataset_updater.py]"]
  node_model_updater["Model updater<br/>[model_updater.py]"]
end

node_user(("Experimenter"))

node_user -->|"prepares data"| node_preprocess
node_preprocess -.->|"writes groups"| node_prepared
node_cogitate -->|"generates groups"| node_prepared
node_swec -->|"saves groups"| node_prepared
node_user -->|"runs experiment"| node_benchmark
node_benchmark -->|"loads checkpoint"| node_model_config
node_benchmark -.->|"runs evaluation"| node_evaluation
node_benchmark -.->|"runs finetuning"| node_training
node_benchmark -.->|"runs prototypes"| node_prototype
node_dataset_meta -->|"selects dataset"| node_dataset_adapters
node_dataset_adapters -.->|"loads prepared data"| node_prepared
node_neurolm_data -.->|"reshapes signal"| node_prepared
node_evaluation -->|"builds loader"| node_dataset_adapters
node_evaluation -->|"updates logs"| node_metrics
node_evaluation -->|"calls model"| node_model_adapters
node_model_config -.->|"configures models"| node_model_adapters
node_model_adapters -.->|"includes"| node_bendr
node_model_adapters -.->|"includes"| node_brainbert
node_model_adapters -.->|"includes"| node_mbrain
node_model_adapters -.->|"includes"| node_neurolm
node_integration -.->|"includes"| node_dataset_updater
node_integration -.->|"includes"| node_model_updater
node_user -->|"extends benchmark"| node_integration
node_dataset_updater -->|"generates adapters"| node_preprocess

click node_preprocess "https://github.com/wajtsq/brain4fms/tree/main/data_preprocess"
click node_signal_utils "https://github.com/wajtsq/brain4fms/blob/main/data_preprocess/utils.py"
click node_cogitate "https://github.com/wajtsq/brain4fms/blob/main/data_preprocess/Cogitate_preprocess/preprocess.py"
click node_swec "https://github.com/wajtsq/brain4fms/blob/main/data_preprocess/SWEC_preprocess/preprocess.py"
click node_dataset_meta "https://github.com/wajtsq/brain4fms/blob/main/utils/data_info.py"
click node_dataset_adapters "https://github.com/wajtsq/brain4fms/tree/main/datasets"
click node_neurolm_data "https://github.com/wajtsq/brain4fms/blob/main/datasets/NeuroLM_dataset.py"
click node_model_config "https://github.com/wajtsq/brain4fms/blob/main/model/model_config.py"
click node_model_adapters "https://github.com/wajtsq/brain4fms/tree/main/model"
click node_bendr "https://github.com/wajtsq/brain4fms/blob/main/model/Bendr/Bendr.py"
click node_brainbert "https://github.com/wajtsq/brain4fms/blob/main/model/BrainBERT/BrainBERT.py"
click node_mbrain "https://github.com/wajtsq/brain4fms/blob/main/model/Mbrain/models/ssl_model.py"
click node_neurolm "https://github.com/wajtsq/brain4fms/blob/main/model/NeuroLM/model/model.py"
click node_benchmark "https://github.com/wajtsq/brain4fms/blob/main/pretrained_run.py"
click node_evaluation "https://github.com/wajtsq/brain4fms/blob/main/pipeline/eval_epoch.py"
click node_training "https://github.com/wajtsq/brain4fms/blob/main/pipeline/train_epoch.py"
click node_prototype "https://github.com/wajtsq/brain4fms/blob/main/pipeline/prototype_epoch.py"
click node_metrics "https://github.com/wajtsq/brain4fms/blob/main/utils/metrics.py"
click node_integration "https://github.com/wajtsq/brain4fms/tree/main/update_skill"
click node_dataset_updater "https://github.com/wajtsq/brain4fms/blob/main/update_skill/dataset_updater.py"
click node_model_updater "https://github.com/wajtsq/brain4fms/blob/main/update_skill/model_updater.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_preprocess,node_signal_utils,node_cogitate,node_swec toneBlue
class node_prepared,node_dataset_meta,node_dataset_adapters,node_neurolm_data toneAmber
class node_model_config,node_model_adapters,node_bendr,node_brainbert,node_mbrain,node_neurolm toneMint
class node_benchmark,node_evaluation,node_training,node_prototype,node_metrics toneRose
class node_integration,node_dataset_updater,node_model_updater,node_user toneIndigo
```

## Components

### Data preparation

- [Dataset preprocessors](https://github.com/wajtsq/brain4fms/tree/main/data_preprocess): `data_preprocess`
- [Signal utilities](https://github.com/wajtsq/brain4fms/blob/main/data_preprocess/utils.py): `data_preprocess/utils.py`
- [Cogitate iEEG](https://github.com/wajtsq/brain4fms/blob/main/data_preprocess/Cogitate_preprocess/preprocess.py): `data_preprocess/Cogitate_preprocess/preprocess.py`
- [SWEC iEEG](https://github.com/wajtsq/brain4fms/blob/main/data_preprocess/SWEC_preprocess/preprocess.py): `data_preprocess/SWEC_preprocess/preprocess.py`

### Dataset interfaces

- **Prepared signal groups**
- [Dataset metadata](https://github.com/wajtsq/brain4fms/blob/main/utils/data_info.py): `utils/data_info.py`
- [Dataset adapters](https://github.com/wajtsq/brain4fms/tree/main/datasets): `datasets`
- [NeuroLM data adapter](https://github.com/wajtsq/brain4fms/blob/main/datasets/NeuroLM_dataset.py): `datasets/NeuroLM_dataset.py`

### Model catalogue

- [Model configuration](https://github.com/wajtsq/brain4fms/blob/main/model/model_config.py): `model/model_config.py`
- [Model adapters](https://github.com/wajtsq/brain4fms/tree/main/model): `model`
- [BENDR model](https://github.com/wajtsq/brain4fms/blob/main/model/Bendr/Bendr.py): `model/Bendr/Bendr.py`
- [BrainBERT model](https://github.com/wajtsq/brain4fms/blob/main/model/BrainBERT/BrainBERT.py): `model/BrainBERT/BrainBERT.py`
- [MBrain model](https://github.com/wajtsq/brain4fms/blob/main/model/Mbrain/models/ssl_model.py): `model/Mbrain/models/ssl_model.py`
- [NeuroLM model](https://github.com/wajtsq/brain4fms/blob/main/model/NeuroLM/model/model.py): `model/NeuroLM/model/model.py`

### Benchmark runtime

- [Run orchestration](https://github.com/wajtsq/brain4fms/blob/main/pretrained_run.py): `pretrained_run.py`
- [Evaluation epoch](https://github.com/wajtsq/brain4fms/blob/main/pipeline/eval_epoch.py): `pipeline/eval_epoch.py`
- [Training epochs](https://github.com/wajtsq/brain4fms/blob/main/pipeline/train_epoch.py): `pipeline/train_epoch.py`
- [Prototype evaluation](https://github.com/wajtsq/brain4fms/blob/main/pipeline/prototype_epoch.py): `pipeline/prototype_epoch.py`
- [Metrics and utilities](https://github.com/wajtsq/brain4fms/blob/main/utils/metrics.py): `utils/metrics.py`

### Integration tools

- [Agent-assisted integration](https://github.com/wajtsq/brain4fms/tree/main/update_skill): `update_skill`
- [Dataset updater](https://github.com/wajtsq/brain4fms/blob/main/update_skill/dataset_updater.py): `update_skill/dataset_updater.py`
- [Model updater](https://github.com/wajtsq/brain4fms/blob/main/update_skill/model_updater.py): `update_skill/model_updater.py`

### Other components

- **Experimenter**

## Connections

- Experimenter → Dataset preprocessors: prepares data
- Dataset preprocessors → Prepared signal groups: writes groups
- Cogitate iEEG → Prepared signal groups: generates groups (evidence: `data_preprocess/Cogitate_preprocess/preprocess.py`)
- SWEC iEEG → Prepared signal groups: saves groups (evidence: `data_preprocess/SWEC_preprocess/preprocess.py`)
- Experimenter → Run orchestration: runs experiment
- Run orchestration → Model configuration: loads checkpoint
- Run orchestration → Evaluation epoch: runs evaluation
- Run orchestration → Training epochs: runs finetuning
- Run orchestration → Prototype evaluation: runs prototypes
- Dataset metadata → Dataset adapters: selects dataset
- Dataset adapters → Prepared signal groups: loads prepared data
- NeuroLM data adapter → Prepared signal groups: reshapes signal (evidence: `datasets/NeuroLM_dataset.py`)
- Evaluation epoch → Dataset adapters: builds loader (evidence: `pipeline/eval_epoch.py`)
- Evaluation epoch → Metrics and utilities: updates logs (evidence: `pipeline/eval_epoch.py`)
- Evaluation epoch → Model adapters: calls model (evidence: `pipeline/eval_epoch.py`)
- Model configuration → Model adapters: configures models (evidence: `model/Bendr/Bendr.py`)
- Model adapters → BENDR model: includes (evidence: `model/Bendr/Bendr.py`)
- Model adapters → BrainBERT model: includes (evidence: `model/BrainBERT/BrainBERT.py`)
- Model adapters → MBrain model: includes (evidence: `model/Mbrain/models/ssl_model.py`)
- Model adapters → NeuroLM model: includes (evidence: `model/NeuroLM/model/model.py`)
- Agent-assisted integration → Dataset updater: includes (evidence: `update_skill/dataset_updater.py`)
- Agent-assisted integration → Model updater: includes
- Experimenter → Agent-assisted integration: extends benchmark
- Dataset updater → Dataset preprocessors: generates adapters (evidence: `update_skill/dataset_updater.py`)
