Skip to content

Commit fb81ad1

Browse files
committed
copy prev val to func
1 parent af7f1a0 commit fb81ad1

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

wfdb/_rdann.py

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## Written by: Chen Xie 2016 ##
1+
# Written by: Chen Xie 2016
22
# Please report bugs and suggestions to
3-
# https://github.com/MIT-LCP/wfdb-python or [email protected] ###
3+
# https://github.com/MIT-LCP/wfdb-python or [email protected]
44

55
import numpy as np
66
import os
@@ -33,17 +33,44 @@ def get_sample_freq(filebytes):
3333
bpi = 0.5 * (auxlen + 12 + (auxlen & 1))
3434
return annfs,bpi
3535

36+
def copy_prev(AT,ts,filebytes,bpi,annsamp,anntype,ai):
37+
if AT == 59: # Skip.
38+
ts = ts + 65536 * filebytes[bpi + 1,0] + \
39+
16777216 * filebytes[bpi + 1,1] + \
40+
filebytes[bpi + 2,0] + 256 * filebytes[bpi + 2,1] # 4 bytes storing dt
41+
annsamp[ai] = ts
42+
# The anntype is stored after the 4 bytes. Samples here should be 0.
43+
anntype[ai] = filebytes[bpi + 3, 1] >> 2
44+
bpi = bpi + 4
45+
# Not a skip so it should be the actual samples + anntype. Should not
46+
# need to check for alternatives.
47+
else:
48+
# total samples = previous + delta samples stored in current byte
49+
# pair
50+
ts = ts + filebytes[bpi, 0] + 256 * (filebytes[bpi, 1] & 3)
51+
annsamp[ai] = ts
52+
anntype[ai] = AT
53+
bpi = bpi + 1
54+
return ts,annsamp,anntype,bpi
55+
3656
def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
3757
""" Read a WFDB annotation file recordname.annot and return the fields as lists or arrays
3858
39-
Usage: annsamp, anntype, num, subtype, chan, aux, annfs) = rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1)
59+
Usage: annsamp, anntype, num, subtype, chan, aux, annfs = rdann(recordname, annot,
60+
sampfrom=0, sampto=[],
61+
anndisp=1)
4062
4163
Input arguments:
42-
- recordname (required): The record name of the WFDB annotation file. ie. for file '100.atr', recordname='100'
43-
- annot (required): The annotator extension of the annotation file. ie. for file '100.atr', annot='atr'
64+
- recordname (required): The record name of the WFDB annotation file. ie. for
65+
file '100.atr', recordname='100'
66+
- annot (required): The annotator extension of the annotation file. ie. for
67+
file '100.atr', annot='atr'
4468
- sampfrom (default=0): The minimum sample number for annotations to be returned.
45-
- sampto (default=the final annotation sample): The maximum sample number for annotations to be returned.
46-
- anndisp (default = 1): The annotation display flag that controls the data type of the 'anntype' output parameter. 'anntype' will either be an integer key(0), a shorthand display symbol(1), or a longer annotation code.
69+
- sampto (default=the final annotation sample): The maximum sample number for
70+
annotations to be returned.
71+
- anndisp (default = 1): The annotation display flag that controls the data type
72+
of the 'anntype' output parameter. 'anntype' will either be an integer key(0),
73+
a shorthand display symbol(1), or a longer annotation code.
4774
4875
Output arguments:
4976
- annsamp: The annotation location in samples relative to the beginning of the record.
@@ -54,7 +81,8 @@ def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
5481
- aux: The auxiliary information string for the annotation.
5582
- annfs: The sampling frequency written in the beginning of the annotation file if present.
5683
57-
*NOTE: Every annotation contains the 'annsamp' and 'anntype' field. All other fields default to 0 or empty if not present.
84+
*NOTE: Every annotation contains the 'annsamp' and 'anntype' field. All
85+
other fields default to 0 or empty if not present.
5886
"""
5987

6088
if sampto and sampto <= sampfrom:
@@ -70,16 +98,13 @@ def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
7098

7199
# Allocate for the maximum possible number of annotations contained in the
72100
# file.
73-
annsamp = np.zeros(filebytes.shape[0])
74-
anntype = np.zeros(filebytes.shape[0])
75-
subtype = np.zeros(filebytes.shape[0])
76-
chan = np.zeros(filebytes.shape[0])
77-
num = np.zeros(filebytes.shape[0])
78-
aux = [''] * filebytes.shape[0]
79-
80-
# Annotation index, the number of annotations processed. Not to be
81-
# comfused with the 'num' field of an annotation.
82-
ai = 0
101+
samplelength = filebytes.shape[0]
102+
annsamp = np.zeros(samplelength)
103+
anntype = np.zeros(samplelength)
104+
subtype = np.zeros(samplelength)
105+
chan = np.zeros(samplelength)
106+
num = np.zeros(samplelength)
107+
aux = [''] * samplelength
83108

84109
# Check the beginning of the annotation file to see if it is storing the
85110
# 'time resolution' field.
@@ -89,10 +114,15 @@ def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
89114
# Annotation bytes only store dt.
90115
ts = 0
91116

92-
# Processing annotations. Sequence for one ann is: SKIP pair (if any) ->
117+
# Annotation index, the number of annotations processed. Not to be
118+
# confused with the 'num' field of an annotation.
119+
ai = 0
120+
121+
# Processing annotations. Iterate across length of sequence
122+
# Sequence for one ann is: SKIP pair (if any) ->
93123
# samp + anntype pair -> other pairs
94124
# The last byte pair is 0 indicating eof.
95-
while bpi < filebytes.shape[0] - 1:
125+
while bpi < samplelength - 1:
96126

97127
# The first byte pair will either store the actual samples + anntype,
98128
# or 0 + SKIP.
@@ -102,28 +132,10 @@ def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
102132
# the current annotation.
103133
cpychan = 1
104134
cpynum = 1
105-
if AT == 59: # Skip.
106-
ts = ts + 65536 * filebytes[bpi + 1,
107-
0] + 16777216 * filebytes[bpi + 1,
108-
1] + filebytes[bpi + 2,
109-
0] + 256 * filebytes[bpi + 2,
110-
1] # 4 bytes storing dt
111-
annsamp[ai] = ts
112-
# The anntype is stored after the 4 bytes. Samples here should be
113-
# 0.
114-
anntype[ai] = filebytes[bpi + 3, 1] >> 2
115-
bpi = bpi + 4
116-
# Not a skip so it should be the actual samples + anntype. Should not
117-
# need to check for alternatives.
118-
else:
119-
# total samples = previous + delta samples stored in current byte
120-
# pair
121-
ts = ts + filebytes[bpi, 0] + 256 * (filebytes[bpi, 1] & 3)
122-
annsamp[ai] = ts
123-
anntype[ai] = AT
124-
bpi = bpi + 1
125-
135+
ts,annsamp,anntype,bpi = copy_prev(AT,ts,filebytes,bpi,annsamp,anntype,ai)
136+
126137
AT = filebytes[bpi, 1] >> 2
138+
127139
while (AT > 59): # Process any other fields belonging to this annotation
128140
if AT == 61: # SUB
129141
# sub is interpreted as signed char. Remember to limit writing

0 commit comments

Comments
 (0)