config¶
Declarative (YAML / dict) configuration of a calibration problem.
The schema keys on the same dotted "<container>.<field>" target paths used
throughout the calibration package, so a calibration setup can live next to a
crop preset as data:
1 2 3 4 5 6 7 8 | |
The loader returns (specs, groups) ready to pass to
~torchcrop.calibration.manager.CalibrationManager.
load_calibration_config(source)
¶
Load a calibration problem from a YAML file, path, or dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source |
str | Path | dict[str, Any] |
Path to a YAML file, or an already-parsed config dict. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[ParameterSpec], list[ConstraintGroup]] |
|
Source code in torchcrop/calibration/config.py
def load_calibration_config(
source: str | Path | dict[str, Any],
) -> tuple[list[ParameterSpec], list[ConstraintGroup]]:
"""Load a calibration problem from a YAML file, path, or dict.
Args:
source: Path to a YAML file, or an already-parsed config dict.
Returns:
``(specs, groups)`` ready for
`~torchcrop.calibration.manager.CalibrationManager`.
"""
if isinstance(source, dict):
return specs_from_config(source)
yaml = _import_yaml()
with open(source) as fh:
return specs_from_config(yaml.safe_load(fh) or {})
specs_from_config(config)
¶
Build specs and groups from an already-parsed config mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config |
dict[str, Any] |
Mapping with optional |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[ParameterSpec], list[ConstraintGroup]] |
|
Exceptions:
| Type | Description |
|---|---|
ValueError |
If the mapping is malformed. |
Source code in torchcrop/calibration/config.py
def specs_from_config(
config: dict[str, Any],
) -> tuple[list[ParameterSpec], list[ConstraintGroup]]:
"""Build specs and groups from an already-parsed config mapping.
Args:
config: Mapping with optional ``parameters`` and ``constraints`` keys
(see module docstring for the schema).
Returns:
``(specs, groups)``.
Raises:
ValueError: If the mapping is malformed.
"""
specs: list[ParameterSpec] = []
for name, opts in (config.get("parameters") or {}).items():
opts = opts or {}
cats = opts.get("categories")
specs.append(
ParameterSpec(
name=name,
bounds=_as_bounds(opts.get("bounds")),
kind=opts.get("kind", "continuous"),
transform=opts.get("transform"),
init=opts.get("init"),
categories=tuple(cats) if cats is not None else None,
)
)
groups: list[ConstraintGroup] = []
for entry in config.get("constraints") or []:
groups.append(
ConstraintGroup(
members=tuple(entry["members"]),
order=entry.get("order", "ascending"),
)
)
return specs, groups