Merge branch 'main' of github.com:optuna/optuna-dashboard into active_trials

This commit is contained in:
Contramundum
2023-09-05 17:02:27 +09:00
12 changed files with 424 additions and 263 deletions
+3
View File
@@ -0,0 +1,3 @@
[run]
concurrency = multiprocessing,thread
source = optuna_dashboard/
+48
View File
@@ -0,0 +1,48 @@
name: python coverage
on:
push:
branches:
- master
pull_request: {}
jobs:
coverage:
runs-on: ubuntu-latest
# Not intended for forks.
if: github.repository == 'optuna/optuna-dashboard'
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: x64
- name: Install dependencies
run: |
python -m pip install --progress-bar off --upgrade pip setuptools
pip install --progress-bar off .[optional]
pip install --progress-bar off .[test]
pip install --progress-bar off "optuna>=3.0.0"
pip install --progress-bar off .
echo 'import coverage; coverage.process_startup()' > sitecustomize.py
- name: Tests
env:
PYTHONPATH: . # To invoke sitecutomize.py
COVERAGE_PROCESS_START: .coveragerc # https://coverage.readthedocs.io/en/6.4.1/subprocess.html
COVERAGE_COVERAGE: yes # https://github.com/nedbat/coveragepy/blob/65bf33fc03209ffb01bbbc0d900017614645ee7a/coverage/control.py#L255-L261
run: |
coverage run --source=optuna_dashboard -m pytest python_tests
coverage combine
coverage xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
fail_ci_if_error: true
+4 -2
View File
@@ -45,7 +45,8 @@ jobs:
# python_tests requires optuna>=3.0.0 since it imports FloatDistribution
run: |
python -m pip install --progress-bar off --upgrade pip setuptools
pip install streamlit boto3 moto[s3] pytest
pip install --progress-bar off .[optional]
pip install --progress-bar off .[test]
pip install --progress-bar off "optuna>=3.0.0"
pip install --progress-bar off .
- run: pytest python_tests
@@ -61,7 +62,8 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --progress-bar off --upgrade pip setuptools
pip install streamlit boto3 moto[s3] pytest
pip install --progress-bar off .[optional]
pip install --progress-bar off .[test]
pip install --progress-bar off .
python -m pip install --progress-bar off --upgrade git+https://github.com/optuna/optuna.git
- run: pytest python_tests
+5
View File
@@ -28,6 +28,11 @@ docs/_generated/
rustlib/target/
rustlib/pkg/
# Test
.coverage
.coverage.*
coverage.xml
# Others
.envrc
.idea/
+1 -1
View File
@@ -45,7 +45,7 @@ docs: docs/conf.py $(RST_FILES)
.PHONY: fmt
fmt:
npm run fmt
black ./optuna_dashboard/ ./python_tests/
black ./optuna_dashboard/ ./python_tests/ ./e2e_tests/
isort .
.PHONY: clean
View File
-245
View File
@@ -1,251 +1,6 @@
import socket
import threading
from wsgiref.simple_server import make_server
import optuna
from optuna_dashboard import wsgi
import pytest
study_names = [
"single",
"single-trial",
"single-1-param",
"single-dynamic",
"single-inf",
"multi-objective",
"multi-dynamic",
"single-pruned-without-report",
"single-inf-report",
"issue-410",
"single-no-trials",
"multi-no-trials",
]
def make_dummy_storage(study_name: str) -> optuna.storages.InMemoryStorage:
storage = optuna.storages.InMemoryStorage()
sampler = optuna.samplers.RandomSampler(seed=0)
# Sinble objective study
if study_name == "single":
study = optuna.create_study(study_name=study_name, storage=storage, sampler=sampler)
def objective_single(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_single, n_trials=50)
# A single objective study with a single trial
# Refs: https://github.com/optuna/optuna-dashboard/issues/401
elif study_name == "single-trial":
study = optuna.create_study(study_name=study_name, storage=storage, sampler=sampler)
def objective_single(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_single, n_trials=1)
# Single-objective study with 1 parameter
elif study_name == "single-1-param":
study = optuna.create_study(
study_name=study_name, storage=storage, direction="maximize", sampler=sampler
)
def objective_single_with_1param(trial: optuna.Trial) -> float:
x1 = trial.suggest_float("x1", 0, 10)
return -((x1 - 2) ** 2)
study.optimize(objective_single_with_1param, n_trials=50)
# Single-objective study with dynamic search space
elif study_name == "single-dynamic":
study = optuna.create_study(
study_name=study_name, storage=storage, direction="maximize", sampler=sampler
)
def objective_single_dynamic(trial: optuna.Trial) -> float:
category = trial.suggest_categorical("category", ["foo", "bar"])
if category == "foo":
return (trial.suggest_float("x1", 0, 10) - 2) ** 2
else:
return -((trial.suggest_float("x2", -10, 0) + 5) ** 2)
study.optimize(objective_single_dynamic, n_trials=50)
# Single objective study with 'inf', '-inf', or 'nan' value
elif study_name == "single-inf":
study = optuna.create_study(study_name=study_name, storage=storage, sampler=sampler)
def objective_single_inf(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", -10, 10)
if trial.number % 3 == 0:
return float("inf")
elif trial.number % 3 == 1:
return float("-inf")
else:
return x**2
study.optimize(objective_single_inf, n_trials=50)
# Multi-objective study
elif study_name == "multi-objective":
study = optuna.create_study(
study_name=study_name,
storage=storage,
directions=["minimize", "minimize"],
sampler=sampler,
)
def objective_multi(trial: optuna.Trial) -> tuple[float, float]:
x = trial.suggest_float("x", 0, 5)
y = trial.suggest_float("y", 0, 3)
v0 = 4 * x**2 + 4 * y**2
v1 = (x - 5) ** 2 + (y - 5) ** 2
return v0, v1
study.optimize(objective_multi, n_trials=50)
# Multi-objective study with dynamic search space
elif study_name == "multi-dynamic":
study = optuna.create_study(
study_name=study_name,
storage=storage,
directions=["minimize", "minimize"],
sampler=sampler,
)
def objective_multi_dynamic(trial: optuna.Trial) -> tuple[float, float]:
category = trial.suggest_categorical("category", ["foo", "bar"])
if category == "foo":
x = trial.suggest_float("x1", 0, 5)
y = trial.suggest_float("y1", 0, 3)
v0 = 4 * x**2 + 4 * y**2
v1 = (x - 5) ** 2 + (y - 5) ** 2
return v0, v1
else:
x = trial.suggest_float("x2", 0, 5)
y = trial.suggest_float("y2", 0, 3)
v0 = 2 * x**2 + 2 * y**2
v1 = (x - 2) ** 2 + (y - 3) ** 2
return v0, v1
study.optimize(objective_multi_dynamic, n_trials=50)
# Pruning with no intermediate values
elif study_name == "single-pruned-without-report":
study = optuna.create_study(study_name=study_name, storage=storage, sampler=sampler)
def objective_prune_without_report(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", -15, 30)
y = trial.suggest_float("y", -15, 30)
v = x**2 + y**2
if v > 100:
raise optuna.TrialPruned()
return v
study.optimize(objective_prune_without_report, n_trials=100)
# Single objective pruned after reported 'inf', '-inf', or 'nan'
elif study_name == "single-inf-report":
study = optuna.create_study(study_name=study_name, storage=storage, sampler=sampler)
def objective_single_inf_report(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", -10, 10)
if trial.number % 3 == 0:
trial.report(float("inf"), 1)
elif trial.number % 3 == 1:
trial.report(float("-inf"), 1)
else:
trial.report(float("nan"), 1)
if x > 0:
raise optuna.TrialPruned()
else:
return x**2
study.optimize(objective_single_inf_report, n_trials=50)
# Issue 410
elif study_name == "issue-410":
study = optuna.create_study(study_name=study_name, storage=storage, sampler=sampler)
def objective_issue_410(trial: optuna.Trial) -> float:
trial.suggest_categorical("resample_rate", ["50ms"])
trial.suggest_categorical("channels", ["all"])
trial.suggest_categorical("window_size", [256])
if trial.number > 15:
raise Exception("Unexpected error")
trial.suggest_categorical("cbow", [True])
trial.suggest_categorical("model", ["m1"])
trial.set_user_attr("epochs", 0)
trial.set_user_attr("deterministic", True)
if trial.number > 10:
raise Exception("unexpeccted error")
trial.set_user_attr("folder", "/path/to/folder")
trial.set_user_attr("resample_type", "foo")
trial.set_user_attr("run_id", "0001")
return 1.0
study.optimize(objective_issue_410, n_trials=20, catch=(Exception,))
# No trials single-objective study
elif study_name == "single-no-trials":
optuna.create_study(study_name=study_name, storage=storage, sampler=sampler)
# No trials multi-objective study
elif study_name == "multi-no-trials":
optuna.create_study(
study_name=study_name,
storage=storage,
directions=["minimize", "maximize"],
sampler=sampler,
)
else:
assert False, f"No study configuration of {study_name} in conftest.py"
return storage
def get_free_port() -> int:
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp.bind(("", 0))
_, port = tcp.getsockname()
tcp.close()
return port
@pytest.fixture(scope="session", params=study_names)
def storage(request: pytest.FixtureRequest) -> optuna.storages.InMemoryStorage:
study_name = request.param
storage = make_dummy_storage(study_name)
return storage
@pytest.fixture(scope="session")
def server_url(request: pytest.FixtureRequest, storage: optuna.storages.InMemoryStorage) -> str:
addr = "127.0.0.1"
port = get_free_port()
app = wsgi(storage)
httpd = make_server(addr, port, app)
thread = threading.Thread(target=httpd.serve_forever)
thread.start()
def stop_server() -> None:
httpd.shutdown()
httpd.server_close()
thread.join()
request.addfinalizer(stop_server)
return f"http://{addr}:{port}/dashboard"
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args: dict) -> dict:
return {
+35
View File
@@ -0,0 +1,35 @@
import socket
import threading
from wsgiref.simple_server import make_server
import optuna
from optuna_dashboard import wsgi
import pytest
def get_free_port() -> int:
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp.bind(("", 0))
_, port = tcp.getsockname()
tcp.close()
return port
def make_test_server(
request: pytest.FixtureRequest, storage: optuna.storages.InMemoryStorage
) -> str:
addr = "127.0.0.1"
port = get_free_port()
app = wsgi(storage)
httpd = make_server(addr, port, app)
thread = threading.Thread(target=httpd.serve_forever)
thread.start()
def stop_server() -> None:
httpd.shutdown()
httpd.server_close()
thread.join()
request.addfinalizer(stop_server)
return f"http://{addr}:{port}/dashboard"
View File
@@ -1,5 +1,35 @@
import optuna
from playwright.sync_api import Page
import pytest
from ..test_server import make_test_server
def make_test_storage() -> optuna.storages.InMemoryStorage:
storage = optuna.storages.InMemoryStorage()
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(study_name="single", storage=storage, sampler=sampler)
def objective_single(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_single, n_trials=50)
return storage
@pytest.fixture
def storage() -> optuna.storages.InMemoryStorage:
storage = make_test_storage()
return storage
@pytest.fixture
def server_url(request: pytest.FixtureRequest, storage: optuna.storages.InMemoryStorage) -> str:
return make_test_server(request, storage)
def test_history_xaxis_click(
+286 -15
View File
@@ -1,15 +1,274 @@
from typing import Callable
import optuna
from playwright.sync_api import Page
import pytest
from .test_server import make_test_server
@pytest.fixture
def storage() -> optuna.storages.InMemoryStorage:
storage = optuna.storages.InMemoryStorage()
return storage
@pytest.fixture
def server_url(request: pytest.FixtureRequest, storage: optuna.storages.InMemoryStorage) -> str:
return make_test_server(request, storage)
def run_single_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(study_name="single", storage=storage, sampler=sampler)
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=50)
return study
def run_single_trial_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
# A single objective study with a single trial
# Refs: https://github.com/optuna/optuna-dashboard/issues/401
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(study_name="single-trial", storage=storage, sampler=sampler)
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=1)
return study
def run_single_1param_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(
study_name="single-1-param", storage=storage, direction="maximize", sampler=sampler
)
def objective(trial: optuna.Trial) -> float:
x1 = trial.suggest_float("x1", 0, 10)
return -((x1 - 2) ** 2)
study.optimize(objective, n_trials=50)
return study
def run_single_dynamic_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
# Single-objective study with dynamic search space
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(
study_name="single-dynamic", storage=storage, direction="maximize", sampler=sampler
)
def objective(trial: optuna.Trial) -> float:
category = trial.suggest_categorical("category", ["foo", "bar"])
if category == "foo":
return (trial.suggest_float("x1", 0, 10) - 2) ** 2
else:
return -((trial.suggest_float("x2", -10, 0) + 5) ** 2)
study.optimize(objective, n_trials=50)
return study
def run_single_inf_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
# Single objective study with 'inf', '-inf', or 'nan' value
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(study_name="single-inf", storage=storage, sampler=sampler)
def objective(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", -10, 10)
if trial.number % 3 == 0:
return float("inf")
elif trial.number % 3 == 1:
return float("-inf")
else:
return x**2
study.optimize(objective, n_trials=50)
return study
def run_multi_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
# Multi-objective study
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(
study_name="multi-objective",
storage=storage,
directions=["minimize", "minimize"],
sampler=sampler,
)
def objective(trial: optuna.Trial) -> tuple[float, float]:
x = trial.suggest_float("x", 0, 5)
y = trial.suggest_float("y", 0, 3)
v0 = 4 * x**2 + 4 * y**2
v1 = (x - 5) ** 2 + (y - 5) ** 2
return v0, v1
study.optimize(objective, n_trials=50)
return study
def run_multi_dynamic_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
# Multi-objective study with dynamic search space
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(
study_name="multi-dynamic",
storage=storage,
directions=["minimize", "minimize"],
sampler=sampler,
)
def objective(trial: optuna.Trial) -> tuple[float, float]:
category = trial.suggest_categorical("category", ["foo", "bar"])
if category == "foo":
x = trial.suggest_float("x1", 0, 5)
y = trial.suggest_float("y1", 0, 3)
v0 = 4 * x**2 + 4 * y**2
v1 = (x - 5) ** 2 + (y - 5) ** 2
return v0, v1
else:
x = trial.suggest_float("x2", 0, 5)
y = trial.suggest_float("y2", 0, 3)
v0 = 2 * x**2 + 2 * y**2
v1 = (x - 2) ** 2 + (y - 3) ** 2
return v0, v1
study.optimize(objective, n_trials=50)
return study
def run_single_pruned_without_report_objective_study(
storage: optuna.storages.InMemoryStorage,
) -> optuna.Study:
# Pruning with no intermediate values
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(
study_name="single-pruned-without-report", storage=storage, sampler=sampler
)
def objective(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", -15, 30)
y = trial.suggest_float("y", -15, 30)
v = x**2 + y**2
if v > 100:
raise optuna.TrialPruned()
return v
study.optimize(objective, n_trials=100)
return study
def run_single_inf_report_objective_study(
storage: optuna.storages.InMemoryStorage,
) -> optuna.Study:
# Single objective pruned after reported 'inf', '-inf', or 'nan'
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(study_name="single-inf-report", storage=storage, sampler=sampler)
def objective(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", -10, 10)
if trial.number % 3 == 0:
trial.report(float("inf"), 1)
elif trial.number % 3 == 1:
trial.report(float("-inf"), 1)
else:
trial.report(float("nan"), 1)
if x > 0:
raise optuna.TrialPruned()
else:
return x**2
study.optimize(objective, n_trials=50)
return study
def run_issue_410_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
# Issue 410
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(study_name="issue-410", storage=storage, sampler=sampler)
def objective(trial: optuna.Trial) -> float:
trial.suggest_categorical("resample_rate", ["50ms"])
trial.suggest_categorical("channels", ["all"])
trial.suggest_categorical("window_size", [256])
if trial.number > 15:
raise Exception("Unexpected error")
trial.suggest_categorical("cbow", [True])
trial.suggest_categorical("model", ["m1"])
trial.set_user_attr("epochs", 0)
trial.set_user_attr("deterministic", True)
if trial.number > 10:
raise Exception("unexpeccted error")
trial.set_user_attr("folder", "/path/to/folder")
trial.set_user_attr("resample_type", "foo")
trial.set_user_attr("run_id", "0001")
return 1.0
study.optimize(objective, n_trials=20, catch=(Exception,))
return study
def run_single_no_trials_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
# No trials single-objective study
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(study_name="single-no-trials", storage=storage, sampler=sampler)
return study
def run_multi_no_trials_objective_study(storage: optuna.storages.InMemoryStorage) -> optuna.Study:
# No trials multi-objective study
sampler = optuna.samplers.RandomSampler(seed=0)
study = optuna.create_study(
study_name="multi-no-trials",
storage=storage,
directions=["minimize", "maximize"],
sampler=sampler,
)
return study
parameterize_studies = pytest.mark.parametrize(
"run_study",
[
run_single_objective_study,
run_single_trial_objective_study,
run_single_1param_objective_study,
run_single_dynamic_objective_study,
run_single_inf_objective_study,
run_multi_objective_study,
run_multi_dynamic_objective_study,
run_single_pruned_without_report_objective_study,
run_single_inf_report_objective_study,
run_issue_410_objective_study,
run_single_no_trials_objective_study,
run_multi_no_trials_objective_study,
],
)
@parameterize_studies
def test_study_list(
page: Page,
storage: optuna.storages.InMemoryStorage,
server_url: str,
run_study: Callable[[optuna.storages.InMemoryStorage], optuna.Study],
) -> None:
summaries = optuna.get_all_study_summaries(storage)
study_id = summaries[0]._study_id
study_name = summaries[0].study_name
study = run_study(storage)
study_id = study._study_id
study_name = study.study_name
page.goto(server_url)
page.click(f"a[href='/dashboard/studies/{study_id}']")
@@ -22,14 +281,17 @@ def test_study_list(
assert study_name in title
@parameterize_studies
def test_study_analytics(
page: Page,
storage: optuna.storages.InMemoryStorage,
server_url: str,
run_study: Callable[[optuna.storages.InMemoryStorage], optuna.Study],
) -> None:
summaries = optuna.get_all_study_summaries(storage)
study_id = summaries[0]._study_id
study_name = summaries[0].study_name
study = run_study(storage)
study_id = study._study_id
study_name = study.study_name
url = f"{server_url}/studies/{study_id}"
page.goto(url)
@@ -43,14 +305,17 @@ def test_study_analytics(
assert study_name in title
@parameterize_studies
def test_trial_list(
page: Page,
storage: optuna.storages.InMemoryStorage,
server_url: str,
run_study: Callable[[optuna.storages.InMemoryStorage], optuna.Study],
) -> None:
summaries = optuna.get_all_study_summaries(storage)
study_id = summaries[0]._study_id
study_name = summaries[0].study_name
study = run_study(storage)
study_id = study._study_id
study_name = study.study_name
url = f"{server_url}/studies/{study_id}"
page.goto(url)
@@ -64,14 +329,17 @@ def test_trial_list(
assert study_name in title
@parameterize_studies
def test_trial_table(
page: Page,
storage: optuna.storages.InMemoryStorage,
server_url: str,
run_study: Callable[[optuna.storages.InMemoryStorage], optuna.Study],
) -> None:
summaries = optuna.get_all_study_summaries(storage)
study_id = summaries[0]._study_id
study_name = summaries[0].study_name
study = run_study(storage)
study_id = study._study_id
study_name = study.study_name
url = f"{server_url}/studies/{study_id}"
page.goto(url)
@@ -85,14 +353,17 @@ def test_trial_table(
assert study_name in title
@parameterize_studies
def test_trial_note(
page: Page,
storage: optuna.storages.InMemoryStorage,
server_url: str,
run_study: Callable[[optuna.storages.InMemoryStorage], optuna.Study],
) -> None:
summaries = optuna.get_all_study_summaries(storage)
study_id = summaries[0]._study_id
study_name = summaries[0].study_name
study = run_study(storage)
study_id = study._study_id
study_name = study.study_name
url = f"{server_url}/studies/{study_id}"
page.goto(url)
+12
View File
@@ -41,6 +41,18 @@ docs = [
"sphinx_rtd_theme",
]
test = [
"coverage",
"pytest",
"moto[s3]",
]
optional = [
"streamlit",
"boto3",
]
[project.scripts]
optuna-dashboard = "optuna_dashboard._cli:main"