Skip to content

Keep every annotation field in Annotation.apply_range - #661

Open
tompollard wants to merge 4 commits into
mainfrom
annotation-apply-range
Open

tompollard wants to merge 4 commits into
mainfrom
annotation-apply-range

Conversation

@tompollard

Copy link
Copy Markdown
Member

Follow-up to #654, from the review there.

Annotation.apply_range filtered sample, label_store, subtype, chan, num and aux_note, but not symbol or description. It also indexed label_store unconditionally, so on record 100:

>>> ann = wfdb.rdann("sample-data/100", "atr")  # default: symbol, no label_store
>>> ann.apply_range(sampfrom=600000)
TypeError: 'NoneType' object is not subscriptable  # main

>>> ann = wfdb.rdann("sample-data/100", "atr", return_label_elements=["label_store", "symbol"])
>>> ann.apply_range(sampfrom=600000)
>>> len(ann.sample), len(ann.symbol)
(180, 2274)  # main
(180, 180)  # this PR

apply_range now filters every per-annotation field that is set (sample, label_store, symbol, description, subtype, chan, num and aux_note), keeping arrays as arrays and lists as lists, and leaves fields that are None as they are. So calling apply_range on an rdann result gives the same fields as reading only that range with rdann.

rdann calls apply_range itself when sampfrom is given, so I compared every field of rdann results before and after this change: for every annotation file in sample-data, with no range, sampfrom, sampto, both, shift_samps, and all three label elements (66 reads), the results are identical.

Testing:

  • New test_apply_range_keeps_every_field_consistent, for the default rdann result and with all three label elements, checks that apply_range gives the same fields as rdann(..., sampfrom=600000, sampto=620000), and that unset fields stay None. Both cases fail on main.
  • uv run pytest -m "not network": 493 passed. uv run pytest -m network: 18 passed.
  • ruff format --check, ruff check, the docs build and towncrier build --draft pass. Changelog entry added in changelog.d/.

@tompollard 🟥 red 🦥 sloth

apply_range filtered sample, label_store, subtype, chan, num and aux_note,
but not symbol or description, which then no longer matched sample. It
also failed with a TypeError on an annotation read by rdann with the
default return_label_elements, where label_store is None.

Filter every per-annotation field that is set, keeping arrays as arrays
and lists as lists, and leave fields that are not set as None. Calling
apply_range on an rdann result now gives the same fields as reading only
that range. rdann's own results are unchanged for every annotation file in
sample-data, with and without sampfrom, sampto and shift_samps.

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

One issue to address before merging: apply_range() now slices the per-annotation fields, but it leaves contained_labels unchanged if the annotation was previously summarized. That makes the summary disagree with the filtered annotations.

For example, starting with wfdb.rdann("sample-data/100", "atr", summarize_labels=True) and then calling apply_range(sampfrom=600000, sampto=620000) leaves 71 annotations, while contained_labels still counts all 2,274 original annotations. Reading that range directly with rdann(..., sampfrom=600000, sampto=620000, summarize_labels=True) gives a summary count of 71.

Could apply_range() refresh or clear contained_labels when it is present, and add a test for this sequence? The new per-annotation field handling otherwise looks good; the focused offline annotation tests passed.

apply_range kept contained_labels as it was, so after summarize_labels=True
the summary still counted every annotation, not only those kept. It now
summarizes the labels again when there is a summary, giving the same
contained_labels as reading only that range.
@tompollard

Copy link
Copy Markdown
Member Author

Thanks, fixed in the latest commit, after merging main (with #660) into this branch.

When the annotation has a label summary, apply_range() now calls get_contained_labels() again after keeping the annotations in range, so contained_labels counts only those. For your example, rdann("sample-data/100", "atr", summarize_labels=True) followed by apply_range(sampfrom=600000, sampto=620000) now gives the same contained_labels as rdann(..., sampfrom=600000, sampto=620000, summarize_labels=True), with counts adding up to 71. When no annotations are left, the summary is empty, which relies on the get_contained_labels() fix from #660. The docstring now says so, and I moved the paragraph on which fields are kept above the Parameters section, where it belongs.

New test_apply_range_updates_the_label_summary covers your sequence and a range with no annotations left, comparing with reading the range directly. Both cases fail without the change. rdann calls apply_range() before it summarizes, so its results are unchanged: I compared every field and every contained_labels for the annotation files in sample-data before and after (66 reads and 33 summaries), and they are identical. Offline tests: 500 passed. Network tests: 18 passed. Ruff and the docs build pass.

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