Skip to content

Config ​

A pyhectiqlab.Config object is an abstract object designed to store your configurations with a simple API. It is a dictionary-like object that supports nested configurations.

Create ​

You can create a Config object by passing keyword arguments to the constructor. The value can be any object, including another Config object.

For example,

python
from pyhectiqlab import Config
config = Config(a=1, b=2, c=3, d=Config(e=4, f=5))

Modify attributes ​

You can use the dot notation to modify the attributes of the Config object or use the bracket notation.

python
config.a = 10
config["a"] = 10

To delete an attribute, you can use the pop method.

python
from pyhectiqlab import Config
config = Config(a=1, b=2, c=3)
config.pop("a")

Alias attributes ​

You can use alias attributes to map the value of an attribute to another, by using a dollar-sign notation, for instance $attr_name.

python
from pyhectiqlab import Config

config = Config(name="$id", id=1, info="Some info.")
assert config.name == config.id

Get attributes ​

You can use the dot notation to get the attributes of the Config object or use the bracket notation.

python
print(config.a)
print(config["a"])

For nested configurations, you can use the dot notation to get the attributes of the Config object or use the bracket notation.

python
from pyhectiqlab import Config
dataset_config = Config(batch_size=128, shuffle=True)
config = Config(dataset=dataset_config)

assert config.dataset.batch_size==128
assert config["dataset"]["batch_size"] == 128
assert config["dataset.batch_size"] == 128

The kwargs iterator is also supported.

python
def f(a, b):
    return a + b

config = Config(a=1, b=2)
assert f(**config) == 3

If an attribute does not exist, the Config object returns None.

Validation ​

You can validate the content of a Config object using templates dictionaries, mapping the keys of the Config to the their respective types.

python
from pyhectiqlab import Config
from pydantic import Field, ValidationError
template = {
    "x" : (int, ...), "y": (str, Field(default=1))
}

assert Config(x=1, y="a").validate(template) == True
assert Config(x="b", y="a").validate(template) == False
try:
    Config(x="b", y="a").validate(template, raise_exception=True)
except: ValidationError as e:
    print("This exception is raised:", e)

You can also use pydantic models as templates to validate a config.

Save ​

You can save the Config object to a file using the save method. It saves the configuration to a file in a JSON format. If the directory does not exist, it will be created. The config must be serializable to JSON using orjson.

python
config.save("path/to/config.json")

Load ​

From a file ​

You can load a configuration from a file using the load method. The file must be in a JSON format and deserializable using orjson.

python
config = Config.load("path/to/config.json")

From a dictionary ​

You can also create a Config object from a dictionary using the from_dict method.

python
config = Config.from_dict({"a": 1, "b": 2, "c": 3})

If a value is a dictionary, it will be converted to a Config object.

python
config = Config.from_dict({"a": 1, "b": 2, "c": {"d": 4, "e": 5}})
config.c.d == 4

Retrieve a config from a run ​

You can retrieve a config from a run using the retrieve method. If the run does not have a config, an empty config is returned.

python
from pyhectiqlab.functional import hl
config = hl.retrieve_config(rank=1)
python
from pyhectiqlab import Config
config = Config.retrieve(rank=1)

Additionally, you can retrieve a config from a run ID using the retrieve_by_id method. If the run does not have a config, an empty config is returned.