Skip to content

Build multi-segment signal reads from a private model - #900

Open
tompollard wants to merge 2 commits into
mainfrom
refactor/private-multisegment-model
Open

tompollard wants to merge 2 commits into
mainfrom
refactor/private-multisegment-model

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

This is step 1b of the reader-model work in #744 and #749. read_signals now decodes and flattens fixed- and variable-layout multi-segment records from parsed headers and sample arrays. A normal read no longer constructs segment Record objects, a MultiRecord, or a flattened Record. The public SignalRead result is unchanged; conversion and writing still create a compatibility Record lazily from the current values.

The assembler preserves channel selection, per-segment missing-sample masks and storage formats, segment provenance, source headers, and the legacy rule for inconsistent calibration across variable-layout segments. It also avoids reading the master header twice. Zero-length single-segment headers still use the retained fallback; removing that temporary path is step 1c.

This PR includes a changelog entry for the now-complete pickle provenance fix: restored SignalRead objects retain source header and selection information for both single- and multi-segment reads.

Evidence

  • Cross-version comparison against merged main (Build single-segment signal reads from a private model #899): 960 multi-segment read cases, zero differences. Cases cover windows at the first ten segment boundaries, gaps, channel order and name selection, both kinds and layouts, resolutions 16 and 64, and both force_channels values. Values, masks, dtypes, metadata, source provenance, conversions, copy, deepcopy, pickle results, and error outcomes were compared.
  • 48 write cases after multi-segment reads produced identical file bytes or identical errors on main and this branch. A separate 16-case comparison of every field on to_legacy_record() also matched.
  • In-repo tests compare the direct path with the retained Record path for boundaries, variable layout, skewed segments, archives, conversion, masks, and provenance. A test asserts that ordinary multi-segment reads do not call the Record constructor path.
  • Offline suite: 1,988 passed, 4 skipped, 23 deselected. Ruff check/format and the Sphinx warnings-as-errors build pass.
  • Benchmark: sample-data/multi-segment/fixed1/v102s, frames 50,000–125,000, 30 measured runs after three warmups, macOS 15.7.3 arm64, Python 3.11.13, NumPy 2.4.6. Median read time versus main was 4.8–13.6% lower across physical/digital and uniform/expanded modes. Peak traced Python allocation was unchanged for physical reads and 0.14–0.24 MiB higher for digital masks, with no extra full sample-array copy.

Review focus

Please check the variable-layout field reconciliation and the lazy compatibility projection, especially writes after editing masked digital values. The cross-version scripts and benchmark are one-off review aids; the durable parity cases are in tests/test_multi_read_model.py.

@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 multi-segment assembler matches main on every sample record, with the pickle fix now covering both paths. It also fixes something the description doesn't mention: on main, read_signals can't read a fixed-layout record that has a null (~) segment. That fix needs one edge case handled, plus a test and a changelog entry. The offline suite (1,988 passed, 4 skipped) and Ruff pass.

What I checked, against main (2151bdc, with #899)

I ran the same script on both versions, covering all six multi-segment records in sample-data, fixed and variable layout:

  • windows across the first twelve segment boundaries;
  • random windows, full reads where practical, and an empty window;
  • reversed channels;
  • channel_names with missing names, with and without force_channels=False;
  • channels=[0] with force_channels=False;
  • resolution 16;
  • both kinds and both layouts.

That gave 976 calls: 614 reads and 362 expected errors. The errors include the variable-layout digital calibration error and the p000878 segments missing from sample-data. For each read I compared:

  • values, masks and dtypes;
  • metadata;
  • the source headers, selection and segments;
  • conversions and every attribute of to_legacy_record();
  • pickle and copies;
  • written bytes.

I repeated this after editing the arrays in place.

Everything matches except:

  • Pickled provenance now survives on both paths, as the changelog entry says.
  • An empty selection. With force_channels=False, a window where none of the requested channels exist (for example p000878, frames 0–5) has to_legacy_record() returning checksum and init_value as None instead of []. The written bytes are identical. It's harmless, but I mention it because the description says the projection is identical.

To fix: fixed-layout records with a null segment

I built small fixed-layout records with a format 16 segment and a ~ gap, in both orders. On main, almost every read fails with an internal error such as AttributeError: 'NoneType' object has no attribute 'd_signal' or ... 'fmt'. The only exception is a window that avoids the gap entirely. Here they read correctly:

  • the gap is NaN (physical) or masked (digital);
  • the source segments include the ~ entry;
  • to_physical, to_digital, to_legacy_record and write all work;
  • a write and read round trip returns the same values and masks.

That's a real fix, but two things are missing:

  1. A window entirely inside the gap raises a bare StopIteration. For example, start=600, stop=700 on a record whose second segment is ~. _reference_signals calls next(...) without a default when no segment model was loaded. The master already reads the first non-gap segment's header into signal_header, before the loop. Using its signals as the reference when nothing is loaded would return an all-missing view, consistent with how the gap is handled when the window only partly overlaps. A fixed record made entirely of ~ segments hits the same unguarded next(...) at the start. That one probably deserves a clear ValueError.

  2. A test and a changelog entry. Please add a fixed-layout gap case to test_multi_read_model.py covering:

    • a window across the gap;
    • a window inside the gap;
    • a write round trip.

    And add a changelog entry, since read_signals users on 5.17 couldn't read these records.

Already on main, for the follow-up list

A format 8 channel has no missing-sample marker. A gap segment in a format 8 multi-segment record therefore fails digital reads with TypeError: int() argument ... 'NoneType', on main and here, in both layouts. fill takes _digi_nan(fmt) and gets None. It belongs with the expanded 999999 issue from #899: both need one rule for missing samples in formats without a marker.

For later, not this PR

To keep parity, the assembler reproduces some legacy behaviour in a modern reader:

  • raise IndexError("list index out of range") for an empty expanded selection;
  • a print when no requested channel is present;
  • a bare Exception for the variable-layout digital calibration error.

That's the right choice while the comparison path still exists. Once 1c removes it, these should become proper ValueErrors, with a changelog entry, before RecordData makes this the main reader.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for the detailed check. I pushed d80c36e to address the fixed-layout gap case.

When a selected window falls entirely inside a ~ segment, the reader now uses the first signal-bearing segment's header to define the channels. A master containing only ~ segments raises a clear ValueError. I added tests for gaps in both positions and both layouts, including windows across and inside the gap, physical NaNs, digital masks, conversions, provenance, and a write/read round trip. There is also a changelog entry for the newly supported reads.

I fixed the empty-selection difference too: to_legacy_record() retains empty checksum and init_value lists when force_channels=False removes every channel.

I agree the format 8 gap needs a separate missing-value rule alongside #899. I'll leave the legacy-shaped errors and print in place for parity here, then clean them up after 1c and before RecordData becomes the main reader.

Locally, the offline suite passes (1,994 passed, 4 skipped), as do Ruff and the Sphinx build with warnings treated as errors.

This branch has not been deployed

No deployments
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