PyHealth follows a structured pipeline to transform raw medical data into trained models. The workflow consists of six main stages:
- Raw Data → BaseDataset: Load CSV/Parquet files using a
BaseDataset subclass and a config.yaml schema. This stage creates a global_event_df.parquet cache. - Patient and Event Objects: Access structured data via
Patient objects. - Task Definition → set_task: Define a
BaseTask (input/output schemas and feature extraction) and call dataset.set_task() to generate a SampleDataset. - Processors → SampleDataset: During
set_task, processors fit to the data and transform features into tensors stored in LitData streaming files. - Model Initialization: Initialize a
BaseModel subclass (e.g., RNN, Transformer) using the SampleDataset. - Training and Evaluation: Use the
Trainer to train the model using DataLoaders generated from the SampleDataset.
Pipeline Visualization:
Raw CSV / Parquet files
│
▼
config.yaml
│
▼
BaseDataset subclass ──── loads tables, caches as global_event_df.parquet
│ .unique_patient_ids → List[str]
│ .get_patient(id) → Patient
│ .iter_patients() → Iterator[Patient]
│ .stats() → prints patient/event counts
│
▼
BaseTask subclass (__call__(patient) → List[Dict])
│ .input_schema = {"feature": "processor_name", ...}
│ .output_schema = {"label": "binary" | "multiclass" | ...}
│
▼
dataset.set_task(task, num_workers=N)
│
▼
SampleDataset ──── len(ds), ds[i], patient_to_index, record_to_index
│ Backed by LitData streaming files
│ Processors fitted during set_task, applied at load time
│
▼
get_dataloader(dataset, batch_size=32, shuffle=True)
│
▼
Model(dataset, ...) ──── BaseModel subclass (RNN, Transformer, MLP, …)
│ EmbeddingModel routes features via processor.is_token()
│ forward(**batch) → {"loss", "y_prob", "y_true", "logit"}
│
▼
Trainer(model, metrics=[...], device=...)
│ .train(train_dl, val_dl, test_dl, epochs=20, ...)
│ .evaluate(test_dl) → Dict[metric_name, value]
│
├──▶ Calibration (pyhealth.calib)
│ TemperatureScaling / HistogramBinning / KCal / …
│ LABEL / SCRIB / FavMac / … (conformal prediction sets)
│
└──▶ Interpretability (pyhealth.interpret)
GradientSaliency / IntegratedGradients / DeepLift / SHAP / LIME / …