Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Black 2024 to codebase #5252

Merged
merged 9 commits into from
Feb 22, 2024
1 change: 1 addition & 0 deletions optuna/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Optuna CLI module.
If you want to add a new command, you also need to update the constant `_COMMANDS`
"""

from argparse import ArgumentParser
from argparse import Namespace
import datetime
Expand Down
20 changes: 7 additions & 13 deletions optuna/multi_objective/trial.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,24 @@ def suggest_int(
return self._trial.suggest_int(name, low, high, step=step, log=log)

@overload
def suggest_categorical(self, name: str, choices: Sequence[None]) -> None:
...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using pass or raise NotImplemented instead of ...?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no strong opinion on this topic but it is reasonable to ignore E704 in this case as mentioned in psf/black#3887 and PEP8. I think it is possible to put pass since NotImplementedError raises when function with @overload is actually called no matter what we write in the body .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, changing the implementation is out of scope of this PR, since this PR aims just to introduce black 2024.

def suggest_categorical(self, name: str, choices: Sequence[None]) -> None: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[bool]) -> bool:
...
def suggest_categorical(self, name: str, choices: Sequence[bool]) -> bool: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[int]) -> int:
...
def suggest_categorical(self, name: str, choices: Sequence[int]) -> int: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[float]) -> float:
...
def suggest_categorical(self, name: str, choices: Sequence[float]) -> float: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[str]) -> str:
...
def suggest_categorical(self, name: str, choices: Sequence[str]) -> str: ... # noqa: E704

@overload
def suggest_categorical(
def suggest_categorical( # noqa: E704
self, name: str, choices: Sequence[CategoricalChoiceType]
) -> CategoricalChoiceType:
...
) -> CategoricalChoiceType: ...

def suggest_categorical(
self, name: str, choices: Sequence[CategoricalChoiceType]
Expand Down
16 changes: 9 additions & 7 deletions optuna/samplers/_brute_force.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,15 @@ def after_trial(
self._populate_tree(
tree,
(
t
if t.number != trial.number
else create_trial(
state=state, # Set current trial as complete.
values=values,
params=trial.params,
distributions=trial.distributions,
(
t
if t.number != trial.number
else create_trial(
state=state, # Set current trial as complete.
values=values,
params=trial.params,
distributions=trial.distributions,
)
)
for t in trials
),
Expand Down
1 change: 0 additions & 1 deletion optuna/samplers/_lazy_random_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class LazyRandomState:

"""Lazy Random State class.


Expand Down
22 changes: 10 additions & 12 deletions optuna/samplers/_nsgaiii/_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@ def __init__(
constraints_func: Callable[[FrozenTrial], Sequence[float]] | None = None,
reference_points: np.ndarray | None = None,
dividing_parameter: int = 3,
elite_population_selection_strategy: Callable[
[Study, list[FrozenTrial]], list[FrozenTrial]
]
| None = None,
child_generation_strategy: Callable[
[Study, dict[str, BaseDistribution], list[FrozenTrial]], dict[str, Any]
]
| None = None,
after_trial_strategy: Callable[
[Study, FrozenTrial, TrialState, Sequence[float] | None], None
]
| None = None,
elite_population_selection_strategy: (
Callable[[Study, list[FrozenTrial]], list[FrozenTrial]] | None
) = None,
child_generation_strategy: (
Callable[[Study, dict[str, BaseDistribution], list[FrozenTrial]], dict[str, Any]]
| None
) = None,
after_trial_strategy: (
Callable[[Study, FrozenTrial, TrialState, Sequence[float] | None], None] | None
) = None,
) -> None:
# TODO(ohta): Reconsider the default value of each parameter.

Expand Down
30 changes: 18 additions & 12 deletions optuna/samplers/_tpe/parzen_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,35 @@ def _is_log(dist: BaseDistribution) -> bool:
def _transform(self, samples_dict: Dict[str, np.ndarray]) -> np.ndarray:
return np.array(
[
np.log(samples_dict[param])
if self._is_log(self._search_space[param])
else samples_dict[param]
(
np.log(samples_dict[param])
if self._is_log(self._search_space[param])
else samples_dict[param]
)
for param in self._search_space
]
).T

