Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parameter_estimation(
objective, library, solver, fixed_pars, n_starts):
options = {
'maxiter': 10
}
if library == 'scipy':
optimizer = pypesto.ScipyOptimizer(method=solver,
options=options)
elif library == 'dlib':
optimizer = pypesto.DlibOptimizer(method=solver,
options=options)
elif library == 'pyswarm':
optimizer = pypesto.PyswarmOptimizer(options=options)
else:
raise ValueError("This code should not be reached")
optimizer.temp_file = os.path.join('test', 'tmp_{index}.csv')
dim = len(objective.x_ids)
lb = -2 * np.ones((1, dim))
ub = 2 * np.ones((1, dim))
pars = objective.amici_model.getParameters()
problem = pypesto.Problem(objective, lb, ub,
def check_minimize(objective, library, solver, allow_failed_starts=False):
options = {
'maxiter': 100
}
optimizer = None
if library == 'scipy':
optimizer = pypesto.ScipyOptimizer(method=solver,
options=options)
elif library == 'ipopt':
optimizer = pypesto.IpoptOptimizer()
elif library == 'dlib':
optimizer = pypesto.DlibOptimizer(method=solver,
options=options)
elif library == 'pyswarm':
optimizer = pypesto.PyswarmOptimizer(options=options)
lb = 0 * np.ones((1, 2))
ub = 1 * np.ones((1, 2))
problem = pypesto.Problem(objective, lb, ub)
optimize_options = pypesto.OptimizeOptions(
allow_failed_starts=allow_failed_starts)
"""
Test whether optimization with preequilibration guesses works, asserts
that steadystate guesses are written and checks that gradient is still
correct with guesses set.
"""
petab_problem = petab.Problem.from_yaml(
folder_base + "Zheng_PNAS2012/Zheng_PNAS2012.yaml")
petab_problem.model_name = "Zheng_PNAS2012"
importer = pypesto.PetabImporter(petab_problem)
obj = importer.create_objective()
problem = importer.create_problem(obj)
# assert that initial guess is uninformative
assert problem.objective.steadystate_guesses['fval'] == np.inf
optimizer = pypesto.ScipyOptimizer('L-BFGS-B', options={'maxiter': 50})
result = pypesto.minimize(
problem=problem, optimizer=optimizer, n_starts=1,
)
assert problem.objective.steadystate_guesses['fval'] < np.inf
assert len(obj.steadystate_guesses['data']) == 1
df = obj.check_grad(
result.optimize_result.list[0]['x'],
eps=1e-3,
verbosity=0,
mode=pypesto.objective.constants.MODE_FUN
)
print("relative errors MODE_FUN: ", df.rel_err.values)
print("absolute errors MODE_FUN: ", df.abs_err.values)
assert np.all((df.rel_err.values < RTOL) | (df.abs_err.values < ATOL))
def test_error_leastsquares_with_ssigma():
petab_problem = petab.Problem.from_yaml(
folder_base + "Zheng_PNAS2012/Zheng_PNAS2012.yaml")
petab_problem.model_name = "Zheng_PNAS2012"
importer = pypesto.PetabImporter(petab_problem)
obj = importer.create_objective()
problem = importer.create_problem(obj)
optimizer = pypesto.ScipyOptimizer('ls_trf', options={'max_nfev': 50})
with pytest.raises(RuntimeError):
pypesto.minimize(
problem=problem, optimizer=optimizer, n_starts=1,
options=pypesto.OptimizeOptions(allow_failed_starts=False)
)
def test_3_optimize(self):
# run optimization
for obj_edatas, importer in \
zip(self.obj_edatas, self.petab_importers):
obj = obj_edatas[0]
optimizer = pypesto.ScipyOptimizer()
problem = importer.create_problem(obj)
result = pypesto.minimize(
problem=problem, optimizer=optimizer, n_starts=2)
self.assertTrue(np.isfinite(
result.optimize_result.get_for_key('fval')[0]))