Skip to content

Commit d647453

Browse files
committed
add apply_annotation_range func
1 parent eb129a6 commit d647453

File tree

1 file changed

+45
-39
lines changed

1 file changed

+45
-39
lines changed

wfdb/_rdann.py

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,42 @@ def proc_extra_fields(AT,subtype,ai,filebytes,bpi,num,chan,cpychan,cpynum,aux):
8080
bpi = bpi + 1 + math.ceil(auxlen / 2.)
8181
return subtype,bpi,num,chan,cpychan,cpynum,aux
8282

83+
def apply_annotation_range(annsamp,sampfrom,sampto,anntype,num,subtype,chan,aux):
84+
# Keep the annotations in the specified range
85+
returnempty = 0
86+
87+
afterfrom = np.where(annsamp >= sampfrom)[0]
88+
if len(afterfrom) > 0:
89+
ik0 = afterfrom[0] # index keep start
90+
else: # No annotations in specified range.
91+
returnempty = 1
92+
93+
if not sampto:
94+
sampto = annsamp[-1]
95+
beforeto = np.where(annsamp <= sampto)[0]
96+
97+
if len(beforeto) > 0:
98+
ik1 = beforeto[-1]
99+
else:
100+
returnempty = 1
101+
102+
if returnempty:
103+
annsamp = []
104+
anntype = []
105+
num = []
106+
subtype = []
107+
chan = []
108+
aux = []
109+
print("No annotations in specified sample range")
110+
else:
111+
annsamp = annsamp[ik0:ik1 + 1]
112+
anntype = anntype[ik0:ik1 + 1]
113+
num = num[ik0:ik1 + 1]
114+
subtype = subtype[ik0:ik1 + 1]
115+
chan = chan[ik0:ik1 + 1]
116+
aux = aux[ik0:ik1 + 1]
117+
return annsamp,anntype,num,subtype,chan,aux
118+
83119
def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
84120
""" Read a WFDB annotation file recordname.annot and return the fields as lists or arrays
85121
@@ -189,46 +225,16 @@ def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
189225
chan = chan[0:ai].astype(int)
190226
aux = aux[0:ai]
191227

192-
# Keep the annotations in the specified range
193-
returnempty = 0
194-
195-
afterfrom = np.where(annsamp >= sampfrom)[0]
196-
if len(afterfrom) > 0:
197-
ik0 = afterfrom[0] # index keep start
198-
else: # No annotations in specified range.
199-
returnempty = 1
200-
201-
if not sampto:
202-
sampto = annsamp[-1]
203-
beforeto = np.where(annsamp <= sampto)[0]
204-
205-
if len(beforeto) > 0:
206-
ik1 = beforeto[-1]
207-
else:
208-
returnempty = 1
209-
210-
if returnempty:
211-
annsamp = []
212-
anntype = []
213-
num = []
214-
subtype = []
215-
chan = []
216-
aux = []
217-
print("No annotations in specified sample range")
218-
else:
219-
annsamp = annsamp[ik0:ik1 + 1]
220-
anntype = anntype[ik0:ik1 + 1]
221-
num = num[ik0:ik1 + 1]
222-
subtype = subtype[ik0:ik1 + 1]
223-
chan = chan[ik0:ik1 + 1]
224-
aux = aux[ik0:ik1 + 1]
228+
# Apply annotation range (from X to Y)
229+
annsamp,anntype,num,subtype,chan,aux = apply_annotation_range(annsamp,
230+
sampfrom,sampto,anntype,num,subtype,chan,aux)
225231

226-
# Return the annotation types as symbols or strings depending on input
227-
# parameter
228-
if anndisp == 1:
229-
anntype = [annsyms[code] for code in anntype]
230-
elif anndisp == 2:
231-
anntype = [anncodes[code] for code in anntype]
232+
# Return the annotation types as symbols or strings depending on input
233+
# parameter
234+
if anndisp == 1:
235+
anntype = [annsyms[code] for code in anntype]
236+
elif anndisp == 2:
237+
anntype = [anncodes[code] for code in anntype]
232238

233239
return (annsamp, anntype, subtype, chan, num, aux, annfs)
234240

0 commit comments

Comments
 (0)