How gatoraim/dt-behrt works
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: train.py Training loop · tokenizer.py Tokenizer · dataset.py Dataset loader · model.py DT-BEHRT model
Components 17
Data pipeline
Raw MIMIC inputs, preprocessing notebooks, and ontology resources that produce model-ready patient sequences.
- MIMIC-III raw · PhysioNet hospital tables ingested as the source MIMIC-III dataset boundary.
data_process/MIMIC-III-raw - MIMIC-IV raw · PhysioNet hospital-side MIMIC-IV data ingested as the source dataset boundary.
data_process/MIMIC-IV-raw - MIMIC-III prep · Transforms raw MIMIC-III tables into longitudinal patient sequences and processed artifacts.
data_process/MIMIC-III.ipynb - MIMIC-IV prep · Transforms raw MIMIC-IV tables into longitudinal patient sequences and processed artifacts.
data_process/MIMIC-IV.ipynb - Code mappings · Medication normalization and hierarchy resources used to map codes into standardized vocabularies.
data_process/GAMENet - MIMIC-III processed · Processed longitudinal sequences used as model-ready input for MIMIC-III experiments.
data_process/MIMIC-III-processed - MIMIC-IV processed · 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 · Converts clinical codes and visits into tokenized representations for the model.
src/heterogt/utils/tokenizer.py - Dataset loader · Loads processed patient data and prepares training and evaluation batches.
src/heterogt/utils/dataset.py - DT-BEHRT model · Top-level disease-trajectory-aware model definition for structured EHR prediction.
src/heterogt/model/model.py - Model layers · Reusable building blocks for embeddings, attention, and graph-enhanced computation.
src/heterogt/model/layer.py - Training loop · Coordinates optimization, evaluation, and checkpointing for experiments.
src/heterogt/utils/train.py - Seed control · Sets deterministic execution for repeated training and evaluation runs.
src/heterogt/utils/seed.py - Pretrain notebook · Dedicated pretraining stage for trajectory masking and ontology-informed representation learning.
pretrain.ipynb - Test notebook · Runs downstream evaluation for mortality, readmission, and prolonged stay tasks.
test_model.ipynb - MIMIC-III ckpts · Stored PyTorch checkpoints for MIMIC-III death, readmission, and stay tasks.
parameters/MIMIC-III - MIMIC-IV ckpts · Stored PyTorch checkpoints for MIMIC-IV death, readmission, and stay tasks.
parameters/MIMIC-IV
Connections 18
- 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.