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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
* `petab.v1.math.petab_symbol` (also in `petab.v2.math`) creates sympy
symbols with the same assumptions as `sympify_petab`
(by @dweindl in https://github.com/PEtab-dev/libpetab-python/pull/563)
* `petab.v2.reparametrize_log` estimates parameters on log scale by replacing
them with new log-transformed parameters
(by @dweindl in https://github.com/PEtab-dev/libpetab-python/pull/560)

## 0.9 series

Expand Down
1 change: 1 addition & 0 deletions doc/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ API Reference
petab.v2.models
petab.v2.options
petab.v2.petab1to2
petab.v2.reparametrize
1 change: 1 addition & 0 deletions petab/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
from .lint import lint_problem # noqa: F401
from .models import MODEL_TYPE_PYSB, MODEL_TYPE_SBML, Model # noqa: F401
from .options import *
from .reparametrize import reparametrize_log # noqa: F401
14 changes: 6 additions & 8 deletions petab/v2/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
ExperimentPeriod,
Problem,
)
from .models._sbml_utils import add_sbml_parameter, check
from .models._sbml_utils import (
add_sbml_parameter,
check,
set_initial_assignment,
)
from .models.sbml_model import SbmlModel

__all__ = ["ExperimentsToSbmlConverter", "assignments_to_experiments"]
Expand Down Expand Up @@ -435,13 +439,7 @@ def _create_initial_assignments(
(default, True),
)

# Create a new initial assignment if necessary, otherwise
# overwrite the existing one.
if ia is None:
ia = model.createInitialAssignment()
ia.setSymbol(target_id)

set_math(ia, expr)
set_initial_assignment(model, target_id, expr)
free_symbols_in_assignments |= expr.free_symbols

# the target value may depend on parameters that are only
Expand Down
31 changes: 31 additions & 0 deletions petab/v2/models/_sbml_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Private utility functions for SBML handling."""

import libsbml
import sympy as sp
from sbmlmath.mathml_printer import SBMLMathMLPrinter

retval_to_str = {
getattr(libsbml, attr): attr
Expand Down Expand Up @@ -49,3 +51,32 @@ def add_sbml_parameter(
check(param.setConstant(constant))

return param


def set_initial_assignment(
model: libsbml.Model,
symbol_id: str,
expr: sp.Expr,
literals_dimensionless: bool = True,
) -> libsbml.InitialAssignment:
"""Set the initial assignment for the given symbol.

Overwrites any existing initial assignment for that symbol.

:param literals_dimensionless:
Whether to annotate numeric literals as dimensionless.
(`sbmlmath` can't read back, e.g., ``10^x`` with a dimensionless
base.)
"""
if (ia := model.getInitialAssignmentBySymbol(symbol_id)) is None:
ia = model.createInitialAssignment()
check(ia.setSymbol(symbol_id))
mathml = SBMLMathMLPrinter(
sbml_level=model.getLevel(),
sbml_version=model.getVersion(),
literals_dimensionless=literals_dimensionless,
).doprint(expr)
if (ast := libsbml.readMathMLFromString(mathml)) is None:
raise ValueError(f"Failed to convert `{expr}' to MathML.")
check(ia.setMath(ast))
return ia
Loading
Loading