From 48c30d5955097e2c0f0ba0fc096640ffe4356dbb Mon Sep 17 00:00:00 2001 From: c-bata Date: Wed, 8 Jun 2022 16:00:21 +0900 Subject: [PATCH] Add black settings --- optuna_dashboard/_app.py | 16 ++------ .../_cached_extra_study_property.py | 4 +- optuna_dashboard/_cli.py | 4 +- optuna_dashboard/_note.py | 8 +--- optuna_dashboard/_serializer.py | 8 +--- optuna_dashboard/_sql_profiler.py | 3 +- pyproject.toml | 26 +++++++++++++ python_tests/test_api.py | 3 +- .../test_cached_extra_study_property.py | 1 - setup.cfg | 11 ------ visual_regression_test.py | 39 +++++-------------- 11 files changed, 48 insertions(+), 75 deletions(-) create mode 100644 pyproject.toml diff --git a/optuna_dashboard/_app.py b/optuna_dashboard/_app.py index dbea1f8a..d6d0a862 100644 --- a/optuna_dashboard/_app.py +++ b/optuna_dashboard/_app.py @@ -146,9 +146,7 @@ def get_study_summary(storage: BaseStorage, study_id: int) -> Optional[StudySumm return None -def get_trials( - storage: BaseStorage, study_id: int, ttl_seconds: int = 10 -) -> List[FrozenTrial]: +def get_trials(storage: BaseStorage, study_id: int, ttl_seconds: int = 10) -> List[FrozenTrial]: with trials_cache_lock: trials = trials_cache.get(study_id, None) last_fetched_at = trials_last_fetched_at.get(study_id, None) @@ -247,9 +245,7 @@ def create_app(storage: BaseStorage, debug: bool = False) -> Bottle: storage.set_study_directions( study_id, [ - StudyDirection.MAXIMIZE - if d.lower() == "maximize" - else StudyDirection.MINIMIZE + StudyDirection.MAXIMIZE if d.lower() == "maximize" else StudyDirection.MINIMIZE for d in directions ], ) @@ -311,15 +307,11 @@ def create_app(storage: BaseStorage, debug: bool = False) -> Bottle: return {"reason": f"study_id={study_id} is not found"} if objective_id >= n_directions: response.status = 400 # Bad request - return { - "reason": f"study_id={study_id} has only {n_directions} direction(s)." - } + return {"reason": f"study_id={study_id} has only {n_directions} direction(s)."} trials = get_trials(storage, study_id) try: - return get_param_importance_from_trials_cache( - storage, study_id, objective_id, trials - ) + return get_param_importance_from_trials_cache(storage, study_id, objective_id, trials) except ValueError as e: response.status = 400 # Bad request return {"reason": str(e)} diff --git a/optuna_dashboard/_cached_extra_study_property.py b/optuna_dashboard/_cached_extra_study_property.py index 5945dfcc..3688a010 100644 --- a/optuna_dashboard/_cached_extra_study_property.py +++ b/optuna_dashboard/_cached_extra_study_property.py @@ -25,9 +25,7 @@ def get_cached_extra_study_property( study_id: int, trials: List[FrozenTrial] ) -> Tuple[SearchSpaceListT, SearchSpaceListT, bool]: with cached_extra_study_property_cache_lock: - cached_extra_study_property = cached_extra_study_property_cache.get( - study_id, None - ) + cached_extra_study_property = cached_extra_study_property_cache.get(study_id, None) if cached_extra_study_property is None: cached_extra_study_property = _CachedExtraStudyProperty() cached_extra_study_property.update(trials) diff --git a/optuna_dashboard/_cli.py b/optuna_dashboard/_cli.py index 6799ef03..e815625d 100644 --- a/optuna_dashboard/_cli.py +++ b/optuna_dashboard/_cli.py @@ -81,9 +81,7 @@ def main() -> None: parser.add_argument( "--port", help="port number (default: %(default)s)", type=int, default=8080 ) - parser.add_argument( - "--host", help="hostname (default: %(default)s)", default="127.0.0.1" - ) + parser.add_argument("--host", help="hostname (default: %(default)s)", default="127.0.0.1") parser.add_argument( "--server", help="server (default: %(default)s)", diff --git a/optuna_dashboard/_note.py b/optuna_dashboard/_note.py index be263d63..15f5f3ff 100644 --- a/optuna_dashboard/_note.py +++ b/optuna_dashboard/_note.py @@ -31,9 +31,7 @@ def get_note_from_system_attrs(system_attrs: Dict[str, Any]) -> NoteType: } note_ver = int(system_attrs[NOTE_VER_KEY]) note_attrs: Dict[str, str] = { - key: value - for key, value in system_attrs.items() - if key.startswith(NOTE_STR_KEY_PREFIX) + key: value for key, value in system_attrs.items() if key.startswith(NOTE_STR_KEY_PREFIX) } return {"version": note_ver, "body": concat_body(note_attrs)} @@ -72,6 +70,4 @@ def split_body(note_str: str) -> Dict[str, str]: def concat_body(note_attrs: Dict[str, str]) -> str: - return "".join( - note_attrs[f"{NOTE_STR_KEY_PREFIX}{i}"] for i in range(len(note_attrs)) - ) + return "".join(note_attrs[f"{NOTE_STR_KEY_PREFIX}{i}"] for i in range(len(note_attrs))) diff --git a/optuna_dashboard/_serializer.py b/optuna_dashboard/_serializer.py index cb9dee49..dc754e82 100644 --- a/optuna_dashboard/_serializer.py +++ b/optuna_dashboard/_serializer.py @@ -80,9 +80,7 @@ def serialize_study_detail( if summary.datetime_start is not None: serialized["datetime_start"] = summary.datetime_start.isoformat() - serialized["trials"] = [ - serialize_frozen_trial(summary._study_id, trial) for trial in trials - ] + serialized["trials"] = [serialize_frozen_trial(summary._study_id, trial) for trial in trials] serialized["intersection_search_space"] = serialize_search_space(intersection) serialized["union_search_space"] = serialize_search_space(union) serialized["has_intermediate_values"] = has_intermediate_values @@ -96,9 +94,7 @@ def serialize_frozen_trial(study_id: int, trial: FrozenTrial) -> Dict[str, Any]: "study_id": study_id, "number": trial.number, "state": trial.state.name.capitalize(), - "params": [ - {"name": name, "value": str(value)} for name, value in trial.params.items() - ], + "params": [{"name": name, "value": str(value)} for name, value in trial.params.items()], "user_attrs": serialize_attrs(trial.user_attrs), "system_attrs": serialize_attrs(trial.system_attrs), } diff --git a/optuna_dashboard/_sql_profiler.py b/optuna_dashboard/_sql_profiler.py index b650fa60..071c056f 100644 --- a/optuna_dashboard/_sql_profiler.py +++ b/optuna_dashboard/_sql_profiler.py @@ -8,9 +8,8 @@ from typing import TYPE_CHECKING from bottle import Bottle from bottle import SimpleTemplate from optuna.storages import RDBStorage -from sqlalchemy import event - from optuna_dashboard._app import BottleView +from sqlalchemy import event if TYPE_CHECKING: diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..6e44b42c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,26 @@ +[tool.black] +line-length = 99 +target-version = ['py38'] +exclude = ''' +/( + \.eggs + | \.git + | \.mypy_cache + | \.tox + | \.venv + | _build + | build + | dist + | venv +)/ +''' + +[tool.isort] +profile = 'black' +src_paths = ['optuna', 'tests', 'docs', 'benchmarks'] +skip_glob = ['docs/source/conf.py', '**/alembic/versions/*.py', 'tutorial/**/*.py'] +line_length = 99 +lines_after_imports = 2 +force_single_line = 'True' +force_sort_within_sections = 'True' +order_by_type = 'False' diff --git a/python_tests/test_api.py b/python_tests/test_api.py index 2060dcee..ad981187 100644 --- a/python_tests/test_api.py +++ b/python_tests/test_api.py @@ -6,9 +6,8 @@ import optuna from optuna.storages import BaseStorage from optuna.study import StudySummary from optuna.version import __version__ as optuna_ver -from packaging import version - from optuna_dashboard._app import create_app +from packaging import version from .wsgi_client import send_request diff --git a/python_tests/test_cached_extra_study_property.py b/python_tests/test_cached_extra_study_property.py index f1aa92fe..08280f59 100644 --- a/python_tests/test_cached_extra_study_property.py +++ b/python_tests/test_cached_extra_study_property.py @@ -9,7 +9,6 @@ from optuna.distributions import BaseDistribution from optuna.distributions import UniformDistribution from optuna.exceptions import ExperimentalWarning from optuna.trial import TrialState - from optuna_dashboard._cached_extra_study_property import _CachedExtraStudyProperty diff --git a/setup.cfg b/setup.cfg index ec180ce6..93bf81d1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -56,14 +56,3 @@ exclude = venv,build,.tox [mypy] ignore_missing_imports = True disallow_untyped_defs = True - -[isort] -profile = black -src_paths = - optuna_dashboard - python_tests -line_length = 99 -lines_after_imports = 2 -force_single_line = True -force_sort_within_sections = True -order_by_type = False diff --git a/visual_regression_test.py b/visual_regression_test.py index ae62f013..13ce514f 100644 --- a/visual_regression_test.py +++ b/visual_regression_test.py @@ -10,41 +10,28 @@ from wsgiref.simple_server import make_server import optuna from optuna.version import __version__ as optuna_ver +from optuna_dashboard import wsgi from packaging import version from pyppeteer import launch from pyppeteer.page import Page -from optuna_dashboard import wsgi - parser = argparse.ArgumentParser() -parser.add_argument( - "--port", help="port number (default: %(default)s)", type=int, default=8081 -) -parser.add_argument( - "--host", help="hostname (default: %(default)s)", default="127.0.0.1" -) +parser.add_argument("--port", help="port number (default: %(default)s)", type=int, default=8081) +parser.add_argument("--host", help="hostname (default: %(default)s)", default="127.0.0.1") parser.add_argument( "--sleep", help="sleep seconds on each page open (default: %(default)s)", type=int, default=5, ) -parser.add_argument( - "--output-dir", help="output directory (default: %(default)s)", default="tmp" -) -parser.add_argument( - "--width", help="window width (default: %(default)s)", type=int, default=1000 -) +parser.add_argument("--output-dir", help="output directory (default: %(default)s)", default="tmp") +parser.add_argument("--width", help="window width (default: %(default)s)", type=int, default=1000) parser.add_argument( "--height", help="window height (default: %(default)s)", type=int, default=3000 ) -parser.add_argument( - "--storage", help="storage url (default: %(default)s)", default=None -) -parser.add_argument( - "--skip-screenshot", help="skip to take screenshot", action="store_true" -) +parser.add_argument("--storage", help="storage url (default: %(default)s)", default=None) +parser.add_argument("--skip-screenshot", help="skip to take screenshot", action="store_true") args = parser.parse_args() @@ -62,9 +49,7 @@ def create_dummy_storage() -> optuna.storages.InMemoryStorage: study.optimize(objective_single, n_trials=50) # Single-objective study with 1 parameter - study = optuna.create_study( - study_name="single-1-param", storage=storage, direction="maximize" - ) + study = optuna.create_study(study_name="single-1-param", storage=storage, direction="maximize") def objective_single_with_1param(trial: optuna.Trial) -> float: x1 = trial.suggest_float("x1", 0, 10) @@ -73,9 +58,7 @@ def create_dummy_storage() -> optuna.storages.InMemoryStorage: study.optimize(objective_single_with_1param, n_trials=50) # Single-objective study with dynamic search space - study = optuna.create_study( - study_name="single-dynamic", storage=storage, direction="maximize" - ) + study = optuna.create_study(study_name="single-dynamic", storage=storage, direction="maximize") def objective_single_dynamic(trial: optuna.Trial) -> float: category = trial.suggest_categorical("category", ["foo", "bar"]) @@ -139,9 +122,7 @@ def create_dummy_storage() -> optuna.storages.InMemoryStorage: study.optimize(objective_multi_dynamic, n_trials=50) # Pruning with no intermediate values - study = optuna.create_study( - study_name="single-pruned-without-report", storage=storage - ) + study = optuna.create_study(study_name="single-pruned-without-report", storage=storage) def objective_prune_without_report(trial: optuna.Trial) -> float: x = trial.suggest_float("x", -15, 30)