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_deepcopy_objective():
"""Test copying objectives (needed for MultiProcessEngine)."""
petab_importer = pypesto.petab.PetabImporter.from_yaml(
folder_base + "Zheng_PNAS2012/Zheng_PNAS2012.yaml")
objective = petab_importer.create_objective()
objective.amici_solver.setSensitivityMethod(
amici.SensitivityMethod_adjoint)
objective2 = copy.deepcopy(objective)
# test some properties
assert objective.amici_model.getParameterIds() \
== objective2.amici_model.getParameterIds()
assert objective.amici_solver.getSensitivityOrder() \
== objective2.amici_solver.getSensitivityOrder()
assert objective.amici_solver.getSensitivityMethod() \
== objective2.amici_solver.getSensitivityMethod()
assert len(objective.edatas) == len(objective2.edatas)
solution = petabtests.load_solution(case)
gt_chi2 = solution[petabtests.CHI2]
gt_llh = solution[petabtests.LLH]
gt_simulation_dfs = solution[petabtests.SIMULATION_DFS]
tol_chi2 = solution[petabtests.TOL_CHI2]
tol_llh = solution[petabtests.TOL_LLH]
tol_simulations = solution[petabtests.TOL_SIMULATIONS]
# import petab problem
yaml_file = os.path.join(case_dir, petabtests.problem_yaml_name(case))
# unique folder for compiled amici model
output_folder = f'amici_models/model_{case}'
# import and create objective function
importer = pypesto.petab.PetabImporter.from_yaml(
yaml_file, output_folder=output_folder)
model = importer.create_model()
obj = importer.create_objective(model=model)
# the scaled parameters
problem_parameters = importer.petab_problem.x_nominal_scaled
# simulate
ret = obj(problem_parameters, sensi_orders=(0,), return_dict=True)
# extract results
rdatas = ret['rdatas']
chi2 = sum(rdata['chi2'] for rdata in rdatas)
llh = - ret['fval']
simulation_df = amici.petab_objective.rdatas_to_measurement_df(
rdatas, model, importer.petab_problem.measurement_df)
def _test_petab(engine):
petab_importer = pypesto.petab.PetabImporter.from_yaml(
folder_base + "Zheng_PNAS2012/Zheng_PNAS2012.yaml")
objective = petab_importer.create_objective()
problem = petab_importer.create_problem(objective)
optimizer = pypesto.optimize.ScipyOptimizer(options={'maxiter': 10})
result = pypesto.optimize.minimize(
problem=problem, n_starts=3, engine=engine, optimizer=optimizer)
assert len(result.optimize_result.as_list()) == 3
def test_pickle_objective():
"""Test serializing objectives (needed for MultiThreadEngine)."""
petab_importer = pypesto.petab.PetabImporter.from_yaml(
folder_base + "Zheng_PNAS2012/Zheng_PNAS2012.yaml")
objective = petab_importer.create_objective()
objective.amici_solver.setSensitivityMethod(
amici.SensitivityMethod_adjoint)
objective2 = pickle.loads(pickle.dumps(objective))
# test some properties
assert objective.amici_model.getParameterIds() \
== objective2.amici_model.getParameterIds()
assert objective.amici_solver.getSensitivityOrder() \
== objective2.amici_solver.getSensitivityOrder()
assert objective.amici_solver.getSensitivityMethod() \
== objective2.amici_solver.getSensitivityMethod()
assert len(objective.edatas) == len(objective2.edatas)