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 1 commit
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
2 changes: 1 addition & 1 deletion pypesto/hierarchical/base_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ 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
of a non-interpretable inner parameters are spline heights of spline
dweindl marked this conversation as resolved.
Show resolved Hide resolved
approximation for semiquantitative data: it is hard to interpret what
the spline heights are just by looking at the parameter value.
"""
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 a non-interpretable inner parameters are spline heights of spline
dweindl marked this conversation as resolved.
Show resolved Hide resolved
approximation for semiquantitative data: it is hard to interpret what
the spline heights are just by looking at the parameter value.
"""
return [
parameter_id
for inner_calculator in self.inner_calculators
Expand Down
1 change: 1 addition & 0 deletions pypesto/petab/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ def create_problem(
startpoint_method=self.create_startpoint_method(
**startpoint_kwargs
),
hierarchical=self._hierarchical,
**problem_kwargs,
)

Expand Down
41 changes: 41 additions & 0 deletions pypesto/problem/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ class Problem:
startpoint_method:
Method for how to choose start points. ``False`` means the optimizer
does not require start points, e.g. for the ``PyswarmOptimizer``.
hierarchical:
A flag indicating whether the problem is hierarchical. If True,
the objective function's calculator will collect the objective
values of all inner optimization problems.
dweindl marked this conversation as resolved.
Show resolved Hide resolved
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.


Notes
-----
Expand Down Expand Up @@ -103,6 +117,10 @@ 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
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,
):
if copy_objective:
objective = copy.deepcopy(objective)
Expand Down Expand Up @@ -166,6 +184,29 @@ def __init__(
# convert startpoint method to class instance
self.startpoint_method = to_startpoint_method(startpoint_method)

self.hierarchical = hierarchical

if inner_x_names is None and hierarchical:
inner_x_names = (
self.objective.calculator.get_interpretable_inner_par_ids()
)
if inner_x_names is not None and 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) and hierarchical:
(
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

self.inner_lb = np.array(inner_lb)
self.inner_ub = np.array(inner_ub)

@property
def lb(self) -> np.ndarray:
"""Return lower bounds of free parameters."""
Expand Down
35 changes: 15 additions & 20 deletions pypesto/visualize/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,26 +462,21 @@ 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()
if (
any(inner_x is not None for inner_x in inner_xs)
and result.problem.hierarchical
):
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
Loading