Configuration#
ConfigManager is the central configuration API. It merges YAML files, CLI overrides, and preset configs into a single resolved config tree. Use get() to read values, get_module() to instantiate swappable components, and get_collection() to build named collections of components.
For a conceptual overview of the config system, see Configuring Simulations in the user guide and Configuration System in the extending guide.
Config manager#
- class jaqmc.utils.config.ConfigManager(config, dotlist=None)[source]#
Manages configuration by dynamically resolving schema from usage.
This class holds the user-provided configuration (from YAML/dicts) and command-line overrides (dotlist). Unlike traditional config systems that validate against a pre-defined schema, ConfigManager defines the schema dynamically as values are accessed via get or get_module.
When a configuration value is requested, the manager uses the provided default value to infer the expected type/structure (schema). It then reads the corresponding value from the stored user config and dotlist, merges it with the default, and returns the result.
When
finalize()is called, any source path whose prefix was never visited is reported as unused, helping detect typos or obsolete entries.- Parameters:
config (
dict[str,Any] |Sequence[dict[str,Any]]) – A single configuration dictionary or a sequence of dictionaries to be merged. These are typically loaded from YAML files.dotlist (
list[str] |None, default:None) – An optional list of command-line overrides in the form of “key=value” or “key.subkey=value”.
- finalize(raise_on_unused=True, verbose=False, compare_yaml=None)[source]#
Finalize the configuration and log the results.
This method outputs the resolved configuration to the log and checks for any unused configuration keys.
- Parameters:
raise_on_unused (
bool, default:True) – If True, raises SystemExit if there are any unused configuration keys in the input.verbose (
bool, default:False) – If True, includes docstrings and source information in the logged YAML output.compare_yaml (
str|None, default:None) – YAML from previous from to log the difference.
- Raises:
SystemExit – If raise_on_unused is True and unused keys are found.
- get(name, default)[source]#
- Overloads:
self, name (str), default (type[ValueT]) → ValueT
self, name (str), default (ValueT) → ValueT
Retrieve a configuration value with type safety.
The supported types are:
- Parameters:
- Returns:
The configuration value, in the same type of default.
- Type Parameters:
ValueT – Type inferred from
defaultand preserved in the return value.- Raises:
NotImplementedError – If the type of default is not supported.
- get_collection(name, defaults=None, context=None)[source]#
Instantiate a collection of modules from configuration.
- Parameters:
name (
str) – The configuration section name (e.g. “writers”).defaults (
dict[str,str|dict] |None, default:None) – A dictionary of {key: default_module_path} for standard items. Users can disable these by setting them to None in config.context (
Mapping[str,Any] |None, default:None) – Runtime context dictionary for auto-wiring dependencies.
- Returns:
instantiated_object}.
- Return type:
- get_module(name, default_module='')[source]#
- Overloads:
self, name (str), default_module (ModuleT) → ModuleT
self, name (str), default_module (str) → Any
Instantiate a class or function specified in the configuration.
- Parameters:
- Returns:
The initialized object or result of the function call.
- Type Parameters:
ModuleT – Module/class/callable type preserved when not using string paths.
- scoped(prefix)[source]#
Return a scoped view that prepends prefix to all lookups.
- Parameters:
prefix (
str) – Dot-separated prefix (e.g."train").- Return type:
- Returns:
A
ScopedConfigManagerforwarding to this instance.
- class jaqmc.utils.config.ScopedConfigManager(cfg, prefix)[source]#
A thin wrapper that prepends a prefix to all config lookups.
Created by
ConfigManager.scoped(prefix). Allget,get_module, andget_collectioncalls are forwarded withprefix + "." + name.- Parameters:
cfg (
ConfigManagerLike) – The underlying ConfigManager.prefix (
str) – Dot-separated prefix prepended to all keys.
- get(name, default)[source]#
Retrieve a configuration value with type safety.
The supported types are:
- Parameters:
- Return type:
TypeVar(ValueT)- Returns:
The configuration value, in the same type of default.
- Type Parameters:
ValueT – Type inferred from
defaultand preserved in the return value.- Raises:
NotImplementedError – If the type of default is not supported.
- get_collection(name, defaults=None, context=None)[source]#
Instantiate a collection of modules from configuration.
- Parameters:
name (
str) – The configuration section name (e.g. “writers”).defaults (
dict[str,str|dict] |None, default:None) – A dictionary of {key: default_module_path} for standard items. Users can disable these by setting them to None in config.context (
Mapping[str,Any] |None, default:None) – Runtime context dictionary for auto-wiring dependencies.
- Returns:
instantiated_object}.
- Return type:
Component wiring#
Components use runtime_dep() to declare dependencies that are injected at runtime rather than set in config. The wire() function injects those dependencies into a component instance.
- jaqmc.utils.config.configurable_dataclass(cls=None, *, frozen=False, kw_only=True)[source]#
Decorator for dataclasses that participate in the config system.
Applies
@dataclass(if needed) and@serde(type_check=coerce)so that the class can be used withConfigManager. Coerce mode auto-converts compatible types (e.g.int→float) during deserialization.- Returns:
The decorated class.
Example:
@configurable_dataclass class MyConfig: learning_rate: float = 0.01 spins: tuple[int, int] = (1, 0)
- jaqmc.utils.wiring.runtime_dep(*, default=<object object>)[source]#
Declare a runtime dependency field on a dataclass.
Required deps (no default) use the LateInit descriptor, which raises a clear
AttributeErrorif accessed before being set. Optional deps use a regular field with the given default.Both kinds are invisible to serde and marked for
wire()discovery viametadata["runtime"].- Parameters:
default (
Any, default:<object object at 0x7f83ab04ce20>) – Default value for optional runtime deps. Omit for required deps.- Return type:
- Returns:
A dataclass field descriptor.
Examples
>>> from dataclasses import dataclass >>> @dataclass ... class Estimator: ... mode: str = "fast" ... f_eval: object = runtime_dep() >>> est = Estimator() >>> est.f_eval # raises before wiring Traceback (most recent call last): ... AttributeError: Estimator.f_eval is a runtime dependency ...
- jaqmc.utils.wiring.wire(component, **kwargs)[source]#
Wire runtime dependencies into a dataclass component.
Sets
runtime_depfields from context/kwargs and validates that all required deps are satisfied.- Parameters:
- Return type:
TypeVar(ComponentT)- Returns:
The wired component (for chaining).
- Type Parameters:
ComponentT – Dataclass component type preserved as the return type.
- Raises:
TypeError – If component is not a dataclass.
ValueError – If required runtime deps are missing after wiring.
Examples
>>> from dataclasses import dataclass >>> @dataclass ... class Estimator: ... f_eval: object = runtime_dep() >>> est = Estimator() >>> wire(est, f_eval=lambda x: x**2) Estimator(...) >>> est.f_eval(3) 9
- jaqmc.utils.wiring.check_wired(component, *, label='')[source]#
Verify all required runtime deps on a dataclass have been set.
Use this to catch missing wiring early — at configure time rather than mid-execution when a
_LateInitdescriptor would raise.- Parameters:
- Raises:
ValueError – If any required runtime deps are unset.
- Return type:
Module resolution#
resolve_object() resolves "module:name" strings into Python objects — the mechanism behind swappable components in YAML config.
- jaqmc.utils.module_resolver.resolve_object(name, package=None)[source]#
Resolve object from
module:namenotation with default export support.The default object (without explicitly specified object name) is the primary object via
__all__[0]in the target module.- Parameters:
name (
str) –The
module:namenotation. Supported forms:"module:name": Explicitly resolvemodule.name(e.g.,"optax:adam"resolves tooptax.adam)."module": Resolve default object frommodule.__all__[0](e.g.,"jaqmc.optimizer.kfac"resolves tokfac).
package (
str|None, default:None) – Base package for relative imports. When specified, tries relative import first, then falls back to absolute import. IfNone, only absolute imports are attempted.
- Return type:
- Returns:
The resolved callable, class, or other object.
- Raises:
ValueError – If the object cannot be resolved, including when using shorthand notation (“module”) but the module has no __all__ attribute defined.
Examples
Explicit
module:nameform:>>> resolve_object("optax:adam") <function adam at ...>
Relative resolution via
package:>>> resolve_object("schedule:Standard", package="jaqmc.optimizer") <class 'jaqmc.optimizer.schedule.Standard'>
YAML formatting#
- jaqmc.utils.yaml_format.dump_yaml(data, *, sort_keys=False)[source]#
Dump data to YAML string with compact numeric lists.
This function produces YAML output where lists containing only numbers are rendered in flow style (e.g., [1, 2, 3]) for better readability.
- Parameters:
- Return type:
- Returns:
A YAML string representation of the data.
Examples
Numeric lists render in compact flow style:
>>> print(dump_yaml({"lr": 0.05, "atoms": [1, 2, 3]})) lr: 0.05 atoms: [1, 2, 3]
Matrices (lists of numeric lists) render each row in flow style:
>>> print(dump_yaml({"matrix": [[1.0, 0.0], [0.0, 1.0]]})) matrix: - [1.0, 0.0] - [0.0, 1.0]
- jaqmc.utils.yaml_format.annotate_yaml_with_sources(yaml_str, source_map, verbose=False)[source]#
Annotate YAML string with source location comments.
This function post-processes a YAML string to inject comments indicating where specific configuration sections (dataclasses/callables) are defined. It uses indentation to reconstruct the nested key structure (e.g., a.b.c) and looks up source locations in the provided source map.
- Parameters:
yaml_str (
str) – The original YAML string to annotate.source_map (
dict[str,tuple[str,int,str|None]]) – A dictionary mapping configuration paths to their source information. Key is the path (e.g., “system.molecule”), value is a tuple of (source_file, line_number, docstring).verbose (
bool, default:False) – If True, also includes docstrings of the configuration objects in the comments.
- Return type:
- Returns:
Annotated YAML string with definition comments.