Skip to content

Commit 44bc837

Browse files
moved build_*_df funcs into models.py since they have mathematical content
1 parent 9921c26 commit 44bc837

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/app.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
write_definitions,
1818
write_footer,
1919
)
20-
from penn_chime.utils import build_admissions_df, build_census_df
21-
from penn_chime.settings import DEFAULTS
22-
from penn_chime.models import sim_sir_df
20+
from penn_chime.settings import DEFAULTS
21+
from penn_chime.models import sim_sir_df, build_admissions_df, build_census_df
2322
from penn_chime.charts import additional_projections_chart, admitted_patients_chart, new_admissions_chart
2423
# This is somewhat dangerous:
2524
# Hide the main menu with "Rerun", "run on Save", "clear cache", and "record a screencast"
@@ -61,7 +60,6 @@
6160
st.markdown(
6261
"Projected **census** of COVID-19 patients, accounting for arrivals and discharges at Penn hospitals"
6362
)
64-
st.dataframe(census_df)
6563
st.altair_chart(
6664
admitted_patients_chart(alt=alt, census=census_df, parameters=p, as_date=as_date), use_container_width=True
6765
)

src/penn_chime/models.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,51 @@ def get_dispositions(
7474
) -> Tuple[np.ndarray, ...]:
7575
"""Get dispositions of infected adjusted by rate and market_share."""
7676
return (*(infected * rate * market_share for rate in rates),)
77+
78+
79+
80+
def build_admissions_df(p) -> pd.DataFrame:
81+
"""Build admissions dataframe from Parameters."""
82+
days = np.array(range(0, p.n_days + 1))
83+
data_dict = dict(zip(["day", "Hospitalized", "ICU", "Ventilated"],
84+
[days] + [disposition for disposition in p.dispositions]
85+
))
86+
projection = pd.DataFrame.from_dict(data_dict)
87+
# New cases
88+
projection_admits = projection.iloc[:-1, :] - projection.shift(1)
89+
projection_admits[projection_admits < 0] = 0
90+
projection_admits["day"] = range(projection_admits.shape[0])
91+
return projection_admits
92+
93+
94+
def build_census_df(
95+
projection_admits: pd.DataFrame,
96+
parameters
97+
) -> pd.DataFrame:
98+
"""ALOS for each category of COVID-19 case (total guesses)"""
99+
n_days = np.shape(projection_admits)[0]
100+
hosp_los, icu_los, vent_los = parameters.lengths_of_stay
101+
los_dict = {
102+
"Hospitalized": hosp_los,
103+
"ICU": icu_los,
104+
"Ventilated": vent_los,
105+
}
106+
107+
census_dict = dict()
108+
for k, los in los_dict.items():
109+
census = (
110+
projection_admits.cumsum().iloc[:-los, :]
111+
- projection_admits.cumsum().shift(los).fillna(0)
112+
).apply(np.ceil)
113+
census_dict[k] = census[k]
114+
115+
census_df = pd.DataFrame(census_dict)
116+
census_df["day"] = census_df.index
117+
census_df = census_df[["day", "Hospitalized", "ICU", "Ventilated"]]
118+
census_df = census_df.head(n_days)
119+
census_df = census_df.rename(
120+
columns={disposition: f"{disposition} Census"
121+
for disposition
122+
in ("Hospitalized", "ICU", "Ventilated")}
123+
)
124+
return census_df

0 commit comments

Comments
 (0)