Skip to content

Require explicit alignment for skewed modern writes - #851

Merged
tompollard merged 2 commits into
mainfrom
fix/require-explicit-skew-alignment
Dec 2, 2026
Merged

tompollard merged 2 commits into
mainfrom
fix/require-explicit-skew-alignment

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

Require signal_alignment="aligned" or "raw" for write_record, Record.write, and SignalRead.write when a selected channel has nonzero skew. The error explains the two choices and is raised before files are created. Records without skew need no choice, and legacy wrsamp is unchanged.

This deliberately changes behavior shipped in 5.14.0. SignalRead is a frozen wrapper, but its NumPy arrays can be changed in place, so its writer cannot safely infer alignment from the original read. The option applies to one write and does not shift samples.

The docs and changelog describe the contract. Tests cover aligned and raw reads, selected channels, uniform and expanded layouts, physical and digital values, in-place replacement, hand-built records, and copy/deepcopy/pickle.

Closes #843.

Verification

  • Offline suite: 1,715 passed, 4 skipped (bxb unavailable), 23 network tests 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.

The contract works as described. Apart from one docs gap below, this is ready to merge.

What I checked

  • I wrote every record in sample-data through write_record and SignalRead.write on this branch and on main: digital, uniform and expanded, aligned and ignore_skew=True (640 writes). For unskewed records the PR passed no option. For the 32 skewed writes (100skew and 03700181), it passed the option matching the read: "aligned" for aligned reads, "raw" for ignore_skew=True. Every successful write is byte-identical to main, so choosing the option that matches the read reproduces the 5.14.0 output.
  • The writes that fail on main fail in the same way here. One variation isn't from this PR: n8_evoked_raw_95_F1_R9 is rejected because of an invalid ADC field, and whether the error names adc_zero or adc_res depends on PYTHONHASHSEED, on main too. The field checks presumably iterate over a set. That would be worth a small separate fix so the error is reproducible.
  • The library doesn't call write_record, Record.write or SignalRead.write internally, so no conversion or download path picks up the new requirement.
  • SignalRead.write declares the alignment on to_legacy_record(), which is a deep copy, so the declaration can't outlive the write or change the read result.
  • The offline suite (1,715 passed, 4 skipped), Ruff, the strict docs build and CI all pass, and the towncrier draft renders the entry correctly.

One docs gap to fix

docs/migration.rst maps wrsamp and Record.wrsamp to write_record and Record.write, but doesn't mention the new requirement. Someone migrating a skewed record from wrsamp, which still infers alignment, will hit the new error with nothing in the guide about it. A sentence in that row, or in the write_record differences list further down, would help. For example: "For records with nonzero skew, pass signal_alignment="aligned" or "raw"."

Optional

  • SignalRead.write's docstring body still says "already applied skew is not applied again" without mentioning that the choice is now required. Only the parameter description says so.
  • The error could name the skewed channels (for example, "channel 1 (V5) has skew 3"). This would help when a record has many channels and the skew wasn't expected.

@tompollard

Copy link
Copy Markdown
Member Author

I added the missing migration note: skewed records written through write_record or Record.write now need signal_alignment="aligned" or "raw", while wrsamp still infers it. I also made the requirement explicit in SignalRead.write's docstring body.

The error now names each skewed channel and its value (for example, channel 1 (V5) has skew 3) before any file is created. The focused tests, Ruff, and the warning-strict docs build pass. I filed the unrelated ADC error-order finding as #855.

@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, 65ecc82 covers everything. The migration row now explains that the newer writers need signal_alignment for skewed records while wrsamp still infers it, and SignalRead.write's docstring says the same.

The new error names the skewed channels: "channel 1 (V5) has skew 3" for 100skew, "channel 2 (RESP) has skew 4" for 03700181, and plain "channel 1 has skew 3" when sig_name isn't a list. A test asserts this. Thanks also for filing #855.

The offline suite (1,715 passed, 4 skipped), Ruff and the strict docs build pass locally. CI is green, so this is ready to merge.

@tompollard
tompollard merged commit 69d39e8 into main Dec 2, 2026
19 checks passed
@tompollard
tompollard deleted the fix/require-explicit-skew-alignment branch December 2, 2026 15:39
tompollard added a commit that referenced this pull request Dec 2, 2026
## Summary

Use `signal_alignment="aligned" | "raw"` on `read_record` and
`read_signals` in place of `ignore_skew`. The default remains aligned;
`"raw"` retains the stored skew, equivalent to the old
`ignore_skew=True`. Legacy `rdrecord` keeps its existing parameter.

This deliberately changes the recently released modern reader signatures
so reads and writes use the same words. The migration guide, API docs,
and changelog explain the change. The migration guide also notes that
newer signatures are still being refined before the next major release.

Tests compare aligned and raw results with the legacy reader, reject
invalid values and the removed modern keyword, and update the skew write
tests to use the new read option.

Part of #852. Rebases on `main` after #851 and #853.

## Verification

- Offline suite: 1,715 passed, 4 skipped (`bxb` unavailable), 23 network
tests deselected
- Ruff check and format check: passed
- Sphinx HTML build with warnings as errors: passed
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.

Define skew provenance when signal values or skew are edited before writing

1 participant