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
8 changes: 8 additions & 0 deletions changelog.d/+placeholders-own-observable.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PEtab v1 validation: `lint_problem` now reports an error for placeholder
parameters that are model entities, condition table columns, mapping table
entries or observable IDs, or that are used in another observable, the
condition table or the measurement table, and for noise placeholders used in
the observable formula
(new `assert_placeholders_only_used_in_own_observable`).
As `petab1to2` validates its input, it now rejects such problems
([#605](https://github.com/PEtab-dev/libpetab-python/pull/605))
64 changes: 0 additions & 64 deletions doc/example/example_Fujita/Fujita_model.xml

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this issue in the benchmark collection, so could check to see if this example copy is up-to-date.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's an ancient version. It wasn't synced on purpose, afair. I think this was mostly due to visualization tests for which it was used here.

Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@
<parameter id="reaction_7_k1" value="0.0119329694583145" constant="true"/>
<parameter id="reaction_8_k1" value="0.000941161525754959" constant="true"/>
<parameter id="reaction_9_k1" value="0.0273281571867514" constant="true"/>
<parameter id="observableParameter1_pAkt_tot" value="334.979345701912" constant="true"/>
<parameter id="observableParameter1_pEGFR_tot" value="9.60908283106834e-08" constant="true"/>
<parameter id="observableParameter1_pS6_tot" value="380222.792056961" constant="true"/>
<parameter id="sigma_pAkt_tot" value="0.1" constant="false"/>
<parameter id="sigma_pEGFR_tot" value="0.1" constant="false"/>
<parameter id="sigma_pS6_tot" value="0.1" constant="false"/>
<parameter id="noiseParameter1_pAkt_tot" value="0.1" constant="true"/>
<parameter id="noiseParameter1_pEGFR_tot" value="0.1" constant="true"/>
<parameter id="noiseParameter1_pS6_tot" value="0.1" constant="true"/>
<parameter id="observable_pEGFR_tot" value="0" constant="false"/>
<parameter id="observable_pAkt_tot" value="0" constant="false"/>
<parameter id="observable_pS6_tot" value="0" constant="false"/>
<parameter id="EGF" value="0.1" constant="false"/>
</listOfParameters>
<listOfInitialAssignments>
Expand All @@ -70,58 +58,6 @@
</math>
</initialAssignment>
</listOfInitialAssignments>
<listOfRules>
<assignmentRule variable="sigma_pAkt_tot">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<ci> noiseParameter1_pAkt_tot </ci>
</math>
</assignmentRule>
<assignmentRule variable="sigma_pEGFR_tot">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<ci> noiseParameter1_pEGFR_tot </ci>
</math>
</assignmentRule>
<assignmentRule variable="sigma_pS6_tot">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<ci> noiseParameter1_pS6_tot </ci>
</math>
</assignmentRule>
<assignmentRule variable="observable_pEGFR_tot">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<ci> observableParameter1_pEGFR_tot </ci>
<apply>
<plus/>
<ci> pEGFR </ci>
<ci> pEGFR_Akt </ci>
</apply>
</apply>
</math>
</assignmentRule>
<assignmentRule variable="observable_pAkt_tot">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<ci> observableParameter1_pAkt_tot </ci>
<apply>
<plus/>
<ci> pAkt </ci>
<ci> pAkt_S6 </ci>
</apply>
</apply>
</math>
</assignmentRule>
<assignmentRule variable="observable_pS6_tot">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<ci> pS6 </ci>
<ci> observableParameter1_pS6_tot </ci>
</apply>
</math>
</assignmentRule>
</listOfRules>
<listOfReactions>
<reaction id="v1_reaction_1" name="reaction_1" reversible="true">
<listOfReactants>
Expand Down
113 changes: 112 additions & 1 deletion petab/v1/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import numbers
import re
from collections import Counter
from collections import Counter, defaultdict
from collections.abc import Iterable
from typing import Any

Expand Down Expand Up @@ -35,6 +35,7 @@
"assert_parameter_prior_parameters_are_valid",
"assert_parameter_prior_type_is_valid",
"assert_parameter_scale_is_valid",
"assert_placeholders_only_used_in_own_observable",
"assert_unique_observable_ids",
"assert_unique_parameter_ids",
"check_condition_df",
Expand Down Expand Up @@ -349,6 +350,111 @@ def check_observable_df(observable_df: pd.DataFrame) -> None:
) from e


def assert_placeholders_only_used_in_own_observable(
problem: "petab.Problem",
) -> None:
"""Check that placeholders are not defined or used elsewhere.

Placeholder parameters are overridden per measurement. A model entity,
a condition table column, a mapping table entry, or an observable with
the same ID conflicts with that, as does the use of the placeholder in
another observable, the condition table, or the measurement table.
Noise placeholders must not be used in the observable formula.
Missing tables or a missing model are skipped.

Arguments:
problem: PEtab problem to check

Raises:
AssertionError: in case of problems
"""
from .observables import get_formula_placeholders

if (observable_df := problem.observable_df) is None:
return
model = problem.model
condition_df = problem.condition_df
mapping_df = problem.mapping_df
measurement_df = problem.measurement_df

def symbols(expression) -> set[str]:
if not isinstance(expression, str):
return set()
return {str(s) for s in sympify_petab(expression).free_symbols}

# placeholder -> ID of the observable that declares it
placeholders: dict[str, str] = {}
# ID -> (where it is used or defined, observable ID or None)
uses: dict[str, list[tuple[str, str | None]]] = defaultdict(list)

for observable_id, row in observable_df.iterrows():
uses[observable_id].append(("observable ID", None))
formula = row.get(OBSERVABLE_FORMULA)
noise_formula = row.get(NOISE_FORMULA)
formula_symbols = symbols(formula)
for symbol in formula_symbols | symbols(noise_formula):
uses[symbol].append(
(f"used in observable `{observable_id}`", observable_id)
)
noise_placeholders = get_formula_placeholders(
noise_formula, observable_id, "noise"
)
for placeholder in (
get_formula_placeholders(formula, observable_id, "observable")
+ noise_placeholders
):
placeholders[placeholder] = observable_id
for placeholder in formula_symbols.intersection(noise_placeholders):
uses[placeholder].append(("used in the observable formula", None))

if condition_df is not None:
for column in condition_df.columns.drop(
CONDITION_NAME, errors="ignore"
):
uses[column].append(("condition table column", None))
for value in condition_df[column].unique():
for symbol in symbols(value):
uses[symbol].append(("used in the condition table", None))

if mapping_df is not None:
for petab_id in mapping_df.index:
uses[petab_id].append(("mapping table petabEntityId", None))

if measurement_df is not None:
for column in (OBSERVABLE_PARAMETERS, NOISE_PARAMETERS):
if column not in measurement_df:
continue
for value in measurement_df[column].unique():
for override in measurements.split_parameter_replacement_list(
value
):
if isinstance(override, str):
uses[override].append(
("used in the measurement table", None)
)

clashes = []
for placeholder, observable_id in placeholders.items():
where = [
use
for use, owner in dict.fromkeys(uses.get(placeholder, []))
if owner != observable_id
]
if model is not None and model.has_entity_with_id(placeholder):
where.append("model entity")
if where:
clashes.append(
f"`{placeholder}` of observable `{observable_id}` "
f"({', '.join(where)})"
)

if clashes:
raise AssertionError(
"Placeholder parameters must only be used in their own "
"observable: " + "; ".join(clashes)
)


def assert_all_parameters_present_in_parameter_df(
parameter_df: pd.DataFrame,
model: Model,
Expand Down Expand Up @@ -929,6 +1035,11 @@ def lint_problem(problem: "petab.Problem") -> bool:
except AssertionError as e:
logger.error(e)
errors_occurred = True
try:
assert_placeholders_only_used_in_own_observable(problem)
except AssertionError as e:
logger.error(e)
errors_occurred = True
if problem.model is not None:
for obs_id in problem.observable_df.index:
if problem.model.has_entity_with_id(obs_id):
Expand Down
160 changes: 160 additions & 0 deletions tests/v1/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,166 @@ def test_check_observable_df():
lint.check_observable_df(bad_observable_df)


def _placeholder_problem(
model: str = "species x = 1; k = 1; p = 1",
) -> petab.Problem:
"""A valid problem with placeholders of observable `obs1`."""
from petab.v1.models.sbml_model import SbmlModel

return petab.Problem(
model=SbmlModel.from_antimony(model),
condition_df=pd.DataFrame(
{CONDITION_ID: ["c1"], "k": [1.0]}
).set_index(CONDITION_ID),
observable_df=pd.DataFrame(
{
OBSERVABLE_ID: ["obs1"],
OBSERVABLE_FORMULA: ["observableParameter1_obs1 * x"],
NOISE_FORMULA: ["noiseParameter1_obs1"],
}
).set_index(OBSERVABLE_ID),
measurement_df=pd.DataFrame(
{
OBSERVABLE_ID: ["obs1"],
SIMULATION_CONDITION_ID: ["c1"],
TIME: [1.0],
MEASUREMENT: [1.0],
OBSERVABLE_PARAMETERS: ["2"],
NOISE_PARAMETERS: ["1"],
}
),
parameter_df=pd.DataFrame(
{
PARAMETER_ID: ["p"],
PARAMETER_SCALE: [LIN],
LOWER_BOUND: [0.0],
UPPER_BOUND: [10.0],
NOMINAL_VALUE: [1.0],
ESTIMATE: [1],
}
).set_index(PARAMETER_ID),
)


def _set_column(table: str, column: str, value):
"""Get a function that sets a column of a table of a problem."""

def modify(problem):
getattr(problem, table)[column] = value

return modify


def _add_observable(problem, observable_id, formula):
problem.observable_df.loc[observable_id] = [formula, "1"]


@pytest.mark.parametrize(
"modify, expected",
[
(
lambda p: setattr(
p,
"model",
_placeholder_problem(
"species x = 1; k = 1; p = 1; noiseParameter1_obs1 = 1"
).model,
),
"`noiseParameter1_obs1` of observable `obs1` (model entity)",
),
(
_set_column("condition_df", "observableParameter1_obs1", 1.0),
(
"`observableParameter1_obs1` of observable `obs1` "
"(condition table column)"
),
),
(
lambda p: setattr(
p,
"mapping_df",
pd.DataFrame(
{
PETAB_ENTITY_ID: ["observableParameter1_obs1"],
MODEL_ENTITY_ID: ["k"],
}
).set_index(PETAB_ENTITY_ID),
),
(
"`observableParameter1_obs1` of observable `obs1` "
"(mapping table petabEntityId)"
),
),
(
lambda p: _add_observable(p, "observableParameter1_obs1", "x"),
"`observableParameter1_obs1` of observable `obs1` (observable ID)",
),
(
lambda p: _add_observable(p, "obs2", "noiseParameter1_obs1 * x"),
(
"`noiseParameter1_obs1` of observable `obs1` "
"(used in observable `obs2`)"
),
),
(
_set_column("condition_df", "k", "observableParameter1_obs1 + 1"),
(
"`observableParameter1_obs1` of observable `obs1` "
"(used in the condition table)"
),
),
(
_set_column(
"measurement_df", NOISE_PARAMETERS, "observableParameter1_obs1"
),
(
"`observableParameter1_obs1` of observable `obs1` "
"(used in the measurement table)"
),
),
(
_set_column(
"observable_df",
OBSERVABLE_FORMULA,
"observableParameter1_obs1 * x * noiseParameter1_obs1",
),
(
"`noiseParameter1_obs1` of observable `obs1` "
"(used in the observable formula)"
),
),
],
)
def test_assert_placeholders_only_used_in_own_observable(modify, expected):
problem = _placeholder_problem()
lint.assert_placeholders_only_used_in_own_observable(problem)

modify(problem)
with pytest.raises(AssertionError) as excinfo:
lint.assert_placeholders_only_used_in_own_observable(problem)
assert str(excinfo.value).endswith(expected)


def test_assert_placeholders_only_used_in_own_observable_missing_column():
problem = _placeholder_problem()
problem.observable_df = problem.observable_df.drop(columns=NOISE_FORMULA)
lint.assert_placeholders_only_used_in_own_observable(problem)


def test_lint_problem_placeholders_only_used_in_own_observable():
assert lint.lint_problem(_placeholder_problem()) is False
problem = _placeholder_problem(
"species x = 1; k = 1; p = 1; observableParameter1_obs1 = 1"
)
with patch.object(lint.logger, "error") as error:
assert lint.lint_problem(problem) is True
assert any(
"`observableParameter1_obs1` of observable `obs1` (model entity)"
in str(call.args[0])
for call in error.call_args_list
)


def test_condition_ids_are_unique():
condition_df = pd.DataFrame(
data={
Expand Down
Loading