Skip to content

Commit ece6e56

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 09c1998 commit ece6e56

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
@@ -1195,6 +1249,7 @@ def get_placeholders(
11951249
CheckValidParameterInConditionOrParameterTable(),
11961250
CheckUnusedExperiments(),
11971251
CheckUnusedConditions(),
1252+
CheckUnusedPlaceholders(),
11981253
CheckPriorDistribution(),
11991254
CheckInitialChangeSymbols(),
12001255
CheckMappingTable(),

‎tests/v2/test_lint.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,48 @@ def test_check_mapping_table(uses_pysb):
167167
"`A` is used in the mapping table and referenced directly"
168168
in error.message
169169
)
170+
171+
172+
def test_check_unused_placeholders():
173+
"""Test detection of declared placeholders that occur in no formula."""
174+
problem = Problem()
175+
problem.model = SbmlModel.from_antimony("xa = 1; xa' = -0.1*xa;")
176+
check = CheckUnusedPlaceholders()
177+
178+
# observable placeholder used only in the noise formula is fine
179+
problem.add_observable(
180+
"obs_a",
181+
formula="xa + offset",
182+
noise_formula="sigma * scale",
183+
observable_placeholders=["offset", "scale"],
184+
noise_placeholders=["sigma"],
185+
)
186+
assert check.run(problem) is None
187+
188+
problem.add_observable(
189+
"obs_b",
190+
formula="xa + offset",
191+
noise_formula="1",
192+
observable_placeholders=["unused_obs", "offset"],
193+
noise_placeholders=["unused_noise"],
194+
)
195+
# noise placeholder occurring only in the observable formula is unused
196+
problem.add_observable(
197+
"obs_c",
198+
formula="xa * sigma",
199+
noise_formula="1",
200+
noise_placeholders=["sigma"],
201+
)
202+
issue = check.run(problem)
203+
assert issue.level == ValidationIssueSeverity.WARNING
204+
assert "obs_a" not in issue.message
205+
assert (
206+
"`obs_b`: observable placeholders `unused_obs` are not used"
207+
in issue.message
208+
)
209+
assert (
210+
"`obs_b`: noise placeholders `unused_noise` are not used"
211+
in issue.message
212+
)
213+
assert "`obs_c`: noise placeholders `sigma` are not used" in issue.message
214+
assert "offset" not in issue.message

0 commit comments

Comments
 (0)