Skip to content

Commit bb4a9cf

Browse files
create methods to dump simulation data to disk
1 parent f9ac85b commit bb4a9cf

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

config.py

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(self, *args, **kwargs):
3636
self.x_plot= [0, 1]
3737
self.y_plot= [0, 1]
3838
self.save_plot=False
39+
self.plot_path='render/'#folder where plots are saved to
3940
self.plot_style='default'#can be default, dark, ...
4041
self.colorblind_mode=False
4142
#if colorblind is enabled, set type of colorblindness

population.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
importnumpyasnp
1010

1111
frommotionimportget_motion_parameters
12+
fromutilsimportcheck_folder
1213

1314
definitialize_population(Config, mean_age=45, max_age=105,
1415
xbounds=[0, 1], ybounds=[0, 1]):
@@ -157,7 +158,7 @@ def set_destination_bounds(population, destinations, xmin, ymin,
157158
returnpopulation, destinations
158159

159160

160-
defsave_data(population, infected, fatalities):
161+
defsave_data(population, pop_tracker):
161162
'''dumps simulation data to disk
162163
163164
Function that dumps the simulation data to specific files on the disk.
@@ -176,10 +177,11 @@ def save_data(population, infected, fatalities):
176177
the array containing data of fatalities over time
177178
'''
178179
num_files=len(glob('data/*'))
179-
os.makedirs('data/%i'%num_files)
180+
check_folder('data/%i'%num_files)
180181
np.save('data/%i/population.npy'%num_files, population)
181-
np.save('data/%i/infected.npy'%num_files, infected)
182-
np.save('data/%i/fatalities.npy'%num_files, fatalities)
182+
np.save('data/%i/infected.npy'%num_files, pop_tacker.infectious)
183+
np.save('data/%i/recovered.npy'%num_files, pop_tracker.recovered)
184+
np.save('data/%i/fatalities.npy'%num_files, pop_tracker.fatalities)
183185

184186

185187
classPopulation_trackers():

simulation.py

+3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ def run(self):
154154
print('\nCTRL-C caught, exiting')
155155
sys.exit(1)
156156

157+
ifself.Config.save_data:
158+
save_data(self.population, self.pop_tracker)
159+
157160

158161
if__name__=='__main__':
159162

utils.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'''
2+
collection of utility methods shared across files
3+
'''
4+
5+
importos
6+
7+
defcheck_folder(folder='render/'):
8+
'''check if folder exists, make if not present'''
9+
ifnotos.path.exists(folder):
10+
os.makedirs(folder)

visualiser.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
importnumpyasnp
88

99
fromenvironmentimportbuild_hospital
10+
fromutilsimportcheck_folder
1011

1112
defset_style(Config):
1213
'''sets the plot style
@@ -15,6 +16,7 @@ def set_style(Config):
1516
ifConfig.plot_style.lower() =='dark':
1617
mpl.style.use('plot_styles/dark.mplstyle')
1718

19+
1820
defbuild_fig(Config, figsize=(5,7)):
1921
set_style(Config)
2022
fig=plt.figure(figsize=(5,7))
@@ -30,6 +32,8 @@ def build_fig(Config, figsize=(5,7)):
3032
#ax2.set_xlim(0, simulation_steps)
3133
ax2.set_ylim(0, Config.pop_size+100)
3234

35+
#if
36+
3337
returnfig, spec, ax1, ax2
3438

3539

@@ -111,4 +115,8 @@ def draw_tstep(Config, population, pop_tracker, frame,
111115
plt.pause(0.0001)
112116

113117
ifConfig.save_plot:
114-
plt.savefig('render/%i.png'%frame)
118+
try:
119+
plt.savefig('%s/%i.png'%(Config.plot_path, frame))
120+
except:
121+
check_folder(Config.plot_path)
122+
plt.savefig('%s/%i.png'%(Config.plot_path, frame))

0 commit comments

Comments
 (0)
close