Skip to content

Commit cb620ec

Browse files
Merge pull request #290 from CodeForPhilly/fix_input_percents
Add PercentInput for clarity
2 parents 62ffe34 + 209bcd9 commit cb620ec

File tree

1 file changed

+42
-53
lines changed

1 file changed

+42
-53
lines changed

src/penn_chime/presentation.py

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def display_header(st, m, p):
112112
return None
113113

114114

115-
class InputWrapper:
115+
class Input:
116116
"""Helper to separate Streamlit input definition from creation/rendering"""
117117
def __init__(self, st_obj, label, value, kwargs):
118118
self.st_obj = st_obj
@@ -124,13 +124,21 @@ def __call__(self):
124124
return self.st_obj(self.label, value=self.value, **self.kwargs)
125125

126126

127-
class NumberInputWrapper(InputWrapper):
127+
class NumberInput(Input):
128128
def __init__(self, st_obj, label, min_value=None, max_value=None, value=None, step=None, format=None, key=None):
129129
kwargs = dict(min_value=min_value, max_value=max_value, step=step, format=format, key=key)
130130
super().__init__(st_obj.number_input, label, value, kwargs)
131131

132132

133-
class CheckboxWrapper(InputWrapper):
133+
class PercentInput(NumberInput):
134+
def __init__(self, st_obj, label, min_value=0.0, max_value=100.0, value=None, step=FLOAT_INPUT_STEP, format="%f", key=None):
135+
super().__init__(st_obj, label, min_value, max_value, value * 100.0, step, format, key)
136+
137+
def __call__(self):
138+
return super().__call__() / 100.0
139+
140+
141+
class CheckboxInput(Input):
134142
def __init__(self, st_obj, label, value=None, key=None):
135143
kwargs = dict(key=key)
136144
super().__init__(st_obj.checkbox, label, value, kwargs)
@@ -145,135 +153,116 @@ def display_sidebar(st, d: Constants) -> Parameters:
145153
if d.known_infected < 1:
146154
raise ValueError("Known cases must be larger than one to enable predictions.")
147155
st_obj = st.sidebar
148-
current_hospitalized_input = NumberInputWrapper(
156+
current_hospitalized_input = NumberInput(
149157
st_obj,
150158
"Currently Hospitalized COVID-19 Patients",
151159
min_value=0,
152160
value=d.current_hospitalized,
153161
step=1,
154162
format="%i",
155163
)
156-
n_days_input = NumberInputWrapper(
164+
n_days_input = NumberInput(
157165
st_obj,
158166
"Number of days to project",
159167
min_value=30,
160168
value=d.n_days,
161169
step=1,
162170
format="%i",
163171
)
164-
doubling_time_input = NumberInputWrapper(
172+
doubling_time_input = NumberInput(
165173
st_obj,
166174
"Doubling time before social distancing (days)",
167175
min_value=FLOAT_INPUT_MIN,
168176
value=d.doubling_time,
169177
step=FLOAT_INPUT_STEP,
170178
format="%f",
171179
)
172-
relative_contact_rate_input = NumberInputWrapper(
180+
relative_contact_pct_input = PercentInput(
173181
st_obj,
174182
"Social distancing (% reduction in social contact)",
175-
min_value=0.0,
176-
max_value=100.0,
177-
value=d.relative_contact_rate * 100.0,
178-
step=FLOAT_INPUT_STEP,
179-
format="%f",
183+
value=d.relative_contact_rate,
180184
)
181-
hospitalized_rate_input = NumberInputWrapper(
185+
hospitalized_pct_input = PercentInput(
182186
st_obj,
183187
"Hospitalization %(total infections)",
184-
min_value=0.0,
185-
max_value=100.0,
186-
value=d.hospitalized.rate * 100,
187-
step=FLOAT_INPUT_STEP,
188-
format="%f",
188+
value=d.hospitalized.rate,
189189
)
190-
icu_rate_input = NumberInputWrapper(
190+
icu_pct_input = PercentInput(
191191
st_obj,
192192
"ICU %(total infections)",
193-
min_value=0.0,
194-
max_value=100.0,
195-
value=d.icu.rate * 100,
196-
step=FLOAT_INPUT_STEP,
197-
format="%f",
193+
value=d.icu.rate,
198194
)
199-
ventilated_rate_input = NumberInputWrapper(
195+
ventilated_pct_input = PercentInput(
200196
st_obj,
201197
"Ventilated %(total infections)",
202-
min_value=0.0,
203-
max_value=100.0,
204-
value=d.ventilated.rate * 100,
205-
step=FLOAT_INPUT_STEP,
206-
format="%f",
198+
value=d.ventilated.rate,
207199
)
208-
hospitalized_los_input = NumberInputWrapper(
200+
hospitalized_los_input = NumberInput(
209201
st_obj,
210202
"Hospital Length of Stay",
211203
min_value=0,
212204
value=d.hospitalized.length_of_stay,
213205
step=1,
214206
format="%i",
215207
)
216-
icu_los_input = NumberInputWrapper(
208+
icu_los_input = NumberInput(
217209
st_obj,
218210
"ICU Length of Stay",
219211
min_value=0,
220212
value=d.icu.length_of_stay,
221213
step=1,
222214
format="%i",
223215
)
224-
ventilated_los_input = NumberInputWrapper(
216+
ventilated_los_input = NumberInput(
225217
st_obj,
226218
"Vent Length of Stay",
227219
min_value=0,
228220
value=d.ventilated.length_of_stay,
229221
step=1,
230222
format="%i",
231223
)
232-
market_share_input = NumberInputWrapper(
224+
market_share_pct_input = PercentInput(
233225
st_obj,
234226
"Hospital Market Share (%)",
235227
min_value=FLOAT_INPUT_MIN,
236-
max_value=100.0,
237-
value=d.market_share * 100,
238-
step=FLOAT_INPUT_STEP,
239-
format="%f",
228+
value=d.market_share,
240229
)
241-
population_input = NumberInputWrapper(
230+
population_input = NumberInput(
242231
st_obj,
243232
"Regional Population",
244233
min_value=1,
245234
value=d.region.population,
246235
step=1,
247236
format="%i",
248237
)
249-
known_infected_input = NumberInputWrapper(
238+
known_infected_input = NumberInput(
250239
st_obj,
251240
"Currently Known Regional Infections (only used to compute detection rate - does not change projections)",
252241
min_value=0,
253242
value=d.known_infected,
254243
step=1,
255244
format="%i",
256245
)
257-
as_date_input = CheckboxWrapper(st_obj, "Present result as dates instead of days", value=False)
258-
max_y_axis_set_input = CheckboxWrapper(st_obj, "Set the Y-axis on graphs to a static value")
259-
max_y_axis_input = NumberInputWrapper(st_obj, "Y-axis static value", value=500, format="%i", step=25)
246+
as_date_input = CheckboxInput(st_obj, "Present result as dates instead of days", value=False)
247+
max_y_axis_set_input = CheckboxInput(st_obj, "Set the Y-axis on graphs to a static value")
248+
max_y_axis_input = NumberInput(st_obj, "Y-axis static value", value=500, format="%i", step=25)
260249

261250
# Build in desired order
262251
st.sidebar.markdown("### Regional Parameters [ℹ]({docs_url}/what-is-chime/parameters)".format(docs_url=DOCS_URL))
263252
population = population_input()
264-
market_share = market_share_input()
253+
market_share = market_share_pct_input()
265254
known_infected = known_infected_input()
266255
current_hospitalized = current_hospitalized_input()
267256

268257
st.sidebar.markdown("### Spread and Contact Parameters [ℹ]({docs_url}/what-is-chime/parameters)"
269258
.format(docs_url=DOCS_URL))
270259
doubling_time = doubling_time_input()
271-
relative_contact_rate = relative_contact_rate_input()
260+
relative_contact_rate = relative_contact_pct_input()
272261

273262
st.sidebar.markdown("### Severity Parameters [ℹ]({docs_url}/what-is-chime/parameters)".format(docs_url=DOCS_URL))
274-
hospitalized_rate = hospitalized_rate_input()
275-
icu_rate = icu_rate_input()
276-
ventilated_rate = ventilated_rate_input()
263+
hospitalized_rate = hospitalized_pct_input()
264+
icu_rate = icu_pct_input()
265+
ventilated_rate = ventilated_pct_input()
277266
hospitalized_los = hospitalized_los_input()
278267
icu_los = icu_los_input()
279268
ventilated_los = ventilated_los_input()
@@ -290,18 +279,18 @@ def display_sidebar(st, d: Constants) -> Parameters:
290279
return Parameters(
291280
as_date=as_date,
292281
current_hospitalized=current_hospitalized,
293-
market_share=market_share / 100.0,
282+
market_share=market_share,
294283
known_infected=known_infected,
295284
doubling_time=doubling_time,
296285

297286
max_y_axis=max_y_axis,
298287
n_days=n_days,
299-
relative_contact_rate=relative_contact_rate / 100.0,
288+
relative_contact_rate=relative_contact_rate,
300289
population=population,
301290

302-
hospitalized=RateLos(hospitalized_rate / 100.0, hospitalized_los),
303-
icu=RateLos(icu_rate / 100.0, icu_los),
304-
ventilated=RateLos(ventilated_rate / 100.0, ventilated_los),
291+
hospitalized=RateLos(hospitalized_rate, hospitalized_los),
292+
icu=RateLos(icu_rate, icu_los),
293+
ventilated=RateLos(ventilated_rate, ventilated_los),
305294
)
306295

307296

0 commit comments

Comments
 (0)