Skip to content

Commit e716487

Browse files
committed
optional
1 parent 2fd6da8 commit e716487

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

petab/v1/priors.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040

4141
__all__ = ["priors_to_measurements"]
4242

43-
# TODO: does anybody really rely on the old behavior?
44-
USE_PROPER_TRUNCATION = True
45-
4643

4744
class Prior:
4845
"""A PEtab parameter prior.
@@ -61,6 +58,15 @@ class Prior:
6158
on the `parameter_scale` scale).
6259
:param bounds: The untransformed bounds of the sample (lower, upper).
6360
:param transformation: The transformation of the distribution.
61+
:param bounds_truncate: Whether the generated prior will be truncated
62+
at the bounds.
63+
If ``True``, the probability density will be rescaled
64+
accordingly and the sample is generated from the truncated
65+
distribution.
66+
If ``False``, the probability density will not account for the
67+
bounds, but any parameter samples outside the bounds will be set to
68+
the value of the closest bound. In this case, the PDF might not match
69+
the sample.
6470
"""
6571

6672
def __init__(
@@ -69,6 +75,7 @@ def __init__(
6975
parameters: tuple,
7076
bounds: tuple = None,
7177
transformation: str = C.LIN,
78+
bounds_truncate: bool = True,
7279
):
7380
if transformation not in C.PARAMETER_SCALES:
7481
raise ValueError(
@@ -91,7 +98,7 @@ def __init__(
9198
self._bounds = bounds
9299
self._transformation = transformation
93100

94-
truncation = bounds if USE_PROPER_TRUNCATION else None
101+
truncation = bounds if bounds_truncate else None
95102
if truncation is not None:
96103
# for uniform, we don't want to implement truncation and just
97104
# adapt the distribution parameters
@@ -235,12 +242,16 @@ def neglogprior(self, x):
235242

236243
@staticmethod
237244
def from_par_dict(
238-
d, type_=Literal["initialization", "objective"]
245+
d,
246+
type_=Literal["initialization", "objective"],
247+
bounds_truncate: bool = True,
239248
) -> Prior:
240249
"""Create a distribution from a row of the parameter table.
241250
242251
:param d: A dictionary representing a row of the parameter table.
243252
:param type_: The type of the distribution.
253+
:param bounds_truncate: Whether the generated prior will be truncated
254+
at the bounds.
244255
:return: A distribution object.
245256
"""
246257
dist_type = d.get(f"{type_}PriorType", C.PARAMETER_SCALE_UNIFORM)
@@ -268,6 +279,7 @@ def from_par_dict(
268279
parameters=params,
269280
bounds=(d[C.LOWER_BOUND], d[C.UPPER_BOUND]),
270281
transformation=pscale,
282+
bounds_truncate=bounds_truncate,
271283
)
272284

273285

tests/v1/test_priors.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def apply_parameter_values(row):
156156
]
157157
priors = [
158158
Prior.from_par_dict(
159-
petab_problem_priors.parameter_df.loc[par_id], type_="objective"
159+
petab_problem_priors.parameter_df.loc[par_id],
160+
type_="objective",
161+
bounds_truncate=False,
160162
)
161163
for par_id in parameter_ids
162164
]

0 commit comments

Comments
 (0)