Skip to content

Commit b70e697

Browse files
various housekeeping
1 parent d621775 commit b70e697

File tree

6 files changed

+165
-170
lines changed

6 files changed

+165
-170
lines changed

src/app.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
"""App."""
22

3-
import altair as alt
4-
import streamlit as st
3+
import altair as alt # type: ignore
4+
import streamlit as st # type: ignore
55

66
from penn_chime.presentation import (
7-
additional_projections_chart,
8-
admitted_patients_chart,
7+
#additional_projections_chart,
98
display_header,
109
display_sidebar,
1110
display_n_days_slider,
1211
draw_census_table,
1312
draw_projected_admissions_table,
1413
draw_raw_sir_simulation_table,
1514
hide_menu_style,
16-
new_admissions_chart,
1715
show_additional_projections,
1816
show_more_info_about_this_tool,
1917
write_definitions,
2018
write_footer,
2119
)
2220
from penn_chime.utils import build_admissions_df, build_census_df
2321
from penn_chime.settings import DEFAULTS
24-
25-
22+
from penn_chime.models import sim_sir_df
23+
from penn_chime.charts import additional_projections_chart, admitted_patients_chart, new_admissions_chart
2624
# This is somewhat dangerous:
2725
# Hide the main menu with "Rerun", "run on Save", "clear cache", and "record a screencast"
2826
# This should not be hidden in prod, but removed
@@ -59,7 +57,6 @@
5957
r_t=p.r_t,
6058
inputs=DEFAULTS,
6159
notes=notes
62-
6360
)
6461

6562
# PRESENTATION
@@ -68,8 +65,8 @@
6865
display_n_days_slider(st, p, DEFAULTS)
6966

7067
# begin format data
71-
projection_admits = build_admissions_df(p.n_days, *p.dispositions)
72-
census_df = build_census_df(projection_admits, *p.lengths_of_stay)
68+
projection_admits = build_admissions_df(p=p) # p.n_days, *p.dispositions)
69+
census_df = build_census_df(projection_admits, parameters=p)
7370
# end format data
7471

7572
st.subheader("New Admissions")

src/penn_chime/charts.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env python
2+
from typing import Optional
3+
4+
from altair import Chart # type: ignore
5+
import pandas as pd # type: ignore
6+
import numpy as np # type: ignore
7+
8+
def new_admissions_chart(
9+
alt,
10+
projection_admits: pd.DataFrame,
11+
plot_projection_days: int,
12+
as_date: bool = False,
13+
max_y_axis: int = None
14+
) -> Chart:
15+
"""docstring"""
16+
projection_admits = projection_admits.rename(
17+
columns={"hosp": "Hospitalized", "icu": "ICU", "vent": "Ventilated"}
18+
)
19+
20+
y_scale = alt.Scale()
21+
22+
if max_y_axis is not None:
23+
y_scale.domain = (0, max_y_axis)
24+
y_scale.clamp = True
25+
26+
tooltip_dict = {False: "day", True: "date:T"}
27+
if as_date:
28+
projection_admits = add_date_column(projection_admits)
29+
x_kwargs = {"shorthand": "date:T", "title": "Date"}
30+
else:
31+
x_kwargs = {"shorthand": "day", "title": "Days from today"}
32+
33+
return (
34+
alt.Chart(projection_admits.head(plot_projection_days))
35+
.transform_fold(fold=["Hospitalized", "ICU", "Ventilated"])
36+
.mark_line(point=True)
37+
.encode(
38+
x=alt.X(**x_kwargs),
39+
y=alt.Y("value:Q", title="Daily admissions", scale=y_scale),
40+
color="key:N",
41+
tooltip=[
42+
tooltip_dict[as_date],
43+
alt.Tooltip("value:Q", format=".0f", title="Admissions"),
44+
"key:N",
45+
],
46+
)
47+
.interactive()
48+
)
49+
50+
51+
def admitted_patients_chart(
52+
alt,
53+
census: pd.DataFrame,
54+
plot_projection_days: int,
55+
as_date: bool = False,
56+
max_y_axis: Optional[int] = None
57+
) -> Chart:
58+
"""docstring"""
59+
60+
tooltip_dict = {False: "day", True: "date:T"}
61+
if as_date:
62+
census = add_date_column(census)
63+
x_kwargs = {"shorthand": "date:T", "title": "Date"}
64+
idx = "date:T"
65+
else:
66+
x_kwargs ={"shorthand": "day", "title": "Days from today"}
67+
idx = "day"
68+
69+
y_scale = alt.Scale()
70+
71+
if max_y_axis:
72+
y_scale.domain = (0, max_y_axis)
73+
y_scale.clamp = True
74+
75+
return (
76+
alt.Chart(census.head(plot_projection_days))
77+
.transform_fold(fold=["Hospital Census", "ICU Census", "Ventilated Census"])
78+
.mark_line(point=True)
79+
.encode(
80+
x=alt.X(**x_kwargs),
81+
y=alt.Y("value:Q", title="Census", scale=y_scale),
82+
color="key:N",
83+
tooltip=[
84+
idx,
85+
alt.Tooltip("value:Q", format=".0f", title="Census"),
86+
"key:N",
87+
],
88+
)
89+
.interactive()
90+
)
91+
92+
93+
def additional_projections_chart(
94+
alt,
95+
i: np.ndarray,
96+
r: np.ndarray,
97+
as_date: bool = False,
98+
max_y_axis: int = None
99+
) -> Chart:
100+
dat = pd.DataFrame({"Infected": i, "Recovered": r})
101+
dat["day"] = dat.index
102+
if as_date:
103+
dat = add_date_column(dat)
104+
x_kwargs = {"shorthand": "date:T", "title": "Date"}
105+
else:
106+
x_kwargs = {"shorthand": "day", "title": "Days from today"}
107+
108+
y_scale = alt.Scale()
109+
110+
if max_y_axis is not None:
111+
y_scale.domain = (0, max_y_axis)
112+
y_scale.clamp = True
113+
114+
return (
115+
alt.Chart(dat)
116+
.transform_fold(fold=["Infected", "Recovered"])
117+
.mark_line()
118+
.encode(
119+
x=alt.X(**x_kwargs),
120+
y=alt.Y("value:Q", title="Case Volume", scale=y_scale),
121+
tooltip=["key:N", "value:Q"],
122+
color="key:N",
123+
)
124+
.interactive()
125+
)

