From 144ca60790f585705a695dd7269d42a17e37ea14 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Tue, 17 Nov 2026 11:21:46 +0100 Subject: [PATCH 1/2] v2 lint: check that identifiers are globally unique Add `CheckGloballyUniqueIds`. Condition, experiment, observable, model and placeholder IDs, mapping table `petabEntityId`s and parameter IDs must be defined only once and must not be model entity IDs. Parameter table entries for model entities or mapped entities are references, not definitions. The task replaces `CheckObservablesDoNotShadowModelEntities` in the default validation tasks, which is deprecated, and the overlapping checks in `CheckMappingTable`. Closes #555. Co-Authored-By: Claude Opus 5.5 --- changelog.d/+globally-unique-ids.breaking.md | 6 + .../+shadow-model-entities.deprecation.md | 3 + petab/v2/lint.py | 140 ++++++++++----- tests/v1/test_model_bngl.py | 2 +- tests/v2/test_lint.py | 170 +++++++++++++++++- 5 files changed, 271 insertions(+), 50 deletions(-) create mode 100644 changelog.d/+globally-unique-ids.breaking.md create mode 100644 changelog.d/+shadow-model-entities.deprecation.md diff --git a/changelog.d/+globally-unique-ids.breaking.md b/changelog.d/+globally-unique-ids.breaking.md new file mode 100644 index 00000000..020e9536 --- /dev/null +++ b/changelog.d/+globally-unique-ids.breaking.md @@ -0,0 +1,6 @@ +PEtab v2 validation: new validation task `CheckGloballyUniqueIds`: +identifiers, including observable and noise placeholders, must be unique +within the problem and must not be model entity IDs. It replaces +`CheckObservablesDoNotShadowModelEntities` in the default validation tasks +and the corresponding checks in `CheckMappingTable` +([#606](https://github.com/PEtab-dev/libpetab-python/pull/606)) diff --git a/changelog.d/+shadow-model-entities.deprecation.md b/changelog.d/+shadow-model-entities.deprecation.md new file mode 100644 index 00000000..d3d823ad --- /dev/null +++ b/changelog.d/+shadow-model-entities.deprecation.md @@ -0,0 +1,3 @@ +`petab.v2.lint.CheckObservablesDoNotShadowModelEntities` is deprecated in +favor of `CheckGloballyUniqueIds` +([#606](https://github.com/PEtab-dev/libpetab-python/pull/606)) diff --git a/petab/v2/lint.py b/petab/v2/lint.py index 3d90976a..07717a2f 100644 --- a/petab/v2/lint.py +++ b/petab/v2/lint.py @@ -2,10 +2,12 @@ from __future__ import annotations +import functools import logging import typing +import warnings from abc import ABC, abstractmethod -from collections import Counter, OrderedDict +from collections import Counter, OrderedDict, defaultdict from dataclasses import dataclass, field from enum import IntEnum from itertools import chain @@ -24,6 +26,7 @@ "CheckArrayDataFiles", "CheckExperimentConditionsExist", "CheckExperimentTable", + "CheckGloballyUniqueIds", "CheckHybridizationTable", "CheckInitialChangeSymbols", "CheckMappingTable", @@ -447,9 +450,6 @@ class CheckUniquePrimaryKeys(ValidationTask): """Check that all primary keys are unique.""" def run(self, problem: Problem) -> ValidationIssue | None: - # TODO: check that IDs are globally unique - # -- replaces CheckObservablesDoNotShadowModelEntities - # check for uniqueness of all primary keys counter = Counter(c.id for c in problem.conditions) duplicates = sorted(id_ for id_, count in counter.items() if count > 1) @@ -486,10 +486,99 @@ def run(self, problem: Problem) -> ValidationIssue | None: return None +class CheckGloballyUniqueIds(ValidationTask): + """Check that identifiers are unique within the PEtab problem. + + Condition, experiment, observable, model and placeholder IDs, + mapping table ``petabEntityId`` values and parameter IDs must not be + defined more than once, and must not be model entity IDs. + Parameter table entries for model entities or mapped entities are + references, not definitions. + Duplicates within a single table are reported by + :class:`CheckUniquePrimaryKeys`. + """ + + def run(self, problem: Problem) -> ValidationIssue | None: + # ID -> where it is defined + definitions: dict[str, list[str]] = defaultdict(list) + + def add(ids: typing.Iterable[str], source: str) -> None: + for id_ in dict.fromkeys(ids): + definitions[id_].append(source) + + add((c.id for c in problem.conditions), "condition ID") + add((e.id for e in problem.experiments), "experiment ID") + add((o.id for o in problem.observables), "observable ID") + add( + (m.model_id for m in problem.models if m.model_id), + "model ID", + ) + for o in problem.observables: + # not deduplicated: a placeholder must occur only once + for placeholder in o.observable_placeholders: + definitions[str(placeholder)].append( + f"observable placeholder of `{o.id}`" + ) + for placeholder in o.noise_placeholders: + definitions[str(placeholder)].append( + f"noise placeholder of `{o.id}`" + ) + # identity mappings and annotation-only rows don't define IDs + add( + ( + m.petab_id + for m in problem.mappings + if m.model_id and m.petab_id != m.model_id + ), + "mapping table petabEntityId", + ) + + @functools.cache + def is_model_entity(id_: str) -> bool: + return any(m.has_entity_with_id(id_) for m in problem.models) + + mapped_ids = {m.petab_id for m in problem.mappings if m.model_id} + add( + ( + p.id + for p in problem.parameters + if p.id not in mapped_ids and not is_model_entity(p.id) + ), + "parameter ID", + ) + + for id_, sources in definitions.items(): + if is_model_entity(id_): + sources.append("model entity") + + if clashes := [ + f"`{id_}` ({', '.join(sources)})" + for id_, sources in definitions.items() + if len(sources) > 1 + ]: + return ValidationError( + "Identifiers must be unique within the PEtab problem: " + + "; ".join(clashes) + ) + + return None + + class CheckObservablesDoNotShadowModelEntities(ValidationTask): - """A task to check that observable IDs do not shadow model entities.""" + """A task to check that observable IDs do not shadow model entities. + + .. deprecated:: 0.11.0 + Use :class:`CheckGloballyUniqueIds`. + """ + + def __init__(self): + warnings.warn( + "`CheckObservablesDoNotShadowModelEntities` is deprecated, " + "use `CheckGloballyUniqueIds` instead.", + DeprecationWarning, + stacklevel=2, + ) - # TODO: all PEtab entity IDs must be disjoint from the model entity IDs def run(self, problem: Problem) -> ValidationIssue | None: if not problem.observables or problem.model is None: return None @@ -981,8 +1070,6 @@ def run(self, problem: Problem) -> ValidationIssue | None: if not problem.mappings: return None - messages = [] - # Check that each id, across both the petabEntityId and # modelEntityId columns, occurs only once must_be_unique_ids = [] @@ -1008,41 +1095,6 @@ def run(self, problem: Problem) -> ValidationIssue | None: f"Mapping table contains non-unique IDs: {non_unique_ids}." ) - # petabEntityId is not defined elsewhere in the PEtab problem - new_petab_ids = { - m.petab_id - for m in problem.mappings - # Ignore identity mappings and annotation-only rows - if m.model_id and m.petab_id != m.model_id - } - old_petab_ids = ( - {c.id for c in problem.conditions} - | {e.id for e in problem.experiments} - | {o.id for o in problem.observables} - ) - if overdefined_ids := sorted(new_petab_ids & old_petab_ids): - messages.append( - f"PEtab IDs `{overdefined_ids}` are " - "defined in the mapping table but also defined through " - "other PEtab tables." - ) - - for mapping in problem.mappings: - # petabEntityId not referenced in any model, if alias - for model in problem.models: - if ( - mapping.petab_id != mapping.model_id - and model.has_entity_with_id(mapping.petab_id) - ): - messages.append( - f"`{mapping.petab_id}` is used in the mapping " - "table and referenced directly in the model " - f"`{model.model_id}`." - ) - - if messages: - return ValidationError("\n".join(messages)) - return None @@ -1255,7 +1307,7 @@ def get_placeholders( CheckExperimentTable(), CheckExperimentConditionsExist(), CheckUndefinedExperiments(), - CheckObservablesDoNotShadowModelEntities(), + CheckGloballyUniqueIds(), CheckAllParametersPresentInParameterTable(), CheckValidParameterInConditionOrParameterTable(), CheckUnusedExperiments(), diff --git a/tests/v1/test_model_bngl.py b/tests/v1/test_model_bngl.py index b4641faf..2d6f8198 100644 --- a/tests/v1/test_model_bngl.py +++ b/tests/v1/test_model_bngl.py @@ -259,7 +259,7 @@ def test_model_factory_routes_bngl(): def test_full_petab_validation_is_clean(tmp_path): """A ``language: bngl`` problem loads via ``Problem.from_yaml`` and passes every default validation task -- including the model-cross checks that - read the BnglModel (CheckModel, CheckObservablesDoNotShadowModelEntities, + read the BnglModel (CheckModel, CheckGloballyUniqueIds, CheckAllParametersPresentInParameterTable, CheckValidConditionTargets, CheckInitialChangeSymbols). """ diff --git a/tests/v2/test_lint.py b/tests/v2/test_lint.py index ac2b3b49..695d44bf 100644 --- a/tests/v2/test_lint.py +++ b/tests/v2/test_lint.py @@ -159,11 +159,9 @@ def test_check_mapping_table(clean_pysb_exports): problem.model = PySBModel( model=pysb_model_invalid, model_id="test_model_invalid" ) - assert (error := check.run(problem)) is not None - assert ( - "`A` is used in the mapping table and referenced directly" - in error.message - ) + assert check.run(problem) is None + assert (error := CheckGloballyUniqueIds().run(problem)) is not None + assert "`A` (mapping table petabEntityId, model entity)" in error.message def test_pysb_species_with_sites_as_condition_target( @@ -290,3 +288,165 @@ def test_check_prior_distribution_invalid_uniform( assert (error := CheckPriorDistribution().run(problem)) is not None assert "`p1` are invalid" in error.message assert hint in error.message + + +def _unique_ids_problem() -> Problem: + problem = Problem() + problem.model = SbmlModel.from_antimony( + "species x = 1; k = 1", model_id="model1" + ) + problem.add_observable( + "o1", "a * x", noise_formula="1", observable_placeholders=["a"] + ) + return problem + + +@pytest.mark.parametrize( + "modify, expected", + [ + ( + lambda p: p.add_observable( + "o2", "a", noise_formula="1", observable_placeholders=["a"] + ), + ( + "`a` (observable placeholder of `o1`, " + "observable placeholder of `o2`)" + ), + ), + ( + lambda p: p.add_observable( + "o2", + "b", + noise_formula="b", + observable_placeholders=["b"], + noise_placeholders=["b"], + ), + "`b` (observable placeholder of `o2`, noise placeholder of `o2`)", + ), + ( + lambda p: p.add_observable( + "o2", "x", noise_formula="1", observable_placeholders=["x"] + ), + "`x` (observable placeholder of `o2`, model entity)", + ), + ( + lambda p: p.add_observable( + "o2", "o1", noise_formula="1", observable_placeholders=["o1"] + ), + "`o1` (observable ID, observable placeholder of `o2`)", + ), + ( + lambda p: p.add_condition("a", k=2), + "`a` (condition ID, observable placeholder of `o1`)", + ), + ( + lambda p: p.add_experiment("a", 0, "c1"), + "`a` (experiment ID, observable placeholder of `o1`)", + ), + ( + lambda p: p.add_parameter("a", estimate=False, nominal_value=1), + "`a` (observable placeholder of `o1`, parameter ID)", + ), + ( + lambda p: p.add_mapping("a", "net.out[0]"), + ( + "`a` (observable placeholder of `o1`, " + "mapping table petabEntityId)" + ), + ), + ( + lambda p: ( + p.add_parameter("a", estimate=False, nominal_value=1), + p.add_mapping("a", None, name="annotation"), + ), + "`a` (observable placeholder of `o1`, parameter ID)", + ), + ( + lambda p: p.add_observable("x", "x", noise_formula="1"), + "`x` (observable ID, model entity)", + ), + ( + lambda p: ( + p.add_condition("c1", k=2), + p.add_experiment("c1", 0, "c1"), + ), + "`c1` (condition ID, experiment ID)", + ), + ( + lambda p: p.add_condition("model1", k=2), + "`model1` (condition ID, model ID)", + ), + ], +) +def test_check_globally_unique_ids(modify, expected): + """Test ``CheckGloballyUniqueIds``.""" + problem = _unique_ids_problem() + check = CheckGloballyUniqueIds() + assert check.run(problem) is None + + modify(problem) + assert (error := check.run(problem)) is not None + assert expected in error.message + assert error.message.count("`") == expected.count("`") + + +def test_check_globally_unique_ids_references(): + """References to model entities or mapped IDs are not definitions.""" + problem = _unique_ids_problem() + # model parameter in the parameter table and as condition target + problem.add_parameter("k", estimate=True, lb=0, ub=10, nominal_value=1) + problem.add_condition("c1", k=2) + problem.add_experiment("e1", 0, "c1") + # identity mapping + problem.add_mapping("x", "x") + # mapped entity in the parameter table + problem.add_mapping("w", "net.parameters[0]") + problem.add_parameter("w", estimate=True, lb=0, ub=10, nominal_value=1) + # annotation only + problem.add_mapping("o1", None, name="Observable 1") + + assert CheckGloballyUniqueIds().run(problem) is None + + +def test_check_globally_unique_ids_multi_model(): + """Model entities only clash with IDs used for the same model.""" + problem = Problem() + problem.models = [ + SbmlModel.from_antimony("species x = 1", model_id="m1"), + SbmlModel.from_antimony("species y = 1; o1 = 1", model_id="m2"), + ] + problem.add_observable("o1", "x", noise_formula="1") + problem.add_observable( + "o2", "a * y", noise_formula="1", observable_placeholders=["a"] + ) + problem.add_measurement("o1", time=1, measurement=1, model_id="m1") + problem.add_measurement( + "o2", + time=1, + measurement=1, + model_id="m2", + observable_parameters=[2], + ) + + def unique_id_issues(): + return [ + issue + for issue in problem.validate() + if issue.task == "CheckGloballyUniqueIds" + ] + + assert unique_id_issues() == [] + + problem.models[1] = SbmlModel.from_antimony( + "species y = 1; a = 1", model_id="m2" + ) + assert len(issues := unique_id_issues()) == 1 + assert issues[0].message.startswith("Model `m2`:") + assert "`a` (observable placeholder of `o2`, model entity)" in ( + issues[0].message + ) + + +def test_check_observables_do_not_shadow_model_entities_deprecated(): + with pytest.deprecated_call(): + CheckObservablesDoNotShadowModelEntities() From aacc56da39a749103d93e759881e068ceec8a24f Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Tue, 17 Nov 2026 11:46:44 +0100 Subject: [PATCH 2/2] v2 lint: compare tables across models, check placeholder use `CheckGloballyUniqueIds` now runs on the full problem of multi-model problems. Table entries are compared across all models. IDs from the condition, experiment, observable and parameter tables only clash with the entities of the models they are used with; mapping table IDs, model IDs and unused IDs are compared with all models. Each clash is reported once. Annotation-only mapping table rows must not refer to model entities. Add `CheckPlaceholdersOnlyUsedInOwnObservable`: placeholders must not be used in other observables, the condition table or measurement overrides, and noise placeholders must not be used in the observable formula. Co-Authored-By: Claude Opus 5.5 --- ...placeholders-own-observable-v2.breaking.md | 5 + petab/v2/core.py | 9 +- petab/v2/lint.py | 198 +++++++++++++++-- tests/v2/test_lint.py | 206 ++++++++++++++++-- 4 files changed, 376 insertions(+), 42 deletions(-) create mode 100644 changelog.d/+placeholders-own-observable-v2.breaking.md diff --git a/changelog.d/+placeholders-own-observable-v2.breaking.md b/changelog.d/+placeholders-own-observable-v2.breaking.md new file mode 100644 index 00000000..d5142302 --- /dev/null +++ b/changelog.d/+placeholders-own-observable-v2.breaking.md @@ -0,0 +1,5 @@ +PEtab v2 validation: new validation task +`CheckPlaceholdersOnlyUsedInOwnObservable`: placeholders must not be used in +other observables, the condition table or measurement overrides, and noise +placeholders must not be used in the observable formula +([#606](https://github.com/PEtab-dev/libpetab-python/pull/606)) diff --git a/petab/v2/core.py b/petab/v2/core.py index a315cb1b..dd3ccf51 100644 --- a/petab/v2/core.py +++ b/petab/v2/core.py @@ -2261,14 +2261,17 @@ def _validate_multi_model( Runs the validation tasks on each part of :meth:`Problem.split_by_model`. Checks of the shared tables - (model IDs of measurements, unique IDs, the mapping table, unused - experiments, conditions and parameters) run on the full problem. + (model IDs of measurements, unique IDs, placeholder use, the mapping + table, unused experiments, conditions and parameters) run on the + full problem. See https://github.com/PEtab-dev/libpetab-python/issues/392. """ from ..v2.lint import ( CheckAllParametersPresentInParameterTable, + CheckGloballyUniqueIds, CheckMappingTable, CheckMeasurementModelId, + CheckPlaceholdersOnlyUsedInOwnObservable, CheckUniquePrimaryKeys, CheckUnusedConditions, CheckUnusedExperiments, @@ -2295,6 +2298,8 @@ def _validate_multi_model( # the split drops unused or other models' table entries, # so checks of the shared tables have to run on the full problem full_problem_task_types = ( + CheckGloballyUniqueIds, + CheckPlaceholdersOnlyUsedInOwnObservable, CheckUniquePrimaryKeys, CheckMappingTable, CheckUnusedExperiments, diff --git a/petab/v2/lint.py b/petab/v2/lint.py index 07717a2f..4ef64fac 100644 --- a/petab/v2/lint.py +++ b/petab/v2/lint.py @@ -2,7 +2,6 @@ from __future__ import annotations -import functools import logging import typing import warnings @@ -37,6 +36,7 @@ "CheckNeuralNetworkModel", "CheckObservablesDoNotShadowModelEntities", "CheckOverridesMatchPlaceholders", + "CheckPlaceholdersOnlyUsedInOwnObservable", "CheckPosLogMeasurements", "CheckPriorDistribution", "CheckProblemConfig", @@ -492,14 +492,36 @@ class CheckGloballyUniqueIds(ValidationTask): Condition, experiment, observable, model and placeholder IDs, mapping table ``petabEntityId`` values and parameter IDs must not be defined more than once, and must not be model entity IDs. - Parameter table entries for model entities or mapped entities are - references, not definitions. + A parameter table entry for a model entity or a mapped entity refers + to that entity. + Annotation-only mapping table rows must not refer to model entities. Duplicates within a single table are reported by :class:`CheckUniquePrimaryKeys`. + + With multiple models, IDs from the condition, experiment, observable + and parameter tables only clash with the entities of the models they + are used with. """ def run(self, problem: Problem) -> ValidationIssue | None: - # ID -> where it is defined + definitions = self._get_definitions(problem) + self._add_model_entities(problem, definitions) + + if clashes := [ + f"`{id_}` ({', '.join(sources)})" + for id_, sources in definitions.items() + if len(sources) > 1 + ]: + return ValidationError( + "Identifiers must be unique within the PEtab problem: " + + "; ".join(clashes) + ) + + return None + + @staticmethod + def _get_definitions(problem: Problem) -> dict[str, list[str]]: + """Get the places where IDs are defined in the PEtab tables.""" definitions: dict[str, list[str]] = defaultdict(list) def add(ids: typing.Iterable[str], source: str) -> None: @@ -532,37 +554,166 @@ def add(ids: typing.Iterable[str], source: str) -> None: ), "mapping table petabEntityId", ) - - @functools.cache - def is_model_entity(id_: str) -> bool: - return any(m.has_entity_with_id(id_) for m in problem.models) - mapped_ids = {m.petab_id for m in problem.mappings if m.model_id} add( - ( - p.id - for p in problem.parameters - if p.id not in mapped_ids and not is_model_entity(p.id) - ), + (p.id for p in problem.parameters if p.id not in mapped_ids), "parameter ID", ) + return definitions + + @classmethod + def _add_model_entities( + cls, problem: Problem, definitions: dict[str, list[str]] + ) -> None: + """Add the model entities that IDs clash with to `definitions`.""" + annotations = {m.petab_id for m in problem.mappings if not m.model_id} + if len(problem.models) > 1: + try: + parts = problem.split_by_model() + except ValueError: + # reported by CheckMeasurementModelId + return + # mapping table and model IDs are shared by all models + shared_ids = annotations | { + id_ + for id_, sources in definitions.items() + if "model ID" in sources + or "mapping table petabEntityId" in sources + } + ids_by_model = { + model: set(cls._get_definitions(part)) | shared_ids + for part in parts.values() + for model in part.models + } + # IDs not used with any model, e.g., of unused conditions + unused_ids = set(definitions).difference(*ids_by_model.values()) + for ids in ids_by_model.values(): + ids |= unused_ids + else: + ids_by_model = { + model: set(definitions) | annotations + for model in problem.models + } - for id_, sources in definitions.items(): - if is_model_entity(id_): - sources.append("model entity") + for model in problem.models: + source = ( + "model entity" + if len(problem.models) == 1 + else f"entity of model `{model.model_id}`" + ) + for id_ in sorted(ids_by_model.get(model, ())): + is_annotation = id_ in annotations + # a parameter table entry for a model entity is no clash + if ( + definitions.get(id_) == ["parameter ID"] + and not is_annotation + ): + continue + if not model.has_entity_with_id(id_): + continue + if ( + is_annotation + and "mapping table annotation" not in definitions[id_] + ): + definitions[id_].append("mapping table annotation") + definitions[id_].append(source) - if clashes := [ - f"`{id_}` ({', '.join(sources)})" - for id_, sources in definitions.items() - if len(sources) > 1 - ]: + +class CheckPlaceholdersOnlyUsedInOwnObservable(ValidationTask): + """Check that placeholders are only used in their own observable. + + Placeholders are overridden per measurement. They must not be used in + other observables, in the condition table, or in measurement overrides, + and noise placeholders must not be used in the observable formula. + """ + + def run(self, problem: Problem) -> ValidationIssue | None: + # placeholder -> IDs of the observables that declare it + declared_by: dict[str, set[str]] = defaultdict(set) + for o in problem.observables: + for placeholder in chain( + o.observable_placeholders, o.noise_placeholders + ): + declared_by[str(placeholder)].add(o.id) + + uses = self._get_uses(problem) + clashes = [] + for placeholder, observable_ids in declared_by.items(): + # duplicate declarations are reported by CheckGloballyUniqueIds + where = [ + use + for use, observable_id in uses.get(placeholder, []) + if observable_id not in observable_ids + ] + if where: + observables = ", ".join( + f"`{x}`" for x in sorted(observable_ids) + ) + clashes.append( + f"`{placeholder}` of observable {observables} " + f"(used in {', '.join(where)})" + ) + + if clashes: return ValidationError( - "Identifiers must be unique within the PEtab problem: " + "Placeholders must only be used in their own observable: " + "; ".join(clashes) ) return None + @staticmethod + def _get_uses( + problem: Problem, + ) -> dict[str, list[tuple[str, str | None]]]: + """Get where symbols are used. + + :returns: Symbol ID -> list of (description, observable ID or + ``None``). + """ + uses: dict[str, list[tuple[str, str | None]]] = defaultdict(list) + + def add( + expressions: typing.Iterable[sp.Basic], + use: str, + observable_id: str | None = None, + ) -> None: + for symbol in dict.fromkeys( + str(s) for e in expressions for s in e.free_symbols + ): + uses[symbol].append((use, observable_id)) + + for o in problem.observables: + add([o.formula, o.noise_formula], f"observable `{o.id}`", o.id) + noise_only = set(map(str, o.noise_placeholders)) - set( + map(str, o.observable_placeholders) + ) + for symbol in map(str, o.formula.free_symbols): + if symbol in noise_only: + uses[symbol].append( + (f"observable formula of `{o.id}`", None) + ) + for c in problem.conditions: + for change in c.changes: + uses[change.target_id].append((f"condition `{c.id}`", None)) + add( + (change.target_value for change in c.changes), + f"condition `{c.id}`", + ) + add( + ( + override + for m in problem.measurements + for override in chain( + m.observable_parameters, m.noise_parameters + ) + ), + "measurement table", + ) + for symbol, symbol_uses in uses.items(): + uses[symbol] = list(dict.fromkeys(symbol_uses)) + return uses + class CheckObservablesDoNotShadowModelEntities(ValidationTask): """A task to check that observable IDs do not shadow model entities. @@ -1308,6 +1459,7 @@ def get_placeholders( CheckExperimentConditionsExist(), CheckUndefinedExperiments(), CheckGloballyUniqueIds(), + CheckPlaceholdersOnlyUsedInOwnObservable(), CheckAllParametersPresentInParameterTable(), CheckValidParameterInConditionOrParameterTable(), CheckUnusedExperiments(), diff --git a/tests/v2/test_lint.py b/tests/v2/test_lint.py index 695d44bf..ac0bd6af 100644 --- a/tests/v2/test_lint.py +++ b/tests/v2/test_lint.py @@ -4,6 +4,7 @@ import pysb import pytest +import sympy as sp from petab.v2 import Problem from petab.v2.C import TIME_SYMBOL @@ -390,6 +391,29 @@ def test_check_globally_unique_ids(modify, expected): assert error.message.count("`") == expected.count("`") +def test_check_globally_unique_ids_annotation_of_model_entity(): + """Annotation-only mapping rows are for entities not in any model.""" + problem = _unique_ids_problem() + problem.add_mapping("k", None, name="k") + assert (error := CheckGloballyUniqueIds().run(problem)) is not None + assert error.message.endswith( + "`k` (mapping table annotation, model entity)" + ) + + +def test_check_globally_unique_ids_annotated_parameter(): + """An annotation-only row for a parameter that is a model entity.""" + problem = _unique_ids_problem() + problem.add_parameter("k", estimate=True, lb=0, ub=10, nominal_value=1) + assert CheckGloballyUniqueIds().run(problem) is None + + problem.add_mapping("k", None, name="k") + assert (error := CheckGloballyUniqueIds().run(problem)) is not None + assert error.message.endswith( + "`k` (parameter ID, mapping table annotation, model entity)" + ) + + def test_check_globally_unique_ids_references(): """References to model entities or mapped IDs are not definitions.""" problem = _unique_ids_problem() @@ -408,43 +432,191 @@ def test_check_globally_unique_ids_references(): assert CheckGloballyUniqueIds().run(problem) is None -def test_check_globally_unique_ids_multi_model(): - """Model entities only clash with IDs used for the same model.""" +def _multi_model_problem(m2: str = "species y = 1; o1 = 1") -> Problem: problem = Problem() problem.models = [ SbmlModel.from_antimony("species x = 1", model_id="m1"), - SbmlModel.from_antimony("species y = 1; o1 = 1", model_id="m2"), + SbmlModel.from_antimony(m2, model_id="m2"), ] problem.add_observable("o1", "x", noise_formula="1") problem.add_observable( "o2", "a * y", noise_formula="1", observable_placeholders=["a"] ) - problem.add_measurement("o1", time=1, measurement=1, model_id="m1") + problem.add_experiment("e1", 0, "c1") + problem.add_condition("c1", x=2) + problem.add_measurement( + "o1", experiment_id="e1", time=1, measurement=1, model_id="m1" + ) problem.add_measurement( "o2", + experiment_id="e1", time=1, measurement=1, model_id="m2", observable_parameters=[2], ) + return problem + + +def _unique_id_issues(problem: Problem) -> list[ValidationIssue]: + return [ + issue + for issue in problem.validate() + if issue.task == "CheckGloballyUniqueIds" + ] - def unique_id_issues(): - return [ - issue - for issue in problem.validate() - if issue.task == "CheckGloballyUniqueIds" - ] - assert unique_id_issues() == [] +def test_check_globally_unique_ids_multi_model(): + """Model entities only clash with IDs used for the same model.""" + # `o1` is an entity of m2, but only measured with m1 + assert _unique_id_issues(_multi_model_problem()) == [] + + problem = _multi_model_problem("species y = 1; a = 1") + assert len(issues := _unique_id_issues(problem)) == 1 + assert issues[0].message == ( + "Identifiers must be unique within the PEtab problem: " + "`a` (observable placeholder of `o2`, entity of model `m2`)" + ) - problem.models[1] = SbmlModel.from_antimony( - "species y = 1; a = 1", model_id="m2" + # entity of both models, used by both: reported once + problem = _multi_model_problem("species y = 1; c1 = 1") + problem.models[0] = SbmlModel.from_antimony( + "species x = 1; c1 = 1", model_id="m1" + ) + assert len(issues := _unique_id_issues(problem)) == 1 + assert issues[0].message.endswith( + "`c1` (condition ID, entity of model `m1`, entity of model `m2`)" ) - assert len(issues := unique_id_issues()) == 1 - assert issues[0].message.startswith("Model `m2`:") - assert "`a` (observable placeholder of `o2`, model entity)" in ( - issues[0].message + + +@pytest.mark.parametrize( + "m2, modify, expected", + [ + # parameter table entries are shared by all models + ( + "species y = 1; k = 1", + lambda p: ( + p.add_parameter( + "k", estimate=True, lb=0, ub=10, nominal_value=1 + ), + p.add_observable( + "o3", + "k * x", + noise_formula="1", + observable_placeholders=["k"], + ), + p.add_measurement( + "o3", + experiment_id="e1", + time=1, + measurement=1, + model_id="m1", + observable_parameters=[2], + ), + ), + ( + "`k` (observable placeholder of `o3`, parameter ID, " + "entity of model `m2`)" + ), + ), + # model IDs are shared by all models + ( + "species y = 1; m1 = 1", + lambda p: None, + "`m1` (model ID, entity of model `m2`)", + ), + # unused entries are compared with all models + ( + "species y = 1; z = 1", + lambda p: p.add_condition("z", y=2), + "`z` (condition ID, entity of model `m2`)", + ), + ], +) +def test_check_globally_unique_ids_multi_model_shared(m2, modify, expected): + """IDs shared by all models are compared with all models.""" + assert _unique_id_issues(_multi_model_problem("species y = 1")) == [] + + problem = _multi_model_problem(m2) + modify(problem) + issues = CheckGloballyUniqueIds().run(problem) + assert issues is not None + assert issues.message.endswith(expected) + + +def test_check_globally_unique_ids_multi_model_tables(): + """Tables are compared across models.""" + problem = _multi_model_problem() + # experiment used with both models, observable only with m2 + problem.add_observable("e1", "y", noise_formula="1") + problem.add_measurement( + "e1", experiment_id="e1", time=1, measurement=1, model_id="m2" + ) + assert len(issues := _unique_id_issues(problem)) == 1 + assert issues[0].message.endswith("`e1` (experiment ID, observable ID)") + + +@pytest.mark.parametrize( + "modify, expected", + [ + ( + lambda p: p.add_observable("o2", "a + x", noise_formula="1"), + "`a` of observable `o1` (used in observable `o2`)", + ), + ( + lambda p: p.add_observable("o2", "x", noise_formula="a"), + "`a` of observable `o1` (used in observable `o2`)", + ), + ( + lambda p: p.add_condition("c1", a=2), + "`a` of observable `o1` (used in condition `c1`)", + ), + ( + lambda p: p.add_condition("c1", k="a + 1"), + "`a` of observable `o1` (used in condition `c1`)", + ), + ( + lambda p: p.add_measurement( + "o1", time=1, measurement=1, observable_parameters=["a"] + ), + "`a` of observable `o1` (used in measurement table)", + ), + ( + lambda p: p.add_observable( + "o2", + "b * x", + noise_formula="b", + noise_placeholders=["b"], + ), + "`b` of observable `o2` (used in observable formula of `o2`)", + ), + ], +) +def test_check_placeholders_only_used_in_own_observable(modify, expected): + """Test ``CheckPlaceholdersOnlyUsedInOwnObservable``.""" + problem = _unique_ids_problem() + # observable placeholders may be used in the noise formula + problem["o1"].noise_formula = sp.Symbol("a") + problem.add_measurement( + "o1", time=1, measurement=1, observable_parameters=[2] + ) + check = CheckPlaceholdersOnlyUsedInOwnObservable() + assert check.run(problem) is None + + modify(problem) + assert (error := check.run(problem)) is not None + assert error.message == ( + "Placeholders must only be used in their own observable: " + expected + ) + + +def test_check_placeholders_only_used_in_own_observable_duplicates(): + """Duplicate placeholders are left to ``CheckGloballyUniqueIds``.""" + problem = _unique_ids_problem() + problem.add_observable( + "o2", "a * x", noise_formula="1", observable_placeholders=["a"] ) + assert CheckPlaceholdersOnlyUsedInOwnObservable().run(problem) is None def test_check_observables_do_not_shadow_model_entities_deprecated():