@@ -67,8 +67,8 @@ class Disposition(_Disposition):
67
67
@classmethod
68
68
def create (cls , * , days : int , rate : float ):
69
69
"""Mandate key word arguments."""
70
- Positive (key = ' days' , value = days )
71
- Rate (key = ' rate' , value = rate )
70
+ Positive (key = " days" , value = days )
71
+ Rate (key = " rate" , value = rate )
72
72
return cls (days , rate )
73
73
74
74
@@ -124,8 +124,6 @@ def validate(string):
124
124
"current_date" : (OptionalDate , cast_date , "Date on which the forecast should be based" ),
125
125
"date_first_hospitalized" : (OptionalDate , cast_date , "Date the first patient was hospitalized" ),
126
126
"doubling_time" : (OptionalStrictlyPositive , float , "Doubling time before social distancing (days)" ),
127
- "hospitalized" : (ValDisposition , None , None ),
128
- "icu" : (ValDisposition , None , None ),
129
127
"infectious_days" : (StrictlyPositive , int , "Infectious days" ),
130
128
"mitigation_date" : (OptionalDate , cast_date , "Date on which social distancing measures too effect" ),
131
129
"market_share" : (Rate , float , "Hospital market share (0.00001 - 1.0)" ),
@@ -136,16 +134,166 @@ def validate(string):
136
134
"region" : (OptionalValue , None , "No help available" ),
137
135
"relative_contact_rate" : (Rate , float , "Social distancing reduction rate: 0.0 - 1.0" ),
138
136
"ventilated" : (ValDisposition , None , None ),
137
+ "hospitalized" : (ValDisposition , None , None ),
138
+ "icu" : (ValDisposition , None , None ),
139
139
}
140
140
141
141
142
- class FromFile (Action ):
143
- """From File."""
144
-
145
- def __call__ (self , parser , namespace , values , option_string = None ):
146
- logger .info ('Using file: %s' , values )
147
- with values as f :
148
- parser .parse_args (f .read ().split (), namespace )
142
+ def cli_args ():
143
+ return (
144
+ (
145
+ "parameters" ,
146
+ str ,
147
+ None ,
148
+ None ,
149
+ "Parameters file." ,
150
+ False ,
151
+ ),
152
+ (
153
+ "current_hospitalized" ,
154
+ int ,
155
+ 0 ,
156
+ None ,
157
+ "Currently hospitalized COVID-19 patients (>= 0)" ,
158
+ True ,
159
+ ),
160
+ (
161
+ "current_date" ,
162
+ cast_date ,
163
+ None ,
164
+ None ,
165
+ "Current date (default is today)" ,
166
+ False ,
167
+ ),
168
+ (
169
+ "date_first_hospitalized" ,
170
+ cast_date ,
171
+ None ,
172
+ None ,
173
+ "Date of first hospitalization" ,
174
+ False ,
175
+ ),
176
+ (
177
+ "doubling_time" ,
178
+ float ,
179
+ 0.0 ,
180
+ None ,
181
+ "Doubling time before social distancing (days)" ,
182
+ True ,
183
+ ),
184
+ (
185
+ "hospitalized_days" ,
186
+ int ,
187
+ 0 ,
188
+ None ,
189
+ "Average hospital length of stay (in days)" ,
190
+ True ,
191
+ ),
192
+ (
193
+ "hospitalized_rate" ,
194
+ float ,
195
+ 0.00001 ,
196
+ 1.0 ,
197
+ "Hospitalized Rate: 0.00001 - 1.0" ,
198
+ True ,
199
+ ),
200
+ (
201
+ "icu_days" ,
202
+ int ,
203
+ 0 ,
204
+ None ,
205
+ "Average days in ICU" ,
206
+ True ,
207
+ ),
208
+ (
209
+ "icu_rate" ,
210
+ float ,
211
+ 0.0 ,
212
+ 1.0 ,
213
+ "ICU rate: 0.0 - 1.0" ,
214
+ True ,
215
+ ),
216
+ (
217
+ "market_share" ,
218
+ float ,
219
+ 0.00001 ,
220
+ 1.0 ,
221
+ "Hospital market share (0.00001 - 1.0)" ,
222
+ True ,
223
+ ),
224
+ (
225
+ "infectious_days" ,
226
+ int ,
227
+ 0.0 ,
228
+ None ,
229
+ "Infectious days" ,
230
+ True ,
231
+ ),
232
+ (
233
+ "mitigation_date" ,
234
+ cast_date ,
235
+ None ,
236
+ None ,
237
+ "Mitigation date for social distancing." ,
238
+ False ,
239
+ ),
240
+ (
241
+ "max_y_axis" ,
242
+ int ,
243
+ 0 ,
244
+ None ,
245
+ "Maximum y axis" ,
246
+ True ,
247
+ ),
248
+ (
249
+ "n-days" ,
250
+ int ,
251
+ 0 ,
252
+ None ,
253
+ "Number of days to project >= 0" ,
254
+ True ,
255
+ ),
256
+ (
257
+ "recovered" ,
258
+ int ,
259
+ 0 ,
260
+ None ,
261
+ "Initial recovered >= 0" ,
262
+ True ,
263
+ ),
264
+ (
265
+ "relative-contact-rate" ,
266
+ float ,
267
+ 0.0 ,
268
+ 1.0 ,
269
+ "Social distancing reduction rate: 0.0 - 1.0" ,
270
+ True ,
271
+ ),
272
+ (
273
+ "population" ,
274
+ int ,
275
+ 1 ,
276
+ None ,
277
+ "Regional population >= 1" ,
278
+ True ,
279
+ ),
280
+ (
281
+ "ventilated_days" ,
282
+ int ,
283
+ 0 ,
284
+ None ,
285
+ "Average days on ventilator" ,
286
+ True ,
287
+ ),
288
+ (
289
+ "ventilated_rate" ,
290
+ float ,
291
+ 0.0 ,
292
+ 1.0 ,
293
+ "Ventilated Rate: 0.0 - 1.0" ,
294
+ True ,
295
+ ),
296
+ )
149
297
150
298
151
299
def to_cli (name ):
@@ -156,43 +304,11 @@ class Parameters:
156
304
"""Parameters."""
157
305
158
306
@classmethod
159
- def parser (cls , environ : Dict [ str , str ] ):
307
+ def parser (cls ):
160
308
parser = ArgumentParser (
161
309
description = f"penn_chime: { VERSION } { CHANGE_DATE } " )
162
- parser .add_argument (
163
- "--parameters" ,
164
- type = open ,
165
- action = FromFile ,
166
- default = environ .get ("PARAMETERS" ),
167
- help = 'Parameters file.'
168
- )
169
-
170
- for name , (params_validator , cast , help ) in ACCEPTED_PARAMETERS .items ():
171
- if cast is None :
172
- continue
173
-
174
- parser .add_argument (
175
- to_cli (name ),
176
- type = declarative_validator (cast ),
177
- help = help
178
- )
179
310
180
- for name , cast , min_value , max_value , help , required in (
181
- ("hospitalized_days" , int , 0 , None , "Average hospital length of stay (in days)" , True ),
182
- (
183
- "hospitalized_rate" ,
184
- float ,
185
- 0.00001 ,
186
- 1.0 ,
187
- "Hospitalized Rate: 0.00001 - 1.0" ,
188
- True ,
189
- ),
190
- ("icu_days" , int , 0 , None , "Average days in ICU" , True ),
191
- ("icu_rate" , float , 0.0 , 1.0 , "ICU rate: 0.0 - 1.0" , True ),
192
-
193
- ("ventilated_days" , int , 0 , None , "Average days on ventilator" , True ),
194
- ("ventilated_rate" , float , 0.0 , 1.0 , "Ventilated Rate: 0.0 - 1.0" , True ),
195
- ):
311
+ for name , cast , min_value , max_value , help , required in cli_args ():
196
312
arg = to_cli (name )
197
313
parser .add_argument (
198
314
arg ,
@@ -204,14 +320,32 @@ def parser(cls, environ: Dict[str, str]):
204
320
@classmethod
205
321
def create (
206
322
cls ,
207
- environ : Dict [str , str ],
323
+ env : Dict [str , str ],
208
324
argv : List [str ],
209
325
) -> Parameters :
210
- parser = cls .parser (environ )
326
+ parser = cls .parser ()
211
327
a = parser .parse_args (argv )
212
328
329
+ if a .parameters is None :
330
+ a .parameters = env .get ("PARAMETERS" )
331
+
332
+ if a .parameters is not None :
333
+ logger .info ('Using file: %s' , a .parameters )
334
+ with open (a .parameters , 'r' ) as fin :
335
+ parser .parse_args (fin .read ().split (), a )
336
+
213
337
del a .parameters
214
338
339
+ logger .info (vars (a ))
340
+
341
+ Positive (key = 'hospitalized_days' , value = a .hospitalized_days )
342
+ Positive (key = 'icu_days' , value = a .icu_days )
343
+ Positive (key = 'ventilated_days' , value = a .ventilated_days )
344
+
345
+ Rate (key = 'hospitalized_rate' , value = a .hospitalized_rate )
346
+ Rate (key = 'icu_rate' , value = a .icu_rate )
347
+ Rate (key = 'ventilated_rate' , value = a .ventilated_rate )
348
+
215
349
hospitalized = Disposition .create (
216
350
days = a .hospitalized_days ,
217
351
rate = a .hospitalized_rate ,
@@ -239,7 +373,6 @@ def create(
239
373
** vars (a ),
240
374
)
241
375
242
-
243
376
def __init__ (self , ** kwargs ):
244
377
today = date .today ()
245
378
@@ -272,7 +405,8 @@ def __init__(self, **kwargs):
272
405
try :
273
406
validator (key = key , value = value )
274
407
except TypeError as ve :
275
- raise ValueError (f"For parameter '{ key } ', with value '{ value } ', validation returned error \" { ve } \" " )
408
+ raise ValueError (
409
+ f"For parameter '{ key } ', with value '{ value } ', validation returned error \" { ve } \" " )
276
410
setattr (self , key , value )
277
411
278
412
if self .region is None and self .population is None :
@@ -282,7 +416,7 @@ def __init__(self, **kwargs):
282
416
self .current_date = today
283
417
284
418
if self .mitigation_date is None :
285
- self .mitigation_Date = today
419
+ self .mitigation_date = today
286
420
287
421
Date (key = 'current_date' , value = self .current_date )
288
422
Date (key = 'mitigation_date' , value = self .mitigation_date )
0 commit comments