Skip to content

Commit

Permalink
Merge pull request #5252 from Alnusjaponica/update-black
Browse files Browse the repository at this point in the history
Apply Black 2024 to codebase
  • Loading branch information
nabenabe0928 committed Feb 22, 2024
2 parents cbf62c3 + 510e7f6 commit 7192d0f
Show file tree
Hide file tree
Showing 33 changed files with 104 additions and 121 deletions.
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
18 changes: 6 additions & 12 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:
...
def suggest_categorical(self, name: str, choices: Sequence[None]) -> None: ...

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

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

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

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

@overload
def suggest_categorical(
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
18 changes: 6 additions & 12 deletions optuna/trial/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,29 @@ 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: ...

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

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

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

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

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

@abc.abstractmethod
def suggest_categorical(
Expand Down
18 changes: 6 additions & 12 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: ...

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

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

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

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

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

def suggest_categorical(
self, name: str, choices: Sequence[CategoricalChoiceType]
Expand Down
18 changes: 6 additions & 12 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: ...

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

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

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

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

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

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

0 comments on commit 7192d0f

Please sign in to comment.