Skip to content

Commit 9921c26

Browse files
refactored functions so they take a parameters instance instead of a bucket of args
1 parent b70e697 commit 9921c26

File tree

4 files changed

+70
-87
lines changed

4 files changed

+70
-87
lines changed

src/app.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,13 @@
2929

3030
p = display_sidebar(st, DEFAULTS)
3131

32-
display_header(
33-
st,
34-
total_infections=p.infected,
35-
initial_infections=p.known_infected,
36-
detection_prob=p.detection_probability,
37-
current_hosp=p.current_hospitalized,
38-
hosp_rate=p.hospitalized.rate,
39-
S=p.susceptible,
40-
market_share=p.market_share,
41-
recovery_days=p.recovery_days,
42-
r_naught=p.r_naught,
43-
doubling_time=p.doubling_time,
44-
relative_contact_rate=p.relative_contact_rate,
45-
r_t=p.r_t,
46-
doubling_time_t=p.doubling_time_t,
47-
)
32+
display_header(st, p)
33+
4834
if st.checkbox("Show more info about this tool"):
4935
notes = "The total size of the susceptible population will be the entire catchment area for Penn Medicine entities (HUP, PAH, PMC, CCH)"
5036
show_more_info_about_this_tool(
5137
st=st,
52-
recovery_days=p.recovery_days,
53-
doubling_time=p.doubling_time,
54-
r_naught=p.r_naught,
55-
relative_contact_rate=p.relative_contact_rate,
56-
doubling_time_t=p.doubling_time_t,
57-
r_t=p.r_t,
38+
parameters=p,
5839
inputs=DEFAULTS,
5940
notes=notes
6041
)
@@ -72,23 +53,30 @@
7253
st.subheader("New Admissions")
7354
st.markdown("Projected number of **daily** COVID-19 admissions at Penn hospitals")
7455
st.altair_chart(
75-
new_admissions_chart(alt, projection_admits, p.n_days - 10, as_date=as_date, max_y_axis=p.max_y_axis), use_container_width=True
56+
new_admissions_chart(alt, projection_admits, parameters=p, as_date=as_date), use_container_width=True
7657
)
7758
if st.checkbox("Show Projected Admissions in tabular form"):
7859
draw_projected_admissions_table(st, projection_admits, as_date=as_date)
7960
st.subheader("Admitted Patients (Census)")
8061
st.markdown(
8162
"Projected **census** of COVID-19 patients, accounting for arrivals and discharges at Penn hospitals"
8263
)
83-
st.altair_chart(admitted_patients_chart(alt, census_df, p.n_days - 10, as_date=as_date, max_y_axis=p.max_y_axis), use_container_width=True)
64+
st.dataframe(census_df)
65+
st.altair_chart(
66+
admitted_patients_chart(alt=alt, census=census_df, parameters=p, as_date=as_date), use_container_width=True
67+
)
8468
if st.checkbox("Show Projected Census in tabular form"):
8569
draw_census_table(st, census_df, as_date=as_date)
8670
st.markdown(
8771
"""**Click the checkbox below to view additional data generated by this simulation**"""
8872
)
8973
if st.checkbox("Show Additional Projections"):
90-
show_additional_projections(st, alt, additional_projections_chart, p.infected_v, p.recovered_v, as_date=as_date, max_y_axis=p.max_y_axis)
74+
show_additional_projections(
75+
st, alt,
76+
additional_projections_chart,
77+
parameters=p,
78+
as_date=as_date)
9179
if st.checkbox("Show Raw SIR Simulation Data"):
92-
draw_raw_sir_simulation_table(st, p.n_days, p.susceptible_v, p.infected_v, p.recovered_v, as_date=as_date)
80+
draw_raw_sir_simulation_table(st, parameters=p, as_date=as_date)
9381
write_definitions(st)
9482
write_footer(st)

src/penn_chime/charts.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
import pandas as pd # type: ignore
66
import numpy as np # type: ignore
77

