Repository navigation
Add Dynamic survival analysis for early event prediction (MIMIC-III decompensation) - #1558
shusted3-gt wants to merge 13 commits into
Conversation
38fe1a4 to
033333c
Compare
|
/review |
033333c to
40057be
Compare
|
/review |
|
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! |
Test CoverageTotal: 88.82% (61884 / 69675 lines)
|
|
/review |
| if self.label_mode == "survival": | ||
| self.task_name = f"{self.task_name}_survival" | ||
|
|
||
| def pre_filter(self, df: Any) -> Any: |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Previous review
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_missingis tested, and a wrong item filter would silently change every sample. - Say in the description or the
event_alarmdocs 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.pyimplements 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_taskend to end. At the moment only the example scripts cover it.
Reviewed commit 40057be
| if self.label_mode == "survival": | ||
| self.task_name = f"{self.task_name}_survival" | ||
|
|
||
| def pre_filter(self, df: Any) -> Any: |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
|
Why this PR has 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. |
|
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:
The AI review suggested approve on 40057be, and this PR was opened first. Before it can merge:
We've asked #1646 to contribute survTLS smoothing, PMF and survival outputs, and extra alarm policies on top of this. |
cf289f6 to
3293448
Compare
|
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. |
|
/review |
3293448 to
82eaff4
Compare
|
/review |
1 similar comment
|
/review |
| label_time = label[1] | ||
| label_sample, _ = _groups(label[2]) | ||
| parts = [] | ||
| for field in self.sequence_fields: |
There was a problem hiding this comment.
[low] Forward does not handle sequence fields off the timeline, unlike RNN_V2
RNN_V2.forward (pyhealth/models/rnn.py:1057-1061) gives a sequence field that is not on the label timeline its per-sample summary instead of label-time states. This forward loop sends every sequence field through _at_labels and asserts states is not None (line 228). So a dataset with an extra non-timeline sequence field either trips that assert or reads the field at label times. The class docstring says the encoder takes 'the same inputs' as RNN_V2. The shipped task has only one field, so this is not hit today. Fix: mirror the timeline branch from RNN_V2, or check in init that every sequence field is on the timeline and raise otherwise.
| ] | ||
| adm = admissions[stay["hadm_id"]] | ||
| deathtime = _dt(adm.get("admissions/deathtime")) | ||
| mortality = in_hospital_mortality( |
There was a problem hiding this comment.
[low] Death with dod but no deathtime is labeled as a censored survivor
mortality comes from in_hospital_mortality, which also uses dod, but lived uses only deathtime. For a stay with dod inside the admission and no deathtime, mortality is 1 and lived is None. decompensation_steps then gives event=0 and label=0 at every step, so the patient is censored at ICU end even though the death happened in hospital. The test test_late_death_and_dod_only in tests/core/test_decompensation_mimic3.py encodes this. Fix: either take the death time from dod, or count a death only when deathtime exists, and say which in the docstring.
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: human review
Thank you for this contribution. It adds a MIMIC-III decompensation task with binary and survival label modes, a DynamicSurvivalRNN with a horizon-truncated hazard loss and bias initialization, and event-level alarm metrics. It also adds two example scripts, a config, docs and 84 tests. It looks ready to merge.
- Core code: The change only adds self-contained components and their
__init__exports. It leaves the base classes,RNN_V2, the Trainer and shared utilities unchanged. - Sharing:
DynamicSurvivalRNNsubclassesRNN_V2and uses its private helpers (_FieldRNN,_groups,_at_labels). Your description says it is built onRNN_V2, so I count that as named and intended. - Redundancy: The description explains why this task differs from the MedMod MIMIC-IV decompensation task.
- Dataset source: No new dataset class is added. The paper is cited in the docstrings.
- Coverage: Total coverage is 88.82%, above the 60% bar. Every added file is above 80%:
event_alarm98.9%,dynamic_survival100%,dynamic_survival_rnn96.7% anddecompensation_mimic395.2%. - Docs and limitations: The docs match the code. The description states the limitations and the non-reproduced paper gain clearly.
Optional suggestions:
DynamicSurvivalRNN.forwardloops oversequence_fieldsand ignoresRNN_V2's off-timeline branch. A short docstring note or a guard would make this clear.- A death recorded in
dodbut with nodeathtimeis currently labeled as a censored survivor. Mentioning this in the task docstring would help users. - The example scripts aren't covered by tests. A small smoke test would help, but it isn't required.
Reviewed commit 82eaff4
|
Switched to
Optional, as noted earlier: a docstring note that |
82eaff4 to
a6a68dc
Compare
|
/review |
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.yamladds 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
horizonhours, predicted every hour, cohort of Harutyunyan et al. 2019).DecompensationMIMIC3): hourly samples from 17 chart/lab variables.label_mode="survival"adds the per-steptime_to_eventandeventlabels the survival model needs. The default"binary"mode is unchanged.DynamicSurvivalRNN, built onRNN_V2): predicts a hazard for each of the nexthorizonhours, 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).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.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 modespyhealth/models/dynamic_survival_rnn.py- model: hazard head, truncated survival loss, risk, bias initializationpyhealth/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 metricsexamples/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 ablationpyhealth/datasets/configs/mimic3_icu.yaml- extra MIMIC-III columns for the tasktests/core/test_decompensation_mimic3.py,test_dynamic_survival.py,test_dynamic_survival_rnn.py,test_event_alarm.py- 84 tests on synthetic datadocs/api/...and the__init__.pyfiles ofpyhealth/models,pyhealth/metricsandpyhealth/tasks- docs pages and exportsResults
(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):
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.