Skip to content

Validate selected channels before legacy record writes - #804

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

tompollard merged 2 commits into
mainfrom
fix/legacy-write-channel-validation

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

Validate the signal form selected by Record.wrsamp() against n_sig and retained per-channel header fields before ADC conversion or file creation. This replaces the NumPy broadcasting error after slicing a physical signal array with an actionable ValueError that names the mismatched fields.

The legacy writer continues to prefer digital values when both forms are loaded. It still recalculates checksum; unlike write_record(), it retains byte_offset and init_value, so those fields are checked. This addresses the legacy writer example noted in #744 without closing that broader issue.

Verification

  • Offline suite: 1,363 passed, 4 skipped, 22 deselected.
  • Ruff format and lint passed.
  • Sphinx HTML build passed with warnings treated as errors.

@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. I compared main and this branch on 15 Record.wrsamp cases: edited n_sig, short or long sig_name, units, skew, init_value, byte_offset, checksum, adc_res, block_size and samps_per_frame, a sliced p_signal, a hand-built record, and the module-level wfdb.wrsamp. Everything this PR rejects was already rejected on main, often as IndexError: list index out of range or the broadcasting error. The cases that wrote before (a stale checksum, the module-level function) still do. So this improves errors without breaking legacy behaviour. Checking only the form wrsamp will write, and ignoring only checksum here while byte_offset and init_value are kept, both look right. The offline suite, Ruff and the strict docs build pass locally.

Two optional notes:

  • With n_sig unset, wrsamp still fails with TypeError: '>' not supported between instances of 'NoneType' and 'int' (and a hand-built Record(p_signal=..., sig_name=..., ...) fails with a NumPy UFuncTypeError), as on main. write_record infers n_sig from the array, and doing the same here, or raising a clear message, would cover the last confusing case. It only affects calls that fail today, so a follow-up is fine.
  • Since some failures change from IndexError to ValueError, a short note in the changelog might help anyone catching the old type.

Ready to merge once CI is green.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for checking the legacy cases. I addressed both notes in 59080b6. Record.wrsamp() now raises a clear ValueError when n_sig is unset, before conversion or file creation, and the changelog explicitly notes that some mismatches now raise ValueError instead of IndexError. I kept n_sig explicit in the legacy method; write_record() remains the option that infers it from the selected array.

I added a regression test for a hand-built record without n_sig. The full offline suite passes (1,364 passed), as do Ruff and the strict docs build.

@tompollard
tompollard merged commit 71d9cf5 into main Nov 30, 2026
19 checks passed
@tompollard
tompollard deleted the fix/legacy-write-channel-validation branch November 30, 2026 20:06
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