Skip to content

Add Deep Cox Mixtures (SurvivalDCM) and a SUPPORT2 time-to-event task - #1611

Open
murphycrosby wants to merge 11 commits into
sunlabuiuc:masterfrom
murphycrosby:deep-cox-mixtures
Open

murphycrosby wants to merge 11 commits into
sunlabuiuc:masterfrom
murphycrosby:deep-cox-mixtures

Conversation

@murphycrosby

@murphycrosby murphycrosby commented Nov 25, 2026 •

Copy link
Copy Markdown

Contributor: Murphy Crosby (mcrosby8), Chidozie Opara (copara8)

Contribution Type: Full Pipeline (model + task subset; uses the existing Support2Dataset)

Paper: Nagpal, Yadlowsky, Rostamzadeh and Heller, Deep Cox Mixtures for Survival Regression, MLHC 2021. https://proceedings.mlr.press/v149/nagpal21a.html

Description: I added Deep Cox Mixtures (DCM) to PyHealth. This is my reproduction of the paper for Georgia Tech CSE 6250.

DCM is a mixture of Cox models. A shared network feeds two heads: a gate that says which hidden subgroup a patient probably belongs to, and an expert that gives a log hazard ratio for each subgroup. Each subgroup gets its own baseline survival curve, so proportional hazards only has to hold inside a subgroup. With one subgroup it's just a single deep Cox model.

What's in the PR:

  • Model, SurvivalDCM: the network, the Cox partial likelihood, Breslow baselines smoothed with a spline, the E-step with sampled assignments, and survival prediction at any times you pick. Training follows the paper's EM procedure, so you call fit_baselines before training and after every epoch.
  • Task, SurvivalDCMSupport2: SUPPORT2 set up the way the DCM authors did it: all 9,105 patients and 44 features (18 numeric, 6 one-hot categorical), with duration and event labels. This is different from the existing SurvivalTimeToEventSupport2, which uses the DeepSurv benchmark version (8,873 complete cases, 27 features).
  • Example and ablation: 5-fold cross-validation on SUPPORT2. I compare a linear Cox model, a single deep Cox model (K=1), and DCM with K=3, 4 and 6, for all patients and for non-White patients. The paper only reports its best K, so the comparison across K is new. The script runs on synthetic data if you don't give it a file.

Results on SUPPORT2 (all patients; 25th / 50th / 75th percentile horizons):

Model C_td Brier ECE
CPH 0.689 / 0.673 / 0.670 0.134 / 0.200 / 0.213 0.020 / 0.024 / 0.020
Deep Cox (K=1) 0.696 / 0.680 / 0.676 0.133 / 0.197 / 0.211 0.022 / 0.023 / 0.016
DCM (K=3) 0.753 / 0.713 / 0.679 0.124 / 0.189 / 0.211 0.018 / 0.022 / 0.029
DCM (K=4) 0.725 / 0.695 / 0.668 0.128 / 0.194 / 0.214 0.017 / 0.017 / 0.021
DCM (K=6) 0.742 / 0.702 / 0.673 0.125 / 0.192 / 0.213 0.015 / 0.021 / 0.027
Paper, DCM (K tuned) 0.743 / 0.706 / 0.675 0.126 / 0.191 / 0.212 0.018 / 0.027 / 0.026

Bootstrap standard errors are about 0.004 to 0.007 for C_td, 0.002 for the Brier score and 0.003 to 0.005 for ECE. They don't include the variation from retraining.

On C_td and the Brier score my baselines match the paper's Table 8 within about 0.003. DCM beats both baselines on discrimination at the 25th and 50th percentile horizons, same as the paper. Calibration is mixed: DCM is at least as good as the baselines at the first two horizons, but not at the 75th. For non-White patients it doesn't have the lowest calibration error, so I couldn't reproduce that claim. In the ablation, every mixture beats K=1 on C_td at the first two horizons (by 0.03 to 0.06 at the 25th percentile and 0.015 to 0.033 at the 50th). The order among K=3, 4 and 6 changed between training runs, so I can't say more than 3 subgroups helps. The full tables, including non-White patients, are in the example script's docstring. Non-White includes the 42 patients with missing race, which gives the paper's 21.02%.

Where I differ from the paper and the authors' code:

  • I fit the imputation and scaling numbers on training patients only. The authors fit them on everyone before cross-validation.
  • I refit the baseline curves once per epoch. They do it every few batches.
  • I smooth the baselines with a cubic spline, like the paper says. Their released PyTorch code uses a linear one.
  • My spline smoothing default is 1e-4. Their training code uses 1e-2.
  • I left out the optional VAE term.
  • I used fixed hyperparameters and didn't tune. Training stops early using a validation split from each training fold.
  • My calibration error uses Kaplan-Meier estimates in 10 bins of predicted survival, which isn't exactly the paper's definition.

Files to Review:

  • pyhealth/models/survival_dcm.py - the model and its three helper functions
  • pyhealth/tasks/survival_dcm_support2.py - the task
  • examples/support2_survival_survival_dcm.py - reproduction and K ablation; runs on synthetic data with no arguments
  • tests/core/test_survival_dcm.py - model tests (36, synthetic tensors, each well under 0.1 s)
  • tests/core/test_survival_dcm_support2.py - task tests (13, synthetic patients)
  • docs/api/models/pyhealth.models.survival_dcm.rst and docs/api/tasks/pyhealth.tasks.survival_dcm_support2.rst - API pages
  • docs/api/models.rst, docs/api/tasks.rst, pyhealth/models/__init__.py, pyhealth/tasks/__init__.py - one added line each

