Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: add inner problem names, bounds and hierarchical flag #1282

Merged
merged 8 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pypesto/hierarchical/base_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def get_interpretable_x_ids(self) -> list[str]:

Interpretable parameters need to be easily interpretable by the user.
Examples are scaling factors, offsets, or noise parameters. An example
for a non-interpretable inner parameters are spline heights of spline
approximation for semiquantitative data: it is hard to interpret what
the spline heights are just by looking at the parameter value.
of non-interpretable inner parameters is the spline heights of spline
approximation for semiquantitative data. It is challenging to interpret
the meaning of these parameters based solely on their value.
"""
return list(self.xs.keys())

Expand Down
9 changes: 8 additions & 1 deletion pypesto/hierarchical/inner_calculator_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,14 @@ def get_inner_par_ids(self) -> list[str]:
]

def get_interpretable_inner_par_ids(self) -> list[str]:
"""Return the ids of interpretable inner parameters of all inner problems."""
"""Return the ids of interpretable inner parameters of all inner problems.

Interpretable parameters need to be easily interpretable by the user.
Examples are scaling factors, offsets, or noise parameters. An example
of non-interpretable inner parameters is the spline heights of spline
approximation for semiquantitative data. It is challenging to interpret
the meaning of these parameters based solely on their value.
"""
Doresic marked this conversation as resolved.
Show resolved Hide resolved
return [
parameter_id
for inner_calculator in self.inner_calculators
Expand Down
9 changes: 7 additions & 2 deletions pypesto/petab/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from ..objective.amici import AmiciObjectBuilder
from ..objective.priors import NegLogParameterPriors, get_parameter_prior_dict
from ..predict import AmiciPredictor
from ..problem import Problem
from ..problem import HierarchicalProblem, Problem
from ..result import PredictionResult
from ..startpoint import CheckedStartpoints, StartpointMethod

Expand Down Expand Up @@ -780,7 +780,12 @@ def create_problem(
)
objective = AggregatedObjective([objective, prior])

problem = Problem(
if self._hierarchical:
problem_class = HierarchicalProblem
else:
problem_class = Problem

problem = problem_class(
objective=objective,
lb=lb,
ub=ub,
Expand Down
1 change: 1 addition & 0 deletions pypesto/problem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
"""

from .base import Problem
from .hierarchical import HierarchicalProblem
1 change: 1 addition & 0 deletions pypesto/problem/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def __init__(
ub_init: Union[np.ndarray, List[float], None] = None,
copy_objective: bool = True,
startpoint_method: Union[StartpointMethod, Callable, bool] = None,
hierarchical: bool = False,
Doresic marked this conversation as resolved.
Show resolved Hide resolved
):
if copy_objective:
objective = copy.deepcopy(objective)
Expand Down
72 changes: 72 additions & 0 deletions pypesto/problem/hierarchical.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import logging
from typing import Iterable, List, Optional, SupportsFloat, SupportsInt, Union

import numpy as np

from .base import Problem

SupportsFloatIterableOrValue = Union[Iterable[SupportsFloat], SupportsFloat]
SupportsIntIterableOrValue = Union[Iterable[SupportsInt], SupportsInt]

logger = logging.getLogger(__name__)


class HierarchicalProblem(Problem):
"""
The Hierarchical Problem.

A hierarchical problem is a problem with a nested structure: One or
multiple inner problems are nested inside the outer problem. The inner
problems are optimized for each evaluation of the outer problem. The
objective's calculator is used to collect the inner problems' objective
values.

Parameters
----------
hierarchical:
A flag indicating the problem is hierarchical.
inner_x_names:
Names of the inner optimization parameters. Only relevant if
hierarchical is True. Contains the names of easily interpretable
inner parameters only, e.g. noise parameters, scaling factors, offsets.
inner_lb, inner_ub:
The lower and upper bounds for the inner optimization parameters.
Only relevant if hierarchical is True. Contains the bounds of easily
interpretable inner parameters only, e.g. noise parameters, scaling
factors, offsets.
"""

def __init__(
self,
inner_x_names: Optional[Iterable[str]] = None,
inner_lb: Optional[Union[np.ndarray, List[float]]] = None,
inner_ub: Optional[Union[np.ndarray, List[float]]] = None,
**problem_kwargs: dict,
):
super().__init__(**problem_kwargs)

if inner_x_names is None:
inner_x_names = (
self.objective.calculator.get_interpretable_inner_par_ids()
)
if len(set(inner_x_names)) != len(inner_x_names):
raise ValueError("Parameter names inner_x_names must be unique")
self.inner_x_names = inner_x_names

if inner_lb is None or inner_ub is None:
dweindl marked this conversation as resolved.
Show resolved Hide resolved
(
default_inner_lb,
default_inner_ub,
) = self.objective.calculator.get_interpretable_inner_par_bounds()
inner_lb = default_inner_lb if inner_lb is None else inner_lb
inner_ub = default_inner_ub if inner_ub is None else inner_ub

if len(inner_lb) != len(inner_ub):
raise ValueError("Parameter bounds must have same length")
if len(inner_lb) != len(inner_x_names):
raise ValueError(
"Parameter bounds must have same length as parameter names"
)

self.inner_lb = np.array(inner_lb)
self.inner_ub = np.array(inner_ub)
36 changes: 16 additions & 20 deletions pypesto/visualize/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,26 +462,22 @@ def _handle_inner_inputs(
inner_lb = None
inner_ub = None

if any(inner_x is not None for inner_x in inner_xs):
from ..hierarchical import InnerCalculatorCollector

if hasattr(result.problem.objective, 'calculator') and isinstance(
inner_calculator := result.problem.objective.calculator,
InnerCalculatorCollector,
):
inner_xs_names = inner_calculator.get_interpretable_inner_par_ids()
# replace None with a list of nans
inner_xs = [
np.full(len(inner_xs_names), np.nan)
if inner_xs_idx is None
else np.asarray(inner_xs_idx)
for inner_xs_idx in inner_xs
]
# set bounds for inner parameters
(
inner_lb,
inner_ub,
) = inner_calculator.get_interpretable_inner_par_bounds()
from ..problem import HierarchicalProblem

if any(inner_x is not None for inner_x in inner_xs) and isinstance(
result.problem, HierarchicalProblem
):
inner_xs_names = result.problem.inner_x_names
# replace None with a list of nans
inner_xs = [
np.full(len(inner_xs_names), np.nan)
if inner_xs_idx is None
else np.asarray(inner_xs_idx)
for inner_xs_idx in inner_xs
]
# set bounds for inner parameters
inner_lb = result.problem.inner_lb
inner_ub = result.problem.inner_ub

if inner_xs_names is None:
inner_xs = None
Expand Down