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:

# mymodule.py
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

log_path

str, optional – Sets the default log path (overriden when path is given to _setup_logger())

config_path

str, optional – Sets the configuration path for custom loggers

logger

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

printer

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

_load_defaults()[source]

Load default logging configuration

_setup_logger(path=None)[source]

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()
        rep.hook(best_cost=best_cost)
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.

Parameters:
  • 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")
        pass
Parameters:
  • iters (int) – Maximum range passed to the tqdm instance
  • desc (str, optional) – Name of the progress bar that will be displayed
Returns:

A tqdm iterable

Return type:

tqdm._tqdm.tqdm

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.

Parameters:
  • 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