Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/install_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ repos:

# Configuration
default_language_version:
python: python3.11
python: python3.13

# Global exclusions
exclude: |
Expand Down
6 changes: 3 additions & 3 deletions doc/examples/petab_application.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions doc/examples/petab_yaml2sbml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
42 changes: 22 additions & 20 deletions pyabc/petab/amici.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""PEtab import with AMICI simulator."""

from __future__ import annotations

import copy
import logging
import os
Expand All @@ -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]`'
)

Expand Down Expand Up @@ -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 '
Expand All @@ -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:
Expand All @@ -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 = ('',)
Expand Down Expand Up @@ -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(
Expand All @@ -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
-------
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
8 changes: 4 additions & 4 deletions test/petab/test_petab.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import sys

import amici.petab.petab_import
import cloudpickle as pickle
import git
import matplotlib.pyplot as plt
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions test/petab/test_petab_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading