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

Return interactive pareto plot via bokeh-json #50

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2021, BoostV
Copyright (c) 2022, BoostV
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
56 changes: 45 additions & 11 deletions optimizerapi/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from rq import Queue
from redis import Redis
from ProcessOptimizer import Optimizer, expected_minimum
from ProcessOptimizer.plots import plot_objective, plot_convergence, plot_Pareto
from ProcessOptimizer.plots import plot_objective, plot_convergence, plot_Pareto_bokeh
from ProcessOptimizer.space import Real
import matplotlib.pyplot as plt
import numpy
Expand Down Expand Up @@ -162,7 +162,7 @@ def process_result(result, optimizer, dimensions, cfg, extras, data, space):
a dictionary containing results and plots.
The dictionary has this structure:
{
plots: [{id: plotname, plot: BASE64 encoded png}],
plots: [{id: plotname, plot: BASE64 encoded png or JSON-object}],
result: { dict with relevant properties, e.g.,
suggestions for next experiment,
model representation etc.}
Expand Down Expand Up @@ -202,21 +202,24 @@ def process_result(result, optimizer, dimensions, cfg, extras, data, space):
if graph_format == "png":
for idx, model in enumerate(result):
plot_convergence(model)
add_plot(plots, f"convergence_{idx}")
add_plt_plot(plots, f"convergence_{idx}")

plot_objective(model, dimensions=dimensions,
usepartialdependence=False,
show_confidence=True)
add_plot(plots, f"objective_{idx}")
add_plt_plot(plots, f"objective_{idx}")

if optimizer.n_objectives == 1:
minimum = expected_minimum(result[0])

result_details["expected_minimum"] = [
round_to_length_scales(minimum[0], optimizer.space), round(minimum[1], 2)]
else:
plot_Pareto(optimizer)
add_plot(plots, "pareto")
json_bokeh = plot_Pareto_bokeh(optimizer,
dimensions=dimensions,
return_data=False,
return_type_bokeh='json')
add_JSON_item(plots, json_bokeh, id="pareto")

result_details["pickled"] = pickleToString(
result, get_crypto())
Expand Down Expand Up @@ -250,7 +253,7 @@ def process_model(model, optimizer):
return result_details


def add_plot(result, id="generic", close=True, debug=False):
def add_plt_plot(result, id="generic", close=True, debug=False):
"""Add the current figure to result as a base64 encoded string.

This function should be called after every plot that is generated.
Expand Down Expand Up @@ -281,14 +284,45 @@ def add_plot(result, id="generic", close=True, debug=False):
})

if debug:
with open('tmp/process_optimizer_' + id + '.png', 'wb') as imgfile:
plt.savefig(imgfile, bbox_inches='tight', pad_inches=0)

# print("IMAGE: " + str(pic_hash, "utf-8"))
# with open('tmp/process_optimizer_' + id + '.png', 'wb') as imgfile:
# plt.savefig(imgfile, bbox_inches='tight', pad_inches=0)
print("IMAGE: " + str(pic_hash, "utf-8"))
if close:
plt.clf()


def add_JSON_item(result, json_obj, id="generic", clean=True, debug=False):
"""Add the 'json_obj' to result as a base64 encoded string.

This function should be called after every plot that is generated.
It takes the current state of the figure canvas and writes it to
a base64 encoded string which is then appended to the list supplied.

Parameters
----------
result : list
The list of plots to which new plots should be addeed.
json_obj : json.dumps-object
A Bokeh json-export object that .
id : str
Identifier for the plot (default is "generic")
debug : bool
Indicate if plots should be written to local files.
If set to True plots are stored in tmp/process_optimizer_[id].png
relative to current working directory. (default is False)
"""
result.append({
"id": id,
"plot": json_obj
})
if debug:
print("JSON:\n")
print(json_obj)

if clean:
del json_obj


def round_to_length_scales(x, space):
""" Rounds a suggested experiment to to the length scales of each dimension

Expand Down
16 changes: 8 additions & 8 deletions requirements-freeze.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ clickclick==20.10.2
colorama==0.4.4
connexion==2.7.0
cryptography==3.4.7
cycler==0.10.0
cycler==0.11.0
deap==1.3.3
Deprecated==1.2.13
docopt==0.6.2
Expand All @@ -19,26 +19,26 @@ idna==2.10
inflection==0.5.1
iniconfig==1.1.1
itsdangerous==1.1.0
Jinja2==2.11.3
joblib==1.0.1
Jinja2==3.1.2
joblib==1.2.0
json-tricks==3.15.5
jsonschema==3.2.0
kiwisolver==1.3.1
MarkupSafe==1.1.1
matplotlib==3.5.3
numpy==1.23.3
openapi-spec-validator==0.2.9
packaging==20.9
Pillow==9.0.1
packaging==21.3
Pillow==9.3.0
pluggy==0.13.1
ProcessOptimizer[browniebee]==0.7.7
py==1.10.0
pycparser==2.20
pyparsing==2.4.7
pyparsing==3.0.9
pyrsistent==0.17.3
pytest==6.2.2
pytest-watch==4.2.0
python-dateutil==2.8.1
python-dateutil==2.8.2
PyYAML==6.0
redis==4.0.2
requests==2.25.1
Expand All @@ -47,7 +47,7 @@ scikit-learn==1.1.2
scipy==1.9.1
six==1.16.0
swagger-ui-bundle==0.0.8
threadpoolctl==2.1.0
threadpoolctl==3.1.0
toml==0.10.2
tornado==6.2
typing_extensions==4.4.0
Expand Down