2
2
3
3
import pytest
4
4
import pandas as pd
5
-
5
+ import numpy as np
6
6
7
7
from app import (projection_admits , alt )
8
8
from penn_chime .models import sir , sim_sir , sim_sir_df
9
+ from penn_chime .parameters import Parameters
9
10
from penn_chime .presentation import display_header , new_admissions_chart
10
11
from penn_chime .settings import DEFAULTS
12
+ from penn_chime .defaults import RateLos
11
13
12
14
13
15
# set up
@@ -69,7 +71,7 @@ def test_header_fail():
69
71
st .cleanup ()
70
72
71
73
72
- def test_defaultS_repr ():
74
+ def test_defaults_repr ():
73
75
"""
74
76
Test DEFAULTS.repr
75
77
"""
@@ -82,36 +84,42 @@ def test_sir():
82
84
"""
83
85
Someone who is good at testing, help
84
86
"""
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 == (
86
89
0.7920792079207921 ,
87
90
0.20297029702970298 ,
88
91
0.0049504950495049506 ,
89
92
), "This contrived example should work"
90
93
94
+ assert isinstance (sir_test , tuple )
95
+ for v in sir_test :
96
+ assert isinstance (v , float )
97
+ assert v >= 0
98
+
91
99
# Certain things should *not* work
92
- with pytest .raises (TypeError ) as E :
100
+ with pytest .raises (TypeError ) as error :
93
101
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'"
95
103
96
- with pytest .raises (TypeError ) as E :
104
+ with pytest .raises (TypeError ) as error :
97
105
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'"
99
107
100
- with pytest .raises (TypeError ) as E :
108
+ with pytest .raises (TypeError ) as error :
101
109
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'"
103
111
104
- with pytest .raises (TypeError ) as E :
112
+ with pytest .raises (TypeError ) as error :
105
113
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'"
107
115
108
- with pytest .raises (TypeError ) as E :
116
+ with pytest .raises (TypeError ) as error :
109
117
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'"
111
119
112
- with pytest .raises (TypeError ) as E :
120
+ with pytest .raises (TypeError ) as error :
113
121
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'"
115
123
116
124
# Zeros across the board should fail
117
125
with pytest .raises (ZeroDivisionError ):
@@ -122,7 +130,8 @@ def test_sim_sir():
122
130
"""
123
131
Rounding to move fast past decimal place issues
124
132
"""
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
126
135
127
136
assert round (s [0 ], 0 ) == 5
128
137
assert round (i [0 ], 2 ) == 6
@@ -131,6 +140,11 @@ def test_sim_sir():
131
140
assert round (i [- 1 ], 2 ) == 0.18
132
141
assert round (r [- 1 ], 2 ) == 17.82
133
142
143
+ assert isinstance (sim_sir_test , tuple )
144
+ for v in sim_sir_test :
145
+ assert isinstance (v , np .ndarray )
146
+
147
+
134
148
135
149
def test_sim_sir_df ():
136
150
"""
@@ -148,32 +162,59 @@ def test_sim_sir_df():
148
162
assert round (last [2 ], 2 ) == 17.82
149
163
150
164
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
-
170
165
def test_new_admissions_chart ():
171
166
chart = new_admissions_chart (alt , projection_admits , 60 - 10 )
172
- assert type (chart ) == alt .Chart
167
+ assert isinstance (chart , alt .Chart )
173
168
assert chart .data .iloc [1 ].Hospitalized < 1
174
169
# assert round(chart.data.iloc[49].ICU, 0) == 43
175
170
with pytest .raises (TypeError ):
176
171
new_admissions_chart ()
177
172
178
173
empty_chart = new_admissions_chart (alt , pd .DataFrame (), - 1 )
179
174
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