Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def gaussian_problem():
def nllh(x):
return - gaussian_llh(x)
objective = pypesto.Objective(fun=nllh)
problem = pypesto.Problem(objective=objective, lb=[-10], ub=[10])
return problem
def create_problem():
objective = rosen_for_sensi(2)['obj']
lb = [-3, -3, -3, -3, -3]
ub = [3, 3, 3, 3, 3]
x_fixed_indices = [1, 3]
x_fixed_vals = [1, 1]
problem = pypesto.Problem(objective=objective,
lb=lb, ub=ub,
x_fixed_indices=x_fixed_indices,
x_fixed_vals=x_fixed_vals)
return problem
def create_problem():
# define a pypesto objective
objective = pypesto.Objective(fun=so.rosen,
grad=so.rosen_der,
hess=so.rosen_hess)
# define a pypesto problem
(lb, ub) = create_bounds()
problem = pypesto.Problem(objective=objective, lb=lb, ub=ub)
return problem
def create_optimization_results(objective):
# create optimizer, pypesto problem and options
options = {
'maxiter': 200
}
optimizer = optimize.ScipyOptimizer(method='TNC', options=options)
lb = -2 * np.ones(2)
ub = 2 * np.ones(2)
problem = pypesto.Problem(objective, lb, ub)
optimize_options = optimize.OptimizeOptions(allow_failed_starts=True)
# run optimization
result = optimize.minimize(
problem=problem,
optimizer=optimizer,
n_starts=5,
startpoint_method=pypesto.startpoint.uniform,
options=optimize_options
)
return problem, result, optimizer
def test_prior():
"""Check that priors are defined for sampling."""
# define negative log posterior
posterior_fun = pypesto.Objective(fun=negative_log_posterior)
# define negative log prior
prior_fun = pypesto.Objective(fun=negative_log_prior)
# define pypesto prior object
prior_object = pypesto.NegLogPriors(objectives=[prior_fun])
# define pypesto problem using prior object
test_problem = pypesto.Problem(objective=posterior_fun,
x_priors_defs=prior_object,
lb=-10, ub=10,
x_names=['x'])
sampler = sample.AdaptiveMetropolisSampler()
result = sample.sample(test_problem, n_samples=1e4, sampler=sampler,
x0=np.array([0.]))
# get log prior values of first chain
logprior_trace = -result.sample_result.trace_neglogprior[0, :]
# check that not all entries are zero
assert (logprior_trace != 0.).any()
# get samples of first chain
filename = ".test_logging.tmp"
pypesto.logging.log_to_file(logging.DEBUG, filename)
logger = logging.getLogger('pypesto')
if os.path.exists(filename):
os.remove(filename)
fh = logging.FileHandler(filename)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
logger.info("start test")
# problem definition
def fun(_):
raise Exception("This function cannot be called.")
objective = pypesto.Objective(fun=fun)
problem = pypesto.Problem(objective, -1, 1)
optimizer = pypesto.optimize.ScipyOptimizer()
options = {'allow_failed_starts': True}
# optimization
pypesto.optimize.minimize(problem, optimizer, 5, options=options)
# assert logging worked
assert os.path.exists(filename)
f = open(filename, 'rb')
content = str(f.read())
f.close()
# tidy up
os.remove(filename)
def _test_basic(engine):
# set up problem
objective = test_objective.rosen_for_sensi(max_sensi_order=2)['obj']
lb = 0 * np.ones((1, 2))
ub = 1 * np.ones((1, 2))
problem = pypesto.Problem(objective, lb, ub)
optimizer = pypesto.optimize.ScipyOptimizer(options={'maxiter': 10})
result = pypesto.optimize.minimize(
problem=problem, n_starts=5, engine=engine, optimizer=optimizer)
assert len(result.optimize_result.as_list()) == 5
def test_profile_with_fixed_parameters():
"""Test using profiles with fixed parameters."""
obj = test_objective.rosen_for_sensi(max_sensi_order=1)['obj']
lb = -2 * np.ones(5)
ub = 2 * np.ones(5)
problem = pypesto.Problem(
objective=obj, lb=lb, ub=ub,
x_fixed_vals=[0.5, -1.8], x_fixed_indices=[0, 3])
optimizer = optimize.ScipyOptimizer(options={'maxiter': 50})
result = optimize.minimize(
problem=problem, optimizer=optimizer, n_starts=2)
for i_method, next_guess_method in enumerate([
'fixed_step', 'adaptive_step_order_0',
'adaptive_step_order_1', 'adaptive_step_regression']):
print(next_guess_method)
profile.parameter_profile(
problem=problem, result=result, optimizer=optimizer,
next_guess_method=next_guess_method)
# standard plotting
axes = visualize.profiles(result, profile_list_ids=i_method)
assert len(axes) == 3
visualize.profile_cis(result, profile_list=i_method)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
logger.info("start test")
# problem definition
def fun(_):
raise Exception("This function cannot be called.")
objective = pypesto.Objective(fun=fun)
problem = pypesto.Problem(objective, -1, 1)
optimizer = pypesto.optimize.ScipyOptimizer()
options = {'allow_failed_starts': True}
# optimization
pypesto.optimize.minimize(problem, optimizer, 5, options=options)
# assert logging worked
assert os.path.exists(filename)
f = open(filename, 'rb')
content = str(f.read())
f.close()
# tidy up
os.remove(filename)
# check if error message got inserted
assert "fail" in content
def create_optimization_history():
# create the pypesto problem
problem = create_problem()
# create optimizer
optimizer_options = {'maxiter': 200}
optimizer = optimize.ScipyOptimizer(
method='TNC', options=optimizer_options)
history_options = pypesto.HistoryOptions(
trace_record=True, trace_save_iter=1)
# run optimization
optimize_options = optimize.OptimizeOptions(allow_failed_starts=True)
result_with_trace = optimize.minimize(
problem=problem,
optimizer=optimizer,
n_starts=5,
startpoint_method=pypesto.startpoint.uniform,
options=optimize_options,
history_options=history_options
)
return result_with_trace