def _untransform(self, samples_array: np.ndarray) -> Dict[str, np.ndarray]:
res = {
param: np.exp(samples_array[:, i])
if self._is_log(self._search_space[param])
else samples_array[:, i]
param: (
np.exp(samples_array[:, i])
if self._is_log(self._search_space[param])
else samples_array[:, i]
)
for i, param in enumerate(self._search_space)
}
# TODO(contramundum53): Remove this line after fixing log-Int hack.
return {
param: np.clip(
dist.low + np.round((res[param] - dist.low) / dist.step) * dist.step,
dist.low,
dist.high,
param: (
np.clip(
dist.low + np.round((res[param] - dist.low) / dist.step) * dist.step,
dist.low,
dist.high,
)
if isinstance(dist, IntDistribution)
else res[param]
)
if isinstance(dist, IntDistribution)
else res[param]
for (param, dist) in self._search_space.items()
}

Expand Down
22 changes: 10 additions & 12 deletions optuna/samplers/nsgaii/_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,16 @@ def __init__(
swapping_prob: float = 0.5,
seed: int | None = None,
constraints_func: Callable[[FrozenTrial], Sequence[float]] | None = None,
elite_population_selection_strategy: Callable[
[Study, list[FrozenTrial]], list[FrozenTrial]
]
| None = None,
child_generation_strategy: Callable[
[Study, dict[str, BaseDistribution], list[FrozenTrial]], dict[str, Any]
]
| None = None,
after_trial_strategy: Callable[
[Study, FrozenTrial, TrialState, Sequence[float] | None], None
]
| None = None,
elite_population_selection_strategy: (
Callable[[Study, list[FrozenTrial]], list[FrozenTrial]] | None
) = None,
child_generation_strategy: (
Callable[[Study, dict[str, BaseDistribution], list[FrozenTrial]], dict[str, Any]]
| None
) = None,
after_trial_strategy: (
Callable[[Study, FrozenTrial, TrialState, Sequence[float] | None], None] | None
) = None,
) -> None:
# TODO(ohta): Reconsider the default value of each parameter.

Expand Down
3 changes: 1 addition & 2 deletions optuna/storages/_journal/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ class JournalStorage(BaseStorage):
import optuna


def objective(trial):
...
def objective(trial): ...


storage = optuna.storages.JournalStorage(
Expand Down
1 change: 1 addition & 0 deletions optuna/storages/_rdb/alembic/versions/v0.9.0.a.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2019-03-12 12:30:31.178819

"""

from alembic import op
import sqlalchemy as sa

Expand Down
1 change: 1 addition & 0 deletions optuna/storages/_rdb/alembic/versions/v1.2.0.a.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2020-02-05 15:17:41.458947

"""

from alembic import op
import sqlalchemy as sa

Expand Down
1 change: 1 addition & 0 deletions optuna/storages/_rdb/alembic/versions/v1.3.0.a.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2020-02-14 16:23:04.800808

"""

import json

from alembic import op
Expand Down
1 change: 1 addition & 0 deletions optuna/storages/_rdb/alembic/versions/v2.4.0.a.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2020-11-17 02:16:16.536171

"""

from alembic import op
import sqlalchemy as sa
from typing import Any
Expand Down
1 change: 1 addition & 0 deletions optuna/storages/_rdb/alembic/versions/v2.6.0.a_.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2021-03-01 11:30:32.214196

"""

from alembic import op
import sqlalchemy as sa

Expand Down
1 change: 1 addition & 0 deletions optuna/storages/_rdb/alembic/versions/v3.0.0.a.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2021-11-21 23:48:42.424430

"""

from typing import Any
from typing import List

Expand Down
1 change: 1 addition & 0 deletions optuna/storages/_rdb/alembic/versions/v3.0.0.b.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2022-04-27 16:31:42.012666

"""

import enum

from alembic import op
Expand Down
1 change: 1 addition & 0 deletions optuna/storages/_rdb/alembic/versions/v3.0.0.c.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2022-05-16 17:17:28.810792

"""

import enum

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions optuna/storages/_rdb/alembic/versions/v3.0.0.d.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2022-06-02 09:57:22.818798

"""

import enum

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions optuna/storages/_rdb/alembic/versions/v3.2.0.a_.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2023-02-25 13:21:00.730272

"""

from alembic import op


Expand Down
21 changes: 7 additions & 14 deletions optuna/trial/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,28 @@ def suggest_int(

@overload
@abc.abstractmethod
def suggest_categorical(self, name: str, choices: Sequence[None]) -> None:
...

def suggest_categorical(self, name: str, choices: Sequence[None]) -> None: ... # noqa: E704
@overload
@abc.abstractmethod
def suggest_categorical(self, name: str, choices: Sequence[bool]) -> bool:
...
def suggest_categorical(self, name: str, choices: Sequence[bool]) -> bool: ... # noqa: E704

@overload
@abc.abstractmethod
def suggest_categorical(self, name: str, choices: Sequence[int]) -> int:
...
def suggest_categorical(self, name: str, choices: Sequence[int]) -> int: ... # noqa: E704

@overload
@abc.abstractmethod
def suggest_categorical(self, name: str, choices: Sequence[float]) -> float:
...
def suggest_categorical(self, name: str, choices: Sequence[float]) -> float: ... # noqa: E704

@overload
@abc.abstractmethod
def suggest_categorical(self, name: str, choices: Sequence[str]) -> str:
...
def suggest_categorical(self, name: str, choices: Sequence[str]) -> str: ... # noqa: E704

@overload
@abc.abstractmethod
def suggest_categorical(
def suggest_categorical( # noqa: E704
self, name: str, choices: Sequence[CategoricalChoiceType]
) -> CategoricalChoiceType:
...
) -> CategoricalChoiceType: ...

@abc.abstractmethod
def suggest_categorical(
Expand Down
20 changes: 7 additions & 13 deletions optuna/trial/_fixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,24 @@ def suggest_int(
return int(self._suggest(name, IntDistribution(low, high, log=log, step=step)))

@overload
def suggest_categorical(self, name: str, choices: Sequence[None]) -> None:
...
def suggest_categorical(self, name: str, choices: Sequence[None]) -> None: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[bool]) -> bool:
...
def suggest_categorical(self, name: str, choices: Sequence[bool]) -> bool: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[int]) -> int:
...
def suggest_categorical(self, name: str, choices: Sequence[int]) -> int: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[float]) -> float:
...
def suggest_categorical(self, name: str, choices: Sequence[float]) -> float: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[str]) -> str:
...
def suggest_categorical(self, name: str, choices: Sequence[str]) -> str: ... # noqa: E704

@overload
def suggest_categorical(
def suggest_categorical( # noqa: E704
self, name: str, choices: Sequence[CategoricalChoiceType]
) -> CategoricalChoiceType:
...
) -> CategoricalChoiceType: ...

def suggest_categorical(
self, name: str, choices: Sequence[CategoricalChoiceType]
Expand Down
20 changes: 7 additions & 13 deletions optuna/trial/_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,30 +235,24 @@ def suggest_int(
return int(self._suggest(name, IntDistribution(low, high, log=log, step=step)))

@overload
def suggest_categorical(self, name: str, choices: Sequence[None]) -> None:
...
def suggest_categorical(self, name: str, choices: Sequence[None]) -> None: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[bool]) -> bool:
...
def suggest_categorical(self, name: str, choices: Sequence[bool]) -> bool: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[int]) -> int:
...
def suggest_categorical(self, name: str, choices: Sequence[int]) -> int: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[float]) -> float:
...
def suggest_categorical(self, name: str, choices: Sequence[float]) -> float: ... # noqa: E704

@overload
def suggest_categorical(self, name: str, choices: Sequence[str]) -> str:
...
def suggest_categorical(self, name: str, choices: Sequence[str]) -> str: ... # noqa: E704

@overload
def suggest_categorical(
def suggest_categorical( # noqa: E704
self, name: str, choices: Sequence[CategoricalChoiceType]
) -> CategoricalChoiceType:
...
) -> CategoricalChoiceType: ...

def suggest_categorical(
self, name: str, choices: Sequence[CategoricalChoiceType]
Expand Down