Skip to content

Commit

Permalink
Return simple result json
Browse files Browse the repository at this point in the history
  • Loading branch information
langdal committed Apr 19, 2021
1 parent 5811799 commit 2b7e616
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
64 changes: 59 additions & 5 deletions optimizerapi/optimizer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from json_tricks import dumps
from ProcessOptimizer import Optimizer, expected_minimum
from ProcessOptimizer.plots import plot_objective, plot_convergence
from ProcessOptimizer.utils import cook_estimator
from ProcessOptimizer.learning.gaussian_process.kernels import Matern
import matplotlib.pyplot as plt
import base64
import io
from numbers import Number
import numpy
numpy.random.seed(42)
Expand All @@ -20,9 +24,59 @@ def run(params: str = None, Xi: [float] = [0.01], yi: [Number] = [1], kappa: flo
}
optimizer = Optimizer(space, **hyperparams)
# TODO call optimizer with proper Xi and Yi values
response = {
"plots": []
}
prettyResult = {}
response["result"] = prettyResult

result = optimizer.tell([Xi], yi)
if params:
return 'Run with {params} = {result}'.format(params=params, result=result)
else:
return 'Run with no parameters = {result}'.format(result=result)


##################### Copied and modified from views.py::view_report #####################

next_exps = optimizer.ask(n_points=1)
prettyResult["next"] = next_exps

header_list = []
result_list = []
if "expected_minimum" in result:
temp_exp_min =[]
for entry,value in zip(header_list[:-1], result.expected_minimum[0]):
temp_exp_min.append([entry, value])
exp_min_out = {'value':temp_exp_min, 'result':result.expected_minimum[1]}
prettyResult['expected_minimum'] = exp_min_out

prettyResult['input_header'] = header_list
prettyResult['input_values'] = result_list

##################### END #####################

plot_convergence(result)
addPlot(response["plots"], "convergence")

dimensions = []
# names = result['config'].gen_opt_vars(result, request)
# for var in names:
# dimensions.append(var.name[:20])
#plot_objective(result, dimensions=dimensions, usepartialdependence=False)
#addPlot(result, "objective", debug=True)

return dumps(response)

def addPlot(result, id="generic", close=True, debug=False):
pic_IObytes = io.BytesIO()
plt.savefig(pic_IObytes, format='png')
pic_IObytes.seek(0)
pic_hash = base64.b64encode(pic_IObytes.read())
result.append({
"id": id,
"plot": str(pic_hash, "utf-8")
})

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"))
if close:
plt.clf()
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
connexion==2.7.0
connexion[swagger-ui]
Flask==1.1.2
ProcessOptimizer==0.5.1
ProcessOptimizer==0.5.1
json-tricks==3.15.5
2 changes: 1 addition & 1 deletion tests/test_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ def test_first_sample():
yi = 1
result = optimizer.run(params="{}", Xi=Xi, yi=yi)
print(result)
assert "Run with" in result
assert "plots" in result

0 comments on commit 2b7e616

Please sign in to comment.