Skip to content

Commit 605286e

Browse files
add method to plot SIR plot only, for example if visualisation is turned off
1 parent f187432 commit 605286e

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

simulation.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
keep_at_destination, reset_destinations
1616
frompopulationimportinitialize_population, initialize_destination_matrix,\
1717
set_destination_bounds, save_data, save_population, Population_trackers
18-
fromvisualiserimportbuild_fig, draw_tstep, set_style
18+
fromvisualiserimportbuild_fig, draw_tstep, set_style, plot_sir
1919

2020
#set seed for reproducibility
2121
#np.random.seed(100)
@@ -33,11 +33,8 @@ def __init__(self, *args, **kwargs):
3333
self.pop_tracker=Population_trackers()
3434

3535
#initalise destinations vector
36-
self.destinations=initialize_destination_matrix(self.Config.pop_size, 1)
36+
self.destinations=initialize_destination_matrix(self.Config.pop_size, 1)
3737

38-
self.fig, self.spec, self.ax1, self.ax2=build_fig(self.Config)
39-
40-
#set_style(self.Config)
4138

4239

4340
defpopulation_init(self):
@@ -50,6 +47,10 @@ def tstep(self):
5047
'''
5148
takes a time step in the simulation
5249
'''
50+
51+
ifself.frame==0andself.Config.visualise:
52+
#initialize figure
53+
self.fig, self.spec, self.ax1, self.ax2=build_fig(self.Config)
5354

5455
#check destinations if active
5556
#define motion vectors if destinations active and not everybody is at destination
@@ -187,6 +188,12 @@ def run(self):
187188
print('total infectious: %i'%len(self.population[(self.population[:,6] ==1) |
188189
(self.population[:,6] ==4)]))
189190
print('total unaffected: %i'%len(self.population[self.population[:,6] ==0]))
191+
192+
193+
defplot_sir(self, size=(6,3), include_fatalities=False,
194+
title='S-I-R plot of simulation'):
195+
plot_sir(self.Config, self.pop_tracker, size, include_fatalities,
196+
title)
190197

191198

192199

visualiser.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,52 @@ def draw_tstep(Config, population, pop_tracker, frame,
119119
plt.savefig('%s/%i.png'%(Config.plot_path, frame))
120120
except:
121121
check_folder(Config.plot_path)
122-
plt.savefig('%s/%i.png'%(Config.plot_path, frame))
122+
plt.savefig('%s/%i.png'%(Config.plot_path, frame))
123+
124+
125+
defplot_sir(Config, pop_tracker, size=(6,3), include_fatalities=False,
126+
title='S-I-R plot of simulation'):
127+
'''plots S-I-R parameters in the population tracker
128+
129+
Keyword arguments
130+
-----------------
131+
Config : class
132+
the configuration class
133+
134+
pop_tracker : ndarray
135+
the population tracker, containing
136+
137+
size : tuple
138+
size at which the plot will be initialised (default: (6,3))
139+
140+
include_fatalities : bool
141+
whether to plot the fatalities as well (default: False)
142+
'''
143+
144+
#set plot style
145+
set_style(Config)
146+
147+
#get color palettes
148+
palette=Config.get_palette()
149+
150+
#plot the thing
151+
plt.figure(figsize=size)
152+
plt.title(title)
153+
plt.plot(pop_tracker.susceptible, color=palette[0], label='susceptible')
154+
plt.plot(pop_tracker.infectious, color=palette[1], label='infectious')
155+
plt.plot(pop_tracker.recovered, color=palette[2], label='recovered')
156+
ifinclude_fatalities:
157+
plt.plot(pop_tracker.fatalities, color=palette[3], label='fatalities')
158+
159+
#add axis labels
160+
plt.xlabel('time in hours')
161+
plt.ylabel('population')
162+
163+
#add legend
164+
plt.legend()
165+
166+
#beautify
167+
plt.tight_layout()
168+
169+
#initialise
170+
plt.show()

0 commit comments

Comments
 (0)
close