Skip to content

omegaconf_ops

omegaconf_ops

OmegaConf utility operations.

resolve_dict_keys

resolve_dict_keys(d)

Resolve any OmegaConf interpolations in dictionary keys recursively.

OmegaConf doesn't resolve interpolations in dict keys by default. This function resolves keys like '${cameras:AGENTVIEW}' to 'agentview_rgb'.

Parameters:

Name Type Description Default
d Mapping[Any, Any]

Dictionary with potentially unresolved interpolation keys.

required

Returns:

Type Description
dict[Any, Any]

New dictionary with resolved keys.

Source code in src/versatil/common/omegaconf_ops.py
def resolve_dict_keys(d: Mapping[Any, Any]) -> dict[Any, Any]:
    """Resolve any OmegaConf interpolations in dictionary keys recursively.

    OmegaConf doesn't resolve interpolations in dict keys by default.
    This function resolves keys like '${cameras:AGENTVIEW}' to 'agentview_rgb'.

    Args:
        d: Dictionary with potentially unresolved interpolation keys.

    Returns:
        New dictionary with resolved keys.
    """
    resolved = {}
    for key, value in d.items():
        if isinstance(key, str) and key.startswith("${") and key.endswith("}"):
            temp_cfg = OmegaConf.create({"_key": key})
            OmegaConf.resolve(temp_cfg)
            resolved_key = OmegaConf.select(temp_cfg, "_key")
        else:
            resolved_key = key
        resolved_value = (
            resolve_dict_keys(value) if isinstance(value, Mapping) else value
        )
        resolved[resolved_key] = resolved_value
    return resolved

make_config_yaml_safe

make_config_yaml_safe(value)

Convert resolved config values unsupported by OmegaConf into YAML-safe values.

Source code in src/versatil/common/omegaconf_ops.py
def make_config_yaml_safe(value: Any) -> Any:
    """Convert resolved config values unsupported by OmegaConf into YAML-safe values."""
    if isinstance(value, torch.dtype):
        # Serialize as the resolver interpolation so reloading the saved
        # config reconstructs the real dtype
        return "${torch_dtype:" + str(value).removeprefix("torch.") + "}"
    if isinstance(value, dict):
        return {
            make_config_yaml_safe(key): make_config_yaml_safe(nested_value)
            for key, nested_value in value.items()
        }
    if isinstance(value, list):
        return [make_config_yaml_safe(item) for item in value]
    if isinstance(value, tuple):
        return tuple(make_config_yaml_safe(item) for item in value)
    return value