"""PEtab v2 simulation."""
from __future__ import annotations
import logging
import numbers
from bisect import bisect_left
from collections import Counter
from collections.abc import Mapping, Sequence
from dataclasses import dataclass, field
import numpy as np
import sympy as sp
from petab import v2 as v2
from petab.v2 import ExperimentPeriod
import amici
from amici.logging import get_logger
from amici.sim._parameter_scales import ParameterScale, get_parameter_scales
from amici.sim.sundials import (
ParameterScaling,
RDataReporting,
SensitivityMethod,
SensitivityOrder,
parameter_scaling_from_int_vector,
)
from .v1._parameter_scaling import (
petab_to_amici_scale,
scale_parameter,
unscale_parameter,
)
logger = get_logger(__name__, log_level=logging.INFO)
__all__ = [
"PetabSimulator",
"ExperimentManager",
"PetabSimulationResult",
]
[docs]
class ExperimentManager:
"""
Handles the creation of :class:`ExpData` objects for a given model and
PEtab problem.
The assumption is that we have a set of :class:`ExpData` objects,
one for each PEtab experiment.
Those are updated based on a set of global parameters (PEtab
problem parameters, as opposed to model parameters for a single experiment
period).
PEtab v2 does not specify on which scale parameters are estimated.
By default, all problem parameters are on linear scale. Other scales can
be chosen via the `parameter_scales` argument. All problem parameter
values passed to or returned by this class are then on the respective
scale, and so are the sensitivities computed for the resulting
:class:`ExpData` objects.
"""
# TODO debug, remove
_debug = False
[docs]
def __init__(
self,
model: amici.sim.sundials.Model,
petab_problem: v2.Problem,
*,
parameter_scales: Mapping[str, ParameterScale] | None = None,
):
"""
Initialize the `ExperimentManager`.
:param model: The AMICI model to use.
:param petab_problem: The PEtab problem to use.
This is expected to be the output of
:class:`petab.v2.converters.ExperimentsToSbmlConverter` or an
equivalent problem.
This object must not be modified after the creation of this
:class:`ExperimentManager` instance.
:param parameter_scales:
The scales of the PEtab problem parameters, as a mapping from
problem parameter IDs to ``"lin"``, ``"log"`` (natural
logarithm), or ``"log10"``. Parameters not included are on
linear scale.
"""
self._model: amici.sim.sundials.Model = model
self._petab_problem: v2.Problem = petab_problem
self._parameter_scales: dict[str, ParameterScale] = (
get_parameter_scales(petab_problem, parameter_scales)
)
# problem parameter IDs to AMICI parameter scales
self._amici_scales: dict[str, ParameterScaling] = {
par_id: petab_to_amici_scale(scale)
for par_id, scale in self._parameter_scales.items()
}
self._state_ids: tuple[str, ...] = tuple(self._model.get_state_ids())
self._free_parameter_ids: tuple[str, ...] = tuple(
self._model.get_free_parameter_ids()
)
self._fixed_parameter_ids: tuple[str, ...] = tuple(
self._model.get_fixed_parameter_ids()
)
# maps parameter IDs to parameter indices in the model
self._pid_to_idx: dict[str, int] = {
id_: i for i, id_ in enumerate(self._free_parameter_ids)
}
self._fixed_pid_to_idx: dict[str, int] = {
id_: i for i, id_ in enumerate(self._fixed_parameter_ids)
}
# maps model observable IDs to observable indices in the model
self._obs_id_to_idx: dict[str, int] = {
id_: i for i, id_ in enumerate(self._model.get_observable_ids())
}
# Lookup tables for the PEtab problem, which must not change after
# this point.
# maps PEtab observable IDs to petab Observable instances
self._petab_id_to_obs: dict[str, v2.Observable] = {
obs.id: obs for obs in self._petab_problem.observables
}
self._id_to_experiment: dict[str, v2.Experiment] = {
exp.id: exp for exp in self._petab_problem.experiments
}
self._id_to_condition: dict[str, v2.Condition] = {
cond.id: cond for cond in self._petab_problem.conditions
}
self._experiment_id_to_measurements: dict[
str | None, list[v2.Measurement]
] = {}
for measurement in self._petab_problem.measurements:
self._experiment_id_to_measurements.setdefault(
measurement.experiment_id, []
).append(measurement)
self._x_ids: frozenset[str] = frozenset(self._petab_problem.x_ids)
# estimated problem parameter IDs to their indices in
# `Problem.x_free_ids`
self._x_free_id_to_idx: dict[str, int] = {
pid: i for i, pid in enumerate(self._petab_problem.x_free_ids)
}
# Indices of free model parameters that are estimated problem
# parameters, mapped to the indices of the latter in
# `Problem.x_free_ids`, in ascending order of the model parameter
# index. Output parameter placeholders are experiment-specific and
# not included.
self._model_par_idx_to_x_free_idx: dict[int, int] = {
model_par_idx: x_free_idx
for model_par_idx, model_par_id in enumerate(
self._free_parameter_ids
)
if (x_free_idx := self._x_free_id_to_idx.get(model_par_id))
is not None
}
# (problem parameter ID, model parameter index) for all problem
# parameters that are free model parameters
self._problem_par_to_free_model_par: list[tuple[str, int]] = [
(pid, idx)
for pid in self._petab_problem.x_ids
if (idx := self._pid_to_idx.get(pid)) is not None
]
# (problem parameter ID, model parameter index) for all problem
# parameters that are fixed model parameters
self._problem_par_to_fixed_model_par: list[tuple[str, int]] = [
(pid, idx)
for pid in self._petab_problem.x_ids
if (idx := self._fixed_pid_to_idx.get(pid)) is not None
]
# create a new model instance from the model module from which
# we can get the default parameters
model0 = model.module.get_model()
self._original_p = np.array(model0.get_free_parameters())
self._original_k = np.array(model0.get_fixed_parameters())
[docs]
def create_edatas(
self, problem_parameters: dict[str, float] | None = None
) -> list[amici.sim.sundials.ExpData]:
"""Create ExpData objects for all experiments.
:param problem_parameters:
Optional dictionary of problem parameters to apply to the
`ExpData` objects, on the scales given by :attr:`parameter_scales`.
If `None`, the nominal parameters of the PEtab problem are used.
:return:
The created `ExpData` objects, in the order of the experiments
in the PEtab problem.
"""
if problem_parameters is None:
problem_parameters = self.scale_parameters(
self._petab_problem.get_x_nominal_dict()
)
return [
self.create_edata(experiment, problem_parameters)
for experiment in self._petab_problem.experiments
]
[docs]
def create_edata(
self,
experiment: v2.core.Experiment | str | None,
problem_parameters: dict[str, float] | None = None,
) -> amici.sim.sundials.ExpData:
"""Create an ExpData object for a single experiment.
Sets timepoints, measurements, initial conditions, ... based on the
given experiment and the nominal parameters of the PEtab problem.
:param experiment:
The experiment or experiment ID to create the `ExpData` for.
:param problem_parameters:
Optional dictionary of problem parameters to apply to the
`ExpData`, on the scales given by :attr:`parameter_scales`.
If `None`, the nominal parameters of the PEtab problem are used.
:return:
The created `ExpData` object for the given experiment.
"""
from amici.sim.sundials import ExpData
if isinstance(experiment, str):
experiment = self._id_to_experiment[experiment]
if len(experiment.periods) > 2:
raise AssertionError(
f"Expected <= 2 periods, got {len(experiment.periods)} "
f"for experiment {experiment.id}."
)
edata = ExpData(self._model)
edata.id = experiment.id
self._set_constants(edata, experiment)
self._set_timepoints_and_measurements(edata, experiment)
if self._debug:
logger.debug(
f"Created ExpData id={edata.id}, "
f"k_preeq={edata.fixed_parameters_pre_equilibration}, "
f"k={edata.fixed_parameters}"
)
if problem_parameters is None:
problem_parameters = self.scale_parameters(
self._petab_problem.get_x_nominal_dict()
)
self.apply_parameters(edata, problem_parameters=problem_parameters)
return edata
def _set_constants(
self, edata: amici.sim.sundials.ExpData, experiment: v2.core.Experiment
) -> None:
"""
Set constant parameters for the given experiment.
:param edata:
The ExpData instance to set the constants for.
:param experiment:
The PEtab experiment to set the constants for.
"""
# After converting experiments to events, all remaining
# condition parameters are constants.
if not experiment.periods:
# No periods, no changes to apply.
# Use the original fixed parameters that are encoded in the model.
return
def get_k(period: ExperimentPeriod):
"""Get the fixed parameters for the period."""
fixed_pars_vals = self._original_k.copy()
for condition_id in period.condition_ids:
for change in self._id_to_condition[condition_id].changes:
pid = self._fixed_pid_to_idx[change.target_id]
# those are only indicator variables that are always
# number literals
fixed_pars_vals[pid] = change.target_value
return fixed_pars_vals
if experiment.sorted_periods[0].time == -np.inf:
# pre-equilibration period
edata.fixed_parameters_pre_equilibration = get_k(
experiment.sorted_periods[0]
)
# In PEtab, pre-equilibration always starts at t=0, since SBML
# does not support specifying a different start time (yet).
edata.t_start_preeq = 0
if len(experiment.periods) >= int(1 + experiment.has_preequilibration):
# simulation period
main_period = experiment.sorted_periods[
int(experiment.has_preequilibration)
]
edata.fixed_parameters = get_k(main_period)
edata.t_start = main_period.time
def _set_timepoints_and_measurements(
self, edata: amici.sim.sundials.ExpData, experiment: v2.core.Experiment
) -> None:
"""
Set timepoints and measurements for the given experiment.
:param edata:
The `ExpData` instance to update.
:param experiment:
The PEtab experiment to set the timepoints and measurements for.
"""
# Get the required time points: this is the superset of timepoints
# of the measurements of all observables, including the different
# replicates
measurements = self._get_measurements(experiment.id)
# number of replicates for each (observable ID, time) pair
n_replicates = Counter((m.observable_id, m.time) for m in measurements)
# each time point is needed as often as the maximum number of
# replicates of any observable at that time point
max_counter = Counter()
for (_, t), count in n_replicates.items():
max_counter[t] = max(max_counter[t], count)
timepoints_w_reps = sorted(max_counter.elements())
edata.set_timepoints(timepoints_w_reps)
# measurements and sigmas
y, sigma_y = self._get_measurements_and_sigmas(
measurements=measurements,
timepoints_w_reps=timepoints_w_reps,
observable_id_to_idx=self._obs_id_to_idx,
)
edata.set_measurements(y.flatten())
edata.set_noise_scales(sigma_y.flatten())
def _get_measurements_and_sigmas(
self,
measurements: Sequence[v2.Measurement],
timepoints_w_reps: Sequence[numbers.Number],
observable_id_to_idx: Mapping[str, int],
) -> tuple[np.ndarray, np.ndarray]:
"""
Get measurements and sigmas
Generate arrays with measured values and sigmas in AMICI format from a
list of PEtab measurements.
:param measurements:
Subset of PEtab measurement table for one experiment
:param timepoints_w_reps:
Sorted timepoints for which there exist measurements, including
replicates.
:param observable_id_to_idx:
Mapping of all model observable IDs to observable indices.
:return:
arrays for measurement and sigmas
"""
# prepare measurement matrix
y = np.full(
shape=(len(timepoints_w_reps), len(observable_id_to_idx)),
fill_value=np.nan,
)
# prepare sigma matrix
sigma_y = y.copy()
time_to_meas = {}
for m in measurements:
time_to_meas.setdefault(m.time, []).append(m)
for time in sorted(time_to_meas):
# subselect for time
time_ix_0 = bisect_left(timepoints_w_reps, time)
# remember used time indices for each observable
time_ix_for_obs_ix = {}
# iterate over measurements
for m in time_to_meas[time]:
# extract observable index
observable_ix = observable_id_to_idx[m.observable_id]
# update time index for observable
if observable_ix in time_ix_for_obs_ix:
time_ix_for_obs_ix[observable_ix] += 1
else:
time_ix_for_obs_ix[observable_ix] = time_ix_0
# fill observable and possibly noise parameter
y[time_ix_for_obs_ix[observable_ix], observable_ix] = (
m.measurement
)
if (
len(m.noise_parameters) == 1
and m.noise_parameters[0].is_Number
):
sigma_y[
time_ix_for_obs_ix[observable_ix], observable_ix
] = float(m.noise_parameters[0])
return y, sigma_y
[docs]
def apply_parameters(
self,
edata: amici.sim.sundials.ExpData,
problem_parameters: dict[str, float],
) -> None:
"""Apply problem parameters.
Update the parameter-dependent values of the given `ExpData` instance
according to the provided problem parameters (i.e., values of the
parameters in the PEtab parameter table).
This assumes that:
* the `ExpData` instance was created by :meth:`create_edata`,
* no other changes except for calls to :meth:`apply_parameters`
were made,
* and the PEtab problem was not modified since the creation of this
:class:`ExperimentManager` instance.
Each model parameter takes the scale of the problem parameter it is
mapped to in the given experiment, and model parameters that are
not mapped to any problem parameter are on linear scale.
Fixed model parameters are always on linear scale, so the values of
the problem parameters they are mapped to are unscaled.
:param edata: The :class:`ExpData` instance to be updated.
In case of errors, the state of `edata` is undefined.
:param problem_parameters: Problem parameters to be applied,
on the scales given by :attr:`parameter_scales`.
"""
# TODO: support ndarray in addition to dict?
# check parameter IDs
if problem_parameters.keys() != self._x_ids:
missing = self._x_ids - problem_parameters.keys()
extra = problem_parameters.keys() - self._x_ids
msg_parts = []
if missing:
# TODO: support a subset of parameters?
# if so, update only those parameters and leave the rest as is
# or use nominal values for missing ones?
msg_parts.append(f"missing parameters: {missing}")
if extra:
msg_parts.append(f"unknown parameters: {extra}")
raise ValueError(
"Provided problem parameters do not match "
"PEtab problem parameters: " + "; ".join(msg_parts)
)
# get the original parameter values
# (model parameters set during model creation)
par_vals = np.array(self._original_p)
pid_to_idx = self._pid_to_idx
experiment_id = edata.id
experiment = self._id_to_experiment[experiment_id]
# plist -- estimated parameters + those mapped via placeholders
# TODO sufficient to set them during creation of edata
# or allow dynamic fixing of parameters?
plist = list(self._model_par_idx_to_x_free_idx)
placeholder_mappings = self._get_placeholder_mapping(experiment)
# placeholders mapped to estimated parameters
estimated_placeholder_idxs = {
model_par_idx
for model_par_id, maps_to in placeholder_mappings.items()
if maps_to in self._x_free_id_to_idx
and (model_par_idx := pid_to_idx.get(model_par_id)) is not None
and model_par_idx not in self._model_par_idx_to_x_free_idx
}
if estimated_placeholder_idxs:
plist.extend(estimated_placeholder_idxs)
plist.sort()
edata.plist = plist
# Update fixed parameters in case they are affected by problem
# parameters (i.e., parameter table parameters).
# Fixed parameters are always on linear scale.
fixed_par_updates = {
p_idx: unscale_parameter(
problem_parameters[p_id], self._parameter_scales[p_id]
)
for p_id, p_idx in self._problem_par_to_fixed_model_par
}
fixed_par_vals = np.asarray(edata.fixed_parameters)
for p_idx, p_val in fixed_par_updates.items():
fixed_par_vals[p_idx] = p_val
edata.fixed_parameters = fixed_par_vals
if edata.fixed_parameters_pre_equilibration:
fixed_par_vals = np.array(edata.fixed_parameters_pre_equilibration)
for p_idx, p_val in fixed_par_updates.items():
fixed_par_vals[p_idx] = p_val
edata.fixed_parameters_pre_equilibration = fixed_par_vals
# Scales of the model parameters. Each model parameter takes the
# scale of the problem parameter it is mapped to, all others
# (model defaults, numeric overrides) are on linear scale.
pscale = [ParameterScaling.none] * len(par_vals)
# Apply problem parameter values to identical model parameters.
# Any other parameter mapping, except for output parameter
# placeholders, is handled by events.
for p_id, idx in self._problem_par_to_free_model_par:
par_vals[idx] = problem_parameters[p_id]
pscale[idx] = self._amici_scales[p_id]
# Handle measurement-specific mappings to placeholders
measurements = self._get_measurements(experiment_id)
def apply_override(placeholder: str, override: sp.Basic):
"""Apply a single placeholder override."""
if (idx := pid_to_idx.get(placeholder)) is not None:
if override.is_Number:
par_vals[idx] = float(override)
pscale[idx] = ParameterScaling.none
elif override.is_Symbol:
par_vals[idx] = problem_parameters[str(override)]
pscale[idx] = self._amici_scales[str(override)]
else:
raise AssertionError(
f"Unexpected override type: {override} for {placeholder} in experiment {experiment_id}"
)
else:
raise NotImplementedError(
f"Cannot handle override `{placeholder}' => '{override}'"
)
# tracks encountered placeholders and their overrides
# (across all observables -- placeholders IDs are globally unique)
# and check that all periods use the same overrides
# (except for numeric sigmas)
# TODO: this can be simplified. we only need to process overrides
# that are parameters. the rest was handled during create_edata
overrides = {}
for m in measurements:
obs = self._petab_id_to_obs[m.observable_id]
if obs.observable_placeholders:
for placeholder, override in zip(
obs.observable_placeholders,
m.observable_parameters,
strict=True,
):
placeholder = str(placeholder)
if (
prev_override := overrides.get(placeholder)
) is not None and prev_override != override:
raise NotImplementedError(
"Timepoint-specific observable placeholder "
"overrides are not supported"
)
apply_override(placeholder, override)
if obs.noise_placeholders:
for placeholder, override in zip(
obs.noise_placeholders, m.noise_parameters, strict=True
):
placeholder = str(placeholder)
if (
prev_override := overrides.get(placeholder)
) is not None and prev_override != override:
# TODO: this might have been handled
# via .sigmay if numeric
raise NotImplementedError(
"Timepoint-specific observable placeholder "
"overrides are not supported"
)
apply_override(placeholder, override)
# TODO: set all unused placeholders to NaN to make it easier to spot problems?
edata.free_parameters = par_vals
edata.pscale = parameter_scaling_from_int_vector(pscale)
if self._debug:
logger.debug("ExperimentManager.apply_parameters:")
logger.debug(
f"Parameters: "
f"{dict(zip(self._free_parameter_ids, map(float, par_vals)))}"
)
@property
def petab_problem(self) -> v2.Problem:
"""The PEtab problem used by this ExperimentManager.
This must not be modified.
"""
return self._petab_problem
@property
def model(self) -> amici.sim.sundials.Model:
"""The AMICI model used by this ExperimentManager."""
return self._model
@property
def parameter_scales(self) -> dict[str, ParameterScale]:
"""The scales of all PEtab problem parameters.
A mapping from problem parameter IDs to ``"lin"``, ``"log"``, or
``"log10"``. The keys are ordered like ``Problem.x_ids``.
"""
return self._parameter_scales.copy()
[docs]
def scale_parameters(
self, problem_parameters: Mapping[str, float]
) -> dict[str, float]:
"""Bring problem parameters from linear scale to their scales.
:param problem_parameters:
Values of (a subset of) the problem parameters on linear scale.
:return:
The values on the scales given by :attr:`parameter_scales`.
"""
return {
par_id: scale_parameter(value, self._parameter_scales[par_id])
for par_id, value in problem_parameters.items()
}
[docs]
def unscale_parameters(
self, problem_parameters: Mapping[str, float]
) -> dict[str, float]:
"""Bring problem parameters from their scales to linear scale.
:param problem_parameters:
Values of (a subset of) the problem parameters on the scales
given by :attr:`parameter_scales`.
:return:
The values on linear scale.
"""
return {
par_id: unscale_parameter(value, self._parameter_scales[par_id])
for par_id, value in problem_parameters.items()
}
def _get_measurements(
self, experiment_id: str
) -> Sequence[v2.Measurement]:
"""Get the measurements for the given experiment.
Equivalent to :meth:`petab.v2.Problem.get_measurements_for_experiment`,
but in constant time. The result must not be modified.
:param experiment_id: The ID of the experiment.
:return: The measurements for the given experiment, in the order of
the measurement table.
"""
return self._experiment_id_to_measurements.get(experiment_id, ())
def _get_placeholder_mapping(
self, experiment: v2.Experiment
) -> dict[str, str]:
"""Get the mapping from model parameter IDs (= PEtab placeholder ID)
to problem parameter IDs for placeholders in the given experiment.
Because AMICI does not support timepoint-specific overrides,
this mapping is unique.
:param experiment: The experiment to get the mapping for.
:return: The mapping from model parameter IDs to problem parameter IDs.
"""
mapping = {}
for measurement in self._get_measurements(experiment.id):
observable = self._petab_id_to_obs[measurement.observable_id]
for placeholder, override in zip(
observable.observable_placeholders,
measurement.observable_parameters,
strict=True,
):
# we don't care about numeric overrides here
if isinstance(override, sp.Symbol):
mapping[str(placeholder)] = str(override)
for placeholder, override in zip(
observable.noise_placeholders,
measurement.noise_parameters,
strict=True,
):
# we don't care about numeric overrides here
if isinstance(override, sp.Symbol):
mapping[str(placeholder)] = str(override)
return mapping
def _has_timepoints(rdata: amici.sim.sundials.ReturnDataView) -> bool:
"""Whether the given simulation result has any timepoints.
Experiments without measurements have no timepoints and thus no
residuals, which is different from residuals not having been computed.
"""
return rdata.ts is not None
[docs]
@dataclass
class PetabSimulationResult:
"""
Container for results of a PEtab simulation.
Holds the per-experiment AMICI data objects and aggregated metrics
produced by :class:`PetabSimulator.simulate`.
All sensitivities are with respect to the PEtab problem parameters on
the scales given by :attr:`ExperimentManager.parameter_scales`.
"""
#: List of :class:`amici.sim.sundials.ExpData` instances, one per
#: simulated experiment. These objects may be modified by subsequent
#: operations.
edatas: list[amici.sim.sundials.ExpData] = field(default_factory=list)
#: List of :class:`amici.sim.sundials.ReturnDataView` instances,
#: one per simulated experiment containing simulation outputs.
rdatas: list[amici.sim.sundials.ReturnDataView] = field(
default_factory=list
)
#: Aggregated first-order sensitivities of the log-likelihood with
#: respect to PEtab problem parameters. Mapping from parameter ID
#: to sensitivity value, or ``None`` if sensitivities were not computed.
sllh: dict[str, float] | None = None
#: Aggregated second-order sensitivities (Hessian or FIM-based)
#: as a 2D :class:`numpy.ndarray` in the order of
#: ``Problem.x_free_ids``. ``None`` if second-order sensitivities
#: were not computed.
s2llh: np.ndarray | None = None
#: Aggregated sensitivities of the residuals w.r.t. the estimated PEtab
#: problem parameters as a 2D :class:`numpy.ndarray` of shape
#: ``(n_residuals, n_estimated)``. The rows correspond to the residuals
#: returned by :attr:`res`, the columns are in the order of
#: ``Problem.x_free_ids``. ``None`` if residual sensitivities were not
#: computed.
sres: np.ndarray | None = None
@property
def llh(self) -> float:
"""The total log-likelihood across all experiments."""
return sum(rdata.llh for rdata in self.rdatas)
@property
def res(self) -> np.ndarray | None:
"""
Concatenated residuals.
The residuals of the individual experiments are concatenated in the
order of :attr:`rdatas`. This matches the row order of :attr:`sres`.
:returns: Concatenated residuals from all experiments as a 1D
:class:`numpy.ndarray`, or ``None`` if they are not available,
i.e., if any experiment failed to simulate or if residuals were
not computed at all. If no experiment has any measurement, an
empty array is returned.
"""
res = []
for rdata in self.rdatas:
# the residuals of a failed simulation are invalidated (NaN),
# there is nothing useful to return
if rdata.status != amici.sim.sundials.AMICI_SUCCESS:
return None
if not _has_timepoints(rdata):
# experiment without measurements -- no residuals
continue
if rdata.res is None:
return None
res.append(rdata.res)
return np.hstack(res) if res else np.zeros(0)
[docs]
class PetabSimulator:
"""
Simulator for PEtab2 problems.
This class is used to simulate all experiments of a given PEtab problem
using a given AMICI model and solver, and to aggregate the results.
The :class:`amici.sim.sundials.ExpData` objects for the experiments are
created by the first call to :meth:`simulate` and reused by all subsequent
calls, which only update their parameter-dependent values
(see :meth:`ExperimentManager.apply_parameters`).
"""
[docs]
def __init__(
self,
em: ExperimentManager,
*,
solver: amici.sim.sundials.Solver | None = None,
num_threads: int = 1,
# TODO: allow selecting specific experiments?
):
"""
Initialize the simulator.
:param em:
The :class:`ExperimentManager` to generate the :class:`amici.ExpData`
objects.
:param solver: The AMICI solver to use for the simulations.
If not provided, a new solver with default settings will be used.
:param num_threads:
The number of threads to use for parallel simulation of experiments.
Only relevant if multiple experiments are present in the PEtab problem.
"""
self._petab_problem: v2.Problem = em.petab_problem
self._model = em.model
self._solver = (
solver if solver is not None else self._model.create_solver()
)
self._exp_man: ExperimentManager = em
self.num_threads = num_threads
# cache for `_get_plist_to_problem_par_ix`; the mapping only depends
# on the (immutable) PEtab problem, the model, and the parameter list
# of the respective experiment
self._plist_to_problem_par_ix: dict[
tuple[str, tuple[int, ...]], dict[int, int]
] = {}
# The `ExpData`s for all experiments, created by the first call to
# `simulate`. Apart from the values set by
# `ExperimentManager.apply_parameters`, they only depend on the
# (immutable) PEtab problem and the model.
self._edatas: list[amici.sim.sundials.ExpData] | None = None
@property
def model(self) -> amici.sim.sundials.Model:
"""The AMICI model used by this simulator."""
return self._model
@property
def solver(self) -> amici.sim.sundials.Solver:
"""The AMICI solver used by this simulator."""
return self._solver
@property
def exp_man(self) -> ExperimentManager:
"""The ExperimentManager used by this simulator."""
return self._exp_man
[docs]
def simulate(
self, problem_parameters: dict[str, float] = None
) -> PetabSimulationResult:
# TODO params: dict|np.ndarray|None?
"""Simulate all experiments of the given PEtab problem.
:param problem_parameters:
Values of the PEtab problem parameters, on the scales given by
:attr:`ExperimentManager.parameter_scales`. Nominal values are
used for all parameters that are not specified.
:return:
A :class:`PetabSimulationResult` instance containing the
per-experiment data objects and aggregated results.
Note that the returned :class:`amici.sim.sundials.ExpData`
instances are reused and updated by subsequent calls to this
function. They must not be modified. Create a copy if needed.
"""
if problem_parameters is None:
# use default parameters, i.e., nominal values for all parameters
# TODO: Nominal parameters, or previously used parameters?
problem_parameters = {}
# use nominal values for all unspecified parameters
problem_parameters_default = self._exp_man.scale_parameters(
self._petab_problem.get_x_nominal_dict()
)
problem_parameters = problem_parameters_default | problem_parameters
if self._edatas is None:
self._edatas = self._exp_man.create_edatas(problem_parameters)
else:
for edata in self._edatas:
self._exp_man.apply_parameters(
edata=edata, problem_parameters=problem_parameters
)
rdatas = amici.sim.sundials.run_simulations(
self._model,
self._solver,
self._edatas,
num_threads=self.num_threads,
)
return PetabSimulationResult(
edatas=list(self._edatas),
rdatas=rdatas,
sllh=self._aggregate_sllh(rdatas),
s2llh=self._aggregate_s2llh(rdatas, use_fim=True),
sres=self._aggregate_sres(rdatas),
)
def _aggregate_sllh(
self, rdatas: Sequence[amici.sim.sundials.ReturnDataView]
) -> dict[str, float] | None:
"""Aggregate the sensitivities of the log-likelihoods.
:param rdatas:
The ReturnData objects to aggregate the sensitivities from.
:return:
The aggregated sensitivities (parameter ID -> sensitivity value),
or ``None`` if the likelihood sensitivities were not computed.
"""
if (
self._solver.get_sensitivity_order() < SensitivityOrder.first
# the log-likelihood and its sensitivities are not computed in
# residual-only reporting mode
or self._solver.get_return_data_reporting_mode()
== RDataReporting.residuals
):
return None
sllh_total: dict[str, float] = {}
# Check for issues in all condition simulation results.
for rdata in rdatas:
# Condition failed during simulation.
if rdata.status != amici.sim.sundials.AMICI_SUCCESS:
return None
# Condition simulation result does not provide SLLH.
if rdata.sllh is None:
raise ValueError(
f"The sensitivities of the likelihood for experiment "
f"{rdata.id} were not computed."
)
free_parameter_ids = self._model.get_free_parameter_ids()
# still needs parameter mapping for placeholders
for rdata in rdatas:
experiment = self._exp_man._id_to_experiment[rdata.id]
placeholder_mappings = self._exp_man._get_placeholder_mapping(
experiment
)
for model_par_idx, sllh in zip(
rdata.plist, rdata.sllh, strict=True
):
model_par_id = problem_par_id = free_parameter_ids[
model_par_idx
]
if maps_to := placeholder_mappings.get(model_par_id):
problem_par_id = maps_to
sllh_total[problem_par_id] = (
sllh_total.get(problem_par_id, 0.0) + sllh
)
return sllh_total
def _get_plist_to_problem_par_ix(
self, rdata: amici.sim.sundials.ReturnDataView
) -> dict[int, int]:
"""Map sensitivity indices of an experiment to problem parameters.
Create the mapping from indices into the parameter list of the given
simulation result (i.e., indices into the parameter dimension of
``rdata.sllh``, ``rdata.sres``, ``rdata.FIM``, ...) to the indices of
the estimated PEtab problem parameters
(i.e., indices into ``Problem.x_free_ids``).
Model parameters that do not correspond to any estimated problem
parameter are omitted. Note that the mapping is not necessarily
injective: several model parameters (output parameter placeholders)
may map to the same problem parameter.
The result is cached, as it is the same for every simulation of a
given experiment, and must not be modified by the caller.
:param rdata: The simulation result to create the mapping for.
:return: The mapping from ``rdata.plist`` indices to
``Problem.x_free_ids`` indices.
"""
plist = tuple(rdata.plist)
cache_key = (rdata.id, plist)
if (
cached := self._plist_to_problem_par_ix.get(cache_key)
) is not None:
return cached
exp_man = self._exp_man
# problem parameter ID to index of the estimated problem parameters
x_free_ix = exp_man._x_free_id_to_idx
# Model parameter index to problem parameter index map for estimated
# parameters except placeholders.
# This is the same for all experiments.
ix_map: dict[int, int] = dict(exp_man._model_par_idx_to_x_free_idx)
# still needs experiment-specific parameter mapping for placeholders
experiment = exp_man._id_to_experiment[rdata.id]
placeholder_mappings = exp_man._get_placeholder_mapping(experiment)
for model_pid, problem_pid in placeholder_mappings.items():
if (problem_par_ix := x_free_ix.get(problem_pid)) is not None:
ix_map[exp_man._pid_to_idx[model_pid]] = problem_par_ix
# else: mapped-to parameter is not estimated
# translate model parameter index to plist index
model_par_ix_to_plist_ix = {
model_par_ix: plist_ix
for plist_ix, model_par_ix in enumerate(plist)
}
ix_map = {
model_par_ix_to_plist_ix[model_par_ix]: problem_par_ix
for model_par_ix, problem_par_ix in ix_map.items()
}
self._plist_to_problem_par_ix[cache_key] = ix_map
return ix_map
def _aggregate_s2llh(
self,
rdatas: Sequence[amici.sim.sundials.ReturnDataView],
use_fim: bool = True,
) -> np.ndarray | None:
"""Aggregate the Hessians from individual experiments.
Compute the total second-order sensitivities of the log-likelihoods
w.r.t. estimated PEtab problem parameters.
:param rdatas:
The ReturnData objects to aggregate the sensitivities from.
:param use_fim:
Whether to use the Fisher Information Matrix (FIM) to compute
the 2nd order sensitivities. Only ``True`` is currently supported.
:return:
The aggregated 2nd order sensitivities as a 2D numpy array
in the order of the estimated PEtab problem parameters
(``Problem.x_free_ids``),
or `None` if sensitivities were not computed.
"""
# TODO: add tests
if (
self._solver.get_sensitivity_order() < SensitivityOrder.first
or self._solver.get_sensitivity_method()
!= SensitivityMethod.forward
or self._solver.get_return_data_reporting_mode()
== RDataReporting.residuals
):
return None
if not use_fim:
raise NotImplementedError(
"Computation of 2nd order sensitivities without FIM is not "
"implemented yet."
)
# Check for issues in all condition simulation results.
for rdata in rdatas:
# Condition failed during simulation.
if rdata.status != amici.sim.sundials.AMICI_SUCCESS:
return None
# Condition simulation result does not provide the FIM, e.g.,
# because of a non-Gaussian noise model.
if rdata.FIM is None:
return None
s2llh_total = np.zeros(
shape=(
self._petab_problem.n_estimated,
self._petab_problem.n_estimated,
),
dtype=float,
)
for rdata in rdatas:
ix_map = self._get_plist_to_problem_par_ix(rdata)
if use_fim:
model_s2llh = rdata.FIM
else:
raise NotImplementedError()
model_par_slice = np.fromiter(ix_map.keys(), dtype=int)
problem_par_slice = np.fromiter(ix_map.values(), dtype=int)
# handle possible non-unique indices in problem_par_slice
# (i.e. multiple model parameters mapping to the same problem
# parameter)
problem_par_slice_unique, unique_index = np.unique(
problem_par_slice, return_index=True
)
# handle unique mappings
s2llh_total[
np.ix_(problem_par_slice_unique, problem_par_slice_unique)
] += model_s2llh[
np.ix_(
model_par_slice[unique_index],
model_par_slice[unique_index],
)
]
# handle non-unique mappings if any
if problem_par_slice_unique.size < problem_par_slice.size:
# index in the mapping arrays of non-unique entries
is_unique = np.zeros(len(problem_par_slice), dtype=bool)
is_unique[unique_index] = True
non_unique_indices = np.flatnonzero(~is_unique)
for idx in non_unique_indices:
s2llh_total[
problem_par_slice[idx], problem_par_slice_unique
] += model_s2llh[
model_par_slice[idx], model_par_slice[unique_index]
]
s2llh_total[
problem_par_slice_unique, problem_par_slice[idx]
] += model_s2llh[
model_par_slice[unique_index], model_par_slice[idx]
]
for jdx in non_unique_indices:
s2llh_total[
problem_par_slice[idx], problem_par_slice[jdx]
] += model_s2llh[
model_par_slice[idx], model_par_slice[jdx]
]
return s2llh_total
def _aggregate_sres(
self, rdatas: Sequence[amici.sim.sundials.ReturnDataView]
) -> np.ndarray | None:
"""Aggregate the residual sensitivities from individual experiments.
Compute the sensitivities of the residuals of all experiments
w.r.t. the estimated PEtab problem parameters.
The residuals of the individual experiments are concatenated in the
order of ``rdatas``, matching
:attr:`PetabSimulationResult.res`. Sensitivities w.r.t. model
parameters that map to the same problem parameter (i.e., output
parameter placeholders) are summed up.
:param rdatas:
The ReturnData objects to aggregate the sensitivities from.
:return:
The aggregated residual sensitivities as a 2D numpy array of
shape ``(n_residuals, n_estimated)``, where the columns are in
the order of the estimated PEtab problem parameters
(``Problem.x_free_ids``), or ``None`` if residual sensitivities
were not computed.
"""
if (
self._solver.get_sensitivity_order() < SensitivityOrder.first
or self._solver.get_sensitivity_method()
!= SensitivityMethod.forward
):
return None
# Check for issues in all experiment simulation results.
rdatas_with_res = []
for rdata in rdatas:
# Experiment failed during simulation.
if rdata.status != amici.sim.sundials.AMICI_SUCCESS:
return None
if not _has_timepoints(rdata):
# experiment without measurements -- no residuals
continue
# Residual sensitivities are unavailable, e.g., because of the
# current ReturnData reporting mode or because the problem
# contains non-Gaussian noise models.
if rdata.sres is None:
return None
rdatas_with_res.append(rdata)
rdatas = rdatas_with_res
sres_total = np.zeros(
shape=(
sum(rdata.sres.shape[0] for rdata in rdatas),
self._petab_problem.n_estimated,
),
dtype=float,
)
cur_row = 0
for rdata in rdatas:
ix_map = self._get_plist_to_problem_par_ix(rdata)
plist_slice = np.fromiter(ix_map.keys(), dtype=int)
problem_par_slice = np.fromiter(ix_map.values(), dtype=int)
# view on the rows belonging to the current experiment
cur_sres = sres_total[cur_row : cur_row + rdata.sres.shape[0], :]
# `np.add.at` to correctly handle non-unique indices in
# `problem_par_slice` (i.e. multiple model parameters mapping to
# the same problem parameter); indexing the transposed arrays,
# since `np.add.at` indexes the first dimension
np.add.at(
cur_sres.T, problem_par_slice, rdata.sres[:, plist_slice].T
)
cur_row += rdata.sres.shape[0]
return sres_total