Skip to content

Add MedMod in-hospital mortality: dataset, task, Early Fusion and ConVIRT models - #1609

Open
Rainie-Hu wants to merge 32 commits into
sunlabuiuc:masterfrom
Rainie-Hu:feature/medmod-reproduction
Open

Rainie-Hu wants to merge 32 commits into
sunlabuiuc:masterfrom
Rainie-Hu:feature/medmod-reproduction

Conversation

@Rainie-Hu

@Rainie-Hu Rainie-Hu commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

**Split into #1637, #1638 (merged), #1639 and #1640 for review.

Contributors: Yuqing Hu (GT username yhu672, yhu672@gatech.edu) and Hanh Trang Do (GT username hdo61, hdo61@gatech.edu), Georgia Tech CSE 6250

Contribution Type: Full Pipeline (dataset + task + models)

Paper: Elsharief, S., et al. (2025). MedMod: Multimodal Benchmark for Medical Prediction Tasks with Electronic Health Records and Chest X-Ray Scans. CHIL 2025, PMLR 287. https://proceedings.mlr.press/v287/elsharief25a.html (code: https://github.com/nyuad-cai/MedMod)

Description:

This PR reproduces MedMod's in-hospital mortality benchmark in PyHealth: predict death from the first 48 hours of an ICU stay, using MIMIC-IV v1.0 EHR and the latest MIMIC-CXR-JPG chest X-ray in that window.

