Skip to content

Commit 09c3ab7

Browse files
committed
pep8. ref #17
1 parent 5050d61 commit 09c3ab7

File tree

2 files changed

+167
-132
lines changed

2 files changed

+167
-132
lines changed

tests/test_readannot.py

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,100 @@
11
import numpy as np
22
from wfdb import rdann
33
import re
4-
4+
5+
56
class test_rdann():
6-
7-
# Test 1 - Annotation file 100.atr
8-
# Target file created with: rdann -r sampledata/100 -a atr > anntarget1
7+
8+
# Test 1 - Annotation file 100.atr
9+
# Target file created with: rdann -r sampledata/100 -a atr > anntarget1
910
def test_1(self):
10-
11+
1112
# Read data from WFDB python package
12-
annsamp, anntype, subtype, chan, num, aux, annfs=rdann('sampledata/100', 'atr')
13-
aux[0]='(N' # This is not the fault of the script. The annotation file specifies a length 3
14-
# aux field with a null written after '(N' which the script correctly picks up. I am just
15-
# getting rid of the null in this unit test to compare with the regexp output below which has
16-
# no null to detect in the output text file of rdann.
13+
annsamp, anntype, subtype, chan, num, aux, annfs = rdann(
14+
'sampledata/100', 'atr')
15+
# This is not the fault of the script. The annotation file specifies a
16+
# length 3
17+
aux[0] = '(N'
18+
# aux field with a null written after '(N' which the script correctly picks up. I am just
19+
# getting rid of the null in this unit test to compare with the regexp output below which has
20+
# no null to detect in the output text file of rdann.
1721

1822
# Target data from WFDB software package
19-
lines=tuple(open('tests/targetoutputdata/anntarget1', 'r'))
20-
nannot=len(lines)
23+
lines = tuple(open('tests/targetoutputdata/anntarget1', 'r'))
24+
nannot = len(lines)
2125

22-
Ttime=[None]*nannot
23-
Tannsamp=np.empty(nannot, dtype='object')
24-
Tanntype=[None]*nannot
25-
Tsubtype=np.empty(nannot, dtype='object')
26-
Tchan=np.empty(nannot, dtype='object')
27-
Tnum=np.empty(nannot, dtype='object')
28-
Taux=[None]*nannot
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
2933

30-
RXannot=re.compile('[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<annsamp>\d+) +(?P<anntype>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux>.*)')
34+
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>.*)')
3136

3237
for i in range(0, nannot):
33-
Ttime[i], Tannsamp[i], Tanntype[i], Tsubtype[i], Tchan[i], Tnum[i], Taux[i]=RXannot.findall(lines[i])[0]
38+
Ttime[i], Tannsamp[i], Tanntype[i], Tsubtype[i], Tchan[
39+
i], Tnum[i], Taux[i] = RXannot.findall(lines[i])[0]
3440

3541
# Convert objects into integers
36-
Tannsamp=Tannsamp.astype('int')
37-
Tnum=Tnum.astype('int')
38-
Tsubtype=Tsubtype.astype('int')
39-
Tchan=Tchan.astype('int')
42+
Tannsamp = Tannsamp.astype('int')
43+
Tnum = Tnum.astype('int')
44+
Tsubtype = Tsubtype.astype('int')
45+
Tchan = Tchan.astype('int')
4046

4147
# Compare
42-
comp=[np.array_equal(annsamp, Tannsamp), np.array_equal(anntype, Tanntype), np.array_equal(subtype, Tsubtype), np.array_equal(chan, Tchan), np.array_equal(num, Tnum), aux==Taux]
48+
comp = [
49+
np.array_equal(
50+
annsamp, Tannsamp), np.array_equal(
51+
anntype, Tanntype), np.array_equal(
52+
subtype, Tsubtype), np.array_equal(
53+
chan, Tchan), np.array_equal(
54+
num, Tnum), aux == Taux]
55+
56+
assert (comp == [True] * 6)
4357

