Repository navigation
[BTY-34] Add Delphi-2M generative time-to-event transformer with MEDS and MIMIC-IV tasks - #1587
Conversation
Ports the Nature 2025 trajectory model onto BaseModel and BaseTask so Trainer can score the next disease and the waiting time without a UK Biobank download.
Test CoverageTotal: 86.29% (45771 / 53043 lines)
|
|
/review |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: need change
decision: need change
Thank you for adding Delphi-2M, with the MEDS, MIMIC-IV and UK Biobank trajectory tasks, a runnable example and docs. The change is well tested, and the model and tasks stay self-contained. Coverage is 95% for models/delphi.py and 91% for tasks/delphi_trajectory.py, and 86% for the whole repository. All three are above the bar (80% per file, 60% overall). It is close to ready.
To get this merged:
DelphiTrajectoryUKBsubclassesDelphiTrajectoryMEDSand reuses its event reading and birth logic. The description doesn't mention this. Please either confirm in the description that the sharing is intended and say why, or make the UKB task self-contained by copying the code.dt_predis documented as the expected number of days. In the code,exp(-lse)is computed afterlsehas been rewritten as-log(exp(-lse) + t_min). So it returns the expected wait plust_min, which is one extra day with the defaultt_min=1. Please either subtractt_minor change the docstring to say what it returns.
Optional suggestions:
- Add a docs page for
DelphiTrajectoryUKBunder docs/api/tasks/, and list it in docs/api/tasks.rst, as you did for the other two tasks. - Add a short note in the docs that the Delphi paper and repository are the source for the model.
Reviewed commit 7b578a7
The time loss still floors the log-rate by t_min, but dt_pred is the mean of the competing exponential, which is what trajectory sampling draws. The UK Biobank task keeps reusing the MEDS reader because the event shape is the same, and that reuse is now documented.
|
/review |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: need change
decision: need change
Thank you for this contribution. Delphi-2M, with MEDS, MIMIC-IV and UK Biobank trajectory tasks, is a useful addition. The docs cite the paper and reference code. Coverage is 95% for delphi.py and 91% for delphi_trajectory.py, and the whole repository is at 86%. All three are above the bar. Subclassing DelphiTrajectoryMEDS for the UK Biobank task is explained in the description.
To get this merged:
- Make the waiting-time rate consistent between training and sampling. In
forward, the labeled-target time loss anddt_preduselogsumexpoverclass_logit, which covers only the label vocabulary. The prefix time loss andgenerate_trajectoryuse the full vocabulary, with only padding masked out. Codes that appear in histories but never as anext_code, such as<unk>, therefore add to the sampled rate but not to the trained or predicted rate. Compute the target-step competing-risks loss anddt_predfrom the same full-vocabulary logits at the last real token, with padding masked as in the prefix loss. Alternatively, restrictgenerate_trajectoryto the same label tokens. If you keep the current behavior on purpose, say so in theDelphiandgenerate_trajectorydocstrings and the description. State that sampled waits anddt_predare not on the same rate. Then add a test that shows the two agree, or states the difference.
Optional suggestions:
- The MIMIC-IV task ignores
icd_version, so ICD-9 and ICD-10 codes for the same condition end up as separate tokens. A note in the docstring would help. - Document the table layout
DelphiTrajectoryUKBexpects: anevent_type="ukb"table with acodecolumn and a timestamp. - The
DelphiTrajectoryMIMIC4docstring says the death clamp applies to a date-onlydod. The code applies it todeathtimeas well, so the docstring could say that. - A small test that runs
DelphiTrajectoryMIMIC4throughMIMIC4EHRDatasetwould be worthwhile.
Reviewed commit d5fa311
The labeled time loss and dt_pred were summing only next-code logits, while prefix loss and sampling summed every token. Those rates now match. The task docs also state the MIMIC-IV ICD version split, the death-time clamp, and the UK Biobank event table.
|
/review |
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: approve
Thank you for adding Delphi-2M. The PR brings a generative time-to-event transformer, MEDS, MIMIC-IV and UK Biobank trajectory tasks, docs, an example and tests. It looks ready to merge.
- Coverage: It meets the bar.
delphi.pyis at 95.2%,delphi_trajectory.pyat 91.3% and the whole repository at 86.3%. - Sharing:
DelphiTrajectoryUKBsubclassesDelphiTrajectoryMEDS. The description explains why, so that link is fine. - Redundancy: The ETHOS model is a different kind of timeline model, so Delphi doesn't duplicate it.
- Citations: The paper and the reference code are cited in the docs and docstrings.
Optional suggestions:
- Generation docstring: The
generate_trajectorydocstring says the wait uses the same rate asdt_predand the time loss. In_rollout, though, the wait is drawn afterno_repeatandtop_khave set some logits to -inf. With the defaultno_repeat=True, sampled waits are therefore longer than that full-vocabulary rate. Saying so directly would avoid confusion. A short test of the sampled wait would also help. - Time-loss test: The labeled time-loss test recomputes its expected value with the same
_competing_nllfunction it checks. A hand-computed value would make it a stronger check. - All-padding rows:
generate_trajectorystarts from token 0 on an all-padding row. You could skip such rows or document this. - UKB task: No loader in the repo produces the UKB events, and the task is tested only on synthetic data. The docs already say so.
Reviewed commit 4929bb0
|
/merge |
Summary
Delphi, a generative time-to-event transformer for longitudinal disease trajectories (Shmatko et al., Nature 2025, https://doi.org/10.1038/s41586-025-09529-3; reference code https://github.com/gerstung-lab/Delphi).mask_ties=True, and the loss joins next-event cross-entropy with an exponential competing-risks wait. Cross-entropy uses the label vocabulary. The time loss anddt_preduse every token except padding:dt_predisexp(-logsumexp)of those logits, andt_minfloors only the time loss.generate_trajectorydraws the same kind of race afterno_repeatandtop_khave removed tokens, so a sampled wait can be longer thandt_pred.DelphiTrajectoryMEDSandDelphiTrajectoryMIMIC4emit the same schema (codes,ages→next_code,time_to_next), soexamples/delphi_disease_trajectory.pyandtests/core/test_delphi.pyintentionally pairDelphiwith those tasks.DelphiTrajectoryUKBreads pre-extracted UK Biobank events and does not download credentialed data. It subclassesDelphiTrajectoryMEDSon purpose: those events have the same timestamp-and-code shape as MEDS and differ only byevent_type="ukb"and the birth-code set (UKB_BIRTHplus the MEDS and SNOMED birth codes), so birth anchoring and prefix samples stay one implementation inpyhealth/tasks/delphi_trajectory.py.DelphisubclassesBaseModel.DelphiTrajectoryMEDSandDelphiTrajectoryMIMIC4subclassBaseTask. No shared processor, dataset, or trainer code is changed.Claims bounty
[BTY-34]on #1489.Test plan
pytest tests/core/test_delphi.py -v(synthetic MEDS parquet, MIMIC-IV patient events, Trainer, trajectory sampling,dt_predequal to the full-vocabulary mean wait fort_minof 1 and 2.5, and the labeled time loss equal to that same rate)pyhealth/models/delphi.py(95%) andpyhealth/tasks/delphi_trajectory.py(91%)ruff checkon the new modules, tests, and examplepython tools/check_pr_rules.py --base upstream/master --head HEADpython examples/delphi_disease_trajectory.pyprints multiclass accuracy and a simulated trajectory