stable_gym.common.utils

Utility functions that are used in multiple Stable Gym gymnasium environments.

Module Contents

Functions

colorize(string, color[, bold, highlight])

Colorize a string.

get_flattened_values(input_obj)

Retrieves all the values that are present in a nested dictionary and appends them

get_flattened_keys(input_obj[, include_root])

Retrieves all the keys that are present in a nested dictionary and appends them

abbreviate(input_item[, length, max_length, capitalize])

Creates unique abbreviations for a string or list of strings.

get_lowest_next_int(input_item)

Retrieves the lowest next integer that is not present in a string or float list.

friendly_list(input_list[, apostrophes])

Transforms a list to a human friendly format (separated by commas and ampersand).

strip_underscores(text[, position])

Strips leading and/or trailing underscores from a string.

inject_value(input_item, value[, round_accuracy, ...])

Injects a value into a list or dictionary if it is not yet present.

verify_number_and_cast(x)

Verify parameter is a single number and cast to a float.

maybe_parse_reset_bounds(options, default_low, ...)

This function can be called during a reset() to customize the sampling

change_dict_key(d, old_key, new_key[, default_value])

Changes the key of a dictionary.

convert_gym_box_to_gymnasium_box(gym_box_space, **kwargs)

Converts a gym box space to a gymnasium box space.

change_precision(input_value[, precision])

Changes the precision of a value.

stable_gym.common.utils.colorize(string, color, bold=False, highlight=False)[source]

Colorize a string.

See also

This function wraps the gym.utils.colorize() function to make sure that it also works with empty color strings.

Parameters:
  • string (str) – The string you want to colorize.

  • color (str) – The color you want to use.

  • bold (bool, optional) – Whether you want the text to be bold. Defaults to False.

  • highlight (bool, optional) – Whether you want to highlight the text. Defaults to False.

Returns:

Colorized string.

Return type:

str

stable_gym.common.utils.get_flattened_values(input_obj)[source]

Retrieves all the values that are present in a nested dictionary and appends them to a list. Its like a recursive version of the dict.values() method.

Parameters:

input_obj (dict) – The input dictionary from which you want to retrieve all the values.

Returns:

A list containing all the values that were present in the nested

dictionary.

Return type:

list

stable_gym.common.utils.get_flattened_keys(input_obj, include_root=False)[source]

Retrieves all the keys that are present in a nested dictionary and appends them to a list. Its like a recursive version of the dict.keys() method.

Parameters:
  • input_obj (dict) – The input dictionary from which you want to retrieve all the keys.

  • include_root (bool) – Whether you want to include the root level keys. Defaults to False.

Returns:

A list containing all the keys that were present in the nested

dictionary.

Return type:

list

stable_gym.common.utils.abbreviate(input_item, length=1, max_length=4, capitalize=True)[source]

Creates unique abbreviations for a string or list of strings.

Parameters:
  • input_item (union[str, list]) – The string of list of strings which you want to abbreviate.

  • length (int, optional) – The desired length of the abbreviation. Defaults to 1.

  • max_length (int, optional) – The maximum length of the abbreviation. Defaults to 4.

  • capitalize (bool, optional) – Whether the abbrevaitions should be capitalized. Defaults to True.

Returns:

List with abbreviations.

Return type:

list

stable_gym.common.utils.get_lowest_next_int(input_item)[source]

Retrieves the lowest next integer that is not present in a string or float list.

Parameters:

input_item (union[int, str, list]) – The input for which you want to determine the next lowest interger.

Returns:

The next lowest integer.

Return type:

int

stable_gym.common.utils.friendly_list(input_list, apostrophes=False)[source]

Transforms a list to a human friendly format (separated by commas and ampersand).

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

  • apostrophes (bool, optional) – Whether the list items should be encapsuled with apostrophes. Defaults to False.

Returns:

Human friendly list string.

Return type:

str

stable_gym.common.utils.strip_underscores(text, position='all')[source]

Strips leading and/or trailing underscores from a string.

Parameters:
  • text (str) – The input string.

  • position (str, optional) – From which position underscores should be removed. Options are ‘leading’, ‘trailing’ & ‘both’. Defaults to “both”.

Returns:

String without the underscores.

Return type:

str

stable_gym.common.utils.inject_value(input_item, value, round_accuracy=2, order=False, axis=0)[source]

Injects a value into a list or dictionary if it is not yet present.

Parameters:
  • input_item (union[list,dict]) – The input list or dictionary.

  • value (float) – The value you want to inject.

  • round_accuracy (int, optional) – The accuracy used for checking whether a value is present. Defaults to 2.

  • order (bool, optional) – Whether the list should be ordered when returned. Defaults to false.

  • axis (int, optional) – The axis along which you want to inject the value. Only used when the input is a numpy array. Defaults to 0.

Returns:

The list or dictionary that contains the value.

Return type:

union[list,dict]

stable_gym.common.utils.verify_number_and_cast(x)[source]

Verify parameter is a single number and cast to a float.

stable_gym.common.utils.maybe_parse_reset_bounds(options, default_low, default_high)[source]

This function can be called during a reset() to customize the sampling ranges for setting the initial state distributions.

Parameters:
  • options – Options passed in to reset().

  • default_low – Default lower limit to use, if none specified in options.

  • default_high – Default upper limit to use, if none specified in options.

Returns:

a tuple containing:

  • low (np.ndarray): Lower limit for each dimension.

  • high ():obj:np.ndarray): Upper limit for each dimension.

Return type:

(tuple)

stable_gym.common.utils.change_dict_key(d, old_key, new_key, default_value=None)[source]

Changes the key of a dictionary.

Parameters:
  • d (dict) – The dictionary.

  • old_key (str) – The old key.

  • new_key (str) – The new key.

  • default_value (any, optional) – The default value to use if the old key is not present in the dictionary. Defaults to None.

stable_gym.common.utils.convert_gym_box_to_gymnasium_box(gym_box_space, **kwargs)[source]

Converts a gym box space to a gymnasium box space.

Parameters:
  • gym_box_space (gym.spaces.Box) – The gym box space.

  • **kwargs – Additional keyword arguments that are passed to the gymnasium box space.

Returns:

The gymnasium box space.

Return type:

gymnasium.spaces.Box

stable_gym.common.utils.change_precision(input_value, precision=16)[source]

Changes the precision of a value.

Parameters:
  • input_value (object) – The input value.

  • precision (int, optional) – The precision (i.e. number of decimals) to use. Defaults to 16. If None, the input value is returned as is.

Returns:

The input value with the new precision.

Return type:

object