Skip to content

Commit

Permalink
Generate single plots
Browse files Browse the repository at this point in the history
  • Loading branch information
langdal committed Jun 27, 2023
1 parent b1aafed commit e9172b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
6 changes: 6 additions & 0 deletions optimizerapi/openapi/specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ components:
type: string
experimentSuggestionCount:
type: number
maxQuality:
type: number
graphs:
type: array
items:
type: string
additionalProperties:
type: string
data:
Expand Down
37 changes: 26 additions & 11 deletions optimizerapi/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import traceback
import hashlib
import json_tricks
import matplotlib as mpl
from rq import Queue
from rq.job import Job
from rq.exceptions import NoSuchJobError
Expand All @@ -27,6 +28,8 @@
import matplotlib.pyplot as plt
import numpy
import connexion

from optimizerapi.plot_patch import plot_brownie_bee
from .securepickle import pickleToString, get_crypto

numpy.random.seed(42)
Expand Down Expand Up @@ -224,6 +227,8 @@ def process_result(result, optimizer, dimensions, cfg, extras, data, space):
# behavior is "png", so the API returns png images. Any other input is interpreted
# as "None" at the moment.
graph_format = extras.get("graphFormat", "png")
max_quality = int(extras.get("maxQuality", "5"))
graphs_to_return = extras.get("graphs", ['objective', 'convergence'])

objective_pars = extras.get("objectivePars", "result")

Expand All @@ -247,17 +252,27 @@ def process_result(result, optimizer, dimensions, cfg, extras, data, space):
result_details["models"] = [process_model(model, optimizer) for model in result]
if graph_format == "png":
for idx, model in enumerate(result):
plot_convergence(model)
add_plot(plots, f"convergence_{idx}")

plot_objective(
model,
dimensions=dimensions,
usepartialdependence=False,
show_confidence=True,
pars=objective_pars,
)
add_plot(plots, f"objective_{idx}")
if 'single' in graphs_to_return:
bb_plots = plot_brownie_bee(model, max_quality=max_quality)
for i, plot in enumerate(bb_plots):
pic_io_bytes = io.BytesIO()
plot.savefig(pic_io_bytes, format="png")
pic_io_bytes.seek(0)
pic_hash = base64.b64encode(pic_io_bytes.read())
plots.append({"id": f"single_{idx}_{i}", "plot": str(pic_hash, "utf-8")})
if 'convergence' in graphs_to_return:
plot_convergence(model)
add_plot(plots, f"convergence_{idx}")

if 'objective' in graphs_to_return:
plot_objective(
model,
dimensions=dimensions,
usepartialdependence=False,
show_confidence=True,
pars=objective_pars,
)
add_plot(plots, f"objective_{idx}")

if optimizer.n_objectives == 1:
minimum = expected_minimum(result[0], return_std=True)
Expand Down

0 comments on commit e9172b3

Please sign in to comment.