Skip to content

Commit 646af3e

Browse files
committed
fix unit tests
1 parent a01dbe9 commit 646af3e

File tree

5 files changed

+112
-99
lines changed

5 files changed

+112
-99
lines changed

tests/test_annotations.py

Lines changed: 80 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,59 @@ def test_1(self):
1414

1515
# This is not the fault of the script. The annotation file specifies a
1616
# length 3
17-
annotation.aux[0] = '(N'
18-
# aux field with a null written after '(N' which the script correctly picks up. I am just
17+
annotation.aux_note[0] = '(N'
18+
# aux_note field with a null written after '(N' which the script correctly picks up. I am just
1919
# getting rid of the null in this unit test to compare with the regexp output below which has
2020
# no null to detect in the output text file of rdann.
2121

2222
# Target data from WFDB software package
2323
lines = tuple(open('tests/targetoutputdata/anntarget1', 'r'))
2424
nannot = len(lines)
2525

26-
Ttime = [None] * nannot
27-
Tannsamp = np.empty(nannot, dtype='object')
28-
Tanntype = [None] * nannot
29-
Tsubtype = np.empty(nannot, dtype='object')
30-
Tchan = np.empty(nannot, dtype='object')
31-
Tnum = np.empty(nannot, dtype='object')
32-
Taux = [None] * nannot
26+
target_time = [None] * nannot
27+
target_sample = np.empty(nannot, dtype='object')
28+
target_symbol = [None] * nannot
29+
target_subtype = np.empty(nannot, dtype='object')
30+
target_chan = np.empty(nannot, dtype='object')
31+
target_num = np.empty(nannot, dtype='object')
32+
target_aux_note = [None] * nannot
3333

3434
RXannot = re.compile(
35-
'[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<annsamp>\d+) +(?P<anntype>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux>.*)')
35+
'[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<sample>\d+) +(?P<symbol>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux_note>.*)')
3636

3737
for i in range(0, nannot):
38-
Ttime[i], Tannsamp[i], Tanntype[i], Tsubtype[i], Tchan[
39-
i], Tnum[i], Taux[i] = RXannot.findall(lines[i])[0]
38+
target_time[i], target_sample[i], target_symbol[i], target_subtype[i], target_chan[
39+
i], target_num[i], target_aux_note[i] = RXannot.findall(lines[i])[0]
4040

4141
# Convert objects into integers
42-
Tannsamp = Tannsamp.astype('int')
43-
Tnum = Tnum.astype('int')
44-
Tsubtype = Tsubtype.astype('int')
45-
Tchan = Tchan.astype('int')
42+
target_sample = target_sample.astype('int')
43+
target_num = target_num.astype('int')
44+
target_subtype = target_subtype.astype('int')
45+
target_chan = target_chan.astype('int')
4646

4747
# Compare
48-
comp = [np.array_equal(annotation.annsamp, Tannsamp),
49-
np.array_equal(annotation.anntype, Tanntype),
50-
np.array_equal(annotation.subtype, Tsubtype),
51-
np.array_equal(annotation.chan, Tchan),
52-
np.array_equal(annotation.num, Tnum),
53-
annotation.aux == Taux]
48+
comp = [np.array_equal(annotation.sample, target_sample),
49+
np.array_equal(annotation.symbol, target_symbol),
50+
np.array_equal(annotation.subtype, target_subtype),
51+
np.array_equal(annotation.chan, target_chan),
52+
np.array_equal(annotation.num, target_num),
53+
annotation.aux_note == target_aux_note]
5454

5555
# Test file streaming
56-
pbannotation = wfdb.rdann('100', 'atr', pbdir = 'mitdb')
57-
pbannotation.aux[0] = '(N'
56+
pbannotation = wfdb.rdann('100', 'atr', pbdir = 'mitdb', return_label_elements=['label_store', 'symbol'])
57+
pbannotation.aux_note[0] = '(N'
58+
pbannotation.create_label_map()
5859

5960
# Test file writing
6061
annotation.wrann(writefs=True)
61-
annotationwrite = wfdb.rdann('100', 'atr')
62+
writeannotation = wfdb.rdann('100', 'atr', return_label_elements=['label_store', 'symbol'])
63+
writeannotation.create_label_map()
6264

6365
assert (comp == [True] * 6)
6466
assert annotation.__eq__(pbannotation)
65-
assert annotation.__eq__(annotationwrite)
67+
assert annotation.__eq__(writeannotation)
6668

67-
# Test 2 - Annotation file 12726.anI with many aux strings.
69+
# Test 2 - Annotation file 12726.anI with many aux_note strings.
6870
# Target file created with: rdann -r sampledata/100 -a atr > anntarget2
6971
def test_2(self):
7072

