Skip to content

MIT-BIH Arrhythmia dataset and ECG anomaly detection task (MedTsLLM, part 1 of #1594) - #1671

Merged
github-actions[bot] merged 2 commits into
sunlabuiuc:masterfrom
lizxiris1995:mitbih-dataset-task
Nov 28, 2026
Merged

github-actions[bot] merged 2 commits into
sunlabuiuc:masterfrom
lizxiris1995:mitbih-dataset-task

Conversation

@lizxiris1995

@lizxiris1995 lizxiris1995 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Contributors:

Georgia Tech CSE 6250 (Big Data for Health), Fall 2026

Part 1 of 3 split out of #1594, as the maintainers asked there. This part adds the MIT-BIH dataset and task. Part 2, #1672 (MedTsLLMAnomaly, the anomaly metrics and the MIT-BIH example), builds on it. Part 3 will add the LUDB MedTsLLM, adapted to the LUDB dataset and task from #1593, after that PR merges. Parts 1 and 2 include no LUDB code, so they don't overlap with #1593.

Contribution Type: Dataset + Task

Paper: Chan et al., "MedTsLLM: Leveraging LLMs for Multimodal Medical Time Series Analysis," MLHC 2024. https://arxiv.org/abs/2408.07773

Description:
The paper's ECG anomaly detection task on the MIT-BIH Arrhythmia Database: find abnormal beats in two-lead ECG.

  • MITBIHDataset reads the 48 WFDB records (two leads, 360 Hz, beat annotations, header metadata).
  • MITBIHAnomalyDetection makes the paper's samples: signals resampled to 125 Hz, 128-step two-lead windows, and a per-step label marking abnormal beats, each expanded by ±150 ms (configurable).
  • MITBIHAnomalyRatioSplit is the paper's record-level split. The paper doesn't list its records. Its sample counts (Tables 1 and 11) match exactly 33 MLII+V1 records and a 6 train / 4 test split by abnormal-beat ratio, which "paper" mode reproduces.

On the real data, the task's signals and labels match the data of our run of the authors' code (within 3e-6, no label mismatches). The only change to existing files is the __init__.py exports and docs index entries.

Design note: the task reuses the dataset's beat-symbol table, event type and wfdb import helper (MITBIH_BEAT_SYMBOLS, MITBIH_NORMAL_SYMBOL, MITBIH_EVENT_TYPE, _require_wfdb), so the two can't drift apart, and MITBIHDataset.default_task returns MITBIHAnomalyDetection, following PyHealth's default-task pattern.

Files to Review:

  • pyhealth/datasets/mitbih.py, pyhealth/datasets/configs/mitbih.yaml - dataset and record split
  • pyhealth/tasks/mitbih.py - anomaly detection task
  • tests/core/test_mitbih.py - test cases
  • Docs and registration: docs/api/datasets/pyhealth.datasets.MITBIHDataset.rst, docs/api/tasks/pyhealth.tasks.mitbih.rst, entries in docs/api/{datasets,tasks}.rst, exports in pyhealth/{datasets,tasks}/__init__.py

Checklist:

  • Targets master, based on the latest upstream master
  • "Allow edits by maintainers" enabled
  • Paper linked
  • Unit tests use synthetic records only (24 tests)
  • tools/check_pr_rules.py passes; code follows PEP 8 with 88-character lines
  • API docs and index entries for the new dataset and task

MITBIHDataset reads the 48 WFDB records; MITBIHAnomalyDetection makes
the MedTsLLM windows with per-step abnormal-beat labels; and
MITBIHAnomalyRatioSplit is the paper's record-level split. Split out of
sunlabuiuc#1594 as the maintainers asked.

Co-authored-by: Thi Van Trinh Tran <ttran493@gatech.edu>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lizxiris1995 lizxiris1995 changed the title MIT-BIH Arrhythmia dataset and ECG anomaly detection task (MedTsLLM, part 1 of #1594) MIT-BIH Arrhythmia dataset and ECG anomaly detection task (MedTsLLM, part 1 of 3 of #1594) Nov 28, 2026
@lizxiris1995 lizxiris1995 changed the title MIT-BIH Arrhythmia dataset and ECG anomaly detection task (MedTsLLM, part 1 of 3 of #1594) MIT-BIH Arrhythmia dataset and ECG anomaly detection task (MedTsLLM, part 1 of #1594) Nov 28, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 87.58% (53157 / 60697 lines)

Modified file Coverage Lines
pyhealth/datasets/__init__.py 100.00% 88 / 88
pyhealth/datasets/mitbih.py 97.35% 110 / 113
pyhealth/tasks/__init__.py 98.40% 123 / 125
pyhealth/tasks/mitbih.py 98.41% 62 / 63

Workflow run · e5580c4

@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 28, 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 6708fc5

@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: need change

Thank you for this contribution. It adds the MIT-BIH Arrhythmia dataset, the paper's record-level anomaly-ratio split, and a windowed ECG anomaly-detection task. The code is well tested and documented. Coverage is above the bar: the new files are at 97.35% and 98.41%, and the whole repository is at 87.58%.

To get this merged:

  • Describe the dataset–task links, or remove them. Two places connect the dataset and the task.
    • pyhealth/tasks/mitbih.py imports MITBIH_BEAT_SYMBOLS, MITBIH_EVENT_TYPE, MITBIH_NORMAL_SYMBOL and the private _require_wfdb from pyhealth/datasets/mitbih.py.
    • MITBIHDataset.default_task imports MITBIHAnomalyDetection from the task module.
      Please either confirm in the description that this sharing is intended and say why, for example so the annotation symbols and the wfdb error handling stay in sync. Or make the task self-contained by copying the few constants and the helper into it. Either option is fine.

Optional suggestions:

  • The class docstring says that changing the records in root never reuses a stale event cache, because of the metadata hash in dataset_name. A small test would back this up. It could build the dataset, change a record, rebuild, and check that dataset_name differs.
  • docs/api/datasets.rst has a summary table (around line 438) that doesn't list MIT-BIH. You could add it there too.
  • With label_level="window", BinaryLabelProcessor may raise if a split part contains only one label value. A short note in the docs would help users.
  • The wfdb error message says it is "also provided by pip install pyhealth[ptbxl]". You may want to check that this extra exists and includes wfdb.

Reviewed commit 6708fc5

…he datasets table

Addresses the AI review: a test rewrites one record's annotations and
checks that the dataset name, the metadata and the loaded event change,
and MIT-BIH is added to the dataset summary table in docs/api/datasets.rst.

Co-authored-by: Thi Van Trinh Tran <ttran493@gatech.edu>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lizxiris1995

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 e5580c4

f"{self.n_train} + n_test={self.n_test}."
)
return ordered[: self.n_train], ordered[len(ordered) - self.n_test :]
n_train = max(1, round(len(ordered) * self.train_frac))

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] Fraction-mode split can silently return an empty train or test part

In "fraction" mode, select_records() does not check train_frac or the number of eligible records. With train_frac=1.0 the test part is empty, and with no eligible records the train part is empty, yet no error is raised. The docstring only promises ValueError for an unknown mode or for too few paper-mode records. Suggested fix: raise ValueError when train_frac is outside (0, 1) or when either part would be empty, or document that the parts may be empty.

self.root = data_root

@property
def default_task(self) -> MITBIHAnomalyDetection:

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] default_task is not exercised by any test

