mirror of
https://github.com/wassname/optuna-dashboard.git
synced 2026-09-09 11:28:14 +08:00
Merge branch 'main' of github.com:optuna/optuna-dashboard into active_trials
This commit is contained in:
@@ -135,7 +135,13 @@ class APITestCase(TestCase):
|
||||
app,
|
||||
f"/api/studies/{study_id}/preference",
|
||||
"POST",
|
||||
body=json.dumps({"best_trials": [0, 2], "worst_trials": [1]}),
|
||||
body=json.dumps(
|
||||
{
|
||||
"mode": "ChooseWorst",
|
||||
"candidates": [0, 1, 2],
|
||||
"clicked": 1,
|
||||
}
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(status, 204)
|
||||
@@ -150,6 +156,30 @@ class APITestCase(TestCase):
|
||||
assert better.number == 2
|
||||
assert worse.number == 1
|
||||
|
||||
def test_report_preference_when_typo_mode(self) -> None:
|
||||
storage = optuna.storages.InMemoryStorage()
|
||||
study = create_study(storage=storage, n_generate=3)
|
||||
for _ in range(3):
|
||||
trial = study.ask()
|
||||
study.mark_comparison_ready(trial)
|
||||
|
||||
app = create_app(storage)
|
||||
study_id = study._study._study_id
|
||||
status, _, _ = send_request(
|
||||
app,
|
||||
f"/api/studies/{study_id}/preference",
|
||||
"POST",
|
||||
body=json.dumps(
|
||||
{
|
||||
"mode": "ChoseWorst",
|
||||
"candidates": [0, 1, 2],
|
||||
"clicked": 1,
|
||||
}
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(status, 400)
|
||||
|
||||
def test_skip_trial(self) -> None:
|
||||
storage = optuna.storages.InMemoryStorage()
|
||||
study = create_study(n_generate=4, storage=storage)
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import optuna
|
||||
from optuna_dashboard import _custom_plot_data as custom_plot_data
|
||||
from optuna_dashboard import save_plotly_graph_object
|
||||
import pytest
|
||||
|
||||
|
||||
def get_dummy_study() -> optuna.Study:
|
||||
def objective(trial: optuna.Trial) -> float:
|
||||
x = trial.suggest_float("x", -100, 100)
|
||||
y = trial.suggest_categorical("y", [-1, 0, 1])
|
||||
return x**2 + y
|
||||
|
||||
study = optuna.create_study()
|
||||
optuna.logging.set_verbosity(optuna.logging.ERROR)
|
||||
study.optimize(objective, n_trials=100)
|
||||
return study
|
||||
|
||||
|
||||
def test_save_plotly_graph_object() -> None:
|
||||
# Save history plot
|
||||
dummy_study = get_dummy_study()
|
||||
plot_data = optuna.visualization.plot_optimization_history(dummy_study)
|
||||
graph_object_id = save_plotly_graph_object(dummy_study, plot_data)
|
||||
|
||||
study_system_attrs = dummy_study._storage.get_study_system_attrs(dummy_study._study_id)
|
||||
plot_data_dict = custom_plot_data.get_plotly_graph_objects(study_system_attrs)
|
||||
assert len(plot_data_dict) == 1
|
||||
assert plot_data_dict[graph_object_id] == plot_data.to_json()
|
||||
|
||||
# Save parallel coordinate plot
|
||||
plot_data = optuna.visualization.plot_parallel_coordinate(dummy_study)
|
||||
graph_object_id = save_plotly_graph_object(dummy_study, plot_data)
|
||||
|
||||
study_system_attrs = dummy_study._storage.get_study_system_attrs(dummy_study._study_id)
|
||||
plot_data_dict = custom_plot_data.get_plotly_graph_objects(study_system_attrs)
|
||||
assert len(plot_data_dict) == 2
|
||||
assert plot_data_dict[graph_object_id] == plot_data.to_json()
|
||||
|
||||
|
||||
def test_update_plotly_graph_object() -> None:
|
||||
# Save history plot
|
||||
dummy_study = get_dummy_study()
|
||||
plot_data = optuna.visualization.plot_optimization_history(dummy_study)
|
||||
graph_object_id = save_plotly_graph_object(dummy_study, plot_data)
|
||||
|
||||
study_system_attrs = dummy_study._storage.get_study_system_attrs(dummy_study._study_id)
|
||||
plot_data_dict = custom_plot_data.get_plotly_graph_objects(study_system_attrs)
|
||||
assert len(plot_data_dict) == 1
|
||||
assert plot_data_dict[graph_object_id] == plot_data.to_json()
|
||||
|
||||
# Save parallel coordinate plot
|
||||
plot_data = optuna.visualization.plot_parallel_coordinate(dummy_study)
|
||||
graph_object_id = save_plotly_graph_object(
|
||||
dummy_study, plot_data, graph_object_id=graph_object_id
|
||||
)
|
||||
|
||||
study_system_attrs = dummy_study._storage.get_study_system_attrs(dummy_study._study_id)
|
||||
plot_data_dict = custom_plot_data.get_plotly_graph_objects(study_system_attrs)
|
||||
assert len(plot_data_dict) == 1
|
||||
assert plot_data_dict[graph_object_id] == plot_data.to_json()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"name",
|
||||
[
|
||||
"0",
|
||||
"a",
|
||||
"a1-:_.",
|
||||
],
|
||||
)
|
||||
def test_is_valid_graph_object_id(name: str) -> None:
|
||||
assert custom_plot_data.is_valid_graph_object_id(name)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"name",
|
||||
[
|
||||
"a,",
|
||||
"a b",
|
||||
"aあいうえお",
|
||||
],
|
||||
)
|
||||
def test_is_invalid_graph_object_id(name: str) -> None:
|
||||
assert not custom_plot_data.is_valid_graph_object_id(name)
|
||||
@@ -0,0 +1,62 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Callable
|
||||
|
||||
from optuna_dashboard._preferential_history import NewHistory
|
||||
from optuna_dashboard._preferential_history import report_history
|
||||
from optuna_dashboard._serializer import serialize_preference_history
|
||||
from optuna_dashboard.preferential import create_study
|
||||
from optuna_dashboard.preferential._system_attrs import _SYSTEM_ATTR_PREFIX_PREFERENCE
|
||||
|
||||
from .storage_supplier import parametrize_storages
|
||||
from .storage_supplier import StorageSupplier
|
||||
|
||||
|
||||
@parametrize_storages
|
||||
def test_report_and_get_choices(storage_supplier: Callable[[], StorageSupplier]) -> None:
|
||||
with storage_supplier() as storage:
|
||||
study = create_study(storage=storage, n_generate=5)
|
||||
for _ in range(5):
|
||||
trial = study.ask()
|
||||
trial.suggest_float("x", 0, 1)
|
||||
study.mark_comparison_ready(trial)
|
||||
|
||||
study_id = study._study._study_id
|
||||
|
||||
report_history(
|
||||
study_id=study_id,
|
||||
storage=storage,
|
||||
input_data=NewHistory(
|
||||
mode="ChooseWorst",
|
||||
candidates=[0, 1, 2],
|
||||
clicked=1,
|
||||
),
|
||||
)
|
||||
report_history(
|
||||
study_id=study_id,
|
||||
storage=storage,
|
||||
input_data=NewHistory(
|
||||
mode="ChooseWorst",
|
||||
candidates=[0, 2, 3, 4],
|
||||
clicked=0,
|
||||
),
|
||||
)
|
||||
history = serialize_preference_history(storage.get_study_system_attrs(study_id))
|
||||
sys_attrs = storage.get_study_system_attrs(study_id)
|
||||
assert len(history) == 2
|
||||
assert history[0]["candidates"] == [0, 1, 2]
|
||||
assert history[0]["clicked"] == 1
|
||||
preferences = sys_attrs[_SYSTEM_ATTR_PREFIX_PREFERENCE + history[0]["preference_id"]]
|
||||
assert len(preferences) == 2
|
||||
for i, (best, worst) in enumerate([(0, 1), (2, 1)]):
|
||||
assert len(preferences[i]) == 2
|
||||
assert preferences[i][0] == best
|
||||
assert preferences[i][1] == worst
|
||||
assert history[1]["candidates"] == [0, 2, 3, 4]
|
||||
assert history[1]["clicked"] == 0
|
||||
preferences = sys_attrs[_SYSTEM_ATTR_PREFIX_PREFERENCE + history[1]["preference_id"]]
|
||||
assert len(preferences) == 3
|
||||
for i, (best, worst) in enumerate([(2, 0), (3, 0), (4, 0)]):
|
||||
assert len(preferences[i]) == 2
|
||||
assert preferences[i][0] == best
|
||||
assert preferences[i][1] == worst
|
||||
@@ -29,7 +29,7 @@ def test_get_study_detail_is_preferential() -> None:
|
||||
assert len(study_summaries) == 1
|
||||
|
||||
study_summary = study_summaries[0]
|
||||
study_detail = serialize_study_detail(study_summary, [], study.trials, [], [], [], False)
|
||||
study_detail = serialize_study_detail(study_summary, [], study.trials, [], [], [], False, {})
|
||||
assert study_detail["is_preferential"]
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ def test_get_study_detail_is_not_preferential() -> None:
|
||||
assert len(study_summaries) == 1
|
||||
|
||||
study_summary = study_summaries[0]
|
||||
study_detail = serialize_study_detail(study_summary, [], study.trials, [], [], [], False)
|
||||
study_detail = serialize_study_detail(study_summary, [], study.trials, [], [], [], False, {})
|
||||
assert not study_detail["is_preferential"]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user