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)
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))
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)
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)