Skip to content

Commit 9cf1a8f

Browse files
committed
replace urlretrieve with requests
1 parent 42187ac commit 9cf1a8f

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

wfdb/_rdann.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
3434
if sampto and sampto<=sampfrom:
3535
raise ValueError("sampto must be greater than sampfrom")
3636

37-
#fields=readheader(recordname) # Get the info from the header file
37+
# Get the info from the header file
38+
#fields=readheader(recordname)
3839
dirname, baserecordname=os.path.split(recordname)
39-
40-
f=open(recordname+'.'+annot, 'rb')
41-
filebytes=np.fromfile(f, '<u1').reshape([-1, 2]) # Read the file's byte pairs.
42-
f.close()
40+
41+
# Read the file's byte pairs.
42+
with open(recordname+'.'+annot, 'rb') as f:
43+
filebytes=np.fromfile(f, '<u1').reshape([-1, 2])
4344

4445
# Allocate for the maximum possible number of annotations contained in the file.
45-
annsamp=np.zeros(filebytes.shape[0])
46-
anntype=np.zeros(filebytes.shape[0])
47-
subtype=np.zeros(filebytes.shape[0])
48-
chan=np.zeros(filebytes.shape[0])
49-
num=np.zeros(filebytes.shape[0])
46+
annsamp=np.empty(filebytes.shape[0])
47+
anntype=np.empty(filebytes.shape[0])
48+
subtype=np.empty(filebytes.shape[0])
49+
chan=np.empty(filebytes.shape[0])
50+
num=np.empty(filebytes.shape[0])
5051
aux=['']*filebytes.shape[0]
51-
52-
annfs=[] # Stores the fs written in the annotation file if it exists.
52+
annfs=[] # Stores the frequencies written in the annotation file if it exists.
5353
ai=0 # Annotation index, the number of annotations processed. Not to be comfused with the 'num' field of an annotation.
5454
bpi=0 # Byte pair index, for searching through bytes of the annotation file.
5555

@@ -165,7 +165,6 @@ def rdann(recordname, annot, sampfrom=0, sampto=[], anndisp=1):
165165
elif anndisp==2:
166166
anntype=[anncodes[code] for code in anntype]
167167

168-
169168
return (annsamp, anntype, subtype, chan, num, aux, annfs)
170169

171170

wfdb/_rdsamp.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import math
55
import sys
66
import configparser
7-
from urllib import urlretrieve
87
import configparser
8+
import requests
99

1010
def checkrecordfiles(recordname, filedirectory):
1111
"""Check a local directory along with the database cache directory specified in 'config.ini' for all necessary files required to read a WFDB record. Calls pbdownload.dlrecordfiles to download any missing files into the database cache directory. Returns the base record name if all files were present, or a full path record name specifying where the downloaded files are to be read, and a list of files downloaded.
@@ -132,8 +132,12 @@ def dlrecordfiles(pbrecname, targetdir):
132132
if not os.path.isfile(os.path.join(targetdir, baserecname+".hea")):
133133
# Not calling dlorexit here. Extra instruction of removing the faulty created directory.
134134
try:
135-
urlretrieve("http://physionet.org/physiobank/database/"+pbrecname+".hea", os.path.join(targetdir, baserecname+".hea"))
136-
dledfiles.append(os.path.join(targetdir, baserecname+".hea"))
135+
remotefile = "http://physionet.org/physiobank/database/"+pbrecname+".hea"
136+
targetfile = os.path.join(targetdir, baserecname+".hea")
137+
r = requests.get(remotefile)
138+
with open(targetfile, "w") as text_file:
139+
text_file.write(r.text)
140+
dledfiles.append(targetfile)
137141
except HTTPError:
138142
if madetargetdir:
139143
os.rmdir(targetdir) # Remove the recently created faulty directory.
@@ -165,7 +169,9 @@ def dlrecordfiles(pbrecname, targetdir):
165169
# Helper function for dlrecordfiles. Download the file from the specified 'url' as the 'filename', or exit with warning.
166170
def dlorexit(url, filename, dledfiles):
167171
try:
168-
urlretrieve(url, filename)
172+
r = requests.get(url)
173+
with open(filename, "w") as text_file:
174+
text_file.write(r.text)
169175
dledfiles.append(filename)
170176
return dledfiles
171177
except HTTPError:

0 commit comments

Comments
 (0)