44-
assert (comp==[True]*6)
45-
46-
47-
# Test 2 - Annotation file 12726.anI with many aux strings.
48-
# Target file created with: rdann -r sampledata/100 -a atr > anntarget2
58+
# Test 2 - Annotation file 12726.anI with many aux strings.
59+
# Target file created with: rdann -r sampledata/100 -a atr > anntarget2
4960
def test_2(self):
50-
61+
5162
# Read data from WFDB python package
52-
annsamp, anntype, subtype, chan, num, aux, annfs=rdann('sampledata/12726', 'anI')
63+
annsamp, anntype, subtype, chan, num, aux, annfs = rdann(
64+
'sampledata/12726', 'anI')
5365

5466
# Target data from WFDB software package
55-
lines=tuple(open('tests/targetoutputdata/anntarget2', 'r'))
56-
nannot=len(lines)
67+
lines = tuple(open('tests/targetoutputdata/anntarget2', 'r'))
68+
nannot = len(lines)
5769

58-
Ttime=[None]*nannot
59-
Tannsamp=np.empty(nannot, dtype='object')
60-
Tanntype=[None]*nannot
61-
Tsubtype=np.empty(nannot, dtype='object')
62-
Tchan=np.empty(nannot, dtype='object')
63-
Tnum=np.empty(nannot, dtype='object')
64-
Taux=[None]*nannot
70+
Ttime = [None] * nannot
71+
Tannsamp = np.empty(nannot, dtype='object')
72+
Tanntype = [None] * nannot
73+
Tsubtype = np.empty(nannot, dtype='object')
74+
Tchan = np.empty(nannot, dtype='object')
75+
Tnum = np.empty(nannot, dtype='object')
76+
Taux = [None] * nannot
6577

66-
RXannot=re.compile('[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<annsamp>\d+) +(?P<anntype>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux>.*)')
78+
RXannot = re.compile(
79+
'[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<annsamp>\d+) +(?P<anntype>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux>.*)')
6780

6881
for i in range(0, nannot):
69-
Ttime[i], Tannsamp[i], Tanntype[i], Tsubtype[i], Tchan[i], Tnum[i], Taux[i]=RXannot.findall(lines[i])[0]
82+
Ttime[i], Tannsamp[i], Tanntype[i], Tsubtype[i], Tchan[
83+
i], Tnum[i], Taux[i] = RXannot.findall(lines[i])[0]
7084

7185
# Convert objects into integers
72-
Tannsamp=Tannsamp.astype('int')
73-
Tnum=Tnum.astype('int')
74-
Tsubtype=Tsubtype.astype('int')
75-
Tchan=Tchan.astype('int')
86+
Tannsamp = Tannsamp.astype('int')
87+
Tnum = Tnum.astype('int')
88+
Tsubtype = Tsubtype.astype('int')
89+
Tchan = Tchan.astype('int')
7690

7791
# Compare
78-
comp=[np.array_equal(annsamp, Tannsamp), np.array_equal(anntype, Tanntype), np.array_equal(subtype, Tsubtype), np.array_equal(chan, Tchan), np.array_equal(num, Tnum), aux==Taux]
92+
comp = [
93+
np.array_equal(
94+
annsamp, Tannsamp), np.array_equal(
95+
anntype, Tanntype), np.array_equal(
96+
subtype, Tsubtype), np.array_equal(
97+
chan, Tchan), np.array_equal(
98+
num, Tnum), aux == Taux]
7999

80-
assert (comp==[True]*6)
81-
100+
assert (comp == [True] * 6)

tests/test_readsignal.py

