def test_progress_bar(): sc.heading('Progress bar') n = 50 for i in range(n): sc.progressbar(i + 1, n) sc.timedsleep(1.0 / n, verbose=False) return i
def run(self, do_plot=False, until=None, restore_pars=True, reset_seed=True, verbose=None, **kwargs): ''' Run the simulation. Args: do_plot (bool): whether to plot until (int): day to run until restore_pars (bool): whether to make a copy of the parameters before the run and restore it after, so runs are repeatable reset_seed (bool): whether to reset the random number stream immediately before run verbose (float): level of detail to print, e.g. 0 = no output, 0.2 = print every 5th day, 1 = print every day kwargs (dict): passed to sim.plot() Returns: results (dict): the results object (also modifies in-place) ''' # Initialize T = sc.tic() if not self.initialized: self.initialize() else: self.validate_pars( ) # We always want to validate the parameters before running self.init_interventions() # And interventions if reset_seed: self.set_seed( ) # Ensure the random number generator is freshly initialized if restore_pars: orig_pars = sc.dcp( self.pars ) # Create a copy of the parameters, to restore after the run, in case they are dynamically modified if verbose is None: verbose = self['verbose'] if until: until = self.day(until) # Main simulation loop for t in self.tvec: # Print progress if verbose: elapsed = sc.toc(output=True) simlabel = f'"{self.label}": ' if self.label else '' string = f' Running {simlabel}{self.datevec[t]} ({t:2.0f}/{self.pars["n_days"]}) ({elapsed:0.2f} s) ' if verbose >= 2: sc.heading(string) else: if not (t % int(1.0 / verbose)): sc.progressbar(t + 1, self.npts, label=string, length=20, newline=True) # Do the heavy lifting -- actually run the model! self.step() # Check if we were asked to stop elapsed = sc.toc(T, output=True) if self['timelimit'] and elapsed > self['timelimit']: sc.printv(f"Time limit ({self['timelimit']} s) exceeded", 1, verbose) break elif self['stopping_func'] and self['stopping_func'](self): sc.printv("Stopping function terminated the simulation", 1, verbose) break if self.t == until: # If until is specified, just stop here return # End of time loop; compute cumulative results outside of the time loop self.finalize(verbose=verbose) # Finalize the results sc.printv(f'Run finished after {elapsed:0.2f} s.\n', 1, verbose) if restore_pars: self.restore_pars(orig_pars) if do_plot: # Optionally plot self.plot(**kwargs) return self.results
def run(self, do_plot=False, until=None, restore_pars=True, reset_seed=True, verbose=None, **kwargs): ''' Run the simulation. Args: do_plot (bool): whether to plot until (int/str): day or date to run until restore_pars (bool): whether to make a copy of the parameters before the run and restore it after, so runs are repeatable reset_seed (bool): whether to reset the random number stream immediately before run verbose (float): level of detail to print, e.g. 0 = no output, 0.2 = print every 5th day, 1 = print every day kwargs (dict): passed to sim.plot() Returns: results (dict): the results object (also modifies in-place) ''' # Initialization steps -- start the timer, initialize the sim and the seed, and check that the sim hasn't been run T = sc.tic() if verbose is None: verbose = self['verbose'] if not self.initialized: self.initialize() self._orig_pars = sc.dcp(self.pars) # Create a copy of the parameters, to restore after the run, in case they are dynamically modified if reset_seed: # Reset the RNG. If the simulation is newly created, then the RNG will be reset by sim.initialize() so the use case # for resetting the seed here is if the simulation has been partially run, and changing the seed is required self.set_seed() until = self.npts if until is None else self.day(until) if until > self.npts: raise AlreadyRunError(f'Requested to run until t={until} but the simulation end is t={self.npts}') if self.complete: raise AlreadyRunError('Simulation is already complete (call sim.initialize() to re-run)') if self.t >= until: # NB. At the start, self.t is None so this check must occur after initialization raise AlreadyRunError(f'Simulation is currently at t={self.t}, requested to run until t={until} which has already been reached') # Main simulation loop while self.t < until: # Check if we were asked to stop elapsed = sc.toc(T, output=True) if self['timelimit'] and elapsed > self['timelimit']: sc.printv(f"Time limit ({self['timelimit']} s) exceeded; call sim.finalize() to compute results if desired", 1, verbose) return elif self['stopping_func'] and self['stopping_func'](self): sc.printv("Stopping function terminated the simulation; call sim.finalize() to compute results if desired", 1, verbose) return # Print progress if verbose: simlabel = f'"{self.label}": ' if self.label else '' string = f' Running {simlabel}{self.datevec[self.t]} ({self.t:2.0f}/{self.pars["n_days"]}) ({elapsed:0.2f} s) ' if verbose >= 2: sc.heading(string) else: if not (self.t % int(1.0/verbose)): sc.progressbar(self.t+1, self.npts, label=string, length=20, newline=True) # Do the heavy lifting -- actually run the model! self.step() # If simulation reached the end, finalize the results if self.complete: self.finalize(verbose=verbose, restore_pars=restore_pars) sc.printv(f'Run finished after {elapsed:0.2f} s.\n', 1, verbose) if do_plot: # Optionally plot self.plot(**kwargs) return self.results else: return # If not complete, return nothing
def run(self, do_plot=False, until=None, verbose=None, **kwargs): ''' Run the simulation. Args: do_plot (bool): whether to plot until (int): day to run until verbose (int): level of detail to print kwargs (dict): passed to self.plot() Returns: results: the results object (also modifies in-place) ''' # Initialize T = sc.tic() if not self.initialized: self.initialize() else: self.validate_pars( ) # We always want to validate the parameters before running self.init_interventions() # And interventions if verbose is None: verbose = self['verbose'] if until: until = self.day(until) # Main simulation loop for t in self.tvec: # Print progress if verbose >= 1: elapsed = sc.toc(output=True) simlabel = f'"{self.label}": ' if self.label else '' string = f' Running {simlabel}{self.datevec[t]} ({t:2.0f}/{self.pars["n_days"]}) ({elapsed:0.2f} s) ' if verbose >= 2: sc.heading(string) elif verbose == 1: sc.progressbar(t + 1, self.npts, label=string, length=20, newline=True) # Do the heavy lifting -- actually run the model! self.step() # Check if we were asked to stop elapsed = sc.toc(T, output=True) if self['timelimit'] and elapsed > self['timelimit']: sc.printv(f"Time limit ({self['timelimit']} s) exceeded", 1, verbose) break elif self['stopping_func'] and self['stopping_func'](self): sc.printv("Stopping function terminated the simulation", 1, verbose) break if self.t == until: # If until is specified, just stop here return # End of time loop; compute cumulative results outside of the time loop self.finalize(verbose=verbose) # Finalize the results sc.printv(f'Run finished after {elapsed:0.2f} s.\n', 1, verbose) if do_plot: # Optionally plot self.plot(**kwargs) return self.results