pyswarms.utils.reporter package

class pyswarms.utils.reporter.reporter.Reporter(log_path=None, config_path=None, logger=None, printer=None)[source]

Bases: object

A Reporter object that abstracts various logging capabilities

To set-up a Reporter, simply perform the following tasks:

from pyswarms.utils import Reporter

rep = Reporter()
rep.log("Here's my message", lvl=logging.INFO)

This will set-up a reporter with a default configuration that logs to a file, report.log, on the current working directory. You can change the log path by passing a string to the log_path parameter:

from pyswarms.utils import Reporter

rep = Reporter(log_path="/path/to/log/file.log")
rep.log("Here's my message", lvl=logging.INFO)

If you are working on a module and you have an existing logger, you can pass that logger instance during initialization:

from pyswarms.utils import Reporter

# An existing logger in a module
logger = logging.getLogger(__name__)
rep = Reporter(logger=logger)

Lastly, if you have your own logger configuration (YAML file), then simply pass that to the config_path parameter. This overrides the default configuration (including log_path):

from pyswarms.utils import Reporter

rep = Reporter(config_path="/path/to/config/file.yml")
rep.log("Here's my message", lvl=logging.INFO)
__init__(log_path=None, config_path=None, logger=None, printer=None)[source]

Initialize the reporter


Sets the default log path (overriden when path is given to _setup_logger())

Type:str, optional

Sets the configuration path for custom loggers

Type:str, optional

The logger object. By default, it creates a new Logger instance

Type:logging.Logger, optional

A printer object. By default, it creates a PrettyPrinter instance with default values

Type:pprint.PrettyPrinter, optional

Load default logging configuration


Set-up the logger with default values

This method is called right after initializing the Reporter module. If no path is supplied, then it loads a default configuration. You can view the defaults via the Reporter._default_config attribute.

Parameters:path (str, optional) – Path to a YAML configuration. If not supplied, uses a default config.
hook(*args, **kwargs)[source]

Set a hook on the progress bar

Method for creating a postfix in tqdm. In practice we use this to report the best cost found during an iteration:

from pyswarms.utils import Reporter

rep = Reporter()
# Create a progress bar
for i in rep.pbar(100, name="Optimizer")
        best_cost = compute()
log(msg, lvl=20, *args, **kwargs)[source]

Log a message within a set level

This method abstracts the logging.Logger.log() method. We use this method during major state changes, errors, or critical events during the optimization run.

You can check logging levels on this link. In essence, DEBUG is 10, INFO is 20, WARNING is 30, ERROR is 40, and CRITICAL is 50.

  • msg (str) – Message to be logged
  • lvl (int, optional) – Logging level. Default is logging.INFO
pbar(iters, desc=None)[source]

Create a tqdm iterable

You can use this method to create progress bars. It uses a set of abstracted methods from tqdm:

from pyswarms.utils import Reporter

rep = Reporter()
# Create a progress bar
for i in rep.pbar(100, name="Optimizer")
  • iters (int) – Maximum range passed to the tqdm instance
  • desc (str, optional) – Name of the progress bar that will be displayed

A tqdm iterable

Return type:


print(msg, verbosity, threshold=0)[source]

Print a message into console

This method can be called during non-system calls or minor state changes. In practice, we call this method when reporting the cost on a given timestep.

  • msg (str) – Message to be printed
  • verbosity (int) – Verbosity parameter, prints message when it’s greater than the threshold
  • threshold (int, optional) – Threshold parameter, prints message when it’s lesser than the verbosity. Default is 0