MITBIHDataset.default_task is new public behavior and no test in tests/core/test_mitbih.py reads it. Tests call set_task with an explicit task, so a regression in the default would go unnoticed. Suggested fix: add one assertion that dataset.default_task is a MITBIHAnomalyDetection with target_fs 125.0 and window_len 128.

@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 an MIT-BIH Arrhythmia dataset, a record-level anomaly-ratio split that reproduces the MedTsLLM selection, and a windowed ECG anomaly detection task. The diff looks correct and complete, and it is ready to merge.

  • Self-contained: The new components subclass BaseDataset, Split and BaseTask without modifying them. The only edits to existing files are the exports and the docs index entries.
  • Sharing: The task reuses the dataset's beat-symbol table, event type and _require_wfdb, and default_task returns the task. Your description names both links and gives a reason, which is what the review needs.
  • Source citation: The dataset cites PhysioNet and the paper in its docstring and its docs page.
  • Docs: The docs match the code.
  • Tests: The 24 synthetic tests cover the main paths. Coverage is 97.35% for mitbih.py and 98.41% for the task file. The repository total is 87.58%, so all of these are above the bar.

Optional suggestions:

  • In fraction mode, train_frac=1.0 or having no eligible records quietly returns an empty part. A clear error or a warning would help users.
  • Add a small test for MITBIHDataset.default_task.

Reviewed commit e5580c4

@lizxiris1995

Copy link
Copy Markdown
Contributor Author

/merge

@github-actions
github-actions Bot merged commit 642d3ab into sunlabuiuc:master Nov 28, 2026
3 checks passed
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