Skip to content

parameterspace.configspace_utils

Initialize a ParameterSpace from a ConfigSpace JSON dictionary.

parameterspace_from_configspace_dict(configspace_dict)

Create ParameterSpace instance from a ConfigSpace JSON dictionary.

Note, that ParameterSpace does not support regular, non-truncated normal priors and will thus translate an unbounded normal prior to a normal truncated at +/- 4 sigma. Also, constant parameters are represented as categoricals with a single value that are fixed to said value.

Parameters:

Name Type Description Default
configspace_dict dict

The dictionary based on a ConfigSpace JSON representation.

required

Returns:

Type Description
Tuple[parameterspace.parameterspace.ParameterSpace, Dict[str, str]]

A ParameterSpace instance. A mapping between parameter names that were changed for compatibility reasons.

Source code in parameterspace/configspace_utils.py
def parameterspace_from_configspace_dict(
    configspace_dict: dict,
) -> Tuple[ps.ParameterSpace, Dict[str, str]]:
    """Create `ParameterSpace` instance from a `ConfigSpace` JSON dictionary.

    Note, that `ParameterSpace` does not support regular, non-truncated normal priors
    and will thus translate an unbounded normal prior to a normal truncated at +/- 4
    sigma. Also, constant parameters are represented as categoricals with a single value
    that are fixed to said value.

    Args:
        configspace_dict: The dictionary based on a `ConfigSpace` JSON representation.

    Returns:
        A `ParameterSpace` instance.
        A mapping between parameter names that were changed for compatibility reasons.

    Raises:
        NotImplementedError in case a given parameter type or configuration is not
        supported.
    """
    space = ps.ParameterSpace()
    names: Dict[str, str] = {}

    for param_dict in configspace_dict["hyperparameters"]:
        param_name = _escape_parameter_name(param_dict["name"])
        if param_dict["name"] != param_name:
            names[param_name] = param_dict["name"]
            names[param_dict["name"]] = param_name

        condition = _get_condition(configspace_dict["conditions"], param_dict["name"])
        if param_dict["type"] == "uniform_int":
            space._parameters[param_name] = {
                "parameter": ps.IntegerParameter(
                    name=param_name,
                    bounds=(param_dict["lower"], param_dict["upper"]),
                    transformation="log" if param_dict["log"] else None,
                ),
                "condition": condition,
            }

        elif param_dict["type"] == "categorical":
            space._parameters[param_name] = {
                "parameter": ps.CategoricalParameter(
                    name=param_name,
                    values=param_dict["choices"],
                    prior=param_dict.get("weights", None),
                ),
                "condition": condition,
            }

        elif param_dict["type"] in ["constant", "unparametrized"]:
            space._parameters[param_name] = {
                "parameter": ps.CategoricalParameter(
                    name=param_name,
                    values=[param_dict["value"]],
                ),
                "condition": condition,
            }
            space.fix(**{param_name: param_dict["value"]})

        elif param_dict["type"] in ["normal_float", "normal_int"]:
            parameter_class = (
                ps.ContinuousParameter
                if param_dict["type"] == "normal_float"
                else ps.IntegerParameter
            )
            lower_bound, upper_bound, mean, std = _convert_for_normal_parameter(
                log=param_dict["log"],
                lower=param_dict.get("lower", None),
                upper=param_dict.get("upper", None),
                mu=param_dict["mu"],
                sigma=param_dict["sigma"],
            )
            space._parameters[param_name] = {
                "parameter": parameter_class(
                    name=param_name,
                    bounds=(lower_bound, upper_bound),
                    prior=ps.priors.TruncatedNormal(mean=mean, std=std),
                    transformation="log" if param_dict["log"] else None,
                ),
                "condition": condition,
            }

        elif param_dict["type"] == "uniform_float":
            space._parameters[param_name] = {
                "parameter": ps.ContinuousParameter(
                    name=param_name,
                    bounds=(param_dict["lower"], param_dict["upper"]),
                    transformation="log" if param_dict["log"] else None,
                ),
                "condition": condition,
            }

        elif param_dict["type"] == "ordinal":
            space._parameters[param_name] = {
                "parameter": ps.OrdinalParameter(
                    name=param_name,
                    values=param_dict["sequence"],
                ),
                "condition": condition,
            }

        else:
            raise NotImplementedError(f"Unsupported type {param_dict['type']}")

    return space, names