-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_should_send_message_gaen.py
300 lines (269 loc) · 10.6 KB
/
test_should_send_message_gaen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import datetime
import numpy as np
import unittest
from covid19sim.locations.city import City
class DummyContactBook(object):
pass
class DummyHuman(object):
pass
class DummyCity(object):
pass
class ShouldSendMessageGaenTests(unittest.TestCase):
def test_intervention_day(self):
"""
check returns false if not far enough from intervention day
"""
cur_day = 10
daily_update_message_budget_sent_gaen = 0
current_timestamp = datetime.datetime.now()
risk_change = 2
city = DummyCity()
city.conf = dict(
BURN_IN_DAYS=2,
DAYS_BETWEEN_MESSAGES=2,
INTERVENTION_DAY=10,
UPDATES_PER_DAY=4,
MESSAGE_BUDGET_GAEN=1,
n_people=1000,
)
city.rng = np.random.RandomState(0)
city.risk_change_hist = {0: 12, 1: 1}
city.risk_change_histogram_sum = sum(city.risk_change_hist.values())
city.sent_messages_by_day = {cur_day: daily_update_message_budget_sent_gaen}
human = DummyHuman()
human.contact_book = DummyContactBook()
human.contact_book.latest_update_time = current_timestamp - datetime.timedelta(days=cur_day)
res = City._check_should_send_message_gaen(
city,
current_day_idx=cur_day,
current_timestamp=current_timestamp,
human=human,
risk_change_score=risk_change,
)
self.assertFalse(res)
city.conf["INTERVENTION_DAY"] = 9
res = City._check_should_send_message_gaen(
city,
current_day_idx=cur_day,
current_timestamp=current_timestamp,
human=human,
risk_change_score=risk_change,
)
self.assertFalse(res)
def test_last_update(self):
"""
check returns false if last update is too recent
"""
cur_day = 10
daily_update_message_budget_sent_gaen = 0
current_timestamp = datetime.datetime.now()
risk_change = 2
city = DummyCity()
city.conf = dict(
BURN_IN_DAYS=2,
DAYS_BETWEEN_MESSAGES=2,
INTERVENTION_DAY=5,
UPDATES_PER_DAY=4,
MESSAGE_BUDGET_GAEN=1,
n_people=1000,
)
city.rng = np.random.RandomState(0)
city.risk_change_histogram = {0: 12, 1: 1}
city.risk_change_histogram_sum = sum(city.risk_change_histogram.values())
city.sent_messages_by_day = {cur_day: daily_update_message_budget_sent_gaen}
human = DummyHuman()
human.contact_book = DummyContactBook()
human.contact_book.latest_update_time = current_timestamp
res = City._check_should_send_message_gaen(
city,
current_day_idx=cur_day,
current_timestamp=current_timestamp,
human=human,
risk_change_score=risk_change,
)
self.assertFalse(res)
def test_should_send_risk_change_true_det(self):
"""
check returns True if in last bucket, which is smaller than total message budget
"""
cur_day = 10
daily_update_message_budget_sent_gaen = 0
current_timestamp = datetime.datetime.now()
risk_change = 1
city = DummyCity()
city.conf = dict(
BURN_IN_DAYS=2,
DAYS_BETWEEN_MESSAGES=2,
INTERVENTION_DAY=5,
UPDATES_PER_DAY=4,
MESSAGE_BUDGET_GAEN=1,
n_people=1000,
)
city.rng = np.random.RandomState(0)
city.risk_change_histogram = {0: 1000, 1: 1}
city.risk_change_histogram_sum = sum(city.risk_change_histogram.values())
city.sent_messages_by_day = {cur_day: daily_update_message_budget_sent_gaen}
human = DummyHuman()
human.contact_book = DummyContactBook()
human.contact_book.latest_update_time = current_timestamp - datetime.timedelta(days=cur_day)
res = City._check_should_send_message_gaen(
city,
current_day_idx=cur_day,
current_timestamp=current_timestamp,
human=human,
risk_change_score=risk_change,
)
self.assertTrue(res)
def test_last_bucket_prob(self):
"""
check if you're in the last bucket but it's larger than message budget, total messages = budget for this update (=> /UPDATES_PER_DAY)
"""
cur_day = 10
daily_update_message_budget_sent_gaen = 0
current_timestamp = datetime.datetime.now()
risk_change = 1 # risk_change HAS to be in risk_change_histogram
city = DummyCity()
city.conf = dict(
BURN_IN_DAYS=2,
DAYS_BETWEEN_MESSAGES=1,
INTERVENTION_DAY=5,
UPDATES_PER_DAY=4,
MESSAGE_BUDGET_GAEN=1,
n_people=1000,
)
city.rng = np.random.RandomState(0)
city.risk_change_histogram = {0: 60, 1: 40}
city.risk_change_histogram_sum = sum(city.risk_change_histogram.values())
city.sent_messages_by_day = {cur_day: daily_update_message_budget_sent_gaen}
human = DummyHuman()
human.contact_book = DummyContactBook()
human.contact_book.latest_update_time = current_timestamp - datetime.timedelta(days=cur_day)
results = []
for i in range(1000):
res = City._check_should_send_message_gaen(
city,
current_day_idx=cur_day,
current_timestamp=current_timestamp,
human=human,
risk_change_score=risk_change,
)
results.append(res)
if res:
if cur_day not in city.sent_messages_by_day:
city.sent_messages_by_day[cur_day] = 0
city.sent_messages_by_day[cur_day] += 1
self.assertAlmostEqual(1 / 4, np.mean(results), 2)
def test_middle_bucket_prob(self):
"""
checks that if in previous to last bucket and last bucket is smaller than
budget, then messages sent correspond to the number of remaining messages
"""
cur_day = 10
daily_update_message_budget_sent_gaen = 0
current_timestamp = datetime.datetime.now()
risk_change = 1 # risk_change HAS to be in risk_change_histogram
city = DummyCity()
city.conf = dict(
BURN_IN_DAYS=2,
DAYS_BETWEEN_MESSAGES=1,
INTERVENTION_DAY=5,
UPDATES_PER_DAY=4,
MESSAGE_BUDGET_GAEN=1,
n_people=1000,
)
city.rng = np.random.RandomState(0)
city.risk_change_histogram = {0: 50, 1: 40, 2: 10}
city.risk_change_histogram_sum = sum(city.risk_change_histogram.values())
city.sent_messages_by_day = {cur_day: daily_update_message_budget_sent_gaen}
human = DummyHuman()
human.contact_book = DummyContactBook()
human.contact_book.latest_update_time = current_timestamp - datetime.timedelta(days=cur_day)
results = []
for i in range(1000):
res = City._check_should_send_message_gaen(
city,
current_day_idx=cur_day,
current_timestamp=current_timestamp,
human=human,
risk_change_score=risk_change,
)
results.append(res)
if res:
if cur_day not in city.sent_messages_by_day:
city.sent_messages_by_day[cur_day] = 0
city.sent_messages_by_day[cur_day] += 1
# allowed messages: 100 / 4 = 25
# already sent messages: 10
# remaining to send for second bucket: 15
self.assertAlmostEqual(1 / 4 - 10 / 100, np.mean(results), 2)
def test_middle_bucket_last_is_full(self):
"""
If the last bucket is larger than the budget then no message is sent when in the second largest bucket
"""
cur_day = 10
daily_update_message_budget_sent_gaen = 0
current_timestamp = datetime.datetime.now()
risk_change = 1 # risk_change HAS to be in risk_change_histogram
city = DummyCity()
city.conf = dict(
BURN_IN_DAYS=2,
DAYS_BETWEEN_MESSAGES=1,
INTERVENTION_DAY=5,
UPDATES_PER_DAY=4,
MESSAGE_BUDGET_GAEN=1,
n_people=1000,
)
city.rng = np.random.RandomState(0)
city.risk_change_histogram = {0: 40, 1: 20, 2: 40}
city.risk_change_histogram_sum = sum(city.risk_change_histogram.values())
city.sent_messages_by_day = {cur_day: daily_update_message_budget_sent_gaen}
human = DummyHuman()
human.contact_book = DummyContactBook()
human.contact_book.latest_update_time = current_timestamp - datetime.timedelta(days=cur_day)
res = City._check_should_send_message_gaen(
city,
current_day_idx=cur_day,
current_timestamp=current_timestamp,
human=human,
risk_change_score=risk_change,
)
self.assertFalse(res)
def test_last_bucket_low_budget(self):
"""
Everything works still with a very low budget
"""
cur_day = 10
daily_update_message_budget_sent_gaen = 0
current_timestamp = datetime.datetime.now()
risk_change = 2 # risk_change HAS to be in risk_change_histogram
city = DummyCity()
city.conf = dict(
BURN_IN_DAYS=2,
DAYS_BETWEEN_MESSAGES=2,
INTERVENTION_DAY=5,
UPDATES_PER_DAY=4,
MESSAGE_BUDGET_GAEN=0.01,
n_people=1000,
)
city.rng = np.random.RandomState(0)
city.risk_change_histogram = {0: 40, 1: 20, 2: 40}
city.risk_change_histogram_sum = sum(city.risk_change_histogram.values())
city.sent_messages_by_day = {cur_day: daily_update_message_budget_sent_gaen}
human = DummyHuman()
human.contact_book = DummyContactBook()
human.contact_book.latest_update_time = current_timestamp - datetime.timedelta(days=cur_day)
results = []
for i in range(1000):
res = City._check_should_send_message_gaen(
city,
current_day_idx=cur_day,
current_timestamp=current_timestamp,
human=human,
risk_change_score=risk_change,
)
results.append(res)
if res:
if cur_day not in city.sent_messages_by_day:
city.sent_messages_by_day[cur_day] = 0
city.sent_messages_by_day[cur_day] += 1
self.assertAlmostEqual(city.conf["MESSAGE_BUDGET_GAEN"] / 4, np.mean(results), 2)