src/penn_chime/models.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from typing import Generator, Tuple
44

5-
import numpy as np
6-
import pandas as pd
5+
import numpy as np # type: ignore
6+
import pandas as pd # type: ignore
77

88

99
def sir(
@@ -58,13 +58,13 @@ def sim_sir(
5858
)
5959

6060

61-
def sim_sir_df(
62-
s: float, i: float, r: float,
63-
beta: float, gamma: float, n_days: int
64-
) -> pd.DataFrame:
65-
"""Simulate the SIR model forward in time."""
61+
def sim_sir_df(p) -> pd.DataFrame:
62+
"""Simulate the SIR model forward in time.
63+
64+
p is a Parameters instance. for circuluar dependency reasons i can't annotate it.
65+
"""
6666
return pd.DataFrame(
67-
data=gen_sir(s, i, r, beta, gamma, n_days),
67+
data=gen_sir(p.susceptible, p.infected, p.recovered, p.beta, p.gamma, p.n_days),
6868
columns=("Susceptible", "Infected", "Recovered"),
6969
)
7070

src/penn_chime/parameters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Parameters."""
22

3-
from numpy import log2
3+
from numpy import log2 # type: ignore
44

55
from .utils import RateLos
66
from .models import (

src/penn_chime/presentation.py

Lines changed: 7 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import altair as alt
2-
import numpy as np
3-
import pandas as pd
1+
"""effectful functions for streamlit io"""
2+
3+
from typing import Optional
4+
5+
import altair as alt # type: ignore
6+
import numpy as np # type: ignore
7+
import pandas as pd # type: ignore
48

59
from .defaults import Constants, RateLos
610
from .utils import add_date_column
711
from .parameters import Parameters
812

9-
1013
DATE_FORMAT = "%b, %d" # see https://strftime.org
1114

1215

@@ -382,134 +385,6 @@ def write_footer(st):
382385
st.markdown("© 2020, The Trustees of the University of Pennsylvania")
383386

384387

385-
##########
386-
# Charts #
387-
##########
388-
389-
390-
def new_admissions_chart(
391-
alt,
392-
projection_admits: pd.DataFrame,
393-
plot_projection_days: int,
394-
as_date: bool = False,
395-
max_y_axis: int = None
396-
) -> alt.Chart:
397-
"""docstring"""
398-
projection_admits = projection_admits.rename(
399-
columns={"hosp": "Hospitalized", "icu": "ICU", "vent": "Ventilated"}
400-
)
401-
402-
y_scale = alt.Scale()
403-
404-
if max_y_axis is not None:
405-
y_scale.domain = (0, max_y_axis)
406-
y_scale.clamp = True
407-
408-
tooltip_dict = {False: "day", True: "date:T"}
409-
if as_date:
410-
projection_admits = add_date_column(projection_admits)
411-
x_kwargs = {"shorthand": "date:T", "title": "Date"}
412-
else:
413-
x_kwargs = {"shorthand": "day", "title": "Days from today"}
414-
415-
return (
416-
alt.Chart(projection_admits.head(plot_projection_days))
417-
.transform_fold(fold=["Hospitalized", "ICU", "Ventilated"])
418-
.mark_line(point=True)
419-
.encode(
420-
x=alt.X(**x_kwargs),
421-
y=alt.Y("value:Q", title="Daily admissions", scale=y_scale),
422-
color="key:N",
423-
tooltip=[
424-
tooltip_dict[as_date],
425-
alt.Tooltip("value:Q", format=".0f", title="Admissions"),
426-
"key:N",
427-
],
428-
)
429-
.interactive()
430-
)
431-
432-
433-
def admitted_patients_chart(
434-
alt,
435-
census: pd.DataFrame,
436-
plot_projection_days: int,
437-
as_date: bool = False,
438-
max_y_axis: int = None
439-
) -> alt.Chart:
440-
"""docstring"""
441-
census = census.rename(
442-
columns={
443-
"hosp": "Hospital Census",
444-
"icu": "ICU Census",
445-
"vent": "Ventilated Census",
446-
}
447-
)
448-
tooltip_dict = {False: "day", True: "date:T"}
449-
if as_date:
450-
census = add_date_column(census)
451-
x_kwargs = {"shorthand": "date:T", "title": "Date"}
452-
else:
453-
x_kwargs ={"shorthand": "day", "title": "Days from today"}
454-
455-
y_scale = alt.Scale()
456-
457-
if max_y_axis is not None:
458-
y_scale.domain = (0, max_y_axis)
459-
y_scale.clamp = True
460-
461-
return (
462-
alt.Chart(census.head(plot_projection_days))
463-
.transform_fold(fold=["Hospital Census", "ICU Census", "Ventilated Census"])
464-
.mark_line(point=True)
465-
.encode(
466-
x=alt.X(**x_kwargs),
467-
y=alt.Y("value:Q", title="Census", scale=y_scale),
468-
color="key:N",
469-
tooltip=[
470-
tooltip_dict[as_date],
471-
alt.Tooltip("value:Q", format=".0f", title="Census"),
472-
"key:N",
473-
],
474-
)
475-
.interactive()
476-
)
477-
478-
479-
def additional_projections_chart(
480-
alt,
481-
i: np.ndarray,
482-
r: np.ndarray,
483-
as_date: bool = False,
484-
max_y_axis: int = None
485-
) -> alt.Chart:
486-
dat = pd.DataFrame({"Infected": i, "Recovered": r})
487-
dat["day"] = dat.index
488-
if as_date:
489-
dat = add_date_column(dat)
490-
x_kwargs = {"shorthand": "date:T", "title": "Date"}
491-
else:
492-
x_kwargs = {"shorthand": "day", "title": "Days from today"}
493-
494-
y_scale = alt.Scale()
495-
496-
if max_y_axis is not None:
497-
y_scale.domain = (0, max_y_axis)
498-
y_scale.clamp = True
499-
500-
return (
501-
alt.Chart(dat)
502-
.transform_fold(fold=["Infected", "Recovered"])
503-
.mark_line()
504-
.encode(
505-
x=alt.X(**x_kwargs),
506-
y=alt.Y("value:Q", title="Case Volume", scale=y_scale),
507-
tooltip=["key:N", "value:Q"],
508-
color="key:N",
509-
)
510-
.interactive()
511-
)
512-
513388

514389
def show_additional_projections(
515390
st,

0 commit comments

Comments
 (0)