Skip to content

Commit a787600

Browse files
committed
sim
1 parent 8519ed2 commit a787600

23 files changed

+199
-208
lines changed

doc/example/amici.ipynb

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"from pprint import pprint\n",
4545
"\n",
4646
"import amici\n",
47+
"import amici.sim.sundials as asd\n",
4748
"import matplotlib as mpl\n",
4849
"import numpy as np\n",
4950
"import petab\n",
@@ -120,7 +121,7 @@
120121
"source": [
121122
"sbml_file = f\"./{model_name}/{model_name}.xml\"\n",
122123
"# Create an SbmlImporter instance for our SBML model\n",
123-
"sbml_importer = amici.SbmlImporter(sbml_file)"
124+
"sbml_importer = amici.importers.sbml.SbmlImporter(sbml_file)"
124125
],
125126
"outputs": [],
126127
"execution_count": null
@@ -136,9 +137,7 @@
136137
"name": "#%% md\n"
137138
}
138139
},
139-
"source": [
140-
"In this example, we want to specify fixed parameters, observables and a $\\sigma$ parameter. Unfortunately, the latter two are not part of the [SBML standard](https://sbml.org/). However, they can be provided to `amici.SbmlImporter.sbml2amici` as demonstrated in the following."
141-
]
140+
"source": "In this example, we want to specify fixed parameters, observables and a $\\sigma$ parameter. Unfortunately, the latter two are not part of the [SBML standard](https://sbml.org/). However, they can be provided to `amici.importers.sbml.SbmlImporter.sbml2amici` as demonstrated in the following."
142141
},
143142
{
144143
"cell_type": "markdown",
@@ -204,7 +203,7 @@
204203
},
205204
"source": [
206205
"# Retrieve model output names and formulae from AssignmentRules and remove the respective rules\n",
207-
"observables = amici.assignment_rules_to_observables(\n",
206+
"observables = amici.importers.sbml.assignment_rules_to_observables(\n",
208207
" sbml_importer.sbml, # the libsbml model object\n",
209208
" filter_function=lambda variable: variable.getId().startswith(\"observable_\")\n",
210209
" and not variable.getId().endswith(\"_sigma\"),\n",
@@ -231,7 +230,7 @@
231230
"source": [
232231
"#### Generating the module\n",
233232
"\n",
234-
"Now we can generate the python module for our model. `amici.SbmlImporter.sbml2amici` will symbolically derive the sensitivity equations, generate C++ code for model simulation, and assemble the python module."
233+
"Now we can generate the python module for our model. `SbmlImporter.sbml2amici` will symbolically derive the sensitivity equations, generate C++ code for model simulation, and assemble the python module."
235234
]
236235
},
237236
{
@@ -287,9 +286,7 @@
287286
"name": "#%%\n"
288287
}
289288
},
290-
"source": [
291-
"model_module = amici.import_model_module(model_name, model_output_dir)"
292-
],
289+
"source": "model_module = amici.import_model_module(model_name, model_output_dir)",
293290
"outputs": [],
294291
"execution_count": null
295292
},
@@ -304,9 +301,7 @@
304301
"name": "#%% md\n"
305302
}
306303
},
307-
"source": [
308-
"Afterwards, we can get an instance of our model from which we can retrieve information such as parameter names:"
309-
]
304+
"source": "Afterwards, we can get an instance of our model from which we can retrieve information such as parameter names:"
310305
},
311306
{
312307
"cell_type": "code",
@@ -343,7 +338,7 @@
343338
"source": [
344339
"#### Running simulations and analyzing results\n",
345340
"\n",
346-
"After importing the model, we can run simulations using `amici.runAmiciSimulation`. This requires a `Model` instance and a `Solver` instance. But, in order go gain a value of fit, we also need to provide some data."
341+
"After importing the model, we can run simulations using `run_simulation`. This requires a `Model` instance and a `Solver` instance. But, in order go gain a value of fit, we also need to provide some data."
347342
]
348343
},
349344
{
@@ -479,14 +474,14 @@
479474
"model.set_fixed_parameters(np.array([0.693, 0.107]))\n",
480475
"\n",
481476
"# set parameters to optimal values found in the benchmark collection\n",
482-
"model.set_parameter_scale(amici.ParameterScaling.log10)\n",
477+
"model.set_parameter_scale(asd.ParameterScaling.log10)\n",
483478
"model.set_free_parameters(benchmark_parameters)\n",
484479
"\n",
485480
"# Create solver instance\n",
486481
"solver = model.create_solver()\n",
487482
"\n",
488483
"# Run simulation using model parameters from the benchmark collection and default solver options\n",
489-
"rdata = amici.run_simulation(model, solver)"
484+
"rdata = asd.run_simulation(model, solver)"
490485
],
491486
"outputs": [],
492487
"execution_count": null
@@ -504,7 +499,7 @@
504499
},
505500
"source": [
506501
"# Create edata instance with dimensions and timepoints\n",
507-
"edata = amici.ExpData(\n",
502+
"edata = asd.ExpData(\n",
508503
" 3, # number of observables\n",
509504
" 0, # number of event outputs\n",
510505
" 0, # maximum number of events\n",
@@ -535,7 +530,7 @@
535530
}
536531
},
537532
"source": [
538-
"rdata = amici.run_simulation(model, solver, edata)\n",
533+
"rdata = asd.run_simulation(model, solver, edata)\n",
539534
"\n",
540535
"print(\"Chi2 value reported in benchmark collection: 47.9765479\")\n",
541536
"print(f\"chi2 value using AMICI: {rdata['chi2']}\")"
@@ -577,8 +572,8 @@
577572
"# we make some more adjustments to our model and the solver\n",
578573
"model.require_sensitivities_for_all_parameters()\n",
579574
"\n",
580-
"solver.set_sensitivity_method(amici.SensitivityMethod.forward)\n",
581-
"solver.set_sensitivity_order(amici.SensitivityOrder.first)\n",
575+
"solver.set_sensitivity_method(asd.SensitivityMethod.forward)\n",
576+
"solver.set_sensitivity_order(asd.SensitivityOrder.first)\n",
582577
"\n",
583578
"\n",
584579
"objective = pypesto.AmiciObjective(\n",

doc/example/conversion_reaction.ipynb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
"import sys\n",
4343
"\n",
4444
"import amici\n",
45-
"import amici.plotting\n",
45+
"import amici.sim.sundials as asd\n",
4646
"import numpy as np\n",
47+
"from amici.sim.sundials.plotting import plot_state_trajectories\n",
4748
"\n",
4849
"import pypesto\n",
4950
"import pypesto.optimize as optimize\n",
@@ -80,7 +81,7 @@
8081
"outputs": [],
8182
"source": [
8283
"# import sbml model, compile and generate amici module\n",
83-
"sbml_importer = amici.SbmlImporter(sbml_file)\n",
84+
"sbml_importer = amici.importers.sbml.SbmlImporter(sbml_file)\n",
8485
"sbml_importer.sbml2amici(model_name, model_output_dir, verbose=False)"
8586
]
8687
},
@@ -111,16 +112,16 @@
111112
"model = model_module.get_model()\n",
112113
"model.require_sensitivities_for_all_parameters()\n",
113114
"model.set_timepoints(np.linspace(0, 10, 11))\n",
114-
"model.set_parameter_scale(amici.ParameterScaling.log10)\n",
115+
"model.set_parameter_scale(asd.ParameterScaling.log10)\n",
115116
"model.set_free_parameters([-0.3, -0.7])\n",
116117
"solver = model.create_solver()\n",
117-
"solver.set_sensitivity_method(amici.SensitivityMethod.forward)\n",
118-
"solver.set_sensitivity_order(amici.SensitivityOrder.first)\n",
118+
"solver.set_sensitivity_method(asd.SensitivityMethod.forward)\n",
119+
"solver.set_sensitivity_order(asd.SensitivityOrder.first)\n",
119120
"\n",
120121
"# how to run amici now:\n",
121-
"rdata = amici.run_simulation(model, solver, None)\n",
122-
"amici.plotting.plot_state_trajectories(rdata)\n",
123-
"edata = amici.ExpData(rdata, 0.2, 0.0)"
122+
"rdata = asd.run_simulation(model, solver, None)\n",
123+
"plot_state_trajectories(rdata)\n",
124+
"edata = asd.ExpData(rdata, 0.2, 0.0)"
124125
]
125126
},
126127
{

doc/example/getting_started.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"source": [
3131
"import logging\n",
3232
"\n",
33-
"import amici\n",
33+
"import amici.sim.sundials as asd\n",
3434
"import matplotlib as mpl\n",
3535
"import numpy as np\n",
3636
"\n",
@@ -517,7 +517,7 @@
517517
"source": [
518518
"# Set gradient computation method to adjoint\n",
519519
"problem.objective.amici_solver.set_sensitivity_method(\n",
520-
" amici.SensitivityMethod.adjoint\n",
520+
" asd.SensitivityMethod.adjoint\n",
521521
")"
522522
]
523523
},

doc/example/relative_data.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
"source": [
4141
"import time\n",
4242
"\n",
43-
"import amici\n",
4443
"import fides\n",
4544
"import matplotlib.pyplot as plt\n",
4645
"import numpy as np\n",
46+
"from amici.sim.sundials import SensitivityMethod\n",
4747
"from matplotlib.colors import to_rgba\n",
4848
"\n",
4949
"import pypesto\n",
@@ -177,7 +177,7 @@
177177
"problem = importer.create_problem(verbose=False)\n",
178178
"# set option to compute objective function gradients using adjoint sensitivity analysis\n",
179179
"problem.objective.amici_solver.set_sensitivity_method(\n",
180-
" amici.SensitivityMethod.adjoint\n",
180+
" SensitivityMethod.adjoint\n",
181181
")\n",
182182
"\n",
183183
"# ... and create another pypesto problem without hierarchical optimization (`problem2`)\n",
@@ -188,7 +188,7 @@
188188
")\n",
189189
"problem2 = importer2.create_problem(verbose=False)\n",
190190
"problem2.objective.amici_solver.set_sensitivity_method(\n",
191-
" amici.SensitivityMethod.adjoint\n",
191+
" SensitivityMethod.adjoint\n",
192192
")\n",
193193
"\n",
194194
"# Options for multi-start optimization\n",
@@ -393,7 +393,7 @@
393393
"start_time = time.time()\n",
394394
"problem.objective.calculator.inner_solver = AnalyticalInnerSolver()\n",
395395
"problem.objective.amici_solver.set_sensitivity_method(\n",
396-
" amici.SensitivityMethod_forward\n",
396+
" SensitivityMethod.forward\n",
397397
")\n",
398398
"result_ana_fw = pypesto.optimize.minimize(problem, **minimize_kwargs)\n",
399399
"print(f\"{result_ana_fw.optimize_result.get_for_key('fval')=}\")\n",

pypesto/hierarchical/base_problem.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .base_parameter import InnerParameter
1313

1414
try:
15-
import amici
15+
import amici.sim.sundials as asd
1616
import petab.v1 as petab
1717
from petab.v1.C import OBSERVABLE_ID, TIME
1818
except ImportError:
@@ -63,8 +63,8 @@ def __init__(self, xs: list[InnerParameter], data: list[np.ndarray]):
6363
@staticmethod
6464
def from_petab_amici(
6565
petab_problem: petab.Problem,
66-
amici_model: amici.Model,
67-
edatas: list[amici.ExpData],
66+
amici_model: asd.Model,
67+
edatas: list[asd.ExpData],
6868
) -> InnerProblem:
6969
"""Create an InnerProblem from a PEtab problem and AMICI objects."""
7070

@@ -176,10 +176,10 @@ class AmiciInnerProblem(InnerProblem):
176176
AMICI ``ExpDataView``s for each simulation condition.
177177
"""
178178

179-
def __init__(self, edatas: list[amici.ExpData], **kwargs):
179+
def __init__(self, edatas: list[asd.ExpData], **kwargs):
180180
super().__init__(**kwargs)
181181

182-
def check_edatas(self, edatas: list[amici.ExpData]) -> bool:
182+
def check_edatas(self, edatas: list[asd.ExpData]) -> bool:
183183
"""Check for consistency in data.
184184
185185
Currently only checks for the actual data values. e.g., timepoints are
@@ -197,9 +197,7 @@ def check_edatas(self, edatas: list[amici.ExpData]) -> bool:
197197
"""
198198
# TODO replace but edata1==edata2 once this makes it into amici
199199
# https://github.com/AMICI-dev/AMICI/issues/1880
200-
data = [
201-
amici.numpy.ExpDataView(edata)["measurements"] for edata in edatas
202-
]
200+
data = [asd.ExpDataView(edata)["measurements"] for edata in edatas]
203201

204202
# Mask the data using the inner problem mask. This is necessary
205203
# because the inner problem is aware of only the data it uses.

pypesto/hierarchical/inner_calculator_collector.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
)
4747

4848
try:
49-
import amici
49+
import amici.sim.sundials as asd
5050
import petab.v1 as petab
5151
from amici.importers.petab.v1.parameter_mapping import ParameterMapping
5252
except ImportError:
@@ -61,8 +61,8 @@
6161
SemiquantProblem,
6262
)
6363

64-
AmiciModel = Union["amici.Model", "amici.ModelPtr"]
65-
AmiciSolver = Union["amici.Solver", "amici.SolverPtr"]
64+
AmiciModel = Union["asd.Model", "asd.ModelPtr"]
65+
AmiciSolver = Union["asd.Solver", "asd.SolverPtr"]
6666

6767

6868
class InnerCalculatorCollector(AmiciCalculator):
@@ -92,7 +92,7 @@ def __init__(
9292
data_types: set[str],
9393
petab_problem: petab.Problem,
9494
model: AmiciModel,
95-
edatas: list[amici.ExpData],
95+
edatas: list[asd.ExpData],
9696
inner_options: dict,
9797
):
9898
super().__init__()
@@ -123,7 +123,7 @@ def construct_inner_calculators(
123123
self,
124124
petab_problem: petab.Problem,
125125
model: AmiciModel,
126-
edatas: list[amici.ExpData],
126+
edatas: list[asd.ExpData],
127127
inner_options: dict,
128128
):
129129
"""Construct inner calculators for each data type."""
@@ -221,12 +221,10 @@ def validate_options(self, inner_options: dict):
221221

222222
def _get_quantitative_data_mask(
223223
self,
224-
edatas: list[amici.ExpData],
224+
edatas: list[asd.ExpData],
225225
) -> list[np.ndarray]:
226226
# transform experimental data
227-
edatas = [
228-
amici.numpy.ExpDataView(edata)["measurements"] for edata in edatas
229-
]
227+
edatas = [asd.ExpDataView(edata)["measurements"] for edata in edatas]
230228

231229
quantitative_data_mask = [
232230
np.ones_like(edata, dtype=bool) for edata in edatas
@@ -301,7 +299,7 @@ def __call__(
301299
mode: ModeType,
302300
amici_model: AmiciModel,
303301
amici_solver: AmiciSolver,
304-
edatas: list[amici.ExpData],
302+
edatas: list[asd.ExpData],
305303
n_threads: int,
306304
x_ids: Sequence[str],
307305
parameter_mapping: ParameterMapping,
@@ -358,7 +356,7 @@ def __call__(
358356

359357
if (
360358
amici_solver.get_sensitivity_method()
361-
== amici.SensitivityMethod.adjoint
359+
== asd.SensitivityMethod.adjoint
362360
and any(
363361
data_type in self.data_types
364362
for data_type in [ORDINAL, CENSORED, SEMIQUANTITATIVE]
@@ -375,7 +373,7 @@ def __call__(
375373
# use the relative calculator directly.
376374
if (
377375
amici_solver.get_sensitivity_method()
378-
== amici.SensitivityMethod.adjoint
376+
== asd.SensitivityMethod.adjoint
379377
or 2 in sensi_orders
380378
or mode == MODE_RES
381379
):
@@ -429,7 +427,7 @@ def __call__(
429427
)
430428

431429
# run amici simulation
432-
rdatas = amici.run_simulations(
430+
rdatas = asd.run_simulations(
433431
amici_model,
434432
amici_solver,
435433
edatas,
@@ -438,7 +436,7 @@ def __call__(
438436

439437
# if any amici simulation failed, it's unlikely we can compute
440438
# meaningful inner parameters, so we better just fail early.
441-
if any(rdata.status != amici.AMICI_SUCCESS for rdata in rdatas):
439+
if any(rdata.status != asd.AMICI_SUCCESS for rdata in rdatas):
442440
ret = {
443441
FVAL: nllh,
444442
GRAD: snllh,
@@ -541,8 +539,8 @@ def __call__(
541539

542540

543541
def calculate_quantitative_result(
544-
rdatas: list[amici.ReturnDataView],
545-
edatas: list[amici.ExpData],
542+
rdatas: list[asd.ReturnDataView],
543+
edatas: list[asd.ExpData],
546544
sensi_orders: tuple[int],
547545
mode: ModeType,
548546
quantitative_data_mask: list[np.ndarray],
@@ -557,9 +555,7 @@ def calculate_quantitative_result(
557555
)
558556

559557
# transform experimental data
560-
edatas = [
561-
amici.numpy.ExpDataView(edata)["measurements"] for edata in edatas
562-
]
558+
edatas = [asd.ExpDataView(edata)["measurements"] for edata in edatas]
563559

564560
# calculate the function value
565561
for rdata, edata, mask in zip(rdatas, edatas, quantitative_data_mask):

0 commit comments

Comments
 (0)