Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog.d/record-data-edits.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
**Select and replace loaded RecordData values**
``RecordData.select`` copies a frame and channel selection from the current
arrays, preserving source provenance. ``RecordData.with_values`` validates
replacement arrays of the same kind and layout, including an explicit
missing-sample policy for plain digital arrays. Both return independent
results. ``select`` recomputes sample-dependent header fields when the
selected data has enough calibration information. Writing a result of
either edit method derives initial values and checksums from the values
being saved.
40 changes: 39 additions & 1 deletion docs/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,52 @@ files that either reader accepts.

.. autoclass:: wfdb.io.RecordData
:no-index:
:members: to_physical, to_digital, to_legacy_record, write
:members: to_physical, to_digital, to_legacy_record, select,
with_values, write

.. autoclass:: wfdb.io.UniformSignals
:no-index:

.. autoclass:: wfdb.io.ExpandedSignals
:no-index:

Editing a selected record
-------------------------

``RecordData.select(start=..., stop=..., channels=...)`` selects from the
current loaded arrays, including any sample edits already made. Frame bounds
and channel indices refer to the current view; ``channel_names`` can be used
instead of indices when each name identifies one channel. The result copies
the selected arrays and masks, and its ``source_selection`` still refers to
the original source record. Flattened ``source_segments`` are clipped and
rebased to the new view. The master ``source_header`` remains unchanged; for
fixed-layout data, ``source_signal_header`` follows the first retained data
segment, as it does on a direct read of that window.

``RecordData.with_values(values)`` replaces the current arrays while keeping
their kind, layout, frame count, channel count, and calibration. Physical
replacement arrays must contain floating values, using NaN for missing
samples. Digital replacement arrays must be NumPy masked integer arrays;
for a plain integer array with no missing samples, pass ``missing="none"``.
Known digital channel formats are checked for out-of-range and unmasked
missing-marker values when ``with_values`` is called. Channels assembled
from different source formats need a target ``formats=`` choice when written.
Both methods return independent arrays. ``select`` recomputes initial values
and checksums when the selected data has enough calibration information;
flattened physical multi-segment reads retain absent checksum declarations.
``with_values`` clears them. A source header can hold a signed checksum while
recalculation uses the equivalent unsigned value modulo 65536, so a full-record
selection can differ numerically from its source header. Writing either result
derives these fields from the values being saved. The source views identify
where the data came from;
they do not assert that edited samples still match the source file.

.. code-block:: python

read = wfdb.read_signals("sample-data/100", layout="uniform")
selected = read.select(start=100, stop=200, channels=[1, 0])
edited = selected.with_values(selected.signals.values * 2)

Writing a selected signal view
------------------------------

Expand Down
1 change: 1 addition & 0 deletions src/wfdb/io/_multi_read_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def mark_missing(channel, first, last, missing):
write_formats=write_formats,
source_layout="variable" if variable else "fixed",
unset_signal_fields=unset_signal_fields,
segment_headers=tuple(headers.items()),
)


Expand Down
1 change: 1 addition & 0 deletions src/wfdb/io/_read_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class _ReadModel:
write_formats: tuple[str | None, ...] | None = None
source_layout: str | None = None
unset_signal_fields: tuple[str, ...] = ()
segment_headers: tuple[tuple[str, _header._ParsedSingleSegmentHeader], ...] = ()

def to_record(self, current_values):
"""Project current values to a legacy Record only when requested."""
Expand Down
Loading
Loading