nicetoolbox.utils.check_and_exception

Check and exception handling functions.

Functions

check_options

Check if an object is of a specific type and if it is within a given list of options.

check_token_in_filepath

Check if a given token is present in a folder name.

check_user_input_config

Check a given configuration dictionary for correct user inputs based on a template dictionary describing the valid inputs.

check_value_bounds

Check if an object's value falls within specified bounds.

check_zeros

Check if any vectors in the last dimension of an array are zero vectors.

error_log_and_raise

This function logs an error message and then raises a specific error with a formatted message.

file_exists

Check if a file exists at the given path.

load_dict_keys_values

Load keys or values from a .toml file based on the given command.

nicetoolbox.utils.check_and_exception.check_options(object, object_type, options) None[source]

Check if an object is of a specific type and if it is within a given list of options.

Parameters:
  • object (any) – The object to be checked.

  • object_type (type) – The expected type of the object.

  • options (list) – A list of valid options for the object.

Raises:
  • TypeError – If the object is not of the expected type.

  • ValueError – If the object is not within the list of valid options.

Returns:

None

nicetoolbox.utils.check_and_exception.check_token_in_filepath(folder_name: str, token: str, description: str) None[source]

Check if a given token is present in a folder name.

Parameters:
  • folder_name (str) – The name of the folder to check.

  • token (str) – The token to search for in the folder name.

  • description (str) – A description of the folder for error messages.

Raises:

ValueError – If the token is not found in the folder name.

Returns:

None

nicetoolbox.utils.check_and_exception.check_user_input_config(config, check, config_name, var=None)[source]

Check a given configuration dictionary for correct user inputs based on a template dictionary describing the valid inputs.

Parameters:
  • config (dict) – The config that contains all inputs to be checked.

  • check (dict) – The template specifying which inputs are valid for each dict key.

  • config_name (str) – The name of the config to be checked, used for more descriptive logs.

Note

The keys of ‘check’ must include the keys of ‘config’. Syntax of the dict values:

‘type:<str/int/bool/…>’: specifies the valid data type ‘folder:<base/full>’: requires existence of the folder (in case of ‘full’)

or parent-folder (in case of ‘base’)

‘file’: requires that the file is existing on the system ‘keys:<toml_filepath>’: valid options are all keys from the dict given

by the toml_filepath

‘tbd’: not yet defined in the template dict ‘check’,

will write a warning to log

[<>, …]: list of valid options, may contain all basetypes

nicetoolbox.utils.check_and_exception.check_value_bounds(object, object_type=None, object_min=None, object_max=None) None[source]

Check if an object’s value falls within specified bounds.

Parameters:
  • object (any) – The object to be checked.

  • object_type (type, optional) – The expected type of the object. Defaults to None.

  • object_min (any, optional) – The minimum value allowed for the object. Defaults to None.

  • object_max (any, optional) – The maximum value allowed for the object. Defaults to None.

Raises:
  • TypeError – If the object is not of the expected type (if object_type is provided).

  • ValueError – If the object’s value is less than object_min (if object_min is provided). If the object’s value is greater than object_max (if object_max is provided).

Returns:

None

nicetoolbox.utils.check_and_exception.check_zeros(arr: ndarray) None[source]

Check if any vectors in the last dimension of an array are zero vectors.

Args

arr (ndarray): The input array.

Raises

AssertionError: If there are any zero vectors found in the last dimension of the array.

Examples >>> arr_3d_example = np.random.randint(0, 255, (10, 10, 3)) >>> check_zeros(arr_3d_example)

nicetoolbox.utils.check_and_exception.error_log_and_raise(error, name, message)[source]

This function logs an error message and then raises a specific error with a formatted message.

Parameters:
  • error (Exception) – The type of error to be raised.

  • name (str) – The name of the function or method where the error occurred.

  • message (str) – The detailed error message.

Raises:

error – The specific error type raised with a formatted error message.

nicetoolbox.utils.check_and_exception.file_exists(file: str) None[source]

Check if a file exists at the given path.

Parameters:

file (str) – The path to the file to check.

Raises:

FileNotFoundError – If the file does not exist at the given path.

Returns:

None

nicetoolbox.utils.check_and_exception.load_dict_keys_values(command: str, config_name: str) list[source]

Load keys or values from a .toml file based on the given command.

Parameters:
  • command (str) –

    A string in the format ‘token:file:key(s)’ or ‘token:file’. ‘token’ should be either ‘keys’ or ‘values’. ‘file’ is the path to the .toml file. ‘key(s)’ is an optional parameter specifying the nested keys in the

    .toml file.

  • config_name (str) – The name of the configuration for error logging.

Returns:

A list of keys or values from the .toml file based on the given command.

Return type:

list

Raises:
  • NotImplementedError – If the file format is not supported.

  • AssertionError – If the token is neither ‘keys’ nor ‘values’.