Skip to content

Commit a14af14

Browse files
author
Benjamin Moody
committed
rdrecord: correctly infer length for multi-frequency records.
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 tsamps_per_frame == n_sig, which is not necessarily the case. Correctly calculate the total spf for the first signal file and use that as the divisor, rather than the number of signals. Rename the "n_sig" parameter of _infer_sig_len to tsamps_per_frame accordingly.
1 parent 408ad93 commit a14af14

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

wfdb/io/_signal.py

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

20612061

2062-
def _infer_sig_len(file_name, fmt, n_sig, dir_name, pn_dir=None):
2062+
def _infer_sig_len(file_name, fmt, tsamps_per_frame, dir_name, pn_dir=None):
20632063
"""
20642064
Infer the length of a signal from a dat file.
20652065
@@ -2069,8 +2069,8 @@ def _infer_sig_len(file_name, fmt, n_sig, dir_name, pn_dir=None):
20692069
Name of the dat file.
20702070
fmt : str
20712071
WFDB fmt of the dat file.
2072-
n_sig : int
2073-
Number of signals contained in the dat file.
2072+
tsamps_per_frame : int
2073+
Total number of samples per frame contained in the dat file.
20742074
dir_name : str
20752075
The full directory where the dat file(s) are located, if the dat
20762076
file(s) are local.
@@ -2081,11 +2081,11 @@ def _infer_sig_len(file_name, fmt, n_sig, dir_name, pn_dir=None):
20812081
Returns
20822082
-------
20832083
sig_len : int
2084-
The length of the signal.
2084+
The length of the signal file in frames.
20852085
20862086
Notes
20872087
-----
2088-
sig_len * n_sig * bytes_per_sample == file_size
2088+
sig_len * tsamps_per_frame * bytes_per_sample == file_size
20892089
20902090
"""
20912091
if pn_dir is None:
@@ -2094,7 +2094,7 @@ def _infer_sig_len(file_name, fmt, n_sig, dir_name, pn_dir=None):
20942094
file_size = download._remote_file_size(file_name=file_name,
20952095
pn_dir=pn_dir)
20962096

2097-
sig_len = int(file_size / (BYTES_PER_SAMPLE[fmt] * n_sig))
2097+
sig_len = int(file_size / (BYTES_PER_SAMPLE[fmt] * tsamps_per_frame))
20982098

20992099
return sig_len
21002100

wfdb/io/record.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3441,9 +3441,18 @@ def rdrecord(record_name, sampfrom=0, sampto=None, channels=None,
34413441
if record.n_sig == 0:
34423442
record.sig_len = 0
34433443
else:
3444+
# Calculate total number of samples per frame in the
3445+
# first dat file.
3446+
tsamps_per_frame = 0
3447+
for fname, spf in zip(record.file_name,
3448+
record.samps_per_frame):
3449+
if fname == record.file_name[0]:
3450+
tsamps_per_frame += spf
3451+
3452+
# Calculate length from size of the dat file.
34443453
record.sig_len = _signal._infer_sig_len(
34453454
file_name=record.file_name[0], fmt=record.fmt[0],
3446-
n_sig=record.file_name.count(record.file_name[0]),
3455+
tsamps_per_frame=tsamps_per_frame,
34473456
dir_name=dir_name, pn_dir=pn_dir)
34483457
sampto = record.sig_len
34493458

0 commit comments

Comments
 (0)