|
6 | 6 | import os |
7 | 7 | import math |
8 | 8 |
|
| 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 |
9 | 56 |
|
10 | 57 | def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1): |
11 | 58 | """ 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): |
60 | 107 |
|
61 | 108 | # Check the beginning of the annotation file to see if it is storing the |
62 | 109 | # '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) |
106 | 111 |
|
107 | 112 | # Total number of samples of current annotation from beginning of record. |
108 | 113 | # Annotation bytes only store dt. |
|
0 commit comments