-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizations.py
150 lines (114 loc) · 3.95 KB
/
visualizations.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 10 07:18:57 2020
@author: ameanasad
"""
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from PandemicModel import PandemicSpread, Node
"""
These parameters control the whole experiment. Modify them to get different
results.
"""
###################
size = 300
nodes = 160
infectionRate = 0.5
radius = 7
speed = 1.9
####################
box = PandemicSpread(size, nodes, infectionRate, radius, speed)
box.startInfection()
box.deployNodes()
fig = plt.figure()
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False,
xlim=(-size*2,size*2), ylim=(-size, size))
particles, = ax.plot([], [], color="#26689e", ms=6, marker="o",linestyle="None")
infected, = ax.plot([], [], color="#db5e5c", ms=6, marker="o",linestyle="None")
dead, = ax.plot([], [], color="#615e5e", ms=6, marker="o",linestyle="None")
recovered, = ax.plot([], [],color="#7acf95", ms=6, marker="o",linestyle="None")
rect = plt.Rectangle([-size*2,-size],
size*4,
size*2,
ec='none', lw=2, fc='none')
ax.add_patch(rect)
def init():
"""initialize animation"""
global box, rect
particles.set_data([], [])
infected.set_data([], [])
dead.set_data([], [])
recovered.set_data([], [])
rect.set_edgecolor('none')
return infected, particles, dead,recovered, rect
def animate(i):
"""perform animation step"""
global box, rect, ax, fig
box.updateNodes()
#Not the best variable assignment but it works
X,Y = box.getSample("Susceptible")
M,N = box.getSample("Infected")
Z,V = box.getSample("Dead")
H,G = box.getSample("Recovered")
rect.set_edgecolor('k')
particles.set_data(X, Y)
particles.set_markersize(6)
infected.set_data(M, N)
infected.set_markersize(6)
dead.set_data(Z, V)
dead.set_markersize(6)
recovered.set_data(H, G)
recovered.set_markersize(6)
return infected, particles, dead, recovered, rect
def visualizeParticles():
"""
Change interval to change the frame rate of the simulation.
Larger interval means slower frame rate and vice versa.
The interval also impacts the actual speed of the particles,
which has an impact on the experiment. So keep it constant when running
multiple experiments to compare response to various parameter changes.
Returns
-------
ani : Animation figure
"""
ani = animation.FuncAnimation(fig, animate, frames=5000,
interval=10, blit=True, init_func=init)
return ani
def SirModelPlot():
fig2 = plt.figure(figsize=(6,4))
plt.tick_params(axis='x',which='both',bottom=False,top=False,labelbottom=False)
ax2 = plt.axes()
im = ax2.stackplot([],[], labels=[ 'infected','vulnerable','removed'],
colors=['#db5e5c','#26689e','#7acf95'])
Dead = {'X': [], "Y": []}
Recovered = {'X': [], "Y": []}
Infected = {'X': [], "Y": []}
Susceptible = {'X': [], "Y": []}
n = 0
loop = True
while loop:
if box.getInfectionNo() == 0:
break
n+=1
x = list(range(0,n))
box.updateNodes()
Infected['X'].append(box.getInfectionNo())
Dead['X'].append(box.getDeathsnNo())
Recovered['X'].append(box.getRecoveredNo())
Susceptible['X'].append(box.getSusceptibleNo())
y = [Infected['X'],Susceptible['X'],Recovered["X"], Dead["X"] ]
#each artist object is a stackplot figure
im = ax2.stackplot(x,y, labels=[ 'infected','vulnerable','removed'],
colors=['#db5e5c','#26689e','#7acf95', '#615e5e'])
plt.show()
return None
"""
Uncomment the following line to show the SIR Plot when running
"""
# SirModelPlot()
"""
Uncomment the following line to show the particle animation when running
"""
# visualizeParticles()