# 935963004/labram architecture

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

- Interactive diagram: https://gitdiagram.com/935963004/labram
- Repository: https://github.com/935963004/labram

## Overview

LaBraM is an EEG foundation-model research implementation: it preprocesses EEG into training data, learns a vector-quantized neural tokenizer, pre-trains a masked-code Transformer, and fine-tunes it for downstream classification. The README documents the end-to-end stages and TUAB/TUEV capabilities; the source index additionally identifies the training, model, dataset, and utility modules. Some dataset-maker internals and model wiring were not excerpted, so their relationships are shown only where documented or indexed.

Main entry points: [`run_labram_pretraining.py`](https://github.com/935963004/labram/blob/main/run_labram_pretraining.py) (LaBraM pre-training), [`run_vqnsp_training.py`](https://github.com/935963004/labram/blob/main/run_vqnsp_training.py) (Tokenizer training), [`run_class_finetuning.py`](https://github.com/935963004/labram/blob/main/run_class_finetuning.py) (Fine-tuning entry), [`modeling_vqnsp.py`](https://github.com/935963004/labram/blob/main/modeling_vqnsp.py) (VQ-NSP model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["EEG data preparation"]
  node_preprocess_utils["EEG preprocessing<br/>[eegUtils.py]"]
  node_tuab_prep["TUAB preparation<br/>[make_TUAB.py]"]
  node_tuev_prep["TUEV preparation<br/>[make_TUEV.py]"]
  node_pretrain_prep["Pretrain preparation"]
  node_h5_writer["HDF5 writer<br/>[h5.py]"]
  node_ring_buffer["Ring buffer<br/>[ringBuffer.py]"]
  node_data_processor["Data preprocessing<br/>[data_preprocess.py]"]
  node_dataset["EEG datasets<br/>[dataset.py]"]
end

subgraph group_tokenizer["Neural tokenizer"]
  node_vqnsp_entry["Tokenizer training"]
  node_vqnsp_engine["Tokenizer engine"]
  node_vqnsp_model["VQ-NSP model<br/>[modeling_vqnsp.py]"]
  node_quantizer["EMA quantizer"]
  node_vqnsp_weights["Tokenizer checkpoint<br/>[vqnsp.pth]"]
end

subgraph group_pretrain["Foundation pre-training"]
  node_pretrain_entry["LaBraM pre-training"]
  node_pretrain_engine["Masked-code engine"]
  node_pretrain_model["Pre-training model"]
  node_labram_weights["LaBraM checkpoint<br/>[labram-base.pth]"]
end

subgraph group_finetune["Downstream fine-tuning"]
  node_finetune_entry["Fine-tuning entry"]
  node_finetune_engine["Classification engine"]
  node_finetune_model["Fine-tune model"]
end

subgraph group_shared["Training infrastructure"]
  node_optimizer["Optimizer factory<br/>[optim_factory.py]"]
  node_utilities["Training utilities<br/>[utils.py]"]
end

node_researcher(("Researcher"))
node_raw_eeg["Raw EEG"]

node_researcher -->|"provides"| node_raw_eeg
node_raw_eeg -->|"preprocesses"| node_preprocess_utils
node_preprocess_utils -.->|"prepares signals"| node_pretrain_prep
node_pretrain_prep -->|"writes HDF5"| node_h5_writer
node_researcher -->|"prepares dataset"| node_tuab_prep
node_researcher -->|"prepares dataset"| node_tuev_prep
node_tuab_prep -.->|"prepares samples"| node_data_processor
node_tuev_prep -.->|"prepares samples"| node_data_processor
node_h5_writer -.->|"stores EEG"| node_dataset
node_dataset -.->|"supplies batches"| node_vqnsp_entry
node_researcher -->|"starts training"| node_vqnsp_entry
node_vqnsp_entry -->|"runs epochs"| node_vqnsp_engine
node_vqnsp_entry -->|"builds model"| node_vqnsp_model
node_vqnsp_engine -->|"trains"| node_vqnsp_model
node_vqnsp_model -->|"quantizes features"| node_quantizer
node_vqnsp_entry -->|"creates optimizer"| node_optimizer
node_vqnsp_entry -->|"uses utilities"| node_utilities
node_vqnsp_entry -->|"saves checkpoint"| node_vqnsp_weights
node_researcher -->|"starts pre-training"| node_pretrain_entry
node_pretrain_entry -->|"runs epochs"| node_pretrain_engine
node_pretrain_entry -->|"builds model"| node_pretrain_model
node_pretrain_entry -->|"loads tokenizer"| node_vqnsp_model
node_pretrain_engine -->|"trains"| node_pretrain_model
node_pretrain_engine -->|"gets target codes"| node_vqnsp_model
node_pretrain_entry -->|"creates optimizer"| node_optimizer
node_pretrain_entry -->|"uses utilities"| node_utilities
node_pretrain_entry -->|"loads checkpoint"| node_labram_weights
node_researcher -->|"starts fine-tuning"| node_finetune_entry
node_finetune_entry -->|"runs epochs"| node_finetune_engine
node_finetune_entry -->|"builds model"| node_finetune_model
node_finetune_engine -->|"trains"| node_finetune_model
node_finetune_entry -->|"creates optimizer"| node_optimizer
node_finetune_entry -->|"uses utilities"| node_utilities
node_labram_weights -->|"initializes model"| node_finetune_entry

click node_preprocess_utils "https://github.com/935963004/labram/blob/main/dataset_maker/shock/utils/eegUtils.py"
click node_tuab_prep "https://github.com/935963004/labram/blob/main/dataset_maker/make_TUAB.py"
click node_tuev_prep "https://github.com/935963004/labram/blob/main/dataset_maker/make_TUEV.py"
click node_pretrain_prep "https://github.com/935963004/labram/blob/main/dataset_maker/make_h5dataset_for_pretrain.py"
click node_h5_writer "https://github.com/935963004/labram/blob/main/dataset_maker/shock/utils/h5.py"
click node_ring_buffer "https://github.com/935963004/labram/blob/main/dataset_maker/shock/utils/ringBuffer.py"
click node_data_processor "https://github.com/935963004/labram/blob/main/data_processor/data_preprocess.py"
click node_dataset "https://github.com/935963004/labram/blob/main/data_processor/dataset.py"
click node_vqnsp_entry "https://github.com/935963004/labram/blob/main/run_vqnsp_training.py"
click node_vqnsp_engine "https://github.com/935963004/labram/blob/main/engine_for_vqnsp.py"
click node_vqnsp_model "https://github.com/935963004/labram/blob/main/modeling_vqnsp.py"
click node_quantizer "https://github.com/935963004/labram/blob/main/norm_ema_quantizer.py"
click node_vqnsp_weights "https://github.com/935963004/labram/blob/main/checkpoints/vqnsp.pth"
click node_pretrain_entry "https://github.com/935963004/labram/blob/main/run_labram_pretraining.py"
click node_pretrain_engine "https://github.com/935963004/labram/blob/main/engine_for_pretraining.py"
click node_pretrain_model "https://github.com/935963004/labram/blob/main/modeling_pretrain.py"
click node_labram_weights "https://github.com/935963004/labram/blob/main/checkpoints/labram-base.pth"
click node_finetune_entry "https://github.com/935963004/labram/blob/main/run_class_finetuning.py"
click node_finetune_engine "https://github.com/935963004/labram/blob/main/engine_for_finetuning.py"
click node_finetune_model "https://github.com/935963004/labram/blob/main/modeling_finetune.py"
click node_optimizer "https://github.com/935963004/labram/blob/main/optim_factory.py"
click node_utilities "https://github.com/935963004/labram/blob/main/utils.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_utils,node_tuab_prep,node_tuev_prep,node_pretrain_prep,node_h5_writer,node_ring_buffer,node_data_processor,node_dataset toneBlue
class node_vqnsp_entry,node_vqnsp_engine,node_vqnsp_model,node_quantizer,node_vqnsp_weights toneAmber
class node_pretrain_entry,node_pretrain_engine,node_pretrain_model,node_labram_weights toneMint
class node_finetune_entry,node_finetune_engine,node_finetune_model toneRose
class node_optimizer,node_utilities,node_researcher,node_raw_eeg toneIndigo
```

## Components

### EEG data preparation

- [EEG preprocessing](https://github.com/935963004/labram/blob/main/dataset_maker/shock/utils/eegUtils.py): `dataset_maker/shock/utils/eegUtils.py`
- [TUAB preparation](https://github.com/935963004/labram/blob/main/dataset_maker/make_TUAB.py): `dataset_maker/make_TUAB.py`
- [TUEV preparation](https://github.com/935963004/labram/blob/main/dataset_maker/make_TUEV.py): `dataset_maker/make_TUEV.py`
- [Pretrain preparation](https://github.com/935963004/labram/blob/main/dataset_maker/make_h5dataset_for_pretrain.py): `dataset_maker/make_h5dataset_for_pretrain.py`
- [HDF5 writer](https://github.com/935963004/labram/blob/main/dataset_maker/shock/utils/h5.py): `dataset_maker/shock/utils/h5.py`
- [Ring buffer](https://github.com/935963004/labram/blob/main/dataset_maker/shock/utils/ringBuffer.py): `dataset_maker/shock/utils/ringBuffer.py`
- [Data preprocessing](https://github.com/935963004/labram/blob/main/data_processor/data_preprocess.py): `data_processor/data_preprocess.py`
- [EEG datasets](https://github.com/935963004/labram/blob/main/data_processor/dataset.py): `data_processor/dataset.py`

### Neural tokenizer

- [Tokenizer training](https://github.com/935963004/labram/blob/main/run_vqnsp_training.py): `run_vqnsp_training.py`
- [Tokenizer engine](https://github.com/935963004/labram/blob/main/engine_for_vqnsp.py): `engine_for_vqnsp.py`
- [VQ-NSP model](https://github.com/935963004/labram/blob/main/modeling_vqnsp.py): `modeling_vqnsp.py`
- [EMA quantizer](https://github.com/935963004/labram/blob/main/norm_ema_quantizer.py): `norm_ema_quantizer.py`
- [Tokenizer checkpoint](https://github.com/935963004/labram/blob/main/checkpoints/vqnsp.pth): `checkpoints/vqnsp.pth`

### Foundation pre-training

- [LaBraM pre-training](https://github.com/935963004/labram/blob/main/run_labram_pretraining.py): `run_labram_pretraining.py`
- [Masked-code engine](https://github.com/935963004/labram/blob/main/engine_for_pretraining.py): `engine_for_pretraining.py`
- [Pre-training model](https://github.com/935963004/labram/blob/main/modeling_pretrain.py): `modeling_pretrain.py`
- [LaBraM checkpoint](https://github.com/935963004/labram/blob/main/checkpoints/labram-base.pth): `checkpoints/labram-base.pth`

### Downstream fine-tuning

- [Fine-tuning entry](https://github.com/935963004/labram/blob/main/run_class_finetuning.py): `run_class_finetuning.py`
- [Classification engine](https://github.com/935963004/labram/blob/main/engine_for_finetuning.py): `engine_for_finetuning.py`
- [Fine-tune model](https://github.com/935963004/labram/blob/main/modeling_finetune.py): `modeling_finetune.py`

### Training infrastructure

- [Optimizer factory](https://github.com/935963004/labram/blob/main/optim_factory.py): `optim_factory.py`
- [Training utilities](https://github.com/935963004/labram/blob/main/utils.py): `utils.py`

### Other components

- **Researcher**
- **Raw EEG**

## Connections

- Researcher → Raw EEG: provides (evidence: `README.md`)
- Raw EEG → EEG preprocessing: preprocesses (evidence: `README.md`)
- EEG preprocessing → Pretrain preparation: prepares signals
- Pretrain preparation → HDF5 writer: writes HDF5 (evidence: `README.md`)
- Researcher → TUAB preparation: prepares dataset (evidence: `README.md`)
- Researcher → TUEV preparation: prepares dataset (evidence: `README.md`)
- TUAB preparation → Data preprocessing: prepares samples
- TUEV preparation → Data preprocessing: prepares samples
- HDF5 writer → EEG datasets: stores EEG
- EEG datasets → Tokenizer training: supplies batches
- Researcher → Tokenizer training: starts training (evidence: `README.md`)
- Tokenizer training → Tokenizer engine: runs epochs (evidence: `run_vqnsp_training.py`)
- Tokenizer training → VQ-NSP model: builds model (evidence: `run_vqnsp_training.py`)
- Tokenizer engine → VQ-NSP model: trains (evidence: `engine_for_vqnsp.py`)
- VQ-NSP model → EMA quantizer: quantizes features (evidence: `modeling_vqnsp.py`)
- Tokenizer training → Optimizer factory: creates optimizer (evidence: `run_vqnsp_training.py`)
- Tokenizer training → Training utilities: uses utilities (evidence: `run_vqnsp_training.py`)
- Tokenizer training → Tokenizer checkpoint: saves checkpoint (evidence: `README.md`)
- Researcher → LaBraM pre-training: starts pre-training (evidence: `README.md`)
- LaBraM pre-training → Masked-code engine: runs epochs (evidence: `run_labram_pretraining.py`)
- LaBraM pre-training → Pre-training model: builds model (evidence: `run_labram_pretraining.py`)
- LaBraM pre-training → VQ-NSP model: loads tokenizer (evidence: `run_labram_pretraining.py`)
- Masked-code engine → Pre-training model: trains (evidence: `engine_for_pretraining.py`)
- Masked-code engine → VQ-NSP model: gets target codes (evidence: `engine_for_pretraining.py`)
- LaBraM pre-training → Optimizer factory: creates optimizer (evidence: `run_labram_pretraining.py`)
- LaBraM pre-training → Training utilities: uses utilities (evidence: `run_labram_pretraining.py`)
- LaBraM pre-training → LaBraM checkpoint: loads checkpoint (evidence: `README.md`)
- Researcher → Fine-tuning entry: starts fine-tuning (evidence: `README.md`)
- Fine-tuning entry → Classification engine: runs epochs (evidence: `run_class_finetuning.py`)
- Fine-tuning entry → Fine-tune model: builds model (evidence: `run_class_finetuning.py`)
- Classification engine → Fine-tune model: trains (evidence: `engine_for_finetuning.py`)
- Fine-tuning entry → Optimizer factory: creates optimizer (evidence: `run_class_finetuning.py`)
- Fine-tuning entry → Training utilities: uses utilities (evidence: `run_class_finetuning.py`)
- LaBraM checkpoint → Fine-tuning entry: initializes model (evidence: `README.md`)
