Skip to content

MENSA on Rotterdam (1/2): RotterdamDataset, MultiEventSurvivalRotterdam and MENSA (Lillelund et al., ML4H 2025) - #1644

Merged
solarsys merged 2 commits into
sunlabuiuc:masterfrom
sosqwq:mensa-rotterdam-core
Nov 28, 2026
Merged

solarsys merged 2 commits into
sunlabuiuc:masterfrom
sosqwq:mensa-rotterdam-core

Conversation

@sosqwq

@sosqwq sosqwq commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Part 1 of 2 of #1642. #1642 was too large for AI review (251k diff characters, mostly the example script), so the dataset, task and model are split out here. The example and the experiments stay in #1642. After this PR merges, #1642 will be rebased so it shows only the example.

Contributor: Zhenshuo Xu (NetID: zx65)
Contribution type: Dataset + Task + Model (part 1 of the full pipeline contribution #1642; CS 598 DL4H Option 4, reproducibility)
Paper: Lillelund et al., "MENSA: A Multi-Event Network for Survival Analysis with Trajectory-based Likelihood Estimation", ML4H 2025 (PMLR 297).
Links: PMLR | arXiv | Reference code

Description

This PR adds multi-event survival analysis on the Rotterdam breast cancer cohort:

  • RotterdamDataset loads the full 2,982-patient cohort (10 baseline features, with separate time and indicator columns for recurrence and death) from a local rotterdam.csv. It validates the full CSV before caching and does not download anything.
  • MultiEventSurvivalRotterdam builds one sample per patient (features, event_times, event_indicators) with event times kept in raw days. Setting use_treatment=False drops the two treatment indicators (8 features instead of 10).
  • MENSA is a native PyTorch implementation of the paper's model. A shared backbone outputs a Weibull mixture for each event, and the trajectory loss penalizes predicting death before recurrence for patients who experienced both. fit_preprocessing(train) fits feature scaling and loss weights on the training split only, and predict_survival evaluates survival curves on any time grid.

Data: The cohort is public in R's survival package (survival::rotterdam). Download data/rotterdam.csv from the authors' repository, or export it from R with write.csv(survival::rotterdam, "rotterdam.csv", row.names = FALSE). Then put the file in the root directory passed to the dataset.

Relation to existing code

PyHealth already has GBSGDataset, which loads DeepSurv's pooled Rotterdam/GBSG benchmark (2,232 records, 7 features, single recurrence-free survival endpoint). RotterdamDataset instead keeps the full 2,982-patient Rotterdam cohort with recurrence and death modeled as separate events.

The task reuses parse_rotterdam_record from pyhealth/datasets/rotterdam.py so row validation lives in one place. Outside the new files, the only edits to existing code are three __init__.py exports and three docs index entries (no changes to PyHealth core).

Differences from the reference code

The model was reimplemented natively in PyTorch, adapted from thecml/mensa (MIT License, commit 6b219cd), with no imports from that repository. A few intentional differences:

  • Trajectory loss: Paper Eq. 1 and the authors' code compute the trajectory term differently. We default to the paper's version; passing trajectory_variant="reference" switches to the code's version.
  • Hyperparameters: Defaults are hidden size 32, dropout 0.25, and weight decay 1e-3, while the reference Rotterdam config uses 16, 0.5, and 1e-4.
  • Seeds: The reference code always sets the model seed to 0, for every split. Here the model seed matches the split seed.
  • Best weights: In the reference repository, saving the best weights stores a reference instead of a copy, so restoring them has no effect. Here PyHealth's Trainer restores an actual copy.

The reproduction results (E1-E4) are in #1642.

Tests

52 unit tests across three files in tests/core/, all using small synthetic data (the suite runs in about 1 second):

python -m pytest tests/core/test_rotterdam.py tests/core/test_multi_event_survival_rotterdam.py tests/core/test_mensa.py

Files to review

  • pyhealth/datasets/rotterdam.py, pyhealth/datasets/configs/rotterdam.yaml
  • pyhealth/tasks/multi_event_survival_rotterdam.py
  • pyhealth/models/mensa.py
  • pyhealth/datasets/__init__.py, pyhealth/tasks/__init__.py, pyhealth/models/__init__.py (exports only)
  • docs/api/datasets/pyhealth.datasets.RotterdamDataset.rst, docs/api/tasks/pyhealth.tasks.multi_event_survival_rotterdam.rst, docs/api/models/pyhealth.models.MENSA.rst, and index entries in datasets.rst, tasks.rst, models.rst
  • tests/core/test_rotterdam.py, tests/core/test_multi_event_survival_rotterdam.py, tests/core/test_mensa.py

@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 27, 2026
@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 87.13% (50562 / 58032 lines)

Modified file Coverage Lines
pyhealth/datasets/__init__.py 100.00% 85 / 85
pyhealth/datasets/rotterdam.py 100.00% 71 / 71
pyhealth/models/__init__.py 100.00% 102 / 102
pyhealth/models/mensa.py 95.65% 220 / 230
pyhealth/tasks/__init__.py 98.26% 113 / 115
pyhealth/tasks/multi_event_survival_rotterdam.py 100.00% 26 / 26

Workflow run · 80cf63e

@solarsys

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 80cf63e

Comment thread pyhealth/models/mensa.py
"""Penalty for predicting the later event before the earlier one.

Applies to patients who had both events. With ``variant="paper"``
(Eq. 1 of the proposal), pass ``log S_later(t_earlier)``: the model

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] Docstring cites 'the proposal' instead of the paper

The trajectory_nll docstring says the paper variant is 'Eq. 1 of the proposal'. The proposal is a private project document that readers of the library cannot see. Cite Eq. 1 of Lillelund et al. (ML4H 2025, PMLR 297) instead.

Comment thread pyhealth/models/mensa.py

@property
def label_processors(self) -> dict[str, LabelProcessor]:
"""None: the model scores its survival outputs with its own loss.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] label_processors summary says None but the property returns {}

The first line of the docstring reads 'None: the model scores its survival outputs with its own loss', but the property returns an empty dict, and the Examples section shows {}. Reword the summary to say it returns an empty dict, as the example does.

@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 this contribution. It adds a Rotterdam multi-event dataset, a recurrence/death task and a native PyTorch MENSA model, with clear docs and a careful comparison to the paper and the reference code. It looks ready to merge.

  • Self-contained components: The new dataset, task and model build on BaseDataset, BaseTask and BaseModel without modifying them. The __init__.py edits only add exports.
  • Links between components: The task's reuse of parse_rotterdam_record is named in the description with a reason (row validation in one place).
  • Overlap with GBSGDataset: The description explains how it differs.
  • Data source: The dataset cites the R survival package and the Royston and Altman paper.
  • Coverage: It meets the bar. The whole repository is at 87.13%. The added files are at 100% (rotterdam.py), 95.65% (mensa.py) and 100% (the task).

Optional suggestions:

  • In the trajectory_nll docstring, "Eq. 1 of the proposal" should read "Eq. 1 of the paper".
  • The label_processors docstring summary says "None", but the property returns {}. Aligning the two would be clearer.

Reviewed commit 80cf63e

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.

2 participants