8+
from .parameters import Parameters
9+
from .utils import add_date_column
10+
11+
812
def new_admissions_chart(
913
alt,
1014
projection_admits: pd.DataFrame,
11-
plot_projection_days: int,
15+
parameters: Parameters,
1216
as_date: bool = False,
13-
max_y_axis: int = None
1417
) -> Chart:
1518
"""docstring"""
16-
projection_admits = projection_admits.rename(
17-
columns={"hosp": "Hospitalized", "icu": "ICU", "vent": "Ventilated"}
18-
)
19+
plot_projection_days = parameters.n_days - 10
20+
max_y_axis = parameters.max_y_axis
1921

2022
y_scale = alt.Scale()
2123

@@ -51,13 +53,13 @@ def new_admissions_chart(
5153
def admitted_patients_chart(
5254
alt,
5355
census: pd.DataFrame,
54-
plot_projection_days: int,
55-
as_date: bool = False,
56-
max_y_axis: Optional[int] = None
56+
parameters: Parameters,
57+
as_date: bool = False
5758
) -> Chart:
5859
"""docstring"""
5960

60-
tooltip_dict = {False: "day", True: "date:T"}
61+
plot_projection_days = parameters.n_days - 10
62+
max_y_axis = parameters.max_y_axis
6163
if as_date:
6264
census = add_date_column(census)
6365
x_kwargs = {"shorthand": "date:T", "title": "Date"}
@@ -73,8 +75,8 @@ def admitted_patients_chart(
7375
y_scale.clamp = True
7476

7577
return (
76-
alt.Chart(census.head(plot_projection_days))
77-
.transform_fold(fold=["Hospital Census", "ICU Census", "Ventilated Census"])
78+
alt.Chart(census)#.head(plot_projection_days))
79+
.transform_fold(fold=["Hospitalized Census", "ICU Census", "Ventilated Census"])
7880
.mark_line(point=True)
7981
.encode(
8082
x=alt.X(**x_kwargs),

src/penn_chime/presentation.py

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,11 @@
2525
########
2626

2727

28-
def display_header(
29-
st,
30-
total_infections,
31-
initial_infections,
32-
detection_prob,
33-
current_hosp,
34-
hosp_rate,
35-
S,
36-
market_share,
37-
recovery_days,
38-
r_naught,
39-
doubling_time,
40-
relative_contact_rate,
41-
r_t,
42-
doubling_time_t,
43-
):
28+
def display_header(st, p):
4429

4530
detection_prob_str = (
46-
"{detection_prob:.0%}".format(detection_prob=detection_prob)
47-
if detection_prob is not None
31+
"{detection_prob:.0%}".format(detection_prob=p.detection_probability)
32+
if p.detection_probability
4833
else "unknown"
4934
)
5035
st.markdown(
@@ -78,19 +63,19 @@ def display_header(
7863
**Mitigation**: A **{relative_contact_rate:.0%}** reduction in social contact after the onset of the
7964
outbreak reduces the doubling time to **{doubling_time_t:.1f}** days, implying an effective $R_t$ of **${r_t:.2f}$**.
8065
""".format(
81-
total_infections=total_infections,
82-
initial_infections=initial_infections,
66+
total_infections=p.infected,
67+
initial_infections=p.known_infected,
8368
detection_prob_str=detection_prob_str,
84-
current_hosp=current_hosp,
85-
hosp_rate=hosp_rate,
86-
S=S,
87-
market_share=market_share,
88-
recovery_days=recovery_days,
89-
r_naught=r_naught,
90-
doubling_time=doubling_time,
91-
relative_contact_rate=relative_contact_rate,
92-
r_t=r_t,
93-
doubling_time_t=doubling_time_t,
69+
current_hosp=p.current_hospitalized,
70+
hosp_rate=p.hospitalized.rate,
71+
S=p.susceptible,
72+
market_share=p.market_share,
73+
recovery_days=p.recovery_days,
74+
r_naught=p.r_naught,
75+
doubling_time=p.doubling_time,
76+
relative_contact_rate=p.relative_contact_rate,
77+
r_t=p.r_t,
78+
doubling_time_t=p.doubling_time_t,
9479
)
9580
)
9681

@@ -255,12 +240,7 @@ def display_n_days_slider(st, p: Parameters, d: Constants):
255240

256241
def show_more_info_about_this_tool(
257242
st,
258-
recovery_days,
259-
doubling_time,
260-
r_naught,
261-
relative_contact_rate,
262-
doubling_time_t,
263-
r_t,
243+
parameters,
264244
inputs: Constants,
265245
notes: str = ''
266246
):
@@ -332,12 +312,12 @@ def show_more_info_about_this_tool(
332312
- $\\gamma$: the CDC is recommending 14 days of self-quarantine, we'll use $\\gamma = 1/{recovery_days}$.
333313
- To estimate $$\\beta$$ directly, we'd need to know transmissibility and social contact rates. since we don't know these things, we can extract it from known _doubling times_. The AHA says to expect a doubling time $T_d$ of 7-10 days. That means an early-phase rate of growth can be computed by using the doubling time formula:
334314
""".format(
335-
doubling_time=doubling_time,
336-
recovery_days=recovery_days,
337-
r_naught=r_naught,
338-
relative_contact_rate=relative_contact_rate,
339-
doubling_time_t=doubling_time_t,
340-
r_t=r_t,
315+
doubling_time=parameters.doubling_time,
316+
recovery_days=parameters.recovery_days,
317+
r_naught=parameters.r_naught,
318+
relative_contact_rate=parameters.relative_contact_rate,
319+
doubling_time_t=parameters.doubling_time_t,
320+
r_t=parameters.r_t,
341321
)
342322
)
343323
st.latex("g = 2^{1/T_d} - 1")
@@ -390,16 +370,21 @@ def show_additional_projections(
390370
st,
391371
alt,
392372
charting_func,
393-
i,
394-
r,
373+
parameters,
395374
as_date: bool = False,
396-
max_y_axis: int = None
397375
):
398376
st.subheader(
399377
"The number of infected and recovered individuals in the hospital catchment region at any given moment"
400378
)
401379

402-
st.altair_chart(charting_func(alt, i, r, as_date=as_date, max_y_axis=max_y_axis), use_container_width=True)
380+
st.altair_chart(
381+
charting_func(
382+
alt,
383+
parameters.infected_v,
384+
parameters.recovered_v,
385+
as_date=as_date,
386+
max_y_axis=parameters.max_y_axis),
387+
use_container_width=True)
403388

404389

405390
##########
@@ -444,10 +429,13 @@ def draw_census_table(st, census_df: pd.DataFrame, as_date: bool = False):
444429
return None
445430

446431

447-
def draw_raw_sir_simulation_table(st, n_days, s, i, r, as_date: bool = False):
448-
days = np.arange(0, n_days + 1)
449-
data_list = [days, s, i, r]
450-
data_dict = dict(zip(["day", "susceptible", "infections", "recovered"], data_list))
432+
def draw_raw_sir_simulation_table(
433+
st,
434+
parameters,
435+
as_date: bool = False):
436+
days = np.arange(0, parameters.n_days + 1)
437+
data_list = [days, parameters.susceptible_v, parameters.infected_v, parameters.recovered_v]
438+
data_dict = dict(zip(["day", "Susceptible", "Infections", "Recovered"], data_list))
451439
projection_area = pd.DataFrame.from_dict(data_dict)
452440
infect_table = (projection_area.iloc[::7, :]).apply(np.floor)
453441
infect_table.index = range(infect_table.shape[0])

src/penn_chime/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ def build_census_df(
5454
census_df["day"] = census_df.index
5555
census_df = census_df[["day", "Hospitalized", "ICU", "Ventilated"]]
5656
census_df = census_df.head(n_days)
57+
census_df = census_df.rename(
58+
columns={disposition: f"{disposition} Census"
59+
for disposition
60+
in ("Hospitalized", "ICU", "Ventilated")}
61+
)
5762
return census_df
5863

5964

0 commit comments

Comments
 (0)