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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ record = wfdb.read_record("path/to/record", layout="uniform")
| Read a selected, combined record | `read_record` |
| Inspect a header without loading samples | `read_header` |
| Keep a multi-segment record's boundaries | `read_segments` |
| Write a multi-segment record with its boundaries | `write_segments` |
| Read an annotation file | `read_annotations` |
| Plot signals and annotations | `record.plot(annotation=...)` |
| Write a loaded record or an array | `write_record` or `write_signals` |
Expand Down Expand Up @@ -95,6 +96,22 @@ selected = record.select(start=0, stop=1000)
wfdb.write_record("selected", selected)
```

To keep the boundaries of a multi-segment record, read and write its
segments instead. This also preserves gaps between segments:

```python
segments = wfdb.read_segments(
"path/to/multi-segment-record", layout="uniform", start=0, stop=1000
)
segments.write("selected-segments")
# Equivalent: wfdb.write_segments("selected-segments", segments)
```

If the source uses a format that cannot be written, pass a target format such
as `formats="16"`. You can also pass one format per selected channel in master
channel order. A fixed-layout selection needs at least one data segment to
define its channels; a window containing only gaps cannot be written.

Use `write_signals` to create a single-segment record from arrays. In that
case, supply the sampling frequency, value kind, storage format, channel
names, and units:
Expand Down
12 changes: 12 additions & 0 deletions changelog.d/write-segments.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
**Write selected multi-segment records**
New ``write_segments`` and ``SegmentedRecordData.write`` write fixed- and
variable-layout records from loaded segments without a legacy
``MultiRecord``. They preserve boundaries and ``~`` gaps, generate distinct
destination segment names, and stage files before replacing local output.
A real segment with no selected channels becomes a ``~`` gap of the same
length, so its original name and header are not included in the new record.
Segment and layout headers take their start times, including dates, from
the selected master. ``formats`` chooses a writable storage format for
every channel, or one per selected channel in master order. A fixed-layout
selection containing only gaps is rejected, because no data segment would
define its channels.
23 changes: 22 additions & 1 deletion docs/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ WFDB Records
:members: rdrecord, read_record, write_record, write_signals, find_record,
wfdb_search_path, rdheader, read_header,
read_multi_segment_headers, read_channel_header,
read_segments, iter_dataframes,
read_segments, write_segments, iter_dataframes,
rdsamp, wrsamp, sampfreq, signame, wfdbdesc,
wfdbtime

Expand Down Expand Up @@ -818,6 +818,27 @@ frame count, signal kind and layout, sampling frequency, and channel mapping.
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.
Use ``write_segments(path, segments)`` or ``segments.write(path)`` to write the
selected view as a new multi-segment record. The output starts at frame zero
and uses generated segment names such as ``record_0001``. The writer preserves
boundaries and ``~`` gaps; a real segment with no selected channels becomes a
``~`` gap of the same length. The selected master metadata supplies the output
start time, while source headers remain provenance. This writer accepts local
record stems, supported writable cloud URIs, and local ``.wfdb`` archives.
It stages every file before replacing a local destination. Segment and layout
headers take their start times, including dates, from the selected master.
Pass ``formats="16"`` for every channel, or one format per selected channel in
master order, when a source format cannot be written or loaded segments do not
establish one format; each segment receives its own channels' formats. A
fixed-layout selection containing only ``~`` gaps is rejected because no data
segment would define its channels.

For example, write a selected window while keeping its segment boundaries::

segments = wfdb.read_segments(
"path/to/multi-segment-record", layout="uniform", start=0, stop=1000
)
segments.write("selected-segments")

.. autoclass:: wfdb.io.SegmentedRecordData
:no-index:
Expand Down
1 change: 1 addition & 0 deletions src/wfdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from wfdb.io._segment_write import write_segments
from wfdb.io.annotation import (
Annotation,
merge_annotations,
Expand Down
1 change: 1 addition & 0 deletions src/wfdb/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from wfdb.io._segment_write import write_segments
from wfdb.io._signal import est_res, wr_dat_file
from wfdb.io.annotation import (
Annotation,
Expand Down
Loading
Loading