Skip to content

Commit 73b2e2d

Browse files
committed
Add v2.Problem.has_{map,ml}_objective
To check for the type of objective function encoded in the PEtab problem.
1 parent 5429412 commit 73b2e2d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

petab/v2/core.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,9 +1808,44 @@ def x_fixed_indices(self) -> list[int]:
18081808
"""Parameter table non-estimated parameter indices."""
18091809
return [i for i, p in enumerate(self.parameters) if not p.estimate]
18101810

1811+
@property
1812+
def has_map_objective(self) -> bool:
1813+
"""Whether this problem encodes a maximum a posteriori (MAP) objective.
1814+
1815+
A PEtab problem is considered to have a MAP objective if there is a
1816+
prior distribution specified for at least one estimated parameter.
1817+
1818+
:returns: ``True`` if MAP objective, ``False`` otherwise.
1819+
"""
1820+
return any(
1821+
p.prior_distribution is not None
1822+
for p in self.parameters
1823+
if p.estimate
1824+
)
1825+
1826+
@property
1827+
def has_ml_objective(self) -> bool:
1828+
"""Whether this problem encodes a maximum likelihood (ML) objective.
1829+
1830+
A PEtab problem is considered to have an ML objective if there are no
1831+
prior distributions specified for any estimated parameters.
1832+
1833+
:returns: ``True`` if ML objective, ``False`` otherwise.
1834+
"""
1835+
return all(
1836+
p.prior_distribution is None for p in self.parameters if p.estimate
1837+
)
1838+
18111839
def get_priors(self) -> dict[str, Distribution]:
18121840
"""Get prior distributions.
18131841
1842+
Note that this will default to uniform distributions over the
1843+
parameter bounds for parameters without an explicit prior.
1844+
1845+
For checking whether this :class:`Problem` encodes a MAP or ML
1846+
objective, use :attr:`Problem.has_map_objective` or
1847+
:attr:`Problem.has_ml_objective`.
1848+
18141849
:returns: The prior distributions for the estimated parameters.
18151850
"""
18161851
return {p.id: p.prior_dist for p in self.parameters if p.estimate}

tests/v2/test_core.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,22 @@ def test_mapping_validation():
866866

867867
# identity mapping is valid
868868
Mapping(petab_id="valid_id", model_id="valid_id", name="some name")
869+
870+
871+
def test_objective_type():
872+
"""Test that MAP and ML problems are recognized correctly."""
873+
problem = Problem()
874+
problem += Parameter(id="par1", lb=0, ub=100, estimate=True)
875+
assert problem.has_ml_objective is True
876+
assert problem.has_map_objective is False
877+
878+
problem += Parameter(
879+
id="par2",
880+
lb=0,
881+
ub=100,
882+
estimate=True,
883+
prior_distribution="normal",
884+
prior_parameters=[50, 10],
885+
)
886+
assert problem.has_map_objective is True
887+
assert problem.has_ml_objective is False

0 commit comments

Comments
 (0)