Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for complex noise formulae in roadrunner #1363

Merged
merged 5 commits into from
Apr 12, 2024
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
34 changes: 28 additions & 6 deletions pypesto/objective/roadrunner/petab_importer_roadrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

import numbers
import re
from collections.abc import Iterable
from pathlib import Path
from typing import Any
Expand Down Expand Up @@ -85,20 +86,41 @@ def _check_noise_formulae(
# check that edatas are available
if edatas is None:
edatas = self.create_edatas()
# save formulae that need to be changed
to_change = []
# check that noise formulae are valid
for edata, par_map in zip(edatas, parameter_mapping):
for noise_formula in edata.noise_formulae:
for i_edata, (edata, par_map) in enumerate(
zip(edatas, parameter_mapping)
):
for j_formula, noise_formula in enumerate(edata.noise_formulae):
# constant values are allowed
if isinstance(noise_formula, numbers.Number):
continue
# single parameters are allowed
if noise_formula in par_map[1].keys():
continue
raise NotImplementedError(
"Noise formulae must be either constants or single "
"parameters. For more complex noise models, please "
"use amici for now."
# extract the observable name via regex pattern
pattern = r"noiseParameter1_(.*?)($|\s)"
observable_name = re.search(pattern, noise_formula).group(1)
to_change.append((i_edata, j_formula, observable_name))
# change formulae
formulae_changed = []
for i_edata, j_formula, obs_name in to_change:
# assign new parameter, formula in RR and parameter into mapping
original_formula = edatas[i_edata].noise_formulae[j_formula]
edatas[i_edata].noise_formulae[
j_formula
] = f"noiseFormula_{obs_name}"
# different conditions will have the same noise formula
if (obs_name, original_formula) not in formulae_changed:
self.rr.addParameter(f"noiseFormula_{obs_name}", 0.0, False)
self.rr.addAssignmentRule(
f"noiseFormula_{obs_name}",
original_formula,
forceRegenerate=False,
)
self.rr.regenerateModel()
formulae_changed.append((obs_name, original_formula))

def _write_observables_to_model(self):
"""Write observables of petab problem to the model."""
Expand Down
8 changes: 7 additions & 1 deletion pypesto/objective/roadrunner/roadrunner_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def simulate_per_condition(
times=timepoints, selections=[TIME] + observables_ids
)

llhs = calculate_llh(sim_res, edata, par_map)
llhs = calculate_llh(sim_res, edata, par_map, roadrunner_instance)

# reset the model
roadrunner_instance.reset()
Expand Down Expand Up @@ -349,6 +349,7 @@ def calculate_llh(
simulations: np.ndarray,
edata: ExpData,
parameter_mapping: dict,
roadrunner_instance: roadrunner.RoadRunner,
) -> float:
"""Calculate the negative log-likelihood for a single condition.

Expand All @@ -360,6 +361,8 @@ def calculate_llh(
ExpData of a single condition.
parameter_mapping:
Parameter mapping for the condition.
roadrunner_instance:
RoadRunner instance. Needed to retrieve complex formulae.

Returns
-------
Expand Down Expand Up @@ -390,6 +393,9 @@ def _fill_in_noise_formula(noise_formula):
# if it is not a number, it is assumed to be a string
if noise_formula in parameter_mapping.keys():
return parameter_mapping[noise_formula]
# if the string starts with "noiseFormula_" it is saved in the model
if noise_formula.startswith("noiseFormula_"):
return roadrunner_instance.getValue(noise_formula)

# replace noise formula with actual value from mapping
noise_formulae = np.array(
Expand Down
Loading