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
5 changes: 4 additions & 1 deletion petab/v2/extensions/sciml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions petab/v2/extensions/sciml_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "
Expand Down
23 changes: 23 additions & 0 deletions tests/v2/test_sciml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Loading