Skip to content

Commit

Permalink
Through beds df and chart. no lines yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmastand committed Apr 1, 2020
1 parent e9e4649 commit c1842de
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 22 deletions.
26 changes: 24 additions & 2 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from penn_chime.charts import (
build_admits_chart,
build_census_chart,
build_beds_chart,
build_descriptions,
build_sim_sir_w_date_chart,
build_table,
Expand All @@ -43,7 +44,7 @@
st.markdown("Projected number of **daily** COVID-19 admissions. \n\n _NOTE: Now including estimates of prior admissions for comparison._")
admits_chart = build_admits_chart(alt=alt, admits_floor_df=m.admits_floor_df, max_y_axis=p.max_y_axis)
st.altair_chart(admits_chart, use_container_width=True)
st.markdown(build_descriptions(chart=admits_chart, labels=p.labels, suffix=" Admissions"))
st.markdown(build_descriptions(chart=admits_chart, labels=p.patient_chart_desc))
display_download_link(
st,
filename=f"{p.current_date}_projected_admits.csv",
Expand All @@ -66,7 +67,7 @@
st.markdown("Projected **census** of COVID-19 patients, accounting for arrivals and discharges \n\n _NOTE: Now including estimates of prior census for comparison._")
census_chart = build_census_chart(alt=alt, census_floor_df=m.census_floor_df, max_y_axis=p.max_y_axis)
st.altair_chart(census_chart, use_container_width=True)
st.markdown(build_descriptions(chart=census_chart, labels=p.labels, suffix=" Census"))
st.markdown(build_descriptions(chart=census_chart, labels=p.patient_chart_desc))
display_download_link(
st,
filename=f"{p.current_date}_projected_census.csv",
Expand All @@ -84,6 +85,27 @@
st.table(table_df)


st.subheader("COVID-19 Capacity")
st.markdown(
"Projected **number** of available COVID-19 beds, accounting for admits and discharges"
)
st.dataframe(m.beds_df)##########
beds_chart = build_beds_chart(alt=alt, beds_floor_df=m.beds_df, max_y_axis=p.max_y_axis)
st.altair_chart(beds_chart, use_container_width=True)
# st.markdown(bed_chart_descriptions(beds_chart, p.bed_chart_desc))
# if st.checkbox("Show Projected Available COVID-19 Beds in tabular form"):
# if st.checkbox("Show Daily Available Bed Counts"):
# draw_beds_table(st, p, m.beds_df, p.labels, as_date=p.as_date, daily_count=True)
# else:
# draw_beds_table(st, p, m.beds_df, p.labels, as_date=p.as_date, daily_count=False)
# build_download_link(st,
# filename="projected_beds.csv",
# df=m.beds_df,
# parameters=p
# )



st.subheader("Susceptible, Infected, and Recovered")
st.markdown("The number of susceptible, infected, and recovered individuals in the hospital catchment region at any given moment")
sim_sir_w_date_chart = build_sim_sir_w_date_chart(alt=alt, sim_sir_w_date_floor_df=m.sim_sir_w_date_floor_df)
Expand Down
64 changes: 57 additions & 7 deletions src/penn_chime/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def build_admits_chart(

x = dict(shorthand="date:T", title="Date", axis=alt.Axis(format=(DATE_FORMAT)))
y = dict(shorthand="value:Q", title="Daily admissions", scale=y_scale)
color = "key:N"
color = alt.Color("key:N", sort = ["total", "icu", "ventilators"])
tooltip=["date:T", alt.Tooltip("value:Q", format=".0f", title="Admit"), "key:N"]

# TODO fix the fold to allow any number of dispositions
points = (
alt.Chart()
.transform_fold(fold=["hospitalized", "icu", "ventilated"])
.transform_fold(fold=["total", "icu", "ventilators"])
.encode(x=alt.X(**x), y=alt.Y(**y), color=color, tooltip=tooltip)
.mark_line(point=True)
)
Expand All @@ -57,13 +57,13 @@ def build_census_chart(

x = dict(shorthand="date:T", title="Date", axis=alt.Axis(format=(DATE_FORMAT)))
y = dict(shorthand="value:Q", title="Census", scale=y_scale)
color = "key:N"
color = alt.Color("key:N", sort = ["total", "icu", "ventilators"])
tooltip = ["date:T", alt.Tooltip("value:Q", format=".0f", title="Census"), "key:N"]

# TODO fix the fold to allow any number of dispositions
points = (
alt.Chart()
.transform_fold(fold=["hospitalized", "icu", "ventilated"])
.transform_fold(fold=["total", "icu", "ventilators"])
.encode(x=alt.X(**x), y=alt.Y(**y), color=color, tooltip=tooltip)
.mark_line(point=True)
)
Expand Down Expand Up @@ -107,7 +107,58 @@ def build_sim_sir_w_date_chart(
)
return alt.layer(points, bar, data=sim_sir_w_date_floor_df)

def build_beds_chart(
alt,
beds_floor_df: pd.DataFrame,
max_y_axis: Optional[int] = None,
) -> Chart:
"""docstring"""
y_scale = alt.Scale()
if max_y_axis is not None:
y_scale.domain = (0, max_y_axis)

x = dict(shorthand="date:T", title="Date", axis=alt.Axis(format=(DATE_FORMAT)))
y = dict(shorthand="value:Q", title="COVID-19 Capacity", scale=y_scale)
color = alt.Color("key:N", sort = ["total", "icu", "ventilators"])
tooltip = ["key:N", "value:Q"]

# census["line"] = 0
# TODO fix the fold to allow any number of dispositions
beds = (
alt.Chart()
.transform_fold(fold=["total", "icu", "ventilators"])
.mark_line(point=True)
.encode(x=alt.X(**x), y=alt.Y(**y), color=color, tooltip=tooltip)
.mark_line()
)
# Horizontal line at 0
# hline = alt.Chart(census.head(plot_projection_days)
# ).transform_fold(
# fold=["line"]
# ).mark_line(
# point=False,
# color="black",
# strokeDash=[5,3],
# opacity=.5,
# ).encode(
# x=alt.X(**x_kwargs),
# y=alt.Y("value:Q"),
# )
# # Vertical line on today
# v_line_location = "0"
# if parameters.as_date:
# today = datetime.date.today()
# v_line_location = f"datetime({today.year}, {today.month - 1}, {today.day})" # Because Altair uses vega which has 0-based month indexes
# vline = alt.Chart(census.head(plot_projection_days)
# ).mark_rule(
# strokeDash=[5,3],
# opacity=.05, # This doesn't seem to do anything
# ).encode(
# x="v_line_location:T" if parameters.as_date else "v_line_location:Q"
# ).transform_calculate(v_line_location=v_line_location)

# return beds + vline + hline, beds
return alt.layer(beds, data=beds_floor_df)
def build_descriptions(
*,
chart: Chart,
Expand All @@ -124,7 +175,7 @@ def build_descriptions(
"""
messages = []

cols = ["hospitalized", "icu", "ventilated"]
cols = ["total", "icu", "ventilators"]
asterisk = False
day = "date" if "date" in chart.data.columns else "day"

Expand All @@ -136,9 +187,8 @@ def build_descriptions(
on = datetime.strftime(chart.data[day][chart.data[col].idxmax()], "%b %d")

messages.append(
"{}{} peaks at {:,} on {}{}".format(
"{} {:,} on {}{}".format(
labels[col],
suffix,
ceil(chart.data[col].max()),
on,
"*" if asterisk else "",
Expand Down
21 changes: 10 additions & 11 deletions src/penn_chime/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def __init__(self, p: Parameters):
self.sim_sir_w_date_floor_df = build_floor_df(self.sim_sir_w_date_df, self.keys)
self.admits_floor_df = build_floor_df(self.admits_df, p.dispositions.keys())
self.census_floor_df = build_floor_df(self.census_df, p.dispositions.keys())
self.beds_floor_df = build_floor_df(self.beds_df, p.dispositions.keys())

self.daily_growth_rate = get_growth_rate(p.doubling_time)
self.daily_growth_rate_t = get_growth_rate(self.doubling_time_t)
Expand All @@ -179,6 +180,7 @@ def run_projection(self, p):
self.dispositions_df = build_dispositions_df(self.raw_df, self.rates, p.market_share, p.current_date)
self.admits_df = build_admits_df(self.dispositions_df)
self.census_df = build_census_df(self.admits_df, self.days)
self.beds_df = build_beds_df(self.census_df, p)
self.current_infected = self.raw_df.infected.loc[self.i_day]

def get_loss(self) -> float:
Expand Down Expand Up @@ -342,22 +344,19 @@ def build_census_df(
})

def build_beds_df(
census_df: pd.DataFrame, lengths_of_stay, p
census_df: pd.DataFrames,
p,
) -> pd.DataFrame:
"""ALOS for each category of COVID-19 case (total guesses)"""
# n_days = np.shape(census_df)[0]
census_dict = {}

beds_df = pd.DataFrame(census_dict)
beds_df["day"] = census_df.index

covid_non_icu_beds = p.total_covid_beds - p.covid_icu_beds
beds_df = pd.DataFrame()
beds_df["day"] = census_df["day"]
beds_df["date"] = census_df["date"]

# If hospitalized < 0 and there's space in icu, start borrowing if possible
# If ICU < 0, raise alarms. No changes.
beds_df["hospitalized"] = p.covid_non_icu_beds - census_df["hospitalized"]
beds_df["icu"] = p.covid_icu_beds - p.census_df["icu"]
beds_df["ventilators"] = p.covid_vents - p.census_df["ventilators"]
beds_df["hospitalized"] = p.total_covid_beds - p.icu_covid_beds - census_df["hospitalized"]
beds_df["icu"] = p.icu_covid_beds - census_df["icu"]
beds_df["ventilators"] = p.covid_ventilators - census_df["ventilators"]
beds_df["total"] = p.total_covid_beds - census_df["hospitalized"] - census_df["icu"]
# beds_df = beds_df.head(n_days)

Expand Down
15 changes: 15 additions & 0 deletions src/penn_chime/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,22 @@ def __init__(
}

self.dispositions = {
"total": hospitalized,
"hospitalized": hospitalized,
"icu": icu,
"ventilators": ventilators,
}

self.patient_chart_desc = {
"hospitalized": "Hospitalized COVID-19 Admissions peak at",
"icu": "ICU COVID-19 Admissions peak at",
"ventilators": "COVID-19 Ventilators peak at",
"total": "Total COVID-19 Admissions peaks at"
}

self.eqp_chart_desc = {
"hospitalized": "Hospitalized COVID-19 Beds",
"icu": "Total ICU COVID-19 Beds",
"ventilators": "COVID-19 Ventilators",
"total": "Total COVID-19 Beds"
}
3 changes: 1 addition & 2 deletions src/penn_chime/presentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def display_header(st, m, p):
and daily growth rate of **{daily_growth_t:.2f}%**.
""".format(
total_infections=m.infected,
current_hosp=p.current_hospitalized,
current_hosp=p.covid_census_value,
hosp_rate=p.hospitalized.rate,
S=p.population,
market_share=p.market_share,
Expand Down Expand Up @@ -330,7 +330,6 @@ def display_sidebar(st, d: Parameters) -> Parameters:
)
population = population_input()
market_share = market_share_pct_input()
# known_infected = known_infected_input()
covid_census_value = covid_census_value_input()
covid_census_date = covid_census_date_input()

Expand Down

0 comments on commit c1842de

Please sign in to comment.