forked from DataForScience/Epidemiology101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EpiModel.py
331 lines (232 loc) · 9.6 KB
/
EpiModel.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
### −∗− mode : python ; −∗−
# @file EpiModel.py
# @author Bruno Goncalves
######################################################
import networkx as nx
import numpy as np
from numpy import linalg
from numpy import random
import scipy.integrate
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm
tqdm.pandas()
class EpiModel(object):
"""Simple Epidemic Model Implementation
Provides a way to implement and numerically integrate
"""
def __init__(self, compartments=None):
self.transitions = nx.MultiDiGraph()
self.seasonality = None
if compartments is not None:
self.transitions.add_nodes_from([comp for comp in compartments])
def add_interaction(self, source, target, agent, rate):
self.transitions.add_edge(source, target, agent=agent, rate=rate)
def add_spontaneous(self, source, target, rate):
self.transitions.add_edge(source, target, rate=rate)
def add_vaccination(self, source, target, rate, start):
self.transitions.add_edge(source, target, rate=rate, start=start)
def _new_cases(self, population, time, pos):
"""Internal function used by integration routine"""
diff = np.zeros(len(pos))
N = np.sum(population)
for edge in self.transitions.edges(data=True):
source = edge[0]
target = edge[1]
trans = edge[2]
rate = trans['rate']*population[pos[source]]
if 'start' in trans and trans['start'] >= time:
continue
if 'agent' in trans:
agent = trans['agent']
rate *= population[pos[agent]]/N
if self.seasonality is not None:
curr_t = int(time)%365
season = float(self.seasonality[curr_t])
rate *= season
diff[pos[source]] -= rate
diff[pos[target]] += rate
return diff
def plot(self, title=None, normed=True, **kwargs):
"""Convenience function for plotting"""
try:
if normed:
N = self.values_.iloc[0].sum()
ax = (self.values_/N).plot(**kwargs)
else:
ax = self.values_.plot(**kwargs)
ax.set_xlabel('Time')
ax.set_ylabel('Population')
if title is not None:
ax.set_title(title)
return ax
except:
raise NotInitialized('You must call integrate() first')
def __getattr__(self, name):
"""Dynamic method to return the individual compartment values"""
if 'values_' in self.__dict__:
return self.values_[name]
else:
raise AttributeError("'EpiModel' object has no attribute '%s'" % name)
def simulate(self, timesteps, t_min=1, seasonality=None, **kwargs):
"""Stochastically simulate the epidemic model"""
pos = {comp: i for i, comp in enumerate(self.transitions.nodes())}
population=np.zeros(len(pos), dtype='int')
for comp in kwargs:
population[pos[comp]] = kwargs[comp]
values = []
values.append(population)
comps = list(self.transitions.nodes)
time = np.arange(t_min, t_min+timesteps, 1, dtype='int')
self.seasonality = seasonality
for t in time:
pop = values[-1]
new_pop = values[-1].copy()
N = np.sum(pop)
for comp in comps:
trans = list(self.transitions.edges(comp, data=True))
prob = np.zeros(len(comps), dtype='float')
for _, node_j, data in trans:
source = pos[comp]
target = pos[node_j]
rate = data['rate']
if 'start' in data and data['start'] >= t:
continue
if 'agent' in data:
agent = pos[data['agent']]
rate *= pop[agent]/N
if self.seasonality is not None:
curr_t = int(t)%365
season = float(self.seasonality[curr_t])
rate *= season
prob[target] = rate
prob[source] = 1-np.sum(prob)
delta = random.multinomial(pop[source], prob)
delta[source] = 0
changes = np.sum(delta)
if changes == 0:
continue
new_pop[source] -= changes
for i in range(len(delta)):
new_pop[i] += delta[i]
values.append(new_pop)
values = np.array(values)
self.values_ = pd.DataFrame(values[1:], columns=comps, index=time)
def integrate(self, timesteps, t_min=1, seasonality=None, **kwargs):
"""Numerically integrate the epidemic model"""
pos = {comp: i for i, comp in enumerate(self.transitions.nodes())}
population=np.zeros(len(pos))
for comp in kwargs:
population[pos[comp]] = kwargs[comp]
time = np.arange(t_min, t_min+timesteps, 1)
self.seasonality = seasonality
self.values_ = pd.DataFrame(scipy.integrate.odeint(self._new_cases, population, time, args=(pos,)), columns=pos.keys(), index=time)
def __repr__(self):
text = 'Epidemic Model with %u compartments and %u transitions:\n\n' % \
(self.transitions.number_of_nodes(),
self.transitions.number_of_edges())
for edge in self.transitions.edges(data=True):
source = edge[0]
target = edge[1]
trans = edge[2]
rate = trans['rate']
if 'agent' in trans:
agent = trans['agent']
text += "%s + %s = %s %f\n" % (source, agent, target, rate)
elif 'start' in trans:
start = trans['start']
text+="%s -> %s %f starting at %s days\n" % (source, target, rate, start)
else:
text+="%s -> %s %f\n" % (source, target, rate)
R0 = self.R0()
if R0 is not None:
text += "\nR0=%1.2f" % R0
return text
def _get_active(self):
active = set()
for node_i, node_j, data in self.transitions.edges(data=True):
if "agent" in data:
active.add(data['agent'])
else:
active.add(node_i)
return active
def _get_susceptible(self):
susceptible = set([node for node, deg in self.transitions.in_degree() if deg==0])
if len(susceptible) == 0:
for node_i, node_j, data in self.transitions.edges(data=True):
if "agent" in data:
susceptible.add(node_i)
return susceptible
def _get_infections(self):
inf = {}
for node_i, node_j, data in self.transitions.edges(data=True):
if "agent" in data:
agent = data['agent']
if agent not in inf:
inf[agent] = {}
if node_i not in inf[agent]:
inf[agent][node_i] = {}
inf[agent][node_i]['target'] = node_j
inf[agent][node_i]['rate'] = data['rate']
return inf
def R0(self):
infected = set()
susceptible = self._get_susceptible()
for node_i, node_j, data in self.transitions.edges(data=True):
if "agent" in data:
infected.add(data['agent'])
infected.add(node_j)
infected = sorted(infected)
N_infected = len(infected)
F = np.zeros((N_infected, N_infected), dtype='float')
V = np.zeros((N_infected, N_infected), dtype='float')
pos = dict(zip(infected, np.arange(N_infected)))
try:
for node_i, node_j, data in self.transitions.edges(data=True):
rate = data['rate']
if "agent" in data:
target = pos[node_j]
agent = pos[data['agent']]
if node_i in susceptible:
F[target, agent] = rate
elif "start" in data:
continue
else:
source = pos[node_i]
V[source, source] += rate
if node_j in pos:
target = pos[node_j]
V[target, source] -= rate
eig, v = linalg.eig(np.dot(F, linalg.inv(V)))
return eig.max()
except:
return None
def __getitem__(self, bla):
return self.values_[bla]
if __name__ == '__main__':
beta = 0.2
mu = 0.1
SIR = EpiModel()
SIR.add_interaction('S', 'I', 'I', beta)
SIR.add_spontaneous('I', 'R', mu)
SIR.add_vaccination('S', 'V', 0.01, 75)
SIR.add_spontaneous('VI', 'VR', mu)
SIR.add_interaction('V', 'VI', 'I', beta*(1-.8))
print(SIR)
N = 100000
I0 = 10
season = np.ones(365+1)
season[74:100] = 0.25
fig, ax = plt.subplots(1)
Nruns = 1000
values = []
#for i in tqdm(range(Nruns), total=Nruns):
SIR.integrate(365, S=.3*N-10, I=10, V=.7*N)
SIR[['I', 'VI', 'VR', 'R']].plot(ax=ax)
print(SIR.S.tail())
#ax.plot(SIR.I/N, lw=.1, c='b')
if SIR.I.max() > 10:
values.append(SIR.I)
values = pd.DataFrame(values)
(values.median(axis=0)/N).plot(ax=ax, c='r')
fig.savefig('SIR.png')