Skip to content

Commit 5b5d897

Browse files
committed
move time resolution check to function
1 parent 09c3ab7 commit 5b5d897

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

wfdb/_rdann.py

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,53 @@
66
import os
77
import math
88

9+
def check_time_resolution(filebytes,annfs,bpi):
10+
# Check the beginning of the annotation file to see if it is storing the
11+
# 'time resolution' field.
12+
if filebytes.size > 24:
13+
testbytes = filebytes[:12, :].flatten()
14+
# First 2 bytes indicate dt=0 and anntype=NOTE. Next 2 indicate auxlen
15+
# and anntype=AUX. Then follows "## time resolution: "
16+
if [
17+
testbytes[i] for i in [
18+
0,
19+
1] +
20+
list(
21+
range(
22+
3,
23+
24))] == [
24+
0,
25+
88,
26+
252,
27+
35,
28+
35,
29+
32,
30+
116,
31+
105,
32+
109,
33+
101,
34+
32,
35+
114,
36+
101,
37+
115,
38+
111,
39+
108,
40+
117,
41+
116,
42+
105,
43+
111,
44+
110,
45+
58,
46+
32]: # The file's leading bytes match the expected pattern for encoding fs.
47+
# Length of the auxilliary string that includes the fs written into
48+
# the file.
49+
auxlen = testbytes[2]
50+
testbytes = filebytes[:(12 + math.ceil(auxlen / 2.)), :].flatten()
51+
annfs = int("".join([chr(char)
52+
for char in testbytes[24:auxlen + 4]]))
53+
# byte pair index to start reading actual annotations.
54+
bpi = 0.5 * (auxlen + 12 + (auxlen & 1))
55+
return annfs,bpi
956

1057
def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
1158
""" Read a WFDB annotation file recordname.annot and return the fields as lists or arrays
@@ -60,49 +107,7 @@ def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
60107

61108
# Check the beginning of the annotation file to see if it is storing the
62109
# 'time resolution' field.
63-
if filebytes.size > 24:
64-
testbytes = filebytes[:12, :].flatten()
65-
# First 2 bytes indicate dt=0 and anntype=NOTE. Next 2 indicate auxlen
66-
# and anntype=AUX. Then follows "## time resolution: "
67-
if [
68-
testbytes[i] for i in [
69-
0,
70-
1] +
71-
list(
72-
range(
73-
3,
74-
24))] == [
75-
0,
76-
88,
77-
252,
78-
35,
79-
35,
80-
32,
81-
116,
82-
105,
83-
109,
84-
101,
85-
32,
86-
114,
87-
101,
88-
115,
89-
111,
90-
108,
91-
117,
92-
116,
93-
105,
94-
111,
95-
110,
96-
58,
97-
32]: # The file's leading bytes match the expected pattern for encoding fs.
98-
# Length of the auxilliary string that includes the fs written into
99-
# the file.
100-
auxlen = testbytes[2]
101-
testbytes = filebytes[:(12 + math.ceil(auxlen / 2.)), :].flatten()
102-
annfs = int("".join([chr(char)
103-
for char in testbytes[24:auxlen + 4]]))
104-
# byte pair index to start reading actual annotations.
105-
bpi = 0.5 * (auxlen + 12 + (auxlen & 1))
110+
annfs,bpi = check_time_resolution(filebytes,annfs,bpi)
106111

107112
# Total number of samples of current annotation from beginning of record.
108113
# Annotation bytes only store dt.

0 commit comments

Comments
 (0)