Skip to content

Commit 8cee9ee

Browse files
add visualisation loop to refactored code
1 parent d4bbcb5 commit 8cee9ee

File tree

3 files changed

+29
-102
lines changed

3 files changed

+29
-102
lines changed

config.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ def __init__(self, *args, **kwargs):
1515
self.tstep=0#current simulation timestep
1616
self.save_data=True#whether to dump data
1717
self.save_timesteps=True#dumps population data every time step
18+
self.endif_no_infections=True#whether to stop simulation if no infections remain
1819

1920
#scenario flags
2021
self.traveling_infects=False
21-
self.self_isolate=True
22+
self.self_isolate=False
2223
self.lockdown=False
2324
self.lockdown_percentage=0.1#after this proportion is infected, lock-down begins
2425
self.lockdown_compliance=0.95#fraction of the population that will obey the lockdown
@@ -33,6 +34,7 @@ def __init__(self, *args, **kwargs):
3334
#size of the simulated world in coordinates
3435
self.x_plot= [0, 1]
3536
self.y_plot= [0, 1]
37+
self.save_plot=False
3638

3739
#population variables
3840
self.pop_size=2000

simulation.py

+20-100
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ def tstep(self):
114114
#update population statistics
115115
self.pop_tracker.update_counts(self.population)
116116

117+
#visualise
118+
draw_tstep(self.Config, self.population, self.pop_tracker, self.frame,
119+
self.fig, self.spec, self.ax1, self.ax2)
120+
121+
117122
#run callback
118123
self.callback()
119124

@@ -133,109 +138,24 @@ def callback(self):
133138
self.population[0][8] =50
134139
self.population[0][10] =1
135140

136-
137-
138-
defupdate(frame, population, pop_tracker, destinations, pop_size, infection_range=0.01,
139-
infection_chance=0.03, speed=0.01, recovery_duration=(200, 500), mortality_chance=0.02,
140-
xbounds=[0.02, 0.98], ybounds=[0.02, 0.98], x_plot=[0, 1],
141-
y_plot=[0, 1], wander_range=0.05, risk_age=55,
142-
critical_age=75, critical_mortality_chance=0.1,
143-
risk_increase='quadratic', no_treatment_factor=3,
144-
treatment_factor=0.5, healthcare_capacity=250, age_dependent_risk=False,
145-
treatment_dependent_risk=False, visualise=False, verbose=False,
146-
self_isolate=True, self_isolate_proportion=0.6, isolation_bounds=[0, 0, 0.1, 0.1],
147-
traveling_infects=False, lockdown=False, lockdown_percentage=0.1,
148-
lockdown_vector=[], plot_style='default'):
149-
150-
#add one infection to jumpstart
151-
ifframe==50:
152-
population[0][6] =1
153-
population[0][8] =50
154-
population[0][10] =1
155-
156-
157-
ifvisualise:
158-
#construct plot and visualise
159-
spec=fig.add_gridspec(ncols=1, nrows=2, height_ratios=[5,2])
160-
ax1.clear()
161-
ax2.clear()
162-
163-
ax1.set_xlim(x_plot[0], x_plot[1])
164-
ax1.set_ylim(y_plot[0], y_plot[1])
165-
166-
ifself_isolateandisolation_bounds!=None:
167-
build_hospital(isolation_bounds[0], isolation_bounds[2],
168-
isolation_bounds[1], isolation_bounds[3], ax1,
169-
addcross=False)
170-
171-
#plot population segments
172-
healthy=population[population[:,6] ==0][:,1:3]
173-
ax1.scatter(healthy[:,0], healthy[:,1], color='gray', s=2, label='healthy')
174-
175-
infected=population[population[:,6] ==1][:,1:3]
176-
ax1.scatter(infected[:,0], infected[:,1], color='red', s=2, label='infected')
177-
178-
immune=population[population[:,6] ==2][:,1:3]
179-
ax1.scatter(immune[:,0], immune[:,1], color='green', s=2, label='immune')
180-
181-
fatalities=population[population[:,6] ==3][:,1:3]
182-
ax1.scatter(fatalities[:,0], fatalities[:,1], color='black', s=2, label='dead')
183-
184-
185-
#add text descriptors
186-
ax1.text(x_plot[0],
187-
y_plot[1] + ((y_plot[1] -y_plot[0]) /100),
188-
'timestep: %i, total: %i, healthy: %i infected: %i immune: %i fatalities: %i'%(frame,
189-
len(population),
190-
len(healthy),
191-
len(infected),
192-
len(immune),
193-
len(fatalities)),
194-
fontsize=6)
195-
196-
ax2.set_title('number of infected')
197-
ax2.text(0, pop_size*0.05,
198-
'https://github.com/paulvangentcom/python-corona-simulation',
199-
fontsize=6, alpha=0.5)
200-
#ax2.set_xlim(0, simulation_steps)
201-
ax2.set_ylim(0, pop_size+200)
202-
203-
iftreatment_dependent_risk:
204-
infected_arr=np.asarray(pop_tracker.infectious)
205-
indices=np.argwhere(infected_arr>=healthcare_capacity)
206-
207-
ax2.plot([healthcare_capacityforxinrange(len(pop_tracker.infectious))],
208-
color='red', label='healthcare capacity')
209-
210-
ifplot_style.lower() =='default':
211-
ax2.plot(pop_tracker.infectious, color='gray')
212-
ax2.plot(pop_tracker.fatalities, color='black', label='fatalities')
213-
elifplot_style.lower() =='sir':
214-
ax2.plot(pop_tracker.infectious, color='gray')
215-
ax2.plot(pop_tracker.fatalities, color='black', label='fatalities')
216-
ax2.plot(pop_tracker.susceptible, color='blue', label='susceptible')
217-
ax2.plot(pop_tracker.recovered, color='green', label='recovered')
218-
else:
219-
raiseValueError('incorrect plot_style specified, use \'sir\' or \'default\'')
220-
221-
iftreatment_dependent_risk:
222-
ax2.plot(indices, infected_arr[infected_arr>=healthcare_capacity],
223-
color='red')
224-
225-
ax2.legend(loc='best', fontsize=6)
226-
#plt.savefig('render/%i.png' %frame)
227-
228-
returnpopulation
141+
defrun(self):
142+
fortinrange(self.Config.simulation_steps):
143+
try:
144+
sim.tstep()
145+
sys.stdout.write('\r')
146+
sys.stdout.write('%i / %i'%(t, self.Config.simulation_steps))
147+
exceptKeyboardInterrupt:
148+
print('\nCTRL-C caught, exciting')
149+
sys.exit(1)
229150

230151

231152
if__name__=='__main__':
232153

233-
tstep=2000
234-
154+
#initialize
235155
sim=Simulation()
236-
237156

238-
fortinrange(tstep):
239-
sim.tstep()
240-
sys.stdout.write('\r')
241-
sys.stdout.write('%i / %i'%(t, tstep))
157+
#set number of simulation steps
158+
sim.Config.simulation_steps=2000
159+
160+
#run
161+
sim.run()

visualiser.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,9 @@ def draw_tstep(Config, population, pop_tracker, frame,
9595
color='red')
9696

9797
ax2.legend(loc='best', fontsize=6)
98-
#plt.savefig('render/%i.png' %frame)
98+
99+
plt.draw()
100+
plt.pause(0.0001)
101+
102+
ifConfig.save_plot:
103+
plt.savefig('render/%i.png'%frame)

0 commit comments

Comments
 (0)
close