Skip to content

Commit bf53e2c

Browse files
dweindlclaude
andcommitted
v2 lint: warn about unused placeholders
Add `CheckUnusedPlaceholders` to the default validation tasks. It warns about entries of `observablePlaceholders` that are used in neither `observableFormula` nor `noiseFormula`, and about entries of `noisePlaceholders` that are not used in `noiseFormula`. Closes #587. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fa87ed3 commit bf53e2c

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

‎petab/v2/lint.py‎

Lines changed: 55 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,59 @@ 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+
764+
def run(self, problem: Problem) -> ValidationIssue | None:
765+
messages = []
766+
for o in problem.observables:
767+
obs_syms = _free_symbol_names(o.formula)
768+
noise_syms = _free_symbol_names(o.noise_formula)
769+
unused_obs = [
770+
str(p)
771+
for p in o.observable_placeholders
772+
if str(p) not in obs_syms | noise_syms
773+
]
774+
unused_noise = [
775+
str(p)
776+
for p in o.noise_placeholders
777+
if str(p) not in noise_syms
778+
]
779+
if unused_obs:
780+
messages.append(
781+
f"Observable `{o.id}`: observable placeholders "
782+
f"{_fmt_ids(unused_obs)} are not used in the observable "
783+
"or noise formula."
784+
)
785+
if unused_noise:
786+
messages.append(
787+
f"Observable `{o.id}`: noise placeholders "
788+
f"{_fmt_ids(unused_noise)} are not used in the noise "
789+
"formula."
790+
)
791+
792+
if messages:
793+
return ValidationWarning("\n".join(messages))
794+
795+
return None
796+
797+
798+
def _fmt_ids(ids: list[str]) -> str:
799+
"""Format IDs as a comma-separated list of backticked IDs."""
800+
return ", ".join(f"`{i}`" for i in ids)
801+
802+
803+
def _free_symbol_names(expr: sp.Basic | None) -> set[str]:
804+
"""Get the names of the free symbols in an expression."""
805+
if expr is None:
806+
return set()
807+
return {str(s) for s in expr.free_symbols}
808+
809+
756810
class CheckInitialChangeSymbols(ValidationTask):
757811
"""
758812
Check that changes of any first period of any experiment only refers to
@@ -1197,6 +1251,7 @@ def get_placeholders(
11971251
CheckValidParameterInConditionOrParameterTable(),
11981252
CheckUnusedExperiments(),
11991253
CheckUnusedConditions(),
1254+
CheckUnusedPlaceholders(),
12001255
CheckPriorDistribution(),
12011256
CheckInitialChangeSymbols(),
12021257
CheckMappingTable(),

‎tests/v2/test_lint.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,48 @@ 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

0 commit comments

Comments
 (0)