Skip to content

Commit bc7465d

Browse files
authored
Merge pull request #58 from pennsignals/altair
Altair
2 parents cbac4df + 39ba08c commit bc7465d

File tree

5 files changed

+93
-143
lines changed

5 files changed

+93
-143
lines changed

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ verify_ssl = true
99
streamlit = "*"
1010
pandas = "*"
1111
numpy = "*"
12-
matplotlib = "*"
12+
altair = "*"
1313

1414
[requires]
1515
python_version = "3.7"

Pipfile.lock

Lines changed: 26 additions & 108 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app.py

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
from functools import reduce
2+
from typing import Tuple, Dict, Any
13
import pandas as pd
24
import streamlit as st
35
import numpy as np
4-
import matplotlib
5-
6-
matplotlib.use("Agg")
7-
import matplotlib.pyplot as plt
6+
import altair as alt
87

98
hide_menu_style = """
109
<style>
@@ -236,17 +235,26 @@ def sim_sir(S, I, R, beta, gamma, n_days, beta_decay=None):
236235
plot_projection_days = n_days - 10
237236
projection_admits["day"] = range(projection_admits.shape[0])
238237

239-
fig, ax = plt.subplots(1, 1, figsize=(10, 4))
240-
ax.plot(
241-
projection_admits.head(plot_projection_days)["hosp"], ".-", label="Hospitalized"
242-
)
243-
ax.plot(projection_admits.head(plot_projection_days)["icu"], ".-", label="ICU")
244-
ax.plot(projection_admits.head(plot_projection_days)["vent"], ".-", label="Ventilated")
245-
ax.legend(loc=0)
246-
ax.set_xlabel("Days from today")
247-
ax.grid("on")
248-
ax.set_ylabel("Daily Admissions")
249-
st.pyplot()
238+
239+
def new_admissions_chart(projection_admits: pd.DataFrame, plot_projection_days: int) -> alt.Chart:
240+
"""docstring"""
241+
projection_admits = projection_admits.rename(columns={"hosp": "Hospitalized", "icu": "ICU", "vent": "Ventilated"})
242+
return (
243+
alt
244+
.Chart(projection_admits.head(plot_projection_days))
245+
.transform_fold(fold=["Hospitalized", "ICU", "Ventilated"])
246+
.mark_line(point=True)
247+
.encode(
248+
x=alt.X("day", title="Days from today"),
249+
y=alt.Y("value:Q", title="Daily admissions"),
250+
color="key:N",
251+
tooltip=["day", "key:N"]
252+
)
253+
.interactive()
254+
)
255+
256+
st.altair_chart(new_admissions_chart(projection_admits, plot_projection_days), use_container_width=True)
257+
250258

251259
admits_table = projection_admits[np.mod(projection_admits.index, 7) == 0].copy()
252260
admits_table["day"] = admits_table.index
@@ -269,22 +277,14 @@ def sim_sir(S, I, R, beta, gamma, n_days, beta_decay=None):
269277
"vent": vent_los,
270278
}
271279

272-
fig, ax = plt.subplots(1, 1, figsize=(10, 4))
273-
274-
census_dict = {}
280+
census_dict = dict()
275281
for k, los in los_dict.items():
276282
census = (
277283
projection_admits.cumsum().iloc[:-los, :]
278284
- projection_admits.cumsum().shift(los).fillna(0)
279285
).apply(np.ceil)
280286
census_dict[k] = census[k]
281-
ax.plot(census.head(plot_projection_days)[k], ".-", label=k + " census")
282-
ax.legend(loc=0)
283287

284-
ax.set_xlabel("Days from today")
285-
ax.grid("on")
286-
ax.set_ylabel("Census")
287-
st.pyplot()
288288

289289
census_df = pd.DataFrame(census_dict)
290290
census_df["day"] = census_df.index
@@ -295,6 +295,26 @@ def sim_sir(S, I, R, beta, gamma, n_days, beta_decay=None):
295295
census_table.loc[0, :] = 0
296296
census_table = census_table.dropna().astype(int)
297297

298+
def admitted_patients_chart(census: pd.DataFrame) -> alt.Chart:
299+
"""docstring"""
300+
census = census.rename(columns={"hosp": "Hospital Census", "icu": "ICU Census", "vent": "Ventilated Census"})
301+
302+
return (
303+
alt
304+
.Chart(census)
305+
.transform_fold(fold=["Hospital Census", "ICU Census", "Ventilated Census"])
306+
.mark_line(point=True)
307+
.encode(
308+
x=alt.X("day", title="Days from today"),
309+
y=alt.Y("value:Q", title="Census"),
310+
color="key:N",
311+
tooltip=["day", "key:N"]
312+
)
313+
.interactive()
314+
)
315+
316+
st.altair_chart(admitted_patients_chart(census_table), use_container_width=True)
317+
298318
if st.checkbox("Show Projected Census in tabular form"):
299319
st.dataframe(census_table)
300320

@@ -305,15 +325,26 @@ def sim_sir(S, I, R, beta, gamma, n_days, beta_decay=None):
305325
st.subheader(
306326
"The number of infected and recovered individuals in the hospital catchment region at any given moment"
307327
)
308-
fig, ax = plt.subplots(1, 1, figsize=(10, 4))
309-
ax.plot(i, label="Infected")
310-
ax.plot(r, label="Recovered")
311-
ax.legend(loc=0)
312-
ax.set_xlabel("days from today")
313-
ax.set_ylabel("Case Volume")
314-
ax.grid("on")
315-
st.pyplot()
316328

329+
def additional_projections_chart(i: np.ndarray, r: np.ndarray) -> alt.Chart:
330+
dat = pd.DataFrame({"Infected": i, "Recovered": r})
331+
332+
return (
333+
alt
334+
.Chart(dat.reset_index())
335+
.transform_fold(fold=["Infected", "Recovered"])
336+
.mark_line()
337+
.encode(
338+
x=alt.X("index", title="Days from today"),
339+
y=alt.Y("value:Q", title="Case Volume"),
340+
tooltip=["key:N", "value:Q"],
341+
color="key:N"
342+
)
343+
.interactive()
344+
)
345+
346+
st.altair_chart(additional_projections_chart(i, r), use_container_width=True)
347+
317348
# Show data
318349
days = np.array(range(0, n_days + 1))
319350
data_list = [days, s, i, r]

environment.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name: chime
22
channels:
33
- defaults
4+
- conda-forge
45
dependencies:
56
- python>=3.6
67
- streamlit
78
- numpy
89
- pandas
9-
- matplotlib
10+
- altair

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
streamlit
22
pandas
33
numpy
4-
matplotlib
4+
altair

0 commit comments

Comments
 (0)