Skip to content

Decouple I/O internals from dynamic Record fields #749

Backlog

Description

@tompollard

Problem

The header field tables, parser, validation, conversion, and writer access record fields dynamically. Parsing assigns fields with setattr, and Record and Annotation equality inspect instance dictionaries. Introducing nested metadata or signal objects can therefore change equality, serialization, and legacy field edits even if read/write results look the same.

Decisions to make

  • What internal schema should own record, signal, and segment fields during the transition?
  • Which legacy mutations and object serialization behaviors must be preserved, including in-place edits to list fields?
  • How should parsing, validation, and writing consume the new schema while keeping warnings out of internal calls?
  • How should tests distinguish structural equality from equivalent signal data produced through different read paths?

Related: #371 (metadata model), #376 (signal model), #672 (compatibility and names), and the design review on #417. This supports #743, #744, #745, #746, #747, and #748.

Activity

  1. tompollard commented on Dec 3, 2026

    @tompollard
    MemberAuthor

    Proposed read-model direction after #866

    The writer refactor now has a private model with no Record encoder inheritance. It converts a mutable Record from its current fields at the write boundary, and the 5.17.0 byte fixtures, independent comparisons on #898, and the 30-run benchmark on #866 support that boundary. The remaining dependency is on the read side: read_signals still calls read_record, then wraps the returned Record; SignalRead retains a hidden _record for conversion and provenance.

    My recommendation is:

    1. Make one private selected-read model. Build it from the parsed header, selected channels/frames, and decoded arrays. It should own the selected channel metadata, calibration, kind, layout, missing-sample representation, alignment, and immutable source provenance. SignalRead conversion and writing should use those fields directly, with no retained Record. A legacy Record can be projected at one compatibility boundary when requested. When a mutable Record is written, continue converting its current fields at write time; never rely on a model saved when it was read.
    2. Use SignalRead as the one modern selected-read result. It already represents exactly one kind and layout and carries masked digital values. Once the private reader model proves it can preserve the existing cases, I would have combined read_record reads return that same result rather than introduce another selected-result class. rdrecord should keep returning mutable Record/MultiRecord. A separate segmented reader should handle uncombined multi-segment access before changing read_record(combine_segments=False); that is a different shape of data. This is a public return-type change, so it needs its own PR and clear migration examples.
    3. Keep Record as the mutable compatibility view. Its public list edits, equality/serialization behavior, and methods remain. Do not put a second mutable metadata object beside those fields. The modern model can be immutable at the metadata level while the signal arrays remain editable; skewed writes still require an explicit signal_alignment because an editable array does not prove its history.

    I would start with a private reader-model PR that changes no public return type. It should cover single and multi-segment reads, channel selection/reordering, variable layouts, expanded and uniform values, missing samples, skew, conversions, copy/pickle, provenance, and source-header defaults. Check selected values and masks against current read_record/read_signals, and measure allocations so the reader does not add a full array copy. Only then switch the public result, with the migration guide and tests in that PR. The channel ownership details are in #744.

  2. tompollard commented on Dec 3, 2026

    @tompollard
    MemberAuthor

    I want to correct one part of my proposal above. Changing only combined read_record calls to return SignalRead would leave its return type dependent on combine_segments, and would make read_record and read_signals largely redundant.

    I would keep distinct roles instead:

    • read_record returns a mutable Record for callers who need to edit its fields or values and write it again. It has modern source and selection arguments, but the result is deliberately the mutable compatibility object.
    • read_signals returns SignalRead: one selected kind and layout, immutable metadata, and editable signal arrays.
    • Uncombined segment access should move to a separate reader. Once that exists, remove combine_segments=False from the modern read_record API so its return type is stable. Keep rdrecord's legacy Record/MultiRecord behavior.

    That gives the two public views a reason to exist without adding a third selected-result type. It does not change the private-model goal: read_signals should build its result from the shared private read model, without retaining a hidden Record; read_record should project a mutable Record from the model when requested. Legacy writes still convert the Record's current fields at write time.

    For the first PR I would keep public returns unchanged and compare the old and new read paths temporarily. The checks need header-only reads, index and name-based channel selection (including missing_channels), converted views, copy/pickle, variable-layout segments, masks, skew, and allocations on multi-segment flattening. I would not switch the public return contract in that PR.

  3. tompollard commented on Dec 3, 2026

    @tompollard
    MemberAuthor

    I agree we should choose the best modern API first, then plan the migration. I am revising my earlier suggestion to keep both modern readers.

    • read_record should be the one modern reader for a selected record. It should always return the same result type. I suggest RecordData for that class: it contains signal values, channel and timing metadata, and source provenance. read_signals should be retired because it currently returns essentially that same record view under a narrower name.
    • write_record should accept RecordData, so the pair is obvious. Uncombined segment access belongs in a separate reader, rather than making read_record's type depend on combine_segments.
    • rdrecord and mutable Record remain the compatibility path. Modern metadata and structure changes should use validated operations that return a new RecordData, such as channel selection, metadata correction, and time shifting. Writing from new arrays is already covered by write_signals.

    Two constraints matter for the edit API. The signal arrays can remain editable, but direct NumPy edits can still change their shape, masks, or alignment without going through our methods. write_record must therefore keep preflight checks for array shape, missing samples, range, and explicit alignment where skew requires it; it cannot blindly trust a RecordData. Also, gain and baseline are not merely file-format choices for digital values: they define the values' physical meaning. A calibration-changing operation must say whether it rescales digital samples or reinterprets them. For physical values, choosing gain/baseline at write time can be an encoding choice.

    The first PR can stay private and preserve all public returns. It should build and compare the selected read model, reuse or map directly to the existing ChannelMetadata classes, and measure allocations on flattened multi-segment reads. We can make the public naming/return change and the validated edit methods in later focused PRs, with migration notes before release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions