Skip to content

Commit 58a27b4

Browse files
add reporting of simulation progress to terminal if simulation not visualised
1 parent fa8d411 commit 58a27b4

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class config_error(Exception):
1111
class Configuration():
1212
def __init__(self, *args, **kwargs):
1313
#simulation variables
14-
self.verbose = False #whether to print infections, recoveries and fatalities to the terminal
14+
self.verbose = True #whether to print infections, recoveries and fatalities to the terminal
1515
self.simulation_steps = 10000 #total simulation steps performed
1616
self.tstep = 0 #current simulation timestep
1717
self.save_data = False #whether to dump data at end of simulation

infection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def infect(population, Config, frame, send_to_location=False,
196196
new_infections.append(np.int32(person[0]))
197197

198198
if len(new_infections) > 0 and Config.verbose:
199-
print('at timestep %i these people got sick: %s' %(frame, new_infections))
199+
print('\nat timestep %i these people got sick: %s' %(frame, new_infections))
200200

201201
if len(destinations) == 0:
202202
return population
@@ -298,9 +298,9 @@ def recover_or_die(population, frame, Config):
298298
recovered.append(np.int32(infected_people[infected_people[:,0] == idx][:,0][0]))
299299

300300
if len(fatalities) > 0 and Config.verbose:
301-
print('at timestep %i these people died: %s' %(frame, fatalities))
301+
print('\nat timestep %i these people died: %s' %(frame, fatalities))
302302
if len(recovered) > 0 and Config.verbose:
303-
print('at timestep %i these people recovered: %s' %(frame, recovered))
303+
print('\nat timestep %i these people recovered: %s' %(frame, recovered))
304304

305305
#put array back into population
306306
population[population[:,6] == 1] = infected_people

simulation.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from path_planning import go_to_location, set_destination, check_at_destination,\
1515
keep_at_destination, reset_destinations
1616
from population import initialize_population, initialize_destination_matrix,\
17-
set_destination_bounds, save_data, Population_trackers
17+
set_destination_bounds, save_data, save_population, Population_trackers
1818
from visualiser import build_fig, draw_tstep, set_style
1919

2020
#set seed for reproducibility
@@ -119,10 +119,19 @@ def tstep(self):
119119
self.pop_tracker.update_counts(self.population)
120120

121121
#visualise
122-
draw_tstep(self.Config, self.population, self.pop_tracker, self.frame,
123-
self.fig, self.spec, self.ax1, self.ax2)
124-
125-
122+
if self.Config.visualise:
123+
draw_tstep(self.Config, self.population, self.pop_tracker, self.frame,
124+
self.fig, self.spec, self.ax1, self.ax2)
125+
else:
126+
#report stuff to console
127+
sys.stdout.write('\r')
128+
sys.stdout.write('%i: healthy: %i, infected: %i, immune: %i, in treatment: %i, \
129+
dead: %i, of total: %i' %(self.frame, self.pop_tracker.susceptible[-1], self.pop_tracker.infectious[-1],
130+
self.pop_tracker.recovered[-1], len(self.population[self.population[:,10] == 1]),
131+
self.pop_tracker.fatalities[-1], self.Config.pop_size))
132+
#save popdata if required
133+
if self.Config.save_pop and (self.frame % self.Config.save_pop_freq) == 0:
134+
save_population(self.population, self.frame, self.Config.save_pop_folder)
126135
#run callback
127136
self.callback()
128137

@@ -145,18 +154,34 @@ def callback(self):
145154

146155

147156
def run(self):
148-
for t in range(self.Config.simulation_steps):
157+
while self.frame < self.Config.simulation_steps:
149158
try:
150159
sim.tstep()
151-
sys.stdout.write('\r')
152-
sys.stdout.write('%i / %i' %(t, self.Config.simulation_steps))
153160
except KeyboardInterrupt:
154161
print('\nCTRL-C caught, exiting')
155162
sys.exit(1)
156163

164+
#check whether to end if no infecious persons remain.
165+
#check if self.frame is above some threshold to prevent early breaking when simulation
166+
#starts initially with no infections.
167+
if self.Config.endif_no_infections and self.frame >= 500:
168+
if len(self.population[(self.population[:,6] == 1) |
169+
(self.population[:,6] == 4)]) == 0:
170+
self.frame = self.Config.simulation_steps
171+
157172
if self.Config.save_data:
158173
save_data(self.population, self.pop_tracker)
159174

175+
#report outcomes
176+
print('\n-----stopping-----\n')
177+
print('total dead: %i' %len(self.population[self.population[:,6] == 3]))
178+
print('total recovered: %i' %len(self.population[self.population[:,6] == 2]))
179+
print('total infected: %i' %len(self.population[self.population[:,6] == 1]))
180+
print('total infectious: %i' %len(self.population[(self.population[:,6] == 1) |
181+
(self.population[:,6] == 4)]))
182+
print('total unaffected: %i' %len(self.population[self.population[:,6] == 0]))
183+
184+
160185

161186
if __name__ == '__main__':
162187

@@ -175,8 +200,8 @@ def run(self):
175200
#sim.Config.colorblind_type = 'deuteranopia'
176201

177202
#set reduced interaction
178-
sim.Config.set_reduced_interaction()
179-
sim.population_init()
203+
#sim.Config.set_reduced_interaction()
204+
#sim.population_init()
180205

181206
#set lockdown scenario
182207
#sim.Config.set_lockdown(lockdown_percentage = 0.1, lockdown_compliance = 0.95)

0 commit comments

Comments
 (0)