Skip to content

Commit a95227f

Browse files
author
Benjamin Moody
committed
rdrecord: correctly infer length for signal files with a prolog.
If the header file does not specify the record length, it must be inferred from the size of the signal file (dividing the data length by the number of samples per frame.) Previously, this calculation assumed that the file did not contain a prolog. Correctly calculate the data length by subtracting the prolog length (byte offset) from the size of the file.
1 parent 1e07f0d commit a95227f

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

wfdb/io/_signal.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,8 @@ def describe_list_indices(full_list):
20592059
return unique_elements, element_indices
20602060

20612061

2062-
def _infer_sig_len(file_name, fmt, tsamps_per_frame, dir_name, pn_dir=None):
2062+
def _infer_sig_len(file_name, fmt, tsamps_per_frame, byte_offset,
2063+
dir_name, pn_dir=None):
20632064
"""
20642065
Infer the length of a signal from a dat file.
20652066
@@ -2071,6 +2072,8 @@ def _infer_sig_len(file_name, fmt, tsamps_per_frame, dir_name, pn_dir=None):
20712072
WFDB fmt of the dat file.
20722073
tsamps_per_frame : int
20732074
Total number of samples per frame contained in the dat file.
2075+
byte_offset : int or None
2076+
The byte offset of the dat file. None is equivalent to zero.
20742077
dir_name : str
20752078
The full directory where the dat file(s) are located, if the dat
20762079
file(s) are local.
@@ -2094,7 +2097,10 @@ def _infer_sig_len(file_name, fmt, tsamps_per_frame, dir_name, pn_dir=None):
20942097
file_size = download._remote_file_size(file_name=file_name,
20952098
pn_dir=pn_dir)
20962099

2097-
sig_len = int(file_size / (BYTES_PER_SAMPLE[fmt] * tsamps_per_frame))
2100+
if byte_offset is None:
2101+
byte_offset = 0
2102+
data_size = file_size - byte_offset
2103+
sig_len = int(data_size / (BYTES_PER_SAMPLE[fmt] * tsamps_per_frame))
20982104

20992105
return sig_len
21002106

wfdb/io/record.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3453,6 +3453,7 @@ def rdrecord(record_name, sampfrom=0, sampto=None, channels=None,
34533453
record.sig_len = _signal._infer_sig_len(
34543454
file_name=record.file_name[0], fmt=record.fmt[0],
34553455
tsamps_per_frame=tsamps_per_frame,
3456+
byte_offset=record.byte_offset[0],
34563457
dir_name=dir_name, pn_dir=pn_dir)
34573458
sampto = record.sig_len
34583459

0 commit comments

Comments
 (0)