Skip to content

Validate record channel counts before writing - #803

Merged
tompollard merged 2 commits into
mainfrom
fix/record-write-count-validation
Nov 30, 2026
Merged

tompollard merged 2 commits into
mainfrom
fix/record-write-count-validation

Conversation

@tompollard

@tompollard tompollard commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Validate the selected signal array, a stated n_sig, and retained per-channel header fields before write_record or Record.write prepares the output. A mismatch now raises a ValueError listing every field with the wrong count before files are created, with guidance to use read_record(..., channels=[...]) for subsets. byte_offset, init_value, and checksum are excluded because the writer clears or regenerates them; unchanged source skew already applied to the values is excluded too.

This addresses the two concrete failures discussed in #744: a manually edited n_sig was silently replaced by the array width, and slicing an array without updating the count produced a NumPy broadcasting error during writing. The validation applies to the selected physical or digital, uniform or expanded form. When n_sig is unset, the writer fills it from that form on its copy of the record; the caller's object remains unchanged.

This is a focused part of #744. The broader decision about a derived public n_signals value and Record.metadata remains there.

Validation

  • Full offline suite: 1,357 passed, 4 skipped, 22 deselected
  • Ruff check and format check: 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, this fixes both cases from #744. An edited n_sig now fails clearly before any file is written (for all four forms), a sliced p_signal gets a readable error instead of the broadcasting failure, only the selected form is checked, and an unset n_sig is still filled on the copy. The offline suite, Ruff and the strict docs build pass locally.

Two things I'd change before merging, both about writing a subset of channels from a loaded record:

  • Fields the writer replaces are still checked. The check covers all 14 per-channel fields, but the writer recalculates checksum and init_value (edited values came back as 3980, 4044 and 995, 1011) and clears byte_offset. So a record can be rejected over values that never reach the file. Could those three be skipped, or the check run on the prepared copy?
  • One mismatch is reported at a time. On sample-data/100, after slicing p_signal to one column and trimming n_sig, sig_name, units, fmt, adc_gain, baseline and file_name, write still failed eight more times, once each for samps_per_frame, skew, byte_offset, adc_res, adc_zero, init_value, checksum and block_size. Listing every mismatched field in one message would help, and so would pointing to read_record(..., channels=[...]) for subsets.

Optional: a non-integer n_sig (e.g. "2") raises ValueError, while the new readers use TypeError for wrong types. Also, the legacy Record.wrsamp still hits the broadcasting error on a sliced array. That's fine to leave here, but it may be worth noting on #744.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, fixed in 30e4305. The early check now skips byte_offset, init_value, and checksum, because the writer clears or regenerates them. It also skips unchanged source skew when that skew was already applied to the loaded values and will be omitted from the output header. A test confirms that stale values for the first three no longer block a valid write and that the written header gets the regenerated values.

The error now lists every retained field with the wrong channel count in one message and points to read_record(..., channels=[...]) for a subset. I added a test that reproduces the sequence of mismatches you found. A non-integer n_sig now raises TypeError.

I reproduced the legacy Record.wrsamp() broadcasting error and noted it on #744 for a separate compatibility review. The revised branch passes 1,357 offline tests, Ruff, and the strict docs build.

@tompollard
tompollard merged commit bbff5f3 into main Nov 30, 2026
19 checks passed
@tompollard
tompollard deleted the fix/record-write-count-validation branch November 30, 2026 19: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