Skip to content

Commit 0a39f79

Browse files
committed
--amend
1 parent 94a511a commit 0a39f79

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

petab/v1/distributions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ def pdf(self, x):
150150

151151
return self._pdf(x) * coeff
152152

153+
def neglogprior(self, x):
154+
"""Negative log-prior at x.
155+
156+
:param x: The value at which to evaluate the negative log-prior.
157+
``x`` is assumed to be on the parameter scale.
158+
:return: The negative log-prior at ``x``.
159+
"""
160+
return -np.log(self.pdf(x))
161+
153162
@staticmethod
154163
def from_par_dict(
155164
d, type_=Literal["initialization", "objective"]

tests/v1/test_priors.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import numpy as np
66
import pandas as pd
77
import pytest
8-
from scipy.stats import norm
98

109
import petab.v1
11-
from petab.v1 import get_simulation_conditions
10+
from petab.v1 import ESTIMATE, get_simulation_conditions
11+
from petab.v1.distributions import Distribution
1212
from petab.v1.priors import priors_to_measurements
1313

1414

@@ -102,33 +102,34 @@ def test_priors_to_measurements(problem_id):
102102

103103
# get prior objective function contribution
104104
parameter_ids = petab_problem_priors.parameter_df.index.values[
105-
(petab_problem_priors.parameter_df[petab.v1.ESTIMATE] == 1)
105+
(petab_problem_priors.parameter_df[ESTIMATE] == 1)
106106
& petab_problem_priors.parameter_df[
107107
petab.v1.OBJECTIVE_PRIOR_TYPE
108108
].notna()
109109
]
110-
priors = petab.v1.get_priors_from_df(
111-
petab_problem_priors.parameter_df,
112-
mode="objective",
113-
parameter_ids=parameter_ids,
114-
)
110+
priors = [
111+
Distribution.from_par_dict(
112+
petab_problem_priors.parameter_df.loc[par_id], type_="objective"
113+
)
114+
for par_id in parameter_ids
115+
]
115116
prior_contrib = 0
117+
x_scaled_dict = dict(
118+
zip(
119+
petab_problem_priors.x_free_ids,
120+
petab_problem_priors.x_nominal_free_scaled,
121+
strict=True,
122+
)
123+
)
116124
for parameter_id, prior in zip(parameter_ids, priors, strict=True):
117-
prior_type, prior_pars, par_scale, par_bounds = prior
118-
if prior_type == petab.v1.PARAMETER_SCALE_NORMAL:
119-
prior_contrib += norm.logpdf(
120-
petab_problem_priors.x_nominal_free_scaled[
121-
petab_problem_priors.x_free_ids.index(parameter_id)
122-
],
123-
loc=prior_pars[0],
124-
scale=prior_pars[1],
125-
)
125+
if prior.type == petab.v1.PARAMETER_SCALE_NORMAL:
126+
prior_contrib -= prior.neglogprior(x_scaled_dict[parameter_id])
126127
else:
127128
# enable other models, once libpetab has proper support for
128129
# evaluating the prior contribution. until then, two test
129130
# problems should suffice
130131
assert problem_id == "Raimundez_PCB2020"
131-
pytest.skip(f"Prior type {prior_type} not implemented")
132+
pytest.skip(f"Prior type {prior.type} not implemented")
132133

133134
assert np.isclose(
134135
llh_priors + prior_contrib, llh_measurements, rtol=1e-3, atol=1e-16

0 commit comments

Comments
 (0)