|
| 1 | +pyhealth.models.MENSA |
| 2 | +===================== |
| 3 | + |
| 4 | +Overview |
| 5 | +-------- |
| 6 | + |
| 7 | +MENSA (Multi-Event Network for Survival Analysis) predicts a survival curve |
| 8 | +for each of several events from one shared network. Each event, plus an |
| 9 | +event-free state used only in training, gets a mixture of Weibull |
| 10 | +distributions and its own residual adapter on a shared trunk. Training |
| 11 | +minimizes ``(1 - lambda) * L_surv + lambda * L_traj``: a weighted censored |
| 12 | +likelihood over all states, plus a trajectory term that penalizes predicting |
| 13 | +the later event (death) before the earlier one (recurrence) for patients who |
| 14 | +had both. ``trajectory_variant="paper"`` follows the paper's Eq. 1; |
| 15 | +``"reference"`` follows the authors' code. |
| 16 | + |
| 17 | +Paper |
| 18 | +----- |
| 19 | + |
| 20 | +Lillelund, Gharari Foomani, Sun, Qi and Greiner, `MENSA: A Multi-Event Network |
| 21 | +for Survival Analysis with Trajectory-based Likelihood Estimation`_, Machine |
| 22 | +Learning for Health (ML4H) 2025, PMLR 297 (`arXiv:2409.06525`_). This |
| 23 | +implementation is adapted from the authors' `reference code`_ (MIT License, |
| 24 | +commit 6b219cd). |
| 25 | + |
| 26 | +Inputs and outputs |
| 27 | +------------------ |
| 28 | + |
| 29 | +MENSA works with the ``"tensor"`` outputs of |
| 30 | +:doc:`../tasks/pyhealth.tasks.multi_event_survival_rotterdam`: ``features |
| 31 | +[F]``, ``event_times [K]`` and ``event_indicators [K]``, in source time units. |
| 32 | +It has no label processor, so ``mode`` is ``None`` and PyHealth's Trainer |
| 33 | +tracks only the loss; compute survival metrics from survival curves. |
| 34 | + |
| 35 | +Call ``fit_preprocessing`` with the **training split only** before training. |
| 36 | +It fits the feature standardization, the per-state loss weights and the time |
| 37 | +grid used for ``y_prob``, and stores them as buffers, so they are saved with |
| 38 | +the model. ``forward`` returns ``loss``, ``y_prob`` (survival probabilities, |
| 39 | +``[batch, K, T]`` on that grid) and ``y_true`` (times and indicators). |
| 40 | +``predict_survival(features, times)`` returns ``[batch, K, len(times)]`` |
| 41 | +curves on any time grid; it runs in evaluation mode without gradients and |
| 42 | +restores each module's previous mode. |
| 43 | + |
| 44 | +Defaults are one hidden layer of 32, dropout 0.25, three Weibull components |
| 45 | +and ``lambda = 0.25``, trained with batch size 32, learning rate 1e-3, weight |
| 46 | +decay 1e-3 and gradient clipping at norm 1.0. The training loader drops its |
| 47 | +last incomplete batch (``drop_last=True``), because BatchNorm cannot train on |
| 48 | +a batch of one. The reference code's Rotterdam config uses |
| 49 | +``hidden_dims=(16,)``, ``dropout=0.5`` and weight decay 1e-4 instead. |
| 50 | + |
| 51 | +Usage |
| 52 | +----- |
| 53 | + |
| 54 | +.. code-block:: python |
| 55 | +
|
| 56 | + import torch |
| 57 | + from pyhealth.datasets import RotterdamDataset, get_dataloader, split_by_patient |
| 58 | + from pyhealth.models import MENSA |
| 59 | + from pyhealth.trainer import Trainer |
| 60 | +
|
| 61 | + samples = RotterdamDataset(root="/path/to/folder").set_task() |
| 62 | + train, val, test = split_by_patient(samples, [0.7, 0.1, 0.2], seed=0) |
| 63 | +
|
| 64 | + model = MENSA(samples) |
| 65 | + model.fit_preprocessing(train) # training split only |
| 66 | + trainer = Trainer(model=model) |
| 67 | + trainer.train( |
| 68 | + train_dataloader=get_dataloader( |
| 69 | + train, batch_size=32, shuffle=True, drop_last=True |
| 70 | + ), |
| 71 | + val_dataloader=get_dataloader(val, batch_size=1024), |
| 72 | + epochs=1000, |
| 73 | + optimizer_params={"lr": 1e-3}, |
| 74 | + weight_decay=1e-3, |
| 75 | + max_grad_norm=1.0, |
| 76 | + monitor="loss", |
| 77 | + monitor_criterion="min", |
| 78 | + patience=20, |
| 79 | + ) |
| 80 | +
|
| 81 | + batch = next(iter(get_dataloader(test, batch_size=len(test)))) |
| 82 | + times = torch.linspace(0, 7000, 50) # days |
| 83 | + curves = model.predict_survival(batch["features"], times) # [N, 2, 50] |
| 84 | +
|
| 85 | +Example and results |
| 86 | +------------------- |
| 87 | + |
| 88 | +``examples/rotterdam_multi_event_survival_mensa.py`` reproduces the paper's |
| 89 | +Rotterdam protocol: the authors' stratified 70/10/20 split, CoxPH and |
| 90 | +DeepSurv baselines, and global/local C-index, AUC, IBS, MAE and |
| 91 | +D-calibration. It also runs a learning curve (E2) and ablations (E3, E4). |
| 92 | +To run the whole pipeline on synthetic data (no download):: |
| 93 | + |
| 94 | + python examples/rotterdam_multi_event_survival_mensa.py --synthetic |
| 95 | + |
| 96 | +With the real CSV in ``../data``:: |
| 97 | + |
| 98 | + python examples/rotterdam_multi_event_survival_mensa.py --data-root ../data \ |
| 99 | + --experiment main --seeds 0-9 --output-dir ../runs/e1 |
| 100 | + |
| 101 | +Over seeds 0-9, MENSA reaches a global C-index of 69.3 and a local C-index of |
| 102 | +90.9 (x100; paper 69.4 and 90.1). Most of the local C-index comes from the |
| 103 | +event order: predicting recurrence before death for every patient, with no |
| 104 | +model, gives 91.0. MENSA equals that on 8 of 10 seeds and is below it on 2. |
| 105 | +This reference is not an upper bound. The example's module docstring records |
| 106 | +all results, including why the default IBS varies widely across splits. |
| 107 | + |
| 108 | +API reference |
| 109 | +------------- |
| 110 | + |
| 111 | +.. autoclass:: pyhealth.models.MENSA |
| 112 | + :members: |
| 113 | + :show-inheritance: |
| 114 | + |
| 115 | +Loss and distribution functions |
| 116 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 117 | + |
| 118 | +Stateless tensor functions used by :class:`~pyhealth.models.MENSA`. |
| 119 | + |
| 120 | +.. autofunction:: pyhealth.models.mensa.mensa_loss |
| 121 | +.. autofunction:: pyhealth.models.mensa.survival_nll |
| 122 | +.. autofunction:: pyhealth.models.mensa.trajectory_nll |
| 123 | +.. autofunction:: pyhealth.models.mensa.extend_labels |
| 124 | +.. autofunction:: pyhealth.models.mensa.compute_state_weights |
| 125 | +.. autofunction:: pyhealth.models.mensa.mixture_log_weights |
| 126 | +.. autofunction:: pyhealth.models.mensa.mixture_log_survival |
| 127 | +.. autofunction:: pyhealth.models.mensa.mixture_log_density |
| 128 | +.. autofunction:: pyhealth.models.mensa.weibull_log_survival |
| 129 | +.. autofunction:: pyhealth.models.mensa.weibull_log_density |
| 130 | + |
| 131 | +.. _MENSA\: A Multi-Event Network for Survival Analysis with Trajectory-based Likelihood Estimation: https://proceedings.mlr.press/v297/lillelund26a.html |
| 132 | +.. _arXiv:2409.06525: https://arxiv.org/abs/2409.06525 |
| 133 | +.. _reference code: https://github.com/thecml/mensa |
0 commit comments