Skip to content

[BTY-57] ProtoECGNet: multi-seed reproduction on PTB-XL - #1525

Merged
github-actions[bot] merged 1 commit into
sunlabuiuc:masterfrom
irvifa:bty-57-protoecgnet-repro
Nov 18, 2026
Merged

github-actions[bot] merged 1 commit into
sunlabuiuc:masterfrom
irvifa:bty-57-protoecgnet-repro

Conversation

@irvifa

@irvifa irvifa commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

[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 the feature_extractor stage 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.
    • The two ProtoECGNet variants share starting weights, data order and hyperparameters, so the comparison is paired with an equal tuning budget. The paper tuned only the contrastive variant.
    • Each branch keeps only the ECGs that have at least one of its labels, as in the branch rows of Table 1. This is done by a small subclass of PTBXLMultilabelClassification defined in the example.
    • It writes results.jsonl, test predictions and summary.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.
    • --synthetic runs the whole pipeline on generated ECGs in about a minute, without a download.
    • It uses ProtoECGNet, PTBXL_BRANCH_LABELS, PTBXL_BRANCH_KWARGS, PTBXLDataset, PTBXLMultilabelClassification and split_by_strat_fold from [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:

variant macro AUROC weighted AUROC paper, Table 1 (macro)
black-box (1D ResNet) 0.9375 ± 0.0046 0.9187 ± 0.0065 0.9403
ProtoECGNet, no contrastive loss 0.9341 ± 0.0030 0.9115 ± 0.0030 0.8903
ProtoECGNet, contrastive loss 0.9290 ± 0.0055 0.8973 ± 0.0074 0.9064

Paired gain from the contrastive loss (contrastive − no contrastive), per seed, with a paired bootstrap 95% CI over test ECGs:

seed no contrastive contrastive gain 95% CI
0 0.9353 0.9207 −0.0146 [−0.0281, −0.0037]
1 0.9279 0.9296 +0.0017 [−0.0099, +0.0162]
2 0.9367 0.9284 −0.0083 [−0.0163, −0.0012]
3 0.9339 0.9249 −0.0090 [−0.0172, −0.0009]
4 0.9370 0.9382 +0.0012 [−0.0057, +0.0080]
5 0.9335 0.9322 −0.0013 [−0.0083, +0.0057]

What reproduces and what doesn't:

  • The black-box baseline matches the paper (0.9375 vs 0.9403).
  • Both ProtoECGNet variants score higher than the paper's, and come within 0.01 of the black-box (paper: 0.034 below it).
  • The contrastive loss does not help here: the mean gain is −0.0050 ± 0.0060, against the paper's +0.0161. It is negative in 4 of 6 seeds (significantly so in 3), never significantly positive, and weighted AUROC is lower on every seed.
  • The paper's gain comes from one run, with only the contrastive variant tuned (Optuna); here both variants get the same settings. So this doesn't show the loss can't help after tuning, only that it doesn't at equal budget.

Setup

  • Folds 1–8 train, 9 validation, 10 test (the recommended strat_fold split).
  • 100 Hz, 12 leads, 1D ResNet (width 64), prototype dimension 128, the paper's prototype count for the branch (PTBXL_BRANCH_KWARGS).
  • Adam 1e-3, batch 32; 20 epochs each for feature_extractor and joint, one projection round, 15 epochs of classifier; checkpoint by validation loss.
  • MacBook Pro M4 Pro, MPS.

To reproduce:

python examples/ecg/ptbxl/protoecgnet_contrastive_sweep.py \
    --root /path/to/ptb-xl/1.0.3 --branch rhythm --seeds 0,1,2,3,4,5 \
    --out-dir runs/rhythm

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.
@irvifa
irvifa marked this pull request as ready for review November 17, 2026 21:49
@irvifa

irvifa commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

/review

@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 84.69% (38726 / 45725 lines)

No modified files under pyhealth/.

Workflow run · a0377b1

@github-actions github-actions Bot mentioned this pull request Nov 17, 2026
@irvifa

irvifa commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

/review

@jhnwu3 jhnwu3 added the status: automatic The PR will go through automatic review process. label Nov 18, 2026
@irvifa

irvifa commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

/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 a0377b1

torch.manual_seed(seed)


def fast_auroc(y_true: np.ndarray, y_prob: np.ndarray) -> np.ndarray:

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] 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")

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] 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:

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] --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

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] 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?

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] 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.

@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 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, PTBXLMultilabelClassification subclass, split_by_strat_fold and 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_auroc and auroc_summary, for example against sklearn.metrics.roc_auc_score. The headline conclusion depends on them. A test for the branch-filtering subclass would help too.
  • Add --cache-dir to the reproduce command, so a resumed run doesn't re-preprocess the dataset.
  • --summarize-only rewrites config.json with default arguments. You could skip that write in this mode.
  • If you merge runs from several machines, copy the checkpoints/ folder along with results.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.py example. The reason is the multi-seed, paired, branch-restricted design.

Reviewed commit a0377b1

@irvifa

irvifa commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@github-actions
github-actions Bot merged commit 07c4e38 into sunlabuiuc:master Nov 18, 2026
2 checks passed
@irvifa
irvifa deleted the bty-57-protoecgnet-repro branch November 18, 2026 03:14
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