Skip to content

[BTY-34] Add Delphi-2M generative time-to-event transformer with MEDS and MIMIC-IV tasks - #1587

Merged
github-actions[bot] merged 3 commits into
sunlabuiuc:masterfrom
etikhonov-dev:feat/bty-34-delphi-2m
Nov 24, 2026
Merged

github-actions[bot] merged 3 commits into
sunlabuiuc:masterfrom
etikhonov-dev:feat/bty-34-delphi-2m

Conversation

@etikhonov-dev

@etikhonov-dev etikhonov-dev commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Adds 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).
  • Continuous age replaces positional embeddings, co-occurring timestamps are masked when 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 and dt_pred use every token except padding: dt_pred is exp(-logsumexp) of those logits, and t_min floors only the time loss. generate_trajectory draws the same kind of race after no_repeat and top_k have removed tokens, so a sampled wait can be longer than dt_pred.
  • DelphiTrajectoryMEDS and DelphiTrajectoryMIMIC4 emit the same schema (codes, ages → next_code, time_to_next), so examples/delphi_disease_trajectory.py and tests/core/test_delphi.py intentionally pair Delphi with those tasks. DelphiTrajectoryUKB reads pre-extracted UK Biobank events and does not download credentialed data. It subclasses DelphiTrajectoryMEDS on purpose: those events have the same timestamp-and-code shape as MEDS and differ only by event_type="ukb" and the birth-code set (UKB_BIRTH plus the MEDS and SNOMED birth codes), so birth anchoring and prefix samples stay one implementation in pyhealth/tasks/delphi_trajectory.py.
  • Delphi subclasses BaseModel. DelphiTrajectoryMEDS and DelphiTrajectoryMIMIC4 subclass BaseTask. 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_pred equal to the full-vocabulary mean wait for t_min of 1 and 2.5, and the labeled time loss equal to that same rate)
  • Coverage ≥ 85% on pyhealth/models/delphi.py (95%) and pyhealth/tasks/delphi_trajectory.py (91%)
  • ruff check on the new modules, tests, and example
  • python tools/check_pr_rules.py --base upstream/master --head HEAD
  • python examples/delphi_disease_trajectory.py prints multiclass accuracy and a simulated trajectory

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.
@github-actions github-actions Bot mentioned this pull request Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 86.29% (45771 / 53043 lines)

Modified file Coverage Lines
pyhealth/models/__init__.py 100.00% 90 / 90
pyhealth/models/delphi.py 95.24% 240 / 252
pyhealth/tasks/__init__.py 98.04% 100 / 102
pyhealth/tasks/delphi_trajectory.py 91.28% 157 / 172

Workflow run · 4929bb0

@jhnwu3 jhnwu3 added the status: automatic The PR will go through automatic review process. label Nov 23, 2026
@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.

Previous review

Reviewed commit 7b578a7

Comment thread pyhealth/tasks/delphi_trajectory.py Resolved
Comment thread pyhealth/models/delphi.py Resolved
Comment thread pyhealth/tasks/__init__.py Resolved

@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.

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:

  1. DelphiTrajectoryUKB subclasses DelphiTrajectoryMEDS and 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.
  2. dt_pred is documented as the expected number of days. In the code, exp(-lse) is computed after lse has been rewritten as -log(exp(-lse) + t_min). So it returns the expected wait plus t_min, which is one extra day with the default t_min=1. Please either subtract t_min or change the docstring to say what it returns.

Optional suggestions:

  • Add a docs page for DelphiTrajectoryUKB under 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.
Comment thread pyhealth/tasks/delphi_trajectory.py Resolved
Comment thread pyhealth/models/delphi.py Resolved
Comment thread pyhealth/tasks/__init__.py Resolved
@etikhonov-dev

Copy link
Copy Markdown
Contributor Author

/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.

Previous review

Reviewed commit d5fa311

Comment thread pyhealth/models/delphi.py Resolved
Comment thread pyhealth/tasks/delphi_trajectory.py Resolved
Comment thread pyhealth/tasks/delphi_trajectory.py Resolved
Comment thread pyhealth/tasks/delphi_trajectory.py Resolved

@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.

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:

  1. Make the waiting-time rate consistent between training and sampling. In forward, the labeled-target time loss and dt_pred use logsumexp over class_logit, which covers only the label vocabulary. The prefix time loss and generate_trajectory use the full vocabulary, with only padding masked out. Codes that appear in histories but never as a next_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 and dt_pred from the same full-vocabulary logits at the last real token, with padding masked as in the prefix loss. Alternatively, restrict generate_trajectory to the same label tokens. If you keep the current behavior on purpose, say so in the Delphi and generate_trajectory docstrings and the description. State that sampled waits and dt_pred are 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 DelphiTrajectoryUKB expects: an event_type="ukb" table with a code column and a timestamp.
  • The DelphiTrajectoryMIMIC4 docstring says the death clamp applies to a date-only dod. The code applies it to deathtime as well, so the docstring could say that.
  • A small test that runs DelphiTrajectoryMIMIC4 through MIMIC4EHRDataset would 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.
Comment thread pyhealth/models/delphi.py Resolved
Comment thread pyhealth/tasks/delphi_trajectory.py Resolved
Comment thread pyhealth/tasks/delphi_trajectory.py Resolved
Comment thread pyhealth/tasks/delphi_trajectory.py Resolved
@etikhonov-dev

Copy link
Copy Markdown
Contributor Author

/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 4929bb0

Comment thread pyhealth/models/delphi.py Resolved

@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: 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.py is at 95.2%, delphi_trajectory.py at 91.3% and the whole repository at 86.3%.
  • Sharing: DelphiTrajectoryUKB subclasses DelphiTrajectoryMEDS. 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_trajectory docstring says the wait uses the same rate as dt_pred and the time loss. In _rollout, though, the wait is drawn after no_repeat and top_k have set some logits to -inf. With the default no_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_nll function it checks. A hand-computed value would make it a stronger check.
  • All-padding rows: generate_trajectory starts 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

@etikhonov-dev

Copy link
Copy Markdown
Contributor Author

/merge

@github-actions
github-actions Bot merged commit b3760fe into sunlabuiuc:master Nov 24, 2026
4 checks passed
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.

3 participants