Exemple #1
0
def test_partly_categorical_space():
    dims = Space([Categorical(["a", "b", "c"]), Categorical(["A", "B", "C"])])
    assert dims.is_partly_categorical
    dims = Space([Categorical(["a", "b", "c"]), Integer(1, 2)])
    assert dims.is_partly_categorical
    assert not dims.is_categorical
    dims = Space([Integer(1, 2), Integer(1, 2)])
    assert not dims.is_partly_categorical
Exemple #2
0
def test_space_from_space():
    # can you pass a Space instance to the Space constructor?
    space = Space([(0.0, 1.0), (-5, 5), ("a", "b", "c"),
                   (1.0, 5.0, "log-uniform"), ("e", "f")])

    space2 = Space(space)

    assert_equal(space, space2)
Exemple #3
0
def test_normalize_types():
    # can you pass a Space instance to the Space constructor?
    space = Space([(0.0, 1.0), Integer(-5, 5, dtype=int), (True, False)])
    space.set_transformer("normalize")
    X = [[0.0, -5, False]]
    Xt = np.zeros((1, 3))
    assert_array_equal(space.transform(X), Xt)
    assert_array_equal(space.inverse_transform(Xt), X)
    assert_array_equal(space.inverse_transform(space.transform(X)), X)
    assert isinstance(space.inverse_transform(Xt)[0][0], float)
    assert isinstance(space.inverse_transform(Xt)[0][1], int)
    assert isinstance(space.inverse_transform(Xt)[0][2], (np.bool_, bool))
Exemple #4
0
def test_minimizer_with_space(minimizer):
    # check we can pass a Space instance as dimensions argument and get same
    # result
    n_calls = 4
    n_initial_points = 2

    space = Space([(-5.0, 10.0), (0.0, 15.0)])
    space_result = minimizer(
        branin,
        space,
        n_calls=n_calls,
        n_initial_points=n_initial_points,
        random_state=1,
    )

    check_minimizer_api(space_result, n_calls)
    check_minimizer_bounds(space_result, n_calls)

    dimensions = [(-5.0, 10.0), (0.0, 15.0)]
    result = minimizer(
        branin,
        dimensions,
        n_calls=n_calls,
        n_initial_points=n_initial_points,
        random_state=1,
    )

    assert_array_almost_equal(space_result.x_iters, result.x_iters)
    assert_array_almost_equal(space_result.func_vals, result.func_vals)
Exemple #5
0
def test_constant_property():
    space = Space([(0.0, 1.0), (1, ), ("a", "b", "c"),
                   (1.0, 5.0, "log-uniform"), ("e", )])
    assert space.n_constant_dimensions == 2
    for i in [1, 4]:
        assert space.dimensions[i].is_constant
    for i in [0, 2, 3]:
        assert not space.dimensions[i].is_constant
Exemple #6
0
def test_normalize():
    # can you pass a Space instance to the Space constructor?
    space = Space([(0.0, 1.0), (-5, 5), ("a", "b", "c"),
                   (1.0, 5.0, "log-uniform"), ("e", "f")])
    space.set_transformer("normalize")
    X = [[0.0, -5, "a", 1.0, "e"]]
    Xt = np.zeros((1, 5))
    assert_array_equal(space.transform(X), Xt)
    assert_array_equal(space.inverse_transform(Xt), X)
    assert_array_equal(space.inverse_transform(space.transform(X)), X)
Exemple #7
0
def test_acquisition_gradient_cookbook():
    rng = np.random.RandomState(0)
    X = rng.randn(20, 5)
    y = rng.randn(20)
    X_new = rng.randn(5)
    gpr = cook_estimator("GP", Space(((-5.0, 5.0), )), random_state=0)
    gpr.fit(X, y)

    for acq_func in ["LCB", "PI", "EI"]:
        check_gradient_correctness(X_new, gpr, acq_func, np.max(y))
Exemple #8
0
def test_acquisition_per_second_gradient(acq_func):
    rng = np.random.RandomState(0)
    X = rng.randn(20, 10)
    # Make the second component large, so that mean_grad and std_grad
    # do not become zero.
    y = np.vstack((X[:, 0], np.abs(X[:, 0])**3)).T

    for X_new in [rng.randn(10), rng.randn(10)]:
        gpr = cook_estimator("GP", Space(((-5.0, 5.0), )), random_state=0)
        mor = MultiOutputRegressor(gpr)
        mor.fit(X, y)
        check_gradient_correctness(X_new, mor, acq_func, 1.5)
