dacbench.logger

Module Contents

Classes

AbstractLogger

Logger interface.

ModuleLogger

A logger for handling logging of one module. e.g. a wrapper or toplevel general logging.

Logger

A logger that manages the creation of the module loggers.

Functions

load_logs(log_file: pathlib.Path) → List[Dict]

Loads the logs from a jsonl written by any logger.

split(predicate: Callable, iterable: Iterable) → Tuple[(List, List)]

Splits the iterable into two list depending on the result of predicate.

flatten_log_entry(log_entry: Dict) → List[Dict]

Transforms a log entry of format like

list_to_tuple(list_: List) → Tuple

Recursively transforms a list of lists into tuples of tuples

log2dataframe(logs: List[dict], wide: bool = False, drop_columns: List[str] = ['time']) → pandas.DataFrame

Converts a list of log entries to a pandas dataframe.

seed_mapper(self)

instance_mapper(self)

dacbench.logger.load_logs(log_file: pathlib.Path) List[Dict]

Loads the logs from a jsonl written by any logger.

The result is the list of dicts in the format: {

‘instance’: 0, ‘episode’: 0, ‘step’: 1, ‘example_log_val’: {

‘values’: [val1, val2, … valn], ‘times: [time1, time2, …, timen],

} :param log_file: The path to the log file :type log_file: pathlib.Path

Returns

Return type

[Dict, …]

dacbench.logger.split(predicate: Callable, iterable: Iterable) Tuple[List, List]

Splits the iterable into two list depending on the result of predicate.

Parameters
  • predicate (Callable) – A function taking an element of the iterable and return Ture or False

  • iterable (Iterable) –

Returns

Return type

(positives, negatives)

dacbench.logger.flatten_log_entry(log_entry: Dict) List[Dict]

Transforms a log entry of format like

{

‘step’: 0, ‘episode’: 2, ‘some_value’: {

‘values’ : [34, 45], ‘times’:[‘28-12-20 16:20:53’, ‘28-12-20 16:21:30’],

}

} into [

{ ‘step’: 0,’episode’: 2, ‘value’: 34, ‘time’: ‘28-12-20 16:20:53’}, { ‘step’: 0,’episode’: 2, ‘value’: 45, ‘time’: ‘28-12-20 16:21:30’}

]

Parameters

log_entry (Dict) – A log entry

dacbench.logger.list_to_tuple(list_: List) Tuple

Recursively transforms a list of lists into tuples of tuples :param list_: (nested) list

Returns

Return type

(nested) tuple

dacbench.logger.log2dataframe(logs: List[dict], wide: bool = False, drop_columns: List[str] = ['time']) pandas.DataFrame

Converts a list of log entries to a pandas dataframe.

Usually used in combination with load_dataframe.

Parameters
  • logs (List) – List of log entries

  • wide (bool) – wide=False (default) produces a dataframe with columns (episode, step, time, name, value) wide=True returns a dataframe (episode, step, time, name_1, name_2, …) if the variable name_n has not been logged at (episode, step, time) name_n is NaN.

  • drop_columns (List[str]) – List of column names to be dropped (before reshaping the long dataframe) mostly used in combination with wide=True to reduce NaN values

Returns

Return type

dataframe

dacbench.logger.seed_mapper(self)
dacbench.logger.instance_mapper(self)
class dacbench.logger.AbstractLogger(experiment_name: str, output_path: pathlib.Path, step_write_frequency: int = None, episode_write_frequency: int = 1)

Logger interface.

The logger classes provide a way of writing structured logs as jsonl files and also help to track information like current episode, step, time …

In the jsonl log file each row corresponds to a step.

valid_types
property additional_info(self)
set_env(self, env: dacbench.AbstractEnv) None

Needed to infer automatically logged information like the instance id :param env: :type env: AbstractEnv

static _pretty_valid_types() str

Returns a string pretty string representation of the types that can be logged as values

static _init_logging_dir(log_dir: pathlib.Path) None

Prepares the logging directory :param log_dir: :type log_dir: pathlib.Path

Returns

Return type

None

is_of_valid_type(self, value: Any) bool
abstract close(self) None

Makes sure, that all remaining entries in the are written to file and the file is closed.

abstract next_step(self) None

Call at the end of the step. Updates the internal state and dumps the information of the last step into a json

abstract next_episode(self) None

Call at the end of episode.

See next_step

abstract write(self) None

Writes buffered logs to file.

Invoke manually if you want to load logs during a run.

abstract log(self, key: str, value) None
abstract log_dict(self, data)

Alternative to log if more the one value should be logged at once.

Parameters

data (dict) – a dict with key-value so that each value is a valid value for log

abstract log_space(self, key: str, value: Union[numpy.ndarray, Dict], space_info=None)

Special for logging gym.spaces.

Currently three types are supported: * Numbers: e.g. samples from Discrete * Fixed length arrays like MultiDiscrete or Box * Dict: assuming each key has fixed length array

Parameters
  • key – see log

  • value – see log

  • space_info – a list of column names. The length of this list must equal the resulting number of columns.

class dacbench.logger.ModuleLogger(output_path: pathlib.Path, experiment_name: str, module: str, step_write_frequency: int = None, episode_write_frequency: int = 1)

Bases: dacbench.logger.AbstractLogger

A logger for handling logging of one module. e.g. a wrapper or toplevel general logging.

Don’t create manually use Logger to manage ModuleLoggers

get_logfile(self) pathlib.Path
Returns

the path to the log file of this logger

Return type

pathlib.Path

close(self)

Makes sure, that all remaining entries in the are written to file and the file is closed.

__del__(self)
static __json_default(object)

Add supoort for dumping numpy arrays and numbers to json :param object:

__end_step(self)
static __init_dict()
reset_episode(self) None

Resets the episode and step.

Be aware that this can lead to ambitious keys if no instance or seed or other identifying additional info is set

Returns
__reset_step(self)
next_step(self)

Call at the end of the step. Updates the internal state and dumps the information of the last step into a json

next_episode(self)

Writes buffered logs to file.

Invoke manually if you want to load logs during a run.

write(self)

Writes buffered logs to file.

Invoke manually if you want to load logs during a run.

__buffer_to_file(self)
set_additional_info(self, **kwargs)

Can be used to log additional information for each step e.g. for seed, and instance id. :param kwargs:

log(self, key: str, value: Union[Dict, List, Tuple, str, int, float, bool]) None
__log(self, key, value, time)
log_dict(self, data: Dict) None

Alternative to log if more the one value should be logged at once.

Parameters

data (dict) – a dict with key-value so that each value is a valid value for log

static __space_dict(key: str, value, space_info)
log_space(self, key, value, space_info=None)

Special for logging gym.spaces.

Currently three types are supported: * Numbers: e.g. samples from Discrete * Fixed length arrays like MultiDiscrete or Box * Dict: assuming each key has fixed length array

Parameters
  • key – see log

  • value – see log

  • space_info – a list of column names. The length of this list must equal the resulting number of columns.

class dacbench.logger.Logger(experiment_name: str, output_path: pathlib.Path, step_write_frequency: int = None, episode_write_frequency: int = 1)

Bases: dacbench.logger.AbstractLogger

A logger that manages the creation of the module loggers.

To get a ModuleLogger for you module (e.g. wrapper) call module_logger = Logger(…).add_module(“my_wrapper”). From now on module_logger.log(…) or logger.log(…, module=”my_wrapper”) can be used to log.

The logger module takes care of updating information like episode and step in the subloggers. To indicate to the loggers the end of the episode or the next_step simple call logger.next_episode() or logger.next_step().

set_env(self, env: dacbench.AbstractEnv) None

Needed to infer automatically logged information like the instance id :param env: :type env: AbstractEnv

close(self)

Makes sure, that all remaining entries (from all sublogger) are written to files and the files are closed.

__del__(self)
next_step(self)

Call at the end of the step. Updates the internal state of all subloggers and dumps the information of the last step into a json

next_episode(self)

Call at the end of episode.

See next_step

reset_episode(self)
write(self)

Writes buffered logs to file.

Invoke manually if you want to load logs during a run.

add_module(self, module: Union[str, type]) dacbench.logger.ModuleLogger

Creates a sub-logger. For more details see class level documentation :param module: The module name or Wrapper-Type to create a sub-logger for :type module: str or type

Returns

Return type

ModuleLogger

add_agent(self, agent: dacbench.abstract_agent.AbstractDACBenchAgent)

Writes information about the agent :param agent: :type agent: AbstractDACBenchAgent

add_benchmark(self, benchmark: dacbench.AbstractBenchmark) None

Writes the config to the experiment path :param benchmark:

set_additional_info(self, **kwargs)
log(self, key, value, module)
log_space(self, key, value, module, space_info=None)

Special for logging gym.spaces.

Currently three types are supported: * Numbers: e.g. samples from Discrete * Fixed length arrays like MultiDiscrete or Box * Dict: assuming each key has fixed length array

Parameters
  • key – see log

  • value – see log

  • space_info – a list of column names. The length of this list must equal the resulting number of columns.

log_dict(self, data, module)

Alternative to log if more the one value should be logged at once.

Parameters

data (dict) – a dict with key-value so that each value is a valid value for log