-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualization.py
More file actions
390 lines (346 loc) · 14.6 KB
/
visualization.py
File metadata and controls
390 lines (346 loc) · 14.6 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
from mesa.visualization.modules import CanvasGrid
from mesa.visualization.ModularVisualization import ModularServer
from disease import DiseaseModel
import matplotlib.pyplot as plt
import numpy as np
import random
import sys
from scipy import stats
def t_test(a, b):
t, p = stats.ttest_ind(a, b, equal_var=False)
return "t_value = " + str(round(t, 6)) + "\tp_value = " + str(round(p, 6))
def disease_graph(models, steps, edu_setting):
""""
Plots progress of disease given a model.
"""
diseased_avg = []
lowS_sick_avg = []
middleS_sick_avg = []
highS_sick_avg = []
lowS_resistant_avg = []
middleS_resistant_avg = []
highS_resistant_avg = []
lowS_avg = []
middleS_avg = []
highS_avg = []
disease_plotter_avg = []
low_last = []
mid_last = []
high_last = []
max_n_mutations = 0
for model in models:
# get dataframe for all timesteps in the model
df = model.datacollector.get_model_vars_dataframe()
# initialize store vars
diseased = []
mutation = []
low_sociability = []
middle_sociability = []
high_sociability = []
low_resistant = []
middle_resistant = []
high_resistant = []
n_mutations = 0
# collect model data
for index, row in df.iterrows():
diseased += [row[0][0]]
mutation += [row[0][1]]
sociability = row[0][3]
resistant = row[0][4]
low_resistant += [resistant['0']]
middle_resistant += [resistant['1']]
high_resistant += [resistant['2']]
low_sociability += [sociability['0']]
middle_sociability += [sociability['1']]
high_sociability += [sociability['2']]
if row[0][2] > n_mutations:
n_mutations = row[0][2]
if n_mutations > max_n_mutations:
max_n_mutations = n_mutations
# collect all diseases
disease_plotter = []
for _ in range(n_mutations):
disease_plotter += [[]]
for j in range(len(mutation)):
for i in range(n_mutations):
if i+1 in mutation[j]:
disease_plotter[i] += [mutation[j][i+1]]
else:
disease_plotter[i] += [0]
lowS_sick = [x / model.lowS for x in low_sociability]
middleS_sick = [x / model.middleS for x in middle_sociability]
highS_sick = [x / model.highS for x in high_sociability]
lowS_resistant = [x / model.lowS for x in low_resistant]
middleS_resistant = [x / model.middleS for x in middle_resistant]
highS_resistant = [x / model.highS for x in high_resistant]
# store for averaging
diseased_avg += [diseased]
lowS_sick_avg += [lowS_sick]
middleS_sick_avg += [middleS_sick]
highS_sick_avg += [highS_sick]
lowS_resistant_avg += [lowS_resistant]
middleS_resistant_avg += [middleS_resistant]
highS_resistant_avg += [highS_resistant]
lowS_avg += [model.lowS]
middleS_avg += [model.middleS]
highS_avg += [model.highS]
disease_plotter_avg += [disease_plotter]
low_last += [low_sociability[-1]/model.lowS]
mid_last += [middle_sociability[-1]/model.middleS]
high_last += [high_sociability[-1]/model.highS]
# Write data to textfile
F = open("workfile.txt", "a")
F.write("Comparing the means of the percentage of infected agents at")
F.write(" the last timestep: \n")
F.write("The current educational setting is " + str(edu_setting) + "\n\n")
F.write("Percentage infected agents low sociability \t\t" +
str(np.mean(low_last)) + "\n")
F.write("Percentage infected agents middle sociability \t" +
str(np.mean(mid_last)) + "\n")
F.write("Percentage infected agents high sociability \t" +
str(np.mean(high_last)) + "\n\n")
F.write("Low sociability versus middle sociability \t" +
t_test(low_last, mid_last) + "\n")
F.write("Low sociability versus high sociability \t" +
t_test(low_last, high_last) + "\n")
F.write("Middle sociability versus high sociability \t" +
t_test(high_last, mid_last) + "\n")
F.write("----------------------------------------------------------------")
F.write("------------------------\n\n")
# Calculate averages
diseased_avg = np.mean(np.array(diseased_avg), axis=0)
lowS_sick_avg = np.mean(np.array(lowS_sick_avg), axis=0)
middleS_sick_avg = np.mean(np.array(middleS_sick_avg), axis=0)
highS_sick_avg = np.mean(np.array(highS_sick_avg), axis=0)
lowS_resistant_avg = np.mean(np.array(lowS_resistant_avg), axis=0)
middleS_resistant_avg = np.mean(np.array(middleS_resistant_avg), axis=0)
highS_resistant_avg = np.mean(np.array(highS_resistant_avg), axis=0)
lowS_avg = np.mean(lowS_avg)
middleS_avg = np.mean(middleS_avg)
highS_avg = np.mean(highS_avg)
for mutation_list in disease_plotter_avg:
len_mutation = len(mutation_list)
if len_mutation < max_n_mutations:
mutation_list.extend([[0 for x in range(0, steps)]] *
(max_n_mutations - len_mutation))
disease_plotter_avg = np.mean(disease_plotter_avg, axis=0)
plt.plot(diseased_avg, color="red", label='total')
# Plot all diseases
for mutation in disease_plotter_avg:
plt.plot(mutation)
axes = plt.gca()
axes.set_ylim([0, 1.1])
plt.ylabel("Infected (%)")
plt.xlabel("Timesteps")
plt.title("Infected agents in " + str(edu_setting) +
" educational setting")
axes = plt.gca()
axes.set_ylim([0, 1.1])
plt.xlabel('Timesteps')
plt.ylabel('Infected (%)')
plt.title("Infected agents in " + str(edu_setting) +
" educational setting")
plt.legend()
plt.show()
# Plot resistance
plt.plot(lowS_resistant_avg, label='Low sociability, total agents: '
+ str(int(lowS_avg)))
plt.plot(middleS_resistant_avg, label='Middle sociability, total agents: '
+ str(int(middleS_avg)))
plt.plot(highS_resistant_avg, label='High sociability, total agents: '
+ str(int(highS_avg)))
plt.ylabel("Amount of resistantcy on average per agent")
plt.xlabel("Timesteps")
plt.title("Resistance agents in " + str(edu_setting) +
" educational setting")
plt.legend()
plt.show()
# Write data to textfile
F = open("workfile.txt", "a")
F.write("Comparing the means of the average resistance of agents at the")
F.write(" last timestep: \n")
F.write("The current educational setting is " + str(edu_setting) + "\n\n")
F.write("Average resistance of agents low sociability \t\t" +
str(np.mean(lowS_resistant)) + "\n")
F.write("Average resistance of agents middle sociability \t" +
str(np.mean(middleS_resistant)) + "\n")
F.write("Average resistance of agents high sociability \t\t" +
str(np.mean(highS_resistant)) + "\n\n")
F.write("Low sociability versus middle sociability \t" +
t_test(lowS_resistant, middleS_resistant) + "\n")
F.write("Low sociability versus high sociability \t" +
t_test(lowS_resistant, highS_resistant) + "\n")
F.write("Middle sociability versus high sociability \t" +
t_test(highS_resistant, middleS_resistant) + "\n")
F.write("----------------------------------------------------------------")
F.write("-----------------------\n\n\n")
# Plot agent sociability
axes = plt.gca()
axes.set_ylim([0, 1.1])
plt.plot(lowS_sick_avg, label='Low sociability, total agents: ' +
str(int(lowS_avg)))
plt.plot(middleS_sick_avg, label='Middle sociability, total agents: ' +
str(int(middleS_avg)))
plt.plot(highS_sick_avg, label='High sociability, total agents: ' +
str(int(highS_avg)))
plt.ylabel("Infected (%)")
plt.xlabel("Timesteps")
plt.title("Infected agents in " + str(edu_setting) +
" educational setting")
plt.legend()
plt.show()
return (np.mean(low_last), np.mean(mid_last), np.mean(high_last))
def graph_edu_non(low_0, mid_0, high_0, low_1, mid_1, high_1, edu_setting):
# Set width of bar
barWidth = 0.25
bars1 = [low_0, low_1]
bars2 = [mid_0, mid_1]
bars3 = [high_0, high_1]
# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
# Set plot dimensions
axes = plt.gca()
axes.set_ylim([0, 1])
# Make the plot
plt.bar(r1, bars1, width=barWidth, edgecolor='white',
label='Low sociability')
plt.bar(r2, bars2, width=barWidth, edgecolor='white',
label='Middle sociability')
plt.bar(r3, bars3, width=barWidth, edgecolor='white',
label='High sociability')
# Add xticks on the middle of the group bars
plt.xlabel('Disease rate per sociability in two settings',
fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))],
['Educational setting ' + str(edu_setting),
'Educational setting ' + str(not edu_setting)])
# Create legend & Show graphic
plt.ylabel("Infected (%)")
plt.title("Infected agents at the last timestep")
plt.legend()
plt.show()
def color_maker():
"""Returns a list of colors."""
R, G, B = 0, 0, 0
color_array = []
for i in range(1, 6):
for j in range(1, 6):
for k in range(1, 6):
# skip gray
if i == j == k:
continue
R = i * 50
G = j * 50
B = k * 50
# Ensure colors are not too dark
if (R + G + B) > 200:
color_array += ["#" + hex(R)[2:] + hex(G)[2:] + hex(B)[2:]]
random.shuffle(color_array)
# 0th color is always black and healthy
color_array.insert(0, "#000000")
return color_array
color_array = color_maker()
def agent_portrayal(agent):
"""Colors and shapes the agent on the grid visualization."""
portrayal = {"Filled": "true", "Layer": 0, "r": 0.5}
# draw agent
if agent.disease > -1:
portrayal["Shape"] = "circle"
# change shape if agent has reached its goal
if agent.goal == agent.pos:
portrayal["r"] = 2
else:
portrayal["r"] = 1
portrayal["Color"] = color_array[agent.disease % len(color_array)]
# draw wall
else:
portrayal["Shape"] = "rect"
portrayal["w"] = 1
portrayal["h"] = 1
portrayal["Color"] = "grey"
return portrayal
def visualization_grid(width, height, highS, middleS, lowS, edu_setting=False,
cureProb=0.1, cureProbFac=2/1440, mutateProb=0.0050,
diseaseRate=0.2):
"""
Launch grid visualization on server.
width: Width of the grid.
height: Height of the grid.
highS: Number of agents with high sociability.
middleS: Number of agents with middle sociability.
lowS: Number of agents with low sociability.
edu_setting: If true, agents will follow a schedule and sit in classrooms,
else they will move freely through an open grid.
cureProb: Probability of agent getting better.
cureProbFac: Factor of cureProb getting higher.
mutateProb: Probability of a disease mutating.
diseaseRate: Rate at which the disease spreads
"""
grid = CanvasGrid(agent_portrayal, width, height, width*10, height*10)
server = ModularServer(DiseaseModel, [grid], "Disease Model",
{"highS": highS, "middleS": middleS, "lowS": lowS,
"width": width, "height": height,
"edu_setting": edu_setting, "cureProb": cureProb,
"cureProbFac": cureProbFac,
"mutateProb": mutateProb,
"diseaseRate": diseaseRate})
server.port = 8521 # The default
server.launch()
def visualization(width, height, highS, middleS, lowS, edu_setting=True,
cureProb=0.1, cureProbFac=2/1440, mutateProb=0.0050,
diseaseRate=0.2, grid=True, graphs=True, steps=300):
"""
Create visualizations.
width: Width of the grid.
height: Height of the grid.
highS: Number of agents with high sociability.
middleS: Number of agents with middle sociability.
lowS: Number of agents with low sociability.
edu_setting: If true, agents will follow a schedule and sit in classrooms,
else they will move freely through an open grid.
cureProb: Probability of agent getting better.
cureProbFac: Factor of cureProb getting higher.
mutateProb: Probability of a disease mutating.
diseaseRate: Rate at which the disease spreads.
grid: if True show grid visualisation.
graphs: if True show graphs.
steps: number of steps in graph.
"""
if graphs:
# create an average over different models
models_0, models_1 = [], []
for i in range(0, 10):
model_0 = DiseaseModel(highS, middleS, lowS, width, height,
edu_setting, cureProb, cureProbFac,
mutateProb, diseaseRate)
model_1 = DiseaseModel(highS, middleS, lowS, width, height,
not edu_setting, cureProb, cureProbFac,
mutateProb, diseaseRate)
for j in range(steps):
# print every 100th step to inform user about progress
if j % 100 == 0:
print(j)
model_0.step()
model_1.step()
models_0 += [model_0]
models_1 += [model_1]
low_0, mid_0, high_0 = disease_graph(models_0, steps, edu_setting)
low_1, mid_1, high_1 = disease_graph(models_1, steps, not edu_setting)
graph_edu_non(low_0, mid_0, high_0, low_1, mid_1, high_1, edu_setting)
# visualization on server
if grid:
visualization_grid(width, height, highS, middleS, lowS, edu_setting,
cureProb, cureProbFac, mutateProb, diseaseRate)
# Run shorter version for demo if d(emo) flag is set
if len(sys.argv) == 2 and sys.argv[1] == "-d":
F = open("workfile.txt", "w")
F.write("")
visualization(50, 50, 10, 10, 10, steps=100, grid=True, edu_setting=True)
else:
F = open("workfile.txt", "w")
F.write("")
visualization(50, 50, 10, 10, 10, steps=30000, edu_setting=False,
cureProb=0.2, cureProbFac=2/1440, mutateProb=0.0000050,
diseaseRate=0.02, grid=False, graphs=True)