# gatoraim/dt-behrt architecture

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

- Interactive diagram: https://gitdiagram.com/gatoraim/dt-behrt
- Repository: https://github.com/gatoraim/dt-behrt

## Overview

1. **Repository purpose**
   - This repo implements **DT-BEHRT**, a disease-trajectory-aware transformer for structured EHR modeling. The core architecture combines sequential patient-visit modeling with graph-enhanced medical-code relationships to support predictive tasks and interpretable patient representations.

2. **End-to-end data flow**
   - The pipeline starts with raw **MIMIC-III** and **MIMIC-IV** hospital tables, moves through notebook-based preprocessing, then feeds processed patient sequences into the model package for training or evaluation.
   - Outputs are stored as pretrained / trained parameter checkpoints and used later by the test notebook for downstream prediction tasks.

3. **Raw data boundary**
   - `data_process/MIMIC-III-raw` and `data_process/MIMIC-IV-raw` are the ingestion points for PhysioNet EHR exports.
   - For MIMIC-III, the expected tables include admissions, diagnoses, labs, patients, prescriptions, and procedures. MIMIC-IV uses the corresponding hospital-side data.
   - This layer is intentionally separated from code, so the repo does not assume raw clinical data is versioned with the source.

4. **Preprocessing subsystem**
   - `data_process/MIMIC-III.ipynb` and `data_process/MIMIC-IV.ipynb` are the main preprocessing pipelines.
   - Their role is to transform raw tables into model-ready, longitudinal patient sequences and to place outputs under `data_process/*-processed`.
   - The notebooks are the boundary where dataset-specific logic lives; the model code itself appears dataset-agnostic once the processed sequences exist.

5. **Ontology / code-mapping support**
   - `data_process/GAMENet/` contains mapping resources such as `ndc2rxnorm_mapping.txt`, `ndc2atc_level4.csv`, and `drug-atc.csv`.
   - These files indicate that medication codes are normalized into standardized vocabularies and drug hierarchy levels, which is important for constructing consistent code tokens and relational features across datasets.

6. **Core model package**
   - `src/heterogt/model/` contains the learned architecture.
   - `model.py` is the top-level model definition, while `layer.py` likely holds the reusable building blocks such as attention, graph, or embedding layers.
   - This is the central computation boundary: it consumes processed patient trajectories and produces task-specific outputs.

7. **Utility / orchestration layer**
   - `src/heterogt/utils/` contains the supporting infrastructure:
     - `dataset.py` for dataset loading and batching,
     - `tokenizer.py` for converting clinical codes / visits into model tokens,
     - `train.py` for training and evaluation loops,
     - `seed.py` for deterministic runs.
   - These utilities separate data representation concerns from model logic.

8. **Pretraining and evaluation flow**
   - `pretrain.ipynb` suggests a dedicated pretraining stage, aligning with the README’s mention of trajectory masking and ontology-informed prediction.
   - `test_model.ipynb` is the evaluation entry point and is configured by `task_index` for three downstream tasks: in-hospital mortality, readmission, and prolonged length of stay.
   - This indicates a two-stage workflow: representation pretraining followed by task-specific assessment.

9. **Checkpoint organization**
   - `parameters/` stores serialized PyTorch models (`.pt`) split by dataset, task, and run index.
   - The layout shows separate checkpoints for **MIMIC-III** and **MIMIC-IV**, each with **death**, **readmission**, and **stay** task folders containing five test models.
   - This supports repeated experiments / ensembling / cross-seed evaluation rather than a single monolithic trained model.

10. **External dependencies that shape architecture**
   - The implementation depends on **PyTorch** and **torch_geometric**, which strongly suggests hybrid neural modeling with graph components.
   - `scikit-learn`, `scipy`, `numpy`, and `pandas` support preprocessing, feature engineering, and metrics.
   - The README also mentions `icd-mappings`, which aligns with the code-normalization and ontology-aware design.