Exemple #9
0
def test_space_from_yaml():
    with NamedTemporaryFile(delete=False) as tmp:
        tmp.write(b"""
        Space:
            - Real:
                low: 0.0
                high: 1.0
            - Integer:
                low: -5
                high: 5
            - Categorical:
                categories:
                - a
                - b
                - c
            - Real:
                low: 1.0
                high: 5.0
                prior: log-uniform
            - Categorical:
                categories:
                - e
                - f
        """)
        tmp.flush()

        space = Space([
            (0.0, 1.0),
            (-5, 5),
            ("a", "b", "c"),
            (1.0, 5.0, "log-uniform"),
            ("e", "f"),
        ])

        space2 = Space.from_yaml(tmp.name)
        assert_equal(space, space2)
        tmp.close()
        os.unlink(tmp.name)
Exemple #10
0
def test_normalize_bounds():
    bounds = [(-999, 189000), Categorical((True, False))]
    space = Space(normalize_dimensions(bounds))
    for a in np.linspace(1e-9, 0.4999, 1000):
        x = space.inverse_transform([[a, a]])
        check_limits(x[0][0], -999, 189000)
        y = space.transform(x)
        check_limits(y, 0.0, 1.0)
    for a in np.linspace(0.50001, 1e-9 + 1.0, 1000):
        x = space.inverse_transform([[a, a]])
        check_limits(x[0][0], -999, 189000)
        y = space.transform(x)
        check_limits(y, 0.0, 1.0)
Exemple #11
0
def test_dimension_name():
    notnames = [1, 1.0, True]
    for n in notnames:
        with pytest.raises(ValueError) as exc:
            real = Real(1, 2, name=n)
            assert ("Dimension's name must be either string or"
                    "None." == exc.value.args[0])
    s = Space([
        Real(1, 2, name="a"),
        Integer(1, 100, name="b"),
        Categorical(["red, blue"], name="c"),
    ])
    assert s["a"] == (0, s.dimensions[0])
    assert s["a", "c"] == [(0, s.dimensions[0]), (2, s.dimensions[2])]
    assert s[["a", "c"]] == [(0, s.dimensions[0]), (2, s.dimensions[2])]
    assert s[("a", "c")] == [(0, s.dimensions[0]), (2, s.dimensions[2])]
    assert s[0] == (0, s.dimensions[0])
    assert s[0, "c"] == [(0, s.dimensions[0]), (2, s.dimensions[2])]
    assert s[0, 2] == [(0, s.dimensions[0]), (2, s.dimensions[2])]
Exemple #12
0
def test_acquisition_per_second(acq_func):
    X = np.reshape(np.linspace(4.0, 8.0, 10), (-1, 1))
    y = np.vstack((np.ones(10), np.ravel(np.log(X)))).T
    cgpr = ConstantGPRSurrogate(Space(((1.0, 9.0), )))
    cgpr.fit(X, y)

    X_pred = np.reshape(np.linspace(1.0, 11.0, 20), (-1, 1))
    indices = np.arange(6)
    vals = _gaussian_acquisition(X_pred, cgpr, y_opt=1.0, acq_func=acq_func)
    for fast, slow in zip(indices[:-1], indices[1:]):
        assert vals[slow] > vals[fast]

    acq_wo_time = _gaussian_acquisition(X,
                                        cgpr.estimators_[0],
                                        y_opt=1.2,
                                        acq_func=acq_func[:2])
    acq_with_time = _gaussian_acquisition(X,
                                          cgpr,
                                          y_opt=1.2,
                                          acq_func=acq_func)
    assert_array_almost_equal(acq_wo_time / acq_with_time, np.ravel(X), 2)
Exemple #13
0
def test_gaussian_acquisition_check_inputs():
    model = ConstantGPRSurrogate(Space(((1.0, 9.0), )))
    with pytest.raises(ValueError) as err:
        _gaussian_acquisition(np.arange(1, 5), model)
    assert "it must be 2-dimensional" in err.value.args[0]