@@ -75,44 +77,46 @@ def test_2(self):
7577
lines = tuple(open('tests/targetoutputdata/anntarget2', 'r'))
7678
nannot = len(lines)
7779

78-
Ttime = [None] * nannot
79-
Tannsamp = np.empty(nannot, dtype='object')
80-
Tanntype = [None] * nannot
81-
Tsubtype = np.empty(nannot, dtype='object')
82-
Tchan = np.empty(nannot, dtype='object')
83-
Tnum = np.empty(nannot, dtype='object')
84-
Taux = [None] * nannot
80+
target_time = [None] * nannot
81+
target_sample = np.empty(nannot, dtype='object')
82+
target_symbol = [None] * nannot
83+
target_subtype = np.empty(nannot, dtype='object')
84+
target_chan = np.empty(nannot, dtype='object')
85+
target_num = np.empty(nannot, dtype='object')
86+
target_aux_note = [None] * nannot
8587

8688
RXannot = re.compile(
87-
'[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<annsamp>\d+) +(?P<anntype>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux>.*)')
89+
'[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<sample>\d+) +(?P<symbol>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux_note>.*)')
8890

8991
for i in range(0, nannot):
90-
Ttime[i], Tannsamp[i], Tanntype[i], Tsubtype[i], Tchan[
91-
i], Tnum[i], Taux[i] = RXannot.findall(lines[i])[0]
92+
target_time[i], target_sample[i], target_symbol[i], target_subtype[i], target_chan[
93+
i], target_num[i], target_aux_note[i] = RXannot.findall(lines[i])[0]
9294

9395
# Convert objects into integers
94-
Tannsamp = Tannsamp.astype('int')
95-
Tnum = Tnum.astype('int')
96-
Tsubtype = Tsubtype.astype('int')
97-
Tchan = Tchan.astype('int')
96+
target_sample = target_sample.astype('int')
97+
target_num = target_num.astype('int')
98+
target_subtype = target_subtype.astype('int')
99+
target_chan = target_chan.astype('int')
98100

99101
# Compare
100-
comp = [np.array_equal(annotation.annsamp, Tannsamp),
101-
np.array_equal(annotation.anntype, Tanntype),
102-
np.array_equal(annotation.subtype, Tsubtype),
103-
np.array_equal(annotation.chan, Tchan),
104-
np.array_equal(annotation.num, Tnum),
105-
annotation.aux == Taux]
102+
comp = [np.array_equal(annotation.sample, target_sample),
103+
np.array_equal(annotation.symbol, target_symbol),
104+
np.array_equal(annotation.subtype, target_subtype),
105+
np.array_equal(annotation.chan, target_chan),
106+
np.array_equal(annotation.num, target_num),
107+
annotation.aux_note == target_aux_note]
106108
# Test file streaming
107-
pbannotation = wfdb.rdann('12726', 'anI', pbdir = 'prcp')
109+
pbannotation = wfdb.rdann('12726', 'anI', pbdir = 'prcp', return_label_elements=['label_store', 'symbol'])
110+
pbannotation.create_label_map()
108111

109112
# Test file writing
110113
annotation.wrann(writefs=True)
111-
annotationwrite = wfdb.rdann('12726', 'anI')
114+
writeannotation = wfdb.rdann('12726', 'anI', return_label_elements=['label_store', 'symbol'])
115+
writeannotation.create_label_map()
112116

113117
assert (comp == [True] * 6)
114118
assert annotation.__eq__(pbannotation)
115-
assert annotation.__eq__(annotationwrite)
119+
assert annotation.__eq__(writeannotation)
116120

117121
# Test 3 - Annotation file 1003.atr with custom annotation types
118122
# Target file created with: rdann -r sampledata/1003 -a atr > anntarget3
@@ -125,42 +129,44 @@ def test_3(self):
125129
lines = tuple(open('tests/targetoutputdata/anntarget3', 'r'))
126130
nannot = len(lines)
127131

128-
Ttime = [None] * nannot
129-
Tannsamp = np.empty(nannot, dtype='object')
130-
Tanntype = [None] * nannot
131-
Tsubtype = np.empty(nannot, dtype='object')
132-
Tchan = np.empty(nannot, dtype='object')
133-
Tnum = np.empty(nannot, dtype='object')
134-
Taux = [None] * nannot
132+
target_time = [None] * nannot
133+
target_sample = np.empty(nannot, dtype='object')
134+
target_symbol = [None] * nannot
135+
target_subtype = np.empty(nannot, dtype='object')
136+
target_chan = np.empty(nannot, dtype='object')
137+
target_num = np.empty(nannot, dtype='object')
138+
target_aux_note = [None] * nannot
135139

