Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# import sbml model, compile and generate amici module
sbml_importer = amici.SbmlImporter(sbml_file)
sbml_importer.sbml2amici(model_name,
model_output_dir,
verbose=False)
# load amici module (the usual starting point later for the analysis)
sys.path.insert(0, os.path.abspath(model_output_dir))
model_module = importlib.import_module(model_name)
model = model_module.getModel()
model.requireSensitivitiesForAllParameters()
model.setTimepoints(np.linspace(0, 10, 11))
model.setParameterScale(amici.ParameterScaling_log10)
model.setParameters([-0.3, -0.7])
solver = model.getSolver()
solver.setSensitivityMethod(amici.SensitivityMethod_forward)
solver.setSensitivityOrder(amici.SensitivityOrder_first)
# generate experimental data
rdata = amici.runAmiciSimulation(model, solver, None)
edata = amici.ExpData(rdata, 0.05, 0.0)
return (pypesto.AmiciObjective(model, solver, [edata], 2),
model)
model.name = 'test_model_presimulation_pysb'
outdir = model.name
amici.pysb2amici(model,
outdir,
verbose=False,
observables=['pPROT_obs'],
constant_parameters=['DRUG_0', 'KIN_0'])
sys.path.insert(0, outdir)
modelModulePYSB = importlib.import_module(outdir)
self.model = modelModulePYSB.getModel()
self.model.setReinitializeFixedParameterInitialStates(True)
self.solver = self.model.getSolver()
self.solver.setSensitivityOrder(amici.SensitivityOrder_first)
self.solver.setSensitivityMethod(amici.SensitivityMethod_forward)
self.edata = get_data(self.model)
self.edata.t_presim = 2
self.edata.fixedParameters = [10, 2]
self.edata.fixedParametersPresimulation = [3, 2]
self.edata.fixedParametersPreequilibration = [3, 0]
self.edata.setTimepoints([1, 5])
self.edata_preeq = amici.ExpData(self.edata)
self.edata_preeq.t_presim = 0
self.edata_preeq.setTimepoints([np.infty])
self.edata_preeq.fixedParameters = \
self.edata.fixedParametersPreequilibration
self.edata_preeq.fixedParametersPresimulation = ()
self.edata_preeq.fixedParametersPreequilibration = ()
def check_sensi_orders(self, sensi_orders, mode) -> bool:
sensi_order = max(sensi_orders)
# dynamically obtain maximum allowed sensitivity order
max_sensi_order = self.max_sensi_order
if max_sensi_order is None:
max_sensi_order = 1
# check whether it is ok to request 2nd order
sensi_mthd = self.amici_solver.getSensitivityMethod()
mthd_fwd = amici.SensitivityMethod_forward
if mode == MODE_FUN and (
self.amici_model.o2mode or (
sensi_mthd == mthd_fwd and self.fim_for_hess)):
max_sensi_order = 2
# evaluate sensitivity order
return sensi_order <= max_sensi_order
if mode == MODE_FUN:
if sensi_order > 0:
# add gradient
add_sim_grad_to_opt_grad(
x_ids,
par_sim_ids,
condition_map_sim_var,
rdata['sllh'],
snllh,
coefficient=-1.0
)
# Hessian
if sensi_order > 1:
if sensi_method == amici.SensitivityMethod_forward \
and fim_for_hess:
# add FIM for Hessian
add_sim_hess_to_opt_hess(
x_ids,
par_sim_ids,
condition_map_sim_var,
rdata['FIM'],
s2nllh,
coefficient=+1.0
)
else:
raise ValueError("AMICI cannot compute Hessians yet.")
elif mode == MODE_RES:
chi2 += rdata['chi2']
res = np.hstack([res, rdata['res']]) \