openecg 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- openecg-0.1.0/.gitignore +81 -0
- openecg-0.1.0/LICENSE +201 -0
- openecg-0.1.0/PKG-INFO +262 -0
- openecg-0.1.0/README.md +230 -0
- openecg-0.1.0/openecg/__init__.py +1 -0
- openecg-0.1.0/openecg/codec.py +144 -0
- openecg-0.1.0/openecg/delineate.py +71 -0
- openecg-0.1.0/openecg/eval.py +189 -0
- openecg-0.1.0/openecg/isp.py +110 -0
- openecg-0.1.0/openecg/labeler.py +154 -0
- openecg-0.1.0/openecg/ludb.py +188 -0
- openecg-0.1.0/openecg/pacer.py +65 -0
- openecg-0.1.0/openecg/qtdb.py +205 -0
- openecg-0.1.0/openecg/stage2/__init__.py +4 -0
- openecg-0.1.0/openecg/stage2/augment.py +178 -0
- openecg-0.1.0/openecg/stage2/dataset.py +170 -0
- openecg-0.1.0/openecg/stage2/evaluate.py +113 -0
- openecg-0.1.0/openecg/stage2/infer.py +249 -0
- openecg-0.1.0/openecg/stage2/model.py +209 -0
- openecg-0.1.0/openecg/stage2/multi_dataset.py +628 -0
- openecg-0.1.0/openecg/stage2/refiner.py +115 -0
- openecg-0.1.0/openecg/stage2/reg_targets.py +105 -0
- openecg-0.1.0/openecg/stage2/soft_labels.py +78 -0
- openecg-0.1.0/openecg/stage2/ssl/__init__.py +4 -0
- openecg-0.1.0/openecg/stage2/ssl/head.py +59 -0
- openecg-0.1.0/openecg/stage2/ssl/hubert.py +66 -0
- openecg-0.1.0/openecg/stage2/ssl/stmem.py +97 -0
- openecg-0.1.0/openecg/stage2/train.py +419 -0
- openecg-0.1.0/openecg/vocab.py +62 -0
- openecg-0.1.0/pyproject.toml +84 -0
openecg-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Internal planning docs (not for public release yet)
|
|
2
|
+
PLAN.md
|
|
3
|
+
DATA_LOCATION.md
|
|
4
|
+
|
|
5
|
+
# Python
|
|
6
|
+
__pycache__/
|
|
7
|
+
*.py[cod]
|
|
8
|
+
*$py.class
|
|
9
|
+
*.so
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
*.egg-info/
|
|
24
|
+
.installed.cfg
|
|
25
|
+
*.egg
|
|
26
|
+
.venv/
|
|
27
|
+
venv/
|
|
28
|
+
env/
|
|
29
|
+
ENV/
|
|
30
|
+
|
|
31
|
+
# uv
|
|
32
|
+
.uv/
|
|
33
|
+
uv.lock
|
|
34
|
+
|
|
35
|
+
# Jupyter
|
|
36
|
+
.ipynb_checkpoints/
|
|
37
|
+
*.ipynb_checkpoints
|
|
38
|
+
|
|
39
|
+
# Data files (not for public release)
|
|
40
|
+
*.npz
|
|
41
|
+
*.npy
|
|
42
|
+
*.dat
|
|
43
|
+
*.hea
|
|
44
|
+
*.atr
|
|
45
|
+
*.xws
|
|
46
|
+
*.qrs
|
|
47
|
+
*.vital
|
|
48
|
+
*.wfdb
|
|
49
|
+
data/*
|
|
50
|
+
!data/splits/
|
|
51
|
+
!data/splits/*.json
|
|
52
|
+
datasets/
|
|
53
|
+
|
|
54
|
+
# Model checkpoints
|
|
55
|
+
*.pt
|
|
56
|
+
*.pth
|
|
57
|
+
*.ckpt
|
|
58
|
+
*.safetensors
|
|
59
|
+
checkpoints/
|
|
60
|
+
runs/
|
|
61
|
+
wandb/
|
|
62
|
+
|
|
63
|
+
# IDE
|
|
64
|
+
.vscode/
|
|
65
|
+
.idea/
|
|
66
|
+
*.swp
|
|
67
|
+
*.swo
|
|
68
|
+
.DS_Store
|
|
69
|
+
Thumbs.db
|
|
70
|
+
|
|
71
|
+
# Env / secrets
|
|
72
|
+
.env
|
|
73
|
+
.env.*
|
|
74
|
+
!.env.example
|
|
75
|
+
|
|
76
|
+
# Third-party vendored libraries (clone manually)
|
|
77
|
+
third_party/
|
|
78
|
+
|
|
79
|
+
# Misc
|
|
80
|
+
.claude/
|
|
81
|
+
out/
|
openecg-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing the Work or
|
|
166
|
+
Derivative Works thereof, You may choose to offer, and charge a
|
|
167
|
+
fee for, acceptance of support, warranty, indemnity, or other
|
|
168
|
+
liability obligations and/or rights consistent with this License.
|
|
169
|
+
However, in accepting such obligations, You may act only on Your
|
|
170
|
+
own behalf and on Your sole responsibility, not on behalf of any
|
|
171
|
+
other Contributor, and only if You agree to indemnify, defend,
|
|
172
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
173
|
+
or claims asserted against, such Contributor by reason of your
|
|
174
|
+
accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Hyung-Chul Lee and OpenECG contributors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing
|
|
201
|
+
permissions and limitations under the License.
|
openecg-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: openecg
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Clinically-grounded discrete tokenization and per-frame wave segmentation for electrocardiograms
|
|
5
|
+
Project-URL: Homepage, https://github.com/vitaldb/openecg
|
|
6
|
+
Project-URL: Repository, https://github.com/vitaldb/openecg
|
|
7
|
+
Project-URL: Issues, https://github.com/vitaldb/openecg/issues
|
|
8
|
+
Author-email: Hyung-Chul Lee <vital@snu.ac.kr>
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: biomedical,deep-learning,delineation,ecg,electrocardiogram,p-wave,qrs,segmentation,t-wave,tokenization
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Healthcare Industry
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Requires-Dist: huggingface-hub>=0.20
|
|
25
|
+
Requires-Dist: neurokit2>=0.2.7
|
|
26
|
+
Requires-Dist: numpy>=1.26
|
|
27
|
+
Requires-Dist: scipy>=1.11
|
|
28
|
+
Requires-Dist: torch>=2.6
|
|
29
|
+
Requires-Dist: transformers>=4.40
|
|
30
|
+
Requires-Dist: wfdb>=4.1
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# OpenECG
|
|
34
|
+
|
|
35
|
+
*Clinically-grounded discrete tokenization and per-frame wave segmentation for electrocardiograms.*
|
|
36
|
+
|
|
37
|
+
[](https://pypi.org/project/openecg/)
|
|
38
|
+
[](https://pypi.org/project/openecg/)
|
|
39
|
+
[](LICENSE)
|
|
40
|
+
|
|
41
|
+
OpenECG ships:
|
|
42
|
+
|
|
43
|
+
- A 13-symbol RLE token format (`openecg.codec`, `openecg.vocab`) that compresses 12-lead ECGs into a clinically interpretable sequence.
|
|
44
|
+
- A pretrained Conv+Transformer per-frame wave classifier (`openecg.stage2`) trained on LUDB + QTDB + ISP that reaches near-SOTA P / QRS / T boundary F1 on ISP test (qrs_on F1 = 0.99).
|
|
45
|
+
- Loaders and converters for **LUDB**, **QTDB**, and **ISP** datasets so you can reproduce every number in this README.
|
|
46
|
+
|
|
47
|
+
## Install
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pip install openecg
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
PyTorch is a runtime dependency. On CUDA boxes, install the matching wheel first (`pip install torch --index-url https://download.pytorch.org/whl/cu124`).
|
|
54
|
+
|
|
55
|
+
## Quickstart
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from openecg import codec, vocab
|
|
59
|
+
|
|
60
|
+
# Tokenise a hand-built event stream of (sym_id, length_ms) tuples.
|
|
61
|
+
events = [
|
|
62
|
+
(vocab.ID_ISO, 200), (vocab.ID_P, 80), (vocab.ID_ISO, 80),
|
|
63
|
+
(vocab.ID_Q, 20), (vocab.ID_R, 40), (vocab.ID_S, 40),
|
|
64
|
+
(vocab.ID_ISO, 120), (vocab.ID_T, 200), (vocab.ID_ISO, 220),
|
|
65
|
+
]
|
|
66
|
+
packed = codec.encode(events) # uint16 array (RLE pack)
|
|
67
|
+
print(codec.render_compact(events)) # one char per event
|
|
68
|
+
print(codec.render_timed(events, 20)) # char count proportional to ms
|
|
69
|
+
print(codec.decode(packed) == events) # round-trip
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
For wave segmentation on a real ECG signal (10s, 250 Hz, single lead → per-frame P/QRS/T/other labels), use `openecg.stage2.infer.predict_frames` after loading a checkpoint with `load_model`. End-to-end examples: `scripts/validate_v4_lit_metrics.py`, `scripts/sota_comparison.py`.
|
|
73
|
+
|
|
74
|
+
## Status
|
|
75
|
+
|
|
76
|
+
Stage 1 v1.0 complete: tokenization pipeline + LUDB validation baseline.
|
|
77
|
+
|
|
78
|
+
- Spec: `docs/superpowers/specs/2026-05-03-ecgcode-stage1-design.md`
|
|
79
|
+
- Plan: `docs/superpowers/plans/2026-05-03-ecgcode-stage1.md`
|
|
80
|
+
- Latest validation: `out/validation_v1_*.json`, `out/ablation_*.json`
|
|
81
|
+
|
|
82
|
+
### v1.0 baseline metrics (NK dwt vs LUDB cardiologist on 41 val records)
|
|
83
|
+
|
|
84
|
+
| Metric | Result | Target |
|
|
85
|
+
|---|---|---|
|
|
86
|
+
| QRS_on boundary median | 8 ms | ≤ 20 ms ✓ |
|
|
87
|
+
| Q-loss rate | 5.1% | ≤ 20% ✓ |
|
|
88
|
+
| Pacer TPR | 10/10 records | ≥ 8/10 ✓ |
|
|
89
|
+
| P frame F1 | 0.49 | ≥ 0.80 ✗ |
|
|
90
|
+
| QRS frame F1 | 0.67 | ≥ 0.90 ✗ |
|
|
91
|
+
| T frame F1 | 0.51 | ≥ 0.75 ✗ |
|
|
92
|
+
| Pacer FPR | 8.07 / 10s | < 2 ✗ |
|
|
93
|
+
|
|
94
|
+
NK dwt over-detects waves vs cardiologist (boundary precision is high but recall too eager). Stage 2 (frame classifier) is the planned mitigation. Pacer FPR requires detector tuning (v1.1).
|
|
95
|
+
|
|
96
|
+
### Stage 2 v1.0 metrics (Conv+Transformer trained supervised on LUDB cardiologist labels, 41 val records)
|
|
97
|
+
|
|
98
|
+
| Metric | Model | NK direct (S1) | Δ | Target |
|
|
99
|
+
|---|---|---|---|---|
|
|
100
|
+
| P frame F1 | **0.604** | 0.492 | +0.112 | ≥ 0.80 |
|
|
101
|
+
| QRS frame F1 | **0.806** | 0.666 | +0.140 | ≥ 0.90 |
|
|
102
|
+
| T frame F1 | **0.695** | 0.512 | +0.183 | ≥ 0.75 |
|
|
103
|
+
| P_on boundary sens / median | 0.89 / 8 ms | 0.79 / 12 ms | — | — |
|
|
104
|
+
| QRS_on boundary sens / median | 0.97 / 8 ms | 0.76 / 6 ms | — | ≤ 20 ms ✓ |
|
|
105
|
+
| T_on boundary sens / median | 0.89 / 12 ms | 0.41 / 32 ms | — | — |
|
|
106
|
+
|
|
107
|
+
**Critical pass criterion (model > NK on all wave classes): ✓** Model improves over NK direct by +0.11~0.18 F1 across P/QRS/T. Boundary sensitivity jumps from NK's 0.41-0.79 to model's 0.89-0.98. Absolute F1 targets (P≥0.80, QRS≥0.90, T≥0.75) not yet hit — v2 candidates: bigger model, augmentation, longer training, multi-lead joint.
|
|
108
|
+
|
|
109
|
+
Stage 2 spec: `docs/superpowers/specs/2026-05-03-ecgcode-stage2-design.md`. Plan: `docs/superpowers/plans/2026-05-03-ecgcode-stage2.md`. Train: `scripts/train_stage2.py` (~35s on RTX 4090). Validate: `scripts/validate_stage2.py`. Checkpoint: `data/checkpoints/stage2_v1.pt` (gitignored).
|
|
110
|
+
|
|
111
|
+
**v1.1 ablation (uniformly worse, kept for reference)**: scaling model to d=128/L=8 + augmentation + longer training produced ~-0.013 F1 across all classes (P=0.591, QRS=0.791, T=0.683). Conclusion: 211K params is right-sized for 1908 LUDB sequences; bigger model without more data slightly regresses. Augmentation (time-shift especially) may have introduced label/signal misalignment. Reverted defaults to v1.0; LUDBFrameDatasetAugmented kept for future v3 experiments. Ablation script: `scripts/train_stage2_v11.py`.
|
|
112
|
+
|
|
113
|
+
### Stage 2 v3 investigation, ISP alignment bug, and v4 baseline
|
|
114
|
+
|
|
115
|
+
The v3 setup (combined LUDB+QTDB+ISP, d=128/L=8, focal+aug) initially appeared to regress on LUDB val by ~0.15-0.20 F1 vs v1.0. Initial 5-setting ablation seemed to isolate `lead_emb` as a "dataset proxy" — removing it appeared to recover most of the regression.
|
|
116
|
+
|
|
117
|
+
**Both findings were artifacts of a label alignment bug** (`scripts/check_isp_alignment.py`). `gt_to_super_frames` previously computed `samples_per_frame = n_samples // n_frames`, which gave 19 instead of 20 for ISP records of 9999 samples at 1000Hz with frame_ms=20. ISP labels then drifted by up to 500ms by frame 499. LUDB (exactly 5000 samples) and QTDB (separate code path) were unaffected, so all "LUDB-only" results stayed valid.
|
|
118
|
+
|
|
119
|
+
Fix: `samples_per_frame = round(fs * frame_ms / 1000)`, with the trailing partial frame dropped and labels padded to `WINDOW_FRAMES` in the ISP loader. After re-running every combined-data setting (`scripts/redo_v3_after_isp_fix.py`, results in `out/redo_v3_after_isp_fix_*.json`):
|
|
120
|
+
|
|
121
|
+
| Setting | LUDB val P/QRS/T | QTDB ext | ISP test |
|
|
122
|
+
|---|---|---|---|
|
|
123
|
+
| F (LUDB only, no lead_emb) | 0.633 / 0.806 / 0.710 | 0.484 / 0.554 / 0.163 | 0.687 / 0.891 / 0.756 |
|
|
124
|
+
| **C (combined, big, lead_emb on, CE)** | **0.659 / 0.798 / 0.704** | **0.751 / 0.756 / 0.537** | **0.833 / 0.935 / 0.848** |
|
|
125
|
+
| D (= v3: combined + big + focal+aug + lead_emb) | 0.649 / 0.789 / 0.705 | 0.642 / 0.641 / 0.249 | 0.821 / 0.935 / 0.841 |
|
|
126
|
+
| E (combined, small, **no lead_emb**) | 0.640 / 0.789 / 0.702 | 0.536 / 0.582 / 0.209 | 0.813 / 0.931 / 0.837 |
|
|
127
|
+
|
|
128
|
+
**v4 = C** (combined LUDB+QTDB+ISP, d=128/L=8, lead_emb on, CE loss): best on every domain. Cross-domain wins are large (+0.10-0.15 on ISP, +0.20-0.30 on QTDB) at the cost of only -0.008 on LUDB val QRS vs F.
|
|
129
|
+
|
|
130
|
+
**Revised conclusions (post-fix):**
|
|
131
|
+
- v3's original design (combined data + bigger model) was correct — the apparent regression was the alignment bug, not bad architecture choices.
|
|
132
|
+
- Lead embedding is roughly neutral with clean labels (lead_emb on vs off differs by ~0.01-0.03 F1).
|
|
133
|
+
- Combined training does help cross-domain generalization (the original v3 intent).
|
|
134
|
+
- Focal loss + augmentation (D vs C) is fine on LUDB/ISP but causes QTDB T-wave F1 to collapse from 0.54 → 0.25; recommend plain CE (= C) until that's understood.
|
|
135
|
+
|
|
136
|
+
**Reference checkpoints**: `data/checkpoints/stage2_v4_C.pt` (C, primary v4), `data/checkpoints/stage2_v4_ludb_only.pt` (F, LUDB-only reference), `data/checkpoints/stage2_v4_combined_fixed.pt` (G, lead-agnostic combined).
|
|
137
|
+
|
|
138
|
+
### Stage 2 v4 — literature-style boundary metrics (150ms tolerance, post-proc)
|
|
139
|
+
|
|
140
|
+
`scripts/validate_v4_lit_metrics.py` reports boundary F1 / Se / PPV / median timing error in the format used by Martinez 2004, LUDB / Kalyakulina 2020, SemiSegECG 2025.
|
|
141
|
+
|
|
142
|
+
| Boundary | C LUDB | F LUDB | **C ISP** | F ISP | C QTDB | F QTDB | Literature |
|
|
143
|
+
|---|---|---|---|---|---|---|---|
|
|
144
|
+
| p_on | 0.758 | 0.701 | **0.919** | 0.795 | 0.801 | 0.769 | LUDB 0.93–0.96 / SemiSegECG ISP 0.97 |
|
|
145
|
+
| qrs_on | 0.870 | 0.857 | **0.970** | 0.958 | 0.844 | 0.829 | LUDB 0.98–0.99 / Martinez QTDB 0.99 / ISP 0.99 |
|
|
146
|
+
| t_on | 0.778 | 0.752 | **0.935** | 0.885 | 0.484 | 0.467 | LUDB 0.92–0.95 / ISP 0.95 |
|
|
147
|
+
| p_off | 0.774 | 0.730 | **0.931** | 0.833 | 0.801 | 0.786 | LUDB 0.93–0.96 |
|
|
148
|
+
| qrs_off | 0.878 | 0.865 | **0.970** | 0.957 | 0.845 | 0.833 | LUDB 0.98–0.99 / Martinez QTDB 0.99 |
|
|
149
|
+
| t_off | 0.771 | 0.747 | **0.928** | 0.877 | 0.828 | 0.792 | LUDB 0.92–0.95 / Martinez QTDB 0.93 |
|
|
150
|
+
|
|
151
|
+
Median timing error in ms (spec target ≤20ms): C achieves 8–20ms on every boundary across LUDB/ISP/QTDB except p_off LUDB (20ms, tied at target). F achieves 8–24ms (t_off ISP=24ms is the only miss).
|
|
152
|
+
|
|
153
|
+
**ISP test ≈ literature SOTA**: C reaches QRS F1=0.970 vs SOTA ~0.99 (gap 0.02), T F1=0.93–0.94 vs ~0.95–0.96 (gap 0.02), P F1=0.92–0.93 vs ~0.97 (gap 0.04) — supervised CE only, no semi-supervised tricks.
|
|
154
|
+
|
|
155
|
+
**LUDB val ≈ 0.10–0.15 below SOTA**: 1908 train sequences from 159 records is small for a 1M-param Transformer; FP rate is high (n_pred > n_true by ~15–20%) which suppresses PPV. Larger models and/or LUDB-style augmentation are the obvious next step. Median timing error is already at spec.
|
|
156
|
+
|
|
157
|
+
### Stage 2 v4 — SOTA paper comparison (Martinez per-boundary tolerances)
|
|
158
|
+
|
|
159
|
+
`scripts/sota_comparison.py` re-evaluates with per-boundary tolerances used in the literature (P 50ms / QRS 40ms / T_on 50ms / T_off 100ms — stricter than the 150ms loose standard) and reports F1 / Se / PPV / signed mean ± SD timing error in the format used by Martinez 2004, DENS-ECG, SemiSegECG.
|
|
160
|
+
|
|
161
|
+
**ISP test (vs SemiSegECG 2025 semi-supervised SOTA):**
|
|
162
|
+
|
|
163
|
+
| Boundary | C F1 | C Se% | mean±SD ms | SOTA F1 | Gap |
|
|
164
|
+
|---|---|---|---|---|---|
|
|
165
|
+
| **qrs_on** | **0.988** | 98.9% | +2.2 ± 9.6 | 0.99 | **≈ SOTA** |
|
|
166
|
+
| qrs_off | 0.953 | 95.3% | -2.7 ± 12.5 | 0.99 | -0.04 |
|
|
167
|
+
| t_off | 0.926 | 93.0% | -4.5 ± 25.0 | 0.96 | -0.03 |
|
|
168
|
+
| p_on | 0.900 | 87.9% | +1.4 ± 15.2 | 0.97 | -0.07 |
|
|
169
|
+
| t_on | 0.853 | 85.6% | +0.4 ± 21.1 | 0.95 | -0.10 |
|
|
170
|
+
| p_off | 0.843 | 82.3% | +19.7 ± 17.4 | 0.97 | -0.13 |
|
|
171
|
+
|
|
172
|
+
**LUDB val (vs DENS-ECG / Moskalenko 2020):**
|
|
173
|
+
|
|
174
|
+
| Boundary | C Se% | C mean±SD ms | DENS Se% | DENS mean±SD | Gap |
|
|
175
|
+
|---|---|---|---|---|---|
|
|
176
|
+
| qrs_on | 95.6% | -1.9 ± 12.1 | 99.6% | -1.5 ± 4.6 | -4 pp |
|
|
177
|
+
| qrs_off | 92.7% | +1.8 ± 13.2 | 99.6% | +1.0 ± 6.0 | -7 pp |
|
|
178
|
+
| p_on | 82.9% | -2.2 ± 15.7 | 96.4% | -0.6 ± 9.9 | -14 pp |
|
|
179
|
+
| **p_off** | **68.7%** | **+21.7 ± 20.0** | 96.4% | -0.6 ± 9.4 | **-28 pp** |
|
|
180
|
+
| t_on | 81.7% | +1.5 ± 18.8 | 95.0% | -2.7 ± 13.7 | -13 pp |
|
|
181
|
+
| t_off | 88.2% | +3.4 ± 24.6 | 95.7% | +1.3 ± 18.1 | -7 pp |
|
|
182
|
+
|
|
183
|
+
**QTDB T-subset (vs Martinez 2004 wavelet):**
|
|
184
|
+
|
|
185
|
+
| Boundary | C F1 | C mean±SD ms | Martinez Se% | Martinez mean±SD |
|
|
186
|
+
|---|---|---|---|---|
|
|
187
|
+
| qrs_on | 0.913 | -1.4 ± 14.7 | 100.0% | +4.5 ± 7.7 |
|
|
188
|
+
| qrs_off | 0.897 | -1.5 ± 16.6 | 100.0% | +0.8 ± 10.9 |
|
|
189
|
+
| t_off | 0.828 | -13.1 ± 40.1 | 99.8% | -1.6 ± 18.1 |
|
|
190
|
+
|
|
191
|
+
**Key finding — p_off systematic bias and fix**: LUDB val p_off had mean error +21.7ms — the model predicted P-wave offset 22ms LATE relative to cardiologist annotation, dragging Se to 68.7%. Same pattern on ISP (+19.7ms). Tested two fixes (`scripts/fix_p_off_bias.py`):
|
|
192
|
+
|
|
193
|
+
| Strategy | C LUDB p_off F1 | C ISP p_off F1 |
|
|
194
|
+
|---|---|---|
|
|
195
|
+
| Baseline | 0.608 (+22ms) | 0.843 (+20ms) |
|
|
196
|
+
| Signal-aware trim k=2.0 | 0.628 (+17ms) | 0.830 (+10ms) |
|
|
197
|
+
| **Fixed -22ms shift** | **0.737 (+6ms)** | **0.911 (+1ms)** |
|
|
198
|
+
|
|
199
|
+
Per-checkpoint shifts now in `openecg/stage2/infer.py`: `BOUNDARY_SHIFT_C = {"p_off": -22}` and `BOUNDARY_SHIFT_F = {"p_off": -15}` (F has smaller +14ms bias). Use with the new `extract_boundaries(frames, boundary_shift_ms=BOUNDARY_SHIFT_C)` helper. With C+shift, LUDB val avg Martinez F1 rises 0.757 → 0.779 (+0.022); ISP test 0.911 → 0.922 (+0.011). p_off is no longer the LUDB outlier; remaining gap to DENS-ECG is uniform ~7-14pp Se across P/QRS/T (likely capacity / data scale).
|
|
200
|
+
|
|
201
|
+
Signal-aware trim was less effective because P-wave's gradual return to baseline isn't crisply distinguishable from baseline noise at the std level. The bias is the model learning to extend P inclusively — fixing it via a learned p_off head (Stage 3 boundary refinement) is the principled path; the shift is a deployment workaround.
|
|
202
|
+
|
|
203
|
+
### Stage 2 v4 — model capacity scan (`scripts/train_v4_bigger.py`)
|
|
204
|
+
|
|
205
|
+
| Model | params | LUDB val avg Martinez F1 | ISP test avg F1 |
|
|
206
|
+
|---|---|---|---|
|
|
207
|
+
| **C (d=128/L=8, current v4)** | **1.08M** | **0.779** | **0.922** |
|
|
208
|
+
| Cbig d=192/L=10 | 2.51M | 0.780 (+0.001) | 0.919 (-0.003) |
|
|
209
|
+
| Cbig d=256/L=8 | 3.21M | 0.783 (+0.004) | 0.918 (-0.004) |
|
|
210
|
+
|
|
211
|
+
3× more parameters yields essentially zero improvement on LUDB val (+0.004 within noise) and a slight decrease on ISP test. **LUDB train scale (1908 sequences) is the bottleneck, not model capacity.** Closing the remaining ~7-14pp Se gap to DENS-ECG SOTA needs more data, not more parameters: Stage 4 SSL pretraining (Icentia 11k / MIMIC-IV / SNUH per PLAN.md), augmentation re-test now that ISP labels are clean, or a Stage 3 learned boundary refinement head. Keep C at d=128/L=8 as the operating point.
|
|
212
|
+
|
|
213
|
+
### Stage 2 v3 investigation — QTDB label sparsity
|
|
214
|
+
|
|
215
|
+
**QTDB T-wave F1 ≈0.49 was a label-sparsity artifact** (`scripts/eval_qtdb_t_annotated.py`): q1c annotates QRS+P on essentially every examined beat but T on only ~half. Per-record windowed T:QRS ratio: median **0.00**, mean 0.45 (only 39 of 105 records have ratio ≥ 0.8). The model correctly predicts T at every beat but unannotated beats become FP. Re-evaluating only on the 39-record T-annotated subset:
|
|
216
|
+
|
|
217
|
+
| Boundary | C full QTDB | C T-subset | Δ |
|
|
218
|
+
|---|---|---|---|
|
|
219
|
+
| qrs_on / qrs_off | 0.858 / 0.861 | **0.957 / 0.958** | +0.099 / +0.098 |
|
|
220
|
+
| t_on / t_off | 0.492 / 0.828 | **0.858 / 0.866** | **+0.366 / +0.038** |
|
|
221
|
+
| p_on / p_off | 0.799 / 0.799 | 0.803 / 0.801 | +0.004 / +0.002 |
|
|
222
|
+
|
|
223
|
+
C on the T-annotated subset reaches QRS F1 ~0.96 vs Martinez QTDB ~0.99 — within 0.03 of literature SOTA. Use the T-subset numbers when comparing to QTDB-based papers.
|
|
224
|
+
|
|
225
|
+
**Post-processing defaults tuned**: `scripts/tune_postproc_v4.py` swept `min_duration_ms × merge_gap_ms` on LUDB val and found `(60, 200)` beats the previous `(40, 300)` by avg +0.010 boundary F1 on C and +0.022 on F. Defaults updated in `openecg/stage2/infer.py`. The remaining LUDB gap to literature (~0.10) is from model capacity / data scale, not post-proc tuning.
|
|
226
|
+
|
|
227
|
+
### Stage 2 v4 — per-lead robustness (12 leads on LUDB val)
|
|
228
|
+
|
|
229
|
+
`scripts/per_lead_v4.py` breaks down boundary F1 per LUDB lead. The deployment goal is a single-lead model that works on any of the 12 leads (Holter, wearable, ICU monitor).
|
|
230
|
+
|
|
231
|
+
| Boundary F1 std across 12 leads | C (combined) | F (LUDB only) |
|
|
232
|
+
|---|---|---|
|
|
233
|
+
| qrs_on | **0.011** | 0.007 |
|
|
234
|
+
| qrs_off | **0.008** | 0.007 |
|
|
235
|
+
| p_on | **0.029** | 0.036 |
|
|
236
|
+
| p_off | **0.030** | 0.040 |
|
|
237
|
+
| t_on | **0.020** | 0.049 |
|
|
238
|
+
| t_off | **0.030** | 0.051 |
|
|
239
|
+
|
|
240
|
+
C is more uniformly robust on P/T (std 0.020–0.030 vs F's 0.040–0.051). QRS robustness is similar (both std ≤0.011). The weakest single leads are physiologically expected: lead **III** (small P/T amplitude due to electrical axis orthogonality) and **aVL** (small P), which are uncommon as sole monitoring leads in clinical practice.
|
|
241
|
+
|
|
242
|
+
Per-lead median timing error meets the ≤20ms spec target on QRS (8–12ms across all 12 leads for both C and F) and on P/T for most leads (8–16ms; p_off occasionally hits 28–40ms on V1–V3).
|
|
243
|
+
|
|
244
|
+
## Setup
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
uv sync
|
|
248
|
+
$env:UV_LINK_MODE = "copy" # Windows + OneDrive workaround
|
|
249
|
+
$env:OPENECG_LUDB_ZIP = "<path-to-LUDB-zip>"
|
|
250
|
+
|
|
251
|
+
uv run pytest # 65 tests (50 unit + 15 stage2 + LUDB integration if env set)
|
|
252
|
+
|
|
253
|
+
# Stage 1 (NK baseline tokenization)
|
|
254
|
+
uv run python scripts/tokenize_ludb.py # → data/ludb_tokens.npz
|
|
255
|
+
uv run python scripts/validate_v1.py # → out/validation_v1_*.json
|
|
256
|
+
uv run python scripts/ablate_methods.py # → out/ablation_*.json
|
|
257
|
+
uv run python scripts/validate_pacer.py # console only
|
|
258
|
+
|
|
259
|
+
# Stage 2 (neural frame classifier, requires CUDA)
|
|
260
|
+
uv run python scripts/train_stage2.py # → data/checkpoints/stage2_v1.pt (~35s on RTX 4090)
|
|
261
|
+
uv run python scripts/validate_stage2.py # → out/validation_stage2_*.json
|
|
262
|
+
```
|