Repository navigation
Add GA2M model and 48h MIMIC-III mortality task - #1656
Conversation
Reproduces Hegselmann et al. (2020), "An evaluation of the doctor-interpretability of generalized additive models with interactions" (PMLR 126): a torch GA2M (quantile-binned additive 1D/2D terms trained by SGD, not boosting) plus the 48-hour MIMIC-III in-hospital mortality task it was evaluated on (mean/std of 17 clinical variables over the first 48h, kept in raw units with an explicit missing-value bin rather than z-scored).
- pyhealth/models/ga2m.py: GA2M model, plus rank_pairs_by_contribution_variance/all_pairs for the paper's pairwise-interaction selection procedure.
- pyhealth/tasks/in_hospital_mortality_48h_mimic3.py: cohort filtering (age/LOS), itemid-to-variable mapping, categorical (GCS/capillary refill) and unit harmonization, and outlier filtering, adapted from the public YerevaNN/mimic3-benchmarks resources used as reference. - Tests (synthetic data only, tests/core/test_ga2m.py and tests/core/test_in_hospital_mortality_48h_mimic3.py).
- examples/mimic3_mortality_ga2m.py: bin-count ablation (B in {32,64,128,256}) and the paper's two-phase pair-selection procedure, runnable on synthetic data by default; results in examples/mimic3_mortality_ga2m_results.md.
- API docs for both new components.
- GA2M: excluded_features drops 1D terms (held at zero, no gradient, omitted from get_shape_functions) and rejects pairs that use them. smoothness_penalty() adds squared second differences of the risk curves (missing bin excluded). - Example: --stages; clinician-pruning ablation (without pH terms, without std-feature pairs, without both); logistic-regression baseline on the same 34 features; early stopping on validation ROC-AUC; --smoothness, --optimizer, --weight-decay, --patience and --pair-bins; tuned defaults (Adam lr 0.01, batch 256, 30 epochs, smoothness 100, 8-bin pair tables). - Results notes: aggregate metrics on MIMIC-III v1.4 (28,919 ICU stays) for the bin ablation, pair selection, clinician pruning and the baseline, and how the settings were chosen. - Tests: InHospitalMortality48hMIMIC3 tests build Patient objects in memory instead of a CSV-backed dataset per class (slowest test ~8 s to ~0.01 s); new tests for excluded features and the smoothness penalty. - Lint the new modules to the repo's ruff config.
Ga2m finish
Test CoverageTotal: 87.20% (51136 / 58643 lines)
|
|
/review |
| public resources of the original Harutyunyan/Hegselmann benchmark code | ||
| (``YerevaNN/mimic3-benchmarks``, ``mimic3benchmark/resources/*`` and | ||
| ``mimic3models/resources/channel_info.json``), used as a reference per the | ||
| course's allowed use of original authors' code; the official cohort-level |
There was a problem hiding this comment.
[low] Module docstring has course/team process notes and misses the MIT notice for adapted code
The module docstring says the itemid tables are used "per the course's allowed use of original authors' code" and that "the team" runs the cohort reproduction locally. Those are process notes that will mean nothing to later readers, so drop them. The helpers _rescale_fio2 (line 82) and _rescale_oxygen_saturation are adapted from MIT-licensed YerevaNN code, and the MIT license requires the copyright notice in substantial copies. pyhealth/tasks/ehr_cxr_mimic4.py (lines 13-16) keeps that notice. Add the same attribution here.
| least the ``chartevents`` table (``labevents`` is also used, for | ||
| glucose and pH readings that are more commonly charted as labs). | ||
|
|
||
| For each ICU stay with a known outcome, age >= 18 at admission, and a |
There was a problem hiding this comment.
[low] Class docstring says age is checked at admission; code checks age at ICU intime
__call__ computes age_years from intime, the ICU admission time, and the min_age_years attribute doc also says ICU admission. The class docstring's "age >= 18 at admission" is ambiguous. Say "at ICU admission" to match the code.
| quantiles = torch.linspace(0, 1, n_edges + 2)[1:-1] | ||
| return torch.quantile(observed.double(), quantiles.double()).float() | ||
|
|
||
| def fit_bins(self, train_data: DataLoader | SampleDataset) -> GA2M: |
There was a problem hiding this comment.
[low] Public fit_bins silently misaligns a trained model
Calling fit_bins after training replaces bin_edges, but terms_1d and terms_2d keep their old bin meanings, so predictions change silently. The docstring says to call it before training, but nothing enforces that. Either refuse when the model has been trained, or state in the docstring that the model must be retrained after a refit.
| stages ran before) and prints its test metrics.""" | ||
| train_data, val_data, test_data = splits | ||
| torch.manual_seed(args.seed) | ||
| model = GA2M(dataset=train_data, **model_kwargs) |
There was a problem hiding this comment.
[low] Example never passes feature_names, so shape-function output is keyed x0..x33
fit_and_evaluate builds every GA2M without feature_names, so get_shape_functions() returns keys like x0. The GA2M docs say this output is for a clinician to inspect. Pass feature_names=FEATURE_NAMES here so the output is labeled.
| out_present = self.model(features=present)["logit"] | ||
| self.assertFalse(torch.allclose(out_missing, out_present)) | ||
|
|
||
| def test_fit_bins_changes_edges_and_uses_train_split_only(self): |
There was a problem hiding this comment.
[low] Test name claims a train-split-only check that is not asserted
test_fit_bins_changes_edges_and_uses_train_split_only only checks that the edges changed and that the shape is the same. It never checks that the edges come from the train split only. Rename it or assert the edges against a train-only computation.
| | without std-feature pairs | 15 | 0.791 (0.799) | 0.407 | | ||
| | without both | 11 | 0.792 (0.801) | 0.408 | | ||
|
|
||
| Removing the flagged terms costs no accuracy. The model without both keeps |
There was a problem hiding this comment.
[low] Overclaims 'for free' from single-seed differences
The pruned variants differ from the unpruned model by 0.001 to 0.002 ROC-AUC, all from one seed with no variance estimate. Soften "costs no accuracy" and "for free" to say the difference is within noise. Also state that the reported real-data numbers depend on the CHARTEVENTS prefilter at line 35, which the script does not do, so the full table may not reproduce from the script alone.
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: approve
Thank you for this contribution. It adds a torch GA2M model with quantile-binned 1D and 2D terms and an explicit missing bin. It also adds a 48h MIMIC-III mortality task with 34 summary features, plus an example script, a results write-up, docs and tests. It looks ready to merge.
The change is self-contained. It builds on BaseModel and BaseTask without modifying them. The shared files change only by export lines in the two init.py files and by docs entries. Coverage is above the bar: 94% for ga2m.py, 90% for the task file, and 87% for the whole repository. The sharing between the example, the model and the task is internal to the new feature, and the description explains it.
Optional suggestions:
- The task module docstring mentions "the course's allowed use of original authors' code" and work run by "the team locally". You could reword this to describe the reference to YerevaNN/mimic3-benchmarks neutrally. Consider adding that project's license notice for the adapted mappings and thresholds.
fit_binsis public. Calling it on an already-trained model silently changes the bins the learned weights refer to. A short docstring warning would help.- The results write-up draws conclusions from single-seed differences of about 0.001 to 0.005 AUC. Softer wording, such as "no measurable loss", would be safer.
- The results also depend on a CHARTEVENTS prefilter that is outside the repository. A short script or a command in the write-up would make them reproducible.
test_missing_value_uses_reserved_binhas a name that overstates what it checks. You could rename it or tighten it.
Reviewed commit a176125
…mortality Both upstream PRs added entries to the same task toctree as this branch; keep both. The other index files merged cleanly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Model + Standalone Task
This adds a torch replacement of GA2M (quantile-binned additive 1D/2D terms trained jointly by SGD, not cyclic gradient boosting like InterpretML's EBM) and the 48 hour MIMIC-III in-hospital-mortality task from the paper (mean/std of 17 clinical variables over the first 48h, 34 raw-unit features, missing values kept as an explicit sentinel bin rather than z-scored). The task's itemid-to-variable mapping, categorical (GCS/capillary-refill) value mapping, and outlier/valid-range thresholds are adapted from the public
YerevaNN/mimic3-benchmarksresources, used as a reference. The example script reproduces the paper's bin-count ablation (B in {32, 64, 128, 256}), the two-phase pairwise-interaction-selection procedure (train all candidate pairs at coarse bins, rank by contribution variance, retrain with the top 34), and a clinician-pruning ablation (retrain without risk curves flagged as hard to interpret). Real MIMIC-III results and the comparison against the paper's numbers are in 'examples/mimic3_mortality_ga2m_results.md`.