Repository navigation
Add readable read_annotations API - #783
Conversation
tompollard
left a comment
There was a problem hiding this comment.
This works, and the argument mapping is right. One thing should be fixed before merge: inferring the annotator from the filename makes it easy to read a record file as annotations.
Checked locally:
stopis exclusive, as documented.rdannkeepssample < sampto, sostop=370drops the beat at sample 370 andstop=371keeps it.relative_samplesshifts samples bystart.- Paths are split correctly for:
- folders with dots (
data.v1/100.atrbecomesdata.v1/100andatr); physionet://mitdb/1.0.0/100.atr;memory://100.atr;- annotator names like
atr_1; - archives with an explicit annotator.
- folders with dots (
- Stems without an extension,
100., and.atrare rejected. - The README example reads from PhysioNet, and the offline suite and ruff pass.
Should fix: record files are accepted as annotation files.
read_annotations("sample-data/100.hea")returns 82 meaningless annotations (symbolsA,t,B, …), andread_annotations("sample-data/100.dat")returns 898,043.a103l.matand100.csvalso get as far as decoding, and then fail with anIndexErrorand a reshape error.rdann("100", "hea")behaves the same way, but there the caller has to ask for it. Under the new contract, passing a file path is the normal call, and100.heais the WFDB file people are most likely to pass.
A ValueError when the inferred annotator is hea or dat would catch this. For example: "100.hea is a record header; pass an annotation file such as 100.atr". The .wfdb case is already handled. Tests for 100.hea and 100.dat would keep it covered.
Smaller points:
read_annotations("100.atr", annotator="qrs")callsrdann("100.atr", "qrs")and ends in aFileNotFoundErrorfor100.atr.qrs. Since record names can't contain dots, this combination is always a mistake. An error like "path ends in .atr but annotator is 'qrs'" would explain what went wrong.annotator=""andannotator=".atr"are passed through unchanged.label_fields=5raises a rawTypeError: 'int' object is not iterablefromrdann.
A quick check of both arguments would catch these.
Contract: read_record("100") takes a record stem, while read_annotations("100.atr") takes an annotation filename. That seems right to me, because an annotation is one file (as with read_edf), and the README explains the difference. It's also something that can't change once released, so it may be worth saying in the PR description that it's intentional.
|
Thanks for checking these cases. I fixed the filename inference issue in It also rejects a filename/annotator mismatch, an empty or dotted The path contract is intentional: |
tompollard
left a comment
There was a problem hiding this comment.
Thanks. Everything from the last review is fixed:
100.hea,100.HEAand100.datare rejected with a clear message, and an explicitannotatorstill works as an escape hatch.- A mismatched annotator, an empty or dotted
annotator, and invalidlabel_fieldsall give clear errors. - The offline suite and ruff pass.
There's one regression: the annotator pattern [A-Za-z][A-Za-z0-9_-]* requires a leading letter, but some PhysioNet annotators start with a digit. The Long-Term ST Database (ltstdb/1.0.0) has .16a files. For record s20011:
wfdb.rdann("physionet://ltstdb/1.0.0/s20011", "16a")reads it.read_annotations("physionet://ltstdb/1.0.0/s20011.16a")raises "does not have a valid annotation extension".read_annotations("physionet://ltstdb/1.0.0/s20011", annotator="16a")raises "annotator must start with a letter…".
So the new reader has no way to read these files. Allowing a leading digit, [A-Za-z0-9][A-Za-z0-9_-]*, in both the inferred and the explicit check would fix it. That still rejects empty and dotted names. A test case with 16a would cover it.
|
You're right; I made the annotator validation too restrictive. Commit The focused reader tests pass (36 passed), and Ruff passes. CI is running on the new commit. |
## Summary Add `write_annotations(path, annotation, *, write_fs=True)` as the write counterpart to `read_annotations`. The path is a complete annotation filename such as `100.atr`; local and supported cloud destinations use the existing annotation writer. The new function writes from a copy, so preparing labels and changing the output name do not mutate the caller's `Annotation`. It stores `fs` when available by default, so an annotation read from a record can be written and read again without needing a header beside the output file. Allow digit-prefixed annotator extensions such as `16a` in the existing writer, matching what `read_annotations` accepts. Filename validation is shared by the new reader and writer. The legacy `wrann` function and `Annotation.wrann` method remain available. Writing annotations into `.wfdb` archives and to `physionet://` is still unsupported. Also fix a pre-existing round-trip failure for annotation files with numeric label codes that have no symbol in the standard table. `rdann` now retains the raw `label_store` values when a requested symbol or description is unknown; the writers encode each annotation from its available symbol, then description, and fall back to the raw code only when its label is unknown. This preserves user edits to known labels even when another annotation has an unknown code. Label summaries include unknown codes. These cases are covered with `sample-data/12726.wabp`, which contains code 42. ## Verification - Tests cover local and `memory://` round trips, `fs` handling, source object preservation (including custom labels), `.16a` writing through both new and legacy interfaces, invalid destinations, unknown label codes, and edits to known symbols and descriptions alongside unknown codes. - Offline suite: 1,123 passed, 4 skipped, 22 network tests deselected. - Ruff check and format pass; Sphinx HTML docs build passes with warnings treated as errors. Related: #672 and #783.
Summary
Add
read_annotationsas the annotation counterpart toread_recordfrom #781. The path difference is intentional:read_record("100")takes a record stem, whileread_annotations("100.atr")takes an annotation filename because an annotation is one file. Full filenames support local, cloud, andphysionet://paths. A record stem or local.wfdbarchive can be used withannotator="atr"; the annotator is required for an archive because it may contain multiple annotation files.The wrapper maps
start,stop,relative_samples, andlabel_fieldsto the existingrdannoptions and returns the same mutableAnnotation. It shares range validation with the new record readers. It rejects common non-annotation suffixes when inferring an annotator, conflicting filename/annotator combinations, and malformed annotator or label options.rdannkeeps its signature and behavior, with no warnings. The README quick start now usesread_recordandread_annotationstogether.Verification
memory://paths, selections, label options, argument forwarding, non-annotation suffixes, and invalid arguments.Related: #672 and #534.