From 2dc23d0c0fbf4b01fd8aa95f08b18877b30a05ea Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 16 Nov 2026 14:54:29 +0100 Subject: [PATCH] Write and lint hybridization target values as PEtab math `HybridizationTable.to_df` and the target value check in `CheckHybridizationTable` used `str()`, which gives sympy syntax, e.g. `x**2.0` or `Abs(y)`, that is not valid PEtab math. As a result, written hybridization tables could not be read again, and the linter reported valid target values as invalid. Use `petab_math_str` instead. Co-Authored-By: Claude Opus 5.5 --- petab/v2/extensions/sciml.py | 5 ++++- petab/v2/extensions/sciml_lint.py | 4 ++-- tests/v2/test_sciml.py | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/petab/v2/extensions/sciml.py b/petab/v2/extensions/sciml.py index 4bd406f1..c8bd54b9 100644 --- a/petab/v2/extensions/sciml.py +++ b/petab/v2/extensions/sciml.py @@ -12,7 +12,7 @@ from pydantic import AnyUrl, BaseModel, ConfigDict, Field, field_validator from petab._utils import _generate_path -from petab.v1.math import sympify_petab +from petab.v1.math import petab_math_str, sympify_petab try: from petab_sciml import ( @@ -100,6 +100,9 @@ def from_tsv( def to_df(self) -> pd.DataFrame: """Convert the HybridizationTable to a DataFrame.""" records = [h.model_dump(by_alias=True) for h in self.hybridizations] + for record in records: + if record[C.TARGET_VALUE] is not None: + record[C.TARGET_VALUE] = petab_math_str(record[C.TARGET_VALUE]) return pd.DataFrame(records) def to_tsv(self, file_path: str | Path | None = None) -> None: diff --git a/petab/v2/extensions/sciml_lint.py b/petab/v2/extensions/sciml_lint.py index ba98ad32..167525b6 100644 --- a/petab/v2/extensions/sciml_lint.py +++ b/petab/v2/extensions/sciml_lint.py @@ -16,7 +16,7 @@ ) from petab.v1.lint import is_valid_identifier -from petab.v1.math import sympify_petab +from petab.v1.math import petab_math_str, sympify_petab from .. import core, lint from ..C import EXT_ID_SCIML @@ -274,7 +274,7 @@ def run(self, problem: core.Problem) -> lint.ValidationIssue | None: # value is sympified on construction); this is a defensive # re-check in case a target value was assigned some other way. try: - sympify_petab(str(hyb.target_value)) + sympify_petab(petab_math_str(hyb.target_value)) except Exception as e: # noqa BLE001 messages.append( f"Hybridization target value for `{hyb.target_id}` is " diff --git a/tests/v2/test_sciml.py b/tests/v2/test_sciml.py index a6cdf098..15ca5f3b 100644 --- a/tests/v2/test_sciml.py +++ b/tests/v2/test_sciml.py @@ -3,10 +3,12 @@ from petab_sciml import Input, Node from pydantic import ConfigDict +from petab.v2 import C from petab.v2.core import * from petab.v2.core import ModelFile, ProblemExtensions from petab.v2.extensions.sciml import ( Hybridization, + HybridizationTable, NeuralNetConfig, SciMLConfig, SciMLExt, @@ -303,6 +305,27 @@ def test_hybridization_invalid_math_rejected(): Hybridization(target_id="alpha", target_value="1 +* 2") +@pytest.mark.parametrize("target_value", ["A^2", "abs(A)", "A > 0"]) +def test_hybridization_valid_math_not_rejected(target_value): + """Valid PEtab math target values pass the lint check.""" + problem = _get_test_problem() + for hyb in problem.extensions.sciml.hybridization_tables[0].hybridizations: + if hyb.target_id == "net1_input1": + hyb.target_value = target_value + assert CheckHybridizationTable().run(problem) is None + + +def test_hybridization_table_to_df_petab_math(): + """Target values are written as PEtab math expressions.""" + table = HybridizationTable( + [ + Hybridization(target_id="a", target_value="x^2"), + Hybridization(target_id="b", target_value="abs(y)"), + ] + ) + assert table.to_df()[C.TARGET_VALUE].tolist() == ["x ^ 2", "abs(y)"] + + def test_hybridization_pre_initialization_not_evaluable(): problem = _get_test_problem() # net1_input1 is assigned the species `A`, which is not available before