Why a separate dataset and task: PyHealth already has EHRCXRMortalityMIMIC4 (MedFuse's in-hospital mortality) and MedModDecompensationMIMIC4 (#1510), which share the 34-column EHR of ehr_cxr_mimic4 (17 values with ordinal Glasgow scores, plus masks, normalized on the training split). MedMod's models instead read MedMod's 76-column discretization: the 5 categorical variables one-hot encoded (47 columns), 12 continuous values and 17 masks, in MedMod's column order and with its stored normalizer. MedFuse's released EHR encoder was trained on exactly this input: its first LSTM layer is 1024 × 76, so it cannot load 34-column data. With these inputs the EHR encoder alone gets 0.833 test AUROC (MedFuse paper 0.825), and Early Fusion gets 0.8407 (paper 0.842). The fixed MedMod split also keeps the test patients out of the checkpoints' training data.

Difference from PyHealth's MedFuse model: MedFuse fuses the two modalities with an LSTM over the EHR and image features and trains its encoders. EHRCXREarlyFusion projects the image feature, concatenates it with the EHR feature and trains only the projection and a linear classifier, on top of frozen pretrained encoders, as in the MedMod paper.

Links to other components:

  • MedModMIMIC4Dataset and MedModEHRDataset subclass MIMIC4Dataset and MIMIC4EHRDataset to reuse PyHealth's MIMIC-IV loading. They add the v1.0 config and MedMod's row filters.
  • ConVIRT reuses build_resnet and value_tensor from ehr_cxr_early_fusion.py (our own Early Fusion model), so both models build the ResNet and read inputs the same way.

Our components do not change or import the existing tasks or models.

  • Dataset: MedModMIMIC4Dataset reads MIMIC-IV v1.0 and MIMIC-CXR-JPG 2.0.0 and applies MedMod's fixed train / validation / test split (MedModSplit, split_by_medmod).
  • Task: MedModInHospitalMortality builds one sample per ICU stay: a 48 × 76 EHR time series (MedMod's 17 variables, discretized as in its released code), the image path and the mortality label. MedModEHRProcessor normalizes the EHR inside PyHealth's processor framework. Our samples match MedMod's own extraction output for all 6,214 stays.
  • Models: EHRCXREarlyFusion (EHR LSTM + ResNet-34, frozen pretrained encoders, as in the paper's Early Fusion), ConVIRT (contrastive pretraining of an EHR LSTM and a ResNet-34) and ConVIRTLinearProbe (a linear probe on the frozen ConVIRT encoders).
  • Extension: the option --embeddings replaces the ResNet-34 with stored embeddings from a frozen MedSigLIP image encoder (google/medsiglip-448) in both models. pyhealth.datasets.medmod.medsiglip computes the embeddings.
  • Without MIMIC access: pyhealth.datasets.medmod.synthetic creates synthetic samples with the same format. Both examples run end to end on them with --synthetic, including the MedSigLIP extension (--embeddings synthetic).

Results on MedMod's test split (1,241 stays, 187 deaths), mean ± sample standard deviation over MedMod's five seeds:

Model Image encoder AUROC AUPRC Paper AUROC
Early Fusion ResNet-34 0.8407 ± 0.0004 0.5140 ± 0.0007 0.842 ± 0.004
Early Fusion MedSigLIP (extension) 0.8461 ± 0.0010 0.5367 ± 0.0035 –
ConVIRT linear probe ResNet-34 0.8206 ± 0.0049 0.4588 ± 0.0114 0.847 ± 0.002 (Table 4), 0.813 ± 0.010 (Table 8)
ConVIRT linear probe MedSigLIP (extension) 0.8200 ± 0.0037 0.4686 ± 0.0077 –

Each ConVIRT probe uses the pretraining epoch with the highest validation AUROC, as the paper describes.

Deviations from the paper and our proposal (examples/mimic4_medmod_ihm_results.md has the details, our hypotheses and all settings):

  • EHR: 1-hour bins and MedMod's phenotyping normalizer, as in MedMod's released code. The paper text says 2-hour bins and a normalizer fitted on the dataset.
  • Early Fusion uses MedFuse's released pretrained encoders, frozen, because MedMod does not release its own. Our proposal said the encoders were trained from scratch, but the paper's Early Fusion uses pretrained encoders.
  • Frozen encoders stay in evaluation mode. MedMod's and MedFuse's code leave them in training mode.
  • Early Fusion stops after 15 epochs without improvement (MedFuse's code). The paper text says 10.
  • ConVIRT pretraining runs all 300 epochs, without MedMod's early stopping. The pretraining epoch is chosen among every 10th epoch, with a quick probe without augmentation. MedMod's script uses epoch 65.
  • The linear probe uses MedMod's script settings (AdamW, up to 200 epochs, patience 30). The paper text describes Adam, up to 300 epochs and patience 10.
  • Both models use the learning rates from the released code. We did not repeat the paper's learning-rate search.
  • The test split has 1,241 stays, not 1,242, because one image is missing from PhysioNet.
  • Training uses the examples' own loops instead of PyHealth's Trainer, which has no learning-rate scheduler.
  • MedSigLIP was trained on data that include MIMIC-CXR.

Files to Review:

  • pyhealth/datasets/medmod_mimic4.py - dataset, MedMod split
  • pyhealth/datasets/medmod/ - EHR extraction and discretizer, image presets and resizing, ConVIRT pretraining samples, MedSigLIP embeddings, synthetic data, and the download of MedMod's item map and split lists on first use
  • pyhealth/datasets/configs/mimic4_ehr_v1.0.yaml - MIMIC-IV v1.0 table config
  • pyhealth/tasks/medmod_mortality_mimic4.py - task
  • pyhealth/processors/medmod_ehr_processor.py - EHR normalizer
  • pyhealth/models/ehr_cxr_early_fusion.py - Early Fusion model
  • pyhealth/models/convirt.py - ConVIRT and the linear probe
  • examples/mimic4_medmod_ihm_early_fusion.py - Early Fusion example (synthetic and real data)
  • examples/mimic4_medmod_convirt.py - ConVIRT pretraining and linear probe example
  • examples/mimic4_medmod_ihm_results.md - results, settings and deviations
  • examples/mimic4_medmod_ihm_colab_runs.ipynb - the Colab notebook of the real-data runs, with their outputs
  • examples/medmod_runners/ - the scripts the Colab notebook runs (same models and settings as the examples, plus checkpoints, bootstrap confidence intervals and a report per run); its README.md explains the data layout and how to prepare the data
  • tests/core/test_medmod_medsiglip.py, tests/core/test_medmod_mimic4_dataset.py, tests/core/test_medmod_mimic4_extraction.py, tests/core/test_medmod_mimic4_images.py, tests/core/test_medmod_mimic4_processor.py, tests/core/test_medmod_mimic4_synthetic.py, tests/core/test_medmod_pretraining.py, tests/core/test_ehr_cxr_early_fusion.py, tests/core/test_convirt.py - tests, with shared helpers in tests/core/medmod_test_utils.py
  • test-resources/medmod/itemid_to_variable_map.csv - 10 rows of MedMod's item map, so the tests need no download
  • pyhealth/datasets/__init__.py, pyhealth/models/__init__.py, pyhealth/tasks/__init__.py and examples/README.md - one-line registrations
  • docs/api/datasets/pyhealth.datasets.MedModMIMIC4Dataset.rst, docs/api/tasks/pyhealth.tasks.MedModInHospitalMortality.rst, docs/api/models/pyhealth.models.EHRCXREarlyFusion.rst, docs/api/models/pyhealth.models.ConVIRT.rst and the index entries in docs/api/datasets.rst, docs/api/tasks.rst and docs/api/models.rst

Testing: pytest on the test files above runs our 112 tests on synthetic data only, with no network access. Together with upstream's MedMod decompensation tests, all 133 pass. Each test takes about one second or less. The dataset tests share one small fake MIMIC folder, which takes a few seconds to build once. ruff check passes on the new pyhealth/ and tests/ files, and python tools/check_pr_rules.py passes.

Data: MIMIC-IV and MIMIC-CXR need PhysioNet credentialing. No MIMIC data, identifiers or trained weights are in this PR. The docs and the Colab notebook show only aggregate scores and counts. MedMod's item map and split lists are downloaded from MedMod's GitHub on first use.

🤖 Generated with Claude Code

dhtrg and others added 29 commits November 16, 2026 21:07
Reproduces the in-hospital mortality data pipeline of MedMod (Elsharief et al., CHIL 2025) on MIMIC-IV v1.0 and MIMIC-CXR-JPG 2.0.0:

- MedModMIMIC4Dataset: MIMIC4Dataset with a v1.0 EHR config and MedMod's row filters; MedModSplit: MedMod's fixed train/val/test subject lists as a PyHealth Split
- MedModInHospitalMortality: one sample per ICU stay (ehr 48 x 76, image_path, label), following MedMod's released code (1-hour bins by default; bin width and normalizer are task arguments)
- MedModEHRProcessor: MedMod's normalizer file, or one fitted on the train split
- image presets for Early Fusion and ConVIRT (train and eval), MedFuse's 512-px resize, and synthetic samples with the same contract
- tests on synthetic data; API docs for the dataset and the task

On the real data the cohort matches the paper's Table 2 and every EHR tensor is bit-identical to MedMod's own extraction.

MedMod's split lists are downloaded from MedMod's GitHub at a pinned commit on first use (SHA-256 checked), not bundled. The item map, discretizer config and normalizer are bundled unmodified (Apache-2.0, see pyhealth/datasets/medmod/files/NOTICE). Contains no MIMIC data.
- MedModEHRSequenceProcessor: standardizes like MedModEHRProcessor; its collate zero-pads a batch at the end and returns the true lengths, as MedMod's pad_zeros / my_collate_fusion (utils.py:160-170, 399-406)
- create_synthetic_pretraining_samples: synthetic whole-stay samples of varying length with the same contract
- fix a circular import when medmod_ehr_processor is imported first
- tests for both; wrap a long docstring line
- ruff: built-in generics and | unions, import order, set comprehensions,
  ClassVar for the task schemas and the unpickler allow-list; noqa where a
  rewrite could change behaviour (MedMod's sort-and-keep-last, int(round())
  on numpy floats).
- A '>>>' usage example for every public class and function (54); all run
  except dataset/split calls that need real data.

tools/check_pr_rules.py against upstream 0e691f8: 0 issues.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add EHRCXREarlyFusion and ConVIRT models with tests and docs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the Early Fusion example and a MedSigLIP embedding option for ConVIRT
Both examples (ConVIRT, Early Fusion) take --embeddings <.npz> from
pyhealth.datasets.medmod.medsiglip: the CXR ResNet-34 is replaced by the
stored frozen MedSigLIP embeddings (cxr_backbone=nn.Identity(),
cxr_feature_dim=1152), looked up per batch with the new
lookup_embeddings(). The ResNet path is unchanged.
MPS crashes on some macOS/torch versions, so use --device mps to enable it.
Document the public methods of the discretizer, normalizer, image
presets, EHR processors and the in-hospital mortality task, with
Google-style docstrings and full type hints. Move imports to module
level where the import order allows it, and explain the one import
that must stay inside a function. Cite MedSigLIP and its terms of use
in the module and the dataset docs, and document the ConVIRT
pretraining samples. Show the image resize step as a Python example.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
write_synthetic_embeddings writes random stand-in vectors (1152 per
image) in the same .npz format as the real MedSigLIP embeddings, so the
MedSigLIP extension runs end to end on synthetic data without the gated
model. Move the synthetic module's imports to module level.

Make the tests faster: the synthetic tests use 8 stays and a batch of
4, the image preset checks run in the test process instead of a child
Python, and the MedSigLIP tests import their helpers at the top. Every
test runs in about a second or less.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 991 KB itemid_to_variable_map.csv is no longer part of PyHealth.
split_lists.py becomes downloads.py: medmod_file_path fetches MedMod's
item map and split lists from a pinned commit of MedMod's GitHub (with
MedFuse's identical copy as a fallback), checks each SHA-256 and caches
them in ~/.cache/pyhealth/medmod. The file is byte-identical to the
bundled one, so samples and results do not change.

MedModResources reads the item map on first use, so creating the task,
its schema or synthetic samples needs no network. MedModMIMIC4Dataset
takes an itemid_map argument for offline use. The tests use 10
unmodified rows of the map in test-resources/medmod and need no
download.

Also adds docstrings and type hints to the dataset and split classes,
and builds the dataset tests' fake MIMIC folder once per module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both docstrings now report the mean and standard deviation over
MedMod's five seeds from the real-data runs, next to the paper, and
name the Colab notebook cells that print them. The ConVIRT example
probes the pretraining epoch with the highest validation AUROC, as the
paper describes, instead of MedMod's hard-coded epoch 65.

Early Fusion always freezes the two MedFuse encoders, as the paper's
Early Fusion does, so the --train-encoders option is removed. Its
docstring lists the deviations from the paper and our proposal. Both
examples run the MedSigLIP extension on synthetic data with
--embeddings synthetic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The scripts in examples/medmod_runners produced the five-seed results.
They train the same models with the same settings as the two example
scripts, and add bootstrap confidence intervals, a JSON report per run
that records the code version, and checkpoints, so that a run
continues after a disconnect. slim_events.py writes the copy of the
MIMIC-IV event tables that the runners read, keeping only rows MedMod
can use. The README explains the data layout, how to prepare the data
and the order of the runs.

mimic4_medmod_ihm_colab_runs.ipynb ran every job on Colab in parallel,
including the paper's choice of the pretraining epoch by validation
AUROC. Its outputs are kept and contain only aggregates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mimic4_medmod_ihm_results.md reports the real-data results of both
models on MedMod's test split, as the mean and standard deviation over
MedMod's five seeds, next to the paper: Early Fusion AUROC 0.8407 +-
0.0004 (paper 0.842 +- 0.004) and the ConVIRT linear probe 0.8206 +-
0.0049 (paper Table 4 0.847, Table 8 0.813), with the MedSigLIP
extension for both. It tests our two hypotheses, lists the settings and
every deviation from the paper and our proposal, and names the Colab
notebook cell behind each number.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…production

# Conflicts:
#	docs/api/datasets.rst
#	docs/api/models.rst
#	examples/README.md
#	pyhealth/tasks/__init__.py
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 88.87% (62610 / 70451 lines)

Modified file Coverage Lines
pyhealth/datasets/__init__.py 100.00% 95 / 95
pyhealth/datasets/medmod/__init__.py 100.00% 0 / 0
pyhealth/datasets/medmod/discretizer.py 97.01% 162 / 167
pyhealth/datasets/medmod/downloads.py 97.78% 44 / 45
pyhealth/datasets/medmod/extraction.py 92.61% 326 / 352
pyhealth/datasets/medmod/image_presets.py 82.22% 37 / 45
pyhealth/datasets/medmod/medsiglip.py 92.06% 116 / 126
pyhealth/datasets/medmod/pretraining.py 97.94% 95 / 97
pyhealth/datasets/medmod/resize_cxr.py 88.64% 78 / 88
pyhealth/datasets/medmod/synthetic.py 94.85% 129 / 136
pyhealth/datasets/medmod_mimic4.py 100.00% 79 / 79
pyhealth/processors/medmod_ehr_processor.py 97.50% 39 / 40
pyhealth/tasks/__init__.py 98.66% 147 / 149
pyhealth/tasks/medmod_mortality_mimic4.py 98.44% 63 / 64
pyhealth/tasks/temporal_outcome_prediction_mimic4.py 85.81% 248 / 289

Workflow run · 5636906

@rianfyi rianfyi added the status: automatic The PR will go through automatic review process. label Nov 25, 2026
@rianfyi

rianfyi commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

/review

@github-actions

Copy link
Copy Markdown

AI Review

suggestion: not available
decision: not available

This PR is too large for AI review (over 300 files or 200000 diff characters). The label stays as it is: you can make the PR smaller, for example by clearing notebook outputs or splitting it, and comment /review again, or a maintainer can review it as it is.

dhtrg added a commit to Rainie-Hu/PyHealth that referenced this pull request Nov 27, 2026
Part 4 of 4 of sunlabuiuc#1609. Builds on part 3.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>
github-actions Bot pushed a commit that referenced this pull request Nov 27, 2026
…1638)

* Add the Early Fusion and ConVIRT models for MedMod

Part 2 of 4 of #1609.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>

* Put frozen encoders in eval mode at construction; reject augmentation without images

* Handle a one-sample ConVIRT batch; clarify EHR length, EHR-only and augmentation docs

* Address the third AI review of part 2: ConVIRT docs and a normalization test

The ehr_feature_key docstring no longer names a processor from an
unmerged PR. The one-sample batch note now says the batch gives no gradient
but is not a full no-op (BatchNorm statistics, optimizer momentum). A new
test checks that cxr_mean and cxr_std undo ImageNet normalization.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>

---------

Co-authored-by: dhtrg <87437721+dhtrg@users.noreply.github.com>
NathanCYee pushed a commit to NathanCYee/PyHealth that referenced this pull request Nov 28, 2026
…unlabuiuc#1638)

* Add the Early Fusion and ConVIRT models for MedMod

Part 2 of 4 of sunlabuiuc#1609.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>

* Put frozen encoders in eval mode at construction; reject augmentation without images

* Handle a one-sample ConVIRT batch; clarify EHR length, EHR-only and augmentation docs

* Address the third AI review of part 2: ConVIRT docs and a normalization test

The ehr_feature_key docstring no longer names a processor from an
unmerged PR. The one-sample batch note now says the batch gives no gradient
but is not a full no-op (BatchNorm statistics, optimizer momentum). A new
test checks that cxr_mean and cxr_std undo ImageNet normalization.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>

---------

Co-authored-by: dhtrg <87437721+dhtrg@users.noreply.github.com>

Copy link
Copy Markdown
Collaborator

/review


Generated by Claude Code

dhtrg added a commit to Rainie-Hu/PyHealth that referenced this pull request Nov 30, 2026
Part 4 of 4 of sunlabuiuc#1609. Builds on part 3.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>
dhtrg added a commit to Rainie-Hu/PyHealth that referenced this pull request Nov 30, 2026
Part 1 of 4 of sunlabuiuc#1609.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>
dhtrg added a commit to Rainie-Hu/PyHealth that referenced this pull request Nov 30, 2026
…ples

Part 3 of 4 of sunlabuiuc#1609. Builds on parts 1 and 2.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>
Same code as sunlabuiuc#1637-sunlabuiuc#1640 after their reviews. sunlabuiuc#1638 is merged as 1aaeaea.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>
dhtrg added a commit to Rainie-Hu/PyHealth that referenced this pull request Nov 30, 2026
Part 4 of 4 of sunlabuiuc#1609. Builds on part 3.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>
Faster dataset tests from sunlabuiuc#1637.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>
dhtrg added a commit to Rainie-Hu/PyHealth that referenced this pull request Nov 30, 2026
…ples

Part 3 of 4 of sunlabuiuc#1609. Builds on parts 1 and 2.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>
dhtrg added a commit to Rainie-Hu/PyHealth that referenced this pull request Nov 30, 2026
Part 4 of 4 of sunlabuiuc#1609. Builds on part 3.

Co-authored-by: rainiehu <57816421+Rainie-Hu@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: automatic The PR will go through automatic review process.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants