Skip to content

Commit 74b4949

Browse files
committed
Patch up validators later
1 parent c65d370 commit 74b4949

File tree

5 files changed

+200
-59
lines changed

5 files changed

+200
-59
lines changed

defaults/cli.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
--current-hospitalized 69
2-
--doubling_time 4.0
2+
--doubling-time 4.0
33
--hospitalized-days 7
44
--hospitalized-rate 0.025
55
--icu-days 9
66
--icu-rate 0.0075
77
--infectious-days 14
8-
--market_share 0.15
8+
--market-share 0.15
99
--n-days 100
1010
--population 3600000
1111
--recovered 0

defaults/webapp.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
--icu-days 9
77
--icu-rate 0.0075
88
--infectious-days 14
9-
--market_share 0.15
9+
--market-share 0.15
1010
--n-days 100
1111
--population 3600000
1212
--recovered 0

src/penn_chime/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
def main():
1111
"""Main."""
12-
p = Parameters.create(os.environ, sys.argv)
12+
p = Parameters.create(os.environ, sys.argv[1:])
1313
m = Model(p)
1414

1515
for df, name in (

src/penn_chime/parameters.py

Lines changed: 184 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ class Disposition(_Disposition):
6767
@classmethod
6868
def create(cls, *, days: int, rate: float):
6969
"""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)
7272
return cls(days, rate)
7373

7474

@@ -124,8 +124,6 @@ def validate(string):
124124
"current_date": (OptionalDate, cast_date, "Date on which the forecast should be based"),
125125
"date_first_hospitalized": (OptionalDate, cast_date, "Date the first patient was hospitalized"),
126126
"doubling_time": (OptionalStrictlyPositive, float, "Doubling time before social distancing (days)"),
127-
"hospitalized": (ValDisposition, None, None),
128-
"icu": (ValDisposition, None, None),
129127
"infectious_days": (StrictlyPositive, int, "Infectious days"),
130128
"mitigation_date": (OptionalDate, cast_date, "Date on which social distancing measures too effect"),
131129
"market_share": (Rate, float, "Hospital market share (0.00001 - 1.0)"),
@@ -136,16 +134,166 @@ def validate(string):
136134
"region": (OptionalValue, None, "No help available"),
137135
"relative_contact_rate": (Rate, float, "Social distancing reduction rate: 0.0 - 1.0"),
138136
"ventilated": (ValDisposition, None, None),
137+
"hospitalized": (ValDisposition, None, None),
138+
"icu": (ValDisposition, None, None),
139139
}
140140

141141

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+
)
149297

150298

151299
def to_cli(name):
@@ -156,43 +304,11 @@ class Parameters:
156304
"""Parameters."""
157305

158306
@classmethod
159-
def parser(cls, environ: Dict[str, str]):
307+
def parser(cls):
160308
parser = ArgumentParser(
161309
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-
)
179310

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():
196312
arg = to_cli(name)
197313
parser.add_argument(
198314
arg,
@@ -204,14 +320,32 @@ def parser(cls, environ: Dict[str, str]):
204320
@classmethod
205321
def create(
206322
cls,
207-
environ: Dict[str, str],
323+
env: Dict[str, str],
208324
argv: List[str],
209325
) -> Parameters:
210-
parser = cls.parser(environ)
326+
parser = cls.parser()
211327
a = parser.parse_args(argv)
212328

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+
213337
del a.parameters
214338

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+
215349
hospitalized = Disposition.create(
216350
days=a.hospitalized_days,
217351
rate=a.hospitalized_rate,
@@ -239,7 +373,6 @@ def create(
239373
**vars(a),
240374
)
241375

242-
243376
def __init__(self, **kwargs):
244377
today = date.today()
245378

@@ -272,7 +405,8 @@ def __init__(self, **kwargs):
272405
try:
273406
validator(key=key, value=value)
274407
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}\"")
276410
setattr(self, key, value)
277411

278412
if self.region is None and self.population is None:
@@ -282,7 +416,7 @@ def __init__(self, **kwargs):
282416
self.current_date = today
283417

284418
if self.mitigation_date is None:
285-
self.mitigation_Date = today
419+
self.mitigation_date = today
286420

287421
Date(key='current_date', value=self.current_date)
288422
Date(key='mitigation_date', value=self.mitigation_date)

src/penn_chime/presentation.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,19 +353,26 @@ def display_sidebar(st, d: Parameters) -> Parameters:
353353

354354
return Parameters(
355355
current_hospitalized=current_hospitalized,
356-
hospitalized=Disposition(hospitalized_rate, hospitalized_days),
357-
icu=Disposition(icu_rate, icu_days),
358-
relative_contact_rate=relative_contact_rate,
359-
mitigation_date=mitigation_date,
360-
ventilated=Disposition(ventilated_rate, ventilated_days),
361356
current_date=current_date,
362357
date_first_hospitalized=date_first_hospitalized,
363358
doubling_time=doubling_time,
359+
hospitalized=Disposition.create(
360+
rate=hospitalized_rate,
361+
days=hospitalized_days),
362+
icu=Disposition.create(
363+
rate=icu_rate,
364+
days=icu_days),
364365
infectious_days=infectious_days,
365366
market_share=market_share,
366367
max_y_axis=max_y_axis,
368+
mitigation_date=mitigation_date,
367369
n_days=n_days,
368370
population=population,
371+
recovered=d.recovered,
372+
relative_contact_rate=relative_contact_rate,
373+
ventilated=Disposition.create(
374+
rate=ventilated_rate,
375+
days=ventilated_days),
369376
)
370377

371378

0 commit comments

Comments
 (0)