Skip to content

Add Dynamic survival analysis for early event prediction (MIMIC-III decompensation) - #1558

Open
shusted3-gt wants to merge 13 commits into
sunlabuiuc:masterfrom
shusted3-gt:dsa-model
Open

shusted3-gt wants to merge 13 commits into
sunlabuiuc:masterfrom
shusted3-gt:dsa-model

Conversation

@shusted3-gt

@shusted3-gt shusted3-gt commented Nov 20, 2026 •

Copy link
Copy Markdown

Contributors:

Georgia Tech CSE/OAN 6250 (Big Data for Health), Fall 2026.

Contribution Type: Full Pipeline (model + task + metrics + pipeline-level ablation on the existing MIMIC3Dataset; no new dataset class, mimic3_icu.yaml adds the extra columns the task needs).

Paper: Yèche, Burger, Veshchezerova, Rätsch. "Dynamic Survival Analysis for Early Event Prediction." CHIL 2024. https://proceedings.mlr.press/v248/yeche24a.html

Description:

Reproduces part of the paper in PyHealth on the MIMIC-III decompensation task (death within horizon hours, predicted every hour, cohort of Harutyunyan et al. 2019).

  • Task (DecompensationMIMIC3): hourly samples from 17 chart/lab variables. label_mode="survival" adds the per-step time_to_event and event labels the survival model needs. The default "binary" mode is unchanged.
  • Model (DynamicSurvivalRNN, built on RNN_V2): predicts a hazard for each of the next horizon hours, trained with the survival loss truncated to the horizon (Eq. 3-4 of the paper). It returns the risk of the event within the horizon, 1 - prod(1 - hazard_k). The hazard head's bias can be initialized to the average hazard of each hour (Section 3.2).
  • Metrics (pyhealth.metrics.event_alarm): the event-level evaluation of Section 4.1: an alarm policy with silencing, alarm precision, event recall at a given alarm precision, first-alarm distance, and the area under the alarm precision / event recall curve.
  • Ablation (examples/mimic3_decompensation_dsa_ablation.py): EEP baseline vs. DSA with and without hazard-bias initialization, same encoder and patient split, over seeds.

The survival loss, the bias initialization and the event-level metrics are implemented from the paper's equations and sections, and the task follows its MIMIC-III decompensation setup.

Not implemented (future work): survTLS, the imminent-prioritization alarm policy, HiRID and the other tasks. Only the fixed-horizon alarm policy is used.

Related: pyhealth/tasks/medmod_decompensation_mimic4.py implements the same 24 h decompensation definition for the MedMod multimodal benchmark on MIMIC-IV with chest X-rays and only a binary label. DecompensationMIMIC3 is separate because it runs on MIMIC-III with the 17 benchmark variables and adds the per-step time-to-event and event labels that the survival model needs.

Files to Review:

  • pyhealth/tasks/decompensation_mimic3.py - task: cohort selection, benchmark cleaning rules, hourly samples, binary and survival label modes
  • pyhealth/models/dynamic_survival_rnn.py - model: hazard head, truncated survival loss, risk, bias initialization
  • pyhealth/models/dynamic_survival.py - NumPy reference for the targets, hazard prior and loss (what the model is tested against)
  • pyhealth/metrics/event_alarm.py - event-level alarm metrics
  • examples/mimic3_decompensation_dsa_ablation.py - runnable end-to-end ablation (demo-data command in its docstring)
  • examples/mimic3_decompensation_rnn_v2.py - baseline classifier and horizon ablation
  • pyhealth/datasets/configs/mimic3_icu.yaml - extra MIMIC-III columns for the task
  • tests/core/test_decompensation_mimic3.py, test_dynamic_survival.py, test_dynamic_survival_rnn.py, test_event_alarm.py - 84 tests on synthetic data
  • docs/api/... and the __init__.py files of pyhealth/models, pyhealth/metrics and pyhealth/tasks - docs pages and exports

