API documentation for the config
module
The config
module is used to read in the various
configuration files, validate their contents, and then configure a ready to run instance
of the virtual ecosystem model. The basic details of how this system is used can be
found here.
The validation of configuration documents is done using JSONSchema documents associated
with the different model components. See the schema
module for details.
Classes:
|
Configuration loading and validation. |
Functions:
|
Recursively merge two dictionaries detecting duplicated key definitions. |
- class virtual_ecosystem.core.config.Config(cfg_paths: str | Path | Sequence[str | Path] = [], cfg_strings: str | list[str] = [], override_params: dict[str, Any] = {}, auto: bool = True)
Configuration loading and validation.
The
Config
class is used to generate a validated configuration for a Virtual Ecosystem simulation. Thecfg_paths
attribute is used to provide paths to TOML configuration files or directories containing sets of files to be used. The provided paths are then run through the follow steps to resolve and load the configuration data.The
collect_config_paths()
method is used to collect the actual TOML files to be used to build the configuration from the provided paths.The
load_config_toml()
method is then used to load the parsed contents of each resolved file into thetoml_contents
attribute.
Alternatively, configuration data may be passed as a string or list of strings using the
cfg_strings
argument. These strings must contain TOML formatted data, which is parsed and added to thetoml_contents
attribute.Whichever approach is used, the next two steps are then applied to the provided TOML data:
The
build_config()
method is used to merge the loaded configuration across files and check that configuration settings are uniquely defined.The
validate_config()
method validates the compiled configuration against the appropriate configuration schema for thecore
module and any models included in the configuration. This validation will also fill in any missing configuration settings with defined defaults.
By default, creating a
Config
instance automatically runs these steps across the providedcfg_paths
, but theauto
argument can be used to turn off automatic validation.The
export_config()
method can be used to export the compiled and validated configuration as a single TOML file.If the core.data_output_options.save_merged_config option is set to true a merged config file will be automatically generated, unless
auto
is set to false.- Parameters:
cfg_paths – A string, Path or list of strings or Paths giving configuration file or directory paths.
cfg_strings – A string or list of strings containing TOML formatted configuration data.
override_params – Extra parameters provided by the user.
auto – A boolean flag setting whether the configuration data is automatically loaded and validated
Methods:
Build a combined configuration from the loaded files.
Build a schema to validate the model configuration.
Collect TOML config files from provided paths.
export_config
(outfile)Exports a validated and merged configuration as a single file.
Load the contents of resolved configuration files.
Load the contents of a config provided as a string.
override_config
(override_params)Override any parameters desired.
Resolve the locations of configured file paths.
Validate the model configuration.
Attributes:
The configuration file paths, normalised from the cfg_paths argument.
A list of strings containing TOML content, provided by the
cfg_strings
argument.Configuration errors, as a list of tuples of key path and error details.
A boolean flag indicating whether paths or strings were used to create the instance.
A list of configuration keys duplicated across configuration files.
The merged schema for the core and modules present in the configuration.
A dictionary of the model classes specified in the configuration, keyed by model name.
A dictionary of the parsed TOML contents of config files or strings, keyed by file path or string index.
A list of TOML file paths resolved from the initial config paths.
A boolean flag indicating successful validation.
- build_config() None
Build a combined configuration from the loaded files.
This method does pairwise merging of the loaded config data from individual files to build a single configuration dictionary, looking for duplicate definitions in the config tree.
- Raises:
ConfigurationError – if duplicate config definitions occur across files.
- build_schema() None
Build a schema to validate the model configuration.
This method identifies the modules to be configured from the top-level configuration keys, setting the requested modules to be used in the configured simulation. The schemas for the requested modules are then loaded and combined using the
merge_schemas()
function to generate a single validation schema for model configuration.
- cfg_strings: list[str]
A list of strings containing TOML content, provided by the
cfg_strings
argument.
- collect_config_paths() None
Collect TOML config files from provided paths.
The
Config
class is initialised with a list of paths to either individual TOML config files or directories containing possibly multiple config files. This method examines that list to collect all the individual TOML config files in the provided locations and then populates thetoml_files
attribute.- Raises:
ConfigurationError – this is raised if any of the paths: do not exist, are directories that do not contain TOML files, are not TOML files or if the resolved files contain duplicate entries.
- config_errors: list[tuple[str, Any]]
Configuration errors, as a list of tuples of key path and error details.
- export_config(outfile: Path) None
Exports a validated and merged configuration as a single file.
This method will only export a configuration file if the
Config
instance has been successfully validated.- Parameters:
outfile – An output path for the TOML configuration file.
- from_cfg_strings: bool
A boolean flag indicating whether paths or strings were used to create the instance.
- load_config_toml() None
Load the contents of resolved configuration files.
This method populates the
toml_contents
dictionary with the contents of the configuration files set intoml_files
. That attribute is normally populated by providing a set of paths toConfig
and running thecollect_config_paths()
method, but it can also be set directly.- Raises:
ConfigurationError – Invalid TOML content in config files.
- load_config_toml_string() None
Load the contents of a config provided as a string.
This method populates the
toml_contents
dictionary with the contents of a provided TOML formatted string.- Raises:
ConfigurationError – Invalid TOML string.
- model_classes: dict[str, Any]
A dictionary of the model classes specified in the configuration, keyed by model name.
- override_config(override_params: dict[str, Any]) None
Override any parameters desired.
- Parameters:
override_params – Extra parameter settings
- resolve_config_file_paths() None
Resolve the locations of configured file paths.
Configuration files can contain paths to other resources, such as the paths to files containing input data variables. These paths can be absolute, but may also be relative to the location of the configuration file itself. This method is used to resolve the location of files to the common root of the provided set of configuration files, typically the path where a simulation is started.
- toml_contents: dict[str | Path, dict]
A dictionary of the parsed TOML contents of config files or strings, keyed by file path or string index.
- validate_config() None
Validate the model configuration.
This method first validates the ‘core’ configuration and applies defaults, which ensures that the modules to be used in the configured model are set. The schemas for the requested modules are then applied to each configuration section to validate the contents and apply any default values.
- Raises:
ConfigurationError – if the loaded configuration is not compatible with the configuration schemas.
- virtual_ecosystem.core.config.config_merge(dest: dict, source: dict, conflicts: tuple = (), path: str = '') tuple[dict, tuple]
Recursively merge two dictionaries detecting duplicated key definitions.
This function returns a copy of the input
dest
dictionary that has been extended recursively with the entries from the inputsource
dictionary. The two input dictionaries must not share any key paths and when duplicated key paths are found, the value from the source dictionary is used and the function extends the returnedconflicts
tuple with the duplicated key path.- Parameters:
dest – A dictionary to extend
source – A dictionary of key value pairs to extend
dest
conflicts – A tuple of duplicated key paths between the two dictionaries
path – A string giving the current key path.
- Returns:
A copy of dest, extended recursively with values from source, and a tuple of duplicate key paths.