mirror of
https://github.com/wassname/optuna-dashboard.git
synced 2026-09-09 11:28:14 +08:00
Follow review comments
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import os
|
||||
import tempfile
|
||||
from typing import Callable
|
||||
|
||||
import optuna
|
||||
from playwright.sync_api import Page
|
||||
import pytest
|
||||
|
||||
from ..test_server import make_standalone_server
|
||||
from ..utils import count_components
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -25,31 +28,49 @@ def test_home(
|
||||
assert title == "Optuna Dashboard (Wasm ver.)"
|
||||
|
||||
|
||||
def test_load_rdb_storage(
|
||||
def create_rdb_storage_sqlite_file(filename: str, study_name: str):
|
||||
storage = optuna.storages.RDBStorage(f"sqlite:///{filename}")
|
||||
study = optuna.create_study(study_name=study_name, storage=storage)
|
||||
|
||||
def objective(trial: optuna.Trial) -> float:
|
||||
x1 = trial.suggest_float("x1", 0, 10)
|
||||
x2 = trial.suggest_float("x2", 0, 10)
|
||||
return (x1 - 2) ** 2 + (x2 - 5) ** 2
|
||||
|
||||
study.optimize(objective, n_trials=100)
|
||||
|
||||
|
||||
def create_journal_storage_local_file(filename: str, study_name: str):
|
||||
storage = optuna.storages.JournalStorage(
|
||||
optuna.storages.JournalFileStorage(f"{filename}"),
|
||||
)
|
||||
study = optuna.create_study(study_name=study_name, storage=storage)
|
||||
|
||||
def objective(trial: optuna.Trial) -> float:
|
||||
x1 = trial.suggest_float("x1", 0, 10)
|
||||
x2 = trial.suggest_float("x2", 0, 10)
|
||||
return (x1 - 2) ** 2 + (x2 - 5) ** 2
|
||||
|
||||
study.optimize(objective, n_trials=100)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"create_storage_file",
|
||||
[create_rdb_storage_sqlite_file, create_journal_storage_local_file],
|
||||
)
|
||||
def test_load_storage(
|
||||
page: Page,
|
||||
server_url: str,
|
||||
create_storage_file: Callable[[str, str], optuna.storages.BaseStorage],
|
||||
) -> None:
|
||||
study_name = "single-objective"
|
||||
url = f"{server_url}"
|
||||
|
||||
def create_storage_file(filename: str):
|
||||
import optuna
|
||||
|
||||
storage = optuna.storages.RDBStorage(f"sqlite:///{filename}")
|
||||
study = optuna.create_study(study_name=study_name, storage=storage)
|
||||
|
||||
def objective(trial: optuna.Trial) -> float:
|
||||
x1 = trial.suggest_float("x1", 0, 10)
|
||||
x2 = trial.suggest_float("x2", 0, 10)
|
||||
return (x1 - 2) ** 2 + (x2 - 5) ** 2
|
||||
|
||||
study.optimize(objective, n_trials=100)
|
||||
|
||||
with tempfile.TemporaryDirectory() as dir:
|
||||
with tempfile.NamedTemporaryFile() as fp:
|
||||
filename = fp.name
|
||||
path = os.path.join(dir, filename)
|
||||
create_storage_file(filename)
|
||||
create_storage_file(filename, study_name)
|
||||
page.goto(url)
|
||||
with page.expect_file_chooser() as fc_info:
|
||||
page.get_by_role("button").nth(2).click()
|
||||
@@ -58,62 +79,5 @@ def test_load_rdb_storage(
|
||||
|
||||
page.get_by_role("link", name=study_name).click()
|
||||
|
||||
def count_components(page: Page, component_name: str):
|
||||
component_count = page.evaluate(
|
||||
f"""() => {{
|
||||
const components = document.querySelectorAll('.{component_name}');
|
||||
return components.length;
|
||||
}}"""
|
||||
)
|
||||
return component_count
|
||||
|
||||
count = count_components(page, "MuiCard-root")
|
||||
assert count == 4
|
||||
|
||||
|
||||
def test_load_journal_storage(
|
||||
page: Page,
|
||||
server_url: str,
|
||||
) -> None:
|
||||
study_name = "single-objective"
|
||||
url = f"{server_url}"
|
||||
|
||||
def create_storage_file(filename: str):
|
||||
import optuna
|
||||
|
||||
storage = optuna.storages.JournalStorage(
|
||||
optuna.storages.JournalFileStorage(f"{filename}"),
|
||||
)
|
||||
study = optuna.create_study(study_name=study_name, storage=storage)
|
||||
|
||||
def objective(trial: optuna.Trial) -> float:
|
||||
x1 = trial.suggest_float("x1", 0, 10)
|
||||
x2 = trial.suggest_float("x2", 0, 10)
|
||||
return (x1 - 2) ** 2 + (x2 - 5) ** 2
|
||||
|
||||
study.optimize(objective, n_trials=100)
|
||||
|
||||
with tempfile.TemporaryDirectory() as dir:
|
||||
with tempfile.NamedTemporaryFile() as fp:
|
||||
filename = fp.name
|
||||
path = os.path.join(dir, filename)
|
||||
create_storage_file(filename)
|
||||
page.goto(url)
|
||||
with page.expect_file_chooser() as fc_info:
|
||||
page.get_by_role("button").nth(2).click()
|
||||
file_chooser = fc_info.value
|
||||
file_chooser.set_files(path)
|
||||
|
||||
page.get_by_role("link", name=study_name).click()
|
||||
|
||||
def count_components(page: Page, component_name: str):
|
||||
component_count = page.evaluate(
|
||||
f"""() => {{
|
||||
const components = document.querySelectorAll('.{component_name}');
|
||||
return components.length;
|
||||
}}"""
|
||||
)
|
||||
return component_count
|
||||
|
||||
count = count_components(page, "MuiCard-root")
|
||||
assert count == 4
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
from optuna_dashboard._storage import trials_cache
|
||||
from optuna_dashboard._storage import trials_cache_lock
|
||||
from optuna_dashboard._storage import trials_last_fetched_at
|
||||
from playwright.sync_api import Page
|
||||
|
||||
|
||||
def clear_inmemory_cache() -> None:
|
||||
with trials_cache_lock:
|
||||
trials_cache.clear()
|
||||
trials_last_fetched_at.clear()
|
||||
|
||||
|
||||
def count_components(page: Page, component_name: str):
|
||||
component_count = page.evaluate(
|
||||
f"""() => {{
|
||||
const components = document.querySelectorAll('.{component_name}');
|
||||
return components.length;
|
||||
}}"""
|
||||
)
|
||||
return component_count
|
||||
|
||||
Reference in New Issue
Block a user