Skip to content

Commit 7149d19

Browse files
dweindlclaude
andauthored
v2 lint: warn about unused placeholders (#599)
Adds `CheckUnusedPlaceholders` to the default v2 validation tasks. It emits a warning for * entries of `observablePlaceholders` that are used in neither `observableFormula` nor `noiseFormula` (observable placeholders are allowed in the noise formula), * entries of `noisePlaceholders` that are not used in `noiseFormula`. The spec allows declaring placeholders that are not used, so this is a warning, not an error. Usage is determined from the sympified formulas, so a placeholder that cancels out (e.g. `x + p - p`) is reported as unused. Closes #587. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b45a01c commit 7149d19

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
New validation task `CheckUnusedPlaceholders` warns about observable and
2+
noise placeholders that are not used in the respective formulas
3+
([#599](https://github.com/PEtab-dev/libpetab-python/pull/599))

‎petab/v2/lint.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"CheckUniquePrimaryKeys",
4444
"CheckUnusedConditions",
4545
"CheckUnusedExperiments",
46+
"CheckUnusedPlaceholders",
4647
"CheckValidConditionTargets",
4748
"CheckValidParameterInConditionOrParameterTable",
4849
"ValidationError",
@@ -753,6 +754,68 @@ def run(self, problem: Problem) -> ValidationIssue | None:
753754
return None
754755

755756

757+
class CheckUnusedPlaceholders(ValidationTask):
758+
"""A task to check for placeholders that are not used in any formula.
759+
760+
Observable placeholders may occur in the observable formula or the noise
761+
formula; noise placeholders may only occur in the noise formula.
762+
763+
Unused placeholders are valid PEtab, but they still require override
764+
values in the measurement table, add complexity without any effect,
765+
and may indicate a typo in a formula. Therefore, a warning is issued.
766+
"""
767+
768+
def run(self, problem: Problem) -> ValidationIssue | None:
769+
messages = []
770+
for o in problem.observables:
771+
obs_syms = _free_symbol_names(o.formula)
772+
noise_syms = _free_symbol_names(o.noise_formula)
773+
unused_obs = [
774+
str(p)
775+
for p in o.observable_placeholders
776+
if str(p) not in obs_syms | noise_syms
777+
]
778+
unused_noise = [
779+
str(p)
780+
for p in o.noise_placeholders
781+
if str(p) not in noise_syms
782+
]
783+
if unused_obs:
784+
messages.append(
785+
f"Observable `{o.id}`: observable placeholders "
786+
f"{_fmt_ids(unused_obs)} are not used in the observable "
787+
"or noise formula."
788+
)
789+
if unused_noise:
790+
messages.append(
791+
f"Observable `{o.id}`: noise placeholders "
792+
f"{_fmt_ids(unused_noise)} are not used in the noise "
793+
"formula."
794+
)
795+
796+
if messages:
797+
messages.append(
798+
"Unused placeholders are allowed, but they have no effect and "
799+
"still require override values in the measurement table. "
800+
"Consider removing them."
801+
)
802+
return ValidationWarning("\n".join(messages))
803+
804+
return None
805+
806+
807+
def _fmt_ids(ids: list[str]) -> str:
808+
"""Format IDs as a comma-separated list of backticked IDs."""
809+
return ", ".join(f"`{i}`" for i in ids)
810+
811+
812+
def _free_symbol_names(expr: sp.Basic | None) -> set[str]:
813+
"""Get the names of the free symbols in an expression."""
814+
if expr is None:
815+
return set()
816+
return {str(s) for s in expr.free_symbols}
817+
818+
756819
class CheckInitialChangeSymbols(ValidationTask):
757820
"""
758821
Check that changes of any first period of any experiment only refers to
@@ -1197,6 +1260,7 @@ def get_placeholders(
11971260
CheckValidParameterInConditionOrParameterTable(),
11981261
CheckUnusedExperiments(),
11991262
CheckUnusedConditions(),
1263+
CheckUnusedPlaceholders(),
12001264
CheckPriorDistribution(),
12011265
CheckInitialChangeSymbols(),
12021266
CheckMappingTable(),

‎tests/v2/test_lint.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,49 @@ def test_time_in_observable_formula_not_a_parameter(clean_pysb_exports):
218218
assert TIME_SYMBOL not in problem.get_output_parameters()
219219
check = CheckAllParametersPresentInParameterTable()
220220
assert (error := check.run(problem)) is None, error
221+
222+
223+
def test_check_unused_placeholders():
224+
"""Test detection of declared placeholders that occur in no formula."""
225+
problem = Problem()
226+
problem.model = SbmlModel.from_antimony("xa = 1; xa' = -0.1*xa;")
227+
check = CheckUnusedPlaceholders()
228+
229+
# observable placeholder used only in the noise formula is fine
230+
problem.add_observable(
231+
"obs_a",
232+
formula="xa + offset",
233+
noise_formula="sigma * scale",
234+
observable_placeholders=["offset", "scale"],
235+
noise_placeholders=["sigma"],
236+
)
237+
assert check.run(problem) is None
238+
239+
problem.add_observable(
240+
"obs_b",
241+
formula="xa + offset",
242+
noise_formula="1",
243+
observable_placeholders=["unused_obs", "offset"],
244+
noise_placeholders=["unused_noise"],
245+
)
246+
# noise placeholder occurring only in the observable formula is unused
247+
problem.add_observable(
248+
"obs_c",
249+
formula="xa * sigma",
250+
noise_formula="1",
251+
noise_placeholders=["sigma"],
252+
)
253+
issue = check.run(problem)
254+
assert issue.level == ValidationIssueSeverity.WARNING
255+
assert "obs_a" not in issue.message
256+
assert (
257+
"`obs_b`: observable placeholders `unused_obs` are not used"
258+
in issue.message
259+
)
260+
assert (
261+
"`obs_b`: noise placeholders `unused_noise` are not used"
262+
in issue.message
263+
)
264+
assert "`obs_c`: noise placeholders `sigma` are not used" in issue.message
265+
assert "offset" not in issue.message
266+
assert "Consider removing them." in issue.message

0 commit comments

Comments
 (0)