Skip to content

[Model + Task] PTB-XL binary tasks and TaskAug ECG augmentation (Raghu et al., CHIL 2022) - #1614

Open
LishaRanganath wants to merge 2 commits into
sunlabuiuc:masterfrom
LishaRanganath:ptbxl-taskaug
Open

LishaRanganath wants to merge 2 commits into
sunlabuiuc:masterfrom
LishaRanganath:ptbxl-taskaug

Conversation

@LishaRanganath

@LishaRanganath LishaRanganath commented Nov 26, 2026 •

Copy link
Copy Markdown

Contributors

Course project for CSE6250 (Georgia Tech), reproducibility challenge.

Contribution type

Model + Standalone Task. The tasks and model build on the existing PTBXLDataset, which is reused unchanged.

Paper

Aniruddh Raghu, Divya Shanmugam, Eugene Pomerantsev, John Guttag, Collin M. Stultz.
"Data Augmentation for Electrocardiograms." CHIL 2022, PMLR 174:282-310.
https://proceedings.mlr.press/v174/raghu22a.html

What this adds

The paper proposes TaskAug, an ECG augmentation policy made of K chained stages, each choosing among temporal warp, baseline wander, Gaussian noise, random crop, displacement, magnitude scaling or no-op, with class-conditional magnitudes. The paper fixes K=2. This PR:

  • adds single-diagnosis binary tasks (MI, HYP, STTC, CD) on PTB-XL, built on the existing PTBXLDataset and the helpers in pyhealth.tasks.ptbxl;
  • adds PTBXLResNet (1D ResNet baseline) and TaskAugResNet (same network preceded by the TaskAug policy, with a configurable number of stages k), applied only in training mode;
  • adds pyhealth.models.taskaug, the augmentation operations adapted from the authors' code (https://github.com/aniruddhraghu/ecg_aug, Apache-2.0; license copied into the folder, modifications noted in each file);
  • adds an example that runs a stage-depth ablation (baseline vs K = 1, 2, 3; the paper fixes K=2 and does not report this sweep).

Limitations

TaskAugResNet applies the augmentation but does not reproduce the paper's bilevel optimization of the augmentation magnitudes (validation-set hypergradient). Here the magnitudes are trained jointly with the network on the training loss. This is stated in the class docstring and the docs page. Minimizing the training loss tends to shrink the magnitudes, so the policy can drift toward little or no augmentation. In training mode TaskAugResNet needs 500 Hz signals (5000 or 2496 samples) and raises ValueError otherwise.

File guide

File Purpose
pyhealth/tasks/ptbxl_binary.py PTBXLBinaryClassification and PTBXLMI, PTBXLHYP, PTBXLSTTC, PTBXLCD
pyhealth/models/ptbxl_resnet.py PTBXLResNet, TaskAugResNet
pyhealth/models/taskaug/ Augmentation policy adapted from the original repository, with LICENSE
examples/ecg/ptbxl/ptbxl_binary_classification_taskaug_resnet.py Ablation / example script (--ablation, --synthetic)
tests/core/test_ptbxl_binary_tasks.py, tests/core/test_ptbxl_taskaug_resnet.py Tests with synthetic data
docs/api/tasks/pyhealth.tasks.ptbxl_binary.rst, docs/api/models/pyhealth.models.PTBXLResNet.rst API docs
docs/api/tasks.rst, docs/api/models.rst, pyhealth/tasks/__init__.py, pyhealth/models/__init__.py Index entries and exports (one block each)

Tests and checks

  • 13 unit tests on synthetic data and the module doctests, all passing.
  • ruff check clean and tools/check_pr_rules.py passes against the current master.
  • No existing files are modified apart from the four index/export files above.

