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
45 changes: 44 additions & 1 deletion src/wfdb/io/_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
import re
import warnings
from collections.abc import Sequence
from dataclasses import dataclass
from typing import Any

import numpy as np
import pandas as pd

from wfdb.io import _signal, util
from wfdb.io.header import HeaderSyntaxError, rx_record, rx_segment, rx_signal
from wfdb.io.header import (
HeaderSyntaxError,
parse_header_content,
rx_record,
rx_segment,
rx_signal,
)

"""
Notes
Expand Down Expand Up @@ -131,6 +138,24 @@
FIELD_SPECS = pd.concat((RECORD_SPECS, SIGNAL_SPECS, SEGMENT_SPECS))


@dataclass(frozen=True)
class _ParsedSingleSegmentHeader:
"""Fields parsed from a header containing signal lines."""

record_fields: dict[str, Any]
signal_fields: dict[str, list[Any]] | None
comments: list[str]


@dataclass(frozen=True)
class _ParsedMultiSegmentHeader:
"""Fields parsed from a header containing segment lines."""

record_fields: dict[str, Any]
segment_fields: dict[str, list[Any]]
comments: list[str]


class BaseHeaderMixin:
"""Mixin class with multi-segment header methods.

Expand Down Expand Up @@ -1246,3 +1271,21 @@ def _read_segment_lines(segment_lines):
segment_fields["seg_len"][i] = int(segment_fields["seg_len"][i])

return segment_fields


def _parse_header_fields(
header_content: str,
) -> _ParsedSingleSegmentHeader | _ParsedMultiSegmentHeader:
"""Parse a header into its single- or multi-segment field structure."""
header_lines, comment_lines = parse_header_content(header_content)
record_fields = _parse_record_line(header_lines[0])
comments = [line.strip(" \t#") for line in comment_lines]

if record_fields["n_seg"] is None:
signal_fields = (
_parse_signal_lines(header_lines[1:]) if len(header_lines) > 1 else None
)
return _ParsedSingleSegmentHeader(record_fields, signal_fields, comments)

segment_fields = _read_segment_lines(header_lines[1:])
return _ParsedMultiSegmentHeader(record_fields, segment_fields, comments)
48 changes: 13 additions & 35 deletions src/wfdb/io/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import numpy as np
import pandas as pd

from wfdb.io import _header, _signal, _url, download, header, util
from wfdb.io import _header, _signal, _url, download, util
from wfdb.io._cloud import check_cloud_write, is_cloud_path, staged_cloud_write
from wfdb.io._coreio import CLOUD_PROTOCOLS, _fsspec_open, _join_path, _split_path
from wfdb.io.archive import WFDBArchive, archive_for_writing, is_archive_path
Expand Down Expand Up @@ -2453,51 +2453,29 @@ def _parse_header_content(header_content):
The record described by the header.

"""
# Separate comment and non-comment lines
header_lines, comment_lines = header.parse_header_content(header_content)
parsed = _header._parse_header_fields(header_content)

# Get fields from record line
record_fields = _header._parse_record_line(header_lines[0])

# Single segment header - Process signal specification lines
if record_fields["n_seg"] is None:
# Create a single-segment WFDB record object
if isinstance(parsed, _header._ParsedSingleSegmentHeader):
record = Record()

# There are signals
if len(header_lines) > 1:
# Read the fields from the signal lines
signal_fields = _header._parse_signal_lines(header_lines[1:])
# Set the object's signal fields
for field in signal_fields:
setattr(record, field, signal_fields[field])

# Set the object's record line fields
for field in record_fields:
if parsed.signal_fields is not None:
for field, value in parsed.signal_fields.items():
setattr(record, field, value)
for field, value in parsed.record_fields.items():
if field == "n_seg":
continue
setattr(record, field, record_fields[field])
# Multi segment header - Process segment specification lines
setattr(record, field, value)
else:
# Create a multi-segment WFDB record object
record = MultiRecord()
# Read the fields from the segment lines
segment_fields = _header._read_segment_lines(header_lines[1:])
# Set the object's segment fields
for field in segment_fields:
setattr(record, field, segment_fields[field])
# Set the objects' record fields
for field in record_fields:
setattr(record, field, record_fields[field])

# Determine whether the record is fixed or variable
for field, value in parsed.segment_fields.items():
setattr(record, field, value)
for field, value in parsed.record_fields.items():
setattr(record, field, value)
if record.seg_len[0] == 0:
record.layout = "variable"
else:
record.layout = "fixed"

# Set the comments field
record.comments = [line.strip(" \t#") for line in comment_lines]
record.comments = parsed.comments

return record

Expand Down
Loading