zea.utils

General utility functions.

Functions

block_until_ready(func)

Decorator that ensures asynchronous (gpu) operations complete before returning.

canonicalize_axis(axis, num_dims)

Canonicalize an axis in [-num_dims, num_dims) to [0, num_dims).

date_string_to_readable(date_string[, ...])

Converts a date string to a more readable format.

deep_compare(obj1, obj2)

Recursively compare two objects for equality.

get_date_string([string])

Generate a date string for current time, according to format specified by string.

map_negative_indices(indices, num_dims)

Maps negative indices for array indexing to positive indices.

print_clear_line()

Clears line.

strtobool(val)

Convert a string representation of truth to True or False.

update_dictionary(dict1, dict2[, keep_none])

Updates dict1 with values dict2

Classes

FunctionTimer()

A decorator class for timing the execution of functions.

ProgressBar(*args, **kwargs)

A keras.utils.Progbar that cooperates with zea.log.

class zea.utils.FunctionTimer[source]

Bases: object

A decorator class for timing the execution of functions.

Example

>>> from zea.utils import FunctionTimer
>>> timer = FunctionTimer()
>>> my_function = lambda: sum(range(10))
>>> my_function = timer(my_function, name="my_function")
>>> _ = my_function()
>>> print(timer.get_stats("my_function"))
{'mean': ..., 'median': ..., 'std_dev': ..., 'min': ..., 'max': ..., 'count': ...}
__call__(func, name=None)[source]

Call self as a function.

append_to_yaml(filename, func_name)[source]

Append the timing data to a YAML file.

export_to_yaml(filename)[source]

Export the timing data to a YAML file.

get_stats(func_name, drop_first=False)[source]

Calculate statistics for the given function.

print(drop_first=False, total_time=False)[source]

Print timing statistics for all recorded functions using formatted output.

class zea.utils.ProgressBar(*args, **kwargs)[source]

Bases: Progbar

A keras.utils.Progbar that cooperates with zea.log.

Drop-in replacement for keras.utils.Progbar that registers itself with the logger while active, so that log.info/log.warning/etc. calls made in between updates are printed above the bar instead of corrupting its line.

redraw()[source]

Forces a fresh render of the current state, bypassing the update-rate throttle.

update(current, values=None, finalize=None)[source]

Updates the progress bar.

Parameters:
  • current – Index of current step.

  • values – List of tuples: (name, value_for_last_step). If name is in stateful_metrics, value_for_last_step will be displayed as-is. Else, an average of the metric over time will be displayed.

  • finalize – Whether this is the last update for the progress bar. If None, defaults to current >= self.target.

zea.utils.block_until_ready(func)[source]

Decorator that ensures asynchronous (gpu) operations complete before returning.

zea.utils.canonicalize_axis(axis, num_dims)[source]

Canonicalize an axis in [-num_dims, num_dims) to [0, num_dims).

Return type:

int

zea.utils.date_string_to_readable(date_string, include_time=False)[source]

Converts a date string to a more readable format.

Parameters:
  • date_string (str) – The input date string.

  • include_time (bool) – Whether to include the time in the output. Defaults to False.

Returns:

The date string in a more readable format.

Return type:

str

zea.utils.deep_compare(obj1, obj2)[source]

Recursively compare two objects for equality.

zea.utils.get_date_string(string=None)[source]

Generate a date string for current time, according to format specified by string. Refer to the documentation of the datetime module for more information on the formatting options.

If no string is specified, the default format is used: “%Y_%m_%d_%H%M%S”.

zea.utils.map_negative_indices(indices, num_dims)[source]

Maps negative indices for array indexing to positive indices. .. rubric:: Example

>>> from zea.utils import map_negative_indices
>>> map_negative_indices([-1, -2], 5)
[4, 3]
zea.utils.print_clear_line()[source]

Clears line. Helpful when printing in a loop on the same line.

zea.utils.strtobool(val)[source]

Convert a string representation of truth to True or False.

True values are ‘y’, ‘yes’, ‘t’, ‘true’, ‘on’, and ‘1’; false values are ‘n’, ‘no’, ‘f’, ‘false’, ‘off’, and ‘0’. Raises ValueError if ‘val’ is anything else.

zea.utils.update_dictionary(dict1, dict2, keep_none=False)[source]

Updates dict1 with values dict2

Parameters:
  • dict1 (dict) – base dictionary

  • dict2 (dict) – update dictionary

  • keep_none (bool) – whether to keep keys with None values in dict2. Defaults to False.

Returns:

updated dictionary

Return type:

dict