Results
(30% sample of MIMIC-III patients: 8,624 / 1,848 / 1,862 stays for train / validation / test, 189 test events; horizon 24 h, 5 seeds, 20 epochs; mean over seeds):

EEP DSA + bias init DSA, no bias init Paper (EEP / DSA)
Time-step PR-AUC 0.332 0.323 0.276 0.371 / 0.362
Alarm/event AuPRC 0.533 0.535 0.399 0.674 / 0.700
  • DSA is slightly below EEP at the time step (as in the paper) and indistinguishable from it at the event level. The paper's event-level gain is not reproduced, and absolute alarm/event AuPRC is lower than the paper's.
  • Bias initialization speeds up training: without it the model never reached 60% alarm precision after 10 epochs and trails (0.40 vs. 0.53) after 20. Whether longer training closes the gap was not tested.

Limitations:
30% patient sample (disk limits for the full event cache), one fixed patient split, 20 epochs, a small GRU. The EEP and bias-init arms changed little between 10 and 20 epochs; the no-bias-init arm may not be converged. Cohort and alarm-evaluation details may differ from the paper's code, so absolute numbers are not directly comparable.

Event recall counts every observed death as an event, including deaths long after the last step of the stay (for example after ICU discharge) that no alarm can warn of. This caps recall below 1 and understates alarm/event AuPRC and event recall relative to a definition that excludes them; the definition is documented in the event_alarm module docstring.

@shusted3-gt
shusted3-gt marked this pull request as ready for review November 21, 2026 20:30
@solarsys solarsys added the status: manual Pending maintainer's review label Nov 21, 2026
@shusted3-gt

Copy link
Copy Markdown
Author

/review

@shusted3-gt

Copy link
Copy Markdown
Author

/review

@shusted3-gt

Copy link
Copy Markdown
Author

I rebased this branch on the latest master (no conflicts) and re-ran the tests and the PR rules check locally: 80 tests pass and the contribution rules pass. The build and contribution-rules workflows are still waiting for approval because this is my first contribution to the repo. Could a maintainer please approve them so the checks can run? Happy to make any recommended adjustments, thank you!

@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 86.30% (45810 / 53082 lines)

Modified file Coverage Lines
pyhealth/metrics/__init__.py 100.00% 20 / 20
pyhealth/metrics/event_alarm.py 98.88% 88 / 89
pyhealth/models/__init__.py 100.00% 90 / 90
pyhealth/models/dynamic_survival.py 100.00% 29 / 29
pyhealth/models/dynamic_survival_rnn.py 96.67% 87 / 90
pyhealth/tasks/__init__.py 98.04% 100 / 102
pyhealth/tasks/decompensation_mimic3.py 90.84% 228 / 251

Workflow run · 40057be

@jhnwu3

jhnwu3 commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 40057be

if self.label_mode == "survival":
self.task_name = f"{self.task_name}_survival"

def pre_filter(self, df: Any) -> Any:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[medium] pre_filter has no test

DecompensationMIMIC3.pre_filter drops every chartevents/labevents row whose itemid is not in ITEMS, and keeps all other event types. The only test in tests/core/test_decompensation_mimic3.py covers _missing, so a wrong filter (for example a cast or a keep-mask error) would silently change every sample and no test would fail. Add a test with a small pl.LazyFrame that checks the mapped itemids are kept, unmapped ones are dropped, and other event types pass through, and that missing columns raise ValueError.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in cf289f6: tests for pre_filter (mapped items kept, unmapped and non-numeric dropped, other tables pass through, missing columns raise ValueError) and an end-to-end MIMIC3Dataset.set_task test.

