diff --git a/docs/api/datasets.rst b/docs/api/datasets.rst index 8b19949c70..40be5ca82d 100644 --- a/docs/api/datasets.rst +++ b/docs/api/datasets.rst @@ -668,6 +668,7 @@ Available Datasets datasets/pyhealth.datasets.MIMIC4EDDataset datasets/pyhealth.datasets.KiTS23Dataset datasets/pyhealth.datasets.MIMIC4ImaGenomeDataset + datasets/pyhealth.datasets.MedModMIMIC4Dataset datasets/pyhealth.datasets.BrainTumorMRIDataset datasets/pyhealth.datasets.FHIRDataset datasets/pyhealth.datasets.MEDSDataset diff --git a/docs/api/datasets/pyhealth.datasets.MedModMIMIC4Dataset.rst b/docs/api/datasets/pyhealth.datasets.MedModMIMIC4Dataset.rst new file mode 100644 index 0000000000..20804c69eb --- /dev/null +++ b/docs/api/datasets/pyhealth.datasets.MedModMIMIC4Dataset.rst @@ -0,0 +1,59 @@ +pyhealth.datasets.MedModMIMIC4Dataset +===================================== + +MIMIC-IV v1.0 and MIMIC-CXR-JPG 2.0.0 as used by MedMod (Elsharief et al., +"MedMod: Multimodal Benchmark for Medical Prediction Tasks with Electronic +Health Records and Chest X-Ray Scans", CHIL 2025, PMLR 287; +`paper `__, +`code `__) for in-hospital mortality, +with MedMod's fixed train / val / test split. Use it with +:class:`~pyhealth.tasks.MedModInHospitalMortality`. + +Access to `MIMIC-IV v1.0 `__ +and `MIMIC-CXR-JPG 2.0.0 `__ +needs PhysioNet credentialing. +Without it, ``pyhealth.datasets.medmod.synthetic.create_synthetic_samples`` +builds synthetic samples with the same contract. + +.. autoclass:: pyhealth.datasets.MedModMIMIC4Dataset + :members: + :undoc-members: + :show-inheritance: + +.. autoclass:: pyhealth.datasets.MedModSplit + :members: + :undoc-members: + :show-inheritance: + +.. autofunction:: pyhealth.datasets.split_by_medmod + +MedSigLIP image embeddings +-------------------------- + +For the extension, :mod:`pyhealth.datasets.medmod.medsiglip` replaces the +ResNet-34 image features with those of a frozen ``google/medsiglip-448`` +image encoder. Each image is embedded once and saved to one ``.npz`` file, +where the embeddings are looked up by image file name. + +MedSigLIP (`google/medsiglip-448 `__) +is introduced in Sellergren et al., "MedGemma Technical Report", Nature +Medicine 32, 3695-3704 (2026), https://doi.org/10.1038/s41591-026-04626-w +(preprint arXiv:2507.05201). Its weights are downloaded from the Hugging Face Hub +under the `Health AI Developer Foundations terms of use +`__. + +.. automodule:: pyhealth.datasets.medmod.medsiglip + :members: + +ConVIRT pretraining samples +--------------------------- + +ConVIRT is pretrained on MedMod's phenotyping train and validation stays, each +with its whole ICU stay of EHR and its latest AP chest X-ray. +:mod:`pyhealth.datasets.medmod.pretraining` builds these samples from the +output of MedMod's extraction scripts and the MIMIC-CXR-JPG metadata +(``load_pretraining_samples``) and turns them into a PyHealth dataset +(``create_pretraining_dataset``). + +.. automodule:: pyhealth.datasets.medmod.pretraining + :members: diff --git a/docs/api/tasks.rst b/docs/api/tasks.rst index 9796b94ed0..745f6622f6 100644 --- a/docs/api/tasks.rst +++ b/docs/api/tasks.rst @@ -266,6 +266,7 @@ Available Tasks LabTOP Lab Value Prediction (HiRID) Length of Stay Prediction Length of Stay Prediction (StageNet MIMIC-IV) + MedMod In-Hospital Mortality (MIMIC-IV + CXR) Medical Transcriptions Classification MPF Clinical Prediction (FHIR) Mortality Prediction (Next Visit) diff --git a/docs/api/tasks/pyhealth.tasks.MedModInHospitalMortality.rst b/docs/api/tasks/pyhealth.tasks.MedModInHospitalMortality.rst new file mode 100644 index 0000000000..44a525dfca --- /dev/null +++ b/docs/api/tasks/pyhealth.tasks.MedModInHospitalMortality.rst @@ -0,0 +1,89 @@ +pyhealth.tasks.MedModInHospitalMortality +======================================== + +In-hospital mortality from the first 48 hours of an ICU stay and the latest +chest X-ray in that window, reproducing MedMod (Elsharief et al., 2025) and its +released code: one sample per ICU stay with ``ehr`` (48 x 76), ``image_path`` +and ``label``. Use it with :class:`~pyhealth.datasets.MedModMIMIC4Dataset`. + +.. autoclass:: pyhealth.tasks.MedModInHospitalMortality + :members: + :undoc-members: + :show-inheritance: + +.. autoclass:: pyhealth.processors.medmod_ehr_processor.MedModEHRProcessor + :members: + :undoc-members: + :show-inheritance: + +Per-model settings +------------------ + +MedMod's models read their data through two code paths. Both use the same +cohort, labels and split. They differ in the EHR rows at Hours == 0, which the +task sets with ``hours_zero``, and in the image transform. The task returns +``image_path`` and applies no image transform: the "Image transform" column is +what the model's own image loader applies. + +.. list-table:: + :header-rows: 1 + + * - Model + - ``hours_zero`` + - Image transform + - Why + * - Early Fusion + - ``"keep"`` + - ``get_preset("early_fusion", ...)`` (ImageNet-normalized) + - MedMod's supervised ``fusion_main.py:13-16`` imports ``datasets.*`` and + ``ehr_utils.*``, which exist only in MedFuse; MedFuse's + ``datasets/ehr_dataset.py`` keeps every row and + ``datasets/cxr_dataset.py`` normalizes. + * - ConVIRT (pretraining, linear evaluation) + - ``"drop"`` + - ``get_preset("convirt", ...)`` (pixels in [0, 1]) + - MedMod's ``src/data/datasets.py:137-163`` keeps 0 < t only; the ConVIRT + scripts pass ``--transforms_cxr simclrv2``. + +``get_preset`` and ``load_cxr`` are in ``pyhealth.datasets.medmod.image_presets``; +``train=True`` adds the training augmentation. + +Paper vs code +------------- + +Where MedMod's paper and released code disagree, the task follows the code, +which produced the reported numbers. The bin width and the normalizer remain +settings of the task (``timestep``, ``normalizer``). + +.. list-table:: + :header-rows: 1 + + * - Rule + - MedMod paper + - Released code + - Default here + * - Bin width + - §3.2.2 "every two hours" (MedFuse §4.1.1 too) + - 1 h (``--timestep`` default 1.0, ``src/parser.py:30``) + - 1 h (``timestep``) + * - Normalizer + - §3.2.2 "task-specific", computed on the dataset + - the phenotyping file ``ph_ts1.0`` (an ``is`` comparison in ``ehr_funcs``) + - ``ph_ts1.0``; ``normalizer=None`` fits one on the train split + * - Split + - §3.2.4 random 70/10/20 by patient + - fixed subject lists + - the fixed lists, which reproduce Table 2 + +Two checks on the real data (image-paired test set, 1,241 stays): + +- MedFuse's released EHR checkpoint (``checkpoints/mortality/uni_ehr_all``), + evaluated frozen: test AUROC 0.833 (95% CI 0.802-0.860) with 1 h + + ``ph_ts1.0``, against 0.735 with 1 h + a normalizer fitted on the train split + and 0.719 with 2 h + fitted; MedFuse reports 0.825 (0.793-0.852). So a model + that uses MedFuse's encoders frozen needs the code's settings. MedFuse split + with the same subject lists, so its training data holds none of these test + patients. +- MedMod's EHR LSTM trained from scratch (5 seeds): 1 h vs 2 h with the same + normalizer makes no difference (Δ AUROC +0.000 ± 0.006); ``ph_ts1.0`` scores + 0.009 higher than a fitted normalizer (0.816 vs 0.807; paper Table 3: 0.829). diff --git a/examples/README.md b/examples/README.md index 63589e3129..12a00baf49 100644 --- a/examples/README.md +++ b/examples/README.md @@ -99,6 +99,8 @@ Older examples that no longer run, or that duplicated a maintained one, are in | `patient_linkage_mimic3_medlink.py` | MedLink record linkage (needs `pytrec_eval`) | | `physionet_deid_ner_transformer_deid.py` | Transformer de-identification (PhysioNet, credentialed) | | `mimic4_mortality_medfuse.py` | MedFuse ablation (synthetic data) | +| `mimic4_medmod_ihm_early_fusion.py` | MedMod Early Fusion for in-hospital mortality | +| `mimic4_medmod_convirt.py` | MedMod ConVIRT pretraining + linear probe for in-hospital mortality | | `mimic4_decompensation_daft.py` | DAFT on MedMod decompensation and mortality, with ablations (synthetic data by default) | | `physionet2012_mortality_stepwise_embedding.py` | Step-wise embedding ablation (backbone, raw, direct, grouped and grouped_shared encoders) on PhysioNet 2012 mortality (synthetic data with `--synthetic`) | | `support_survival_survival_mdn.py` (with `survival_mdn_data.py`, `survival_mdn_metrics.py`, results in `survival_mdn_results/`) | Survival mixture density network on SUPPORT | diff --git a/examples/medmod_runners/README.md b/examples/medmod_runners/README.md new file mode 100644 index 0000000000..d141c38d53 --- /dev/null +++ b/examples/medmod_runners/README.md @@ -0,0 +1,125 @@ +# MedMod runner scripts + +These scripts produced the real-data results in +[`../mimic4_medmod_ihm_results.md`](../mimic4_medmod_ihm_results.md). They +train the same models with the same settings as the example scripts +`mimic4_medmod_ihm_early_fusion.py` and `mimic4_medmod_convirt.py`. They also +compute bootstrap confidence intervals, write a JSON report for every run and +save checkpoints, so that a run continues after a disconnect. The Colab +notebook [`../mimic4_medmod_ihm_colab_runs.ipynb`](../mimic4_medmod_ihm_colab_runs.ipynb) +runs all of them. + +The runs need credentialed PhysioNet access to MIMIC-IV v1.0 and +MIMIC-CXR-JPG 2.0.0. Keep the data out of the code folder and out of git. The +scripts print only aggregates, such as counts, losses and scores. + +| Script | What it does | +|---|---| +| `slim_events.py` | Writes the slim copy of the MIMIC-IV event tables (once) | +| `samples.py` | Builds and checks an in-hospital mortality sample set | +| `early_fusion_run.py` | Trains and tests Early Fusion | +| `pretrain_samples.py` | Builds the ConVIRT pretraining samples | +| `convirt_pretrain.py` | Pretrains ConVIRT | +| `convirt_probe.py` | Trains and tests the linear probe on a pretraining checkpoint | +| `colab_bundle.py` | Zips the code and data for Google Colab | +| `provenance.py` | Records the code version in every report and checkpoint | + +Run each script from the repository root. This shows its options: +`python -m examples.medmod_runners.