Skip to content

Commit 10c1143

Browse files
committed
add option for rdheader to automatically read segment headers when targeting multi-segment headers
1 parent df3c649 commit 10c1143

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

wfdb/readwrite/records.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ def __init__(self, segments = None, layout = None,
503503
fs, counterfreq, basecounter, siglen,
504504
basetime, basedate, comments)
505505

506+
self.layout = layout
506507
self.segments = segments
507508
self.segname = segname
508509
self.seglen = seglen
@@ -794,7 +795,7 @@ def rdsamp(recordname, sampfrom=0, sampto=None, channels = None, physical = True
794795
dirname, baserecordname = os.path.split(recordname)
795796

796797
# Read the header fields into the appropriate record object
797-
record = rdheader(recordname, pbdir = pbdir)
798+
record = rdheader(recordname, pbdir = pbdir, rdsegments = False)
798799

799800
# Set defaults for sampto and channels input variables
800801
if sampto is None:
@@ -899,11 +900,11 @@ def rdsamp(recordname, sampfrom=0, sampto=None, channels = None, physical = True
899900

900901

901902
# Read a WFDB header. Return a Record object or MultiRecord object
902-
def rdheader(recordname, pbdir = None):
903+
def rdheader(recordname, pbdir = None, rdsegments = False):
903904
"""Read a WFDB header file and return the record descriptors as attributes in a Record object
904905
905906
Usage:
906-
record = rdheader(recordname, pbdir = None)
907+
record = rdheader(recordname, pbdir = None, rdsegments = False)
907908
908909
Input arguments:
909910
- recordname (required): The name of the WFDB record to be read (without any file extensions).
@@ -912,6 +913,8 @@ def rdheader(recordname, pbdir = None):
912913
- pbdir (default=None): Option used to stream data from Physiobank. The Physiobank database
913914
directory from which to find the required record files.
914915
eg. For record '100' in 'http://physionet.org/physiobank/database/mitdb', pbdir = 'mitdb'.
916+
- rdsegments (default=False): Boolean flag used when reading multi-segment headers. If True,
917+
segment headers will also be read (into the record object's 'segments' field).
915918
916919
Output argument:
917920
- record: The wfdb Record or MultiRecord object representing the contents of the header read.
@@ -959,6 +962,23 @@ def rdheader(recordname, pbdir = None):
959962
# Set the objects' record line fields
960963
for field in _headers.recfieldspecs:
961964
setattr(record, field, d_rec[field])
965+
# Determine whether the record is fixed or variable
966+
if record.seglen[0] == 0:
967+
record.layout = 'Variable'
968+
else:
969+
record.layout = 'Fixed'
970+
971+
# If specified, read the segment headers
972+
if rdsegments:
973+
record.segments = []
974+
# Get the base record name (could be empty)
975+
dirname = os.path.split(recordname)[0]
976+
for s in record.segname:
977+
if s == '~':
978+
record.segments.append(None)
979+
else:
980+
record.segments.append(rdheader(os.path.join(dirname,s), pbdir))
981+
962982
# Set the comments field
963983
record.comments = []
964984
for line in commentlines:

0 commit comments

Comments
 (0)