def convert_study_config_to_hps()

in src/python/tensorflow_cloud/tuner/utils.py [0:0]


def convert_study_config_to_hps(
    study_config: Dict[Text, Any]) -> hp_module.HyperParameters:
    """Converts Vizier study_config to HyperParameters."""
    if not study_config.get("parameters"):
        raise ValueError("Parameters are not found in the study_config: ",
                         study_config)
    if not isinstance(study_config["parameters"], list):
        raise ValueError(
            "Parameters should be a list of parameter with at least 1 "
            "parameter, found ", study_config["parameters"],
        )

    hps = hp_module.HyperParameters()
    for param in study_config["parameters"]:
        _is_parameter_valid(param)
        name = param["parameter"]
        if param["type"] == _DISCRETE:
            values = param["discrete_value_spec"]["values"]
            is_numeric = True
            for v in values:
                if not isinstance(v, (int, float)):
                    is_numeric = False
            if (
                is_numeric and len(values) > 2 and
                np.all(np.diff(values, 2) == 0)
            ):
                # If the numeric sequence is an arithmetic sequence, use
                # Int/Float with step
                is_int = True
                for v in values:
                    if not isinstance(v, int):
                        is_int = False
                hps_type = hps.Int if is_int else hps.Float

                if (
                    param.get("scale_type")
                    and param["scale_type"] != _SCALE_TYPE_UNSPECIFIED
                ):
                    hps_type(
                        name,
                        min_value=values[0],
                        max_value=values[-1],
                        step=values[1] - values[0],
                        sampling=_format_sampling(param["scale_type"]),
                    )
                else:
                    hps_type(
                        name,
                        min_value=values[0],
                        max_value=values[-1],
                        step=values[1] - values[0],
                    )
            else:
                hps.Choice(name, values)
        elif param["type"] == _CATEGORICAL:
            hps.Choice(name, param["categorical_value_spec"]["values"])
        elif param["type"] == _DOUBLE:
            if (
                param.get("scale_type")
                and param["scale_type"] != _SCALE_TYPE_UNSPECIFIED
            ):
                hps.Float(
                    name,
                    min_value=param["double_value_spec"]["min_value"],
                    max_value=param["double_value_spec"]["max_value"],
                    sampling=_format_sampling(param["scale_type"]),
                )
            else:
                hps.Float(
                    name,
                    min_value=param["double_value_spec"]["min_value"],
                    max_value=param["double_value_spec"]["max_value"],
                )
        elif param["type"] == _INTEGER:
            if (
                param.get("scale_type")
                and param["scale_type"] != _SCALE_TYPE_UNSPECIFIED
            ):
                hps.Int(
                    name,
                    min_value=param["integer_value_spec"]["min_value"],
                    max_value=param["integer_value_spec"]["max_value"],
                    sampling=_format_sampling(param["scale_type"]),
                )
            else:
                hps.Int(
                    name,
                    min_value=param["integer_value_spec"]["min_value"],
                    max_value=param["integer_value_spec"]["max_value"],
                )
        else:
            raise ValueError(
                "Unknown parameter type: {}.".format(param["type"]))
    return hps