@@ -27,104 +27,75 @@ def dlrecordfiles(pbrecname, targetdir):
27
27
physioneturl = "http://physionet.org/physiobank/database/"
28
28
pbdir , baserecname = os .path .split (pbrecname )
29
29
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
33
33
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
+
39
36
# For any missing file, check if the input physiobank record name is
40
37
# valid, ie whether the file exists on physionet. Download if valid, exit
41
38
# 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
+
62
41
fields = readheader (os .path .join (targetdir , baserecname ))
63
42
64
- # Even if the header file exists, it could have been downloaded prior.
65
43
# Need to check validity of link if ANY file is missing.
66
44
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 )
80
48
else : # Multi segment. Check for all segment headers and their dat files
81
49
for segment in fields ["filename" ]:
82
50
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 )
100
53
segfields = readheader (os .path .join (targetdir , segment ))
101
- for f in segfields ["filename" ]:
54
+ for f in set ( segfields ["filename" ]) :
102
55
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
+
111
59
if dledfiles :
112
- print ('Downloaded all files for record' )
60
+ print ('Downloaded all missing files for record. ' )
113
61
return dledfiles # downloaded files
114
62
115
63
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.\n Remove 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 = []):
120
92
if displaydlmsg : # We want this message to be called once for all files downloaded.
121
93
print ('Downloading missing file(s) into directory: {}' .format (targetdir ))
122
94
try :
123
95
r = requests .get (url )
124
96
with open (filename , "w" ) as text_file :
125
97
text_file .write (r .text )
126
- dledfiles .append (filename )
127
- return dledfiles
98
+ return filename
128
99
except requests .HTTPError :
129
100
sys .exit ("Attempted to download invalid target file: " + url )
130
101
0 commit comments