Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog.d/+globally-unique-ids.breaking.md
Original file line number Diff line number Diff line change
@@ -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))
5 changes: 5 additions & 0 deletions changelog.d/+placeholders-own-observable-v2.breaking.md
Original file line number Diff line number Diff line change
@@ -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))
3 changes: 3 additions & 0 deletions changelog.d/+shadow-model-entities.deprecation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`petab.v2.lint.CheckObservablesDoNotShadowModelEntities` is deprecated in
favor of `CheckGloballyUniqueIds`
([#606](https://github.com/PEtab-dev/libpetab-python/pull/606))
9 changes: 7 additions & 2 deletions petab/v2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
292 changes: 248 additions & 44 deletions petab/v2/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

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
Expand All @@ -24,6 +25,7 @@
"CheckArrayDataFiles",
"CheckExperimentConditionsExist",
"CheckExperimentTable",
"CheckGloballyUniqueIds",
"CheckHybridizationTable",
"CheckInitialChangeSymbols",
"CheckMappingTable",
Expand All @@ -34,6 +36,7 @@
"CheckNeuralNetworkModel",
"CheckObservablesDoNotShadowModelEntities",
"CheckOverridesMatchPlaceholders",
"CheckPlaceholdersOnlyUsedInOwnObservable",
"CheckPosLogMeasurements",
"CheckPriorDistribution",
"CheckProblemConfig",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -486,10 +486,250 @@ 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.
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:
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:
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",
)
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),
"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 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)


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(
"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."""
"""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
Expand Down Expand Up @@ -981,8 +1221,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 = []
Expand All @@ -1008,41 +1246,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


Expand Down Expand Up @@ -1255,7 +1458,8 @@ def get_placeholders(
CheckExperimentTable(),
CheckExperimentConditionsExist(),
CheckUndefinedExperiments(),
CheckObservablesDoNotShadowModelEntities(),
CheckGloballyUniqueIds(),
CheckPlaceholdersOnlyUsedInOwnObservable(),
CheckAllParametersPresentInParameterTable(),
CheckValidParameterInConditionOrParameterTable(),
CheckUnusedExperiments(),
Expand Down
Loading
Loading