From 852dd3d16b2679100860961b839803b7ee28a901 Mon Sep 17 00:00:00 2001 From: moririn2528 Date: Wed, 16 Aug 2023 13:25:33 +0900 Subject: [PATCH] add API to report preference --- optuna_dashboard/_app.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/optuna_dashboard/_app.py b/optuna_dashboard/_app.py index ab26a1a6..53a98df7 100644 --- a/optuna_dashboard/_app.py +++ b/optuna_dashboard/_app.py @@ -38,6 +38,9 @@ from ._storage_url import get_storage from .artifact._backend import delete_all_artifacts from .artifact._backend import register_artifact_route from .artifact._backend_to_store import to_artifact_store +from .preferential._study import _SYSTEM_ATTR_PREFERENTIAL_STUDY +from .preferential._study import get_best_trials as get_best_preferential_trials +from .preferential._system_attrs import report_preferences if typing.TYPE_CHECKING: @@ -187,8 +190,12 @@ def create_app( return {"reason": f"study_id={study_id} is not found"} trials = get_trials(storage, study_id) + system_attrs = getattr(summary, "system_attrs", {}) + is_preferential = system_attrs.get(_SYSTEM_ATTR_PREFERENTIAL_STUDY, False) # TODO(c-bata): Cache best_trials - if len(summary.directions) == 1: + if is_preferential: + best_trials = get_best_preferential_trials(study_id, storage) + elif len(summary.directions) == 1: if len([t for t in trials if t.state == TrialState.COMPLETE]) == 0: best_trials = [] else: @@ -255,6 +262,29 @@ def create_app( response.status = 204 # No content return {} + @app.post("/api/studies//preference") + @json_api_view + def post_preference(study_id: int) -> dict[str, Any]: + try: + best_trials = [int(d) for d in request.json.get("best_trials", [])] + worst_trials = [int(d) for d in request.json.get("worst_trials", [])] + except ValueError: + response.status = 400 + return {"reason": "best_trials and worst_trials must be an array of integers."} + if len(best_trials) == 0 or len(worst_trials) == 0: + response.status = 400 # Bad request + return {"reason": "You need to set best_trials and worst_trials"} + + try: + preferences = [(best, worst) for best in best_trials for worst in worst_trials] + report_preferences(study_id, storage, preferences) + except Exception as e: + response.status = 500 + return {"reason": f"Internal server error: {e}"} + + response.status = 204 + return {} + @app.post("/api/trials//tell") @json_api_view def tell_trial(trial_id: int) -> dict[str, Any]: