|
| 1 | +from datetime import date |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 | import pandas as pd
|
3 | 5 | import numpy as np
|
|
8 | 10 | get_growth_rate,
|
9 | 11 | )
|
10 | 12 |
|
| 13 | +from src.penn_chime.constants import EPSILON |
| 14 | + |
11 | 15 |
|
12 | 16 | def test_sir():
|
13 | 17 | """
|
@@ -80,3 +84,95 @@ def test_growth_rate():
|
80 | 84 | assert np.round(get_growth_rate(5) * 100.0, decimals=4) == 14.8698
|
81 | 85 | assert np.round(get_growth_rate(0) * 100.0, decimals=4) == 0.0
|
82 | 86 | 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() |
0 commit comments