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_batch_index_value(ma2):
bi = lambda meta : meta['batch_index']
# Test the correct batch_index value
m = elfi.ElfiModel()
op = elfi.Operation(bi, model=m, name='op')
op['_uses_meta'] = True
client = elfi.get_client()
c = elfi.ComputationContext()
compiled_net = client.compile(m.source_net, m.nodes)
loaded_net = client.load_data(compiled_net, c, batch_index=3)
res = client.compute(loaded_net)
assert res['op'] == 3
def test_BO_works_with_zero_init_samples(ma2):
log_d = elfi.Operation(np.log, ma2['d'], name='log_d')
bounds = {n:(-2, 2) for n in ma2.parameter_names}
bo = elfi.BayesianOptimization(log_d, initial_evidence=0,
update_interval=4, batch_size=2,
bounds=bounds)
assert bo.target_model.n_evidence == 0
assert bo.n_evidence == 0
assert bo.n_precomputed_evidence == 0
assert bo.n_initial_evidence == 0
samples = 4
bo.infer(samples)
assert bo.target_model.n_evidence == samples
assert bo.n_evidence == samples
assert bo.n_precomputed_evidence == 0
assert bo.n_initial_evidence == 0
def test_BOLFI_short(ma2, distribution_test):
# Log discrepancy tends to work better
log_d = elfi.Operation(np.log, ma2['d'])
bolfi = elfi.BOLFI(
log_d,
initial_evidence=10,
update_interval=10,
batch_size=5,
bounds={'t1': (-2, 2),
't2': (-1, 1)})
n = 20
res = bolfi.infer(n)
assert bolfi.target_model.n_evidence == n
acq_x = bolfi.target_model._gp.X
# Test that you can continue the inference where we left off
res = bolfi.infer(n + 5)
assert bolfi.target_model.n_evidence == n + 5
def test_BO(ma2):
# Log transform of the distance usually smooths the distance surface
log_d = elfi.Operation(np.log, ma2['d'], name='log_d')
n_init = 20
res_init = elfi.Rejection(log_d, batch_size=5).sample(n_init, quantile=1)
bounds = {n:(-2, 2) for n in ma2.parameter_names}
bo = elfi.BayesianOptimization(log_d, initial_evidence=res_init.outputs,
update_interval=10, batch_size=5,
bounds=bounds)
assert bo.target_model.n_evidence == n_init
assert bo.n_evidence == n_init
assert bo.n_precomputed_evidence == n_init
assert bo.n_initial_evidence == n_init
n1 = 5
bo.infer(n_init + n1)