Skip to content

Add validated RecordData selection and value replacement - #908

Merged
tompollard merged 3 commits into
mainfrom
feat/record-data-edits
Dec 4, 2026
Merged

tompollard merged 3 commits into
mainfrom
feat/record-data-edits

Conversation

@tompollard

@tompollard tompollard commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Add the first validated edits for RecordData, following the modern read/edit/write direction in #749:

  • select(start=..., stop=..., channels=... or channel_names=...) copies from the current loaded arrays, updates selected metadata and source coordinates, and clips/rebases flattened source segments. It recomputes initial values and checksums where calibration permits. It works for uniform and expanded physical/digital reads.
  • with_values(values, *, missing=...) copies replacement arrays of the same kind, layout, frame count, and channels. Physical gaps use NaN; digital replacements must carry a mask or explicitly declare missing="none" for plain arrays. Values are checked immediately against known channel formats; ambiguous target formats remain a write-time choice.
  • with_values clears sample-dependent initial values and checksums. RecordData.write() derives them for results of either method. Unedited read/write output retains its previous behavior and byte fixtures. Source provenance identifies where data came from, even when current samples have been edited.
  • Converted results retain the private read model so they can also be edited. The v5.17.0 pickle fixtures remain editable through a compatibility path without inventing lost provenance.

This PR leaves channel metadata correction and time shifting for the next focused edits. It does not change read_record or write_record signatures.

Checks

  • 30 new edit tests cover both kinds/layouts, source selection, flattened segment offsets, mask copying, validation, conversion, raw skew, legacy pickles, and edited writes. Selection is compared with a direct read for values, masks, metadata, provenance, conversions, and written bytes, including physical multi-segment metadata and fixed-layout header provenance.
  • Full offline suite: 2,133 passed, 4 skipped.
  • Ruff and warnings-as-errors Sphinx build pass.

@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.

select and with_values validate well, and nothing about existing reads changes. Against a direct read of the same window, select matches in 1,118 of 1,163 randomised cases. The other 45 come from three small differences, listed below. I'd settle them, or document them, before this sets the pattern for the next edits. None is a data error. The offline suite (2,131 passed, 4 skipped), Ruff and CI pass.

What I checked

  • Existing behaviour against main (239206d). Conversions now keep the private model rather than going through the Record adapter, so I re-ran my two comparisons: 3,520 single-segment calls and 976 multi-segment calls, covering values, masks, metadata, provenance, conversions, to_legacy_record(), pickle and written bytes, plus in-place edits. There are zero differences.

  • select against a direct read. For every sample record except p000878 (whose segments aren't in sample-data), with both kinds, both layouts and both alignments, I:

    1. read a random window;
    2. selected a random sub-window and a random subset and order of channels;
    3. read the corresponding window and source channels directly;
    4. compared values, masks, metadata, all four source views, to_physical/to_digital, and written bytes.

    I also checked a nested select of a select. That's 1,163 comparisons in total.

  • with_values validation. Out-of-range values, unmasked missing markers, float arrays for digital data, wrong shapes, and plain integers without missing="none" each fail immediately with a clear message. Replacing values with the same values writes the same .dat bytes in 56 of 56 cases, across seven records, both kinds, both layouts, and full and partial windows.

Where select differs from a direct read

  1. Multi-segment physical data gets checksums that a direct read doesn't have (30 cases). For a flattened multi-segment physical read, the reader leaves init_value and checksum as None. select derives them from the physical values through ADC, so read_signals(p, start=a, stop=b).metadata and read_signals(p).select(start=a, stop=b).metadata disagree for 041s and s00001. Their conversions differ in the same way. The written bytes match, because the writer derives these fields anyway. Since the aim is that selecting equals reading, I'd leave them None where the read would, meaning physical data assembled from segments.
  2. source_signal_header for fixed layout (6 cases). Selecting a window that starts in a later segment keeps the original read's header (041s01), while reading that window directly reports the first segment it loads (041s02). Fixed-layout segments define the same channels, so both are valid. But one should be chosen. Recomputing it from the clipped source_segments would match the reader.
  3. Checksums written two ways (3 write cases, plus the matching metadata). When the selection covers a whole record, a direct read keeps the header's signed checksum (for example -59), while select recomputes it modulo 65536 (65477). The values are equivalent, and the unsigned form is the library's existing convention for recomputed checksums: 5.17.0's wrsamp writes it for any partial record. with_values likewise writes 43405 where an unedited full read of sample-data/100 writes the header's -22131. I'd accept this, but a sentence in the docs would save someone comparing headers from wondering.

Not this PR: format 8 windows decode incorrectly

The remaining 10 differences were all binformats channel 0, and they led to a real bug that's on main and in 5.17.0. Format 8 stores differences between consecutive samples, and a window that starts after frame 0 restarts the running sum at the window start instead of carrying it from frame 0. For binformats, frame 477 of channel 0 is 570 in a full read, but -2175 from read_signals(..., start=477), from rdrecord(..., sampfrom=477), and on 5.17.0. My comparison caught it because both sides were partial reads starting at different frames, so they were wrong by different amounts. A select from a full read gives the correct values. This needs its own issue and fix in the decoder.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for the broad comparison. I addressed the two provenance and metadata differences in f001edf:

  • Selecting flattened physical multi-segment data now leaves init_value and checksum absent, matching a direct read and its conversions.
  • For fixed-layout data, source_signal_header now follows the first retained data segment. The read model keeps the parsed segment headers so a nested selection can make the same choice without rereading files.

I added regression tests for both. I kept the signed-versus-modulo-equivalent checksum behavior and documented it in docs/io.rst; recomputed checksums use the unsigned modulo-65536 form. The full offline suite passes (2,133 passed, 4 skipped), as do Ruff and the warnings-as-errors docs build.

I reproduced the format-8 window difference (570 versus -2175 at frame 477 of binformats). It is tracked separately in #909 because it is in the shared decoder and predates this PR.

@tompollard
tompollard merged commit e2ffc00 into main Dec 4, 2026
19 checks passed
@tompollard
tompollard deleted the feat/record-data-edits branch December 4, 2026 17:17
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