Skip to content

Commit 94449b8

Browse files
author
Benjamin Moody
committed
rdann: compute timestamps as int, not a numpy integer.
When reading an annotation file in WFDB format, the timestamp (sample number) must be computed by adding up the relative timestamp difference for each annotation. For long records, sample numbers can easily exceed 2**32. The input to proc_core_fields is a numpy array, so if we operate on the byte values with ordinary arithmetic operations, the result will be a numpy integer object with numpy's default precision (i.e., int32 on 32-bit architectures, int64 on 64-bit architectures.) Instead, calculate the result as a Python integer, to avoid architecture-dependent behavior and (possible) silent wrapping. (Furthermore, use left-shift operations instead of multiplying by constants that are hard to remember.)
1 parent 2dd4845 commit 94449b8

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

wfdb/io/annotation.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,8 +1875,10 @@ def proc_core_fields(filebytes, bpi):
18751875
# or 0 + SKIP.
18761876
while filebytes[bpi, 1] >> 2 == 59:
18771877
# 4 bytes storing dt
1878-
skip_diff = 65536 * filebytes[bpi + 1,0] + 16777216 * filebytes[bpi + 1,1] \
1879-
+ filebytes[bpi + 2,0] + 256 * filebytes[bpi + 2,1]
1878+
skip_diff = ((int(filebytes[bpi + 1, 0]) << 16)
1879+
+ (int(filebytes[bpi + 1, 1]) << 24)
1880+
+ (int(filebytes[bpi + 2, 0]) << 0)
1881+
+ (int(filebytes[bpi + 2, 1]) << 8))
18801882

18811883
# Data type is long integer (stored in two's complement). Range -2**31 to 2**31 - 1
18821884
if skip_diff > 2147483647:
@@ -1887,7 +1889,7 @@ def proc_core_fields(filebytes, bpi):
18871889

18881890
# Not a skip - it is the actual sample number + annotation type store value
18891891
label_store = filebytes[bpi, 1] >> 2
1890-
sample_diff += filebytes[bpi, 0] + 256 * (filebytes[bpi, 1] & 3)
1892+
sample_diff += int(filebytes[bpi, 0] + 256 * (filebytes[bpi, 1] & 3))
18911893
bpi = bpi + 1
18921894

18931895
return sample_diff, label_store, bpi

0 commit comments

Comments
 (0)