Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_random_sampling_with_context():
space = ParameterSpace([ContinuousParameter('x', 0, 1), ContinuousParameter('y', 0, 1)])
rs = RandomSampling(space)
loop_state_mock = mock.create_autospec(LoopState)
next_points = rs.compute_next_points(loop_state_mock, context={'x': 0.25})
assert(len(next_points) == 1)
# Context value should be what we set
assert np.isclose(next_points[0, 0], 0.25)
def space():
space = ParameterSpace([ContinuousParameter('x1', 0, 1),
ContinuousParameter('x2', 0, 1),
ContinuousParameter('x3', 0, 1)])
return space
def test_multi_source_sequential_with_source_context():
# Check that we can fix a non-information source parameter with context
mock_acquisition = mock.create_autospec(Acquisition)
mock_acquisition.has_gradients = False
mock_acquisition.evaluate = lambda x: np.sum(x**2, axis=1)[:, None]
space = ParameterSpace([ContinuousParameter('x', 0, 1),
ContinuousParameter('y', 0, 1),
InformationSourceParameter(2)])
acquisition_optimizer = AcquisitionOptimizer(space)
multi_source_acquisition_optimizer = MultiSourceAcquisitionOptimizer(acquisition_optimizer, space)
loop_state_mock = mock.create_autospec(LoopState)
seq = SequentialPointCalculator(mock_acquisition, multi_source_acquisition_optimizer)
next_points = seq.compute_next_points(loop_state_mock, context={'source': 1.0})
# "SequentialPointCalculator" should only ever return 1 value
assert(len(next_points) == 1)
# Context value should be what we set
assert np.isclose(next_points[0, 1], 1.)
def space_2d():
p1 = ContinuousParameter('c', 1.0, 5.0)
p2 = ContinuousParameter('d', 1.0, 6.0)
return ParameterSpace([p1, p2])
def test_cost_sensitive_bayesian_optimization_loop():
space = ParameterSpace([ContinuousParameter('x', 0, 1)])
x_init = np.random.rand(10, 1)
def function_with_cost(x):
return np.sin(x), x
user_fcn = UserFunctionWrapper(function_with_cost)
y_init, cost_init = function_with_cost(x_init)
gpy_model_objective = GPy.models.GPRegression(x_init, y_init)
gpy_model_cost = GPy.models.GPRegression(x_init, cost_init)
model_objective = GPyModelWrapper(gpy_model_objective)
model_cost = GPyModelWrapper(gpy_model_cost)
def test_batch_experimental_design_loop():
class MockGPyModel(GPyModelWrapper):
def optimize(self):
# speed up test by skipping the actual hyper-parameter optimization
pass
user_function = lambda x: x
space = ParameterSpace([ContinuousParameter('x', 0, 3)])
# Make model
x_init = np.linspace(0, 3, 5)[:, None]
y_init = user_function(x_init)
gpy_model = GPy.models.GPRegression(x_init, y_init)
model = MockGPyModel(gpy_model)
loop = ExperimentalDesignLoop(space, model, batch_size=5)
loop.run_loop(user_function, 5)
assert(loop.loop_state.iteration == 5)
assert(loop.loop_state.X.shape[0] == 30)
def test_iteration_end_event():
space = ParameterSpace([ContinuousParameter('x', 0, 1)])
def user_function(x):
return x
x_test = np.linspace(0, 1)[:, None]
y_test = user_function(x_test)
x_init = np.linspace(0, 1, 5)[:, None]
y_init = user_function(x_init)
gpy_model = GPy.models.GPRegression(x_init, y_init)
model = GPyModelWrapper(gpy_model)
mse = []
def compute_mse(self, loop_state):
def multi_source_optimizer():
mock_acquisition_optimizer = mock.create_autospec(AcquisitionOptimizer)
mock_acquisition_optimizer.optimize.return_value = (np.array([[0.]]), None)
space = ParameterSpace([ContinuousParameter('x', 0, 1), InformationSourceParameter(2)])
return MultiSourceAcquisitionOptimizer(mock_acquisition_optimizer, space)
def test_design_returns_correct_number_of_points():
p = ContinuousParameter('c', 1.0, 5.0)
space = ParameterSpace([p])
points_count = 5
designs = create_model_free_designs(space)
for design in designs:
points = design.get_samples(points_count)
assert points_count == len(points)
columns_count = 1
assert all([len(p) == columns_count for p in points])
def test_random_sampling_without_context():
space = ParameterSpace([ContinuousParameter('x', 0, 1), ContinuousParameter('y', 0, 1)])
rs = RandomSampling(space)
loop_state_mock = mock.create_autospec(LoopState)
next_points = rs.compute_next_points(loop_state_mock)
assert(len(next_points) == 1)