Skip to content

Commit d621775

Browse files
authored
Add parameter tests
Add parameter tests
2 parents 4cb13f2 + 75d17ee commit d621775

File tree

1 file changed

+77
-36
lines changed

1 file changed

+77
-36
lines changed

src/test_app.py

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import pytest
44
import pandas as pd
5-
5+
import numpy as np
66

77
from app import (projection_admits, alt)
88
from penn_chime.models import sir, sim_sir, sim_sir_df
9+
from penn_chime.parameters import Parameters
910
from penn_chime.presentation import display_header, new_admissions_chart
1011
from penn_chime.settings import DEFAULTS
12+
from penn_chime.defaults import RateLos
1113

1214

1315
# set up
@@ -69,7 +71,7 @@ def test_header_fail():
6971
st.cleanup()
7072

7173

72-
def test_defaultS_repr():
74+
def test_defaults_repr():
7375
"""
7476
Test DEFAULTS.repr
7577
"""
@@ -82,36 +84,42 @@ def test_sir():
8284
"""
8385
Someone who is good at testing, help
8486
"""
85-
assert sir(100, 1, 0, 0.2, 0.5, 1) == (
87+
sir_test = sir(100, 1, 0, 0.2, 0.5, 1)
88+
assert sir_test == (
8689
0.7920792079207921,
8790
0.20297029702970298,
8891
0.0049504950495049506,
8992
), "This contrived example should work"
9093

94+
assert isinstance(sir_test, tuple)
95+
for v in sir_test:
96+
assert isinstance(v, float)
97+
assert v >= 0
98+
9199
# Certain things should *not* work
92-
with pytest.raises(TypeError) as E:
100+
with pytest.raises(TypeError) as error:
93101
sir("S", 1, 0, 0.2, 0.5, 1)
94-
assert str(E.value) == "can't multiply sequence by non-int of type 'float'"
102+
assert str(error.value) == "can't multiply sequence by non-int of type 'float'"
95103

96-
with pytest.raises(TypeError) as E:
104+
with pytest.raises(TypeError) as error:
97105
sir(100, "I", 0, 0.2, 0.5, 1)
98-
assert str(E.value) == "can't multiply sequence by non-int of type 'float'"
106+
assert str(error.value) == "can't multiply sequence by non-int of type 'float'"
99107

100-
with pytest.raises(TypeError) as E:
108+
with pytest.raises(TypeError) as error:
101109
sir(100, 1, "R", 0.2, 0.5, 1)
102-
assert str(E.value) == "unsupported operand type(s) for +: 'float' and 'str'"
110+
assert str(error.value) == "unsupported operand type(s) for +: 'float' and 'str'"
103111

104-
with pytest.raises(TypeError) as E:
112+
with pytest.raises(TypeError) as error:
105113
sir(100, 1, 0, "beta", 0.5, 1)
106-
assert str(E.value) == "bad operand type for unary -: 'str'"
114+
assert str(error.value) == "bad operand type for unary -: 'str'"
107115

108-
with pytest.raises(TypeError) as E:
116+
with pytest.raises(TypeError) as error:
109117
sir(100, 1, 0, 0.2, "gamma", 1)
110-
assert str(E.value) == "unsupported operand type(s) for -: 'float' and 'str'"
118+
assert str(error.value) == "unsupported operand type(s) for -: 'float' and 'str'"
111119

112-
with pytest.raises(TypeError) as E:
120+
with pytest.raises(TypeError) as error:
113121
sir(100, 1, 0, 0.2, 0.5, "N")
114-
assert str(E.value) == "unsupported operand type(s) for /: 'str' and 'float'"
122+
assert str(error.value) == "unsupported operand type(s) for /: 'str' and 'float'"
115123

116124
# Zeros across the board should fail
117125
with pytest.raises(ZeroDivisionError):
@@ -122,7 +130,8 @@ def test_sim_sir():
122130
"""
123131
Rounding to move fast past decimal place issues
124132
"""
125-
s,i,r = sim_sir(5, 6, 7, 0.1, 0.1, 40)
133+
sim_sir_test = sim_sir(5, 6, 7, 0.1, 0.1, 40)
134+
s, i, r = sim_sir_test
126135

127136
assert round(s[0], 0) == 5
128137
assert round(i[0], 2) == 6
@@ -131,6 +140,11 @@ def test_sim_sir():
131140
assert round(i[-1], 2) == 0.18
132141
assert round(r[-1], 2) == 17.82
133142

143+
assert isinstance(sim_sir_test, tuple)
144+
for v in sim_sir_test:
145+
assert isinstance(v, np.ndarray)
146+
147+
134148

135149
def test_sim_sir_df():
136150
"""
@@ -148,32 +162,59 @@ def test_sim_sir_df():
148162
assert round(last[2], 2) == 17.82
149163

150164

151-
#ef test_initial_conditions():
152-
# """
153-
# Note: For the rates (ie hosp_rate) - just change the value, leave the "100" alone.
154-
# Easier to change whole numbers than decimals.
155-
# """
156-
# assert current_hosp == known_cases
157-
# assert doubling_time == 6
158-
# assert relative_contact_rate == 0
159-
# assert hosp_rate == 5 / 100
160-
# assert icu_rate == 2 / 100
161-
# assert vent_rate == 1 / 100
162-
# assert hosp_los == 7
163-
# assert icu_los == 9
164-
# assert vent_los == 10
165-
# assert market_share == 15 / 100
166-
# assert S == S_default
167-
# assert initial_infections == known_infections
168-
169-
170165
def test_new_admissions_chart():
171166
chart = new_admissions_chart(alt, projection_admits, 60 - 10)
172-
assert type(chart) == alt.Chart
167+
assert isinstance(chart, alt.Chart)
173168
assert chart.data.iloc[1].Hospitalized < 1
174169
# assert round(chart.data.iloc[49].ICU, 0) == 43
175170
with pytest.raises(TypeError):
176171
new_admissions_chart()
177172

178173
empty_chart = new_admissions_chart(alt, pd.DataFrame(), -1)
179174
assert empty_chart.data.empty
175+
176+
177+
def test_parameters():
178+
param = Parameters(
179+
current_hospitalized=100,
180+
doubling_time=6.0,
181+
known_infected=5000,
182+
market_share=0.05,
183+
relative_contact_rate=0.15,
184+
susceptible=500000,
185+
hospitalized=RateLos(0.05, 7),
186+
icu=RateLos(0.02, 9),
187+
ventilated=RateLos(0.01, 10),
188+
n_days=60
189+
)
190+
191+
# test the Parameters
192+
193+
# hospitalized, icu, ventilated
194+
assert param.rates == (0.05, 0.02, 0.01)
195+
assert param.lengths_of_stay == (7, 9, 10)
196+
197+
assert param.infected == 40000.0
198+
assert isinstance(param.infected, float) # based off note in models.py
199+
200+
# test the class-calculated attributes
201+
assert param.detection_probability == 0.125
202+
assert param.intrinsic_growth_rate == 0.12246204830937302
203+
assert param.beta == 3.2961405355450555e-07
204+
assert param.r_t == 2.307298374881539
205+
assert param.r_naught == 2.7144686763312222
206+
assert param.doubling_time_t == 7.764405988534983
207+
208+
# test the things n_days creates, which in turn tests sim_sir, sir, and get_dispositions
209+
assert len(param.susceptible_v) == len(param.infected_v) == len(param.recovered_v) == param.n_days + 1 == 61
210+
211+
assert param.susceptible_v[0] == 500000.0
212+
assert round(param.susceptible_v[-1], 0) == 67202
213+
assert round(param.infected_v[1], 0) == 43735
214+
assert round(param.recovered_v[30], 0) == 224048
215+
assert [d[0] for d in param.dispositions] == [100.0, 40.0, 20.0]
216+
assert [round(d[-1], 0) for d in param.dispositions] == [115.0, 46.0, 23.0]
217+
218+
# change n_days, make sure it cascades
219+
param.n_days = 2
220+
assert len(param.susceptible_v) == len(param.infected_v) == len(param.recovered_v) == param.n_days + 1 == 3

0 commit comments

Comments
 (0)