BaseConfig#

class caf.base.segmentation.BaseConfig#

Bases: BaseModel

Base class for storing model parameters.

Contains functionality for reading / writing parameters to config files in the YAML format.

See also

pydantic.BaseModel

handles converting data to Python types.

pydantic.field_validator

custom validation for attributes.

pydantic.model_validator

custom validation for class.

Examples

Example of creating a config class and initialising it with values, values will be validated and converted to correct type on initialisation.

>>> from pathlib import Path
>>> from caf.toolkit import BaseConfig
>>> class ExampleParameters(BaseConfig):
...     import_folder: Path
...     name: str
...     some_option: bool = True
>>> parameters = ExampleParameters(
...     import_folder="Test Folder",
...     name="Test",
...     some_option=False,
... )

Example of instance of class after initialisation, the path differs depending on operating system.

>>> parameters
ExampleParameters(
    import_folder=WindowsPath('Test Folder'),
    name='Test',
    some_option=False,
)

Config class can be converted to YAML or saved with BaseConfig.save_yaml().

>>> print(parameters.to_yaml())
import_folder: Test Folder
name: Test
some_option: no

Config class data can be loaded from a YAML config file using BaseConfig.load_yaml().

>>> yaml_text = '''
... import_folder: Test Folder
... name: Test
... some_option: no
... '''
>>> loaded_parameters = ExampleParameters.from_yaml(yaml_text)
>>> loaded_parameters == parameters
True

Attributes

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

Methods

from_yaml(text)

Parse class attributes from YAML text.

load_yaml(path)

Read YAML file and load the data using from_yaml.

save_yaml(path[, datetime_comment, ...])

Write data from self to a YAML file.

to_yaml()

Convert attributes from self to YAML string.

write_example(path_, /[, comment_])

Write examples to a config file.

Attributes Documentation

model_config = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].