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
7 changes: 7 additions & 0 deletions changelog.d/segmented-selected-master.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**Describe and edit the selected master view of segmented reads**
``SegmentedRecordData.metadata`` now describes the selected master frames,
channels, start time, and loaded signal kind and layout separately from
``source_header``. ``with_segment`` validates a replacement data segment,
and ``shift_time`` moves the selected master and stated segment times
together without changing source provenance. Segmented reads pickled by
5.18.0 or 6.0.0 remain usable after loading.
11 changes: 10 additions & 1 deletion docs/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,9 @@ provenance. A preserved segment sequence is available through
``read_segments`` reads the selected window eagerly and returns a
``SegmentedRecordData``. Its ``source_header`` is the master header, and
``channel_header`` is the variable-layout header or the first non-gap fixed
segment header. Each entry has a name and ``master_start``/``master_stop``:
segment header. Its ``metadata`` describes the selected master view, including
the selected start time and channels, separately from the source headers.
Each entry has a name and ``master_start``/``master_stop``:
absolute, clipped master-record frame positions. In contrast,
``RecordData.source_segments`` uses offsets in the returned combined view.
A ``~`` gap has ``data=None``. A real entry contains a ``RecordData``
Expand All @@ -810,12 +812,19 @@ there. A real entry can therefore have zero channels. The layout header is
never an entry, and selected channels are not padded across segments. A
segment with values can be written on its own with ``data.write(path)``;
writing a zero-channel entry raises ``ValueError``.
``with_segment(index, data)`` replaces a real segment after checking its
frame count, signal kind and layout, sampling frequency, and channel mapping.
``shift_time(delta)`` moves the selected master start and stated segment
times together, while keeping source headers as provenance.
Call ``segments.plot()`` to show all loaded entries on one master-record
timeline, with segment boundaries marked and gaps shaded.

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

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

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

Expand Down
7 changes: 6 additions & 1 deletion src/wfdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@
write_record,
wrsamp,
)
from wfdb.io.segment_values import SegmentData, SegmentedRecordData, read_segments
from wfdb.io.segment_values import (
SegmentData,
SegmentedRecordData,
SegmentedRecordMetadata,
read_segments,
)
from wfdb.io.signal_values import ExpandedSignals, RecordData, UniformSignals
from wfdb.io.signal_write import write_signals
from wfdb.plot.plot import plot_all_records, plot_items, plot_wfdb
Expand Down
7 changes: 6 additions & 1 deletion src/wfdb/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@
write_record,
wrsamp,
)
from wfdb.io.segment_values import SegmentData, SegmentedRecordData, read_segments
from wfdb.io.segment_values import (
SegmentData,
SegmentedRecordData,
SegmentedRecordMetadata,
read_segments,
)
from wfdb.io.signal_values import ExpandedSignals, RecordData, UniformSignals
from wfdb.io.signal_write import write_signals

Expand Down
14 changes: 9 additions & 5 deletions src/wfdb/io/_record_data_edits.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,14 @@ def replace_record_calibration(read, adc_gain, baseline, mode):

def shift_record_time(read, delta):
"""Shift the selected start date or time without changing its source."""
timing = _shift_base_time(read.metadata.base_date, read.metadata.base_time, delta)
return _replace_record_metadata(read, timing=timing)


def _shift_base_time(base_date, base_time, delta):
"""Apply the RecordData date, time, and range rules to a selected start."""
if not isinstance(delta, datetime.timedelta):
raise TypeError("delta must be a datetime.timedelta")
base_date = read.metadata.base_date
base_time = read.metadata.base_time
if base_date is None and base_time is None:
raise ValueError("Cannot shift a record without a base date or time")
if base_date is not None and base_time is None:
Expand All @@ -457,7 +461,7 @@ def shift_record_time(read, delta):
raise ValueError("Shifted date is outside years 1 to 9999") from error
# A WFDB header cannot write a date without a time. Normalize the
# date-only input to midnight so the edited result can be saved.
return _replace_record_metadata(read, timing=(shifted_date, datetime.time(0)))
return shifted_date, datetime.time(0)
if base_time.tzinfo is not None:
raise ValueError("WFDB base times cannot have time zones")
if base_date is None:
Expand All @@ -473,12 +477,12 @@ def shift_record_time(read, delta):
minute, clock_us = divmod(clock_us, 60 * 1_000_000)
second, microsecond = divmod(clock_us, 1_000_000)
shifted_time = datetime.time(hour, minute, second, microsecond)
return _replace_record_metadata(read, timing=(None, shifted_time))
return None, shifted_time
try:
shifted = datetime.datetime.combine(base_date, base_time) + delta
except OverflowError as error:
raise ValueError("Shifted date is outside years 1 to 9999") from error
return _replace_record_metadata(read, timing=(shifted.date(), shifted.time()))
return shifted.date(), shifted.time()


def _replace_record_metadata(
Expand Down
Loading
Loading