@@ -114,6 +114,11 @@ def tstep(self):
114
114
#update population statistics
115
115
self .pop_tracker .update_counts (self .population )
116
116
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
+
117
122
#run callback
118
123
self .callback ()
119
124
@@ -133,109 +138,24 @@ def callback(self):
133
138
self .population [0 ][8 ] = 50
134
139
self .population [0 ][10 ] = 1
135
140
136
-
137
-
138
- def update (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
- if frame == 50 :
152
- population [0 ][6 ] = 1
153
- population [0 ][8 ] = 50
154
- population [0 ][10 ] = 1
155
-
156
-
157
- if visualise :
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
- if self_isolate and isolation_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
- if treatment_dependent_risk :
204
- infected_arr = np .asarray (pop_tracker .infectious )
205
- indices = np .argwhere (infected_arr >= healthcare_capacity )
206
-
207
- ax2 .plot ([healthcare_capacity for x in range (len (pop_tracker .infectious ))],
208
- color = 'red' , label = 'healthcare capacity' )
209
-
210
- if plot_style .lower () == 'default' :
211
- ax2 .plot (pop_tracker .infectious , color = 'gray' )
212
- ax2 .plot (pop_tracker .fatalities , color = 'black' , label = 'fatalities' )
213
- elif plot_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
- raise ValueError ('incorrect plot_style specified, use \' sir\' or \' default\' ' )
220
-
221
- if treatment_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
- return population
141
+ def run (self ):
142
+ for t in range (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
+ except KeyboardInterrupt :
148
+ print ('\n CTRL-C caught, exciting' )
149
+ sys .exit (1 )
229
150
230
151
231
152
if __name__ == '__main__' :
232
153
233
- tstep = 2000
234
-
154
+ #initialize
235
155
sim = Simulation ()
236
-
237
156
238
- for t in range (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 ()
0 commit comments