-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSEIR.py
More file actions
74 lines (59 loc) · 1.97 KB
/
SEIR.py
File metadata and controls
74 lines (59 loc) · 1.97 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
from simulation import *
from population import *
from transitions import *
from numpy import random
wait_exp = lambda rate: lambda _: random.exponential(1 / rate)
N=5000000
beta = 0.5
infectious = lambda _: random.gamma(4, 1)
latent = lambda _: random.gamma(5, 1)
I0 = 20
E0 = 20
control_measures = [(0, 1), (40, 0.8), (60, 0.6), (80, 0.4), (100, 0.6)]
def eps(time):
n = len(control_measures) - 1
for i in range(n):
if control_measures[i][0] <= time < control_measures[i+1][0]:
return control_measures[i][1]
return control_measures[n][1]
#save = False
save = True
def to_transmit(time, sim, agent, from_state):
return random.rand() < eps(time)
def init(time, agent):
if agent.id < E0:
s = "E"
elif agent.id < I0 + E0:
s = "I"
else:
s = "S"
return State({"transmitted": []}) & State(s)
def run(times):
sim = Simulation("test", N)
rm = RandomMixing(sim)
sim.set(rm)
sim.set(Transition(State("E"), State("I"), latent))
sim.set(Transition(State("I"), State("R"), infectious))
sim.set(Transition(State("I") + State("S"),
State("I") + State("E"),
waiting_time=wait_exp(beta),
to_change_callback=to_transmit,
contact = rm))
sim.set(Counter(name="S", state=State("S")))
sim.set(Counter(name="E", state=State("E")))
sim.set(Counter(name="I", state=State("I")))
sim.set(Counter(name="R", state=State("R")))
sim.set(InitFunction(init))
return sim.run(times)
v = run(range(250))
t = v["times"]
print('t', "S", "E", "I", "R")
for i in range(len(t)):
print(t[i], v["S"][i], v["E"][i], v["I"][i], v["R"][i])
if save:
import csv
with open("SEIR.csv", 'w') as csvfile:
w = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
w.writerow(['t', "S", "E", "I", "R"])
for i in range(len(v["times"])):
w.writerow([t[i], v["S"][i], v["E"][i], v["I"][i], v["R"][i]])