Repository navigation
Add write_annotations API - #784
Conversation
tompollard
left a comment
There was a problem hiding this comment.
This looks ready to merge.
Checked locally:
- The caller's object is unchanged. I compared every attribute of the annotation before and after
write_annotationsfor seven kinds of annotation: read from files, built with symbols only, built withlabel_storeonly, and built with custom labels as a DataFrame and as a list. Nothing changed:wrannreplaces attributes on the copy rather than editing shared values, andcustom_labelsis deep-copied. - Destinations.
bare.atrin the current folder works, as dox.atr_1,x.ATRandmemory://…/r.16a.x.v1.atr,my rec.atranda+b.atrare rejected with the record-name error.- A missing parent folder raises
FileNotFoundError.
write_fs=Truewithoutfswrites no frequency and reads backfs=None, as the docstring says.- Round trips. Every annotation file in
sample-datagives the same samples, symbols and notes when read back, except the one below. - The offline suite and ruff pass.
An older bug, outside this PR: wfdb.rdann("sample-data/12726", "wabp") can't be written back by either write_annotations or Annotation.wrann. Its label codes aren't in the standard table, so rdann returns nan symbols, and the writer then raises TypeError: Subelements of the symbol field must be strings. The cause is in rdann, so this is probably worth its own issue.
Optional: test_local_round_trip_preserves_source checks record_name, extension and label_store. A case built with symbols only and custom labels, checking that the arrays and custom_labels are unchanged after writing, would test the full guarantee.
Taking the path first matches read_annotations and numpy.save. It would be good for the planned write_record to use the same order.
|
Thanks for the thorough check. I fixed the older I also added the suggested source-preservation test using custom labels. The full offline suite passes (1,119 passed, 4 skipped), as do Ruff and the strict docs build. I agree that a future |
tompollard
left a comment
There was a problem hiding this comment.
Thanks for fixing the unknown-code bug here.
- The
12726.wabpround trips pass in all three label modes and through the legacy writer. summarize_labels=Truecounts code 42.- Normal files are unaffected:
rdann("sample-data/100", "atr").label_storeis stillNone.
There's one remaining problem: while any nan symbol is present, edits to symbol are silently discarded.
ann = wfdb.rdann("sample-data/12726", "wabp")
ann.symbol[4] = "V" # was "N"
ann.record_name = "edit"
ann.wrann(write_dir=out)
wfdb.rdann(f"{out}/edit", "wabp").symbol[4] # "N"On main, this raised TypeError: Subelements of the symbol field must be strings. Now it writes without warning and drops the edit, because the nan path in Annotation.wrann sets symbol to None and writes every code from label_store.
It goes the other way too. If every nan is replaced (for example with "Q"), the writer uses symbol and ignores label_store. So which field wins depends on whether a single nan is left anywhere.
Building each code from symbol where it's a string, and falling back to label_store only where it's nan, would keep both the unknown codes and the user's edits. A test that edits one known symbol in 12726.wabp and checks the written value would cover it.
Not blocking, just noting:
rdannnow returns alabel_storearray for files with unknown codes, even with the defaultreturn_label_elements=["symbol"]. The changelog covers this.- Code that filters only
sampleandsymbolnow fails with "The lengths of the 'sample' and 'label_store' fields do not match". It already failed onmainfor these files, so this isn't a regression.
|
You're right; the previous fallback could silently discard an edit. I fixed that in I added regression tests that change The extra |
tompollard
left a comment
There was a problem hiding this comment.
Thanks, this looks ready to merge.
- Edits now survive. Each position now resolves to its symbol, then its description, then the raw code, and the new tests cover symbol and description edits through both writers.
- No codes change. I wrote one annotation for every code from 1 to 49 in the label table, plus the unknown code 42, and read it back first with symbols and then with descriptions. Writing it again changed no codes. No symbol or description appears twice in the table, so the lookups can't map a label to a different code.
- Custom labels. Adding a custom label for 42 after reading still writes 42.
- The offline suite and ruff pass.
One limitation to be aware of, not blocking: because symbols take precedence, editing label_store where a symbol is known has no effect. Setting label_store[4] from 1 to 5 in 12726.wabp still writes 1. That matches the documented precedence, and editing symbols is the normal workflow.
Summary
Add
write_annotations(path, annotation, *, write_fs=True)as the write counterpart toread_annotations. The path is a complete annotation filename such as100.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'sAnnotation. It storesfswhen 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
16ain the existing writer, matching whatread_annotationsaccepts. Filename validation is shared by the new reader and writer. The legacywrannfunction andAnnotation.wrannmethod remain available. Writing annotations into.wfdbarchives and tophysionet://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.
rdannnow retains the rawlabel_storevalues 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 withsample-data/12726.wabp, which contains code 42.Verification
memory://round trips,fshandling, source object preservation (including custom labels),.16awriting through both new and legacy interfaces, invalid destinations, unknown label codes, and edits to known symbols and descriptions alongside unknown codes.Related: #672 and #783.