Skip to content

Reject new skew when writing aligned signals - #842

Merged
tompollard merged 2 commits into
mainfrom
feature/reject-edited-applied-skew
Dec 2, 2026
Merged

tompollard merged 2 commits into
mainfrom
feature/reject-edited-applied-skew

Conversation

@tompollard

@tompollard tompollard commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Problem

Writing a record whose source skew was already applied can produce an unreadable file after a partial skew edit. I reproduced this with read_record("sample-data/100skew", stop=12, kind="digital"), setting skew[0] = 1, then calling Record.write(): writing succeeds, but reading the result raises a NumPy broadcasting error. wrsamp() previously omitted skew only when the entire current list matched _applied_skew.

Change

Both Record.write() and Record.wrsamp() now reject a new nonzero skew on already aligned values before creating files. Retained or cleared source skew still writes aligned values without skew in the output header. Manually reducing a record to channels with no remaining skew is allowed; a remaining nonzero skew with no reliable mapping to the original channels gets a clear error suggesting read_record(..., channels=[...]). Reads made with ignore_skew=True retain their existing behavior, and header-only wrheader() still writes the declared skew.

The previous test that expected a newly assigned skew in the header is updated to check the early error. New tests cover Record.write(), no partial files, a round trip after clearing one channel's skew, and both safe and unsafe manual channel removal. The change has a changelog entry.

Verification

Full offline suite: 1,627 passed, 4 skipped, 23 deselected. Ruff and the strict Sphinx build pass.

Refs #744.

@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 the reported case and a quieter one: on main, changing 100skew's channel 1 skew from 3 to 2 writes a readable file whose channel 1 is shifted by 2; here both that and the skew[0] = 1 case are rejected before any file is written. Retained and cleared source skew and channels=[1] reads write as before. The offline suite, Ruff and the strict docs build pass locally.

Two things I'd change:

  • Manual channel removal. Dropping channel 1 from d_signal and every per-channel list (with n_sig=1) writes correctly on main (skew [None]), but now raises "skew and applied source skew have different channel counts". The existing channel-count error says updating n_sig and per-channel fields together is supported. When the lengths differ, could _skew_for_write accept the write if every remaining skew is None or 0, and otherwise raise an error pointing to read_record(..., channels=[...])?
  • Error message. "provide unaligned signal values" doesn't help: replacing d_signal with the values from an ignore_skew=True read still raises, because _applied_skew is still set. Suggesting ignore_skew=True when reading would point to the route that works.

Possible follow-ups, both already on main: putting unaligned values into an aligned record while keeping its skew writes without skew, so the data is misaligned; and setting a new skew after an ignore_skew=True read still writes a file that fails to read (could not broadcast input array from shape (14,) into shape (12,)).

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, I addressed both points in 0e62fab.

  • A manually reduced record now writes when every remaining skew is None or 0, even if _applied_skew still has the original channel count. I added a round-trip test for that case.
  • A nonzero skew with no reliable mapping to the original channels still fails before writing, with an error suggesting read_record(..., channels=[...]). The changed-skew error now points to ignore_skew=True for preserving source skew.

I added tests for both paths. The full offline suite passes (1,627 passed, 4 skipped), as do Ruff and the strict docs build.

I agree the two remaining provenance cases are real. I filed them as #843 so we can define the array-replacement and new-skew behavior separately from this fix.

@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, 0e62fab covers both. A record reduced by hand to channel 0 now writes without skew through both write_record and wrsamp, a remaining nonzero skew gets the clearer read_record(..., channels=[...]) error, and the changed-skew message points to ignore_skew=True, which preserves [None, 3] and reads back correctly. Thanks for filing #843 for the remaining cases. The offline suite, Ruff and the strict docs build pass locally.

Ready to merge once CI is green.

@tompollard
tompollard merged commit fe1d88c into main Dec 2, 2026
19 checks passed
@tompollard
tompollard deleted the feature/reject-edited-applied-skew branch December 2, 2026 02:56
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