Skip to content

Commit 65ef80f

Browse files
committed
optional
1 parent 2fd6da8 commit 65ef80f

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

petab/v1/priors.py

Lines changed: 19 additions & 6 deletions
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(
@@ -90,8 +97,9 @@ def __init__(
9097
self._parameters = parameters
9198
self._bounds = bounds
9299
self._transformation = transformation
100+
self._bounds_truncate = bounds_truncate
93101

94-
truncation = bounds if USE_PROPER_TRUNCATION else None
102+
truncation = bounds if bounds_truncate else None
95103
if truncation is not None:
96104
# for uniform, we don't want to implement truncation and just
97105
# adapt the distribution parameters
@@ -184,7 +192,7 @@ def _clip_to_bounds(self, x):
184192
185193
:param x: The values to clip. Assumed to be on the parameter scale.
186194
"""
187-
if self.bounds is None or USE_PROPER_TRUNCATION:
195+
if self.bounds is None or self._bounds_truncate:
188196
return x
189197

190198
return np.maximum(
@@ -235,12 +243,16 @@ def neglogprior(self, x):
235243

236244
@staticmethod
237245
def from_par_dict(
238-
d, type_=Literal["initialization", "objective"]
246+
d,
247+
type_=Literal["initialization", "objective"],
248+
bounds_truncate: bool = True,
239249
) -> Prior:
240250
"""Create a distribution from a row of the parameter table.
241251
242252
:param d: A dictionary representing a row of the parameter table.
243253
:param type_: The type of the distribution.
254+
:param bounds_truncate: Whether the generated prior will be truncated
255+
at the bounds.
244256
:return: A distribution object.
245257
"""
246258
dist_type = d.get(f"{type_}PriorType", C.PARAMETER_SCALE_UNIFORM)
@@ -268,6 +280,7 @@ def from_par_dict(
268280
parameters=params,
269281
bounds=(d[C.LOWER_BOUND], d[C.UPPER_BOUND]),
270282
transformation=pscale,
283+
bounds_truncate=bounds_truncate,
271284
)
272285

273286

petab/v1/sampling.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ def sample_from_prior(
2828
# unpack info
2929
p_type, p_params, scaling, bounds = prior
3030
prior = Prior(
31-
p_type, tuple(p_params), bounds=tuple(bounds), transformation=scaling
31+
p_type,
32+
tuple(p_params),
33+
bounds=tuple(bounds),
34+
transformation=scaling,
35+
bounds_truncate=True,
3236
)
3337
return prior.sample(shape=(n_starts,))
3438

@@ -74,7 +78,9 @@ def sample_parameter_startpoints(
7478
# get types and parameters of priors from dataframe
7579
return np.array(
7680
[
77-
Prior.from_par_dict(row, type_="initialization").sample(n_starts)
81+
Prior.from_par_dict(
82+
row, type_="initialization", bounds_truncate=True
83+
).sample(n_starts)
7884
for row in par_to_estimate.to_dict("records")
7985
]
8086
).T

tests/v1/test_priors.py

Lines changed: 3 additions & 1 deletion
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)