From 0cd6659c955354247c2d1cf9105d0a5656039843 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 16 Nov 2026 20:51:21 +0100 Subject: [PATCH] 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 --- changelog.d/+unused-placeholders.feature.md | 3 + petab/v2/lint.py | 64 +++++++++++++++++++++ tests/v2/test_lint.py | 46 +++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 changelog.d/+unused-placeholders.feature.md diff --git a/changelog.d/+unused-placeholders.feature.md b/changelog.d/+unused-placeholders.feature.md new file mode 100644 index 00000000..bfb0fa6c --- /dev/null +++ b/changelog.d/+unused-placeholders.feature.md @@ -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)) diff --git a/petab/v2/lint.py b/petab/v2/lint.py index 7222af48..3d90976a 100644 --- a/petab/v2/lint.py +++ b/petab/v2/lint.py @@ -43,6 +43,7 @@ "CheckUniquePrimaryKeys", "CheckUnusedConditions", "CheckUnusedExperiments", + "CheckUnusedPlaceholders", "CheckValidConditionTargets", "CheckValidParameterInConditionOrParameterTable", "ValidationError", @@ -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 @@ -1197,6 +1260,7 @@ def get_placeholders( CheckValidParameterInConditionOrParameterTable(), CheckUnusedExperiments(), CheckUnusedConditions(), + CheckUnusedPlaceholders(), CheckPriorDistribution(), CheckInitialChangeSymbols(), CheckMappingTable(), diff --git a/tests/v2/test_lint.py b/tests/v2/test_lint.py index df5ccc38..f0405daf 100644 --- a/tests/v2/test_lint.py +++ b/tests/v2/test_lint.py @@ -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