Repository navigation
Add MedTsLLM full pipelines for LUDB ECG delineation and MIT-BIH anomaly detection (Chan et al., MLHC 2024) - #1594
Draft
lizxiris1995 wants to merge 3 commits into
Draft
lizxiris1995 wants to merge 3 commits into
lizxiris1995 wants to merge 3 commits into
Conversation
Test CoverageTotal: 86.30% (46156 / 53485 lines)
|
…C 2024) Reproducibility contribution for MedTsLLM (Chan et al., MLHC 2024), https://arxiv.org/abs/2408.07773. - MITBIHDataset: 48 two-lead recordings, one patient per record, YAML metadata table, lazy wfdb loading - MITBIHAnomalyRatioSplit: deterministic record-level split by abnormal-beat ratio; "paper" mode reproduces MedTsLLM's 6/4 MLII+V1 split - MITBIHAnomalyDetection: 125 Hz windows with step- or window-level labels (abnormal beats expanded +/-150 ms) - MedTsLLMAnomaly: port of the authors' MedTsLLM for reconstruction-based anomaly detection (RevIN, patch embedding, reprogramming onto text prototypes, frozen LLM, dataset/task/patient prompt); identical outputs and gradients to the original given the same weights - pyhealth.metrics.anomaly_detection: reconstruction scores, quantile threshold and point adjustment as in the paper, plus threshold-free AUROC - example examples/mitbih_anomaly_detection_medtsllm.py: full pipeline on synthetic data by default, ablations of the evaluation protocol and of the label width - tests with synthetic WFDB records and tiny offline GPT-2/Llama backbones, docs Contributors: Thi Van Trinh Tran and Zexi Li. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Thi Van Trinh Tran <ttran493@gatech.edu>
… as is) LUDB contribution by Thi Van Trinh Tran (ttran493), imported unchanged from the team repository (lizxiris1995/MedTsLLM master 66ddb66, LUDB/), which targets PyHealth ed124e3. Only the new files are copied; the package __init__ files and docs indexes get the new export and toctree lines rather than the full-file copies. Compatibility fixes for current master follow in a separate commit. - LUDBDataset, LUDBManifestSplit: frozen LUDB 1.0.1 window manifests - ECGDelineationLUDB: four-class ignore-aware per-point delineation - MedTsLLM: frozen GPT-2/Llama backbone with trainable ECG adapters - pyhealth.metrics.ecg_delineation: point metrics and segment mIoU - tests, docs, examples/ludb_segmentation_medtsllm.py Contributors: Thi Van Trinh Tran and Zexi Li. Co-authored-by: Thi Van Trinh Tran <ttran493@gatech.edu>
- ECGDelineationLUDB: pass the now-required timeline to the timeseries_multiclass label (sunlabuiuc#1429, merged after ed124e3); without it 11 LUDB tests fail - ClassVar annotations on the task schemas and a merged nested if in MedTsLLM.forward, for the ruff rules of tools/check_pr_rules.py - test_ecg_delineation_ludb: replace patch.dict("sys.modules", ...) by a swap of sys.modules["wfdb"] only. patch.dict restores the whole dict, so modules first imported inside the block (dask/pyarrow parts loaded by set_task) were dropped, and later dataset tests in the same run failed with dask TokenizationError. Temporary directories ignore cleanup errors (Windows keeps the cache files open). - test_native_dataset_set_task_end_to_end: build the dataset with a threaded Dask cluster (as in tests/core/survival_test_utils.py) instead of worker processes, which take seconds to start; 3.4 s -> 0.5 s, same checks No behavior change to the LUDB dataset, task or model. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Thi Van Trinh Tran <ttran493@gatech.edu>
lizxiris1995
force-pushed
the
medtsllm-mitbih-ludb
branch
from
November 24, 2026 03:37
5461ca7 to
88f9327
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contributors:
Georgia Tech CSE 6250 (Big Data for Health), Fall 2026
Contribution Type: Full Pipeline (Dataset + Task + Model), reproducibility contribution
Paper: Chan et al., "MedTsLLM: Leveraging LLMs for Multimodal Medical Time Series Analysis," MLHC 2024. https://arxiv.org/abs/2408.07773
Description:
MedTsLLM cuts a medical time series into patches and maps them into a frozen LLM's word-embedding space by cross-attention onto learned text prototypes. A text prompt (dataset, task, optionally patient information) is prepended, and a small head turns the LLM's hidden states into the task output. Only the adapters train. This PR adds two of the paper's ECG tasks end to end, from data loading to evaluation:
LUDBDataset,LUDBManifestSplit: prepared 512-sample windows, fixed patient-level 80/20 splitMITBIHDataset,MITBIHAnomalyRatioSplit: 48 two-lead records, the paper's 6 train / 4 test record splitECGDelineationLUDB: per-point background / P / QRS / T; unannotated points ignoredMITBIHAnomalyDetection: 125 Hz, 128-step two-lead windows, abnormal beats expanded by ±150 msMedTsLLM: per-point segmentation; prompt parts switchableMedTsLLMAnomaly: reconstruction, a port of the authors' modelpyhealth.metrics.ecg_delineation: point F1/IoU, segment mIoUpyhealth.metrics.anomaly_detection: the paper's threshold and point adjustment, raw-score AUROCBoth models take any Hugging Face GPT-2- or Llama-style backbone, pretrained or random (frozen). The two parts share no code and change no existing files beyond
__init__.pyexports and docs indexes.MedTsLLMkeeps the architecture but uses per-window scaling,nn.MultiheadAttentionand an interpolated per-patch head (see its docs page).MedTsLLMAnomalyis architecturally identical. The only difference is in the task: it windows each MIT-BIH record separately instead of the concatenated records."paper"mode reproduces.Validation:
With the same weights and inputs,
MedTsLLMAnomalyand the anomaly metrics give outputs, gradients and scores identical to the authors' code. This was checked with tiny Llama and GPT-2 backbones.The MIT-BIH task's signals and labels match the data of our run of the authors' code (within 3e-6, no label mismatches). That run used Llama-2-7B in the paper configuration with 1 seed:
The random backbone matches the pretrained one, so on this task the adapters, not the LLM's pretrained knowledge, carry the performance.
62 tests (22 LUDB, 40 MIT-BIH) use synthetic records and tiny offline backbones, each under a second.
Examples and ablations (offline on synthetic data by default; real data and backbones by path or name):
examples/ludb_segmentation_medtsllm.py: prompt ablation in nine conditions (no prompt; dataset, task, patient or statistics only; all parts with the correct, a mismatched or no patient; all parts with a random backbone). Each condition runs with 3 seeds on GPT-2 XL and Llama-2-7B.examples/mitbih_anomaly_detection_medtsllm.py: one trained model evaluated under (1) the evaluation protocol and (2) label widths of 50 / 150 / 300 ms. On synthetic data, point adjustment accounts for most of the paper-protocol F1 (0.44 → 0.96), while the threshold-free AUROC is unchanged (results in the docstring).Files to Review:
pyhealth/datasets/ludb.py,pyhealth/datasets/configs/ludb.yaml- dataset and patient splitpyhealth/tasks/ecg_delineation_ludb.py- delineation taskpyhealth/models/medtsllm.py-MedTsLLMmodelpyhealth/metrics/ecg_delineation.py- delineation metricstests/core/test_ludb.py,tests/core/test_ecg_delineation_ludb.py,tests/core/test_medtsllm.py,tests/core/test_ecg_delineation_metrics.py- test casesexamples/ludb_segmentation_medtsllm.py- full pipeline and prompt ablationspyhealth/datasets/mitbih.py,pyhealth/datasets/configs/mitbih.yaml- dataset and record splitpyhealth/tasks/mitbih.py- anomaly detection taskpyhealth/models/medtsllm_anomaly.py-MedTsLLMAnomalymodelpyhealth/metrics/anomaly_detection.py- anomaly scores, threshold and point adjustmenttests/core/test_mitbih.py,tests/core/test_medtsllm_anomaly.py,tests/core/test_anomaly_detection_metrics.py- test casesexamples/mitbih_anomaly_detection_medtsllm.py- full pipeline and ablationsdocs/api/, entries indocs/api/{datasets,tasks,models,metrics}.rst, exports inpyhealth/{datasets,tasks,models,metrics}/__init__.pyChecklist:
master, rebased on the latest upstreammasterexamples/named{dataset}_{task}_{model}.pytools/check_pr_rules.pypasses; code follows PEP 8 with 88-character lines