Skip to content

Add n_signals alias for declared record channel count - #805

Merged
tompollard merged 3 commits into
mainfrom
feature/declared-signal-count
Nov 30, 2026
Merged

tompollard merged 3 commits into
mainfrom
feature/declared-signal-count

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

Add n_signals as a mutable alias for the existing n_sig field on Record and MultiRecord. Both constructors accept the new keyword and reject simultaneous values for both spellings. The alias adds no second stored count and does not change existing read or write behavior.

n_signals means the declared number of channels in the current record view. It is available on header-only records and is not inferred from whichever signal array happens to be loaded. Editing it does not resize arrays or per-channel fields; the writers now reject count mismatches before writing. Record.metadata.n_signals remains a snapshot of the declared count.

This is a small naming step for #672 and addresses the count-name question in #744. The broader per-channel metadata design remains open.

Verification

  • Full offline suite: 1,373 passed, 4 skipped, 22 deselected.
  • Ruff format and lint passed.
  • Strict Sphinx HTML build 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. The alias itself works: one stored value, both constructor spellings rejected together, and it survives copying and pickling. Since #803 and #804, an edited count on a single-segment Record is caught at write time. The offline suite, Ruff and the strict docs build pass locally.

Two things I'd fix first:

  • MultiRecord writes aren't checked. The docs say "the writers reject inconsistencies", but on 041s read with combine_segments=False, setting n_signals = 9 (7 signals) and calling wrsamp writes 041s/1 9 125 10 .... Reading that back fails with ValueError: Input channels must all be lower than the total number of channels, and wfdb check reports "No WFDB problems found". Could MultiRecord.wrsamp check the declared count against its segments, or the docs limit the claim to single-segment writers? The wfdb check gap probably deserves its own issue.
  • The new name accepts any value. record.n_signals = "3", -1, True and 2.5 are all stored, and Record(n_signals="2") works. Since this is a new name, could the setter and constructor argument raise TypeError for non-integers (including bool) and ValueError for negatives, leaving n_sig itself unchanged?

On the design: this settles #744's question of which count is authoritative as a declared count checked at write time, which makes sense for header-only and multi-segment records. It would be good to record that decision on #744, since a mismatch is still visible in memory before writing. After slicing p_signal to one column, n_signals, metadata.n_signals and len(metadata.signals) all still say 2. Minor wording: metadata.n_signals is recalculated from the declared count on each access rather than being a snapshot.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, these were good catches. I pushed d233d19 to address both blocking points:

  • Assigning through n_signals, including in either constructor, now accepts only non-negative integers or None. It rejects booleans, strings, and floats with TypeError, and negative integers with ValueError. Direct assignment to the legacy n_sig field is unchanged.
  • MultiRecord.wrsamp() now checks the master count against each fixed-layout segment or the variable-layout header before writing. I added tests for both layouts and confirmed the mismatch leaves no header behind.

I corrected the docs to say that each Record.metadata access creates a fresh snapshot, and recorded the declared-count decision on #744. I also reproduced the wfdb check gap and opened #806 for it.

The full offline suite passes (1,387 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, d233d19 covers both points. Setting n_signals = 9 on 041s (fixed) or p000878 (variable) now fails before writing, naming the real count (7 or 4), with no files left behind. I compared main and this branch on eight MultiRecord.wrsamp cases (full reads, channel subsets and reordering, variable-layout name selection with and without force_channels, and read_record(..., combine_segments=False)), and all behave the same and read back correctly, so the new check doesn't affect valid writes. The setter and constructor now reject strings, booleans (including np.True_), floats and negatives, while accepting np.int64 and None, and n_sig keeps its legacy behaviour. The decision note on #744 and #806 for the checker gap look good. The offline suite, Ruff and the strict docs build pass locally.

One small nit: the new MultiRecord messages say "n_sig must match ...", even when the count was set through n_signals. Mentioning both names would help, but it's fine as is.

Ready to merge once CI is green.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for checking the fixed- and variable-layout cases. I took the wording suggestion in 33276b2: both MultiRecord count errors now say n_sig (n_signals), so the message is clear whichever spelling was used. The 23 focused tests and Ruff pass; the full offline suite and strict docs build passed on the preceding revision.

@tompollard
tompollard merged commit 0d132db into main Nov 30, 2026
19 checks passed
@tompollard
tompollard deleted the feature/declared-signal-count branch November 30, 2026 20:19
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