136140
RXannot = re.compile(
137-
'[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<annsamp>\d+) +(?P<anntype>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux>.*)')
141+
'[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<sample>\d+) +(?P<symbol>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux_note>.*)')
138142

139143
for i in range(0, nannot):
140-
Ttime[i], Tannsamp[i], Tanntype[i], Tsubtype[i], Tchan[
141-
i], Tnum[i], Taux[i] = RXannot.findall(lines[i])[0]
144+
target_time[i], target_sample[i], target_symbol[i], target_subtype[i], target_chan[
145+
i], target_num[i], target_aux_note[i] = RXannot.findall(lines[i])[0]
142146

143147
# Convert objects into integers
144-
Tannsamp = Tannsamp.astype('int')
145-
Tnum = Tnum.astype('int')
146-
Tsubtype = Tsubtype.astype('int')
147-
Tchan = Tchan.astype('int')
148+
target_sample = target_sample.astype('int')
149+
target_num = target_num.astype('int')
150+
target_subtype = target_subtype.astype('int')
151+
target_chan = target_chan.astype('int')
148152

149153
# Compare
150-
comp = [np.array_equal(annotation.annsamp, Tannsamp),
151-
np.array_equal(annotation.anntype, Tanntype),
152-
np.array_equal(annotation.subtype, Tsubtype),
153-
np.array_equal(annotation.chan, Tchan),
154-
np.array_equal(annotation.num, Tnum),
155-
annotation.aux == Taux]
154+
comp = [np.array_equal(annotation.sample, target_sample),
155+
np.array_equal(annotation.symbol, target_symbol),
156+
np.array_equal(annotation.subtype, target_subtype),
157+
np.array_equal(annotation.chan, target_chan),
158+
np.array_equal(annotation.num, target_num),
159+
annotation.aux_note == target_aux_note]
156160

157161
# Test file streaming
158-
pbannotation = wfdb.rdann('1003', 'atr', pbdir = 'challenge/2014/set-p2')
159-
162+
pbannotation = wfdb.rdann('1003', 'atr', pbdir = 'challenge/2014/set-p2', return_label_elements=['label_store', 'symbol'])
163+
pbannotation.create_label_map()
164+
160165
# Test file writing
161166
annotation.wrann(writefs=True)
162-
annotationwrite = wfdb.rdann('1003', 'atr')
167+
writeannotation = wfdb.rdann('1003', 'atr', return_label_elements=['label_store', 'symbol'])
168+
writeannotation.create_label_map()
163169

164170
assert (comp == [True] * 6)
165171
assert annotation.__eq__(pbannotation)
166-
assert annotation.__eq__(annotationwrite)
172+
assert annotation.__eq__(writeannotation)

tests/test_processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_7(self):
7272
min_gap = fs*60/min_bpm
7373
max_gap = fs*60/max_bpm
7474

75-
y_idxs = wfdb.processing.correct_peaks(sig[:,0], ann.annsamp, min_gap, max_gap, smooth_window=150)
75+
y_idxs = wfdb.processing.correct_peaks(sig[:,0], ann.sample, min_gap, max_gap, smooth_window=150)
7676

7777
yz = numpy.zeros(sig.shape[0])
7878
yz[y_idxs] = 1

