utils.utils#

The utils module contains various utility functions for conversions and config handling.

soulsai.utils.utils.running_mean(x: List, N: int) ndarray#

Compute the running mean of a list with a sliding window.

The first N-1 values are left as is, since the sliding window does not have sufficient values.

Parameters:
  • x – A list of numeric values.

  • N – The size of the sliding window.

Returns:

An array of the running mean.

soulsai.utils.utils.running_std(x: List, N: int) ndarray#

Compute the running standard deviation of a list with a sliding window.

The first N-1 entries of the deviation are 0.

Parameters:
  • x – A list of numeric values.

  • N – The size of the sliding window.

Returns:

An array of the running standard deviation.

soulsai.utils.utils.module_type_from_string(module_name: str) Callable[[str], type]#

Create a function that gets attributes from a module by name.

Example

>>> import torch
>>> torch_type = module_type_from_string("torch")
>>> tensor_type = torch_type("Tensor")
>>> tensor_type
<class 'torch.Tensor'>
Parameters:

module_name – The module name.

Returns:

The module attribute.

soulsai.utils.utils.mkdir_date(path: Path) Path#

Make a unique directory within the given directory with the current time as name.

Parameters:

path – Parent folder path.

soulsai.utils.utils.load_config(default_config_path: Path, config_path: Path | None = None) SimpleNamespace#

Load the training configuration from the specified paths.

The default_config_path argument should point to a complete configuration with all necessary parameters. In order to overwrite the default parameters, another config file at config_path can be specified. This configuration always superseeds the default configuration.

Parameters:
  • default_config_path – Path to the default configuration.

  • config_path – Optional path to a custom configuration.

Returns:

The configuration as a SimpleNamespace.

Raises:

InvalidConfigError – An invalid logging level has been specified.

soulsai.utils.utils.dict2namespace(ns_dict: dict) SimpleNamespace#

Convert a dictionary to a (possibly nested) SimpleNamespace.

All dictionary key value pairs are converted to namespace attributes. If the value is a dictionary, another namespace object is used.

Parameters:

ns_dict – Dictionary for conversion.

Returns:

A namespace equivalent of the dictionary.

soulsai.utils.utils.namespace2dict(ns: SimpleNamespace | None) dict#

Convert a SimpleNamespace to a (possibly nested) dictionary.

All namespace attributes are converted to dictionary key value pairs. If the attribute is another namespace, it is also converted to a dictionary.

Parameters:

ns – NameSpace object.

Returns:

A (possibly nested) dictionary of the namespace.

soulsai.utils.utils.load_remote_config(address: str, secret: str, red: RedisType | None = None, local_config: SimpleNamespace | None = None) SimpleNamespace#

Load the training configuration from the training server.

This function allows us to only specify the address of a training server and its credentials. All hyperparameters etc. are copied from the server.

Parameters:
  • address – Address of the training server.

  • secret – Redis secret.

  • red – Optional redis instance that is used to load the remote config.

  • local_config – Optional local config that should not be overwritten by the remote config.

Returns:

The remote training configuration.

soulsai.utils.utils.load_redis_secret(path: Path, default_path: Path = PosixPath('/run/secrets/redis_secret')) str#

Load the redis secret from a .secret file.

The file is expected to contain the the line “requirepass XXX”, where XXX is the redis secret. If no file is found, the default path is used.

Warning

If a secret file is present under /run/secrets/redis.secret, the supplied path argument is ignored!

Parameters:
  • path – Path to the secret file.

  • default_path – Default path that is checked if the secret is not found under path.

Returns:

The secret.