forked from ama-yash/SimulatorX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompartmental_models.py
175 lines (170 loc) · 8.09 KB
/
compartmental_models.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
import random
from count import count_all_si,count_all_sir
from covid_19_selectors import infect as cov_inf, sis_recover as cov_sis_recover, sir_recover as cov_sir_recover
from gen_simulation_tools import infect as gen_inf, recover_sir as gen_sir_recover, recover_sis as gen_sis_recover
from gen_simulation_tools import init_si_counts, update_si_counts, init_sir_counts, update_sir_counts
from genNode import generateNodes
from selector.selector import (activate_graph, load_susceptibility_matrix,
power_law, seed_graph)
class Models():
#initialise parameters, generate network, and preparing for simulation
def __init__(self, mode, parameters):
self.mode = mode
self.N = int(parameters["pop_size"])
ethn = {
"white": parameters["white"],
"black": parameters["black"],
"asian": parameters["asian"],
"other": parameters["other"],
}
gen = {"male": parameters["male"], "female": parameters["female"]}
ag = {
"child": parameters["child"],
"adult": parameters["adult"],
"senior": parameters["senior"],
}
self.seeds = float(parameters["seeds"])
self.time = int(parameters["time"])
self.graph_code = int(parameters["graph_code"])
self.m = int(parameters["m"])
self.G = generateNodes(self.N, ethn, gen, ag, self.graph_code, self.m)
if self.graph_code == 0:
epsilon = 0.001
gamma = -2.1
self.act = power_law(epsilon, 1, gamma, self.N)
if mode == 'manual':
self.infection_parameters = {
"white": float(parameters["white_inf"]),
"black": float(parameters["black_inf"]),
"asian": float(parameters["asian_inf"]),
"other": float(parameters["other_inf"]),
"male": float(parameters["male_inf"]),
"female": float(parameters["female_inf"]),
"child": float(parameters["child_inf"]),
"adult": float(parameters["adult_inf"]),
"senior": float(parameters["senior_inf"]),
}
if parameters["model"] != 0:
self.recovery_parameters = {
"white": float(parameters["white_rec"]),
"black": float(parameters["black_rec"]),
"asian": float(parameters["asian_rec"]),
"other": float(parameters["other_rec"]),
"male": float(parameters["male_rec"]),
"female": float(parameters["female_rec"]),
"child": float(parameters["child_rec"]),
"adult": float(parameters["adult_rec"]),
"senior": float(parameters["senior_rec"]),
}
else:
self.dataframe = load_susceptibility_matrix()
#perform SI compartment model
def si(self):
count_dicts = init_si_counts()
seed_graph(self.G, self.seeds)
if self.mode == 'manual':
for i in range(self.time):
if self.graph_code == 0:
active_nodes = activate_graph(self.act, self.N)
for i in active_nodes:
count = 0
while count < self.m:
target = random.randint(0, self.N - 1)
if target != i and target not in self.G.neighbors(i):
self.G.add_edge(i, target)
count += 1
else:
active_nodes = self.G.nodes()
gen_inf(active_nodes, self.G, self.infection_parameters)
data = count_all_si(self.G)
count_dicts = update_si_counts(count_dicts, data)
return count_dicts
elif self.mode == 'covid':
for t in range(self.time):
active_nodes = activate_graph(self.act, self.N)
for i in active_nodes:
count = 0
while count < self.m:
target = random.randint(0, self.N - 1)
if target != i and target not in self.G.neighbors(i):
self.G.add_edge(i, target)
count += 1
cov_inf(active_nodes, self.G, self.dataframe)
data = count_all_si(self.G)
count_dicts = update_si_counts(count_dicts, data)
return count_dicts
#perform SIS compartment model
def sis(self):
count_dicts = init_si_counts()
seed_graph(self.G, self.seeds)
if self.mode == 'manual':
for i in range(self.time):
if self.graph_code == 0:
active_nodes = activate_graph(self.act, self.N)
for i in active_nodes:
count = 0
while count < self.m:
target = random.randint(0, self.N - 1)
if target != i and target not in self.G.neighbors(i):
self.G.add_edge(i, target)
count += 1
else:
active_nodes = self.G.nodes()
gen_inf(active_nodes, self.G, self.infection_parameters)
gen_sis_recover(active_nodes,self.G,self.recovery_parameters)
data = count_all_si(self.G)
count_dicts = update_si_counts(count_dicts, data)
return count_dicts
elif self.mode == 'covid':
for t in range(self.time):
active_nodes = activate_graph(self.act, self.N)
for i in active_nodes:
count = 0
while count < self.m:
target = random.randint(0, self.N - 1)
if target != i and target not in self.G.neighbors(i):
self.G.add_edge(i, target)
count += 1
cov_inf(active_nodes, self.G, self.dataframe)
cov_sis_recover(active_nodes,self.G)
data = count_all_si(self.G)
count_dicts = update_si_counts(count_dicts, data)
return count_dicts
#perform SIR compartment model
def sir(self):
count_dicts = init_sir_counts()
seed_graph(self.G, self.seeds)
if self.mode == 'manual':
for i in range(self.time):
if self.graph_code == 0:
active_nodes = activate_graph(self.act, self.N)
for i in active_nodes:
count = 0
while count < self.m:
target = random.randint(0, self.N - 1)
if target != i and target not in self.G.neighbors(i):
self.G.add_edge(i, target)
count += 1
else:
active_nodes = self.G.nodes()
gen_inf(active_nodes, self.G, self.infection_parameters)
gen_sir_recover(active_nodes,self.G,self.recovery_parameters)
data = count_all_sir(self.G)
count_dicts = update_sir_counts(count_dicts, data)
return count_dicts
elif self.mode == 'covid':
for t in range(self.time):
active_nodes = activate_graph(self.act, self.N)
for i in active_nodes:
count = 0
while count < self.m:
target = random.randint(0, self.N - 1)
if target != i and target not in self.G.neighbors(i):
self.G.add_edge(i, target)
count += 1
cov_inf(active_nodes, self.G, self.dataframe)
cov_sir_recover(active_nodes,self.G)
data = count_all_sir(self.G)
count_dicts = update_sir_counts(count_dicts, data)
return count_dicts
#define additional models below