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,
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.
config.a = 10
config["a"] = 10To delete an attribute, you can use the pop method.
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.
from pyhectiqlab import Config
config = Config(name="$id", id=1, info="Some info.")
assert config.name == config.idGet attributes ​
You can use the dot notation to get the attributes of the Config object or use the bracket notation.
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.
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"] == 128The kwargs iterator is also supported.
def f(a, b):
return a + b
config = Config(a=1, b=2)
assert f(**config) == 3If 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.
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.
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.
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.
config = Config.from_dict({"a": 1, "b": 2, "c": 3})If a value is a dictionary, it will be converted to a Config object.
config = Config.from_dict({"a": 1, "b": 2, "c": {"d": 4, "e": 5}})
config.c.d == 4Retrieve 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.
from pyhectiqlab.functional import hl
config = hl.retrieve_config(rank=1)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.