Config

Module to save and load config files (python or yaml).

class biom3d.utils.config.AttrDict(*args, **kwargs)[source]

Convenience class that behaves exactly like dict(), but allows accessing, the keys and values using the attribute syntax, i.e., “mydict.key = value”.

Author

Terro Keras (progressive_growing_of_gans).

__init__(*args, **kwargs)[source]

Intialize the Dict.

biom3d.utils.config.adaptive_load_config(config_path: str) AttrDict[source]

Return the configuration dictionary given the path of the configuration file.

The configuration file is in Python or YAML format.

Parameters:

config_path (str) – Path of the configuration file. Should have the ‘.py’ or ‘.yaml’ extension.

Returns:

cfg – Dictionary of the config.

Return type:

biom3d.utils.AttrDict

biom3d.utils.config.compat_old_config(config: MutableMapping[str, Any]) MutableMapping[str, Any][source]

Rename a set of key in a dictionary.

Used to interpret old config files as new one and prevent crashing.

Parameters:

config (dictionary like from str to any) – The config to adapt.

Returns:

config – Config updated.

Return type:

dictionary like from str to any

biom3d.utils.config.config_to_type(cfg: MutableMapping[str, Any], new_type: type[T]) T[source]

Change config type to a new type. This function is recursive and can be use to change the type of nested dictionaries.

Parameters:
  • cfg (dictionary like from str to Any) – The config file we want to convert (possibly nested).

  • new_type (T) – The type we want to convert to, a dictionary like from str to Any (dict, AttrDict,…).

Returns:

cfg – The config converted in given type.

Return type:

T

biom3d.utils.config.load_python_config(config_path: str) AttrDict[source]

Return the configuration dictionary given the path of the configuration file. The configuration file is in Python format.

Adapted from: https://stackoverflow.com/questions/67631/how-can-i-import-a-module-dynamically-given-the-full-path

Parameters:

config_path (str) – Path of the configuration file. Should have the ‘.py’ extension.

Returns:

cfg – Dictionary of the config.

Return type:

biom3d.utils.AttrDict

biom3d.utils.config.load_yaml_config(path: str) AttrDict[source]

Load a yaml stored with the self.save method.

Returns:

cfg – The content of the config file.

Return type:

biom3d.utils.AttrDict

biom3d.utils.config.nested_dict_change_value(dic: MutableMapping[str, Any], key: str, value: Any) MutableMapping[str, Any][source]

Change all value with a given key from a nested dictionary.

Parameters:
  • dic (dictonary like from str to Any) – The dictonary we want to alter.

  • key (str) – The key we want to modify the value.

  • value (any) – The new value.

Returns:

dic – Modified dictionary, this is not a copy of dict.

Return type:

dictonary like from str to Any

biom3d.utils.config.nested_dict_change_value_case_insensitive(dic: MutableMapping[str, Any], key: str, value: Any) MutableMapping[str, Any][source]

Change all values with a given key (case-insensitive) from a nested dictionary.

Parameters:
  • dic (dict[str, Any]) – The dictionary we want to alter.

  • key (str) – The key we want to modify the value.

  • value (Any) – The new value.

Returns:

dic – Modified dictionary, this is not a copy of dict.

Return type:

dict[str, Any]

biom3d.utils.config.nested_dict_pairs_iterator(dic: MutableMapping[str, Any]) Iterator[list[Any]][source]

Iterate over a dictionary by returning a [key,value] iterator.

This function accepts a nested dictionary as argument and iterate over all values of nested dictionaries. Stolen from: https://thispointer.com/python-how-to-iterate-over-nested-dictionary-dict-of-dicts/

Parameters:

dict (dictionary like from str to Any) – Our dictionary we want to iterate over.

biom3d.utils.config.recursive_rename_key(d: MutableMapping[str, Any] | list[MutableMapping[str, Any]], old_key: str, new_key: str) MutableMapping[str, Any][source]

Rename all instance of a given key.

Parameters:
  • d (dictionary like from str to any or a list of said dictionary) – The dictionary to modify.

  • old_key (str) – The key to rename.

  • new_key (str) – New name for the key.

Returns:

d – Renamed dictionary.

Return type:

dictionary like from str to any

biom3d.utils.config.replace_line_multiple(line: str, dic: MutableMapping[str, str]) str[source]

Similar to replace_line_single but with a dictionary of keys and values.

Parameters:
  • line (str) – The input line that follows the format: 'key = value'.

  • dict (dictionary like from str to str) – The dictionary that associate key str and value str

Returns:

line – The modified line.

Return type:

str

biom3d.utils.config.replace_line_single(line: str, key: str, value: str) str[source]

Replace a key, value association in a given line.

Given a line, replace the value if the key is in the line. This function follows the following format: 'key = value'. The line must follow this format and the output will respect this format.

Parameters:
  • line (str) – The input line that follows the format: 'key = value'.

  • key (str) – The key to look for in the line.

  • value (str) – The new value that will replace the previous one.

Raises:

AssertionError – If line doesn’t follow given format.

Returns:

line – The modified line.

Return type:

str

Examples

>>> line = "IMG_PATH = None"
>>> key = "IMG_PATH"
>>> value = "path/img"
>>> replace_line_single(line, key, value)
IMG_PATH = 'path/img'
biom3d.utils.config.save_python_config(config_dir: str, base_config: str | None = None, **kwargs) str[source]

Save the configuration in a config file. If the path to a base configuration is provided, then update this file with the new auto-configured parameters else use biom3d.config_default file.

Parameters:
  • config_dir (str) – Path to the configuration folder. If the folder does not exist, then create it.

  • base_config (str, optional) – Path to an existing configuration file which will be updated with the auto-config values.

  • **kwargs – Keyword arguments of the configuration file.

Raises:

RuntimeError – If base config is None and default config module can’t be loaded.

Returns:

config_path – Path to the new configuration file.

Return type:

str

Examples

>>> config_path = save_config_python(\\
    config_dir="configs/",\\
    base_config="configs/pancreas_unet.py",\\
    IMG_PATH="/pancreas/imagesTs_tiny_out",\\
    MSK_PATH="pancreas/labelsTs_tiny_out",\\
    NUM_CLASSES=2,\\
    BATCH_SIZE=2,\\
    AUG_PATCH_SIZE=[56, 288, 288],\\
    PATCH_SIZE=[40, 224, 224],\\
    NUM_POOLS=[3, 5, 5])
biom3d.utils.config.save_yaml_config(path: str, cfg: MutableMapping[str, Any]) None[source]

Save a configuration in a yaml file.

Parameters:
  • path (str) – Path to file, must contains a yaml extension (.yaml or .yml), e.g.: path=’logs/test.yaml’.

  • cfg (dictionary like from str to any) – The config dictionary to save.

Return type:

None