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