11. **What the architecture diagram should emphasize**
   - A good diagram should show:
     - raw MIMIC data ingestion,
     - preprocessing notebooks generating processed patient sequences,
     - optional medication ontology mapping resources,
     - the model package consuming tokenized trajectories,
     - pretraining and downstream task evaluation,
     - checkpoint storage as the persistence layer.
   - The main boundaries are **data preparation vs. model computation** and **pretraining vs. task evaluation**.

12. **Overall structure**
   - This is not a web service or deployment repo; it is a **research / experimentation codebase** centered on EHR preprocessing, model training, and offline evaluation.
   - The repo’s operational center is the notebook-driven dataset pipeline plus the Python package under `src/heterogt`, with saved model weights and benchmark-specific preprocessing artifacts as the persistent outputs.

Main entry points: [`src/heterogt/utils/train.py`](https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/train.py) (Training loop), [`src/heterogt/utils/tokenizer.py`](https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/tokenizer.py) (Tokenizer), [`src/heterogt/utils/dataset.py`](https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/dataset.py) (Dataset loader), [`src/heterogt/model/model.py`](https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/model/model.py) (DT-BEHRT model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data pipeline"]
  node_mimic3_raw[("MIMIC-III raw<br/>ehr tables")]
  node_mimic4_raw[("MIMIC-IV raw<br/>ehr tables")]
  node_mimic3_prep["MIMIC-III prep<br/>notebook pipeline<br/>[MIMIC-III.ipynb]"]
  node_mimic4_prep["MIMIC-IV prep<br/>notebook pipeline<br/>[MIMIC-IV.ipynb]"]
  node_ontologies["Code mappings<br/>vocab resources"]
  node_mimic3_proc[("MIMIC-III processed<br/>patient sequences")]
  node_mimic4_proc[("MIMIC-IV processed<br/>patient sequences")]
end

subgraph group_model["Model stack"]
  node_tokenizer["Tokenizer<br/>code encoder<br/>[tokenizer.py]"]
  node_dataset["Dataset loader<br/>batching layer<br/>[dataset.py]"]
  node_model_py{{"DT-BEHRT model<br/>transformer model<br/>[model.py]"}}
  node_layers["Model layers<br/>attention/graph layers<br/>[layer.py]"]
  node_train["Training loop<br/>orchestration<br/>[train.py]"]
  node_seed["Seed control<br/>reproducibility<br/>[seed.py]"]
  node_pretrain["Pretrain notebook<br/>training notebook<br/>[pretrain.ipynb]"]
  node_test_model["Test notebook<br/>evaluation notebook<br/>[test_model.ipynb]"]
  node_mimic3_ckpts[("MIMIC-III ckpts<br/>saved models")]
  node_mimic4_ckpts[("MIMIC-IV ckpts<br/>saved models")]
end

node_mimic3_raw -->|"preprocess"| node_mimic3_prep
node_mimic4_raw -->|"preprocess"| node_mimic4_prep
node_ontologies -->|"normalize codes"| node_tokenizer
node_mimic3_prep -->|"write output"| node_mimic3_proc
node_mimic4_prep -->|"write output"| node_mimic4_proc
node_mimic3_proc -->|"load"| node_dataset
node_mimic4_proc -->|"load"| node_dataset
node_dataset -->|"encode"| node_tokenizer
node_tokenizer -->|"inputs"| node_model_py
node_layers -->|"build model"| node_model_py
node_model_py -->|"optimize"| node_train
node_seed -->|"reproducible runs"| node_train
node_pretrain -->|"pretrain"| node_train
node_test_model -->|"evaluate"| node_train
node_train -->|"save"| node_mimic3_ckpts
node_train -->|"save"| node_mimic4_ckpts
node_mimic3_ckpts -->|"load"| node_test_model
node_mimic4_ckpts -->|"load"| node_test_model

click node_mimic3_raw "https://github.com/gatoraim/dt-behrt/tree/main/data_process/MIMIC-III-raw"
click node_mimic4_raw "https://github.com/gatoraim/dt-behrt/tree/main/data_process/MIMIC-IV-raw"
click node_mimic3_prep "https://github.com/gatoraim/dt-behrt/blob/main/data_process/MIMIC-III.ipynb"
click node_mimic4_prep "https://github.com/gatoraim/dt-behrt/blob/main/data_process/MIMIC-IV.ipynb"
click node_ontologies "https://github.com/gatoraim/dt-behrt/tree/main/data_process/GAMENet"
click node_mimic3_proc "https://github.com/gatoraim/dt-behrt/tree/main/data_process/MIMIC-III-processed"
click node_mimic4_proc "https://github.com/gatoraim/dt-behrt/tree/main/data_process/MIMIC-IV-processed"
click node_tokenizer "https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/tokenizer.py"
click node_dataset "https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/dataset.py"
click node_model_py "https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/model/model.py"
click node_layers "https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/model/layer.py"
click node_train "https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/train.py"
click node_seed "https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/seed.py"
click node_pretrain "https://github.com/gatoraim/dt-behrt/blob/main/pretrain.ipynb"
click node_test_model "https://github.com/gatoraim/dt-behrt/blob/main/test_model.ipynb"
click node_mimic3_ckpts "https://github.com/gatoraim/dt-behrt/tree/main/parameters/MIMIC-III"
click node_mimic4_ckpts "https://github.com/gatoraim/dt-behrt/tree/main/parameters/MIMIC-IV"

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_mimic3_raw,node_mimic4_raw,node_mimic3_prep,node_mimic4_prep,node_ontologies,node_mimic3_proc,node_mimic4_proc toneBlue
class node_tokenizer,node_dataset,node_model_py,node_layers,node_train,node_seed,node_pretrain,node_test_model,node_mimic3_ckpts,node_mimic4_ckpts toneAmber
```

## Components

### Data pipeline

Raw MIMIC inputs, preprocessing notebooks, and ontology resources that produce model-ready patient sequences.

- [MIMIC-III raw](https://github.com/gatoraim/dt-behrt/tree/main/data_process/MIMIC-III-raw): ehr tables · PhysioNet hospital tables ingested as the source MIMIC-III dataset boundary. · `data_process/MIMIC-III-raw`
- [MIMIC-IV raw](https://github.com/gatoraim/dt-behrt/tree/main/data_process/MIMIC-IV-raw): ehr tables · PhysioNet hospital-side MIMIC-IV data ingested as the source dataset boundary. · `data_process/MIMIC-IV-raw`
- [MIMIC-III prep](https://github.com/gatoraim/dt-behrt/blob/main/data_process/MIMIC-III.ipynb): notebook pipeline · Transforms raw MIMIC-III tables into longitudinal patient sequences and processed artifacts. · `data_process/MIMIC-III.ipynb`
- [MIMIC-IV prep](https://github.com/gatoraim/dt-behrt/blob/main/data_process/MIMIC-IV.ipynb): notebook pipeline · Transforms raw MIMIC-IV tables into longitudinal patient sequences and processed artifacts. · `data_process/MIMIC-IV.ipynb`
- [Code mappings](https://github.com/gatoraim/dt-behrt/tree/main/data_process/GAMENet): vocab resources · Medication normalization and hierarchy resources used to map codes into standardized vocabularies. · `data_process/GAMENet`
- [MIMIC-III processed](https://github.com/gatoraim/dt-behrt/tree/main/data_process/MIMIC-III-processed): patient sequences · Processed longitudinal sequences used as model-ready input for MIMIC-III experiments. · `data_process/MIMIC-III-processed`
- [MIMIC-IV processed](https://github.com/gatoraim/dt-behrt/tree/main/data_process/MIMIC-IV-processed): patient sequences · Processed longitudinal sequences used as model-ready input for MIMIC-IV experiments. · `data_process/MIMIC-IV-processed`

### Model stack

Core DT-BEHRT implementation, training utilities, and saved checkpoints for downstream tasks.

- [Tokenizer](https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/tokenizer.py): code encoder · Converts clinical codes and visits into tokenized representations for the model. · `src/heterogt/utils/tokenizer.py`
- [Dataset loader](https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/dataset.py): batching layer · Loads processed patient data and prepares training and evaluation batches. · `src/heterogt/utils/dataset.py`
- [DT-BEHRT model](https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/model/model.py): transformer model · Top-level disease-trajectory-aware model definition for structured EHR prediction. · `src/heterogt/model/model.py`
- [Model layers](https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/model/layer.py): attention/graph layers · Reusable building blocks for embeddings, attention, and graph-enhanced computation. · `src/heterogt/model/layer.py`
- [Training loop](https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/train.py): orchestration · Coordinates optimization, evaluation, and checkpointing for experiments. · `src/heterogt/utils/train.py`
- [Seed control](https://github.com/gatoraim/dt-behrt/blob/main/src/heterogt/utils/seed.py): reproducibility · Sets deterministic execution for repeated training and evaluation runs. · `src/heterogt/utils/seed.py`
- [Pretrain notebook](https://github.com/gatoraim/dt-behrt/blob/main/pretrain.ipynb): training notebook · Dedicated pretraining stage for trajectory masking and ontology-informed representation learning. · `pretrain.ipynb`
- [Test notebook](https://github.com/gatoraim/dt-behrt/blob/main/test_model.ipynb): evaluation notebook · Runs downstream evaluation for mortality, readmission, and prolonged stay tasks. · `test_model.ipynb`
- [MIMIC-III ckpts](https://github.com/gatoraim/dt-behrt/tree/main/parameters/MIMIC-III): saved models · Stored PyTorch checkpoints for MIMIC-III death, readmission, and stay tasks. · `parameters/MIMIC-III`
- [MIMIC-IV ckpts](https://github.com/gatoraim/dt-behrt/tree/main/parameters/MIMIC-IV): saved models · Stored PyTorch checkpoints for MIMIC-IV death, readmission, and stay tasks. · `parameters/MIMIC-IV`

## Connections

- MIMIC-III raw → MIMIC-III prep: preprocess · Raw MIMIC-III tables are converted into structured patient trajectories.
- MIMIC-IV raw → MIMIC-IV prep: preprocess · Raw MIMIC-IV tables are converted into structured patient trajectories.
- Code mappings → Tokenizer: normalize codes · Medication vocabularies inform code normalization and token construction.
- MIMIC-III prep → MIMIC-III processed: write output · Notebook pipeline emits processed MIMIC-III artifacts for modeling.
- MIMIC-IV prep → MIMIC-IV processed: write output · Notebook pipeline emits processed MIMIC-IV artifacts for modeling.
- MIMIC-III processed → Dataset loader: load · Processed MIMIC-III sequences are loaded for batching and training.
- MIMIC-IV processed → Dataset loader: load · Processed MIMIC-IV sequences are loaded for batching and training.
- Dataset loader → Tokenizer: encode · Loaded visits and codes are converted into model token sequences.
- Tokenizer → DT-BEHRT model: inputs · Tokenized patient trajectories are passed into the core model.
- Model layers → DT-BEHRT model: build model · Reusable layers implement the model's internal computation blocks.
- DT-BEHRT model → Training loop: optimize · The model is trained and evaluated through the experiment loop.
- Seed control → Training loop: reproducible runs · Deterministic seeding controls repeated experiments and comparisons.
- Pretrain notebook → Training loop: pretrain · The pretraining notebook drives the shared training workflow.
- Test notebook → Training loop: evaluate · The evaluation notebook invokes the same model and data pipeline for downstream tasks.
- Training loop → MIMIC-III ckpts: save · Training outputs are persisted as MIMIC-III task checkpoints.
- Training loop → MIMIC-IV ckpts: save · Training outputs are persisted as MIMIC-IV task checkpoints.
- MIMIC-III ckpts → Test notebook: load · Saved MIMIC-III checkpoints are reused for downstream evaluation.
- MIMIC-IV ckpts → Test notebook: load · Saved MIMIC-IV checkpoints are reused for downstream evaluation.
