Skip to content

Add readable read_annotations API - #783

Merged
tompollard merged 3 commits into
mainfrom
read-annotations-api
Nov 30, 2026
Merged

tompollard merged 3 commits into
mainfrom
read-annotations-api

Conversation

@tompollard

@tompollard tompollard commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Add read_annotations as the annotation counterpart to read_record from #781. The path difference is intentional: read_record("100") takes a record stem, while read_annotations("100.atr") takes an annotation filename because an annotation is one file. Full filenames support local, cloud, and physionet:// paths. A record stem or local .wfdb archive can be used with annotator="atr"; the annotator is required for an archive because it may contain multiple annotation files.

The wrapper maps start, stop, relative_samples, and label_fields to the existing rdann options and returns the same mutable Annotation. 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. rdann keeps its signature and behavior, with no warnings. The README quick start now uses read_record and read_annotations together.

Verification

  • Tests cover filename and stem reads, archive and memory:// paths, selections, label options, argument forwarding, non-annotation suffixes, and invalid arguments.
  • Offline suite: 1,090 passed, 4 skipped, 22 network tests deselected.
  • Ruff check and format pass; Sphinx HTML documentation builds with warnings treated as errors.

Related: #672 and #534.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  • stop is exclusive, as documented. rdann keeps sample < sampto, so stop=370 drops the beat at sample 370 and stop=371 keeps it.
  • relative_samples shifts samples by start.
  • Paths are split correctly for:
    • folders with dots (data.v1/100.atr becomes data.v1/100 and atr);
    • physionet://mitdb/1.0.0/100.atr;
    • memory://100.atr;
    • annotator names like atr_1;
    • archives with an explicit annotator.
  • Stems without an extension, 100., and .atr are 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 (symbols A, t, B, …), and read_annotations("sample-data/100.dat") returns 898,043.
  • a103l.mat and 100.csv also get as far as decoding, and then fail with an IndexError and 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, and 100.hea is 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") calls rdann("100.atr", "qrs") and ends in a FileNotFoundError for 100.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="" and annotator=".atr" are passed through unchanged.
  • label_fields=5 raises a raw TypeError: 'int' object is not iterable from rdann.

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.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for checking these cases. I fixed the filename inference issue in 0fdd2b4: read_annotations now rejects common record and other non-annotation suffixes (.hea, .dat, .mat, .csv, .edf, and .wav) instead of decoding them as annotations. An explicit annotator still supports unusual annotation filenames.

It also rejects a filename/annotator mismatch, an empty or dotted annotator, and non-string or invalid label_fields with errors that name the new arguments. I added tests for these cases, including 100.hea and 100.dat.

The path contract is intentional: read_record("100") takes a record stem, while read_annotations("100.atr") takes the annotation filename. I’ve made that explicit in the PR description. The focused reader tests pass (36 passed), as do Ruff and the strict docs build.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Everything from the last review is fixed:

  • 100.hea, 100.HEA and 100.dat are rejected with a clear message, and an explicit annotator still works as an escape hatch.
  • A mismatched annotator, an empty or dotted annotator, and invalid label_fields all 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.

@tompollard

Copy link
Copy Markdown
Member Author

You're right; I made the annotator validation too restrictive. Commit 1b6e333 allows names beginning with a digit in both the filename and explicit annotator forms. I added a local .16a fixture and checked both calls against rdann; they return the same annotations. The known non-annotation suffixes remain blocked when inferred.

The focused reader tests pass (36 passed), and Ruff passes. CI is running on the new commit.

@tompollard
tompollard merged commit 272cbc1 into main Nov 30, 2026
19 checks passed
@tompollard
tompollard deleted the read-annotations-api branch November 30, 2026 05:47
tompollard added a commit that referenced this pull request Nov 30, 2026
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant