Skip to content

Commit

Permalink
Support black 23.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
not522 committed Feb 1, 2023
1 parent ead2e13 commit 03b08eb
Show file tree
Hide file tree
Showing 171 changed files with 6 additions and 1,209 deletions.
5 changes: 0 additions & 5 deletions benchmarks/bayesmark/optuna_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@


class OptunaOptimizer(AbstractOptimizer):

primary_import = "optuna"

def __init__(self, api_config: ApiConfig, **kwargs: Any) -> None:

super().__init__(api_config, **kwargs)

try:
Expand All @@ -74,7 +72,6 @@ def __init__(self, api_config: ApiConfig, **kwargs: Any) -> None:
self.current_trials: Dict[int, int] = dict()

def _suggest(self, trial: optuna.trial.Trial) -> Suggestion:

suggestions: Suggestion = dict()
for name, config in self.api_config.items():
low, high = config["range"]
Expand All @@ -100,7 +97,6 @@ def _suggest(self, trial: optuna.trial.Trial) -> Suggestion:
return suggestions

def suggest(self, n_suggestions: int) -> List[Suggestion]:

suggestions: List[Suggestion] = list()
for _ in range(n_suggestions):
trial = self.study.ask()
Expand All @@ -112,7 +108,6 @@ def suggest(self, n_suggestions: int) -> List[Suggestion]:
return suggestions

def observe(self, X: List[Suggestion], y: List[float]) -> None:

for params, objective_value in zip(X, y):
sid = hash(frozenset(params.items()))
trial = self.current_trials.pop(sid)
Expand Down
23 changes: 0 additions & 23 deletions benchmarks/bayesmark/report_bayesmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class BestValueMetric(BaseMetric):
precision = 6

def calculate(self, data: pd.DataFrame) -> List[float]:

return data.groupby("uuid").generalization.min().values


Expand All @@ -58,7 +57,6 @@ class AUCMetric(BaseMetric):
precision = 3

def calculate(self, data: pd.DataFrame) -> List[float]:

aucs: List[float] = list()
for _, grp in data.groupby("uuid"):
auc = np.sum(grp.generalization.cummin())
Expand All @@ -71,7 +69,6 @@ class ElapsedMetric(BaseMetric):
precision = 3

def calculate(self, data: pd.DataFrame) -> List[float]:

# Total time does not include evaluation of bayesmark
# objective function (no Optuna APIs are called there).
time_cols = ["suggest", "observe"]
Expand All @@ -80,22 +77,18 @@ def calculate(self, data: pd.DataFrame) -> List[float]:

class PartialReport:
def __init__(self, data: pd.DataFrame) -> None:

self._data = data

@property
def optimizers(self) -> List[str]:

return list(self._data.opt.unique())

@classmethod
def from_json(cls, path: str) -> "PartialReport":

data = pd.read_json(path)
return cls(data)

def summarize_solver(self, solver: str, metric: BaseMetric) -> Moments:

solver_data = self._data[self._data.opt == solver]
if solver_data.shape[0] == 0:
raise ValueError(f"{solver} not found in report.")
Expand All @@ -104,7 +97,6 @@ def summarize_solver(self, solver: str, metric: BaseMetric) -> Moments:
return np.mean(run_metrics).item(), np.var(run_metrics).item()

def sample_performance(self, metric: BaseMetric) -> Samples:

performance: Dict[str, List[float]] = {}
for solver, data in self._data.groupby("opt"):
run_metrics = metric.calculate(data)
Expand All @@ -119,26 +111,22 @@ def __init__(self, metrics: List[BaseMetric]) -> None:
self._borda: Optional[np.ndarray] = None

def __iter__(self) -> Generator[Tuple[str, int], None, None]:

yield from zip(self.solvers, self.borda)

@property
def solvers(self) -> List[str]:

if self._ranking is None:
raise ValueError("Call rank first.")
return self._ranking

@property
def borda(self) -> np.ndarray:

if self._borda is None:
raise ValueError("Call rank first.")
return self._borda

@staticmethod
def pick_alpha(report: PartialReport) -> float:

# https://github.com/optuna/kurobako/blob/788dd4cf618965a4a5158aa4e13607a5803dea9d/src/report.rs#L412-L424 # noqa E501
num_optimizers = len(report.optimizers)
candidates = [0.075, 0.05, 0.025, 0.01] * 4 / np.repeat([1, 10, 100, 1000], 4)
Expand All @@ -149,19 +137,16 @@ def pick_alpha(report: PartialReport) -> float:
return candidates[-1]

def _set_ranking(self, wins: Dict[str, int]) -> None:

sorted_wins = [k for k, _ in sorted(wins.items(), key=lambda x: x[1])]
self._ranking = sorted_wins[::-1]

def _set_borda(self, wins: Dict[str, int]) -> None:

sorted_wins = np.array(sorted(wins.values()))
num_wins, num_ties = np.unique(sorted_wins, return_counts=True)
points = np.searchsorted(sorted_wins, num_wins)
self._borda = np.repeat(points, num_ties)[::-1]

def rank(self, report: PartialReport) -> None:

# Implements Section 2.1.1
# https://proceedings.mlr.press/v64/dewancker_strategy_2016.pdf
wins: Dict[str, int] = defaultdict(int)
Expand Down Expand Up @@ -200,7 +185,6 @@ class Problem:

class BayesmarkReportBuilder:
def __init__(self) -> None:

self.solvers: Set[str] = set()
self.datasets: Set[str] = set()
self.models: Set[str] = set()
Expand All @@ -210,7 +194,6 @@ def __init__(self) -> None:
self.problems: List[Problem] = []

def set_precedence(self, metrics: List[BaseMetric]) -> None:

self.metric_precedence = " -> ".join([m.name for m in metrics])

def add_problem(
Expand All @@ -220,7 +203,6 @@ def add_problem(
ranking: DewanckerRanker,
metrics: List[BaseMetric],
) -> "BayesmarkReportBuilder":

solvers: List[Solver] = list()
positions = np.abs(ranking.borda - (max(ranking.borda) + 1))
for pos, solver in zip(positions, ranking.solvers):
Expand All @@ -238,33 +220,28 @@ def add_problem(
return self

def update_leaderboard(self, ranking: DewanckerRanker) -> "BayesmarkReportBuilder":

for solver, borda in ranking:
if borda == max(ranking.borda):
self.firsts[solver] += 1
self.borda[solver] += borda
return self

def add_dataset(self, dataset: str) -> "BayesmarkReportBuilder":

self.datasets.update(dataset)
return self

def add_model(self, model: str) -> "BayesmarkReportBuilder":

self.models.update(model)
return self

def assemble_report(self) -> str:

loader = FileSystemLoader(os.path.join("benchmarks", "bayesmark"))
env = Environment(loader=loader)
report_template = env.get_template("report_template.md")
return report_template.render(report=self)


def build_report() -> None:

# Order of this list sets metric precedence.
# https://proceedings.mlr.press/v64/dewancker_strategy_2016.pdf
metrics = [BestValueMetric(), AUCMetric()]
Expand Down
1 change: 0 additions & 1 deletion benchmarks/kurobako/mo_create_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def create_study(seed: int) -> optuna.Study:


if __name__ == "__main__":

factory = OptunaSolverFactory(create_study)
runner = solver.SolverRunner(factory)
runner.run()
11 changes: 0 additions & 11 deletions benchmarks/kurobako/problems/wfg/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def __init__(
shapes: List[shape_functions.BaseShapeFunction],
transformations: List[List[transformation_functions.BaseTransformations]],
) -> None:

assert all(S > 0)
assert all((A == 0) + (A == 1))
assert all(upper_bounds > 0)
Expand All @@ -31,7 +30,6 @@ def __init__(
self._transformations = transformations

def __call__(self, z: np.ndarray) -> np.ndarray:

S = self._S
A = self._A
unit_z = z / self._upper_bounds
Expand Down Expand Up @@ -69,7 +67,6 @@ class WFG1:
"""

def __init__(self, n_arguments: int, n_objectives: int, k: int):

assert k % (n_objectives - 1) == 0
assert k + 1 <= n_arguments

Expand Down Expand Up @@ -148,7 +145,6 @@ class WFG2:
"""

def __init__(self, n_arguments: int, n_objectives: int, k: int):

assert k % (n_objectives - 1) == 0
assert k + 1 <= n_arguments // 2
assert (n_arguments - k) % 2 == 0
Expand Down Expand Up @@ -229,7 +225,6 @@ class WFG3:
"""

def __init__(self, n_arguments: int, n_objectives: int, k: int):

assert k % (n_objectives - 1) == 0
assert k + 1 <= n_arguments // 2
assert (n_arguments - k) % 2 == 0
Expand Down Expand Up @@ -310,7 +305,6 @@ class WFG4:
"""

def __init__(self, n_arguments: int, n_objectives: int, k: int):

assert k % (n_objectives - 1) == 0
assert k + 1 <= n_arguments

Expand Down Expand Up @@ -377,7 +371,6 @@ class WFG5:
"""

def __init__(self, n_arguments: int, n_objectives: int, k: int):

assert k % (n_objectives - 1) == 0
assert k + 1 <= n_arguments

Expand Down Expand Up @@ -445,7 +438,6 @@ class WFG6:
"""

def __init__(self, n_arguments: int, n_objectives: int, k: int):

assert k % (n_objectives - 1) == 0
assert k + 1 <= n_arguments

Expand Down Expand Up @@ -512,7 +504,6 @@ class WFG7:
"""

def __init__(self, n_arguments: int, n_objectives: int, k: int):

assert k % (n_objectives - 1) == 0
assert k + 1 <= n_arguments

Expand Down Expand Up @@ -596,7 +587,6 @@ class WFG8:
"""

def __init__(self, n_arguments: int, n_objectives: int, k: int):

assert k % (n_objectives - 1) == 0
assert k + 1 <= n_arguments

Expand Down Expand Up @@ -679,7 +669,6 @@ class WFG9:
"""

def __init__(self, n_arguments: int, n_objectives: int, k: int):

assert k % (n_objectives - 1) == 0
assert k + 1 <= n_arguments

Expand Down
5 changes: 0 additions & 5 deletions benchmarks/kurobako/problems/wfg/shape_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

class BaseShapeFunction(object, metaclass=abc.ABCMeta):
def __init__(self, n_objectives: int) -> None:

self._n_objectives = n_objectives

def __call__(self, m: int, x: np.ndarray) -> float:
Expand All @@ -19,13 +18,11 @@ def _call(self, m: int, x: np.ndarray) -> float:

@property
def n_objectives(self) -> int:

return self._n_objectives


class LinearShapeFunction(BaseShapeFunction):
def _call(self, m: int, x: np.ndarray) -> float:

if m == 1:
return x[:-1].prod()

Expand All @@ -37,7 +34,6 @@ def _call(self, m: int, x: np.ndarray) -> float:

class ConvexShapeFunction(BaseShapeFunction):
def _call(self, m: int, x: np.ndarray) -> float:

if m == 1:
return (
1
Expand All @@ -56,7 +52,6 @@ def _call(self, m: int, x: np.ndarray) -> float:

class ConcaveShapeFunction(BaseShapeFunction):
def _call(self, m: int, x: np.ndarray) -> float:

if m == 1:
return np.sin(x * np.pi / 2.0)[:-1].prod()

Expand Down

0 comments on commit 03b08eb

Please sign in to comment.