zea.config

Config module for managing configuration settings.

This module provides the Config class for managing configuration settings, with support for loading from YAML files, HuggingFace Hub, and dot notation access.

Features

  • Dot notation access to dictionary keys.

  • Recursive conversion of nested dictionaries/lists to Config objects.

  • Attribute access logging and suggestion of similar attribute names.

  • Freezing/unfreezing to prevent/allow new attributes.

  • Serialization to YAML/JSON.

  • Integration with Hugging Face Hub.

Example Usage

from zea import Config

# Load from YAML
config = Config.from_yaml("config.yaml")
# Load from HuggingFace Hub
config = Config.from_hf("zea/diffusion-echonet-dynamic", "train_config.yaml")

# Access attributes with dot notation
print(config.model.name)

# Update recursively
config.update_recursive({"model": {"name": "new_model"}})

# Save to YAML
config.save_to_yaml("new_config.yaml")

Functions

check_config(config[, verbose])

Check a config given dictionary

Classes

Config([dictionary, __parent__])

Config class.

class zea.config.Config(dictionary=None, __parent__=None, **kwargs)[source]

Bases: dict

Config class.

This Config class extends a normal dictionary with dot notation access.

Features:
  • Config.from_yaml method to load a config from a yaml file.

  • Config.from_hf method to load a config from a huggingface hub.

  • save_to_yaml method to save the config to a yaml file.

  • copy method to create a deep copy of the config.

  • Normal dictionary methods such as keys, values, items, pop, update, get.

  • Propose similar attribute names if a non-existing attribute is accessed.

  • Freeze the config object to prevent new attributes from being added.

  • Load config object from yaml file.

  • Logs all accessed attributes such that you can check if all attributes have been accessed.

We took inspiration from the following sources:

But this implementation is superior :)

as_dict(func_on_leaves=None)[source]

Convert the config to a normal dictionary (recursively).

Parameters:

func_on_leaves (callable, optional) – Function to apply to each leaf node. The function should take three arguments: the config object, the key, and the value. You can change the key and value inside the function. Defaults to None.

clear()[source]

Clear the config object.

copy()[source]

Deep copy the config object.

This is useful when you want to modify the config object without changing the original. Does not preserve the access history or frozen state!

freeze()[source]

Freeze config object.

This means that no new attributes can be added. Only existing attributes can be modified.

classmethod from_hf(repo_id, path, **kwargs)[source]

Load config object from huggingface hub.

Example:

config = Config.from_hf("zeahub/configs", "config_camus.yaml", repo_type="dataset")
Parameters:
  • repo_id (str) – huggingface hub repo id. For example: “zeahub/configs”

  • path (str) – path to the config file in the repo. For example: “train_config.yaml”

  • **kwargs – additional arguments to pass to the hf_hub_download function. For example, use repo_type=”dataset” to download from a dataset repo, or revision=”main” to download from a specific branch.

Returns:

config object.

Return type:

Config

classmethod from_path(path, **kwargs)[source]

Load config object from a file path.

Parameters:

path (str or Path) – The path to the config file. Can be a string or a Path object. Additionally can be a string with the prefix ‘hf://’, in which case it will be resolved to a huggingface path.

Returns:

config object.

Return type:

Config

classmethod from_yaml(path, **kwargs)[source]

Load config object from yaml file.

fromkeys(keys, value=None)[source]

Returns a config with the specified keys and value

get(key, default=None)[source]

Returns the value of the specified key

items()[source]

Returns a list containing a tuple for each key value pair

keys()[source]

Returns a list containing the config’s keys

pop(key, default=None)[source]

Removes the element with the specified key

popitem()[source]

Removes the last inserted key-value pair

save_to_yaml(path)[source]

Save config contents to yaml

serialize()[source]

Return a dict of this config object with all Path objects converted to strings.

setdefault(key, default=None)[source]

Returns the value of the specified key. If the key does not exist: insert the key, with the specified value

to_json()[source]

Return the config as a json string.

to_tensor(keep_as_is=None)[source]

Convert the attributes in the object to keras tensors

unfreeze()[source]

Unfreeze config object. This means that new attributes can be added.

update(dictionary=None, **kwargs)[source]

Updates the config with the specified key-value pairs

update_recursive(dictionary=None, **kwargs)[source]

Recursively update the config with the provided dictionary and keyword arguments.

If a key corresponds to another Config object, the update_recursive method is called recursively on that object. This makes it possible to update nested Config objects without replacing them.

If a value is a list and the corresponding config value is also a list, each element is updated recursively if it is a Config, otherwise replaced.

Example:

config = Config({"a": 1, "b": {"c": 2, "d": 3}})
config.update_recursive({"a": 4, "b": {"c": 5}})
print(config)
# <Config {'a': 4, 'b': {'c': 5, 'd': 3}}>
# Notice how "d" is kept and only "c" is updated.
Parameters:
  • dictionary (dict, optional) – Dictionary to update from.

  • **kwargs – Additional key-value pairs to update.

values()[source]

Returns a list of all the values in the config

zea.config.check_config(config, verbose=False)[source]

Check a config given dictionary