Repository navigation
Add TaskAug model for binary ECG classification and paired PTB-XL MI evaluation - #1685
Michaelwyx wants to merge 1 commit into
Conversation
Developed with OpenAI Codex assistance. Includes synthetic checks, API documentation, train-only preprocessing, and clean and missing-lead comparisons.
Test CoverageTotal: 87.99% (55969 / 63609 lines)
|
|
Why this PR has What you can contribute instead. After #1598 merges, rebase and keep only what yours adds, as an option on #1598's |
|
/review |
| return signals | ||
|
|
||
|
|
||
| def load_ptbxl_mi( |
There was a problem hiding this comment.
[medium] Cohort loader for the reported MI results has no tests
load_ptbxl_mi carries the data-integrity claims the description makes: patient-disjoint folds, label-stratified 800/200 sampling, MI labels from the SCP mapping, normalization fit on the training split only, and the manifest check used by replay. None of this is tested. tests/core/test_taskaug.py covers only the model, and the --smoke path uses synthetic() and never calls this loader. The --checkpoints replay branch in examples/ptbxl_mi_taskaug.py is also never exercised by the smoke run. The existing PTB-XL dataset and task have tests in tests/core/test_ptbxl.py. Add a small test that writes a tiny ptbxl_database.csv and scp_statements.csv and stubs the record reader. It should check that _stratify keeps both classes and the requested size, that no patient appears in more than one split, that normalization statistics come from the training split only, and that a replay with a mismatched manifest raises.
| **kwargs: Dataset-named fields or ``waveform`` of shape | ||
| (batch, leads, time) and optional ``label`` of shape (batch,) | ||
| or (batch, 1). Augmentation requires both training mode and | ||
| labels. Call ``eval()`` to use stored normalization statistics. |
There was a problem hiding this comment.
[low] forward docstring implies stored normalization statistics
The sentence says eval() is needed "to use stored normalization statistics". TaskAug stores no input normalization. eval() only turns off augmentation and makes BatchNorm use running statistics. Reword it to say that, for example, "Call eval() to disable augmentation and use BatchNorm running statistics."
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: human review
Thank you for this contribution. TaskAug adds a BaseModel for binary ECG classification with a learned two-stage augmentation policy and a truncated implicit hypergradient policy_step(). It comes with a PTB-XL MI ablation and a missing-lead example. It looks ready to merge.
Checks:
- The model is self-contained. The only change to existing code is the export line in
pyhealth/models/__init__.py, which is registration. - Your description names the sharing of
ResNet1dBackboneand the PTB-XL record and superclass helpers, with a clear purpose. - Coverage is 99.3% for
pyhealth/models/taskaug.pyand 87.99% for the whole repository. Both meet the bar. The tests are thorough and include a finite-difference hypergradient check. - The docs match the code.
Optional suggestions:
- The description says the learned policy's +0.78 AUROC gain over the frozen policy is "across 4 seeds". The mean seems to be over 5 seeds, with seed 45 tied at zero. Please make the wording consistent.
- The bootstrap CI and the aggregate tables aren't produced by any script in the PR. Consider adding the aggregation script, or saying how you computed them.
load_ptbxl_miinexamples/ptbxl_taskaug_data.pyhas no tests, and the smoke run bypasses it and the--checkpointsreplay path. A small test with a mock PTB-XL CSV would help, but it isn't blocking for an example script.- The loader keeps ECGs with no diagnostic superclass as negatives. This differs from the default in the existing PTB-XL superclass task. Consider a sentence in the docs about that choice.
Reviewed commit dca1693
Contributor: Yixuan Wang (
yixuan41@illinois.edu, NetIDyixuan41)Contribution Type: Model (
pyhealth.models.TaskAug) — CS 598 Deep Learning for Healthcare reproducibility contribution (developed with Codex assistance)Original Paper: Raghu et al., Data Augmentation for Electrocardiograms, CHIL 2022 (Paper · Authors' reference code)
High-Level Description
This PR adds
TaskAug(pyhealth.models.TaskAug), aBaseModelimplementation adapting task-dependent ECG data augmentation for binary ECG classification in PyHealth:ResNet1dBackbone(61,841 parameters atwidth=8) with a two-stage categorical augmentation policy over 7 operations (warp,wander,noise,mask,shift,scale,identity), where the 5 continuous transforms learn class-conditional magnitudes.forward) train on augmented ECGs without updating policy parameters; a separatepolicy_step()method updates only the policy via a truncated implicit hypergradient (Neumann series) using clean validation loss while keeping BatchNorm buffers private.eval()), the model takes raw waveforms without labels.Ablation Study & Missing-Lead Extension (
examples/ptbxl_mi_taskaug.py)Each of five paired seeds (
41–45) draws 800 training ECGs (folds 1–8) and 200 validation ECGs (fold 9), fits per-lead normalization on the training split only, and evaluates on all 2,198 test ECGs in fold 10 (1,904 patient-disjoint test patients, 550 MI-positive). All four conditions share identical splits, classifier initialization, and minibatch order within a seed.{10%, 20%, 50%}, and (2) a frozen stochastic policy ablation (initial policy sampled without policy updates) to separate random transform diversity from policy learning.Clean AUROC and average precision (AP) are percentages reported as mean ± standard error across the 5 seeds:
[+2.03, +3.56]), matching the direction of the paper's +2.3-point gain (80.0→82.3). Against the frozen policy ablation, learning added +0.78 AUROC points and +1.98 AP points across 4 seeds (seed 45 selected epoch 4 before the first policy update and tied frozen exactly).78.92%without II;79.48%without V1), while the smallest relative drop depended on which lead was removed.Files to Review
pyhealth/models/taskaug.py—TaskAugmodel (BaseModel), 7 ECG transforms, and truncated implicit hypergradientpolicy_step().pyhealth/models/__init__.py— exportsTaskAug.tests/core/test_taskaug.py— fast synthetic unit tests (PyHealth batch interface, label-free inference,state_dictsave/load, fixed masking, class-conditional transform gradients, classifier/policy update separation, input validation, and an analytic finite-difference hypergradient check).examples/ptbxl_mi_taskaug.py— runnable training/ablation/missing-lead evaluation script (supports--smokeon synthetic tensors and full PTB-XL reproduction/replay).examples/ptbxl_taskaug_data.py— PTB-XL v1.0.3 binary MI cohort loader, patient-disjoint fold sampling, and train-only per-lead standardization.docs/api/models/pyhealth.models.taskaug.rst&docs/api/models.rst— API documentation and index entry.Reproduction & Verification
Run unit tests and the synthetic smoke example (CPU, seconds):
python -m pip install -e '.[ptbxl]' pytest python -m pytest -q tests/core/test_taskaug.py python examples/ptbxl_mi_taskaug.py --smoke --output taskaug-smokeFull PTB-XL v1.0.3 experiment (
PTBXL_ROOTcontaining metadata CSVs andrecords100/; supportscpu,mps,cuda):python examples/ptbxl_mi_taskaug.py --root "${PTBXL_ROOT}" \ --output taskaug-mi-results --cache-dir taskaug-waveform-cache \ --device mps --seeds 41 42 43 44 45 --epochs 100 --patience 8 \ --width 8 --batch-size 64 --policy-batch 32 --policy-every 1 \ --warmup 5 --mask-fractions 0.1 0.2 0.5 --lr 0.001 --policy-lr 0.01Relationship to Earlier Contributions
Other public PRs also explore TaskAug or ECG lead masking: PR #1490 targets 71 diagnostic codes, PR #1598 studies cross-backbone transfer in binary MI, and closed PR #1123 studied random lead dropout during training. This contribution provides a clean
BaseModeladaptation with a finite-difference-verified hypergradient, a paired frozen-policy ablation, and a fixed-checkpoint single-lead (II / V1) stress test.