Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
SCHI2: lambda x: sres_to_schi2(*self.obj(
x, (0, 1,),
pypesto.objective.constants.MODE_RES
))
def runTest(self):
for example in ['conversion_reaction']:
objective, model = load_model_objective(example)
x0 = np.array(list(model.getParameters()))
df = objective.check_grad(
x0, 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))
df = objective.check_grad(
x0, eps=1e-3, verbosity=0,
mode=pypesto.objective.constants.MODE_RES)
print("relative errors MODE_RES: ", df.rel_err.values)
print("absolute errors MODE_RES: ", df.rel_err.values)
assert np.all((df.rel_err.values < RTOL) |
(df.abs_err.values < ATOL))
for library in optimizers.keys():
for method in optimizers[library]:
for fp in [[], [1]]:
with self.subTest(library=library,
solver=method,
fp=fp):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
parameter_estimation(
objective, library, method, fp, 2)
def _test_evaluate_resmode(struct):
obj = pypesto.objective.AggregatedObjective(
[struct['obj'], struct['obj']]
)
x = struct['x']
res_true = np.hstack([struct['res'], struct['res']])
sres_true = np.vstack([struct['sres'], struct['sres']])
max_sensi_order = struct['max_sensi_order']
# check function values
if max_sensi_order >= 1:
res, sres = obj(x, (0, 1), MODE_RES)
assert np.isclose(res, res_true, atol=ATOL, rtol=RTOL).all()
assert np.isclose(sres, sres_true, atol=ATOL, rtol=RTOL).all()
res = obj(x, (0,), MODE_RES)
assert np.isclose(res, res_true, atol=ATOL, rtol=RTOL).all()
# check convenience functions)
assert np.isclose(obj.get_res(x), res_true, atol=ATOL, rtol=RTOL).all()
if max_sensi_order >= 1:
assert np.isclose(obj.get_sres(x), sres_true,
atol=ATOL, rtol=RTOL).all()
def _test_evaluate_resmode(struct):
obj = pypesto.objective.AggregatedObjective(
[struct['obj'], struct['obj']]
)
x = struct['x']
res_true = np.hstack([struct['res'], struct['res']])
sres_true = np.vstack([struct['sres'], struct['sres']])
max_sensi_order = struct['max_sensi_order']
# check function values
if max_sensi_order >= 1:
res, sres = obj(x, (0, 1), MODE_RES)
assert np.isclose(res, res_true, atol=ATOL, rtol=RTOL).all()
assert np.isclose(sres, sres_true, atol=ATOL, rtol=RTOL).all()
res = obj(x, (0,), MODE_RES)
assert np.isclose(res, res_true, atol=ATOL, rtol=RTOL).all()
# check convenience functions)
assert np.isclose(obj.get_res(x), res_true, atol=ATOL, rtol=RTOL).all()
if max_sensi_order >= 1:
assert np.isclose(obj.get_sres(x), sres_true,
atol=ATOL, rtol=RTOL).all()
def check_mode(self, mode):
return mode in [MODE_FUN, MODE_RES]
def check_sensi_orders(self, sensi_orders, mode) -> None:
"""
Check if the objective is able to compute the requested
sensitivities. If not, throw an exception.
Raises
------
ValueError if the objective function cannot be called as
requested.
"""
if (mode is MODE_FUN and
(0 in sensi_orders and not self.has_fun
or 1 in sensi_orders and not self.has_grad
or 2 in sensi_orders and not self.has_hess)
) or (mode is MODE_RES and
(0 in sensi_orders and not self.has_res
or 1 in sensi_orders and not self.has_sres)
):
raise ValueError(
f"Objective cannot be called with sensi_orders={sensi_orders}"
f" and mode={mode}")
# check if the simulation failed
if any(rdata['status'] < 0.0 for rdata in rdatas):
return get_error_output(amici_model, edatas, rdatas, dim)
# prepare outputs
nllh = 0.0
snllh = None
s2nllh = None
if mode == MODE_FUN and sensi_order > 0:
snllh = np.zeros(dim)
s2nllh = np.zeros([dim, dim])
chi2 = None
res = None
sres = None
if mode == MODE_RES:
chi2 = 0.0
res = np.zeros([0])
if sensi_order > 0:
sres = np.zeros([0, dim])
par_sim_ids = list(amici_model.getParameterIds())
sensi_method = amici_solver.getSensitivityMethod()
# iterate over return data
for data_ix, rdata in enumerate(rdatas):
log_simulation(data_ix, rdata)
condition_map_sim_var = \
parameter_mapping[data_ix].map_sim_var
# add objective value
def _call_mode_res(
self, x: np.ndarray, sensi_orders: Tuple[int, ...]
) -> Dict:
"""
The method __call__ was called with mode MODE_RES.
"""
if self.res_accept_sensi_orders:
result = self.res(x, sensi_orders)
if not isinstance(result, dict):
result = Objective.output_to_dict(
sensi_orders, MODE_RES, result)
elif sensi_orders == (0,):
if self.sres is True:
res = self.res(x)[0]
else:
res = self.res(x)
result = {RES: res}
elif sensi_orders == (1,):
if self.sres is True:
sres = self.res(x)[1]
else:
sres = self.sres(x)
result = {SRES: sres}
elif sensi_orders == (0, 1):
if self.sres is True:
res, sres = self.res(x)
else:
def _update_counts(self,
sensi_orders: Tuple[int, ...],
mode: str):
"""
Update the counters.
"""
if mode == MODE_FUN:
if 0 in sensi_orders:
self._n_fval += 1
if 1 in sensi_orders:
self._n_grad += 1
if 2 in sensi_orders:
self._n_hess += 1
elif mode == MODE_RES:
if 0 in sensi_orders:
self._n_res += 1
if 1 in sensi_orders:
self._n_sres += 1