From a94071679667789161ce304cab2e48c0051e6bff Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Tue, 17 Nov 2026 09:13:04 +0100 Subject: [PATCH] Treat `trunc=[-inf, inf]` as no truncation in distributions `Distribution.__init__` only recognized the tuple `(-inf, inf)` as "no truncation", but `petab.v2.Parameter.prior_dist` passes the bounds as a list. Such distributions were therefore handled as truncated, e.g., sampled via the inverse CDF instead of directly. Convert `trunc` to a tuple before the comparison, so any sequence is accepted and stored consistently. Co-Authored-By: Claude Opus 5.5 --- changelog.d/+distributions-trunc-sequence.fix.md | 4 ++++ petab/v1/distributions.py | 7 +++++-- tests/v1/test_distributions.py | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 changelog.d/+distributions-trunc-sequence.fix.md diff --git a/changelog.d/+distributions-trunc-sequence.fix.md b/changelog.d/+distributions-trunc-sequence.fix.md new file mode 100644 index 00000000..501a309c --- /dev/null +++ b/changelog.d/+distributions-trunc-sequence.fix.md @@ -0,0 +1,4 @@ +Distributions with truncation limits `(-inf, inf)` given as a list instead +of a tuple, as created by `petab.v2.Parameter.prior_dist` for unbounded +parameters, are no longer treated as truncated +([#603](https://github.com/PEtab-dev/libpetab-python/pull/603)) diff --git a/petab/v1/distributions.py b/petab/v1/distributions.py index ce6d4663..84475a33 100644 --- a/petab/v1/distributions.py +++ b/petab/v1/distributions.py @@ -51,6 +51,7 @@ class Distribution(abc.ABC): If ``False``, no transformation is applied. :param trunc: The truncation points (lower, upper) of the distribution or ``None`` if the distribution is not truncated. + ``(-inf, inf)`` is equivalent to ``None``. If the distribution is log-scaled, the truncation limits are expected to be on the same log scale. """ @@ -64,8 +65,10 @@ def __init__( if log is True: log = np.exp(1) - if trunc == (-np.inf, np.inf): - trunc = None + if trunc is not None: + trunc = tuple(trunc) + if trunc == (-np.inf, np.inf): + trunc = None if trunc is not None and trunc[0] >= trunc[1]: raise ValueError( diff --git a/tests/v1/test_distributions.py b/tests/v1/test_distributions.py index aace4487..2c78a6ae 100644 --- a/tests/v1/test_distributions.py +++ b/tests/v1/test_distributions.py @@ -132,3 +132,19 @@ def test_log_uniform(): assert dist.pdf(1) > 0 assert dist.pdf(2) > 0 assert dist.pdf(3) == 0 + + +@pytest.mark.parametrize("trunc", [(-np.inf, np.inf), [-np.inf, np.inf]]) +def test_infinite_truncation_is_no_truncation(trunc): + """Truncation at (-inf, inf) is equivalent to no truncation.""" + d = Normal(0, 1, trunc=trunc) + assert d._trunc is None + assert repr(d) == repr(Normal(0, 1)) + + +def test_trunc_sequence(): + """Truncation limits may be given as any sequence.""" + d = Normal(0, 1, trunc=[-1, 1]) + assert d._trunc == (-1, 1) + assert d.trunc_low == -1 + assert d.trunc_high == 1