def test_real_log_sampling_in_bounds(): dim = Real(low=1, high=32, prior="log-uniform", transform="normalize") # round trip a value that is within the bounds of the space # # x = dim.inverse_transform(dim.transform(31.999999999999999)) for n in (32.0, 31.999999999999999): round_tripped = dim.inverse_transform(dim.transform([n])) assert np.allclose([n], round_tripped) assert n in dim assert round_tripped in dim
def test_real(): a = Real(1, 25) for i in range(50): r = a.rvs(random_state=i) check_limits(r, 1, 25) assert r in a random_values = a.rvs(random_state=0, n_samples=10) assert len(random_values) == 10 assert_array_equal(a.transform(random_values), random_values) assert_array_equal(a.inverse_transform(random_values), random_values) log_uniform = Real(10**-5, 10**5, prior="log-uniform") assert log_uniform != Real(10**-5, 10**5) for i in range(50): random_val = log_uniform.rvs(random_state=i) check_limits(random_val, 10**-5, 10**5) random_values = log_uniform.rvs(random_state=0, n_samples=10) assert len(random_values) == 10 transformed_vals = log_uniform.transform(random_values) assert_array_equal(transformed_vals, np.log10(random_values)) assert_array_equal(log_uniform.inverse_transform(transformed_vals), random_values)
def test_normalize_real(): a = Real(2.0, 30.0, transform="normalize") for i in range(50): check_limits(a.rvs(random_state=i), 2, 30) rng = np.random.RandomState(0) X = rng.randn(100) X = 28 * (X - X.min()) / (X.max() - X.min()) + 2 # Check transformed values are in [0, 1] assert np.all(a.transform(X) <= np.ones_like(X)) assert np.all(np.zeros_like(X) <= a.transform(X)) # Check inverse transform assert_array_almost_equal(a.inverse_transform(a.transform(X)), X) # log-uniform prior a = Real(10**2.0, 10**4.0, prior="log-uniform", transform="normalize") for i in range(50): check_limits(a.rvs(random_state=i), 10**2, 10**4) rng = np.random.RandomState(0) X = np.clip(10**3 * rng.randn(100), 10**2.0, 10**4.0) # Check transform assert np.all(a.transform(X) <= np.ones_like(X)) assert np.all(np.zeros_like(X) <= a.transform(X)) # Check inverse transform assert_array_almost_equal(a.inverse_transform(a.transform(X)), X) a = Real(0, 1, transform="normalize", dtype=float) for i in range(50): check_limits(a.rvs(random_state=i), 0, 1) assert_array_equal(a.transformed_bounds, (0, 1)) X = rng.rand() # Check transformed values are in [0, 1] assert np.all(a.transform(X) <= np.ones_like(X)) assert np.all(np.zeros_like(X) <= a.transform(X)) # Check inverse transform X_orig = a.inverse_transform(a.transform(X)) assert isinstance(X_orig, float) assert_array_equal(X_orig, X) a = Real(0, 1, transform="normalize", dtype="float64") X = np.float64(rng.rand()) # Check inverse transform X_orig = a.inverse_transform(a.transform(X)) assert isinstance(X_orig, np.float64) a = Real(0, 1, transform="normalize", dtype=np.float64) X = np.float64(rng.rand()) # Check inverse transform X_orig = a.inverse_transform(a.transform(X)) assert isinstance(X_orig, np.float64) a = Real(0, 1, transform="normalize", dtype="float64") X = np.float64(rng.rand()) # Check inverse transform X_orig = a.inverse_transform(a.transform(X)) assert isinstance(X_orig, np.float64)