From cb3d692ff9514dedd9ad45b399629be84c34228d Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 23 Mar 2026 15:18:24 +0100 Subject: [PATCH] Update for amici>=1.0 Adapt to API changes. Update URLs. Closes #661. --- .github/workflows/install_deps.sh | 2 +- .pre-commit-config.yaml | 2 +- doc/examples/petab_application.ipynb | 6 ++-- doc/examples/petab_yaml2sbml.ipynb | 14 +++++----- pyabc/petab/amici.py | 42 +++++++++++++++------------- pyproject.toml | 2 +- test/petab/test_petab.py | 8 +++--- test/petab/test_petab_suite.py | 12 ++++---- tox.ini | 3 ++ 9 files changed, 48 insertions(+), 43 deletions(-) diff --git a/.github/workflows/install_deps.sh b/.github/workflows/install_deps.sh index d497a6f3..4cdd3c8c 100755 --- a/.github/workflows/install_deps.sh +++ b/.github/workflows/install_deps.sh @@ -70,7 +70,7 @@ install_r() { install_amici() { log_info "Installing AMICI dependencies..." if ! is_macos; then - apt_install swig libatlas-base-dev libhdf5-serial-dev libboost-all-dev + apt_install libhdf5-serial-dev libboost-all-dev fi log_info "Installing AMICI Python package..." python -m pip uninstall -y amici pyabc || true diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1487ed83..baa15b38 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -61,7 +61,7 @@ repos: # Configuration default_language_version: - python: python3.11 + python: python3.13 # Global exclusions exclude: | diff --git a/doc/examples/petab_application.ipynb b/doc/examples/petab_application.ipynb index 7332092e..cf7e13b2 100644 --- a/doc/examples/petab_application.ipynb +++ b/doc/examples/petab_application.ipynb @@ -31,7 +31,7 @@ "outputs": [], "source": [ "import petab.v1 as petab\n", - "from amici.petab import import_petab_problem\n", + "from amici.importers.petab.v1 import import_petab_problem\n", "\n", "from pyabc.petab import AmiciPetabImporter" ] @@ -91,7 +91,7 @@ "model = import_petab_problem(petab_problem)\n", "\n", "# the solver to numerically solve the ODE\n", - "solver = model.getSolver()\n", + "solver = model.create_solver()\n", "\n", "# import everything to pyABC\n", "importer = AmiciPetabImporter(petab_problem, model, solver)\n", @@ -197,7 +197,7 @@ "acceptor = pyabc.StochasticAcceptor(\n", " pdf_norm_method = pyabc.ScaledPDFNorm())\n", "\n", - "abc = pyabc.ABCSMC(model, prior, kernel, \n", + "abc = pyabc.ABCSMC(model, prior, kernel,\n", " eps=temperature,\n", " acceptor=acceptor,\n", " sampler=sampler,\n", diff --git a/doc/examples/petab_yaml2sbml.ipynb b/doc/examples/petab_yaml2sbml.ipynb index 8c595784..b395a419 100644 --- a/doc/examples/petab_yaml2sbml.ipynb +++ b/doc/examples/petab_yaml2sbml.ipynb @@ -36,7 +36,7 @@ "\n", "import amici\n", "import numpy as np\n", - "from amici.petab import import_petab_problem\n", + "from amici.importers.petab.v1 import import_petab_problem\n", "\n", "import pyabc\n", "import pyabc.petab\n", @@ -219,13 +219,13 @@ " sys.path.insert(0, os.path.abspath(amici_dir))\n", "model = import_petab_problem(\n", " petab_problem,\n", - " model_output_dir=amici_dir,\n", + " output_dir=amici_dir,\n", " verbose=False,\n", " generate_sensitivity_code=False,\n", ")\n", "\n", "# the solver to numerically solve the ODE\n", - "solver = model.getSolver()\n", + "solver = model.create_solver()\n", "\n", "# import everything to pyABC\n", "importer = pyabc.petab.AmiciPetabImporter(petab_problem, model, solver)\n", @@ -421,16 +421,16 @@ " if amici_dir not in sys.path:\n", " sys.path.insert(0, os.path.abspath(amici_dir))\n", " model_module = importlib.import_module(model_name)\n", - " model = model_module.getModel()\n", - " solver = model.getSolver()\n", + " model = model_module.get_model()\n", + " solver = model.create_solver()\n", "\n", " # measurement times\n", " n_time = 10\n", " meas_times = np.linspace(0, 10, n_time)\n", - " model.setTimepoints(meas_times)\n", + " model.set_timepoints(meas_times)\n", "\n", " # simulate with nominal parameters\n", - " rdata = amici.runAmiciSimulation(model, solver)\n", + " rdata = model.simulate(solver=solver)\n", "\n", " # create noisy data\n", " np.random.seed(2)\n", diff --git a/pyabc/petab/amici.py b/pyabc/petab/amici.py index 23054375..f67c8ab4 100644 --- a/pyabc/petab/amici.py +++ b/pyabc/petab/amici.py @@ -1,5 +1,7 @@ """PEtab import with AMICI simulator.""" +from __future__ import annotations + import copy import logging import os @@ -18,18 +20,20 @@ except ImportError: petab = C = None logger.error( - 'Install PEtab (see https://github.com/icb-dcm/petab) to use ' + 'Install the PEtab library ' + '(see https://github.com/PEtab-dev/libpetab-python/) to use ' 'the petab functionality, e.g. via `pip install pyabc[petab]`' ) try: import amici - from amici.petab import petab_import as amici_petab_import - from amici.petab.simulations import LLH, RDATAS, simulate_petab + import amici.sim.sundials as asd + from amici.importers.petab.v1 import import_petab_problem + from amici.sim.sundials.petab.v1 import LLH, RDATAS, simulate_petab except ImportError: - amici = amici_petab_import = simulate_petab = LLH = RDATAS = None + amici = import_petab_problem = simulate_petab = LLH = RDATAS = None logger.error( - 'Install amici (see https://github.com/icb-dcm/amici) to use ' + 'Install amici (see https://github.com/AMICI-dev/AMICI/) to use ' 'the amici functionality, e.g. via `pip install pyabc[amici]`' ) @@ -113,7 +117,7 @@ def __getstate__(self) -> dict: try: # write amici solver settings to file try: - amici.writeSolverSettingsToHDF5(self.amici_solver, _file) + asd.write_solver_settings_to_hdf5(self.amici_solver, _file) except AttributeError as e: e.args += ( 'Pickling the AmiciObjective requires an AMICI ' @@ -133,8 +137,8 @@ def __getstate__(self) -> dict: def __setstate__(self, state: dict): self.__dict__.update(state) - model = amici_petab_import.import_petab_problem(self.petab_problem) - solver = model.getSolver() + model = import_petab_problem(self.petab_problem) + solver = model.create_solver() _fd, _file = tempfile.mkstemp() try: @@ -143,7 +147,7 @@ def __setstate__(self, state: dict): f.write(state['amici_solver_settings']) # read in solver settings try: - amici.readSolverSettingsFromHDF5(_file, solver) + asd.read_solver_settings_from_hdf5(_file, solver) except AttributeError as err: if not err.args: err.args = ('',) @@ -173,28 +177,26 @@ class AmiciPetabImporter(PetabImporter): amici_model: A corresponding compiled AMICI model that allows simulating data for parameters. If not provided, one is created using - `amici.petab_import.import_petab_problem`. + `amici.importers.petab.v1.import_petab_problem`. amici_solver: An AMICI solver to simulate the model. If not provided, one is created - using `amici_model.getSolver()`. + using `amici_model.create_solver()`. """ def __init__( self, petab_problem: petab.Problem, - amici_model: 'amici.Model' = None, - amici_solver: 'amici.Solver' = None, + amici_model: amici.sim.sundials.Model = None, + amici_solver: amici.sim.sundials.Solver = None, ): super().__init__(petab_problem=petab_problem) if amici_model is None: - amici_model = amici_petab_import.import_petab_problem( - petab_problem - ) + amici_model = import_petab_problem(petab_problem) self.amici_model = amici_model if amici_solver is None: - amici_solver = self.amici_model.getSolver() + amici_solver = self.amici_model.create_solver() self.amici_solver = amici_solver def create_model( @@ -214,8 +216,8 @@ def create_model( Whether to return the simulations also (large, can be stored in database). return_rdatas: - Whether to return the full `List[amici.ExpData]` objects (large, - cannot be stored in database). + Whether to return the full `list[amici.sim.sundials.ExpData]` + objects (large, cannot be stored in database). Returns ------- @@ -237,7 +239,7 @@ def create_model( raise AssertionError('Parameter id mismatch') # no gradients for pyabc - self.amici_solver.setSensitivityOrder(0) + self.amici_solver.set_sensitivity_order(asd.SensitivityOrder.none) model = AmiciModel( petab_problem=self.petab_problem, diff --git a/pyproject.toml b/pyproject.toml index 3bd82587..d753ace7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,7 +93,7 @@ ot = [ ] petab = ["petab>=0.2.0"] #petab-test = ["petabtests>=0.0.1"] # problem with pysb -amici = ["amici>=0.32.0,<1.0.0"] +amici = ["amici>=1.0.0"] yaml2sbml = ["yaml2sbml>=0.2.1"] migrate = ["alembic>=1.5.4"] plotly = ["plotly>=5.3.1", "kaleido>=0.2.1"] diff --git a/test/petab/test_petab.py b/test/petab/test_petab.py index 3e6a050e..326aab4b 100644 --- a/test/petab/test_petab.py +++ b/test/petab/test_petab.py @@ -2,7 +2,6 @@ import os import sys -import amici.petab.petab_import import cloudpickle as pickle import git import matplotlib.pyplot as plt @@ -12,6 +11,7 @@ import petab.v1.C as C import pytest import scipy.stats +from amici.importers.petab.v1 import import_petab_problem import pyabc.petab import pyabc.petab.base @@ -322,12 +322,12 @@ def boehm_model_importer(): output_folder = f'amici_models/{model_name}' if output_folder not in sys.path: sys.path.insert(0, output_folder) - model = amici.petab.petab_import.import_petab_problem( + model = import_petab_problem( petab_problem, - model_output_dir=output_folder, + output_dir=output_folder, generate_sensitivity_code=False, ) - solver = model.getSolver() + solver = model.create_solver() # import to pyabc importer = pyabc.petab.AmiciPetabImporter(petab_problem, model, solver) diff --git a/test/petab/test_petab_suite.py b/test/petab/test_petab_suite.py index d0f1bfa4..78068a7a 100644 --- a/test/petab/test_petab_suite.py +++ b/test/petab/test_petab_suite.py @@ -9,10 +9,10 @@ import pyabc try: - import amici.petab.petab_import - import amici.petab.simulations import petab.v1 as petab import petabtests + from amici.importers.petab.v1 import import_petab_problem + from amici.sim.sundials.petab.v1 import rdatas_to_measurement_df import pyabc.petab except ImportError: @@ -104,13 +104,13 @@ def _execute_case(case): # models with the same name in a single python session model_name = f'petab_{MODEL_TYPE}_test_case_{case}_{PETAB_VERSION.replace(".", "_")}' - amici_model = amici.petab.petab_import.import_petab_problem( + amici_model = import_petab_problem( petab_problem=petab_problem, model_name=model_name, - model_output_dir=output_folder, + output_dir=output_folder, generate_sensitivity_code=False, ) - solver = amici_model.getSolver() + solver = amici_model.create_solver() # import to pyabc importer = pyabc.petab.AmiciPetabImporter( @@ -127,7 +127,7 @@ def _execute_case(case): # extract results rdatas = ret['rdatas'] chi2 = sum(rdata['chi2'] for rdata in rdatas) - simulation_df = amici.petab.simulations.rdatas_to_measurement_df( + simulation_df = rdatas_to_measurement_df( rdatas, amici_model, importer.petab_problem.measurement_df ) petab.check_measurement_df( diff --git a/tox.ini b/tox.ini index 916404fa..595a2224 100644 --- a/tox.ini +++ b/tox.ini @@ -99,6 +99,9 @@ description = Test external model simulators (Julia, COPASI) [testenv:petab] +deps = + git+https://github.com/PEtab-dev/petab_test_suite@main + git+https://github.com/Benchmarking-Initiative/Benchmark-Models-PEtab.git@master\#subdirectory=src/python&egg=benchmark_models_petab extras = test petab