Repository navigation
[BTY-57] ProtoECGNet: multi-seed reproduction on PTB-XL - #1525
Conversation
Add examples/ecg/ptbxl/protoecgnet_contrastive_sweep.py, which trains one of ProtoECGNet's branches (Sethi et al., MLHC 2025) over several seeds and compares the black-box baseline and ProtoECGNet with and without the contrastive prototype loss, paired on the same starting weights. It writes per-run results, test predictions and a summary with paired bootstrap CIs and the paper's Table 1 numbers.
|
/review |
Test CoverageTotal: 84.69% (38726 / 45725 lines) No modified files under |
|
/review |
|
/review |
| torch.manual_seed(seed) | ||
|
|
||
|
|
||
| def fast_auroc(y_true: np.ndarray, y_prob: np.ndarray) -> np.ndarray: |
There was a problem hiding this comment.
[medium] Hand-written AUROC and branch filter have no tests
fast_auroc (rank-sum AUROC) and auroc_summary produce every reported number, and PTBXLBranchClassification (line 84) decides which ECGs enter each branch's train, validation and test sets. Neither is tested, and the script's only check is a manual --synthetic run, which shows the pipeline runs but not that the AUROC is right. Since the PR's conclusion that the contrastive loss does not help rests on these numbers, add a small test under tests/core/ (tests/core/test_pbc_neural_fine_gray_example.py shows an example script can be imported and tested). It should compare fast_auroc with sklearn roc_auc_score on random data with ties and on single-class labels, and check that the branch subclass drops ECGs with none of the branch's labels.
| parser.add_argument( | ||
| "--device", default="auto", choices=["auto", "cpu", "cuda", "mps"] | ||
| ) | ||
| parser.add_argument("--cache-dir", help="persistent processed-data cache") |
There was a problem hiding this comment.
[low] Resume re-preprocesses the dataset unless --cache-dir is given
Without --cache-dir the processed cache lives in a temp directory that is deleted at exit, so every invocation, including each resume the docstring advertises, rebuilds the cache for about 21k ECGs. Add --cache-dir to the reproduce command in the docstring (line 28-ish usage block) and say that it is needed to resume cheaply.
| args.device = "cpu" | ||
| for sub in ("preds", "checkpoints"): | ||
| os.makedirs(os.path.join(args.out_dir, sub), exist_ok=True) | ||
| with open(os.path.join(args.out_dir, "config.json"), "w", encoding="utf-8") as h: |
There was a problem hiding this comment.
[low] --summarize-only overwrites config.json with the summary call's defaults
config.json is written unconditionally, before the --summarize-only branch. When a merged out-dir is summarized, its config.json then records default arguments (for example --epochs 20, --seeds 0,1,2) rather than the settings the runs used. Write config.json only when training runs, or skip it under --summarize-only.
|
|
||
| Results are appended to ``<out-dir>/results.jsonl`` as each run finishes, and | ||
| test predictions go to ``<out-dir>/preds/``. Re-running with the same | ||
| ``--out-dir`` skips finished runs. Runs from several machines can be merged |
There was a problem hiding this comment.
[low] Merging runs across machines can split a seed's paired variants
The docstring says runs from several machines can be merged by copying results.jsonl and preds/, but checkpoints/ is not copied. If one seed's variants run on different machines, the no-contrastive and contrastive runs come from different feature_extractor checkpoints, and the paired gain and black-box gap no longer compare like with like. Say that each seed must run on one machine, or that checkpoints/ must be copied with it.
| @@ -0,0 +1,597 @@ | |||
| """ProtoECGNet on PTB-XL: does the contrastive prototype loss help? | |||
There was a problem hiding this comment.
[low] Overlaps the existing ProtoECGNet example without saying why
examples/ecg/ptbxl/ptbxl_multilabel_classification_protoecgnet.py already runs the same feature_extractor -> joint -> projection -> classifier pipeline, with --no-contrastive and an --ablation over the contrastive loss. The new script is a meaningful extension (several seeds, paired variants on a shared checkpoint, branch-restricted labels), but the docstring should say so, or the seed loop and pairing could be added to the existing script as options.
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: approve
Thank you for this contribution. It adds a multi-seed, paired reproduction of ProtoECGNet's rhythm branch on PTB-XL. It compares a black-box baseline with the model trained with and without the contrastive loss. The description is candid about what reproduces and what doesn't, and the example is resumable and has a --synthetic smoke mode. It looks ready to merge.
- Scope: The PR adds only an example script and changes no library code. Its use of existing components (
ProtoECGNet,PTBXLMultilabelClassificationsubclass,split_by_strat_foldand others) is named in the description with a clear purpose. - Coverage: No files under
pyhealth/changed, and the repository total is 84.69%, above the 60% bar. - Documentation: The docstring and description match the code.
Optional suggestions:
- Add a small test for
fast_aurocandauroc_summary, for example againstsklearn.metrics.roc_auc_score. The headline conclusion depends on them. A test for the branch-filtering subclass would help too. - Add
--cache-dirto the reproduce command, so a resumed run doesn't re-preprocess the dataset. --summarize-onlyrewritesconfig.jsonwith default arguments. You could skip that write in this mode.- If you merge runs from several machines, copy the
checkpoints/folder along withresults.jsonl. Otherwise a seed's variants may pair against different feature extractors. - Briefly say in the description why this is a separate script from the existing
ptbxl_multilabel_classification_protoecgnet.pyexample. The reason is the multi-seed, paired, branch-restricted design.
Reviewed commit a0377b1
|
/merge |
[BTY-57] ProtoECGNet: multi-seed reproduction on PTB-XL
ProtoECGNet (Sethi et al., "ProtoECGNet: Case-Based Interpretable Deep Learning for Multi-Label ECG Classification with Contrastive Learning", MLHC 2025; https://arxiv.org/abs/2504.08713, code https://github.com/bbj-lab/protoecgnet) and the PTB-XL multi-label task were added in #1323. That PR's results were single-seed runs on the 5 superdiagnostic labels, not the paper's setting. This PR adds an example that reproduces the paper's rhythm-branch comparison (Table 1) over several seeds. It doesn't change any library code.
Changes
examples/ecg/ptbxl/protoecgnet_contrastive_sweep.py: for each seed, it trains thefeature_extractorstage once, scores it as the black-box ResNet baseline, then finishes the staged protocol (joint-> prototype projection ->classifier) twice from that same checkpoint: once with the contrastive loss (lam_cnrst=300, the paper's default) and once without.PTBXLMultilabelClassificationdefined in the example.results.jsonl, test predictions andsummary.md(mean ± std over seeds, paired gain per seed with a paired bootstrap 95% CI on fold 10, and the paper's numbers). Finished runs are skipped on rerun, so an interrupted sweep resumes.--syntheticruns the whole pipeline on generated ECGs in about a minute, without a download.ProtoECGNet,PTBXL_BRANCH_LABELS,PTBXL_BRANCH_KWARGS,PTBXLDataset,PTBXLMultilabelClassificationandsplit_by_strat_foldfrom [BTY-57] Add PTB-XL multi-label task and ProtoECGNet model for ECG classification #1323 through their public API.Results: rhythm branch (16 labels), PTB-XL 1.0.3
Fold-10 macro AUROC, mean ± std over 6 seeds (0–5), on 16,913 / 2,111 / 2,108 train / validation / test ECGs:
Paired gain from the contrastive loss (contrastive − no contrastive), per seed, with a paired bootstrap 95% CI over test ECGs:
What reproduces and what doesn't:
Setup
strat_foldsplit).PTBXL_BRANCH_KWARGS).feature_extractorandjoint, one projection round, 15 epochs ofclassifier; checkpoint by validation loss.To reproduce: