44import sys
55import requests
66from IPython .display import display
7-
7+
88# Read a header file from physiobank
99def streamheader (recordname , pbdir ):
1010
1111 # Full url of header location
1212 url = os .path .join (dbindexurl , pbdir , recordname + '.hea' )
1313 r = requests .get (url )
14-
14+
1515 # Raise HTTPError if invalid url
1616 r .raise_for_status ()
17-
17+
1818 # Get each line as a string
1919 filelines = r .content .decode ('ascii' ).splitlines ()
20-
20+
2121 # Separate content into header and comment lines
2222 headerlines = []
2323 commentlines = []
24-
24+
2525 for line in filelines :
2626 line = line .strip ()
2727 # Comment line
2828 if line .startswith ('#' ):
2929 commentlines .append (line )
3030 # Non-empty non-comment line = header line.
31- elif line :
31+ elif line :
3232 # Look for a comment in the line
3333 ci = line .find ('#' )
3434 if ci > 0 :
@@ -37,25 +37,25 @@ def streamheader(recordname, pbdir):
3737 commentlines .append (line [ci :])
3838 else :
3939 headerlines .append (line )
40-
41- return (headerlines , commentlines )
40+
41+ return (headerlines , commentlines )
4242
4343# Read certain bytes from a dat file from physiobank
4444def streamdat (filename , pbdir , fmt , bytecount , startbyte , datatypes ):
45-
45+
4646 # Full url of dat file
4747 url = os .path .join (dbindexurl , pbdir , filename )
4848
4949 # 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+
5353 # Get the content
5454 r = requests .get (url , headers = headers , stream = True )
55-
55+
5656 # Raise HTTPError if invalid url
5757 r .raise_for_status ()
58-
58+
5959 sigbytes = r .content
6060
6161 # Convert to numpy array
@@ -78,7 +78,7 @@ def streamannotation(filename, pbdir):
7878 r = requests .get (url )
7979 # Raise HTTPError if invalid url
8080 r .raise_for_status ()
81-
81+
8282 annbytes = r .content
8383
8484 # Convert to numpy array
@@ -90,7 +90,7 @@ def streamannotation(filename, pbdir):
9090
9191def getdblist ():
9292 """Return a list of all the physiobank databases available.
93-
93+
9494 Usage:
9595 dblist = getdblist()
9696 """
@@ -122,7 +122,7 @@ def getrecordlist(dburl, records):
122122 return recordlist
123123
124124def getannotators (dburl , annotators ):
125-
125+
126126 if annotators is not None :
127127 # Check for an ANNOTATORS file
128128 r = requests .get (os .path .join (dburl , 'ANNOTATORS' ))
@@ -143,7 +143,7 @@ def getannotators(dburl, annotators):
143143 # In case they didn't input a list
144144 if type (annotators ) == str :
145145 annotators = [annotators ]
146- # user input ones. Check validity.
146+ # user input ones. Check validity.
147147 for a in annotators :
148148 if a not in annlist :
149149 sys .exit ('The database contains no annotators with extension: ' , a )
@@ -176,24 +176,24 @@ def dlpbfile(inputs):
176176
177177 # Full url of file
178178 url = os .path .join (dbindexurl , pbdb , subdir , basefile )
179-
179+
180180 # Get the request header
181181 rh = requests .head (url , headers = {'Accept-Encoding' : 'identity' })
182182 # Raise HTTPError if invalid url
183183 rh .raise_for_status ()
184184
185185 # Supposed size of the file
186186 onlinefilesize = int (rh .headers ['content-length' ])
187-
187+
188188 # Figure out where the file should be locally
189189 if keepsubdirs :
190190 dldir = os .path .join (dlbasedir , subdir )
191191 else :
192192 dldir = dlbasedir
193-
193+
194194 localfile = os .path .join (dldir , basefile )
195-
196- # The file exists locally.
195+
196+ # The file exists locally.
197197 if os .path .isfile (localfile ):
198198 # Redownload regardless
199199 if overwrite :
@@ -204,33 +204,33 @@ def dlpbfile(inputs):
204204 # Local file is smaller than it should be. Append it.
205205 if localfilesize < onlinefilesize :
206206 print ('Detected partially downloaded file: ' + localfile + ' Appending file...' )
207- headers = {"Range" : "bytes=" + str (localfilesize )+ "-" , 'Accept-Encoding' : '*/*' }
207+ headers = {"Range" : "bytes=" + str (localfilesize )+ "-" , 'Accept-Encoding' : '*/*' }
208208 r = requests .get (url , headers = headers , stream = True )
209209 print ('headers: ' , headers )
210210 print ('r content length: ' , len (r .content ))
211211 with open (localfile , "ba" ) as writefile :
212212 writefile .write (r .content )
213213 print ('Done appending.' )
214- # Local file is larger than it should be. Redownload.
214+ # Local file is larger than it should be. Redownload.
215215 elif localfilesize > onlinefilesize :
216216 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.
220220 else :
221221 dlfullfile (url , localfile )
222-
222+
223223 return
224224
225- # Download a file. No checks.
225+ # Download a file. No checks.
226226def dlfullfile (url , localfile ):
227227 r = requests .get (url )
228228 with open (localfile , "wb" ) as writefile :
229229 writefile .write (r .content )
230-
230+
231231 return
232232
233233
234234
235235
236- dbindexurl = 'http://physionet.org/physiobank/database/'
236+ dbindexurl = 'http://physionet.org/physiobank/database/'
0 commit comments