[2.0]
"""
stays = _check_stays(scores, times, time_to_event, event, horizon)
n_events = sum(s[3] for s in stays)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[medium] Events no alarm can reach are counted in the recall denominator

n_events counts every stay with an observed death (event == 1). The task sets event to 1 for deaths after ICU discharge too, as the test 'late death' expects. A death more than horizon hours after the last step can never fall in an alarm window, so those events cap event recall below 1 and lower alarm_event_auprc and event_recall@​p. The reported numbers (for example 0.533 vs 0.674 in the paper) are therefore understated by this definition, and the description does not mention it. Fix: count only events that some step of the stay could warn of, such as deaths within horizon hours of the stay's end, or state the definition and the share of such events in the description and docs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented in the event_alarm module docstring and in the PR description (cf289f6). The metric itself is unchanged, so the reported numbers stay as they are.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Review

suggestion: approve
decision: human review

Thank you for this contribution, Sarah and Allyson. It adds a MIMIC-III decompensation task with binary and survival label modes, a DynamicSurvivalRNN built on RNN_V2, event-level alarm metrics, a runnable ablation, docs and about 80 tests. It also reports its results, including the ones that don't match the paper, which I appreciate.

This looks ready to merge. All the components are new and self-contained, and the exports in the __init__.py files are registration only. The link to RNN_V2 (subclassing it and using its helpers) is covered by the description's "built on RNN_V2". Coverage meets the bar: 86.3% for the repository, 98.9% for event_alarm.py, 100% for dynamic_survival.py, 96.7% for dynamic_survival_rnn.py and 90.8% for decompensation_mimic3.py. The docs match the code.

Optional suggestions:

  • Add a small test for DecompensationMIMIC3.pre_filter, for example on a tiny polars LazyFrame. Today only _missing is tested, and a wrong item filter would silently change every sample.
  • Say in the description or the event_alarm docs how events are counted for recall. Deaths that happen long after ICU discharge count as events, but no step can warn of them. This caps recall below 1 and may explain part of the gap to the paper's alarm/event numbers.
  • Mention that pyhealth/tasks/medmod_decompensation_mimic4.py implements a similar 24 h label for MIMIC-IV, and why this MIMIC-III task is separate.
  • Consider a small test that goes through MIMIC3Dataset.set_task end to end. At the moment only the example scripts cover it.

Reviewed commit 40057be

@solarsys

Copy link
Copy Markdown
Collaborator

Why this PR has status: manual: #1558 and #1646 both add a dynamic survival analysis model and a MIMIC-III decompensation task (tasks/decompensation_mimic3.py in both), so a maintainer has to pick one.

To help the maintainers decide, please comment here with what your PR does that the others don't, such as fidelity to the paper, results or test coverage. Whichever PR isn't picked can still contribute its unique parts on top of the chosen one. The decision is tracked in #1668.

The AI review suggested approve on an older commit. Please rebase (registration lines only) so it can be reviewed again on the latest code.

@solarsys

Copy link
Copy Markdown
Collaborator

Picked as PyHealth's dynamic survival analysis model and MIMIC-III decompensation task. We compared this with #1646. Yours is the more correct and complete of the two:

  • It ports the benchmark cohort rules: no transfers, one ICU stay per admission, adults only.
  • It labels hourly from hour 4 without leakage.
  • It implements the per-hour hazard head with the truncated loss and hazard-prior initialization.
  • It adds event-level alarm metrics.
  • It has 84 synthetic tests, including an end-to-end set_task run.

The AI review suggested approve on 40057be, and this PR was opened first.

Before it can merge:

  1. Rebase on master. The conflicts are only in pyhealth/tasks/__init__.py and docs/api/tasks.rst.
  2. CI hasn't run on your latest commit, because it's waiting for a maintainer to approve the workflow run. We've flagged it.
  3. After the rebase, a fresh AI review on the new commit.
  4. Optional: a docstring note that DynamicSurvivalRNN relies on RNN_V2 internals (_FieldRNN, _groups, _at_labels).

We've asked #1646 to contribute survTLS smoothing, PMF and survival outputs, and extra alarm policies on top of this.

@shusted3-gt

Copy link
Copy Markdown
Author

Completed rebase on master (no conflicts) and re-ran the tests and the PR rules check locally: 84 tests pass and the contribution rules pass.

@shusted3-gt

Copy link
Copy Markdown
Author

/review

@shusted3-gt

Copy link
Copy Markdown
Author

/review

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: manual Pending maintainer's review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants