Skip to content

Allow writing annotation files with no events - #796

Merged
tompollard merged 1 commit into
mainfrom
fix/empty-annotations
Nov 30, 2026
Merged

tompollard merged 1 commit into
mainfrom
fix/empty-annotations

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

An empty result from merge_annotations could not be saved because the annotation writer required at least one event. This PR allows zero-event annotation files in the shared writer used by write_annotations, Annotation.write, and Annotation.wrann.

Empty files can still carry sampling frequency and custom label definitions. Event fields are validated for consistent lengths before writing, and the result can be read back with read_annotations.

Verification

  • Full offline suite: 1216 passed, 4 skipped, 22 deselected.
  • Ruff check and format check passed.
  • Sphinx HTML build with warnings as errors passed.

@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, this looks good. An empty annotation with no fs writes just the end marker (00 00). With fs or custom labels, it writes the usual header followed by the end marker, the same bytes the writer already produces for non-empty files. Both read back correctly with read_annotations and rdann, including with start/stop and summarize_labels=True. Empty float arrays, all-empty event fields and an empty merge_annotations result written with wrann also work. A non-empty annotation without a label field, and an empty sample with a non-empty symbol, are still rejected. The offline suite, Ruff and the strict docs build pass locally.

Not for this PR, but related: the new write path still passes on some old error styles. A non-empty annotation with no label field raises a bare Exception with a tuple message, and sample=[] (a list) raises TypeError: ('The sample field must be one of the following types:', (<class 'numpy.ndarray'>,)). It would be good for write_annotations and Annotation.write to raise ValueError or TypeError with plain messages, as the new readers now do. I'm happy to open an issue for that.

Ready to merge once CI is green.

@tompollard

Copy link
Copy Markdown
Member Author

Agreed. I reproduced the two tuple-valued errors and opened #797 to improve validation messages in write_annotations and Annotation.write. I’ve kept #796 focused on writing valid empty annotations. The checks that have finished are green; the macOS matrix is still running.

@tompollard
tompollard merged commit 220a6c1 into main Nov 30, 2026
19 checks passed
@tompollard
tompollard deleted the fix/empty-annotations branch November 30, 2026 17:58
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