def test_Float():
    # Test with step arg
    linear = hp_module.Float("linear",
                             min_value=0.5,
                             max_value=9.5,
                             step=0.1,
                             default=9.0)
    linear = hp_module.Float.from_config(linear.get_config())
    assert linear.default == 9.0
    assert 0.5 <= linear.random_sample() <= 9.5
    assert isinstance(linear.random_sample(), float)
    assert linear.random_sample(123) == linear.random_sample(123)

    # Test without step arg
    linear = hp_module.Float("linear",
                             min_value=0.5,
                             max_value=6.5,
                             default=2.0)
    linear = hp_module.Float.from_config(linear.get_config())
    assert linear.default == 2.0
    assert 0.5 <= linear.random_sample() < 6.5
    assert isinstance(linear.random_sample(), float)
    assert linear.random_sample(123) == linear.random_sample(123)

    # No default
    linear = hp_module.Float("linear", min_value=0.5, max_value=9.5, step=0.1)
    assert linear.default == 0.5
def test_sampling_zero_length_intervals():
    f = hp_module.Float("f", 2, 2)
    rand_sample = f.random_sample()
    assert rand_sample == 2

    val = 2
    prob = hp_module.value_to_cumulative_prob(val, f)
    assert prob == 1
def test_float_proto():
    hp = hp_module.Float("a", -10, 10, sampling="linear", default=3)
    proto = hp.to_proto()
    assert proto.name == "a"
    assert proto.min_value == -10.0
    assert proto.max_value == 10.0
    assert proto.sampling == keras_tuner_pb2.Sampling.LINEAR
    assert proto.default == 3.0
    # Zero is the default, gets converted to `None` in `from_proto`.
    assert proto.step == 0.0

    new_hp = hp_module.Float.from_proto(proto)
    assert new_hp.get_config() == hp.get_config()
def test_reverse_log_sampling_random_state():
    f = hp_module.Float("f", 1e-3, 1e3, sampling="reverse_log")
    rand_sample = f.random_sample()
    assert rand_sample >= f.min_value
    assert rand_sample <= f.max_value

    val = 1e-3
    prob = hp_module.value_to_cumulative_prob(val, f)
    assert prob == 0
    new_val = hp_module.cumulative_prob_to_value(prob, f)
    assert np.isclose(val, new_val)

    val = 1
    prob = hp_module.value_to_cumulative_prob(val, f)
    assert prob > 0 and prob < 1
    new_val = hp_module.cumulative_prob_to_value(prob, f)
    assert np.isclose(val, new_val)
def test_sampling_arg():
    f = hp_module.Float("f", 1e-20, 1e10, sampling="log")
    f = hp_module.Float.from_config(f.get_config())
    assert f.sampling == "log"

    i = hp_module.Int("i", 0, 10, sampling="linear")
    i = hp_module.Int.from_config(i.get_config())
    assert i.sampling == "linear"

    with pytest.raises(ValueError, match="`sampling` must be one of"):
        hp_module.Int("j", 0, 10, sampling="invalid")

    with pytest.raises(
            ValueError,
            match="`sampling` `min_value` 1 is greater than the `max_value` 0",
    ):
        hp_module.Int("k", 1, 0, sampling="linear")

    with pytest.raises(
            ValueError,
            match="`sampling` `min_value` 1 is greater than the `max_value` 0",
    ):
        hp_module.Int("k", 1, 0, sampling="linear")