Lines changed: 97 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,141 @@
11
import numpy as np
22
from wfdb import rdsamp
3-
3+
4+
45
class test_rdsamp():
5-
6-
# Test 1 - Format 212/Entire signal/Physical
7-
# Target file created with: rdsamp -r sampledata/100 -P | cut -f 2- > target1
6+
7+
# Test 1 - Format 212/Entire signal/Physical
8+
# Target file created with: rdsamp -r sampledata/100 -P | cut -f 2- >
9+
# target1
810
def test_1(self):
9-
sig, fields=rdsamp('sampledata/100')
10-
sig=np.round(sig, decimals=8)
11-
targetsig=np.genfromtxt('tests/targetoutputdata/target1')
11+
sig, fields = rdsamp('sampledata/100')
12+
sig = np.round(sig, decimals=8)
13+
targetsig = np.genfromtxt('tests/targetoutputdata/target1')
1214
assert np.array_equal(sig, targetsig)
13-
14-
# Test 2 - Format 212/Selected Duration/Selected Channel/Digital.
15-
# Target file created with: rdsamp -r sampledata/100 -f 0.002 -t 30 -s 1 | cut -f 2- > target2
16-
def test_2(self):
17-
sig, fields=rdsamp('sampledata/100', sampfrom=1, sampto=10800, channels=[1], physical=0)
18-
targetsig=np.genfromtxt('tests/targetoutputdata/target2')
19-
targetsig=targetsig.reshape(len(targetsig), 1)
15+
16+
# Test 2 - Format 212/Selected Duration/Selected Channel/Digital.
17+
# Target file created with: rdsamp -r sampledata/100 -f 0.002 -t 30 -s 1 |
18+
# cut -f 2- > target2
19+
def test_2(self):
20+
sig, fields = rdsamp('sampledata/100', sampfrom=1,
21+
sampto=10800, channels=[1], physical=0)
22+
targetsig = np.genfromtxt('tests/targetoutputdata/target2')
23+
targetsig = targetsig.reshape(len(targetsig), 1)
2024
assert np.array_equal(sig, targetsig)
21-
25+
2226
# Test 3 - Format 16/Entire signal/Digital
23-
# Target file created with: rdsamp -r sampledata/test01_00s | cut -f 2- > target3
27+
# Target file created with: rdsamp -r sampledata/test01_00s | cut -f 2- >
28+
# target3
2429
def test_3(self):
25-
sig, fields=rdsamp('sampledata/test01_00s', physical=0)
26-
targetsig=np.genfromtxt('tests/targetoutputdata/target3')
30+
sig, fields = rdsamp('sampledata/test01_00s', physical=0)
31+
targetsig = np.genfromtxt('tests/targetoutputdata/target3')
2732
assert np.array_equal(sig, targetsig)
28-
33+
2934
# Test 4 - Format 16 with byte offset/Selected Duration/Selected Channels/Physical
30-
# Target file created with: rdsamp -r sampledata/a103l -f 50 -t 160 -s 2 0 -P | cut -f 2- > target4
35+
# Target file created with: rdsamp -r sampledata/a103l -f 50 -t 160 -s 2 0
36+
# -P | cut -f 2- > target4
3137
def test_4(self):
32-
sig, fields=rdsamp('sampledata/a103l', sampfrom=12500, sampto=40000, channels=[2, 0])
33-
sig=np.round(sig, decimals=8)
34-
targetsig=np.genfromtxt('tests/targetoutputdata/target4')
35-
assert np.array_equal(sig, targetsig)
36-
38+
sig, fields = rdsamp('sampledata/a103l',
39+
sampfrom=12500, sampto=40000, channels=[2, 0])
40+
sig = np.round(sig, decimals=8)
41+
targetsig = np.genfromtxt('tests/targetoutputdata/target4')
42+
assert np.array_equal(sig, targetsig)
43+
3744
# Test 5 - Format 16 with byte offset/Selected Duration/Selected Channels/Digital
38-
# Target file created with: rdsamp -r sampledata/a103l -f 80 -s 0 1 | cut -f 2- > target5
45+
# Target file created with: rdsamp -r sampledata/a103l -f 80 -s 0 1 | cut
46+
# -f 2- > target5
3947
def test_5(self):
40-
sig, fields=rdsamp('sampledata/a103l', sampfrom=20000, physical=0, channels=[0, 1])
41-
targetsig=np.genfromtxt('tests/targetoutputdata/target5')
42-
assert np.array_equal(sig, targetsig)
43-
48+
sig, fields = rdsamp('sampledata/a103l',
49+
sampfrom=20000, physical=0, channels=[0, 1])
50+
targetsig = np.genfromtxt('tests/targetoutputdata/target5')
51+
assert np.array_equal(sig, targetsig)
52+
4453
# Test 6 - Format 80/Selected Duration/Selected Channels/Physical
45-
# Target file created with: rdsamp -r sampledata/3000003_0003 -f 1 -t 8 -s 1 -P | cut -f 2- > target6
54+
# Target file created with: rdsamp -r sampledata/3000003_0003 -f 1 -t 8 -s
55+
# 1 -P | cut -f 2- > target6
4656
def test_6(self):
47-
sig, fields=rdsamp('sampledata/3000003_0003', sampfrom=125, sampto=1000, channels=[1])
48-
sig=np.round(sig, decimals=8)
49-
targetsig=np.genfromtxt('tests/targetoutputdata/target6')
50-
targetsig=targetsig.reshape(len(targetsig), 1)
51-
assert np.array_equal(sig, targetsig)
52-
57+
sig, fields = rdsamp('sampledata/3000003_0003',
58+
sampfrom=125, sampto=1000, channels=[1])
59+
sig = np.round(sig, decimals=8)
60+
targetsig = np.genfromtxt('tests/targetoutputdata/target6')
61+
targetsig = targetsig.reshape(len(targetsig), 1)
62+
assert np.array_equal(sig, targetsig)
63+
5364
# Test 7 - Multi-dat/Entire signal/Digital
54-
# Target file created with: rdsamp -r sampledata/s0010_re | cut -f 2- > target7
65+
# Target file created with: rdsamp -r sampledata/s0010_re | cut -f 2- >
66+
# target7
5567
def test_7(self):
56-
sig, fields=rdsamp('sampledata/s0010_re', physical=0)
57-
targetsig=np.genfromtxt('tests/targetoutputdata/target7')
58-
assert np.array_equal(sig, targetsig)
59-
68+
sig, fields = rdsamp('sampledata/s0010_re', physical=0)
69+
targetsig = np.genfromtxt('tests/targetoutputdata/target7')
70+
assert np.array_equal(sig, targetsig)
71+
6072
# Test 8 - Multi-dat/Selected Duration/Selected Channels/Physical
61-
# Target file created with: rdsamp -r sampledata/s0010_re -f 5 -t 38 -P -s 13 0 4 8 3 | cut -f 2- > target8
73+
# Target file created with: rdsamp -r sampledata/s0010_re -f 5 -t 38 -P -s
74+
# 13 0 4 8 3 | cut -f 2- > target8
6275
def test_8(self):
63-
sig, fields=rdsamp('sampledata/s0010_re', sampfrom=5000, sampto=38000, channels=[13, 0, 4, 8, 3])
64-
sig=np.round(sig, decimals=8)
65-
targetsig=np.genfromtxt('tests/targetoutputdata/target8')
66-
assert np.array_equal(sig, targetsig)
67-
76+
sig, fields = rdsamp('sampledata/s0010_re', sampfrom=5000,
77+
sampto=38000, channels=[13, 0, 4, 8, 3])
78+
sig = np.round(sig, decimals=8)
79+
targetsig = np.genfromtxt('tests/targetoutputdata/target8')
80+
assert np.array_equal(sig, targetsig)
81+
6882
# Test 9 - Format 12 multi-samples per frame and skew/Entire Signal/Digital
69-
# Target file created with: rdsamp -r sampledata/03700181 | cut -f 2- > target9
83+
# Target file created with: rdsamp -r sampledata/03700181 | cut -f 2- >
84+
# target9
7085
def test_9(self):
71-
sig, fields=rdsamp('sampledata/03700181', physical=0)
72-
sig=sig[:-4,:] # The WFDB library rdsamp does not return the final N samples for all channels due to the skew.
73-
# The WFDB python rdsamp does return the final N samples, filling in NANs for end of skewed channels only.
74-
targetsig=np.genfromtxt('tests/targetoutputdata/target9')
86+
sig, fields = rdsamp('sampledata/03700181', physical=0)
87+
# The WFDB library rdsamp does not return the final N samples for all
88+
# channels due to the skew.
89+
sig = sig[:-4, :]
90+
# The WFDB python rdsamp does return the final N samples, filling in
91+
# NANs for end of skewed channels only.
92+
targetsig = np.genfromtxt('tests/targetoutputdata/target9')
7593
assert np.array_equal(sig, targetsig)
76-
94+
7795
# Test 10 - Format 12 multi-samples per frame and skew/Selected Duration/Selected Channels/Physical
78-
# Target file created with: rdsamp -r sampledata/03700181 -f 8 -t 128 -s 0 2 -P | cut -f 2- > target10
96+
# Target file created with: rdsamp -r sampledata/03700181 -f 8 -t 128 -s 0
97+
# 2 -P | cut -f 2- > target10
7998
def test_10(self):
80-
sig, fields=rdsamp('sampledata/03700181', channels=[0,2], sampfrom=1000, sampto=16000)
81-
sig=np.round(sig, decimals=8)
82-
targetsig=np.genfromtxt('tests/targetoutputdata/target10')
99+
sig, fields = rdsamp('sampledata/03700181',
100+
channels=[0, 2], sampfrom=1000, sampto=16000)
101+
sig = np.round(sig, decimals=8)
102+
targetsig = np.genfromtxt('tests/targetoutputdata/target10')
83103
assert np.array_equal(sig, targetsig)
84-
85-
86-
#### Temporarily removing multi-segment tests due to differences in function. See iss14 #####
87-
104+
105+
#### Temporarily removing multi-segment tests due to differences in functi
106+
88107
# Test 11 - Multi-segment variable layout/Entire signal/Physical
89108
# Target file created with: rdsamp -r sampledata/matched/s25047/s25047-2704-05-04-10-44 -P | cut -f 2- > target11
90-
#def test_11(self):
109+
# def test_11(self):
91110
#sig, fields=rdsamp('sampledata/matched/s25047/s25047-2704-05-04-10-44')
92111
#sig=np.round(sig, decimals=8)
93-
#targetsig=np.genfromtxt('tests/targetoutputdata/target11')
112+
# targetsig=np.genfromtxt('tests/targetoutputdata/target11')
94113
#assert np.array_equal(sig, targetsig)
95-
114+
96115
# Test 12 - Multi-segment variable layout/Selected duration/Selected Channels/Physical
97116
# Target file created with: rdsamp -r sampledata/matched/s00001/s00001-2896-10-10-00-31 -f 70 -t 4000 -s 3 0 -P | cut -f 2- > target12
98-
#def test_12(self):
117+
# def test_12(self):
99118
#sig, fields=rdsamp('sampledata/matched/s00001/s00001-2896-10-10-00-31', sampfrom=8750, sampto=500000, channels=[3, 0])
100119
#sig=np.round(sig, decimals=8)
101-
#targetsig=np.genfromtxt('tests/targetoutputdata/target12')
120+
# targetsig=np.genfromtxt('tests/targetoutputdata/target12')
102121
#assert np.array_equal(sig, targetsig)
103-
122+
104123
#################
105-
124+
106125
# Test 13 - Format 310/Selected Duration/Digital
107126
# Target file created with: rdsamp -r sampledata/3000003_0003 -f 0 -t 8.21 | cut -f 2- | wrsamp -o 310derive -O 310
108127
# rdsamp -r 310derive -f 0.007 | cut -f 2- > target13
109128
def test_13(self):
110-
sig, fields=rdsamp('sampledata/310derive', sampfrom=2, physical=0)
111-
targetsig=np.genfromtxt('tests/targetoutputdata/target13')
112-
assert np.array_equal(sig, targetsig)
113-
129+
sig, fields = rdsamp('sampledata/310derive', sampfrom=2, physical=0)
130+
targetsig = np.genfromtxt('tests/targetoutputdata/target13')
131+
assert np.array_equal(sig, targetsig)
132+
114133
# Test 14 - Format 311/Selected Duration/Physical
115134
# Target file created with: rdsamp -r sampledata/3000003_0003 -f 0 -t 8.21 -s 1 | cut -f 2- | wrsamp -o 311derive -O 311
116135
# rdsamp -r 311derive -f 0.005 -t 3.91 -P | cut -f 2- > target14
117136
def test_14(self):
118-
sig, fields=rdsamp('sampledata/311derive', sampfrom=1, sampto=978)
119-
sig=np.round(sig, decimals=8)
120-
targetsig=np.genfromtxt('tests/targetoutputdata/target14')
121-
targetsig=targetsig.reshape([977,1])
122-
assert np.array_equal(sig, targetsig)
123-
124-
125-
137+
sig, fields = rdsamp('sampledata/311derive', sampfrom=1, sampto=978)
138+
sig = np.round(sig, decimals=8)
139+
targetsig = np.genfromtxt('tests/targetoutputdata/target14')
140+
targetsig = targetsig.reshape([977, 1])
141+
assert np.array_equal(sig, targetsig)

0 commit comments

Comments
 (0)