Skip to content

Commit

Permalink
Merge branch 'develop' into fix_install
Browse files Browse the repository at this point in the history
  • Loading branch information
m-philipps committed Dec 20, 2023
2 parents d193073 + b4b3ac4 commit 82f6ad7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
22 changes: 12 additions & 10 deletions pypesto/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"""
from numbers import Number
from typing import Any, Callable, Optional, Tuple, Union
from operator import itemgetter
from typing import Any, Callable, Optional, Sequence, Tuple, Union

import numpy as np
from scipy import cluster
Expand Down Expand Up @@ -241,7 +242,7 @@ def assign_clusters(vals):

def delete_nan_inf(
fvals: np.ndarray,
x: Optional[np.ndarray] = None,
x: Optional[Sequence[Union[np.ndarray, list[float]]]] = None,
xdim: Optional[int] = 1,
magnitude_bound: Optional[float] = np.inf,
) -> Tuple[np.ndarray, np.ndarray]:
Expand Down Expand Up @@ -272,16 +273,17 @@ def delete_nan_inf(
fvals = np.asarray(fvals)
finite_fvals = np.isfinite(fvals) & (np.absolute(fvals) < magnitude_bound)
if x is not None:
# if we start out with a list of x, the x corresponding
# to finite fvals may be None, so we cannot stack the x before taking
# subindexing
# If none of the fvals are finite, np.vstack will fail and np.take
# will not yield the correct dimension, so we try to construct an
# empty np.ndarray with the correct dimension (other functions rely
# on x.shape[1] to be of correct dimension)
if np.isfinite(fvals).any():
x = np.vstack(np.take(x, np.where(finite_fvals)[0], axis=0))
finite_indices = np.where(finite_fvals)[0]
x = np.array(itemgetter(*finite_indices)(x))
# itemgetter does not return list for single element
if len(x.shape) == 1:
x = x.reshape((1, x.shape[0]))
else:
# If none of the fvals are finite we try to construct an
# empty np.ndarray with the correct dimension (other functions rely
# on x.shape[1] to be of correct dimension)
x = np.array(x)
x = np.empty(
(
0,
Expand Down
2 changes: 1 addition & 1 deletion pypesto/visualize/optimization_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def stats_lowlevel(
The plot axes.
"""
fvals = result.optimize_result.fval
values = [res[property_name] for res in result.optimize_result.list]
values = [[res[property_name]] for res in result.optimize_result.list]
values, fvals = delete_nan_inf(fvals, values)

if start_indices is not None:
Expand Down
29 changes: 15 additions & 14 deletions pypesto/visualize/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ def scale_parameters(x):
start_indices=start_indices,
plot_inner_parameters=plot_inner_parameters,
)

# parse fvals and parameters
fvals = np.array(fvals)
# remove nan or inf values
xs, fvals = delete_nan_inf(
fvals=fvals,
x=xs,
xdim=len(ub) if ub is not None else 1,
magnitude_bound=WATERFALL_MAX_VALUE,
)

lb, ub, xs = map(scale_parameters, (lb, ub, xs))

# call lowlevel routine
Expand Down Expand Up @@ -223,8 +234,8 @@ def parameter_hist(


def parameters_lowlevel(
xs: Sequence[Union[np.ndarray, List[float]]],
fvals: Union[np.ndarray, List[float]],
xs: np.ndarray,
fvals: np.ndarray,
lb: Optional[Union[np.ndarray, List[float]]] = None,
ub: Optional[Union[np.ndarray, List[float]]] = None,
x_labels: Optional[Iterable[str]] = None,
Expand All @@ -241,8 +252,8 @@ def parameters_lowlevel(
Parameters
----------
xs:
Including optimized parameters for each startpoint.
Shape: (n_starts, dim).
Including optimized parameters for each start that did not result in an infinite fval.
Shape: (n_starts_successful, dim).
fvals:
Function values. Needed to assign cluster colors.
lb, ub:
Expand All @@ -268,16 +279,6 @@ def parameters_lowlevel(
ax:
The plot axes.
"""
# parse input
xs = np.array(xs)
fvals = np.array(fvals)
# remove nan or inf values in fvals and xs
xs, fvals = delete_nan_inf(
fvals=fvals,
x=xs,
xdim=len(ub) if ub is not None else 1,
magnitude_bound=WATERFALL_MAX_VALUE,
)

if size is None:
# 0.5 inch height per parameter
Expand Down
33 changes: 20 additions & 13 deletions test/visualize/test_visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,15 @@ def create_optimization_result_nan_inf():
result = create_optimization_result()

# append nan and inf
# depending on optimizer failed starts's x can be None or vector of np.nan
optimizer_result = pypesto.OptimizerResult(
fval=float('nan'), x=np.array([float('nan'), float('nan')]), id='nan'
)
result.optimize_result.append(optimize_result=optimizer_result)
optimizer_result = pypesto.OptimizerResult(
fval=float('nan'), x=None, id='nan_none'
)
result.optimize_result.append(optimize_result=optimizer_result)
optimizer_result = pypesto.OptimizerResult(
fval=-float('inf'),
x=np.array([-float('inf'), -float('inf')]),
Expand Down Expand Up @@ -406,19 +411,21 @@ def test_parameters_with_options(scale_to_interval):
def test_parameters_lowlevel():
# create some dummy results
(lb, ub) = create_bounds()
fvals = [0.01, 0.02, 1.01, 2.02, 2.03, 2.04, 3, 4, 4.1, 4.11]
xs = [
[0.1, 1],
[1.2, 3],
[2, 4],
[1.2, 4.1],
[1.1, 3.5],
[4.2, 3.5],
[1, 4],
[6.2, 5],
[4.3, 3],
[3, 2],
]
fvals = np.array([0.01, 0.02, 1.01, 2.02, 2.03, 2.04, 3, 4, 4.1, 4.11])
xs = np.array(
[
[0.1, 1],
[1.2, 3],
[2, 4],
[1.2, 4.1],
[1.1, 3.5],
[4.2, 3.5],
[1, 4],
[6.2, 5],
[4.3, 3],
[3, 2],
]
)

# pass lists
visualize.parameters_lowlevel(xs, fvals, lb=lb, ub=ub)
Expand Down

0 comments on commit 82f6ad7

Please sign in to comment.