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

visualization of optimization result properties #486

Merged
merged 14 commits into from
Nov 10, 2020
4 changes: 2 additions & 2 deletions doc/example/amici_import.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"metadata": {},
"outputs": [],
"source": [
"constantParameters = {'ratio', 'specC17'}"
"constantParameters = ['ratio', 'specC17']"
]
},
{
Expand Down Expand Up @@ -530,7 +530,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.6.9"
}
},
"nbformat": 4,
Expand Down
3 changes: 3 additions & 0 deletions pypesto/visualize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
parameter_hist)
from .optimizer_history import (optimizer_history,
optimizer_history_lowlevel)
from .optimization_stats import (optimization_run_properties_per_multistart,
optimization_run_property_per_multistart,
optimization_run_properties_one_plot)
from .optimizer_convergence import optimizer_convergence
from .profiles import (profiles,
profiles_lowlevel,
Expand Down
26 changes: 16 additions & 10 deletions pypesto/visualize/clust_color.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from scipy import cluster
from typing import Tuple
from typing import List, Tuple, Optional, Union
import matplotlib.cm as cm
import numpy as np

# for typehints
RGBA = List[float]


def assign_clusters(vals):
"""
Expand Down Expand Up @@ -207,32 +210,35 @@ def assign_colors(vals, colors=None, balance_alpha=True,
'one single [r, g, b, alpha] color.')


def assign_colors_for_result_list(num_results, colors=None):
def assign_colors_for_list(
num_entries: int,
colors: Optional[Union[RGBA, List[RGBA], np.ndarray]] = None
) -> Union[List[List[float]], np.ndarray]:
"""
Creates a list of colors for a list of pypesto.Result objects or checks
Creates a list of colors for a list of items or checks
a user-provided list of colors and uses this if everything is ok

Parameters
----------

num_results: int
num_entries:
number of results in list

colors: list, or RGBA, optional
colors:
list of colors, or single color

Returns
-------

colors: list of RGBA
One for each element in 'vals'.
colors:
List of RGBA, one for each element in 'vals'.
"""

# if the user did not specify any colors:
if colors is None:
# default colors will be used, on for each entry in the result list.
# Colors are created from assign_colors, which needs a dummy list
dummy_clusters = np.array(list(range(num_results)) * 2)
dummy_clusters = np.array(list(range(num_entries)) * 2)

# we don't want alpha levels for all plotting routines in this case...
colors = assign_colors(dummy_clusters, balance_alpha=False,
Expand All @@ -243,10 +249,10 @@ def assign_colors_for_result_list(num_results, colors=None):
return colors[real_indices]

# if the user specified color lies does not match the number of results
if len(colors) != num_results:
if len(colors) != num_entries:
raise ('Incorrect color input. Colors must be specified either as '
'list of [r, g, b, alpha] with length equal to function '
'values Number of function (here: ' + str(num_results) + '), '
'values Number of function (here: ' + str(num_entries) + '), '
'or as one single [r, g, b, alpha] color.')

return colors
Expand Down
42 changes: 34 additions & 8 deletions pypesto/visualize/misc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import numpy as np
import warnings
from .clust_color import assign_colors
from .clust_color import assign_colors_for_result_list
from .clust_color import assign_colors_for_list

from typing import Optional
from numbers import Number
from typing import Iterable, List, Optional, Union


def process_result_list(results, colors=None, legends=None):
Expand Down Expand Up @@ -57,7 +58,7 @@ def process_result_list(results, colors=None, legends=None):
legends = [legends]
else:
# if more than one result is passed, we use one color per result
colors = assign_colors_for_result_list(len(results), colors)
colors = assign_colors_for_list(len(results), colors)

# check whether list of legends has the correct length
if legends is None:
Expand All @@ -75,8 +76,8 @@ def process_result_list(results, colors=None, legends=None):

# size of legend list and size of results does not match
if legend_error:
raise ('List of results passed and list of labels do not have the'
' same length but should. Stopping.')
raise ValueError('List of results passed and list of labels do '
'not have the same length but should. Stopping.')

return results, colors, legends

Expand Down Expand Up @@ -141,9 +142,6 @@ def process_y_limits(ax, y_limits):
y_limits: ndarray
y_limits, minimum and maximum, for current axes object

min_val: float
Smallest value to be plotted

Returns
-------

Expand Down Expand Up @@ -202,3 +200,31 @@ def process_y_limits(ax, y_limits):
ax.set_ylim(new_limits)

return ax


def process_start_indices(start_indices: Union[int, Iterable[int]],
max_length: int) -> List[int]:
"""
helper function that processes the start_indices and
creates an array of indices if a number was provided and checks that the
indices do not exceed the max_index

Parameters
----------
start_indices:
list of indices or int specifying an endpoint of the sequence of
indices
max_length:
maximum possible index for the start_indices
"""

if isinstance(start_indices, Number):
start_indices = range(int(start_indices))

start_indices = np.array(start_indices, dtype=int)

# check, whether index set is not too big
start_indices = [start_index for start_index in start_indices if
start_index < max_length]

return start_indices
Loading