Repository navigation
Add validated RecordData selection and value replacement - #908
Conversation
tompollard
left a comment
There was a problem hiding this comment.
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 theRecordadapter, 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. -
selectagainst a direct read. For every sample record exceptp000878(whose segments aren't insample-data), with both kinds, both layouts and both alignments, I:- read a random window;
- selected a random sub-window and a random subset and order of channels;
- read the corresponding window and source channels directly;
- compared values, masks, metadata, all four source views,
to_physical/to_digital, and written bytes.
I also checked a nested
selectof aselect. That's 1,163 comparisons in total. -
with_valuesvalidation. Out-of-range values, unmasked missing markers, float arrays for digital data, wrong shapes, and plain integers withoutmissing="none"each fail immediately with a clear message. Replacing values with the same values writes the same.datbytes in 56 of 56 cases, across seven records, both kinds, both layouts, and full and partial windows.
Where select differs from a direct read
- 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_valueandchecksumasNone.selectderives them from the physical values through ADC, soread_signals(p, start=a, stop=b).metadataandread_signals(p).select(start=a, stop=b).metadatadisagree for041sands00001. 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 themNonewhere the read would, meaning physical data assembled from segments. source_signal_headerfor 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 clippedsource_segmentswould match the reader.- 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), whileselectrecomputes it modulo 65536 (65477). The values are equivalent, and the unsigned form is the library's existing convention for recomputed checksums: 5.17.0'swrsampwrites it for any partial record.with_valueslikewise writes43405where an unedited full read ofsample-data/100writes 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.
|
Thanks for the broad comparison. I addressed the two provenance and metadata differences in f001edf:
I added regression tests for both. I kept the signed-versus-modulo-equivalent checksum behavior and documented it in I reproduced the format-8 window difference ( |
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 declaremissing="none"for plain arrays. Values are checked immediately against known channel formats; ambiguous target formats remain a write-time choice.with_valuesclears 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.This PR leaves channel metadata correction and time shifting for the next focused edits. It does not change
read_recordorwrite_recordsignatures.Checks