Exemple #14
0
def test_set_get_transformer():
    # can you pass a Space instance to the Space constructor?
    space = Space([(0.0, 1.0), (-5, 5), ("a", "b", "c"),
                   (1.0, 5.0, "log-uniform"), ("e", "f")])

    transformer = space.get_transformer()
    assert_array_equal(
        ["identity", "identity", "onehot", "identity", "onehot"], transformer)
    space.set_transformer("normalize")
    transformer = space.get_transformer()
    assert_array_equal(["normalize"] * 5, transformer)
    space.set_transformer(transformer)
    assert_array_equal(transformer, space.get_transformer())

    space.set_transformer_by_type("label", Categorical)
    assert space.dimensions[2].transform(["a"]) == [0]
Exemple #15
0
def test_space_api():
    space = Space([(0.0, 1.0), (-5, 5), ("a", "b", "c"),
                   (1.0, 5.0, "log-uniform"), ("e", "f")])

    cat_space = Space([(1, "r"), (1.0, "r")])
    assert isinstance(cat_space.dimensions[0], Categorical)
    assert isinstance(cat_space.dimensions[1], Categorical)

    assert_equal(len(space.dimensions), 5)
    assert isinstance(space.dimensions[0], Real)
    assert isinstance(space.dimensions[1], Integer)
    assert isinstance(space.dimensions[2], Categorical)
    assert isinstance(space.dimensions[3], Real)
    assert isinstance(space.dimensions[4], Categorical)

    samples = space.rvs(n_samples=10, random_state=0)
    assert_equal(len(samples), 10)
    assert_equal(len(samples[0]), 5)

    assert isinstance(samples, list)
    for n in range(4):
        assert isinstance(samples[n], list)

    assert isinstance(samples[0][0], numbers.Real)
    assert isinstance(samples[0][1], numbers.Integral)
    assert isinstance(samples[0][2], str)
    assert isinstance(samples[0][3], numbers.Real)
    assert isinstance(samples[0][4], str)

    samples_transformed = space.transform(samples)
    assert_equal(samples_transformed.shape[0], len(samples))
    assert_equal(samples_transformed.shape[1], 1 + 1 + 3 + 1 + 1)

    # our space contains mixed types, this means we can't use
    # `array_allclose` or similar to check points are close after a round-trip
    # of transformations
    for orig, round_trip in zip(samples,
                                space.inverse_transform(samples_transformed)):
        assert space.distance(orig, round_trip) < 1.0e-8

    samples = space.inverse_transform(samples_transformed)
    assert isinstance(samples[0][0], numbers.Real)
    assert isinstance(samples[0][1], numbers.Integral)
    assert isinstance(samples[0][2], str)
    assert isinstance(samples[0][3], numbers.Real)
    assert isinstance(samples[0][4], str)

    for b1, b2 in zip(
            space.bounds,
        [
            (0.0, 1.0),
            (-5, 5),
            np.asarray(["a", "b", "c"]),
            (1.0, 5.0),
            np.asarray(["e", "f"]),
        ],
    ):
        assert_array_equal(b1, b2)

    for b1, b2 in zip(
            space.transformed_bounds,
        [
            (0.0, 1.0),
            (-5, 5),
            (0.0, 1.0),
            (0.0, 1.0),
            (0.0, 1.0),
            (np.log10(1.0), np.log10(5.0)),
            (0.0, 1.0),
        ],
    ):
        assert_array_equal(b1, b2)
