1
1
import numpy as np
2
2
import re
3
3
import os
4
+ import posixpath
4
5
import requests
5
-
6
+
6
7
# Read a header file from physiobank
7
8
def streamheader (recordname , pbdir ):
8
9
9
10
# Full url of header location
10
- url = os . path .join (dbindexurl , pbdir , recordname + '.hea' )
11
+ url = posixpath .join (dbindexurl , pbdir , recordname + '.hea' )
11
12
r = requests .get (url )
12
-
13
+
13
14
# Raise HTTPError if invalid url
14
15
r .raise_for_status ()
15
-
16
+
16
17
# Get each line as a string
17
18
filelines = r .content .decode ('ascii' ).splitlines ()
18
-
19
+
19
20
# Separate content into header and comment lines
20
21
headerlines = []
21
22
commentlines = []
22
-
23
+
23
24
for line in filelines :
24
25
line = line .strip ()
25
26
# Comment line
26
27
if line .startswith ('#' ):
27
28
commentlines .append (line )
28
29
# Non-empty non-comment line = header line.
29
- elif line :
30
+ elif line :
30
31
# Look for a comment in the line
31
32
ci = line .find ('#' )
32
33
if ci > 0 :
@@ -35,25 +36,25 @@ def streamheader(recordname, pbdir):
35
36
commentlines .append (line [ci :])
36
37
else :
37
38
headerlines .append (line )
38
-
39
- return (headerlines , commentlines )
39
+
40
+ return (headerlines , commentlines )
40
41
41
42
# Read certain bytes from a dat file from physiobank
42
43
def streamdat (filename , pbdir , fmt , bytecount , startbyte , datatypes ):
43
-
44
+
44
45
# Full url of dat file
45
- url = os . path .join (dbindexurl , pbdir , filename )
46
+ url = posixpath .join (dbindexurl , pbdir , filename )
46
47
47
48
# Specify the byte range
48
- endbyte = startbyte + bytecount - 1
49
- headers = {"Range" : "bytes=" + str (startbyte )+ "-" + str (endbyte ), 'Accept-Encoding' : '*/*' }
50
-
49
+ endbyte = startbyte + bytecount - 1
50
+ headers = {"Range" : "bytes=" + str (startbyte )+ "-" + str (endbyte ), 'Accept-Encoding' : '*/*' }
51
+
51
52
# Get the content
52
53
r = requests .get (url , headers = headers , stream = True )
53
-
54
+
54
55
# Raise HTTPError if invalid url
55
56
r .raise_for_status ()
56
-
57
+
57
58
sigbytes = r .content
58
59
59
60
# Convert to numpy array
@@ -70,13 +71,13 @@ def streamdat(filename, pbdir, fmt, bytecount, startbyte, datatypes):
70
71
def streamannotation (filename , pbdir ):
71
72
72
73
# Full url of annotation file
73
- url = os . path .join (dbindexurl , pbdir , filename )
74
+ url = posixpath .join (dbindexurl , pbdir , filename )
74
75
75
76
# Get the content
76
77
r = requests .get (url )
77
78
# Raise HTTPError if invalid url
78
79
r .raise_for_status ()
79
-
80
+
80
81
annbytes = r .content
81
82
82
83
# Convert to numpy array
@@ -88,11 +89,11 @@ def streamannotation(filename, pbdir):
88
89
89
90
def getdblist ():
90
91
"""Return a list of all the physiobank databases available.
91
-
92
+
92
93
Usage:
93
94
dblist = getdblist()
94
95
"""
95
- url = os . path .join (dbindexurl , 'DBS' )
96
+ url = posixpath .join (dbindexurl , 'DBS' )
96
97
r = requests .get (url )
97
98
98
99
dblist = r .content .decode ('ascii' ).splitlines ()
@@ -107,7 +108,7 @@ def getdblist():
107
108
def getrecordlist (dburl , records ):
108
109
# Check for a RECORDS file
109
110
if records == 'all' :
110
- r = requests .get (os . path .join (dburl , 'RECORDS' ))
111
+ r = requests .get (posixpath .join (dburl , 'RECORDS' ))
111
112
if r .status_code == 404 :
112
113
raise ValueError ('The database ' + dburl + ' has no WFDB files to download' )
113
114
@@ -120,10 +121,10 @@ def getrecordlist(dburl, records):
120
121
return recordlist
121
122
122
123
def getannotators (dburl , annotators ):
123
-
124
+
124
125
if annotators is not None :
125
126
# Check for an ANNOTATORS file
126
- r = requests .get (os . path .join (dburl , 'ANNOTATORS' ))
127
+ r = requests .get (posixpath .join (dburl , 'ANNOTATORS' ))
127
128
if r .status_code == 404 :
128
129
if annotators == 'all' :
129
130
return
@@ -141,7 +142,7 @@ def getannotators(dburl, annotators):
141
142
# In case they didn't input a list
142
143
if type (annotators ) == str :
143
144
annotators = [annotators ]
144
- # user input ones. Check validity.
145
+ # user input ones. Check validity.
145
146
for a in annotators :
146
147
if a not in annlist :
147
148
raise ValueError ('The database contains no annotators with extension: ' + a )
@@ -173,25 +174,25 @@ def dlpbfile(inputs):
173
174
basefile , subdir , pbdb , dlbasedir , keepsubdirs , overwrite = inputs
174
175
175
176
# Full url of file
176
- url = os . path .join (dbindexurl , pbdb , subdir , basefile )
177
-
177
+ url = posixpath .join (dbindexurl , pbdb , subdir , basefile )
178
+
178
179
# Get the request header
179
180
rh = requests .head (url , headers = {'Accept-Encoding' : 'identity' })
180
181
# Raise HTTPError if invalid url
181
182
rh .raise_for_status ()
182
183
183
184
# Supposed size of the file
184
185
onlinefilesize = int (rh .headers ['content-length' ])
185
-
186
+
186
187
# Figure out where the file should be locally
187
188
if keepsubdirs :
188
189
dldir = os .path .join (dlbasedir , subdir )
189
190
else :
190
191
dldir = dlbasedir
191
-
192
+
192
193
localfile = os .path .join (dldir , basefile )
193
-
194
- # The file exists locally.
194
+
195
+ # The file exists locally.
195
196
if os .path .isfile (localfile ):
196
197
# Redownload regardless
197
198
if overwrite :
@@ -202,33 +203,33 @@ def dlpbfile(inputs):
202
203
# Local file is smaller than it should be. Append it.
203
204
if localfilesize < onlinefilesize :
204
205
print ('Detected partially downloaded file: ' + localfile + ' Appending file...' )
205
- headers = {"Range" : "bytes=" + str (localfilesize )+ "-" , 'Accept-Encoding' : '*/*' }
206
+ headers = {"Range" : "bytes=" + str (localfilesize )+ "-" , 'Accept-Encoding' : '*/*' }
206
207
r = requests .get (url , headers = headers , stream = True )
207
208
print ('headers: ' , headers )
208
209
print ('r content length: ' , len (r .content ))
209
210
with open (localfile , "ba" ) as writefile :
210
211
writefile .write (r .content )
211
212
print ('Done appending.' )
212
- # Local file is larger than it should be. Redownload.
213
+ # Local file is larger than it should be. Redownload.
213
214
elif localfilesize > onlinefilesize :
214
215
dlfullfile (url , localfile )
215
- # If they're the same size, do nothing.
216
-
217
- # The file doesn't exist. Download it.
216
+ # If they're the same size, do nothing.
217
+
218
+ # The file doesn't exist. Download it.
218
219
else :
219
220
dlfullfile (url , localfile )
220
-
221
+
221
222
return
222
223
223
- # Download a file. No checks.
224
+ # Download a file. No checks.
224
225
def dlfullfile (url , localfile ):
225
226
r = requests .get (url )
226
227
with open (localfile , "wb" ) as writefile :
227
228
writefile .write (r .content )
228
-
229
+
229
230
return
230
231
231
232
232
233
233
234
234
- dbindexurl = 'http://physionet.org/physiobank/database/'
235
+ dbindexurl = 'http://physionet.org/physiobank/database/'
0 commit comments