Notes:

@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 86.51% (46982 / 54307 lines)

Modified file Coverage Lines
pyhealth/models/__init__.py 100.00% 93 / 93
pyhealth/models/survival_dcm.py 97.81% 179 / 183
pyhealth/tasks/__init__.py 98.11% 104 / 106
pyhealth/tasks/survival_dcm_support2.py 98.73% 78 / 79

Workflow run · 51b127b

@solarsys solarsys added the status: manual Pending maintainer's review label Nov 26, 2026
@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 51b127b

seed: Random seed.
"""
rng = np.random.default_rng(seed)
config = Path(pyhealth.datasets.__file__).parent / "configs" / "support2.yaml"

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] Example reads Support2Dataset's YAML config, a link not explained in the description

write_synthetic_support2 reads pyhealth/datasets/configs/support2.yaml to get the column list of the Support2Dataset table. This creates a new link between this example and another component's config file, and the PR description does not mention it. The same link appears in tests/core/test_survival_dcm_support2.py:210. Please either confirm this dependency and say why in the description, or make the example self-contained by hard-coding the columns it writes. The synthetic file only needs the columns the task reads, so either choice is workable.

# Hazard at the observed time: how fast the baseline curve is falling
# relative to its height, scaled by the patient's hazard ratio. The
# small floor keeps the log finite where a curve is flat.
baseline_hazard = (-baseline_slope / baseline_survival).clamp(min=1e-8)

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] Event-free subgroup keeps a flat survival-1 baseline and can absorb censored patients

When a subgroup has no observed events in fit_baselines, breslow_baseline_survival returns an empty curve and smooth_baseline_survival returns a flat curve at 1 with slope 0. In the E-step, a censored patient's log-likelihood under that subgroup is then 0, the maximum possible, while event patients get about -18 from the 1e-8 hazard floor. So censored patients are pulled into the event-free subgroup, which then stays event-free, and its flat curve pulls predicted survival upward through the gate weights in _mixture_survival. I reasoned this from the code and did not reproduce it, but no test covers an empty or event-free subgroup, and the ECE numbers in the description are worse for DCM than for the baselines at some horizons. Please handle this case explicitly (for example, exclude subgroups with no events from the posterior or warn when one occurs) and add a test for it.


# Run the dataset's Dask workers as threads, so loading the CSV starts
# no child processes. Everything else in the loader runs unchanged.
def threaded_cluster(**kwargs):

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] Re-implements the threaded Dask loader already in tests/core/survival_test_utils.py

The repo already provides threaded_csv_loading() in tests/core/survival_test_utils.py, and test_gbsg.py and test_survival_pipeline.py use it. This test defines its own threaded_cluster and patches DaskCluster directly. Using the shared context manager would remove the duplicate and match the other survival tests.

@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, Murphy. It adds Deep Cox Mixtures (SurvivalDCM) with an EM-style training procedure. It also adds a 9,105-patient, 44-feature SUPPORT2 time-to-event task, a cross-validated example with a K ablation, tests and API docs. The model follows the paper, and the docs openly list where it differs from the authors' code. It looks ready to merge.

Coverage meets the bar. The whole repository is at 86.51%, against the 60% guideline. survival_dcm.py is at 97.81% and survival_dcm_support2.py is at 98.73%, both above the 80% guideline. The tests check hand-computed values for the Cox likelihood, the Breslow baseline and the E-step posterior.

Optional suggestions:

  • Empty subgroups: A subgroup that gets no observed events keeps a flat baseline at survival 1 with slope 0. In subgroup_posterior, a censored patient then gets a log-likelihood of 0 under that subgroup, which is higher than under any subgroup with real events. This can pull censored patients into empty subgroups and bias predictions upward. Consider a short test or a docstring note on this case, or a guard such as skipping or re-seeding empty subgroups.
  • Support2 config: The example and the task test both read pyhealth/datasets/configs/support2.yaml to get the column names for synthetic data. Since the description says the task builds on Support2Dataset, this seems intended. A sentence in the description or a comment saying so would make it explicit.
  • Test helper: The threaded Dask patch in the task test may duplicate an existing helper in the repository. Reuse it if one exists.

Reviewed commit 51b127b

@solarsys

Copy link
Copy Markdown
Collaborator

Why this PR has status: manual: four open PRs add a Deep Cox Mixtures model for SUPPORT2: #1579, #1611, #1629 and #1641. PyHealth keeps one implementation per component, 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.

This branch also conflicts with master, only in registration lines; please rebase.

@solarsys

Copy link
Copy Markdown
Collaborator

We compared the four Deep Cox Mixtures PRs and picked #1641 to merge first. Its model has a self-contained fit() that matches the authors' per-batch baseline refit, vectorized likelihoods that handle tied times, and the highest model coverage. Yours was a close second, and it has things #1641 lacks.

After #1641 merges, please rebase and turn this PR into the following.

Add:

Drop:

  • SurvivalDCM, SurvivalDCMSupport2 and their docs pages. The two tasks encode the features almost identically.

Thank you, and sorry about the overlap.

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.

2 participants