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
54 changes: 18 additions & 36 deletions src/wfdb/io/_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ class _ParsedSignalLine:
sig_name: str | None


@dataclass(frozen=True)
class _ParsedSegmentLine:
"""Fields from one segment line in a multi-segment header."""

seg_name: str
seg_len: int


@dataclass(frozen=True)
class _ParsedSingleSegmentHeader:
"""Fields parsed from a header containing signal lines."""
Expand All @@ -172,7 +180,7 @@ class _ParsedMultiSegmentHeader:
"""Fields parsed from a header containing segment lines."""

record_fields: dict[str, Any]
segment_fields: dict[str, list[Any]]
segments: tuple[_ParsedSegmentLine, ...]
comments: list[str]


Expand Down Expand Up @@ -1256,41 +1264,15 @@ def _parse_signal_lines(signal_lines):


def _read_segment_lines(segment_lines):
"""Extract fields from segment line strings into a dictionary.

Parameters
----------
segment_line : list
The name of the segment line that will be used to extact fields.

Returns
-------
segment_fields : dict
The fields for the given segment line.

"""
# Dictionary for segment fields
segment_fields = {}

# Each dictionary field is a list
for field in SEGMENT_SPECS.index:
segment_fields[field] = [None] * len(segment_lines)

# Read string fields from signal line
for i in range(len(segment_lines)):
match = rx_segment.match(segment_lines[i])
"""Parse segment lines into paired names and lengths."""
segments = []
for segment_line in segment_lines:
match = rx_segment.match(segment_line)
if match is None:
raise HeaderSyntaxError("invalid syntax in segment line")
(
segment_fields["seg_name"][i],
segment_fields["seg_len"][i],
) = match.groups()

# Typecast strings for numerical field
if field == "seg_len":
segment_fields["seg_len"][i] = int(segment_fields["seg_len"][i])

return segment_fields
seg_name, seg_len = match.groups()
segments.append(_ParsedSegmentLine(seg_name, int(seg_len)))
return tuple(segments)


def _parse_header_fields(
Expand All @@ -1313,5 +1295,5 @@ def _parse_header_fields(
)
return _ParsedSingleSegmentHeader(record_fields, signals, comments)

segment_fields = _read_segment_lines(header_lines[1:])
return _ParsedMultiSegmentHeader(record_fields, segment_fields, comments)
segments = _read_segment_lines(header_lines[1:])
return _ParsedMultiSegmentHeader(record_fields, segments, comments)
6 changes: 4 additions & 2 deletions src/wfdb/io/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -2574,8 +2574,10 @@ def _record_from_header_fields(parsed):
setattr(record, field, value)
else:
record = MultiRecord()
for field, value in parsed.segment_fields.items():
setattr(record, field, value.copy())
for field in _header.SEGMENT_SPECS.index:
setattr(
record, field, [getattr(segment, field) for segment in parsed.segments]
)
for field, value in parsed.record_fields.items():
setattr(record, field, value)
if record.seg_len[0] == 0:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dataframe_chunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ def test_cached_header_lists_are_independent(path, field):
if field == "sig_name":
assert [signal.sig_name for signal in cached.signals] == original
else:
assert cached.segment_fields[field] == original
assert [getattr(segment, field) for segment in cached.segments] == original
19 changes: 19 additions & 0 deletions tests/test_record_source_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,22 @@ def test_parsed_signal_lines_keep_skew_with_their_signals():
assert [signal.skew for signal in source.signals] == [None, 3]
header = record._record_from_header_fields(source)
assert header.skew == [None, 3]


def test_parsed_segment_lines_keep_names_with_lengths():
"""Empty and layout segments retain their lengths in parsed headers."""
path = "sample-data/multi-segment/p000878/p000878-2137-10-26-16-57"
source = record._read_header_fields(path, pn_dir=None)

assert isinstance(source, _header._ParsedMultiSegmentHeader)
assert [(segment.seg_name, segment.seg_len) for segment in source.segments[:3]] == [
("3269321_layout", 0),
("~", 3750),
("3269321_0001", 2000),
]

header = record._record_from_header_fields(source)
assert header.seg_name[:3] == ["3269321_layout", "~", "3269321_0001"]
assert header.seg_len[:3] == [0, 3750, 2000]
header.seg_name[1] = "changed"
assert source.segments[1].seg_name == "~"
Loading