|
| 1 | +from datetime import date |
| 2 | + |
| 3 | +import pytest |
| 4 | +import pandas as pd |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from src.penn_chime.models import ( |
| 8 | + sir, |
| 9 | + sim_sir_df, |
| 10 | + get_growth_rate, |
| 11 | +) |
| 12 | + |
| 13 | +from src.penn_chime.constants import EPSILON |
| 14 | + |
| 15 | + |
| 16 | +def test_sir(): |
| 17 | + """ |
| 18 | + Someone who is good at testing, help |
| 19 | + """ |
| 20 | + sir_test = sir(100, 1, 0, 0.2, 0.5, 1) |
| 21 | + assert sir_test == ( |
| 22 | + 0.7920792079207921, |
| 23 | + 0.20297029702970298, |
| 24 | + 0.0049504950495049506, |
| 25 | + ), "This contrived example should work" |
| 26 | + |
| 27 | + assert isinstance(sir_test, tuple) |
| 28 | + for v in sir_test: |
| 29 | + assert isinstance(v, float) |
| 30 | + assert v >= 0 |
| 31 | + |
| 32 | + # Certain things should *not* work |
| 33 | + with pytest.raises(TypeError) as error: |
| 34 | + sir("S", 1, 0, 0.2, 0.5, 1) |
| 35 | + assert str(error.value) == "can't multiply sequence by non-int of type 'float'" |
| 36 | + |
| 37 | + with pytest.raises(TypeError) as error: |
| 38 | + sir(100, "I", 0, 0.2, 0.5, 1) |
| 39 | + assert str(error.value) == "can't multiply sequence by non-int of type 'float'" |
| 40 | + |
| 41 | + with pytest.raises(TypeError) as error: |
| 42 | + sir(100, 1, "R", 0.2, 0.5, 1) |
| 43 | + assert str(error.value) == "unsupported operand type(s) for +: 'float' and 'str'" |
| 44 | + |
| 45 | + with pytest.raises(TypeError) as error: |
| 46 | + sir(100, 1, 0, "beta", 0.5, 1) |
| 47 | + assert str(error.value) == "bad operand type for unary -: 'str'" |
| 48 | + |
| 49 | + with pytest.raises(TypeError) as error: |
| 50 | + sir(100, 1, 0, 0.2, "gamma", 1) |
| 51 | + assert str(error.value) == "unsupported operand type(s) for -: 'float' and 'str'" |
| 52 | + |
| 53 | + with pytest.raises(TypeError) as error: |
| 54 | + sir(100, 1, 0, 0.2, 0.5, "N") |
| 55 | + assert str(error.value) == "unsupported operand type(s) for /: 'str' and 'float'" |
| 56 | + |
| 57 | + # Zeros across the board should fail |
| 58 | + with pytest.raises(ZeroDivisionError): |
| 59 | + sir(0, 0, 0, 0, 0, 0) |
| 60 | + |
| 61 | + |
| 62 | +def test_sim_sir(): |
| 63 | + """ |
| 64 | + Rounding to move fast past decimal place issues |
| 65 | + """ |
| 66 | + raw_df = sim_sir_df( |
| 67 | + 5, 6, 7, 0.1, 0, 0.1, 40, # s # i # r # gamma # i_day # beta1 # n_days1 |
| 68 | + ) |
| 69 | + |
| 70 | + first = raw_df.iloc[0, :] |
| 71 | + last = raw_df.iloc[-1, :] |
| 72 | + |
| 73 | + assert round(first.susceptible, 0) == 5 |
| 74 | + assert round(first.infected, 2) == 6 |
| 75 | + assert round(first.recovered, 0) == 7 |
| 76 | + assert round(last.susceptible, 2) == 0 |
| 77 | + assert round(last.infected, 2) == 0.18 |
| 78 | + assert round(last.recovered, 2) == 17.82 |
| 79 | + |
| 80 | + assert isinstance(raw_df, pd.DataFrame) |
| 81 | + |
| 82 | + |
| 83 | +def test_growth_rate(): |
| 84 | + assert np.round(get_growth_rate(5) * 100.0, decimals=4) == 14.8698 |
| 85 | + assert np.round(get_growth_rate(0) * 100.0, decimals=4) == 0.0 |
| 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