Exemple #16
0
def test_space_consistency():
    # Reals (uniform)

    s1 = Space([Real(0.0, 1.0)])
    s2 = Space([Real(0.0, 1.0)])
    s3 = Space([Real(0, 1)])
    s4 = Space([(0.0, 1.0)])
    s5 = Space([(0.0, 1.0, "uniform")])
    s6 = Space([(0, 1.0)])
    s7 = Space([(np.float64(0.0), 1.0)])
    s8 = Space([(0, np.float64(1.0))])
    a1 = s1.rvs(n_samples=10, random_state=0)
    a2 = s2.rvs(n_samples=10, random_state=0)
    a3 = s3.rvs(n_samples=10, random_state=0)
    a4 = s4.rvs(n_samples=10, random_state=0)
    a5 = s5.rvs(n_samples=10, random_state=0)
    assert_equal(s1, s2)
    assert_equal(s1, s3)
    assert_equal(s1, s4)
    assert_equal(s1, s5)
    assert_equal(s1, s6)
    assert_equal(s1, s7)
    assert_equal(s1, s8)
    assert_array_equal(a1, a2)
    assert_array_equal(a1, a3)
    assert_array_equal(a1, a4)
    assert_array_equal(a1, a5)

    # Reals (log-uniform)
    s1 = Space([Real(10**-3.0, 10**3.0, prior="log-uniform", base=10)])
    s2 = Space([Real(10**-3.0, 10**3.0, prior="log-uniform", base=10)])
    s3 = Space([Real(10**-3, 10**3, prior="log-uniform", base=10)])
    s4 = Space([(10**-3.0, 10**3.0, "log-uniform", 10)])
    s5 = Space([(np.float64(10**-3.0), 10**3.0, "log-uniform", 10)])
    a1 = s1.rvs(n_samples=10, random_state=0)
    a2 = s2.rvs(n_samples=10, random_state=0)
    a3 = s3.rvs(n_samples=10, random_state=0)
    a4 = s4.rvs(n_samples=10, random_state=0)
    assert_equal(s1, s2)
    assert_equal(s1, s3)
    assert_equal(s1, s4)
    assert_equal(s1, s5)
    assert_array_equal(a1, a2)
    assert_array_equal(a1, a3)
    assert_array_equal(a1, a4)

    # Integers
    s1 = Space([Integer(1, 5)])
    s2 = Space([Integer(1.0, 5.0)])
    s3 = Space([(1, 5)])
    s4 = Space([(np.int64(1.0), 5)])
    s5 = Space([(1, np.int64(5.0))])
    a1 = s1.rvs(n_samples=10, random_state=0)
    a2 = s2.rvs(n_samples=10, random_state=0)
    a3 = s3.rvs(n_samples=10, random_state=0)
    assert_equal(s1, s2)
    assert_equal(s1, s3)
    assert_equal(s1, s4)
    assert_equal(s1, s5)
    assert_array_equal(a1, a2)
    assert_array_equal(a1, a3)

    # Integers (log-uniform)
    s1 = Space([Integer(16, 512, prior="log-uniform", base=2)])
    s2 = Space([Integer(16.0, 512.0, prior="log-uniform", base=2)])
    s3 = Space([(16, 512, "log-uniform", 2)])
    s4 = Space([(np.int64(16.0), 512, "log-uniform", 2)])
    s5 = Space([(16, np.int64(512.0), "log-uniform", 2)])
    a1 = s1.rvs(n_samples=10, random_state=0)
    a2 = s2.rvs(n_samples=10, random_state=0)
    a3 = s3.rvs(n_samples=10, random_state=0)
    assert_equal(s1, s2)
    assert_equal(s1, s3)
    assert_equal(s1, s4)
    assert_equal(s1, s5)
    assert_array_equal(a1, a2)
    assert_array_equal(a1, a3)

    # Categoricals
    s1 = Space([Categorical(["a", "b", "c"])])
    s2 = Space([Categorical(["a", "b", "c"])])
    s3 = Space([["a", "b", "c"]])
    a1 = s1.rvs(n_samples=10, random_state=0)
    a2 = s2.rvs(n_samples=10, random_state=0)
    a3 = s3.rvs(n_samples=10, random_state=0)
    assert_equal(s1, s2)
    assert_array_equal(a1, a2)
    assert_equal(s1, s3)
    assert_array_equal(a1, a3)

    s1 = Space([(True, False)])
    s2 = Space([Categorical([True, False])])
    s3 = Space([np.array([True, False])])
    assert s1 == s2 == s3

    # Categoricals Integer
    s1 = Space([Categorical([1, 2, 3])])
    s2 = Space([Categorical([1, 2, 3])])
    s3 = Space([[1, 2, 3]])
    a1 = s1.rvs(n_samples=10, random_state=0)
    a2 = s2.rvs(n_samples=10, random_state=0)
    a3 = s3.rvs(n_samples=10, random_state=0)
    assert_equal(s1, s2)
    assert_array_equal(a1, a2)
    assert_equal(s1, s3)
    assert_array_equal(a1, a3)

    s1 = Space([(True, False)])
    s2 = Space([Categorical([True, False])])
    s3 = Space([np.array([True, False])])
    assert s1 == s2 == s3