Skip to content

Commit 651039b

Browse files
author
Chen Xie
committed
improved method of downloading files via rdsamp
1 parent d835e95 commit 651039b

File tree

2 files changed

+60
-75
lines changed

2 files changed

+60
-75
lines changed

devtests.ipynb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,20 @@
815815
"print(sig)"
816816
]
817817
},
818+
{
819+
"cell_type": "code",
820+
"execution_count": null,
821+
"metadata": {
822+
"collapsed": false
823+
},
824+
"outputs": [],
825+
"source": [
826+
"import wfdb\n",
827+
"sig, fields=wfdb.rdsamp('mitdb/101', pbdl=1, dldir='/home/cx1111/Downloads/wfdbrecords/mitdb', keepfiles=1)\n",
828+
"\n",
829+
"print(sig)"
830+
]
831+
},
818832
{
819833
"cell_type": "code",
820834
"execution_count": null,

wfdb/_rdsamp.py

Lines changed: 46 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -27,104 +27,75 @@ def dlrecordfiles(pbrecname, targetdir):
2727
physioneturl = "http://physionet.org/physiobank/database/"
2828
pbdir, baserecname = os.path.split(pbrecname)
2929
displaydlmsg=1
30-
31-
if not os.path.isdir(
32-
targetdir): # Make the target directory if it doesn't already exist
30+
dledfiles = []
31+
32+
if not os.path.isdir(targetdir): # Make the target dir if it doesn't exist
3333
os.makedirs(targetdir)
34-
madetargetdir = 1
35-
else:
36-
madetargetdir = 0
37-
dledfiles = [] # List of downloaded files
38-
34+
print("Created local directory: ", targetdir)
35+
3936
# For any missing file, check if the input physiobank record name is
4037
# valid, ie whether the file exists on physionet. Download if valid, exit
4138
# if invalid.
42-
43-
if not os.path.isfile(os.path.join(targetdir, baserecname + ".hea")):
44-
# Not calling dlorexit here. Extra instruction of removing the faulty
45-
# created directory.
46-
try:
47-
remotefile = physioneturl + pbrecname + ".hea"
48-
targetfile = os.path.join(targetdir, baserecname + ".hea")
49-
print('Downloading missing file(s) into directory: {}'.format(targetdir))
50-
r = requests.get(remotefile)
51-
displaydlmsg=0
52-
with open(targetfile, "w") as text_file:
53-
text_file.write(r.text)
54-
dledfiles.append(targetfile)
55-
except requests.HTTPError:
56-
if madetargetdir:
57-
# Remove the recently created faulty directory.
58-
os.rmdir(targetdir)
59-
sys.exit(
60-
"Attempted to download invalid target file: {}".format(remotefile))
61-
39+
dledfiles, displaydlmsg = dlifmissing(physioneturl+pbdir+"/"+baserecname+".hea", os.path.join(targetdir, baserecname+".hea"), dledfiles, displaydlmsg, targetdir)
40+
6241
fields = readheader(os.path.join(targetdir, baserecname))
6342

64-
# Even if the header file exists, it could have been downloaded prior.
6543
# Need to check validity of link if ANY file is missing.
6644
if fields["nseg"] == 1: # Single segment. Check for all the required dat files
67-
for f in fields["filename"]:
68-
if not os.path.isfile(
69-
os.path.join(
70-
targetdir,
71-
f)): # Missing a dat file
72-
dledfiles = dlorexit(
73-
physioneturl + pbdir + "/" + f,
74-
os.path.join(
75-
targetdir,
76-
f),
77-
dledfiles, displaydlmsg, targetdir)
78-
displaydlmsg=0
79-
45+
for f in set(fields["filename"]):
46+
# Missing dat file
47+
dledfiles, displaydlmsg = dlifmissing(physioneturl+pbdir+"/"+f, os.path.join(targetdir, f), dledfiles, displaydlmsg, targetdir)
8048
else: # Multi segment. Check for all segment headers and their dat files
8149
for segment in fields["filename"]:
8250
if segment != '~':
83-
if not os.path.isfile(
84-
os.path.join(
85-
targetdir,
86-
segment +
87-
".hea")): # Missing a segment header
88-
dledfiles = dlorexit(
89-
physioneturl +
90-
pbdir +
91-
"/" +
92-
segment +
93-
".hea",
94-
os.path.join(
95-
targetdir,
96-
segment +
97-
".hea"),
98-
dledfiles, displaydlmsg, targetdir)
99-
displaydlmsg=0
51+
# Check the segment header
52+
dledfiles, displaydlmsg = dlifmissing(physioneturl+pbdir+"/"+segment+".hea", os.path.join(targetdir, segment+".hea"), dledfiles, displaydlmsg, targetdir)
10053
segfields = readheader(os.path.join(targetdir, segment))
101-
for f in segfields["filename"]:
54+
for f in set(segfields["filename"]):
10255
if f != '~':
103-
if not os.path.isfile(os.path.join(targetdir, f)): # Missing a segment's dat file
104-
dledfiles = dlorexit(
105-
physioneturl + pbdir + "/" + f,
106-
os.path.join(
107-
targetdir,
108-
f),
109-
dledfiles, displaydlmsg, targetdir)
110-
displaydlmsg=0
56+
# Check the segment's dat file
57+
dledfiles, displaydlmsg = dlifmissing(physioneturl+pbdir+"/"+f, os.path.join(targetdir, f), dledfiles, displaydlmsg, targetdir)
58+
11159
if dledfiles:
112-
print('Downloaded all files for record')
60+
print('Downloaded all missing files for record.')
11361
return dledfiles # downloaded files
11462

11563

116-
# Helper function for dlrecordfiles. Download the file from the specified
117-
# 'url' as the 'filename', or exit with warning.
118-
def dlorexit(url, filename, dledfiles, displaydlmsg=0, targetdir=[]):
119-
64+
# Download a file if it is missing. Also error check 0 byte files.
65+
def dlifmissing(url, filename, dledfiles, displaydlmsg, targetdir):
66+
67+
if os.path.isfile(filename):
68+
# Likely interrupted download
69+
if os.path.getsize(filename)==0:
70+
userresponse=input("Warning - File "+filename+" is 0 bytes. Likely interrupted download.\nRemove file and redownload? [y/n] - ")
71+
while userresponse not in ['y','n']:
72+
userresponse=input("Remove file and redownload? [y/n] - ")
73+
if userresponse=='y':
74+
os.remove(filename)
75+
dledfiles.append(dlorexit(url, filename, displaydlmsg, targetdir))
76+
displaydlmsg=0
77+
else:
78+
print("Skipping download.")
79+
# File is already present.
80+
else:
81+
print("File "+filename+" is already present.")
82+
else:
83+
dledfiles.append(dlorexit(url, filename, displaydlmsg, targetdir))
84+
displaydlmsg=0
85+
86+
# If a file gets downloaded, displaydlmsg is set to 0. No need to print the message more than once.
87+
return dledfiles, displaydlmsg
88+
89+
90+
# Download the file from the specified 'url' as the 'filename', or exit with warning.
91+
def dlorexit(url, filename, displaydlmsg=0, targetdir=[]):
12092
if displaydlmsg: # We want this message to be called once for all files downloaded.
12193
print('Downloading missing file(s) into directory: {}'.format(targetdir))
12294
try:
12395
r = requests.get(url)
12496
with open(filename, "w") as text_file:
12597
text_file.write(r.text)
126-
dledfiles.append(filename)
127-
return dledfiles
98+
return filename
12899
except requests.HTTPError:
129100
sys.exit("Attempted to download invalid target file: " + url)
130101

0 commit comments

Comments
 (0)