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