Custom progress reporting

Progress reporting

For custom progress reporting (e.g. graphical output, writing to a file, etc.), the report keyword accepts a callable (i.e. a function or an object with a __call__ method) that will be called with four parameters:

  • elapsed: the total (real) time since the start of the run
  • completed: the fraction of the total simulation that is completed, i.e. a value between 0 and 1
  • start: The start of the simulation (in biological time)
  • duration: the total duration (in biological time) of the simulation

The function will be called every report_period during the simulation, but also at the beginning and end with completed equal to 0.0 and 1.0, respectively.

For the C++ standalone mode, the same standard options are available. It is also possible to implement custom progress reporting by directly passing the code (as a multi-line string) to the report argument. This code will be filled into a progress report function template, it should therefore only contain a function body. The simplest use of this might look like:, report='std::cout << (int)(completed*100.) << "% completed" << std::endl;')

Examples of custom reporting

Progress printed to a file

from import TextReport
report_file = open('report.txt', 'w')
file_reporter = TextReport(report_file), report=file_reporter)

“Graphical” output on the console

This needs a “normal” Linux console, i.e. it might not work in an integrated console in an IDE.

Adapted from

import sys

class ProgressBar(object):
    def __init__(self, toolbar_width):
        self.toolbar_width = toolbar_width
        self.ticks = 0

    def __call__(self, elapsed, complete, start, duration):
        if complete == 0.0:
            # setup toolbar
            sys.stdout.write("[%s]" % (" " * self.toolbar_width))
            sys.stdout.write("\b" * (self.toolbar_width + 1)) # return to start of line, after '['
            ticks_needed = int(round(complete * 40))
            if self.ticks < ticks_needed:
                sys.stdout.write("-" * (ticks_needed-self.ticks))
                self.ticks = ticks_needed
        if complete == 1.0:
            sys.stdout.write("\n"), report=progress_bar, report_period=1*second)