Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ae67d09

Browse files
authoredSep 10, 2021
Merge pull request #323 from MIT-LCP/infer-length
Correctly infer length of record when unspecified
2 parents df15eeb + e36dcbf commit ae67d09

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed
 

‎sample-data/a103l-no-len.hea

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
a103l-no-len 3 250
2+
a103l.mat 16+24 7247/mV 16 0 -171 -27403 0 II
3+
a103l.mat 16+24 1.052e+04/mV 16 0 9127 -301 0 V
4+
a103l.mat 16+24 1.253e+04/NU 16 0 6042 -17391 0 PLETH
5+
#Asystole
6+
#False alarm

‎sample-data/drive02-no-len.hea

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
drive02-no-len 5 15.5
2+
drive02.dat 16x32 1000 16 0 -1236 14736 0 ECG
3+
drive02.dat 16x2 1000 16 0 1802 13501 0 foot GSR
4+
drive02.dat 16 1.0001/bpm 16 0 75 -19070 0 HR
5+
drive02.dat 16 100 16 0 0 -9226 0 marker
6+
drive02.dat 16x2 500 16 0 5804 -14191 0 RESP

‎tests/test_record.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,9 +703,15 @@ def test_infer_sig_len(self):
703703
Read two headers. The records should be the same.
704704
"""
705705

706-
record = wfdb.rdrecord('sample-data/100')
707-
record_2 = wfdb.rdrecord('sample-data/100-no-len')
708-
record_2.record_name = '100'
706+
record = wfdb.rdrecord('sample-data/drive02')
707+
record_2 = wfdb.rdrecord('sample-data/drive02-no-len')
708+
record_2.record_name = record.record_name
709+
710+
assert record_2.__eq__(record)
711+
712+
record = wfdb.rdrecord('sample-data/a103l')
713+
record_2 = wfdb.rdrecord('sample-data/a103l-no-len')
714+
record_2.record_name = record.record_name
709715

710716
assert record_2.__eq__(record)
711717

‎wfdb/io/_signal.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,8 @@ def describe_list_indices(full_list):
20712071
return unique_elements, element_indices
20722072

20732073

2074-
def _infer_sig_len(file_name, fmt, n_sig, dir_name, pn_dir=None):
2074+
def _infer_sig_len(file_name, fmt, tsamps_per_frame, byte_offset,
2075+
dir_name, pn_dir=None):
20752076
"""
20762077
Infer the length of a signal from a dat file.
20772078
@@ -2081,8 +2082,10 @@ def _infer_sig_len(file_name, fmt, n_sig, dir_name, pn_dir=None):
20812082
Name of the dat file.
20822083
fmt : str
20832084
WFDB fmt of the dat file.
2084-
n_sig : int
2085-
Number of signals contained in the dat file.
2085+
tsamps_per_frame : int
2086+
Total number of samples per frame contained in the dat file.
2087+
byte_offset : int or None
2088+
The byte offset of the dat file. None is equivalent to zero.
20862089
dir_name : str
20872090
The full directory where the dat file(s) are located, if the dat
20882091
file(s) are local.
@@ -2093,11 +2096,11 @@ def _infer_sig_len(file_name, fmt, n_sig, dir_name, pn_dir=None):
20932096
Returns
20942097
-------
20952098
sig_len : int
2096-
The length of the signal.
2099+
The length of the signal file in frames.
20972100
20982101
Notes
20992102
-----
2100-
sig_len * n_sig * bytes_per_sample == file_size
2103+
sig_len * tsamps_per_frame * bytes_per_sample == file_size
21012104
21022105
"""
21032106
if pn_dir is None:
@@ -2106,7 +2109,10 @@ def _infer_sig_len(file_name, fmt, n_sig, dir_name, pn_dir=None):
21062109
file_size = download._remote_file_size(file_name=file_name,
21072110
pn_dir=pn_dir)
21082111

2109-
sig_len = int(file_size / (BYTES_PER_SAMPLE[fmt] * n_sig))
2112+
if byte_offset is None:
2113+
byte_offset = 0
2114+
data_size = file_size - byte_offset
2115+
sig_len = int(data_size / (BYTES_PER_SAMPLE[fmt] * tsamps_per_frame))
21102116

21112117
return sig_len
21122118

‎wfdb/io/record.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3441,9 +3441,19 @@ 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,
3456+
byte_offset=record.byte_offset[0],
34473457
dir_name=dir_name, pn_dir=pn_dir)
34483458
sampto = record.sig_len
34493459

0 commit comments

Comments
 (0)
Please sign in to comment.