Skip to content

Write custom annotation symbols of more than one character - #669

Merged
tompollard merged 2 commits into
mainfrom
wrann-multichar-symbols
Nov 23, 2026
Merged

tompollard merged 2 commits into
mainfrom
wrann-multichar-symbols

Conversation

@tompollard

@tompollard tompollard commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Fixes #411.

wrann failed with TypeError: ord() expected a character for a custom label whose symbol has more than one character, although rdann reads such symbols. custom_triplet_bytes called ord() on the whole symbol, and computed the note's length byte for a one-character symbol. It now builds the note ("<label_store> <symbol> <description>") as a string and encodes it as Latin-1, as aux_note is.

  • Symbols of any length: the checks limited symbols to 3 characters, which the WFDB format doesn't. They may now have any length, and must still be non-empty and have no whitespace.
  • Notes over 255 bytes: the note's length is stored in one byte, so a longer note used to be written as a file that rdann couldn't read. wrann now raises a ValueError before writing anything, as it already does for a long aux_note. It does the same for characters that Latin-1 can't hold.

For one-character symbols the bytes are the same as before: a file with two custom labels and fs written with this branch is identical to one written with main.

Testing

  • New round-trip tests for custom symbols of 1, 2, 3, 4 and 10 characters, for the 255/256-byte note boundary (the longer one raises and writes no file), and for a non-Latin-1 description. All but the one-character case fail on main.
  • pytest -m "not network": all pass. Ruff passes.

@tompollard 🟥 red 🦥 sloth · aspen

custom_triplet_bytes called ord() on the whole symbol, and computed the
note's length for a one-character symbol, so wrann failed with a TypeError
for custom labels with 2 or 3 character symbols. Build the note as a string
first. Files with one-character symbols are unchanged.

Fixes #411.

@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.

I found two issues to address before merging 6da26ce:

  1. custom_triplet_bytes() can write an unreadable annotation file when the encoded note is longer than the one-byte AUX length (src/wfdb/io/annotation.py:1595). I reproduced this with custom symbol PV2 and a 249-character description: the note length is 256, which becomes 0 when converted to u1. wrann() returns successfully, but rdann() raises IndexError on the file. Please reject an oversized note before writing and test the 255/256-byte boundary.

  2. The PR says it fixes #411, but validation still rejects symbols longer than three characters (src/wfdb/io/annotation.py:504). wrann(..., symbol=["ABCD"], custom_labels=[(42, "ABCD", "beat")]) raises ValueError. The issue explicitly describes multi-character mnemonics without a three-character limit. Could this support longer symbols within the annotation note's capacity, or narrow the PR's scope and leave #411 open?

The 43 focused offline annotation tests and all 18 PR checks pass; they do not cover these cases.

Lift the 3-character limit on symbols, which the WFDB format does not
have; symbols must still be non-empty and have no whitespace. A custom
label is written as a note whose length is stored in one byte, so raise a
ValueError, before any file is written, when the note is longer than 255
Latin-1 characters or cannot be encoded, instead of writing a file that
rdann cannot read.
@tompollard

Copy link
Copy Markdown
Member Author

Thanks, both fixed in 95dd3b6.

  1. Notes over 255 bytes: custom_triplet_bytes now encodes the note ("<number> <symbol> <description>") as Latin-1, as aux_note already is, and raises a ValueError if it's longer than 255 bytes or has characters that Latin-1 can't hold. The custom label bytes are made before the file is opened, so nothing is written. This also fixes one-character symbols, which had the same problem with a long enough description. New tests cover the boundary: with PV2, a 248-character description (255 bytes) round-trips, and 249 (256 bytes) raises without creating a file. A third test covers a non-Latin-1 description.
  2. Symbol length: I removed the 3-character limit in both checks (custom_labels and symbol), so symbols may have any length within the note's capacity, as Write multi-character annotation mnemnonics #411 describes. They must still be non-empty and have no whitespace. ABCD and LONGSYMBOL now round-trip through wrann and rdann, so this PR does fix Write multi-character annotation mnemnonics #411.

One-character custom labels still give a file byte-identical to main. All offline tests and ruff pass.

@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.

Re-reviewed commit 95dd3b6. Both findings from my earlier review are resolved: custom symbols longer than three characters now round-trip, and custom label notes over 255 bytes raise ValueError before creating a file. The 48 focused offline annotation tests pass. I found no remaining blocker. Docs and style checks pass; the test matrix is still running.

@tompollard
tompollard merged commit 493bd7e into main Nov 23, 2026
18 checks passed
@tompollard
tompollard deleted the wrann-multichar-symbols branch November 23, 2026 23:01
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.

Write multi-character annotation mnemnonics

1 participant