Reproduction results (original authors' code, not this PyHealth model)

Test AUROC with 1000 training ECGs, seed 42, 100 epochs, run with https://github.com/aniruddhraghu/ecg_aug:

task no aug K=1 K=2 K=3 paper (K=2)
MI 0.8213 0.7986 0.8040 0.8046 0.823
HYP 0.8458 0.8523 0.8385 0.8103 0.837
STTC 0.8761 0.8619 0.8824 0.8547 0.878
CD 0.8201 0.8294 0.8363 0.8260 0.831

On MI the no-aug baseline is best and K=2 ties K=3 within noise; K=2 is best on STTC and CD; HYP prefers K=1. TaskAug clearly helps only on CD. Single seed, so small differences are within noise. The MI results and STTC K=2 come from an earlier run with identical settings.

How to run

python examples/ecg/ptbxl/ptbxl_binary_classification_taskaug_resnet.py --synthetic --ablation
python examples/ecg/ptbxl/ptbxl_binary_classification_taskaug_resnet.py --root /data/ptb-xl/1.0.3 --task HYP --ablation --train-samples 1000

Adds single-diagnosis tasks (MI, HYP, STTC, CD) on top of PTBXLDataset, a 1D ResNet baseline, and TaskAugResNet, which applies the TaskAug augmentation policy with a configurable number of stages K. TaskAug code is adapted from aniruddhraghu/ecg_aug (Apache-2.0). Includes tests, an ablation example script with a synthetic mode, and API docs.

Co-authored-by: Claude <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 86.63% (47760 / 55132 lines)

Modified file Coverage Lines
pyhealth/interpret/__init__.py 100.00% 4 / 4
pyhealth/interpret/uni_acs.py 82.35% 182 / 221
pyhealth/models/__init__.py 100.00% 94 / 94
pyhealth/models/joint_seq_image.py 99.29% 278 / 280
pyhealth/models/ptbxl_resnet.py 100.00% 58 / 58
pyhealth/models/taskaug/__init__.py 100.00% 0 / 0
pyhealth/models/taskaug/aug_policy.py 96.00% 48 / 50
pyhealth/models/taskaug/functional.py 100.00% 35 / 35
pyhealth/models/taskaug/operations.py 97.85% 91 / 93
pyhealth/models/taskaug/warp_ops.py 96.08% 98 / 102
pyhealth/models/time_series_images.py 100.00% 126 / 126
pyhealth/tasks/__init__.py 98.13% 105 / 107
pyhealth/tasks/physionet2012_mortality.py 96.20% 76 / 79
pyhealth/tasks/ptbxl_binary.py 100.00% 40 / 40

Workflow run · 4111de4

@LishaRanganath

Copy link
Copy Markdown
Author

/review

@solarsys solarsys added the status: manual Pending maintainer's review label Nov 26, 2026
@solarsys

Copy link
Copy Markdown
Collaborator

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

Previous review

Reviewed commit 08798e1

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

Previous review

AI Review

suggestion: need change
decision: human review

Thank you for this contribution. It adds single-diagnosis PTB-XL binary tasks (MI, HYP, STTC, CD), a 1D ResNet baseline, a TaskAug-augmented variant adapted from the authors' Apache-2.0 code (license included), and an ablation example with a synthetic mode. Coverage is well above the bar: the whole repository is at 86.5%, and each changed file is at 96% or higher. Sharing with the existing PTB-XL helpers and dataset is named in the description. You are close.

To get this merged:

  1. TaskAugResNet crashes in training mode on inputs that are not 5000 or 2496 samples long. PTBXLDataset defaults to 100 Hz (1000 samples), so the first training batch hits a shape mismatch in the warp and displacement operations, which are hard-coded to 2496 samples. Eval mode works, so the failure is easy to miss. Please do one of these:
    • Raise a clear ValueError in TaskAugResNet.forward or __init__ when the length is not 2496 after trimming.
    • Support other lengths.
      Either way, state the 500 Hz requirement in the TaskAugResNet and PTBXLResNet docstrings and add a test for it. The task docs mention sampling_rate=500, but the model docs do not.
  2. Add a test showing that training mode changes the input to the encoder, or the output, compared with eval mode. Right now nothing checks that the augmentation is applied.

Optional suggestions:

  • The description says 15 unit tests, but I count 11 test methods. Please correct the number.
  • The example docstring says K=2 is "best or tied" on MI. In your table the no-aug baseline (0.8213) beats every TaskAug column on MI, so please reword.
  • The description calls the K sweep the paper's ablation, while the example says the paper does not report it. Please make these consistent.
  • The docstring notes that the magnitudes are trained on the training loss. It could add that this tends to push them toward weaker augmentation.

Reviewed commit 08798e1

@LishaRanganath

Copy link
Copy Markdown
Author

Thanks for the review! I pushed 4111de4, which addresses all the points: ValueError for non-500 Hz input in training mode (with docs and tests for it), a test that training mode changes the encoder input, the corrected MI wording and test count, the training-loss note, and the moskomule/dda MIT notice in the taskaug LICENSE. @solarsys could you re-run /review when you have a moment? Thank you!

@solarsys

Copy link
Copy Markdown
Collaborator

/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 4111de4

y_true = kwargs[self.label_keys[0]].to(self.device)
if self.training:
# TaskAug conditions its magnitudes on integer class labels.
x = self.taskaug(x, y_true.view(-1).long())

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] TaskAugResNet silently assumes binary labels

The augmentation uses y_true.view(-1).long() as an index into per-class magnitudes that are built for exactly two classes. With a multilabel or multiclass dataset, such as PTBXLSuperclassClassification from the same dataset, the batch length or label values do not match, so training fails with an indexing error that does not name the cause. Fix: in TaskAugResNet.__init__, check that self.label_processor is binary (output size 1) and raise a clear ValueError otherwise, or state in the class docstring that the model is binary-only.

)


class PTBXLBinaryClassification(BaseTask):

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] Records without a diagnostic superclass are silently labelled 0

Records with no diagnostic superclass, such as pacemaker ECGs, are kept with label 0 for every binary task. PTBXLSuperclassClassification drops them by default (drop_empty_labels=True), so the two tasks disagree on the same records. Fix: say in the class docstring that these records are negatives, or drop them to match the existing task.

python examples/ecg/ptbxl/ptbxl_binary_classification_taskaug_resnet.py \
--root /data/ptb-xl/1.0.3 --task HYP --ablation --train-samples 1000

Findings (reference, original TaskAug code):

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] Documented run command does not reproduce the reported numbers

The Findings block reports results from 100-epoch runs of the authors' code, but the How-to-run commands use the script default of 5 epochs (line 190). Fix: add --epochs 100 to the documented command, or state in the Findings block that the table needs that setting.

@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: human review

Thank you for this contribution. It adds binary PTB-XL tasks (MI, HYP, STTC, CD), a 1D ResNet baseline, TaskAugResNet with the adapted TaskAug augmentation policy, an ablation example, docs and tests. It looks ready to merge.

The change is self-contained. It only adds export lines and docs index entries to existing files. The reuse of the existing PTBXL helpers and PTBXLDataset is named in the description. The Apache license for the adapted code is included. The limitation that the bilevel optimization isn't reproduced is clearly stated. The dataset is existing, and the paper is cited. Coverage is good: every added file is at 96% or higher, and the repository total is 86.6%.

Optional suggestions:

  • TaskAugResNet only supports binary labels. Running it on a multilabel PTB-XL task gives an obscure indexing error. A clear check in __init__ would help.
  • Say in the task docs that records with no diagnostic superclass are labeled 0.
  • The documented run command uses the default of 5 epochs, but the reported numbers used 100. Mention --epochs 100 in the docs or example.
  • test_training_mode_augments_input is fairly weak. It would help to check that training-mode input differs from the unaugmented input for a fixed seed.
  • Fix the missing newlines at the end of the example file and warp_ops.py.

Reviewed commit 4111de4

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: manual Pending maintainer's review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants