One of the main reasons people don’t use ABMs is because they can be very slow. While “vanilla Starsim” is quite fast (10,000 agents running for 100 timesteps should take about a second), custom modules, if not properly written, can be quite slow.
The first step of fixing a slow module is to identify the problem. To do this, Starsim includes some built-in profiling tools.
Initializing sim with 10000 agents
Profiling 15 function(s):
<bound method Sim.run of Sim(n=10000; 2000.01.01—2020.01.01; networks=randomnet; diseases=sis)>
<bound method Sim.start_step of Sim(n=10000; 2000.01.01—2020.01.01; networks=randomnet; diseases=sis [...]
<function Module.start_step at 0x7f3602690040>
<function Module.start_step at 0x7f3602690040>
<bound method SIS.step_state of sis(pars=[init_prev, beta, dur_inf, waning, imm_boost, _n_initial_ca [...]
<bound method DynamicNetwork.step of randomnet(n_edges=50000; pars=[n_contacts, dur, beta]; states=[ [...]
<bound method Infection.step of sis(pars=[init_prev, beta, dur_inf, waning, imm_boost, _n_initial_ca [...]
<bound method People.step_die of People(n=10000; age=30.0±17.3)>
<bound method People.update_results of People(n=10000; age=30.0±17.3)>
<bound method Network.update_results of randomnet(n_edges=50000; pars=[n_contacts, dur, beta]; state [...]
<function SIS.update_results at 0x7f3602032020>
<function Module.finish_step at 0x7f3602690220>
<function Module.finish_step at 0x7f3602690220>
<bound method People.finish_step of People(n=10000; age=30.0±17.3)>
<bound method Sim.finish_step of Sim(n=10000; 2000.01.01—2020.01.01; networks=randomnet; diseases=si [...]
Running 2000.01.01 ( 0/21) (0.00 s) ———————————————————— 5%
Running 2010.01.01 (10/21) (0.77 s) ••••••••••—————————— 52%
Running 2020.01.01 (20/21) (0.85 s) •••••••••••••••••••• 100%
Elapsed time: 0.873 s
—————————————————————————————————————————————————————————————————————
Profile of networks.Network.update_results: 0.000493946 s (0.056581%)
—————————————————————————————————————————————————————————————————————
Total time: 0.000493946 s
File: /home/runner/work/starsim/starsim/starsim/networks.py
Function: Network.update_results at line 256
Line # Hits Time Per Hit % Time Line Contents
==============================================================
256 def update_results(self):
257 """ Store the number of edges in the network """
258 21 115603.0 5504.9 23.4 super().update_results()
259 21 370129.0 17625.2 74.9 self.results['n_edges'][self.ti] = len(self)
260 21 8214.0 391.1 1.7 return
————————————————————————————————————————————————————————————————
Profile of people.People.finish_step: 0.000852201 s (0.0976187%)
————————————————————————————————————————————————————————————————
Total time: 0.000852201 s
File: /home/runner/work/starsim/starsim/starsim/people.py
Function: People.finish_step at line 497
Line # Hits Time Per Hit % Time Line Contents
==============================================================
497 def finish_step(self):
498 21 534031.0 25430.0 62.7 self.remove_dead()
499 21 310789.0 14799.5 36.5 self.update_post()
500 21 7381.0 351.5 0.9 return
——————————————————————————————————————————————————————————
Profile of people.People.step_die: 0.00202665 s (0.23215%)
——————————————————————————————————————————————————————————
Total time: 0.00202665 s
File: /home/runner/work/starsim/starsim/starsim/people.py
Function: People.step_die at line 459
Line # Hits Time Per Hit % Time Line Contents
==============================================================
459 def step_die(self):
460 """ Carry out any deaths or removals that took place this timestep """
461 21 1717039.0 81763.8 84.7 death_uids = ((self.ti_dead <= self.sim.ti) | (self.ti_removed <= self.sim.ti)).uids
462 21 78596.0 3742.7 3.9 self.alive[death_uids] = False # Whilst not dead, removed agents should not be included in alive totals
463
464 # Execute deaths that took place this timestep (i.e., changing the `alive` state of the agents). This is executed
465 # before analyzers have run so that analyzers are able to inspect and record outcomes for agents that died this timestep
466 42 166018.0 3952.8 8.2 for disease in self.sim.diseases():
467 21 20392.0 971.0 1.0 if isinstance(disease, ss.Disease):
468 21 35773.0 1703.5 1.8 disease.step_die(death_uids)
469
470 21 8832.0 420.6 0.4 return death_uids
————————————————————————————————————————————————————————————————
Profile of diseases.SIS.update_results: 0.00335713 s (0.384555%)
————————————————————————————————————————————————————————————————
Total time: 0.00335713 s
File: /home/runner/work/starsim/starsim/starsim/diseases.py
Function: SIS.update_results at line 779
Line # Hits Time Per Hit % Time Line Contents
==============================================================
779 @ss.required()
780 def update_results(self):
781 """ Store the population immunity (susceptibility) """
782 21 2365691.0 112652.0 70.5 super().update_results()
783 21 983493.0 46833.0 29.3 self.results['rel_sus'][self.ti] = self.rel_sus.mean()
784 21 7948.0 378.5 0.2 return
—————————————————————————————————————————————————————————————————
Profile of people.People.update_results: 0.00336593 s (0.385563%)
—————————————————————————————————————————————————————————————————
Total time: 0.00336593 s
File: /home/runner/work/starsim/starsim/starsim/people.py
Function: People.update_results at line 488
Line # Hits Time Per Hit % Time Line Contents
==============================================================
488 def update_results(self):
489 21 28243.0 1344.9 0.8 ti = self.sim.ti
490 21 12180.0 580.0 0.4 res = self.sim.results
491 21 688025.0 32763.1 20.4 res.n_alive[ti] = np.count_nonzero(self.alive)
492 21 920466.0 43831.7 27.3 res.new_deaths[ti] = np.count_nonzero(self.ti_dead == ti)
493 21 765947.0 36473.7 22.8 res.new_emigrants[ti] = np.count_nonzero(self.ti_removed == ti)
494 21 941344.0 44825.9 28.0 res.cum_deaths[ti] = np.sum(res.new_deaths[:ti]) # TODO: inefficient to compute the cumulative sum on every timestep!
495 21 9722.0 463.0 0.3 return
———————————————————————————————————————————————————————
Profile of sim.Sim.start_step: 0.00532916 s (0.610449%)
———————————————————————————————————————————————————————
Total time: 0.00532916 s
File: /home/runner/work/starsim/starsim/starsim/sim.py
Function: Sim.start_step at line 450
Line # Hits Time Per Hit % Time Line Contents
==============================================================
450 def start_step(self):
451 """ Start the step -- only print progress; all actual changes happen in the modules """
452
453 # Set the time and if we have reached the end of the simulation, then do nothing
454 21 12258.0 583.7 0.2 if self.complete:
455 errormsg = 'Simulation already complete (call sim.init() to re-run)'
456 raise AlreadyRunError(errormsg)
457
458 # Print out progress if needed
459 21 3565591.0 169790.0 66.9 self.elapsed = self.timer.toc(output=True)
460 21 14662.0 698.2 0.3 if self.verbose: # Print progress
461 21 8195.0 390.2 0.2 t = self.t
462 21 262179.0 12484.7 4.9 simlabel = f'"{self.label}": ' if self.label else ''
463 21 930546.0 44311.7 17.5 string = f' Running {simlabel}{t.now("str")} ({t.ti:2.0f}/{t.npts}) ({self.elapsed:0.2f} s) '
464 21 13890.0 661.4 0.3 if self.verbose >= 1:
465 sc.heading(string)
466 21 10715.0 510.2 0.2 elif self.verbose > 0:
467 21 21363.0 1017.3 0.4 if not (t.ti % int(1.0 / self.verbose)):
468 3 480219.0 160073.0 9.0 sc.progressbar(t.ti + 1, t.npts, label=string, length=20, newline=True)
469 21 9544.0 454.5 0.2 return
———————————————————————————————————————————————————————————
Profile of diseases.SIS.step_state: 0.00582485 s (0.66723%)
———————————————————————————————————————————————————————————
Total time: 0.00582485 s
File: /home/runner/work/starsim/starsim/starsim/diseases.py
Function: SIS.step_state at line 739
Line # Hits Time Per Hit % Time Line Contents
==============================================================
739 def step_state(self):
740 """ Progress infectious -> recovered """
741 21 1838616.0 87553.1 31.6 recovered = (self.infected & (self.ti_recovered <= self.ti)).uids
742 21 97400.0 4638.1 1.7 self.infected[recovered] = False
743 21 62375.0 2970.2 1.1 self.susceptible[recovered] = True
744 21 3818052.0 181812.0 65.5 self.update_immunity()
745 21 8403.0 400.1 0.1 return
————————————————————————————————————————————————————————————
Profile of modules.Module.start_step: 0.0116471 s (1.33416%)
————————————————————————————————————————————————————————————
Total time: 0.0116471 s
File: /home/runner/work/starsim/starsim/starsim/modules.py
Function: Module.start_step at line 679
Line # Hits Time Per Hit % Time Line Contents
==============================================================
679 @required()
680 def start_step(self):
681 """ Tasks to perform at the beginning of the step """
682 42 41090.0 978.3 0.4 if self.finalized:
683 errormsg = f'The module {self._debug_name} has already been run. Did you mean to copy it before running it?'
684 raise RuntimeError(errormsg)
685 42 25069.0 596.9 0.2 if self.dists is not None: # Will be None if no distributions are defined
686 42 11562842.0 275305.8 99.3 self.dists.jump_dt() # Advance random number generators forward for calls on this step
687 42 18118.0 431.4 0.2 return
——————————————————————————————————————————————————————————————
Profile of networks.DynamicNetwork.step: 0.0457202 s (5.2372%)
——————————————————————————————————————————————————————————————
Total time: 0.0457202 s
File: /home/runner/work/starsim/starsim/starsim/networks.py
Function: DynamicNetwork.step at line 414
Line # Hits Time Per Hit % Time Line Contents
==============================================================
414 def step(self):
415 21 14912229.0 710106.1 32.6 self.end_pairs()
416 21 30797806.0 1.47e+06 67.4 self.add_pairs()
417 21 10203.0 485.9 0.0 return
—————————————————————————————————————————————————————————
Profile of diseases.Infection.step: 0.771241 s (88.3448%)
—————————————————————————————————————————————————————————
Total time: 0.771241 s
File: /home/runner/work/starsim/starsim/starsim/diseases.py
Function: Infection.step at line 215
Line # Hits Time Per Hit % Time Line Contents
==============================================================
215 def step(self):
216 """
217 Perform key infection updates, including infection and setting prognoses
218 """
219 # Create new cases
220 21 755027308.0 3.6e+07 97.9 new_cases, sources, networks = self.infect() # TODO: store outputs in self or use objdict rather than 3 returns
221
222 # Set prognoses
223 21 15844.0 754.5 0.0 if len(new_cases):
224 21 16186251.0 770773.9 2.1 self.set_outcomes(new_cases, sources)
225
226 21 11390.0 542.4 0.0 return new_cases, sources, networks
Figure(672x480)
This graph (which is a shortcut to sim.loop.plot_cpu()) shows us how much time each step in the integration loop takes. We can get line-by-line detail of where each function is taking time, though:
prof.disp(maxentries=5)
———————————————————————————————————————————————————————
Profile of sim.Sim.start_step: 0.00532916 s (0.610449%)
———————————————————————————————————————————————————————
Total time: 0.00532916 s
File: /home/runner/work/starsim/starsim/starsim/sim.py
Function: Sim.start_step at line 450
Line # Hits Time Per Hit % Time Line Contents
==============================================================
450 def start_step(self):
451 """ Start the step -- only print progress; all actual changes happen in the modules """
452
453 # Set the time and if we have reached the end of the simulation, then do nothing
454 21 12258.0 583.7 0.2 if self.complete:
455 errormsg = 'Simulation already complete (call sim.init() to re-run)'
456 raise AlreadyRunError(errormsg)
457
458 # Print out progress if needed
459 21 3565591.0 169790.0 66.9 self.elapsed = self.timer.toc(output=True)
460 21 14662.0 698.2 0.3 if self.verbose: # Print progress
461 21 8195.0 390.2 0.2 t = self.t
462 21 262179.0 12484.7 4.9 simlabel = f'"{self.label}": ' if self.label else ''
463 21 930546.0 44311.7 17.5 string = f' Running {simlabel}{t.now("str")} ({t.ti:2.0f}/{t.npts}) ({self.elapsed:0.2f} s) '
464 21 13890.0 661.4 0.3 if self.verbose >= 1:
465 sc.heading(string)
466 21 10715.0 510.2 0.2 elif self.verbose > 0:
467 21 21363.0 1017.3 0.4 if not (t.ti % int(1.0 / self.verbose)):
468 3 480219.0 160073.0 9.0 sc.progressbar(t.ti + 1, t.npts, label=string, length=20, newline=True)
469 21 9544.0 454.5 0.2 return
———————————————————————————————————————————————————————————
Profile of diseases.SIS.step_state: 0.00582485 s (0.66723%)
———————————————————————————————————————————————————————————
Total time: 0.00582485 s
File: /home/runner/work/starsim/starsim/starsim/diseases.py
Function: SIS.step_state at line 739
Line # Hits Time Per Hit % Time Line Contents
==============================================================
739 def step_state(self):
740 """ Progress infectious -> recovered """
741 21 1838616.0 87553.1 31.6 recovered = (self.infected & (self.ti_recovered <= self.ti)).uids
742 21 97400.0 4638.1 1.7 self.infected[recovered] = False
743 21 62375.0 2970.2 1.1 self.susceptible[recovered] = True
744 21 3818052.0 181812.0 65.5 self.update_immunity()
745 21 8403.0 400.1 0.1 return
————————————————————————————————————————————————————————————
Profile of modules.Module.start_step: 0.0116471 s (1.33416%)
————————————————————————————————————————————————————————————
Total time: 0.0116471 s
File: /home/runner/work/starsim/starsim/starsim/modules.py
Function: Module.start_step at line 679
Line # Hits Time Per Hit % Time Line Contents
==============================================================
679 @required()
680 def start_step(self):
681 """ Tasks to perform at the beginning of the step """
682 42 41090.0 978.3 0.4 if self.finalized:
683 errormsg = f'The module {self._debug_name} has already been run. Did you mean to copy it before running it?'
684 raise RuntimeError(errormsg)
685 42 25069.0 596.9 0.2 if self.dists is not None: # Will be None if no distributions are defined
686 42 11562842.0 275305.8 99.3 self.dists.jump_dt() # Advance random number generators forward for calls on this step
687 42 18118.0 431.4 0.2 return
——————————————————————————————————————————————————————————————
Profile of networks.DynamicNetwork.step: 0.0457202 s (5.2372%)
——————————————————————————————————————————————————————————————
Total time: 0.0457202 s
File: /home/runner/work/starsim/starsim/starsim/networks.py
Function: DynamicNetwork.step at line 414
Line # Hits Time Per Hit % Time Line Contents
==============================================================
414 def step(self):
415 21 14912229.0 710106.1 32.6 self.end_pairs()
416 21 30797806.0 1.47e+06 67.4 self.add_pairs()
417 21 10203.0 485.9 0.0 return
—————————————————————————————————————————————————————————
Profile of diseases.Infection.step: 0.771241 s (88.3448%)
—————————————————————————————————————————————————————————
Total time: 0.771241 s
File: /home/runner/work/starsim/starsim/starsim/diseases.py
Function: Infection.step at line 215
Line # Hits Time Per Hit % Time Line Contents
==============================================================
215 def step(self):
216 """
217 Perform key infection updates, including infection and setting prognoses
218 """
219 # Create new cases
220 21 755027308.0 3.6e+07 97.9 new_cases, sources, networks = self.infect() # TODO: store outputs in self or use objdict rather than 3 returns
221
222 # Set prognoses
223 21 15844.0 754.5 0.0 if len(new_cases):
224 21 16186251.0 770773.9 2.1 self.set_outcomes(new_cases, sources)
225
226 21 11390.0 542.4 0.0 return new_cases, sources, networks
(Note that the names of the functions here refer to the actual functions called, which may not match the graph above. That’s because, for example, ss.SIS does not define its own step() method, but instead inherits step() from Infection. In the graph, this is shown as sis.step(), but is listed in the table as Infection.step(). This is because it’s referring to the actual code being run, so refers to where those lines of code exist in the codebase; there is no code corresponding to SIS.step() since it’s just inherited from Infection.step().)
If you want more detail, you can also define custom functions to follow. For example, we can see that ss.SIS.infect() takes the most time in ss.SIS.step(), so let’s profile that:
Initializing sim with 10000 agents
Profiling 1 function(s):
<function Infection.infect at 0x7f3602030ae0>
Running 2000.01.01 ( 0/21) (0.00 s) ———————————————————— 5%
Running 2010.01.01 (10/21) (0.08 s) ••••••••••—————————— 52%
Running 2020.01.01 (20/21) (0.15 s) •••••••••••••••••••• 100%
Elapsed time: 0.174 s
————————————————————————————————————————————————————————————
Profile of diseases.Infection.infect: 0.0693591 s (39.9006%)
————————————————————————————————————————————————————————————
Total time: 0.0693591 s
File: /home/runner/work/starsim/starsim/starsim/diseases.py
Function: Infection.infect at line 237
Line # Hits Time Per Hit % Time Line Contents
==============================================================
237 def infect(self):
238 """ Determine who gets infected on this timestep via transmission on the network """
239 21 17638.0 839.9 0.0 new_cases = []
240 21 7993.0 380.6 0.0 sources = []
241 21 7089.0 337.6 0.0 networks = []
242 21 639642.0 30459.1 0.9 betamap = self.validate_beta()
243
244 21 2626742.0 125083.0 3.8 rel_trans = self.rel_trans.asnew(self.infectious * self.rel_trans)
245 21 2058114.0 98005.4 3.0 rel_sus = self.rel_sus.asnew(self.susceptible * self.rel_sus)
246
247 42 152497.0 3630.9 0.2 for i, (nkey,route) in enumerate(self.sim.networks.items()):
248 21 34332.0 1634.9 0.0 nk = ss.standardize_netkey(nkey)
249
250 # Main use case: networks
251 21 14110.0 671.9 0.0 if isinstance(route, ss.Network):
252 21 245540.0 11692.4 0.4 if len(route): # Skip networks with no edges
253 21 9102.0 433.4 0.0 edges = route.edges
254 21 339498.0 16166.6 0.5 p1p2b0 = [edges.p1, edges.p2, betamap[nk][0]] # Person 1, person 2, beta 0
255 21 351036.0 16716.0 0.5 p2p1b1 = [edges.p2, edges.p1, betamap[nk][1]] # Person 2, person 1, beta 1
256 63 43005.0 682.6 0.1 for src, trg, beta in [p1p2b0, p2p1b1]:
257 42 73803.0 1757.2 0.1 if beta: # Skip networks with no transmission
258 42 1147050.0 27310.7 1.7 disease_beta = beta.to_prob(self.t.dt) if isinstance(beta, ss.Rate) else beta
259 42 1806643.0 43015.3 2.6 beta_per_dt = route.net_beta(disease_beta=disease_beta, disease=self) # Compute beta for this network and timestep
260 42 49036756.0 1.17e+06 70.7 randvals = self.trans_rng.rvs(src, trg) # Generate a new random number based on the two other random numbers
261 42 37786.0 899.7 0.1 args = (src, trg, rel_trans, rel_sus, beta_per_dt, randvals) # Set up the arguments to calculate transmission
262 42 8029165.0 191170.6 11.6 target_uids, source_uids = self.compute_transmission(*args) # Actually calculate it
263 42 28622.0 681.5 0.0 new_cases.append(target_uids)
264 42 16997.0 404.7 0.0 sources.append(source_uids)
265 42 449567.0 10704.0 0.6 networks.append(np.full(len(target_uids), dtype=ss_int, fill_value=i))
266
267 # Handle everything else: mixing pools, environmental transmission, etc.
268 elif isinstance(route, ss.Route):
269 # Mixing pools are unidirectional, only use the first beta value
270 disease_beta = betamap[nk][0].to_prob(self.t.dt) if isinstance(betamap[nk][0], ss.Rate) else betamap[nk][0]
271 target_uids = route.compute_transmission(rel_sus, rel_trans, disease_beta, disease=self)
272 new_cases.append(target_uids)
273 sources.append(np.full(len(target_uids), dtype=ss_int, fill_value=ss.dtypes.int_nan))
274 networks.append(np.full(len(target_uids), dtype=ss_int, fill_value=i))
275 else:
276 errormsg = f'Cannot compute transmission via route {type(route)}; please subclass ss.Route and define a compute_transmission() method'
277 raise TypeError(errormsg)
278
279 # Finalize
280 21 16545.0 787.9 0.0 if len(new_cases) and len(sources):
281 21 366780.0 17465.7 0.5 new_cases = ss.uids.concatenate(new_cases)
282 21 1401365.0 66731.7 2.0 new_cases, inds = new_cases.unique(return_index=True)
283 21 287012.0 13667.2 0.4 sources = ss.uids.concatenate(sources)[inds]
284 21 102853.0 4897.8 0.1 networks = np.concatenate(networks)[inds]
285 else:
286 new_cases = ss.uids()
287 sources = ss.uids()
288 networks = np.empty(0, dtype=ss_int)
289
290 21 11837.0 563.7 0.0 return new_cases, sources, networks
————————————————————————————————————————————————————————————
Profile of diseases.Infection.infect: 0.0693591 s (39.9006%)
————————————————————————————————————————————————————————————
Total time: 0.0693591 s
File: /home/runner/work/starsim/starsim/starsim/diseases.py
Function: Infection.infect at line 237
Line # Hits Time Per Hit % Time Line Contents
==============================================================
237 def infect(self):
238 """ Determine who gets infected on this timestep via transmission on the network """
239 21 17638.0 839.9 0.0 new_cases = []
240 21 7993.0 380.6 0.0 sources = []
241 21 7089.0 337.6 0.0 networks = []
242 21 639642.0 30459.1 0.9 betamap = self.validate_beta()
243
244 21 2626742.0 125083.0 3.8 rel_trans = self.rel_trans.asnew(self.infectious * self.rel_trans)
245 21 2058114.0 98005.4 3.0 rel_sus = self.rel_sus.asnew(self.susceptible * self.rel_sus)
246
247 42 152497.0 3630.9 0.2 for i, (nkey,route) in enumerate(self.sim.networks.items()):
248 21 34332.0 1634.9 0.0 nk = ss.standardize_netkey(nkey)
249
250 # Main use case: networks
251 21 14110.0 671.9 0.0 if isinstance(route, ss.Network):
252 21 245540.0 11692.4 0.4 if len(route): # Skip networks with no edges
253 21 9102.0 433.4 0.0 edges = route.edges
254 21 339498.0 16166.6 0.5 p1p2b0 = [edges.p1, edges.p2, betamap[nk][0]] # Person 1, person 2, beta 0
255 21 351036.0 16716.0 0.5 p2p1b1 = [edges.p2, edges.p1, betamap[nk][1]] # Person 2, person 1, beta 1
256 63 43005.0 682.6 0.1 for src, trg, beta in [p1p2b0, p2p1b1]:
257 42 73803.0 1757.2 0.1 if beta: # Skip networks with no transmission
258 42 1147050.0 27310.7 1.7 disease_beta = beta.to_prob(self.t.dt) if isinstance(beta, ss.Rate) else beta
259 42 1806643.0 43015.3 2.6 beta_per_dt = route.net_beta(disease_beta=disease_beta, disease=self) # Compute beta for this network and timestep
260 42 49036756.0 1.17e+06 70.7 randvals = self.trans_rng.rvs(src, trg) # Generate a new random number based on the two other random numbers
261 42 37786.0 899.7 0.1 args = (src, trg, rel_trans, rel_sus, beta_per_dt, randvals) # Set up the arguments to calculate transmission
262 42 8029165.0 191170.6 11.6 target_uids, source_uids = self.compute_transmission(*args) # Actually calculate it
263 42 28622.0 681.5 0.0 new_cases.append(target_uids)
264 42 16997.0 404.7 0.0 sources.append(source_uids)
265 42 449567.0 10704.0 0.6 networks.append(np.full(len(target_uids), dtype=ss_int, fill_value=i))
266
267 # Handle everything else: mixing pools, environmental transmission, etc.
268 elif isinstance(route, ss.Route):
269 # Mixing pools are unidirectional, only use the first beta value
270 disease_beta = betamap[nk][0].to_prob(self.t.dt) if isinstance(betamap[nk][0], ss.Rate) else betamap[nk][0]
271 target_uids = route.compute_transmission(rel_sus, rel_trans, disease_beta, disease=self)
272 new_cases.append(target_uids)
273 sources.append(np.full(len(target_uids), dtype=ss_int, fill_value=ss.dtypes.int_nan))
274 networks.append(np.full(len(target_uids), dtype=ss_int, fill_value=i))
275 else:
276 errormsg = f'Cannot compute transmission via route {type(route)}; please subclass ss.Route and define a compute_transmission() method'
277 raise TypeError(errormsg)
278
279 # Finalize
280 21 16545.0 787.9 0.0 if len(new_cases) and len(sources):
281 21 366780.0 17465.7 0.5 new_cases = ss.uids.concatenate(new_cases)
282 21 1401365.0 66731.7 2.0 new_cases, inds = new_cases.unique(return_index=True)
283 21 287012.0 13667.2 0.4 sources = ss.uids.concatenate(sources)[inds]
284 21 102853.0 4897.8 0.1 networks = np.concatenate(networks)[inds]
285 else:
286 new_cases = ss.uids()
287 sources = ss.uids()
288 networks = np.empty(0, dtype=ss_int)
289
290 21 11837.0 563.7 0.0 return new_cases, sources, networks
(Note: you can only follow functions that are called as part of sim.run() this way. To follow other functions, such as those run by sim.init(), you can use sc.profile() directly.)
Debugging
When figuring out what your sim is doing – whether it’s doing something it shouldn’t be, or not doing something it should – sim.loop is your friend. It shows everything that will happen in the sim, and in what order:
As you can see, it’s a lot – this is only three timesteps and two modules, and it’s already 41 steps.
The typical way to do debugging is to insert breakpoints or print statements into your modules for custom debugging (e.g., to print a value), or to use analyzers for heavier-lift debugging. Starsim also lets you manually modify the loop by inserting “probes” or other arbitrary functions. For example, if you wanted to check the population size after each time the People object is updated:
Initializing sim with 10000 agents
Running 2000.01.01 ( 0/11) (0.00 s) •——————————————————— 9%
Population size is 10191
Population size is 10264
Population size is 10357
Population size is 10447
Population size is 10557
Population size is 10664
Population size is 10739
Population size is 10817
Population size is 10917
Population size is 11033
Running 2010.01.01 (10/11) (0.08 s) •••••••••••••••••••• 100%
Population size is 11124
Initializing sim with 10000 agents
Running 2000.01.01 ( 0/11) (0.00 s) •——————————————————— 9%
Population size is 10191
Population size is 10264
Population size is 10357
Population size is 10447
Population size is 10557
Population size is 10664
Population size is 10739
Population size is 10817
Population size is 10917
Population size is 11033
Running 2010.01.01 (10/11) (0.08 s) •••••••••••••••••••• 100%
Population size is 11124
However, inserting functions directly in the loop gives you more control over their exact placement, whereas analyzers are always executed last in the timestep.
The loop also has methods for visualizing itself. You can get a simple representation of the loop with loop.plot():
sim.loop.plot()
Figure(672x480)
Or a slightly more detailed one with loop.plot_step_order():
sim.loop.plot_step_order()
Figure(672x480)
This is especially useful if your simulation has modules with different timesteps, e.g.:
Initializing sim with 10000 agents
Figure(672x480)
(Note: this is a 3D plot, so it helps if you can plot it in a separate window interactively to be able to move it around, rather than just in a notebook.)