Skip to content

Commit 4c6d70a

Browse files
committed
Finish splitting up tests into modules
- Add remaining models tests to models module - Create settings module - Reformat conftest
1 parent a256e83 commit 4c6d70a

File tree

4 files changed

+111
-104
lines changed

4 files changed

+111
-104
lines changed

tests/conftest.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313

1414
class MockStreamlit:
15+
"""Mock implementation of streamlit
16+
17+
We just want to verify that st _attempted_ to render the right stuff
18+
so we store the input, and make sure that it matches what we expect
19+
"""
20+
1521
def __init__(self):
1622
self.render_store = []
1723
self.markdown = self.just_store_instead_of_rendering
@@ -96,9 +102,13 @@ def halving_model(halving_param):
96102

97103
@pytest.fixture
98104
def admits_df():
99-
return pd.read_csv('tests/by_doubling_time/2020-03-28_projected_admits.csv', parse_dates=['date'])
105+
return pd.read_csv(
106+
"tests/by_doubling_time/2020-03-28_projected_admits.csv", parse_dates=["date"]
107+
)
100108

101109

102110
@pytest.fixture
103111
def census_df():
104-
return pd.read_csv('tests/by_doubling_time/2020-03-28_projected_census.csv', parse_dates=['date'])
112+
return pd.read_csv(
113+
"tests/by_doubling_time/2020-03-28_projected_census.csv", parse_dates=["date"]
114+
)

tests/penn_chime/test_models.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from datetime import date
2+
13
import pytest
24
import pandas as pd
35
import numpy as np
@@ -8,6 +10,8 @@
810
get_growth_rate,
911
)
1012

13+
from src.penn_chime.constants import EPSILON
14+
1115

1216
def test_sir():
1317
"""
@@ -80,3 +84,95 @@ def test_growth_rate():
8084
assert np.round(get_growth_rate(5) * 100.0, decimals=4) == 14.8698
8185
assert np.round(get_growth_rate(0) * 100.0, decimals=4) == 0.0
8286
assert np.round(get_growth_rate(-4) * 100.0, decimals=4) == -15.9104
87+
88+
89+
def test_model(model, param):
90+
# test the Model
91+
92+
assert round(model.infected, 0) == 45810.0
93+
assert isinstance(model.infected, float) # based off note in models.py
94+
95+
# test the class-calculated attributes
96+
# we're talking about getting rid of detection probability
97+
# assert model.detection_probability == 0.125
98+
assert model.intrinsic_growth_rate == 0.12246204830937302
99+
assert abs(model.beta - 4.21501347256401e-07) < EPSILON
100+
assert model.r_t == 2.307298374881539
101+
assert model.r_naught == 2.7144686763312222
102+
assert model.doubling_time_t == 7.764405988534983
103+
104+
105+
def test_model_raw_start(model, param):
106+
raw_df = model.raw_df
107+
108+
# test the things n_days creates, which in turn tests sim_sir, sir, and get_dispositions
109+
110+
# print('n_days: %s; i_day: %s' % (param.n_days, model.i_day))
111+
assert len(raw_df) == (len(np.arange(-model.i_day, param.n_days + 1))) == 104
112+
113+
first = raw_df.iloc[0, :]
114+
second = raw_df.iloc[1, :]
115+
116+
assert first.susceptible == 499600.0
117+
assert round(second.infected, 0) == 449.0
118+
assert list(model.dispositions_df.iloc[0, :]) == [
119+
-43,
120+
date(year=2020, month=2, day=14),
121+
1.0,
122+
0.4,
123+
0.2,
124+
]
125+
assert round(raw_df.recovered[30], 0) == 7083.0
126+
127+
d, dt, s, i, r = list(model.dispositions_df.iloc[60, :])
128+
assert dt == date(year=2020, month=4, day=14)
129+
assert [round(v, 0) for v in (d, s, i, r)] == [17, 549.0, 220.0, 110.0]
130+
131+
132+
def test_model_conservation(param, model):
133+
raw_df = model.raw_df
134+
135+
assert (0.0 <= raw_df.susceptible).all()
136+
assert (0.0 <= raw_df.infected).all()
137+
assert (0.0 <= raw_df.recovered).all()
138+
139+
diff = raw_df.susceptible + raw_df.infected + raw_df.recovered - param.population
140+
assert (diff < 0.1).all()
141+
142+
assert (raw_df.susceptible <= param.population).all()
143+
assert (raw_df.infected <= param.population).all()
144+
assert (raw_df.recovered <= param.population).all()
145+
146+
147+
def test_model_raw_end(param, model):
148+
raw_df = model.raw_df
149+
last = raw_df.iloc[-1, :]
150+
assert round(last.susceptible, 0) == 83391.0
151+
152+
153+
def test_model_monotonicity(param, model):
154+
raw_df = model.raw_df
155+
156+
# Susceptible population should be non-increasing, and Recovered non-decreasing
157+
assert (raw_df.susceptible[1:] - raw_df.susceptible.shift(1)[1:] <= 0).all()
158+
assert (raw_df.recovered[1:] - raw_df.recovered.shift(1)[1:] >= 0).all()
159+
160+
161+
def test_model_cumulative_census(param, model):
162+
# test that census is being properly calculated
163+
raw_df = model.raw_df
164+
admits_df = model.admits_df
165+
df = pd.DataFrame(
166+
{
167+
"hospitalized": admits_df.hospitalized,
168+
"icu": admits_df.icu,
169+
"ventilated": admits_df.ventilated,
170+
}
171+
)
172+
admits = df.cumsum()
173+
174+
# TODO: is 1.0 for ceil function?
175+
diff = admits.hospitalized[1:-1] - (
176+
0.05 * 0.05 * (raw_df.infected[1:-1] + raw_df.recovered[1:-1]) - 1.0
177+
)
178+
assert (diff.abs() < 0.1).all()

tests/penn_chime/test_settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def test_defaults_repr(DEFAULTS):
2+
repr(DEFAULTS)
3+
# TODO: Add assertions here

tests/test_app.py

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)