Skip to content
This repository was archived by the owner on Jun 14, 2018. It is now read-only.

Commit 2500e38

Browse files
authored
Merge pull request MIT-LCP#42 from kbrose/windows-compatibility
Windows compatibility
2 parents f58e942 + b8ba1f3 commit 2500e38

File tree

2 files changed

+226
-229
lines changed

2 files changed

+226
-229
lines changed

wfdb/downloads.py

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
import numpy as np
22
import re
33
import os
4+
import posixpath
45
import requests
5-
6+
67
# Read a header file from physiobank
78
def streamheader(recordname, pbdir):
89

910
# Full url of header location
10-
url = os.path.join(dbindexurl, pbdir, recordname+'.hea')
11+
url = posixpath.join(dbindexurl, pbdir, recordname+'.hea')
1112
r = requests.get(url)
12-
13+
1314
# Raise HTTPError if invalid url
1415
r.raise_for_status()
15-
16+
1617
# Get each line as a string
1718
filelines = r.content.decode('ascii').splitlines()
18-
19+
1920
# Separate content into header and comment lines
2021
headerlines = []
2122
commentlines = []
22-
23+
2324
for line in filelines:
2425
line = line.strip()
2526
# Comment line
2627
if line.startswith('#'):
2728
commentlines.append(line)
2829
# Non-empty non-comment line = header line.
29-
elif line:
30+
elif line:
3031
# Look for a comment in the line
3132
ci = line.find('#')
3233
if ci > 0:
@@ -35,25 +36,25 @@ def streamheader(recordname, pbdir):
3536
commentlines.append(line[ci:])
3637
else:
3738
headerlines.append(line)
38-
39-
return (headerlines, commentlines)
39+
40+
return (headerlines, commentlines)
4041

4142
# Read certain bytes from a dat file from physiobank
4243
def streamdat(filename, pbdir, fmt, bytecount, startbyte, datatypes):
43-
44+
4445
# Full url of dat file
45-
url = os.path.join(dbindexurl, pbdir, filename)
46+
url = posixpath.join(dbindexurl, pbdir, filename)
4647

4748
# 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+
5152
# Get the content
5253
r = requests.get(url, headers=headers, stream=True)
53-
54+
5455
# Raise HTTPError if invalid url
5556
r.raise_for_status()
56-
57+
5758
sigbytes = r.content
5859

5960
# Convert to numpy array
@@ -70,13 +71,13 @@ def streamdat(filename, pbdir, fmt, bytecount, startbyte, datatypes):
7071
def streamannotation(filename, pbdir):
7172

7273
# Full url of annotation file
73-
url = os.path.join(dbindexurl, pbdir, filename)
74+
url = posixpath.join(dbindexurl, pbdir, filename)
7475

7576
# Get the content
7677
r = requests.get(url)
7778
# Raise HTTPError if invalid url
7879
r.raise_for_status()
79-
80+
8081
annbytes = r.content
8182

8283
# Convert to numpy array
@@ -88,11 +89,11 @@ def streamannotation(filename, pbdir):
8889

8990
def getdblist():
9091
"""Return a list of all the physiobank databases available.
91-
92+
9293
Usage:
9394
dblist = getdblist()
9495
"""
95-
url = os.path.join(dbindexurl, 'DBS')
96+
url = posixpath.join(dbindexurl, 'DBS')
9697
r = requests.get(url)
9798

9899
dblist = r.content.decode('ascii').splitlines()
@@ -107,7 +108,7 @@ def getdblist():
107108
def getrecordlist(dburl, records):
108109
# Check for a RECORDS file
109110
if records == 'all':
110-
r = requests.get(os.path.join(dburl, 'RECORDS'))
111+
r = requests.get(posixpath.join(dburl, 'RECORDS'))
111112
if r.status_code == 404:
112113
raise ValueError('The database '+dburl+' has no WFDB files to download')
113114

@@ -120,10 +121,10 @@ def getrecordlist(dburl, records):
120121
return recordlist
121122

122123
def getannotators(dburl, annotators):
123-
124+
124125
if annotators is not None:
125126
# Check for an ANNOTATORS file
126-
r = requests.get(os.path.join(dburl, 'ANNOTATORS'))
127+
r = requests.get(posixpath.join(dburl, 'ANNOTATORS'))
127128
if r.status_code == 404:
128129
if annotators == 'all':
129130
return
@@ -141,7 +142,7 @@ def getannotators(dburl, annotators):
141142
# In case they didn't input a list
142143
if type(annotators) == str:
143144
annotators = [annotators]
144-
# user input ones. Check validity.
145+
# user input ones. Check validity.
145146
for a in annotators:
146147
if a not in annlist:
147148
raise ValueError('The database contains no annotators with extension: '+a)
@@ -173,25 +174,25 @@ def dlpbfile(inputs):
173174
basefile, subdir, pbdb, dlbasedir, keepsubdirs, overwrite = inputs
174175

175176
# Full url of file
176-
url = os.path.join(dbindexurl, pbdb, subdir, basefile)
177-
177+
url = posixpath.join(dbindexurl, pbdb, subdir, basefile)
178+
178179
# Get the request header
179180
rh = requests.head(url, headers={'Accept-Encoding': 'identity'})
180181
# Raise HTTPError if invalid url
181182
rh.raise_for_status()
182183

183184
# Supposed size of the file
184185
onlinefilesize = int(rh.headers['content-length'])
185-
186+
186187
# Figure out where the file should be locally
187188
if keepsubdirs:
188189
dldir = os.path.join(dlbasedir, subdir)
189190
else:
190191
dldir = dlbasedir
191-
192+
192193
localfile = os.path.join(dldir, basefile)
193-
194-
# The file exists locally.
194+
195+
# The file exists locally.
195196
if os.path.isfile(localfile):
196197
# Redownload regardless
197198
if overwrite:
@@ -202,33 +203,33 @@ def dlpbfile(inputs):
202203
# Local file is smaller than it should be. Append it.
203204
if localfilesize < onlinefilesize:
204205
print('Detected partially downloaded file: '+localfile+' Appending file...')
205-
headers = {"Range": "bytes="+str(localfilesize)+"-", 'Accept-Encoding': '*/*'}
206+
headers = {"Range": "bytes="+str(localfilesize)+"-", 'Accept-Encoding': '*/*'}
206207
r = requests.get(url, headers=headers, stream=True)
207208
print('headers: ', headers)
208209
print('r content length: ', len(r.content))
209210
with open(localfile, "ba") as writefile:
210211
writefile.write(r.content)
211212
print('Done appending.')
212-
# Local file is larger than it should be. Redownload.
213+
# Local file is larger than it should be. Redownload.
213214
elif localfilesize > onlinefilesize:
214215
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.
218219
else:
219220
dlfullfile(url, localfile)
220-
221+
221222
return
222223

223-
# Download a file. No checks.
224+
# Download a file. No checks.
224225
def dlfullfile(url, localfile):
225226
r = requests.get(url)
226227
with open(localfile, "wb") as writefile:
227228
writefile.write(r.content)
228-
229+
229230
return
230231

231232

232233

233234

234-
dbindexurl = 'http://physionet.org/physiobank/database/'
235+
dbindexurl = 'http://physionet.org/physiobank/database/'

0 commit comments

Comments
 (0)