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.d/+unused-placeholders.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
New validation task `CheckUnusedPlaceholders` warns about observable and
noise placeholders that are not used in the respective formulas
([#599](https://github.com/PEtab-dev/libpetab-python/pull/599))
64 changes: 64 additions & 0 deletions petab/v2/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"CheckUniquePrimaryKeys",
"CheckUnusedConditions",
"CheckUnusedExperiments",
"CheckUnusedPlaceholders",
"CheckValidConditionTargets",
"CheckValidParameterInConditionOrParameterTable",
"ValidationError",
Expand Down Expand Up @@ -753,6 +754,68 @@ def run(self, problem: Problem) -> ValidationIssue | None:
return None


class CheckUnusedPlaceholders(ValidationTask):
"""A task to check for placeholders that are not used in any formula.

Observable placeholders may occur in the observable formula or the noise
formula; noise placeholders may only occur in the noise formula.

Unused placeholders are valid PEtab, but they still require override
values in the measurement table, add complexity without any effect,
and may indicate a typo in a formula. Therefore, a warning is issued.
"""

def run(self, problem: Problem) -> ValidationIssue | None:
messages = []
for o in problem.observables:
obs_syms = _free_symbol_names(o.formula)
noise_syms = _free_symbol_names(o.noise_formula)
unused_obs = [
str(p)
for p in o.observable_placeholders
if str(p) not in obs_syms | noise_syms
]
unused_noise = [
str(p)
for p in o.noise_placeholders
if str(p) not in noise_syms
]
if unused_obs:
messages.append(
f"Observable `{o.id}`: observable placeholders "
f"{_fmt_ids(unused_obs)} are not used in the observable "
"or noise formula."
)
if unused_noise:
messages.append(
f"Observable `{o.id}`: noise placeholders "
f"{_fmt_ids(unused_noise)} are not used in the noise "
"formula."
)

if messages:
messages.append(
"Unused placeholders are allowed, but they have no effect and "
"still require override values in the measurement table. "
"Consider removing them."
)
return ValidationWarning("\n".join(messages))

return None


def _fmt_ids(ids: list[str]) -> str:
"""Format IDs as a comma-separated list of backticked IDs."""
return ", ".join(f"`{i}`" for i in ids)


def _free_symbol_names(expr: sp.Basic | None) -> set[str]:
"""Get the names of the free symbols in an expression."""
if expr is None:
return set()
return {str(s) for s in expr.free_symbols}


class CheckInitialChangeSymbols(ValidationTask):
"""
Check that changes of any first period of any experiment only refers to
Expand Down Expand Up @@ -1197,6 +1260,7 @@ def get_placeholders(
CheckValidParameterInConditionOrParameterTable(),
CheckUnusedExperiments(),
CheckUnusedConditions(),
CheckUnusedPlaceholders(),
CheckPriorDistribution(),
CheckInitialChangeSymbols(),
CheckMappingTable(),
Expand Down
46 changes: 46 additions & 0 deletions tests/v2/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,49 @@ def test_time_in_observable_formula_not_a_parameter(clean_pysb_exports):
assert TIME_SYMBOL not in problem.get_output_parameters()
check = CheckAllParametersPresentInParameterTable()
assert (error := check.run(problem)) is None, error


def test_check_unused_placeholders():
"""Test detection of declared placeholders that occur in no formula."""
problem = Problem()
problem.model = SbmlModel.from_antimony("xa = 1; xa' = -0.1*xa;")
check = CheckUnusedPlaceholders()

# observable placeholder used only in the noise formula is fine
problem.add_observable(
"obs_a",
formula="xa + offset",
noise_formula="sigma * scale",
observable_placeholders=["offset", "scale"],
noise_placeholders=["sigma"],
)
assert check.run(problem) is None

problem.add_observable(
"obs_b",
formula="xa + offset",
noise_formula="1",
observable_placeholders=["unused_obs", "offset"],
noise_placeholders=["unused_noise"],
)
# noise placeholder occurring only in the observable formula is unused
problem.add_observable(
"obs_c",
formula="xa * sigma",
noise_formula="1",
noise_placeholders=["sigma"],
)
issue = check.run(problem)
assert issue.level == ValidationIssueSeverity.WARNING
assert "obs_a" not in issue.message
assert (
"`obs_b`: observable placeholders `unused_obs` are not used"
in issue.message
)
assert (
"`obs_b`: noise placeholders `unused_noise` are not used"
in issue.message
)
assert "`obs_c`: noise placeholders `sigma` are not used" in issue.message
assert "offset" not in issue.message
assert "Consider removing them." in issue.message
Loading