Skip to content
Open
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
8 changes: 8 additions & 0 deletions changelog.d/+v2-infinite-bounds.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Validate infinite parameter bounds according to PEtab 2.0 erratum E1:
`lowerBound=inf` and `upperBound=-inf` are rejected; if any estimated
parameter has a prior, estimated parameters with infinite bounds
require an explicit prior, which is now checked by `petablint`.
`Problem.get_priors` and `Problem.get_startpoint_distributions` raise a
`ValueError` naming estimated parameters with infinite bounds and no
prior
([#601](https://github.com/PEtab-dev/libpetab-python/pull/601))
4 changes: 4 additions & 0 deletions changelog.d/+v2-sample-startpoints.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
`petab.v2.Problem.sample_parameter_startpoints` no longer fails for
maximum likelihood problems or problems without estimated parameters,
and no longer accepts and ignores arbitrary keyword arguments
([#601](https://github.com/PEtab-dev/libpetab-python/pull/601))
86 changes: 65 additions & 21 deletions petab/v2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,30 @@
logger = logging.getLogger(__name__)


def _is_finite_or_neg_inf(v: float, info: ValidationInfo) -> float:
if not np.isfinite(v) and v != -np.inf:
def _field_to_column(info: ValidationInfo) -> str:
"""The PEtab column name for the validated field."""
return {"lb": C.LOWER_BOUND, "ub": C.UPPER_BOUND}.get(
info.field_name, info.field_name
)


def _is_finite_or_neg_inf(
v: float | None, info: ValidationInfo
) -> float | None:
if v is not None and not np.isfinite(v) and v != -np.inf:
raise ValueError(
f"{info.field_name} value must be finite or -inf but got {v}"
f"{_field_to_column(info)} value must be finite or -inf "
f"but got {v}"
)
return v


def _is_finite_or_pos_inf(v: float, info: ValidationInfo) -> float:
if not np.isfinite(v) and v != np.inf:
def _is_finite_or_pos_inf(
v: float | None, info: ValidationInfo
) -> float | None:
if v is not None and not np.isfinite(v) and v != np.inf:
raise ValueError(
f"{info.field_name} value must be finite or inf but got {v}"
f"{_field_to_column(info)} value must be finite or inf but got {v}"
)
return v

Expand Down Expand Up @@ -1061,13 +1073,17 @@ class Parameter(BaseModel):
alias=C.PARAMETER_NAME, default=None
)
#: Lower bound.
lb: Annotated[float | None, BeforeValidator(_convert_nan_to_none)] = Field(
alias=C.LOWER_BOUND, default=None
)
lb: Annotated[
float | None,
BeforeValidator(_convert_nan_to_none),
AfterValidator(_is_finite_or_neg_inf),
] = Field(alias=C.LOWER_BOUND, default=None)
#: Upper bound.
ub: Annotated[float | None, BeforeValidator(_convert_nan_to_none)] = Field(
alias=C.UPPER_BOUND, default=None
)
ub: Annotated[
float | None,
BeforeValidator(_convert_nan_to_none),
AfterValidator(_is_finite_or_pos_inf),
] = Field(alias=C.UPPER_BOUND, default=None)
#: Nominal value.
nominal_value: Annotated[
# PEtab SciML supports arrays via "array" nominal values
Expand Down Expand Up @@ -2102,6 +2118,17 @@ def has_ml_objective(self) -> bool:
"""
return not self.has_map_objective

def _get_unbounded_ids_without_prior(self) -> list[str]:
"""IDs of estimated parameters with infinite or missing bounds and
no prior."""
return [
p.id
for p in self.parameters
if p.estimate
and p.prior_distribution is None
and any(b is None or np.isinf(b) for b in (p.lb, p.ub))
]

def get_priors(self) -> dict[str, Distribution]:
"""Get prior distributions.

Expand All @@ -2110,15 +2137,13 @@ def get_priors(self) -> dict[str, Distribution]:

:returns: The prior distributions for the estimated parameters in case
the problem has a MAP objective, an empty dictionary otherwise.
:raises ValueError: If an estimated parameter without prior has an
infinite or missing bound.
"""
if not self.has_map_objective:
return {}

return {
p.id: p.prior_dist if p.prior_distribution else Uniform(p.lb, p.ub)
for p in self.parameters
if p.estimate
}
return self.get_startpoint_distributions()

def get_startpoint_distributions(self) -> dict[str, Distribution]:
"""Get distributions for sampling startpoints.
Expand All @@ -2130,17 +2155,36 @@ def get_startpoint_distributions(self) -> dict[str, Distribution]:

:returns: Mapping of parameter IDs to distributions for sampling
startpoints.
:raises ValueError: If an estimated parameter without prior has an
infinite or missing bound.
"""
if unbounded := self._get_unbounded_ids_without_prior():
raise ValueError(
"The default uniform distribution is undefined for estimated "
"parameters with infinite or missing bounds and no prior: "
f"{unbounded}."
)

return {
p.id: p.prior_dist if p.prior_distribution else Uniform(p.lb, p.ub)
for p in self.parameters
if p.estimate
}

def sample_parameter_startpoints(self, n_starts: int = 100, **kwargs):
"""Create 2D array with starting points for optimization"""
priors = self.get_priors()
return np.vstack([p.sample(n_starts) for p in priors.values()]).T
def sample_parameter_startpoints(self, n_starts: int = 100) -> np.ndarray:
"""Create 2D array with starting points for optimization.

:param n_starts: Number of starting points.
:returns: Array of shape ``(n_starts, n_estimated)`` with columns
ordered as :attr:`x_free_ids`.
:raises ValueError: See :meth:`get_startpoint_distributions`.
"""
distributions = self.get_startpoint_distributions()
if not distributions:
return np.empty((n_starts, 0))
return np.vstack(
[d.sample(n_starts) for d in distributions.values()]
).T

def sample_parameter_startpoints_dict(
self, n_starts: int = 100
Expand Down
12 changes: 12 additions & 0 deletions petab/v2/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,18 @@ class CheckPriorDistribution(ValidationTask):

def run(self, problem: Problem) -> ValidationIssue | None:
messages = []

# The default uniform(lowerBound, upperBound) prior is improper for
# infinite bounds
if problem.has_map_objective and (
unbounded := problem._get_unbounded_ids_without_prior()
):
messages.append(
"If any estimated parameter has a prior, all estimated "
"parameters with infinite bounds require an explicit proper "
f"prior, but the following parameters have none: {unbounded}."
)

for parameter in problem.parameters:
if parameter.prior_distribution is None:
continue
Expand Down
148 changes: 148 additions & 0 deletions tests/v2/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sympy.abc import x, y

import petab.v2 as petab
from petab.v1.distributions import Normal
from petab.v2 import C
from petab.v2.C import (
CONDITION_ID,
Expand Down Expand Up @@ -379,6 +380,17 @@ def test_parameter():
with pytest.raises(ValidationError, match="less than"):
Parameter(id="k1", lb=2, ub=1)

# infinite bounds
Parameter(id="k1", lb=-np.inf, ub=np.inf)
Parameter(id="k1", lb="-inf", ub="inf")
with pytest.raises(ValidationError, match="lowerBound .* finite or -inf"):
Parameter(id="k1", lb=np.inf, ub=np.inf)
with pytest.raises(ValidationError, match="upperBound .* finite or inf"):
Parameter(id="k1", lb=-np.inf, ub=-np.inf)
p = Parameter(id="k1", lb=0, ub=1)
with pytest.raises(ValidationError, match="finite or inf"):
p.ub = -np.inf

assert Parameter(
id="k1", estimate=True, lb=1, ub=2, prior_parameters=[1, 2]
).model_dump() == {
Expand Down Expand Up @@ -741,6 +753,142 @@ def test_sample_startpoint_shape():
assert sp.shape == (n_starts, 2)


def test_sample_startpoints_ml():
"""Test startpoint sampling for maximum likelihood problems."""
problem = Problem()
problem += Parameter(id="p1", estimate=True, lb=1, ub=2)
problem += Parameter(id="p2", estimate=True, lb=3, ub=4)
assert problem.has_ml_objective

sp = problem.sample_parameter_startpoints(n_starts=10)
assert sp.shape == (10, 2)
assert np.all((sp[:, 0] >= 1) & (sp[:, 0] <= 2))
assert np.all((sp[:, 1] >= 3) & (sp[:, 1] <= 4))

# infinite bounds require a prior for sampling startpoints
problem += Parameter(id="p3", estimate=True, lb=0, ub=np.inf)
with pytest.raises(ValueError, match=r"missing bounds.*\['p3'\]"):
problem.get_startpoint_distributions()
with pytest.raises(ValueError, match=r"missing bounds.*\['p3'\]"):
problem.sample_parameter_startpoints()
# ML problem: no priors
assert problem.get_priors() == {}

problem["p3"].prior_distribution = C.LOG_NORMAL
problem["p3"].prior_parameters = [0, 1]
sp = problem.sample_parameter_startpoints(n_starts=10)
assert sp.shape == (10, 3)
assert np.all(sp[:, 2] > 0)

# missing bounds (bypassing validation) are reported like infinite ones
problem += Parameter.model_construct(id="p4", estimate=True)
with pytest.raises(ValueError, match=r"missing bounds.*\['p4'\]"):
problem.get_startpoint_distributions()


def test_sample_startpoints_no_estimated_parameters():
"""Test startpoint sampling without estimated parameters."""
problem = Problem()
problem += Parameter(id="p1", estimate=False, nominal_value=1)
assert problem.sample_parameter_startpoints(n_starts=5).shape == (5, 0)


def test_get_priors_infinite_bounds():
"""Test that infinite bounds without prior are rejected for MAP
problems."""
problem = Problem()
problem += Parameter(id="p1", estimate=True, lb=-np.inf, ub=np.inf)
problem += Parameter(
id="p2",
estimate=True,
lb=1,
ub=2,
prior_distribution=C.NORMAL,
prior_parameters=[1.5, 1],
)
assert problem.has_map_objective
with pytest.raises(ValueError, match=r"missing bounds.*\['p1'\]"):
problem.get_priors()

problem["p1"].prior_distribution = C.NORMAL
problem["p1"].prior_parameters = [0, 1]
assert set(problem.get_priors()) == {"p1", "p2"}


@pytest.mark.parametrize(
"prior_distribution, prior_parameters, lb, ub",
[
(C.NORMAL, [0, 1], -np.inf, np.inf),
(C.LOG_NORMAL, [0, 1], 0, np.inf),
(C.EXPONENTIAL, [2], 0, np.inf),
(C.GAMMA, [2, 1], 0, np.inf),
(C.CHI_SQUARED, [2], 0, np.inf),
(C.RAYLEIGH, [1], 0, np.inf),
(C.LOG_LAPLACE, [0, 1], 0, np.inf),
(C.CAUCHY, [0, 1], -np.inf, np.inf),
(C.LAPLACE, [0, 1], -np.inf, np.inf),
],
)
def test_prior_truncation_noop_at_domain(
prior_distribution, prior_parameters, lb, ub
):
"""Bounds matching the prior's domain do not truncate the prior."""
truncated = Parameter(
id="p",
lb=lb,
ub=ub,
prior_distribution=prior_distribution,
prior_parameters=prior_parameters,
).prior_dist
# reference: the same distribution without truncation
untruncated = type(truncated)(
*prior_parameters,
**({"log": True} if prior_distribution.startswith("log-") else {}),
)

x = np.array([0.1, 0.5, 1.0, 2.0, 5.0])
np.testing.assert_allclose(truncated.pdf(x), untruncated.pdf(x))
np.testing.assert_allclose(truncated.cdf(x), untruncated.cdf(x))
assert np.all(truncated.pdf(x) > 0)


def test_prior_truncation_one_sided_infinite():
"""A finite bound truncates the prior also if the other is infinite."""
truncated = Parameter(
id="p",
lb=1,
ub=np.inf,
prior_distribution=C.LOG_NORMAL,
prior_parameters=[0, 1],
).prior_dist
untruncated = Parameter(
id="p",
lb=0,
ub=np.inf,
prior_distribution=C.LOG_NORMAL,
prior_parameters=[0, 1],
).prior_dist
# median of LogNormal(0, 1) is 1, so truncation at 1 doubles the density
x = np.array([1.5, 2.0, 5.0])
np.testing.assert_allclose(truncated.pdf(x), 2 * untruncated.pdf(x))
assert truncated.pdf(0.5) == 0
assert np.all(truncated.sample(100) >= 1)

# mirrored: finite upper bound
truncated = Parameter(
id="p",
lb=-np.inf,
ub=0,
prior_distribution=C.NORMAL,
prior_parameters=[0, 1],
).prior_dist
untruncated = Normal(0, 1)
x = np.array([-2.0, -1.0, -0.5])
np.testing.assert_allclose(truncated.pdf(x), 2 * untruncated.pdf(x))
assert truncated.pdf(0.5) == 0
assert np.all(truncated.sample(100) <= 0)


def test_problem_config_paths():
"""Test handling of URLS and local paths in ProblemConfig."""

Expand Down
32 changes: 31 additions & 1 deletion tests/v2/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from petab.v2 import Problem
from petab.v2.C import TIME_SYMBOL
from petab.v2.C import LOG_NORMAL, NORMAL, TIME_SYMBOL, UNIFORM
from petab.v2.lint import *
from petab.v2.lint import (
get_required_parameters_for_parameter_table,
Expand Down Expand Up @@ -290,3 +290,33 @@ 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 test_check_prior_distribution_infinite_bounds():
"""Estimated parameters with infinite bounds need a prior if the problem
has a MAP objective."""
problem = Problem()
problem.add_parameter("p1", estimate=True, lb=0, ub=float("inf"))
problem.add_parameter("p2", estimate=True, lb=0, ub=1)
# non-estimated parameters are irrelevant
problem.add_parameter("p3", estimate=False, nominal_value=1)

check = CheckPriorDistribution()
# ML problem: infinite bounds are fine
assert check.run(problem) is None

problem["p2"].prior_distribution = NORMAL
problem["p2"].prior_parameters = [0.5, 1]
assert (error := check.run(problem)) is not None
assert error.level == ValidationIssueSeverity.ERROR
assert "['p1']" in error.message

problem["p1"].prior_distribution = LOG_NORMAL
problem["p1"].prior_parameters = [0, 1]
assert check.run(problem) is None

# improper explicit prior
problem["p1"].prior_distribution = UNIFORM
problem["p1"].prior_parameters = [0, float("inf")]
assert (error := check.run(problem)) is not None
assert "`p1` are invalid" in error.message
Loading