Skip to content

Commit 8ed2e00

Browse files
authored
Update for amici>=1.0 (#663)
Update for amici>=1.0 Closes #661.
1 parent 4b0e60a commit 8ed2e00

9 files changed

Lines changed: 48 additions & 43 deletions

File tree

.github/workflows/install_deps.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ install_r() {
7070
install_amici() {
7171
log_info "Installing AMICI dependencies..."
7272
if ! is_macos; then
73-
apt_install swig libatlas-base-dev libhdf5-serial-dev libboost-all-dev
73+
apt_install libhdf5-serial-dev libboost-all-dev
7474
fi
7575
log_info "Installing AMICI Python package..."
7676
python -m pip uninstall -y amici pyabc || true

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ repos:
6161

6262
# Configuration
6363
default_language_version:
64-
python: python3.11
64+
python: python3.13
6565

6666
# Global exclusions
6767
exclude: |

doc/examples/petab_application.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"outputs": [],
3232
"source": [
3333
"import petab.v1 as petab\n",
34-
"from amici.petab import import_petab_problem\n",
34+
"from amici.importers.petab.v1 import import_petab_problem\n",
3535
"\n",
3636
"from pyabc.petab import AmiciPetabImporter"
3737
]
@@ -91,7 +91,7 @@
9191
"model = import_petab_problem(petab_problem)\n",
9292
"\n",
9393
"# the solver to numerically solve the ODE\n",
94-
"solver = model.getSolver()\n",
94+
"solver = model.create_solver()\n",
9595
"\n",
9696
"# import everything to pyABC\n",
9797
"importer = AmiciPetabImporter(petab_problem, model, solver)\n",
@@ -197,7 +197,7 @@
197197
"acceptor = pyabc.StochasticAcceptor(\n",
198198
" pdf_norm_method = pyabc.ScaledPDFNorm())\n",
199199
"\n",
200-
"abc = pyabc.ABCSMC(model, prior, kernel, \n",
200+
"abc = pyabc.ABCSMC(model, prior, kernel,\n",
201201
" eps=temperature,\n",
202202
" acceptor=acceptor,\n",
203203
" sampler=sampler,\n",

doc/examples/petab_yaml2sbml.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"\n",
3737
"import amici\n",
3838
"import numpy as np\n",
39-
"from amici.petab import import_petab_problem\n",
39+
"from amici.importers.petab.v1 import import_petab_problem\n",
4040
"\n",
4141
"import pyabc\n",
4242
"import pyabc.petab\n",
@@ -219,13 +219,13 @@
219219
" sys.path.insert(0, os.path.abspath(amici_dir))\n",
220220
"model = import_petab_problem(\n",
221221
" petab_problem,\n",
222-
" model_output_dir=amici_dir,\n",
222+
" output_dir=amici_dir,\n",
223223
" verbose=False,\n",
224224
" generate_sensitivity_code=False,\n",
225225
")\n",
226226
"\n",
227227
"# the solver to numerically solve the ODE\n",
228-
"solver = model.getSolver()\n",
228+
"solver = model.create_solver()\n",
229229
"\n",
230230
"# import everything to pyABC\n",
231231
"importer = pyabc.petab.AmiciPetabImporter(petab_problem, model, solver)\n",
@@ -421,16 +421,16 @@
421421
" if amici_dir not in sys.path:\n",
422422
" sys.path.insert(0, os.path.abspath(amici_dir))\n",
423423
" model_module = importlib.import_module(model_name)\n",
424-
" model = model_module.getModel()\n",
425-
" solver = model.getSolver()\n",
424+
" model = model_module.get_model()\n",
425+
" solver = model.create_solver()\n",
426426
"\n",
427427
" # measurement times\n",
428428
" n_time = 10\n",
429429
" meas_times = np.linspace(0, 10, n_time)\n",
430-
" model.setTimepoints(meas_times)\n",
430+
" model.set_timepoints(meas_times)\n",
431431
"\n",
432432
" # simulate with nominal parameters\n",
433-
" rdata = amici.runAmiciSimulation(model, solver)\n",
433+
" rdata = model.simulate(solver=solver)\n",
434434
"\n",
435435
" # create noisy data\n",
436436
" np.random.seed(2)\n",

pyabc/petab/amici.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""PEtab import with AMICI simulator."""
22

3+
from __future__ import annotations
4+
35
import copy
46
import logging
57
import os
@@ -18,18 +20,20 @@
1820
except ImportError:
1921
petab = C = None
2022
logger.error(
21-
'Install PEtab (see https://github.com/icb-dcm/petab) to use '
23+
'Install the PEtab library '
24+
'(see https://github.com/PEtab-dev/libpetab-python/) to use '
2225
'the petab functionality, e.g. via `pip install pyabc[petab]`'
2326
)
2427

2528
try:
2629
import amici
27-
from amici.petab import petab_import as amici_petab_import
28-
from amici.petab.simulations import LLH, RDATAS, simulate_petab
30+
import amici.sim.sundials as asd
31+
from amici.importers.petab.v1 import import_petab_problem
32+
from amici.sim.sundials.petab.v1 import LLH, RDATAS, simulate_petab
2933
except ImportError:
30-
amici = amici_petab_import = simulate_petab = LLH = RDATAS = None
34+
amici = import_petab_problem = simulate_petab = LLH = RDATAS = None
3135
logger.error(
32-
'Install amici (see https://github.com/icb-dcm/amici) to use '
36+
'Install amici (see https://github.com/AMICI-dev/AMICI/) to use '
3337
'the amici functionality, e.g. via `pip install pyabc[amici]`'
3438
)
3539

@@ -113,7 +117,7 @@ def __getstate__(self) -> dict:
113117
try:
114118
# write amici solver settings to file
115119
try:
116-
amici.writeSolverSettingsToHDF5(self.amici_solver, _file)
120+
asd.write_solver_settings_to_hdf5(self.amici_solver, _file)
117121
except AttributeError as e:
118122
e.args += (
119123
'Pickling the AmiciObjective requires an AMICI '
@@ -133,8 +137,8 @@ def __getstate__(self) -> dict:
133137
def __setstate__(self, state: dict):
134138
self.__dict__.update(state)
135139

136-
model = amici_petab_import.import_petab_problem(self.petab_problem)
137-
solver = model.getSolver()
140+
model = import_petab_problem(self.petab_problem)
141+
solver = model.create_solver()
138142

139143
_fd, _file = tempfile.mkstemp()
140144
try:
@@ -143,7 +147,7 @@ def __setstate__(self, state: dict):
143147
f.write(state['amici_solver_settings'])
144148
# read in solver settings
145149
try:
146-
amici.readSolverSettingsFromHDF5(_file, solver)
150+
asd.read_solver_settings_from_hdf5(_file, solver)
147151
except AttributeError as err:
148152
if not err.args:
149153
err.args = ('',)
@@ -173,28 +177,26 @@ class AmiciPetabImporter(PetabImporter):
173177
amici_model:
174178
A corresponding compiled AMICI model that allows simulating data for
175179
parameters. If not provided, one is created using
176-
`amici.petab_import.import_petab_problem`.
180+
`amici.importers.petab.v1.import_petab_problem`.
177181
amici_solver:
178182
An AMICI solver to simulate the model. If not provided, one is created
179-
using `amici_model.getSolver()`.
183+
using `amici_model.create_solver()`.
180184
"""
181185

182186
def __init__(
183187
self,
184188
petab_problem: petab.Problem,
185-
amici_model: 'amici.Model' = None,
186-
amici_solver: 'amici.Solver' = None,
189+
amici_model: amici.sim.sundials.Model = None,
190+
amici_solver: amici.sim.sundials.Solver = None,
187191
):
188192
super().__init__(petab_problem=petab_problem)
189193

190194
if amici_model is None:
191-
amici_model = amici_petab_import.import_petab_problem(
192-
petab_problem
193-
)
195+
amici_model = import_petab_problem(petab_problem)
194196
self.amici_model = amici_model
195197

196198
if amici_solver is None:
197-
amici_solver = self.amici_model.getSolver()
199+
amici_solver = self.amici_model.create_solver()
198200
self.amici_solver = amici_solver
199201

200202
def create_model(
@@ -214,8 +216,8 @@ def create_model(
214216
Whether to return the simulations also (large, can be stored
215217
in database).
216218
return_rdatas:
217-
Whether to return the full `List[amici.ExpData]` objects (large,
218-
cannot be stored in database).
219+
Whether to return the full `list[amici.sim.sundials.ExpData]`
220+
objects (large, cannot be stored in database).
219221
220222
Returns
221223
-------
@@ -237,7 +239,7 @@ def create_model(
237239
raise AssertionError('Parameter id mismatch')
238240

239241
# no gradients for pyabc
240-
self.amici_solver.setSensitivityOrder(0)
242+
self.amici_solver.set_sensitivity_order(asd.SensitivityOrder.none)
241243

242244
model = AmiciModel(
243245
petab_problem=self.petab_problem,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ ot = [
9393
]
9494
petab = ["petab>=0.2.0"]
9595
#petab-test = ["petabtests>=0.0.1"] # problem with pysb
96-
amici = ["amici>=0.32.0,<1.0.0"]
96+
amici = ["amici>=1.0.0"]
9797
yaml2sbml = ["yaml2sbml>=0.2.1"]
9898
migrate = ["alembic>=1.5.4"]
9999
plotly = ["plotly>=5.3.1", "kaleido>=0.2.1"]

test/petab/test_petab.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33
import sys
44

5-
import amici.petab.petab_import
65
import cloudpickle as pickle
76
import git
87
import matplotlib.pyplot as plt
@@ -12,6 +11,7 @@
1211
import petab.v1.C as C
1312
import pytest
1413
import scipy.stats
14+
from amici.importers.petab.v1 import import_petab_problem
1515

1616
import pyabc.petab
1717
import pyabc.petab.base
@@ -322,12 +322,12 @@ def boehm_model_importer():
322322
output_folder = f'amici_models/{model_name}'
323323
if output_folder not in sys.path:
324324
sys.path.insert(0, output_folder)
325-
model = amici.petab.petab_import.import_petab_problem(
325+
model = import_petab_problem(
326326
petab_problem,
327-
model_output_dir=output_folder,
327+
output_dir=output_folder,
328328
generate_sensitivity_code=False,
329329
)
330-
solver = model.getSolver()
330+
solver = model.create_solver()
331331

332332
# import to pyabc
333333
importer = pyabc.petab.AmiciPetabImporter(petab_problem, model, solver)

test/petab/test_petab_suite.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import pyabc
1010

1111
try:
12-
import amici.petab.petab_import
13-
import amici.petab.simulations
1412
import petab.v1 as petab
1513
import petabtests
14+
from amici.importers.petab.v1 import import_petab_problem
15+
from amici.sim.sundials.petab.v1 import rdatas_to_measurement_df
1616

1717
import pyabc.petab
1818
except ImportError:
@@ -104,13 +104,13 @@ def _execute_case(case):
104104
# models with the same name in a single python session
105105
model_name = f'petab_{MODEL_TYPE}_test_case_{case}_{PETAB_VERSION.replace(".", "_")}'
106106

107-
amici_model = amici.petab.petab_import.import_petab_problem(
107+
amici_model = import_petab_problem(
108108
petab_problem=petab_problem,
109109
model_name=model_name,
110-
model_output_dir=output_folder,
110+
output_dir=output_folder,
111111
generate_sensitivity_code=False,
112112
)
113-
solver = amici_model.getSolver()
113+
solver = amici_model.create_solver()
114114

115115
# import to pyabc
116116
importer = pyabc.petab.AmiciPetabImporter(
@@ -127,7 +127,7 @@ def _execute_case(case):
127127
# extract results
128128
rdatas = ret['rdatas']
129129
chi2 = sum(rdata['chi2'] for rdata in rdatas)
130-
simulation_df = amici.petab.simulations.rdatas_to_measurement_df(
130+
simulation_df = rdatas_to_measurement_df(
131131
rdatas, amici_model, importer.petab_problem.measurement_df
132132
)
133133
petab.check_measurement_df(

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ description =
9999
Test external model simulators (Julia, COPASI)
100100

101101
[testenv:petab]
102+
deps =
103+
git+https://github.com/PEtab-dev/petab_test_suite@main
104+
git+https://github.com/Benchmarking-Initiative/Benchmark-Models-PEtab.git@master\#subdirectory=src/python&egg=benchmark_models_petab
102105
extras =
103106
test
104107
petab

0 commit comments

Comments
 (0)