Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
benchmark_dir, depth=1)
g = git.Git(benchmark_dir)
# update repo if online
try:
g.pull()
except git.exc.GitCommandError:
pass
# create problem
petab_problem = petab.Problem.from_yaml(os.path.join(
benchmark_dir, "Benchmark-Models",
"Boehm_JProteomeRes2014", "Boehm_JProteomeRes2014.yaml"))
# compile amici
model = amici.petab_import.import_petab_problem(petab_problem)
solver = model.getSolver()
# import to pyabc
importer = pyabc.petab.AmiciPetabImporter(petab_problem, model, solver)
# extract required objects
prior = importer.create_prior()
model = importer.create_model()
kernel = importer.create_kernel()
# call model
assert np.isclose(
model(petab_problem.x_nominal_free_scaled)['llh'], -138.221996)
# mini analysis
temperature = pyabc.Temperature(
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)
def test_parameter_reordering(self):
rdata_ordered = amici.runAmiciSimulation(
self.model, self.solver, self.edata
)
for plist in self.plists:
with self.subTest(plist=plist):
self.model.setParameterList(plist)
rdata_reordered = amici.runAmiciSimulation(
self.model, self.solver, self.edata
)
for ip, p_index in enumerate(plist):
self.assertTrue(np.isclose(
rdata_ordered['sx'][:, p_index, :],
rdata_reordered['sx'][:, ip, :],
1e-6, 1e-6
def get_results(model, edata):
solver = model.getSolver()
solver.setSensitivityOrder(1)
model.setTimepoints(np.linspace(0, 60, 61))
model.setReinitializeFixedParameterInitialStates(True)
model.setSteadyStateSensitivityMode(
amici.SteadyStateSensitivityMode_simulationFSA
)
return amici.runAmiciSimulation(model, solver, edata)
settings = itertools.product(self.pscales, self.plists)
for pscale, plist in settings:
self.model.setInitialStates([])
self.model.setInitialStateSensitivities([])
self.model.setParameterList(plist)
self.model.setParameterScale(pscale)
# combined
rdata_auto = amici.runAmiciSimulation(
self.model, self.solver, self.edata
)
# manual preqquilibration
rdata_preeq = amici.runAmiciSimulation(
self.model, self.solver, self.edata_preeq
)
# manual reinitialization + presimulation
x0 = rdata_preeq['x'][0, :]
x0[1] = self.edata_presim.fixedParameters[0]
x0[2] = self.edata_presim.fixedParameters[1]
sx0 = rdata_preeq['sx'][0, :, :]
sx0[:, 1] = 0
sx0[:, 2] = 0
self.model.setInitialStates(x0)
self.model.setInitialStateSensitivities(
sx0.flatten()
)
rdata_presim = amici.runAmiciSimulation(
self.model, self.solver, self.edata_presim
# otherwise we will mess up parameter value
self.model.setParameterScale(amici.parameterScalingFromIntVector([
amici.ParameterScaling_log10
if scaling == amici.ParameterScaling_none
else amici.ParameterScaling_none
for scaling in self.model.getParameterScale()
]))
self.edata.x0 = rdata['x_ss']
self.edata.sx0 = rdata['sx_ss'].flatten()
# perturb model initial states
self.model.setInitialStates(rdata['x_ss'] * 4)
self.model.setInitialStateSensitivities(rdata['sx_ss'].flatten() / 2)
rdata_edata = amici.runAmiciSimulation(
self.model, self.solver, self.edata
)
for variable in ['x', 'sx']:
with self.subTest(variable=variable):
self.assertTrue(np.isclose(
rdata[variable][0, :],
rdata_edata[variable][0, :],
1e-6, 1e-6
).all())
verbose=False,
compute_conservation_laws=compute_conservation_laws
)
sys.path.insert(0, outdir)
amici_model_module = importlib.import_module(pysb_model.name)
model_pysb = amici_model_module.getModel()
model_pysb.setTimepoints(tspan)
solver = model_pysb.getSolver()
solver.setMaxSteps(int(1e5))
solver.setAbsoluteTolerance(atol)
solver.setRelativeTolerance(rtol)
rdata = amici.runAmiciSimulation(model_pysb, solver)
# check agreement of species simulation
self.assertTrue(np.isclose(
rdata['x'],
pysb_simres.species,
1e-4, 1e-4
).all())
def get_data(model):
solver = model.getSolver()
model.setTimepoints(np.linspace(0, 60, 61))
model.setReinitializeFixedParameterInitialStates(True)
model.setSteadyStateSensitivityMode(
amici.SteadyStateSensitivityMode_simulationFSA
)
rdata = amici.runAmiciSimulation(model, solver)
edata = amici.ExpData(rdata, 0.1, 0.0)
edata.t_presim = 2
edata.fixedParameters = [10, 2]
edata.fixedParametersPresimulation = [3, 2]
edata.fixedParametersPreequilibration = [3, 0]
return edata
rdata_presim = amici.runAmiciSimulation(
self.model, self.solver, self.edata_presim
)
# manual reinitialization + simulation
x0 = rdata_presim['x'][0, :]
x0[1] = self.edata_sim.fixedParameters[0]
x0[2] = self.edata_sim.fixedParameters[1]
sx0 = rdata_presim['sx'][0, :, :]
sx0[:, 1] = 0
sx0[:, 2] = 0
self.model.setInitialStates(x0)
self.model.setInitialStateSensitivities(
sx0.flatten()
)
rdata_sim = amici.runAmiciSimulation(
self.model, self.solver, self.edata_sim
)
for variable in ['x', 'sx']:
with self.subTest(pscale=pscale, plist=plist,
variable=variable):
self.assertTrue(np.isclose(
rdata_auto[variable],
rdata_sim[variable],
1e-6, 1e-6
).all())
rdata_preeq = amici.runAmiciSimulation(
self.model, self.solver, self.edata_preeq
)
# manual reinitialization + presimulation
x0 = rdata_preeq['x'][0, :]
x0[1] = self.edata_presim.fixedParameters[0]
x0[2] = self.edata_presim.fixedParameters[1]
sx0 = rdata_preeq['sx'][0, :, :]
sx0[:, 1] = 0
sx0[:, 2] = 0
self.model.setInitialStates(x0)
self.model.setInitialStateSensitivities(
sx0.flatten()
)
rdata_presim = amici.runAmiciSimulation(
self.model, self.solver, self.edata_presim
)
# manual reinitialization + simulation
x0 = rdata_presim['x'][0, :]
x0[1] = self.edata_sim.fixedParameters[0]
x0[2] = self.edata_sim.fixedParameters[1]
sx0 = rdata_presim['sx'][0, :, :]
sx0[:, 1] = 0
sx0[:, 2] = 0
self.model.setInitialStates(x0)
self.model.setInitialStateSensitivities(
sx0.flatten()
)
rdata_sim = amici.runAmiciSimulation(
self.model, self.solver, self.edata_sim