Skip to content

Commit 481978e

Browse files
author
Benjamin Moody
committed
wrann: allow intervals larger than 2**31 - 1.
If the gap between two consecutive annotation timestamps is greater than 2**31 - 1 ticks, it must be represented as two or more SKIP pseudo-annotations. Handle this correctly in field2bytes() (to actually generate the correct byte sequences) and in Annotation.check_field() (to permit the application to specify such a gap.) (Previously, if there was a gap of exactly 2**31 ticks, this would not be caught by check_field, and field2bytes would incorrectly generate a SKIP of -2**31 instead.)
1 parent c098e38 commit 481978e

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

wfdb/io/annotation.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,6 @@ def check_field(self, field):
466466
raise ValueError("The 'sample' field must only contain non-negative integers")
467467
if min(sampdiffs) < 0 :
468468
raise ValueError("The 'sample' field must contain monotonically increasing sample numbers")
469-
if max(sampdiffs) > 2147483648:
470-
raise ValueError('WFDB annotation files cannot store sample differences greater than 2**31')
471469

472470
elif field == 'label_store':
473471
if min(item) < 1 or max(item) > 49:
@@ -1370,19 +1368,19 @@ def field2bytes(field, value):
13701368
# sample difference
13711369
sd = value[0]
13721370

1373-
# Add SKIP element if value is too large for single byte
1374-
if sd>1023:
1375-
# 8 bytes in total:
1376-
# - [0, 59>>2] indicates SKIP
1377-
# - Next 4 gives sample difference
1378-
# - Final 2 give 0 and sym
1379-
data_bytes = [0, 236, (sd&16711680)>>16, (sd&4278190080)>>24, sd&255, (sd&65280)>>8, 0, 4*typecode]
1380-
# Just need samp and sym
1381-
else:
1382-
# - First byte stores low 8 bits of samp
1383-
# - Second byte stores high 2 bits of samp
1384-
# and sym
1385-
data_bytes = [sd & 255, ((sd & 768) >> 8) + 4*typecode]
1371+
data_bytes = []
1372+
# Add SKIP elements if value is too large
1373+
while sd > 0x7fffffff:
1374+
data_bytes += [0, 59 << 2, 0xff, 0x7f, 0xff, 0xff]
1375+
sd -= 0x7fffffff
1376+
if sd > 1023:
1377+
data_bytes += [0, 59 << 2,
1378+
(sd >> 16) & 255,
1379+
(sd >> 24) & 255,
1380+
(sd >> 0) & 255,
1381+
(sd >> 8) & 255]
1382+
sd = 0
1383+
data_bytes += [sd & 255, ((sd & 768) >> 8) + 4 * typecode]
13861384

13871385
elif field == 'num':
13881386
# First byte stores num

0 commit comments

Comments
 (0)