Skip to content

Commit c75a8c5

Browse files
author
Eric Smyth
committed
Resolve implementation given refactor that occured as part of PR 208
1 parent 9191a32 commit c75a8c5

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

src/penn_chime/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ def __init__(self, p: Parameters) -> SimSirModel:
8787
self.dispositions_df = pd.DataFrame(self.dispositions)
8888
self.admits_df = admits_df = build_admits_df(p.n_days, self.dispositions)
8989
self.census_df = build_census_df(admits_df, lengths_of_stay)
90+
self.daily_growth = daily_growth_helper(p.doubling_time)
91+
self.daily_growth_t = daily_growth_helper(self.doubling_time_t)
9092

9193

9294
def sir(
@@ -171,3 +173,10 @@ def build_census_df(
171173
census_df = census_df[["day", *lengths_of_stay.keys()]]
172174
census_df = census_df.head(n_days)
173175
return census_df
176+
177+
178+
def daily_growth_helper(doubling_time):
179+
result = 0
180+
if doubling_time != 0:
181+
result = (np.power(2, 1.0 / doubling_time) - 1) * 100
182+
return result

src/penn_chime/presentation.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ def display_header(st, m, p):
6060
and Hospital market share (**{market_share:.0%}**).
6161
6262
An initial doubling time of **{doubling_time}** days and a recovery time of **{recovery_days}** days imply an $R_0$ of
63-
**{r_naught:.2f}** and daily growth rate of **{daily_growth:.2f}%**.
64-
63+
**{r_naught:.2f}** and daily growth rate of **{daily_growth:.2f}%**.
64+
6565
**Mitigation**: A **{relative_contact_rate:.0%}** reduction in social contact after the onset of the
66-
outbreak **{impact_statement:s} {doubling_time_t:.1f}** days, implying an effective $R_t$ of **${r_t:.2f}$** and daily
67-
growth rate of **{daily_growth_t:.2f}%**..
66+
outbreak **{impact_statement:s} {doubling_time_t:.1f}** days, implying an effective $R_t$ of **${r_t:.2f}$**
67+
and daily growth rate of **{daily_growth_t:.2f}%**.
6868
""".format(
6969
total_infections=m.infected,
7070
initial_infections=p.known_infected,
@@ -77,11 +77,11 @@ def display_header(st, m, p):
7777
r_naught=m.r_naught,
7878
doubling_time=p.doubling_time,
7979
relative_contact_rate=p.relative_contact_rate,
80-
daily_growth=p.daily_growth,
81-
daily_growth_t=p.daily_growth_t,
8280
r_t=m.r_t,
8381
doubling_time_t=abs(m.doubling_time_t),
84-
impact_statement=("halves the infections every" if m.r_t < 1 else "reduces the doubling time to")
82+
impact_statement=("halves the infections every" if m.r_t < 1 else "reduces the doubling time to"),
83+
daily_growth=m.daily_growth,
84+
daily_growth_t=m.daily_growth_t,
8585
)
8686
)
8787

tests/test_app.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import altair as alt # type: ignore
99

1010
from src.penn_chime.charts import new_admissions_chart, admitted_patients_chart, chart_descriptions
11-
from src.penn_chime.models import SimSirModel, sir, sim_sir_df, build_admits_df
11+
from src.penn_chime.models import SimSirModel, sir, sim_sir_df, build_admits_df, daily_growth_helper
1212
from src.penn_chime.parameters import Parameters
1313
from src.penn_chime.presentation import display_header
1414
from src.penn_chime.settings import DEFAULTS
@@ -27,7 +27,21 @@
2727
n_days=60,
2828
)
2929

30+
HALVING_PARAM = Parameters(
31+
current_hospitalized=100,
32+
doubling_time=6.0,
33+
known_infected=5000,
34+
market_share=0.05,
35+
relative_contact_rate=0.7,
36+
susceptible=500000,
37+
hospitalized=RateLos(0.05, 7),
38+
icu=RateLos(0.02, 9),
39+
ventilated=RateLos(0.01, 10),
40+
n_days=60,
41+
)
42+
3043
MODEL = SimSirModel(PARAM)
44+
HALVING_MODEL = SimSirModel(HALVING_PARAM)
3145

3246

3347
# set up
@@ -58,56 +72,38 @@ def cleanup(self):
5872
# test presentation
5973

6074

75+
def header_test_helper(expected_str, model, param):
76+
st.cleanup()
77+
display_header(st, model, param)
78+
assert [s for s in st.render_store if expected_str in s],\
79+
"Expected the string '{expected}' in the display header".format(expected=expected_str)
80+
st.cleanup()
81+
82+
6183
def test_penn_logo_in_header():
6284
penn_css = '<link rel="stylesheet" href="https://www1.pennmedicine.org/styles/shared/penn-medicine-header.css">'
63-
display_header(st, MODEL, PARAM)
64-
assert len(
65-
list(filter(lambda s: penn_css in s, st.render_store))
66-
), "The Penn Medicine header should be printed"
85+
header_test_helper(penn_css, MODEL, PARAM)
6786

6887

6988
def test_the_rest_of_header_shows_up():
7089
random_part_of_header = "implying an effective $R_t$ of"
71-
assert len(
72-
list(filter(lambda s: random_part_of_header in s, st.render_store))
73-
), "The whole header should render"
90+
header_test_helper(random_part_of_header, MODEL, PARAM)
7491

7592

7693
def test_mitigation_statement():
77-
st.cleanup()
7894
expected_doubling = "outbreak **reduces the doubling time to 7.8** days"
79-
display_header(st, MODEL, PARAM)
80-
assert [s for s in st.render_store if expected_doubling in s]
81-
# assert len((list(filter(lambda s: expected_doubling in s, st.render_store))))
82-
st.cleanup()
8395
expected_halving = "outbreak **halves the infections every 51.9** days"
84-
halving_params = Parameters(
85-
current_hospitalized=100,
86-
doubling_time=6.0,
87-
known_infected=5000,
88-
market_share=0.05,
89-
relative_contact_rate=0.7,
90-
susceptible=500000,
91-
hospitalized=RateLos(0.05, 7),
92-
icu=RateLos(0.02, 9),
93-
ventilated=RateLos(0.01, 10),
94-
n_days=60,
95-
)
96-
halving_model = SimSirModel(halving_params)
97-
display_header(st, halving_model, halving_params)
98-
assert [s for s in st.render_store if expected_halving in s]
99-
#assert len((list(filter(lambda s: expected_halving in s, st.render_store))))
100-
st.cleanup()
96+
header_test_helper(expected_doubling, MODEL, PARAM)
97+
header_test_helper(expected_halving, HALVING_MODEL, HALVING_PARAM)
10198

10299

103-
def test_daily_growth():
104-
st.cleanup()
100+
def test_daily_growth_presentation():
105101
initial_growth = "and daily growth rate of **12.25%**."
106102
mitigated_growth = "and daily growth rate of **9.34%**."
107-
display_header(st, PARAM)
108-
assert len((list(filter(lambda s: initial_growth in s, st.render_store))))
109-
assert len((list(filter(lambda s: mitigated_growth in s, st.render_store))))
110-
st.cleanup()
103+
mitigated_halving = "and daily growth rate of **-1.33%**."
104+
header_test_helper(initial_growth, MODEL, PARAM)
105+
header_test_helper(mitigated_growth, MODEL, PARAM)
106+
header_test_helper(mitigated_halving, HALVING_MODEL, HALVING_PARAM)
111107

112108

113109
st.cleanup()
@@ -271,6 +267,12 @@ def test_model(model=MODEL, param=PARAM):
271267
assert (diff.abs() < 0.1).all()
272268

273269

270+
def test_daily_growth_helper():
271+
assert np.round(daily_growth_helper(5), decimals=4) == 14.8698
272+
assert np.round(daily_growth_helper(0), decimals=4) == 0.0
273+
assert np.round(daily_growth_helper(-4), decimals=4) == -15.9104
274+
275+
274276
def test_chart_descriptions(p=PARAM):
275277
# new admissions chart
276278
projection_admits = pd.read_csv('tests/projection_admits.csv')

0 commit comments

Comments
 (0)