-
Notifications
You must be signed in to change notification settings - Fork 0
/
SIR_Lattice.py
384 lines (267 loc) · 9.07 KB
/
SIR_Lattice.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
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
import random
import numpy as np
import matplotlib.pyplot as plt
import copy
class SIR_Lattice(object):
"""
Class for a SIR_Lattice object
"""
def __init__(self, N, lattice=None, immune = 0. ):
"""
Initialisation function for SIR Lattice class.
Parameters
----------
N : int
determines size of square lattce (NxN)
lattice : str
starting lattice with options: "uniform" or "blobby" (see functions for specifics)
immune : float
fraction of cells to start immune
Returns
-------
"""
#initialise variables
self.immune = immune
self.N = N
self.sweep_list = []
# N**2 is the required length of time between visualisations as in the lecture notes
self.sweep_size = self.N ** 2
if type(lattice) == np.ndarray: # 'numpy.ndarray':
if lattice.shape == (N, N):
# print("yeye")
self.lattice = lattice
else:
print("input lattice must be correct shape!!")
elif lattice == "uniform":
# assume uniform
self.lattice = self.uniform_generate()
elif lattice == "blobby":
# assume blobF
self.lattice = self.blobby_generate()
else:
print("Must provide appropriate lattice input")
self.dynamics = self.SIR
def uniform_generate(self):
"""
Generates "uniform" lattice, where each point on the lattice has equal probability of being -1 or 1
Parameters
----------
Returns
-------
a : numpy array
uiform (NxN) array
"""
#S, I, R
probs = [1/3, 1/3, 1/3, self.immune]
probs = probs / np.sum(probs)
print(probs)
a = np.random.choice((0, 1, 10,100), size=[self.N, self.N], replace=True, p=probs)
return a
def blobby_generate(self):
"""
Generates "blobby" lattice, where one area of the lattice begins infected
Parameters
----------
Returns
-------
a : numpy array
blob (NxN) array
"""
#S, I, R
a = np.zeros((self.N, self.N))
a[15:20,15:20] = 1
return a
def find_variance(self, nums = None):
"""
Function to calculate the variance
Parameters
----------
nums: optional array
number or infected accross many sample sweep
Returns
-------
err : float
error in variance given by jacknife method
"""
if nums is None:
nums = np.array(self.num_inf)
var = (np.mean(nums ** 2) - np.mean(nums) ** 2) / self.N ** 2
if self.terminate:
#as if an infinite number of samples were taken, it would go to 0 anyway
return 0
else:
return var
def jacknife_var(self):
"""
Function to calculate the error in variance by jacknife method
Parameters
----------
Returns
-------
err : float
error in variance given by jacknife method
"""
var = self.find_variance()
total_nums = self.num_inf
sums = 0
for i in range(len(total_nums) - 1):
mini_nums = np.hstack((total_nums[:i], total_nums[i:-1]))
# print(Es)
var_i = self.find_variance(nums=mini_nums)
sums += (var_i - var) ** 2
err = np.sqrt(sums)
return err
def find_sum_NN(self):
"""
Finds how many nearest neightbours for the entire lattice
Parameters
----------
Returns
-------
"""
#initialises 0 array, then counts NN by rolling
NN_sum = np.zeros_like(self.lattice)
NN_sum += np.roll(self.lattice, 1, axis=0)
NN_sum += np.roll(self.lattice, -1, axis=0)
NN_sum += np.roll(self.lattice, 1, axis=1)
NN_sum += np.roll(self.lattice, -1, axis=1)
return NN_sum
def SIR(self):
"""
Function to do update step for SIRS
Parameters
----------
Returns
-------
"""
# randomly choose a point to assess
flip_coords = self.rand_flip_coords()
state = self.lattice[flip_coords[0], flip_coords[1]]
if state == 0:
#susc
NN = [self.lattice[(flip_coords[0]-1)%self.N,flip_coords[1]],self.lattice[(flip_coords[0]+1)%self.N,flip_coords[1]],
self.lattice[flip_coords[0],(flip_coords[1]-1)%self.N],self.lattice[flip_coords[0],(flip_coords[1]+1)%self.N]]
#find if Nearest Neighbours are infected
NN_inf = False
for i in NN:
if i == 1:
NN_inf = True
if NN_inf:
if np.random.uniform(0,1)-self.p1<0:
new_state = 1
else:
new_state = state
else:
new_state = state
elif state == 1:
#infected
if np.random.uniform(0,1)-self.p2<0:
new_state = 10
else:
new_state = state
elif state ==10:
#recovered
if np.random.uniform(0,1)-self.p3<0:
new_state = 0
else:
new_state = state
elif state ==100:
#immune
new_state = state
else:
print("somethings fucked")
print(state)
self.lattice[flip_coords[0], flip_coords[1]] = new_state
def rand_flip_coords(self):
"""
Function to choose a random point on the lattice
Parameters
----------
Returns
-------
rand_coords : numpy array
[x,y] random point on graph
"""
# randint is very slow
r = int(self.N * random.random())
r_2 = int(self.N * random.random())
# return np.random.randint(0,self.N,2)
return np.asarray([r, r_2])
def plot_lattice(self):
"""
Function to plot the lattice
It does not write to file to simplify the procedure.
It plots directly from self.lattice.
Parameters
----------
Returns
-------
"""
plt.clf()
plt.text(-0.1,-1.1, "Susc: black, Inf: orange, Recov: yellow, Immune: white", fontsize=14)#, verticalalignment='top')
lat = copy.copy(self.lattice)
lat[lat == 10] = 2
lat[lat == 100] = 3
im = plt.imshow(lat, animated=True, cmap = "hot", vmin=0, vmax=3,)#Oranges")
#plt.colorbar(im)
plt.draw()
plt.pause(0.1)
def find_frac_inf(self):
"""
Finds the fraction of lattice that are infected
Parameters
----------
Returns
-------
"""
num_inf = len(self.lattice[self.lattice == 1])
self.num_inf.append(num_inf)
frac_inf = num_inf/self.N**2
self.frac_inf.append(frac_inf)
if frac_inf ==0:
self.terminate = True
def run(self, probs, wait_sweeps=100, num_tot_sweeps=1000, plot_anim=True):
"""
Function to run full SIRS given simulation inputs.
It uses the specified dynamics of the system and simply loops through when required.
Parameters
----------
probs: arraylike
[p1,p2,p3], the probabilities of S --> I, I-->R AND R --> S
wait_sweeps : int
number of sweeps to wait before starting measurements
num_total_sweeps : int
number of total sweeps to run
plot_anim : bool
whether or not to animate as it runs. Saves time on long simulations to not animate.
Returns
-------
"""
#have to invert the probabilities because I kinda coded it in reverse
self.p1 =probs[0]
self.p2 =probs[1]
self.p3 =probs[2]
self.frac_inf = []
self.num_inf = []
self.terminate = False
# loop for required number of sweeps
for i in range(num_tot_sweeps* self.sweep_size):
# self.N**2 is the required length of time between visualisations as in the lecture notes
if i % self.sweep_size == 0:
# if it is time to take a measurement
if i % (self.sweep_size*10) == 0 and i >= wait_sweeps * self.sweep_size:
self.sweep_list.append(i / self.sweep_size)
self.find_frac_inf()
# plot animation if required
if plot_anim:
self.plot_lattice()
# print every 100 sweeps to check the sim progress
if i % (self.sweep_size * 100) == 0:
print(i / self.sweep_size)
if self.terminate:
print("breaking")
break
# run dynamics
self.dynamics()
plt.show()
#self.plot_frac_inf()