Skip to content

Expose grouped current channel metadata - #850

Merged
tompollard merged 3 commits into
mainfrom
feature/grouped-channel-metadata
Dec 2, 2026
Merged

tompollard merged 3 commits into
mainfrom
feature/grouped-channel-metadata

Conversation

@tompollard

@tompollard tompollard commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Add Record.metadata.channels: one immutable entry per current channel, grouping logical fields with current storage declarations.
  • Keep Record.metadata.signals as the existing logical-only view. The new grouped view is None during an inconsistent per-channel list edit; the logical-only view remains available when its own fields are consistent.
  • Take a fresh snapshot on every access, so in-place edits to legacy lists are reflected without maintaining a second mutable owner. The original source header remains available through source_header.
  • Exclude skew from the public storage view because Record.skew does not distinguish raw from already aligned loaded values. storage.samples_per_frame is a current record declaration, not a promise about a uniform array's shape.
  • Use plain Python integers in the new storage type, including for flattened multi-segment records.

This is a first public grouped view for #744 and #749. ChannelMetadata and SignalStorageMetadata are read-only snapshots; they do not replace the mutable legacy attributes. A future internal owner can change without changing this view's contract.

Validation

  • Offline suite: 1,666 passed, 4 skipped, 23 network tests deselected.
  • Ruff lint and format checks passed.
  • Strict Sphinx docs build passed.

@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 grouping works as described. Without edits, metadata.channels[i].storage matches source_header.signals[i] for all 253 single-segment channels in sample-data. Edits are reflected on the next access, and init_value and checksum match the selected values after a range read. The offline suite, Ruff, the strict docs build and CI all pass.

This is the first time these fields become public, and the new class names will be hard to change once released. So I'd settle a few things before merging.

1. storage.skew doesn't say whether the loaded values are already aligned (metadata.py, CurrentStorageMetadata.skew)

On 100skew, an aligned read and an ignore_skew=True read both report [None, 3]:

wfdb.read_record("sample-data/100skew", stop=10).metadata.channels[1].storage.skew                     # 3
wfdb.read_record("sample-data/100skew", stop=10, ignore_skew=True).metadata.channels[1].storage.skew   # 3

For the aligned read, the 3 is the source file's skew, not a property of the values held, and the writer will drop it. That is the legacy record.skew ambiguity, copied into a new public type. #841 dropped skew_applied and kept the storage view internal for this reason ("keeps the public naming and semantics open"). I think this needs to be settled before the view is public. Options, roughly in order of preference:

  • Report the skew of the loaded values, so 0 or None once it has been applied, and leave the source skew to source_header.
  • Keep skew and add a per-channel skew_applied. This needs the per-channel comparison raised in #841, not the whole-list one.
  • Leave skew out of CurrentStorageMetadata for now.

2. The existing docstrings now contradict this view (metadata.py, LogicalSignalMetadata)

LogicalSignalMetadata still says "File names, encoding formats, and source skew belong to the source header instead" and that samples-per-frame "belongs to the loaded arrays". RecordMetadata now carries all of these. A related case: on a uniform read of a record with several samples per frame (03700181 gives [4, 1, 1]), storage.samples_per_frame reports 4 while the only loaded array has one value per frame. That matches "current declarations", but the two docstrings should agree about what the snapshot describes.

3. NumPy integers inside the frozen view

On a flattened multi-segment read (multi-segment/041s), storage.init_value is numpy.int64, although the annotation says int. So json.dumps(dataclasses.asdict(storage)) raises TypeError. Converting the integer fields with int() when building the view would keep the public type honest. Single-segment reads already give plain int.

Smaller points

  • Naming: CurrentStorageMetadata has exactly the storage fields of SignalHeader (file_name, format, samples_per_frame, skew, byte_offset, adc_res, ...). The "Current" prefix is unusual for a class name. Something like SignalStorage, or saying in the docs that it mirrors SignalHeader's fields, would help readers connect the two.
  • The PR says this doesn't settle whether channel objects become authoritative later. If that's still open in #744 and #749, shipping two new public class names now commits to their shape. If you prefer to keep that open, channels could stay private for one more step.
  • Tests: a skewed record (aligned and ignore_skew=True) and a flattened multi-segment read would cover the two cases above.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for checking this across the sample data. I updated #850 in a1d10b2.

I removed skew from the new public storage view. The legacy Record.skew value can remain 3 whether the loaded values are aligned or raw, and I don't want the new type to imply that it describes their alignment. The source skew is available on source_header; tests now cover both read modes. I also clarified in the docstrings and guide that storage.samples_per_frame is the current record declaration, while the loaded array's shape depends on its layout. A uniform read can therefore have one value per frame while the declaration says 4.

I renamed CurrentStorageMetadata to SignalStorageMetadata and documented its relationship to SignalHeader. The new view converts integer fields with operator.index, so flattened records expose plain Python int values; the test serializes the storage dataclass to JSON.

I do think the grouped view is ready to be public as an immutable snapshot. It gives callers a stable way to inspect aligned channel fields without making these objects a second mutable owner. If we later change which internal model owns the fields, we can keep this snapshot contract. The larger ownership decision remains open in #744 and #749.

The full offline suite passes (1,666 passed, 4 skipped), as do Ruff and the strict docs build.

@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, a1d10b2 addresses each point, and the reasoning for keeping the view public as an immutable snapshot makes sense to me. Checked against the new head:

  • skew is gone from SignalStorageMetadata, and the docstrings and guide now agree on what the view describes, including samples_per_frame on uniform reads.
  • Across 244 reads of sample-data (default, digital and expanded), every storage value is a plain int or str, including on the flattened multi-segment records, and channels is never None while signals is available. NumPy integers set by hand (np.int16, np.uint8) also come out as int.
  • A record built from a physical array, with set_d_features(do_adc=True) and then set_defaults(), gets a complete view.
  • The offline suite (1,666 passed, 4 skipped), Ruff and the strict docs build pass locally.

One small, optional thing: because operator.index rejects floats and the TypeError is caught, a whole-number float hides the whole grouped view. For example, record.init_value = [1.0, 2.0] or samps_per_frame = [1.0, 1.0] gives metadata.channels is None, although write_record still writes those records. That seems a reasonable rule for an integer-typed view, but the docs only mention inconsistent lists as a reason for None. Adding "or a storage field is not an integer" would explain it.

Ready to merge once CI is green.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, I agree the None case needed a clearer explanation. In f2cd27f, the RecordMetadata docstring and I/O guide now say that channels is also None when an integer storage field contains a non-integer value, including a whole-number float. They also note that the logical-only signals view can still be available. The strict docs build and Ruff checks pass; this follow-up changes documentation only.

@tompollard
tompollard merged commit 5576030 into main Dec 2, 2026
19 checks passed
@tompollard
tompollard deleted the feature/grouped-channel-metadata branch December 2, 2026 14:59
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