Skip to content

Commit c07edf1

Browse files
committed
modifying wrsamp helper functions
1 parent 765de93 commit c07edf1

File tree

6 files changed

+654
-370
lines changed

6 files changed

+654
-370
lines changed

devtests.ipynb

Lines changed: 45 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -37,124 +37,6 @@
3737
"print(sig[-20:,:])"
3838
]
3939
},
40-
{
41-
"cell_type": "code",
42-
"execution_count": null,
43-
"metadata": {
44-
"collapsed": false,
45-
"scrolled": true
46-
},
47-
"outputs": [],
48-
"source": [
49-
"# Testing rdsamp format 80\n",
50-
"sig, fields=wfdb.srdsamp('sampledata/3000003_0003')\n",
51-
"print(sig)\n",
52-
"print(fields)"
53-
]
54-
},
55-
{
56-
"cell_type": "code",
57-
"execution_count": null,
58-
"metadata": {
59-
"collapsed": false
60-
},
61-
"outputs": [],
62-
"source": [
63-
"# Testing rdsamp format 16 with byte offset (actually a .mat file too)\n",
64-
"sig, fields=wfdb.srdsamp('sampledata/a103l') \n",
65-
"#print(sig)\n",
66-
"#print(sig.shape)\n",
67-
"#print(fields)\n",
68-
"\n",
69-
"from wfdb import plotwfdb\n",
70-
"\n",
71-
"plotwfdb.plotsigs(sig, fields)\n",
72-
"\n",
73-
"\n",
74-
"# With channel selection and sampfrom sampto\n",
75-
"#sig, fields=readsignal.rdsamp('sampledata/a103l', channels=[2,0], sampfrom=250, sampto=80000) \n",
76-
"#print('\\n\\n')\n",
77-
"#print(sig)\n",
78-
"#print(fields)"
79-
]
80-
},
81-
{
82-
"cell_type": "code",
83-
"execution_count": null,
84-
"metadata": {
85-
"collapsed": false
86-
},
87-
"outputs": [],
88-
"source": []
89-
},
90-
{
91-
"cell_type": "code",
92-
"execution_count": null,
93-
"metadata": {
94-
"collapsed": false
95-
},
96-
"outputs": [],
97-
"source": [
98-
"# For writing test annotation files based on first 3 annotations of 12726.anI\n",
99-
"\n",
100-
"f=open('tmp.fake', 'wb')\n",
101-
"\n",
102-
"indata= np.array([0, 236, 1, 0, 200, 84, 0, 88, 255, 251, 21, 252, 73, 110, 105, 116,\n",
103-
" 105, 97, 116, 101, 32, 115, 108, 111, 119, 32, 116, 105, 108, 116, 32, 117,\n",
104-
" 112, 0, 0, 236, 0, 0, 67, 50, 0, 88, 21, 252, 67, 111, 110, 99,\n",
105-
"108, 117, 100, 101, 32, 115, 108, 111, 119, 32, 116, 105, 108, 116, 32, 117,\n",
106-
"112, 0, 0, 236, 0, 0, 114, 183, 0, 88, 23, 252, 73, 110, 105, 116,\n",
107-
"105, 97, 116, 101, 32, 115, 108, 111, 119, 32, 116, 105, 108, 116, 32, 100,\n",
108-
"111, 119, 110, 0, 0 ,0 ]).astype('u1')\n",
109-
"\n",
110-
"\n",
111-
"print(indata)\n",
112-
"\n",
113-
"indata.tofile(f)\n",
114-
"\n",
115-
"\n",
116-
"f.close()\n"
117-
]
118-
},
119-
{
120-
"cell_type": "code",
121-
"execution_count": null,
122-
"metadata": {
123-
"collapsed": false
124-
},
125-
"outputs": [],
126-
"source": [
127-
"# Trying to fix byte pointer for reading skewed signals. \n",
128-
"\n",
129-
"from wfdb import readsignal\n",
130-
"import numpy as np\n",
131-
"#sig, fields=readsignal.rdsamp('sampledata/03700181', channels=[0,2], sampfrom=1000, sampto=16000)\n",
132-
"#sig=np.round(sig, decimals=8)\n",
133-
"#targetsig=np.genfromtxt('tests/targetoutputdata/target10')\n",
134-
"\n",
135-
"sig, fields=readsignal.rdsamp('sampledata/03700181', sampfrom=1000, sampto=16000, physical=0) # fs=125. -f 8 -t 128\n",
136-
"\n",
137-
"print(\"\\nsig: \", sig)\n",
138-
"print('\\n\\n')\n",
139-
"print(\"End of sig: \", sig[-10:,:])\n",
140-
"\n",
141-
"\n",
142-
"# nbytesread = 15000 frames * 6 samples/frame * 1.5 bytes/sample = 135000\n",
143-
"\n",
144-
"# Try to octal dump the right section of the file. After 16000 frames x (6 samples/frame) = 96000 samples * (1.5 bytes per sample)\n",
145-
"# = 144000 bytes skip. \n",
146-
"\n",
147-
"\n",
148-
"\n",
149-
"# od -t u1 03700181.dat -j 144000 -N 30 \n",
150-
"\n",
151-
"# Result: \n",
152-
"# 236 255 236 2 240 236 163 27 61 236 255 236 236 15 2 147\n",
153-
"# 27 40 236 255 236 236 255 236 130 27 19 2 240 236\n",
154-
"\n",
155-
"\n"
156-
]
157-
},
15840
{
15941
"cell_type": "code",
16042
"execution_count": null,
@@ -220,15 +102,56 @@
220102
},
221103
{
222104
"cell_type": "code",
223-
"execution_count": null,
105+
"execution_count": 18,
224106
"metadata": {
225107
"collapsed": false
226108
},
227-
"outputs": [],
109+
"outputs": [
110+
{
111+
"data": {
112+
"text/plain": [
113+
"list"
114+
]
115+
},
116+
"execution_count": 18,
117+
"metadata": {},
118+
"output_type": "execute_result"
119+
}
120+
],
121+
"source": [
122+
"type(record.initvalue)"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 19,
128+
"metadata": {
129+
"collapsed": false
130+
},
131+
"outputs": [
132+
{
133+
"data": {
134+
"text/plain": [
135+
"int"
136+
]
137+
},
138+
"execution_count": 19,
139+
"metadata": {},
140+
"output_type": "execute_result"
141+
}
142+
],
228143
"source": [
229-
"r = requests.get('http://physionet.org/physiobank/database/mitdb/100.hea')\n",
230-
"help(r.raise_for_status)"
144+
"type(record.siglen)"
231145
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {
151+
"collapsed": true
152+
},
153+
"outputs": [],
154+
"source": []
232155
}
233156
],
234157
"metadata": {

tests/test_records.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ def test_1(self):
1212
siground = np.round(sig, decimals=8)
1313
targetsig = np.genfromtxt('tests/targetoutputdata/target1')
1414

15-
# Also test data streaming from physiobank
15+
# Compare data streaming from physiobank
1616
pbsig, pbfields = wfdb.srdsamp('100', pbdir = 'mitdb')
1717
# This comment line was manually added and is not present in the original physiobank record
1818
del(fields['comments'][0])
1919

20-
assert np.array_equal(siground, targetsig) and np.array_equal(sig, pbsig) and fields == pbfields
20+
# Test file writing
21+
22+
23+
assert np.array_equal(siground, targetsig)
24+
assert np.array_equal(sig, pbsig) and fields == pbfields
2125

2226
# Test 2 - Format 212/Selected Duration/Selected Channel/Digital.
2327
# Target file created with: rdsamp -r sampledata/100 -f 0.002 -t 30 -s 1 |
@@ -29,12 +33,18 @@ def test_2(self):
2933
targetsig = np.genfromtxt('tests/targetoutputdata/target2')
3034
targetsig = targetsig.reshape(len(targetsig), 1)
3135

32-
# Also test data streaming from physiobank
33-
pbrecord= wfdb.rdsamp('100', sampfrom=1, sampto=10800, channels=[1], physical=False, pbdir = 'mitdb')
36+
# Compare data streaming from physiobank
37+
pbrecord = wfdb.rdsamp('100', sampfrom=1, sampto=10800, channels=[1], physical=False, pbdir = 'mitdb')
3438
# This comment line was manually added and is not present in the original physiobank record
3539
del(record.comments[0])
3640

37-
assert np.array_equal(sig, targetsig) and record.__eq__(pbrecord)
41+
# Test file writing
42+
#record.wrsamp()
43+
#recordwrite = wfdb.rdsamp('100')
44+
45+
assert np.array_equal(sig, targetsig)
46+
assert record.__eq__(pbrecord)
47+
#assert record.__eq__(recordwrite)
3848

3949
# Test 3 - Format 16/Entire signal/Digital
4050
# Target file created with: rdsamp -r sampledata/test01_00s | cut -f 2- >
@@ -44,10 +54,11 @@ def test_3(self):
4454
sig = record.d_signals
4555
targetsig = np.genfromtxt('tests/targetoutputdata/target3')
4656

47-
# Also test data streaming from physiobank
57+
# Compare data streaming from physiobank
4858
pbrecord = wfdb.rdsamp('test01_00s', physical=False, pbdir = 'macecgdb')
4959

50-
assert np.array_equal(sig, targetsig) and record.__eq__(record)
60+
assert np.array_equal(sig, targetsig)
61+
assert record.__eq__(record)
5162

5263
# Test 4 - Format 16 with byte offset/Selected Duration/Selected Channels/Physical
5364
# Target file created with: rdsamp -r sampledata/a103l -f 50 -t 160 -s 2 0
@@ -58,10 +69,11 @@ def test_4(self):
5869
siground = np.round(sig, decimals=8)
5970
targetsig = np.genfromtxt('tests/targetoutputdata/target4')
6071

61-
# Also test data streaming from physiobank
72+
# Compare data streaming from physiobank
6273
pbsig, pbfields = wfdb.srdsamp('a103l', pbdir = 'challenge/2015/training',
6374
sampfrom=12500, sampto=40000, channels=[2, 0])
64-
assert np.array_equal(siground, targetsig) and np.array_equal(sig, pbsig) and fields == pbfields
75+
assert np.array_equal(siground, targetsig)
76+
assert np.array_equal(sig, pbsig) and fields == pbfields
6577

6678
# Test 5 - Format 16 with byte offset/Selected Duration/Selected Channels/Digital
6779
# Target file created with: rdsamp -r sampledata/a103l -f 80 -s 0 1 | cut
@@ -72,10 +84,11 @@ def test_5(self):
7284
sig = record.d_signals
7385
targetsig = np.genfromtxt('tests/targetoutputdata/target5')
7486

75-
# Also test data streaming from physiobank
87+
# Compare data streaming from physiobank
7688
pbrecord = wfdb.rdsamp('a103l', pbdir = 'challenge/2015/training',
7789
sampfrom=20000, channels=[0, 1], physical=False)
78-
assert np.array_equal(sig, targetsig) and record.__eq__(pbrecord)
90+
assert np.array_equal(sig, targetsig)
91+
assert record.__eq__(pbrecord)
7992

8093
# Test 6 - Format 80/Selected Duration/Selected Channels/Physical
8194
# Target file created with: rdsamp -r sampledata/3000003_0003 -f 1 -t 8 -s
@@ -87,11 +100,12 @@ def test_6(self):
87100
targetsig = np.genfromtxt('tests/targetoutputdata/target6')
88101
targetsig = targetsig.reshape(len(targetsig), 1)
89102

90-
# Also test data streaming from physiobank
103+
# Compare data streaming from physiobank
91104
pbsig, pbfields = wfdb.srdsamp('3000003_0003', pbdir = 'mimic2wdb/30/3000003/',
92105
sampfrom=125, sampto=1000, channels=[1])
93106

94-
assert np.array_equal(siground, targetsig) and np.array_equal(sig, pbsig) and fields == pbfields
107+
assert np.array_equal(siground, targetsig)
108+
assert np.array_equal(sig, pbsig) and fields == pbfields
95109

96110
# Test 7 - Multi-dat/Entire signal/Digital
97111
# Target file created with: rdsamp -r sampledata/s0010_re | cut -f 2- >
@@ -101,10 +115,11 @@ def test_7(self):
101115
sig = record.d_signals
102116
targetsig = np.genfromtxt('tests/targetoutputdata/target7')
103117

104-
# Also test data streaming from physiobank
118+
# Compare data streaming from physiobank
105119
pbrecord= wfdb.rdsamp('s0010_re', physical=False, pbdir = 'ptbdb/patient001')
106120

107-
assert np.array_equal(sig, targetsig) and record.__eq__(pbrecord)
121+
assert np.array_equal(sig, targetsig)
122+
assert record.__eq__(pbrecord)
108123

109124
# Test 8 - Multi-dat/Selected Duration/Selected Channels/Physical
110125
# Target file created with: rdsamp -r sampledata/s0010_re -f 5 -t 38 -P -s
@@ -115,11 +130,12 @@ def test_8(self):
115130
siground = np.round(sig, decimals=8)
116131
targetsig = np.genfromtxt('tests/targetoutputdata/target8')
117132

118-
# Also test data streaming from physiobank
133+
# Compare data streaming from physiobank
119134
pbsig, pbfields = wfdb.srdsamp('s0010_re', sampfrom=5000, pbdir = 'ptbdb/patient001',
120135
sampto=38000, channels=[13, 0, 4, 8, 3])
121136

122-
assert np.array_equal(siground, targetsig) and np.array_equal(sig, pbsig) and fields == pbfields
137+
assert np.array_equal(siground, targetsig)
138+
assert np.array_equal(sig, pbsig) and fields == pbfields
123139

124140
# Test 9 - Format 12 multi-samples per frame and skew/Entire Signal/Digital
125141
# Target file created with: rdsamp -r sampledata/03700181 | cut -f 2- >
@@ -134,10 +150,11 @@ def test_9(self):
134150
# NANs for end of skewed channels only.
135151
targetsig = np.genfromtxt('tests/targetoutputdata/target9')
136152

137-
# Also test data streaming from physiobank
138-
record = wfdb.rdsamp('03700181', physical=False, pbdir = 'mimicdb/037')
153+
# Compare data streaming from physiobank
154+
pbrecord = wfdb.rdsamp('03700181', physical=False, pbdir = 'mimicdb/037')
139155

140-
assert np.array_equal(sig, targetsig) and record.__eq__(pbrecord)
156+
assert np.array_equal(sig, targetsig)
157+
assert record.__eq__(pbrecord)
141158

142159
# Test 10 - Format 12 multi-samples per frame and skew/Selected Duration/Selected Channels/Physical
143160
# Target file created with: rdsamp -r sampledata/03700181 -f 8 -t 128 -s 0
@@ -148,11 +165,12 @@ def test_10(self):
148165
siground = np.round(sig, decimals=8)
149166
targetsig = np.genfromtxt('tests/targetoutputdata/target10')
150167

151-
# Also test data streaming from physiobank
168+
# Compare data streaming from physiobank
152169
pbsig, pbfields = wfdb.srdsamp('03700181', pbdir = 'mimicdb/037',
153170
channels=[0, 2], sampfrom=1000, sampto=16000)
154171

155-
assert np.array_equal(siground, targetsig) and np.array_equal(sig, pbsig) and fields == pbfields
172+
assert np.array_equal(siground, targetsig)
173+
assert np.array_equal(sig, pbsig) and fields == pbfields
156174

157175
#### Temporarily removing multi-segment tests due to differences in function workings
158176

@@ -194,5 +212,3 @@ def test_14(self):
194212
assert np.array_equal(sig, targetsig)
195213

196214

197-
#def test_write1():
198-

0 commit comments

Comments
 (0)