wfdb/plot/plots.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def plotrec(record=None, title = None, annotation = None, timeunits='samples', s
1919
- record (required): A wfdb Record object. The p_signals attribute will be plotted.
2020
- title (default=None): A string containing the title of the graph.
2121
- annotation (default=None): A list of Annotation objects or numpy arrays. The locations of the Annotation
22-
objects' 'annsamp' attribute, or the locations of the numpy arrays' values, will be overlaid on the signals.
22+
objects' 'sample' attribute, or the locations of the numpy arrays' values, will be overlaid on the signals.
2323
The list index of the annotation item corresponds to the signal channel that each annotation set will be
2424
plotted on. For channels without annotations to plot, put None in the list. This argument may also be just
2525
an Annotation object or numpy array, which will be plotted over channel 0.
@@ -255,7 +255,7 @@ def checkplotitems(record, title, annotation, timeunits, sigstyle, annstyle):
255255

256256
# Move single channel annotations to channel 0
257257
if type(annotation) == annotations.Annotation:
258-
annplot[0] = annotation.annsamp
258+
annplot[0] = annotation.sample
259259
elif type(annotation) == np.ndarray:
260260
annplot[0] = annotation
261261
# Ready list.
@@ -267,7 +267,7 @@ def checkplotitems(record, title, annotation, timeunits, sigstyle, annstyle):
267267
# Check elements. Copy over to new list.
268268
for ch in range(record.nsig):
269269
if type(annotation[ch]) == annotations.Annotation:
270-
annplot[ch] = annotation[ch].annsamp
270+
annplot[ch] = annotation[ch].sample
271271
elif type(annotation[ch]) == np.ndarray:
272272
annplot[ch] = annotation[ch]
273273
elif annotation[ch] is None:
@@ -307,7 +307,7 @@ def plotann(annotation, title = None, timeunits = 'samples', returnfig = False):
307307
Usage: plotann(annotation, title = None, timeunits = 'samples', returnfig = False)
308308
309309
Input arguments:
310-
- annotation (required): An Annotation object. The annsamp attribute locations will be overlaid on the signal.
310+
- annotation (required): An Annotation object. The sample attribute locations will be overlaid on the signal.
311311
- title (default=None): A string containing the title of the graph.
312312
- timeunits (default='samples'): String specifying the x axis unit.
313313
Allowed options are: 'samples', 'seconds', 'minutes', and 'hours'.
@@ -368,13 +368,13 @@ def checkannplotitems(annotation, title, timeunits):
368368

369369
# Get x axis values to plot
370370
if timeunits == 'samples':
371-
plotvals = annotation.annsamp
371+
plotvals = annotation.sample
372372
elif timeunits == 'seconds':
373-
plotvals = annotation.annsamp/annotation.fs
373+
plotvals = annotation.sample/annotation.fs
374374
elif timeunits == 'minutes':
375-
plotvals = annotation.annsamp/(annotation.fs*60)
375+
plotvals = annotation.sample/(annotation.fs*60)
376376
elif timeunits == 'hours':
377-
plotvals = annotation.annsamp/(annotation.fs*3600)
377+
plotvals = annotation.sample/(annotation.fs*3600)
378378

379379
# title
380380
if title is not None and type(title) != str:

wfdb/processing/basic.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
from wfdb import Annotation
55

66

7-
def resample_ann(tt, annsamp):
7+
def resample_ann(tt, sample):
88
# tt: numpy.array as returned by signal.resample
9-
# annsamp: numpy.array containing indexes of annotations (Annotation.annsamp)
9+
# sample: numpy.array containing indexes of annotations (Annotation.sample)
1010

1111
# Compute the new annotation indexes
1212

1313
tmp = numpy.zeros(len(tt), dtype='int16')
1414
j = 0
1515
tprec = tt[j]
16-
for i, v in enumerate(annsamp):
16+
for i, v in enumerate(sample):
1717
while True:
1818
d = False
1919
if v < tprec:
@@ -41,7 +41,7 @@ def resample_ann(tt, annsamp):
4141
for i in idx:
4242
for j in range(tmp[i]):
4343
res.append(i)
44-
assert len(res) == len(annsamp)
44+
assert len(res) == len(sample)
4545
return numpy.asarray(res, dtype='int64')
4646

4747

@@ -74,10 +74,10 @@ def resample_singlechan(x, ann, fs, fs_target):
7474

7575
xx, tt = resample_sig(x, fs, fs_target)
7676

77-
new_annsamp = resample_ann(tt, ann.annsamp)
78-
assert ann.annsamp.shape == new_annsamp.shape
77+
new_sample = resample_ann(tt, ann.sample)
78+
assert ann.sample.shape == new_sample.shape
7979

80-
new_ann = Annotation(ann.recordname, ann.annotator, new_annsamp, ann.anntype, ann.num, ann.subtype, ann.chan, ann.aux, ann.fs)
80+
new_ann = Annotation(ann.recordname, ann.extension, new_sample, ann.symbol, ann.num, ann.subtype, ann.chan, ann.aux_note, ann.fs)
8181
return xx, new_ann
8282

8383

@@ -100,10 +100,10 @@ def resample_multichan(xs, ann, fs, fs_target, resamp_ann_chan=0):
100100
if chan == resamp_ann_chan:
101101
lt = tt
102102

103-
new_annsamp = resample_ann(lt, ann.annsamp)
104-
assert ann.annsamp.shape == new_annsamp.shape
103+
new_sample = resample_ann(lt, ann.sample)
104+
assert ann.sample.shape == new_sample.shape
105105

106-
new_ann = Annotation(ann.recordname, ann.annotator, new_annsamp, ann.anntype, ann.num, ann.subtype, ann.chan, ann.aux, ann.fs)
106+
new_ann = Annotation(ann.recordname, ann.extension, new_sample, ann.symbol, ann.num, ann.subtype, ann.chan, ann.aux_note, ann.fs)
107107
return numpy.column_stack(lx), new_ann
108108

109109

0 commit comments

Comments
 (0)