forked from paulvangentcom/python_corona_simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_simulation.py
More file actions
392 lines (297 loc) · 14.8 KB
/
simple_simulation.py
File metadata and controls
392 lines (297 loc) · 14.8 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
#set seed for reproducibility
np.random.seed(100)
def initialize_population(pop_size, mean_age=45, max_age=105,
xbounds = [0, 1], ybounds = [0, 1]):
'''initialized the population for the simulation
the population matrix for this simulation has the following columns:
0 : unique ID
1 : current x coordinate
2 : current y coordinate
3 : current heading in x direction
4 : current heading in y direction
5 : current speed
6 : current state (0=healthy, 1=sick, 2=immune, 3=dead)
7 : age
8 : infected_since (frame the person got infected)
9 : recovery vector (used in determining when someone recovers or dies)
Keyword arguments
-----------------
pop_size : int
the size of the population
mean_age : int
the mean age of the population. Age affects mortality chances
max_age : int
the max age of the population
xbounds : 2d array
lower and upper bounds of x axis
ybounds : 2d array
lower and upper bounds of y axis
'''
#initialize population matrix
population = np.zeros((pop_size, 10))
#initalize unique IDs
population[:,0] = [x for x in range(pop_size)]
#initialize random coordinates
population[:,1] = np.random.uniform(low = xbounds[0] + 0.05, high = xbounds[1] - 0.05,
size = (pop_size,))
population[:,2] = np.random.uniform(low = ybounds[0] + 0.05, high = ybounds[1] - 0.05,
size=(pop_size,))
#initialize random headings -1 to 1
population[:,3] = np.random.normal(loc = 0, scale = 1/3,
size=(pop_size,))
population[:,4] = np.random.normal(loc = 0, scale = 1/3,
size=(pop_size,))
#initialize random speeds
population[:,5] = np.random.normal(0.01, 0.01/3)
#initalize ages
std_age = (max_age - mean_age) / 3
population[:,7] = np.int32(np.random.normal(loc = mean_age,
scale = std_age,
size=(pop_size,)))
population[:,7] = np.clip(population[:,7], a_min = 0,
a_max = max_age) #clip those younger than 0 years
#build recovery_vector
#TODO: make risks age dependent
population[:,9] = np.random.normal(loc = 0.5, scale = 0.5 / 3, size=(pop_size,))
return population
def update_positions(population):
'''update positions of all people
Uses heading and speed to update all positions for
the next time step
Keyword arguments
-----------------
population : ndarray
the array numpy containing all the population information
'''
#update positions
#x
population[:,1] = population[:,1] + (population[:,3] * population[:,5])
#y
population[:,2] = population[:,2] + (population [:,4] * population[:,5])
return population
def out_of_bounds(population, xbounds, ybounds):
'''checks which people are about to go out of bounds and corrects
'''
#update headings and positions where out of bounds
#update x heading
#determine number of elements that need to be updated
shp = population[:,3][(population[:,1] <= xbounds[:,0]) &
(population[:,3] < 0)].shape
population[:,3][(population[:,1] <= xbounds[:,0]) &
(population[:,3] < 0)] = np.random.normal(loc = 0.5,
scale = 0.5/3,
size = shp)
shp = population[:,3][(population[:,1] >= xbounds[:,1]) &
(population[:,3] > 0)].shape
population[:,3][(population[:,1] >= xbounds[:,1]) &
(population[:,3] > 0)] = -np.random.normal(loc = 0.5,
scale = 0.5/3,
size = shp)
#update y heading
shp = population[:,4][(population[:,2] <= ybounds[:,0]) &
(population[:,4] < 0)].shape
population[:,4][(population[:,2] <= ybounds[:,0]) &
(population[:,4] < 0)] = np.random.normal(loc = 0.5,
scale = 0.5/3,
size = shp)
shp = population[:,4][(population[:,2] >= ybounds[:,1]) &
(population[:,4] > 0)].shape
population[:,4][(population[:,2] >= ybounds[:,1]) &
(population[:,4] > 0)] = -np.random.normal(loc = 0.5,
scale = 0.5/3,
size = shp)
return population
def update_randoms(population, heading_update_chance=0.02,
speed_update_chance=0.02):
'''updates random states such as heading and speed'''
#randomly update heading
#x
update = np.random.random(size=(pop_size,))
shp = update[update <= heading_update_chance].shape
population[:,3][update <= heading_update_chance] = np.random.normal(loc = 0,
scale = 1/3,
size = shp)
#y
update = np.random.random(size=(pop_size,))
shp = update[update <= heading_update_chance].shape
population[:,4][update <= heading_update_chance] = np.random.normal(loc = 0,
scale = 1/3,
size = shp)
#randomize speeds
update = np.random.random(size=(pop_size,))
shp = update[update <= heading_update_chance].shape
population[:,5][update <= heading_update_chance] = np.random.normal(loc = 0.01,
scale = 0.01/3,
size = shp)
return population
def infect(population, infection_range, infection_chance, frame):
'''finds new infections
Keyword arguments
-----------------
'''
#find new infections
infected_previous_step = population[population[:,6] == 1]
new_infections = []
#if less than half are infected, slice based on infected (to speed up computation)
if len(infected_previous_step) < (pop_size // 2):
for patient in infected_previous_step:
#define infection zone for patient
infection_zone = [patient[1] - infection_range, patient[2] - infection_range,
patient[1] + infection_range, patient[2] + infection_range]
#find healthy people surrounding infected patient
indices = np.int32(population[:,0][(infection_zone[0] < population[:,1]) &
(population[:,1] < infection_zone[2]) &
(infection_zone[1] < population [:,2]) &
(population[:,2] < infection_zone[3]) &
(population[:,6] == 0)])
for idx in indices:
#roll die to see if healthy person will be infected
if np.random.random() < infection_chance:
population[idx][6] = 1
population[idx][8] = frame
new_infections.append(idx)
else:
#if more than half are infected slice based in healthy people (to speed up computation)
healthy_previous_step = population[population[:,6] == 0]
sick_previous_step = population[population[:,6] == 1]
for person in healthy_previous_step:
#define infecftion range around healthy person
infection_zone = [person[1] - infection_range, person[2] - infection_range,
person[1] + infection_range, person[2] + infection_range]
if person[6] == 0: #if person is not already infected, find if infected are nearby
#find infected nearby healthy person
poplen = len(sick_previous_step[:,6][(infection_zone[0] < sick_previous_step[:,1]) &
(sick_previous_step[:,1] < infection_zone[2]) &
(infection_zone[1] < sick_previous_step [:,2]) &
(sick_previous_step[:,2] < infection_zone[3]) &
(sick_previous_step[:,6] == 1)])
if poplen > 0:
if np.random.random() < (infection_chance * poplen):
#roll die to see if healthy person will be infected
population[np.int32(person[0])][6] = 1
population[np.int32(person[0])][8] = frame
new_infections.append(np.int32(person[0]))
if len(new_infections) > 0:
print('at timestep %i these people got sick: %s' %(frame, new_infections))
return population
def recover_or_die(population, frame, recovery_duration, mortality_chance):
'''see whether to recover or die
'''
#find sick people
sick_people = population[population[:,6] == 1]
#define vector of how long everyone has been sick
illness_duration_vector = frame - sick_people[:,8]
recovery_odds_vector = (illness_duration_vector - recovery_duration[0]) / np.ptp(recovery_duration)
recovery_odds_vector = np.clip(recovery_odds_vector, a_min = 0, a_max = None)
#update states of sick people
indices = sick_people[:,0][recovery_odds_vector >= sick_people[:,9]]
cured = []
died = []
#decide whether to die or recover
for idx in indices:
if np.random.random() <= mortality_chance:
#die
sick_people[:,6][sick_people[:,0] == idx] = 3
died.append(np.int32(sick_people[sick_people[:,0] == idx][:,0][0]))
else:
#recover (become immune)
sick_people[:,6][sick_people[:,0] == idx] = 2
cured.append(np.int32(sick_people[sick_people[:,0] == idx][:,0][0]))
if len(died) > 0:
print('at timestep %i these people died: %s' %(frame, died))
if len(cured) > 0:
print('at timestep %i these people recovered: %s' %(frame, cured))
#put array back into population
population[population[:,6] == 1] = sick_people
return population
def update(frame, population, infection_range=0.01, infection_chance=0.03,
recovery_duration=(200, 500), mortality_chance=0.02,
xbounds=[0.02, 0.98], ybounds=[0.02, 0.98], wander_range=0.05,
visualise=True, infected_plot = []):
#add one infection to jumpstart
if frame == 50:
population[0][6] = 1
population[0][8] = 75
#update out of bounds
#define bounds arrays
_xbounds = np.array([[xbounds[0] + 0.02, xbounds[1] - 0.02]] * len(population))
_ybounds = np.array([[ybounds[0] + 0.02, ybounds[1] - 0.02]] * len(population))
population = out_of_bounds(population, _xbounds, _ybounds)
#update randoms
population = update_randoms(population)
#for dead ones: set speed and heading to 0
population[:,3:5][population[:,6] == 3] = 0
#update positions
population = update_positions(population)
#find new infections
population = infect(population, infection_range, infection_chance, frame)
infected_plot.append(len(population[population[:,6] == 1]))
#recover and die
population = recover_or_die(population, frame, recovery_duration, mortality_chance)
if visualise:
#construct plot and visualise
spec = fig.add_gridspec(ncols=1, nrows=2, height_ratios=[5,2])
ax1.clear()
ax2.clear()
ax1.set_xlim(xbounds[0] - 0.02, xbounds[1] + 0.02)
ax1.set_ylim(ybounds[0] - 0.02, ybounds[1] + 0.02)
healthy = population[population[:,6] == 0][:,1:3]
ax1.scatter(healthy[:,0], healthy[:,1], color='gray', s = 2, label='healthy')
infected = population[population[:,6] == 1][:,1:3]
ax1.scatter(infected[:,0], infected[:,1], color='red', s = 2, label='infected')
immune = population[population[:,6] == 2][:,1:3]
ax1.scatter(immune[:,0], immune[:,1], color='green', s = 2, label='immune')
fatalities = population[population[:,6] == 3][:,1:3]
ax1.scatter(fatalities[:,0], fatalities[:,1], color='black', s = 2, label='fatalities')
#add text descriptors
ax1.text(xbounds[0],
ybounds[1] + 0.03,
'timestep: %i, total: %i, healthy: %i infected: %i immune: %i fatalities: %i' %(frame,
len(population),
len(healthy),
len(infected),
len(immune),
len(fatalities)),
fontsize=6)
ax2.set_title('number of infected')
ax2.text(0, pop_size * 0.05,
'https://github.com/paulvangentcom/python-corona-simulation',
fontsize=6, alpha=0.5)
#ax2.set_xlim(0, simulation_steps)
ax2.set_ylim(0, pop_size + 100)
ax2.plot(infected_plot, color='gray')
#plt.savefig('render/%s.png' %frame)
if __name__ == '__main__':
#set simulation parameters
pop_size = 2000
simulation_steps = 10000
xbounds = [0, 1]
ybounds = [0, 1]
population = initialize_population(pop_size)
#define figure
fig = plt.figure(figsize=(5,7))
spec = fig.add_gridspec(ncols=1, nrows=2, height_ratios=[5,2])
ax1 = fig.add_subplot(spec[0,0])
plt.title('infection simulation')
plt.xlim(xbounds[0], xbounds[1])
plt.ylim(ybounds[0], ybounds[1])
ax2 = fig.add_subplot(spec[1,0])
ax2.set_title('number of infected')
ax2.set_xlim(0, simulation_steps)
ax2.set_ylim(0, pop_size + 100)
#create render folder if doesn't exist
if not os.path.exists('render/'):
os.makedirs('render/')
#start animation loop through matplotlib visualisation
animation = FuncAnimation(fig, update, fargs = (population,), frames = simulation_steps, interval = 33)
plt.show()
#alternatively dry run simulation without visualising
#for i in range(simulation_steps):
#update(i, population, visualise=False)
#sys.stdout.write('\r')
#sys.stdout.write('%i: %i/%i' %(i, len(infected), pop_size))