stable_learning_control.common.helpers

Contains several helper functions that are used throughout the SLC package.

Functions

atleast_2d(array[, axis])

Similar to numpy.atleast_2d() but with an additional axis argument

convert_to_tuple(input_var)

Converts input into a tuple.

flatten(items)

Flatten a list with any nested iterable.

get_unique_list(input_list[, trim])

Removes non-unique items from a list.

combine_shapes(*args[, remove_none])

Combines multiple tuples/ints/floats into one tuple.

strict_dict_update(input_dict, update_obj)

Updates a dictionary with values supplied in another dict or python

valid_str(v)

Convert a value or values to a string which could go in a filepath.

all_bools(vals)

Check if list contains only strings.

is_scalar(obj)

Recursive function that checks whether a input

get_env_id(env)

Returns the environment id of a given environment.

get_env_class(env)

Get the environment class.

parse_config_env_key(config)

Replace environment objects (i.e. gym.Env) with their id and class path if they

convert_to_snake_case(input_str)

Converts a string from camel/pascal case to snake case.

friendly_err(err_msg[, prepend, append])

Add whitespace line to error message to make it more readable.

flatten_dict(d[, parent_key, sep])

Flattens a nested dictionary.

convert_to_wandb_config(config)

Transform the config to a format that looks better on Weights & Biases.

convert_to_tb_config(config)

Transform the config to a format that looks better on TensorBoard.

Module Contents

stable_learning_control.common.helpers.atleast_2d(array, axis=1)[source]

Similar to numpy.atleast_2d() but with an additional axis argument which can be used to specify where the extra dimension should be-added.

Parameters:
  • array (numpy.ndarray) – [description]

  • axis (int, optional) – Position in the expanded axes where the new axis (or axes) is placed if the dimension is smaller than 2. Defaults to 1.

Returns:

The 2D numpy array.

Return type:

numpy.ndarray

stable_learning_control.common.helpers.convert_to_tuple(input_var)[source]

Converts input into a tuple.

Parameters:

input_arg (Union[int, float, list]) – A input variable.

Returns:

A tuple.

Return type:

tuple

stable_learning_control.common.helpers.flatten(items)[source]

Flatten a list with any nested iterable.

Parameters:

items (list) – A nested list.

Returns:

A flattened version of the list.

Return type:

list

stable_learning_control.common.helpers.get_unique_list(input_list, trim=True)[source]

Removes non-unique items from a list.

Parameters:
  • input_list (list) – The input list.

  • trim (list, optional) – Trim empty items. Defaults to True.

Returns:

The new list containing only unique items.

Return type:

list

stable_learning_control.common.helpers.combine_shapes(*args, remove_none=False)[source]

Combines multiple tuples/ints/floats into one tuple.

Parameters:
  • *args (Union[tuple,int,float]) – Input arguments to combine.

  • remove_none (bool, optional) – Remove None values from the resulting tuple.

Returns:

A tuple in which al the input arguments are combined.

Return type:

Tuple

stable_learning_control.common.helpers.strict_dict_update(input_dict, update_obj)[source]

Updates a dictionary with values supplied in another dict or python object. This function performs a strict update, meaning it does not add new keys to the original dictionary.Additionally, if a Python object is supplied, it will be applied to all keys in the dictionary.

Parameters:
  • input_dict (dict) – The input dictionary.

  • update_dict (Union[dict, list]) – Dictionary or list containing the update values.

Returns:

tuple containing:

  • input_dict(list): The new updated dictionary.

  • ignored (str): The ignored keys.

Return type:

(tuple)

stable_learning_control.common.helpers.valid_str(v)[source]

Convert a value or values to a string which could go in a filepath.

Note

Partly based on this gist.

Parameters:

v (list) – List with values.

stable_learning_control.common.helpers.all_bools(vals)[source]

Check if list contains only strings.

Parameters:

vals (list) – List with values.

Returns:

Boolean specifying the result.

Return type:

bool

stable_learning_control.common.helpers.is_scalar(obj)[source]

Recursive function that checks whether a input

Parameters:

obj (object) – Object for which you want to check if it is a scalar.

Returns:

Boolean specifying whether the object is a scalar.

Return type:

boole

stable_learning_control.common.helpers.get_env_id(env)[source]

Returns the environment id of a given environment.

Parameters:

env (gym.Env) – The environment.

Returns:

The environment id.

Return type:

str

stable_learning_control.common.helpers.get_env_class(env)[source]

Get the environment class.

Parameters:

env (gym.Env) – The environment.

Returns:

The environment class.

Return type:

str

stable_learning_control.common.helpers.parse_config_env_key(config)[source]

Replace environment objects (i.e. gym.Env) with their id and class path if they are present in the config. Also removes the ‘env_fn’ from the config.

Parameters:

config (dict) – The configuration dictionary.

Returns:

The parsed configuration dictionary.

Return type:

dict

stable_learning_control.common.helpers.convert_to_snake_case(input_str)[source]

Converts a string from camel/pascal case to snake case.

Parameters:

input_str (str) – The input string.

Returns:

The converted string.

Return type:

str

stable_learning_control.common.helpers.friendly_err(err_msg, prepend=True, append=True)[source]

Add whitespace line to error message to make it more readable.

Parameters:
  • err_msg (str) – Error message.

  • prepend (bool, optional) – whether to prepend empty whitespace line before the string. Defaults to True.

  • append (bool, optional) – Whether to append empty whitespace line after the string. Defaults to True.

Returns:

Error message with extra whitespace line.

Return type:

str

stable_learning_control.common.helpers.flatten_dict(d, parent_key='', sep='.')[source]

Flattens a nested dictionary.

Parameters:
  • d (dict) – The input dictionary.

  • parent_key (str, optional) – The parent key. Defaults to "".

  • sep (str, optional) – The separator. Defaults to ".".

Returns:

The flattened dictionary.

Return type:

dict

stable_learning_control.common.helpers.convert_to_wandb_config(config)[source]

Transform the config to a format that looks better on Weights & Biases.

Parameters:

config (dict) – The config that should be transformed.

Returns:

The transformed config.

Return type:

dict

stable_learning_control.common.helpers.convert_to_tb_config(config)[source]

Transform the config to a format that looks better on TensorBoard.

Parameters:

config (dict) – The config that should be transformed.

Returns:

The transformed config.

Return type:

dict