Skip to content

Commit

Permalink
fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEimer committed Sep 30, 2024
1 parent 55500dd commit a802df0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
52 changes: 25 additions & 27 deletions hydra_plugins/hyper_pbt/bg_pbt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def get_start_point(cs: CS.ConfigurationSpace, x_center, frozen_dims: list[int]
# print(param_name)
if np.isnan(new_config_array[i]) or (frozen_dims is not None and i in frozen_dims):
continue
param_name = cs.get_hyperparameter_by_idx(i)
if type(cs[param_name]) == CSH.CategoricalHyperparameter:
param_name = cs.at(i)
if isinstance(cs[param_name], CSH.CategoricalHyperparameter):
new_config_array[i] = rng.choice(range(len(cs[param_name].choices)))
elif (
type(cs[param_name]) in [CSH.UniformIntegerHyperparameter, CSH.NormalIntegerHyperparameter]
Expand All @@ -140,9 +140,9 @@ def get_start_point(cs: CS.ConfigurationSpace, x_center, frozen_dims: list[int]
config = deactivate_inactive_hyperparameters(config, cs)

try:
cs.check_configuration(config)
config.check_valid_configuration()
except ValueError:
config = CS.Configuration(cs, config.get_dictionary())
config = CS.Configuration(cs, dict(config))
new_config_array = config.get_array()
if return_config:
return new_config_array, config
Expand Down Expand Up @@ -171,20 +171,20 @@ def construct_bounding_box(
if np.isnan(_dim) or i >= len(cs):
lb[i], ub[i] = 0.0, 1.0
else:
hp = cs[cs.get_hyperparameter_by_idx(i)]
if type(hp) == CSH.CategoricalHyperparameter:
hp = cs[cs.at(i)]
if isinstance(hp, CSH.CategoricalHyperparameter):
lb[i], ub[i] = 0, len(hp.choices)
else:
lb[i] = np.clip(_dim - weights[i] * tr_length / 2.0, 0.0, 1.0)
ub[i] = np.clip(_dim + weights[i] * tr_length / 2.0, 0.0, 1.0)
if type(hp) in [
CSH.UniformIntegerHyperparameter,
CSH.NormalIntegerHyperparameter,
CSH.NormalFloatHyperparameter,
CSH.UniformFloatHyperparameter,
if any[
isinstance(hp, CSH.UniformIntegerHyperparameter),
isinstance(hp, CSH.NormalIntegerHyperparameter),
isinstance(hp, CSH.NormalFloatHyperparameter),
isinstance(hp, CSH.UniformFloatHyperparameter),
]:
lb[i] = max(hp._inverse_transform(hp.lower), lb[i])
ub[i] = min(hp._inverse_transform(hp.upper), ub[i])
lb[i] = max(hp.to_vector(hp.lower), lb[i])
ub[i] = min(hp.to_vector(hp.upper), ub[i])
return lb, ub


Expand All @@ -200,20 +200,20 @@ def get_dim_info(cs: CS.ConfigurationSpace, x, return_indices=False):
# do not sample an inactivated hyperparameter (such a hyperparameter has nan value imputed)
if x[variable] != x[variable]:
continue
if type(cs[cs.get_hyperparameter_by_idx(variable)]) == CSH.CategoricalHyperparameter:
cat_dims.append(cs.get_hyperparameter_by_idx(variable))
if isinstance(cs[cs.at(variable)], CSH.CategoricalHyperparameter):
cat_dims.append(cs.at(variable))
cat_dims_idx.append(i)
elif type(cs[cs.get_hyperparameter_by_idx(variable)]) in [
elif type(cs[cs.at(variable)]) in [
CSH.UniformIntegerHyperparameter,
CSH.NormalIntegerHyperparameter,
]:
int_dims.append(cs.get_hyperparameter_by_idx(variable))
int_dims.append(cs.at(variable))
int_dims_idx.append(i)
elif type(cs[cs.get_hyperparameter_by_idx(variable)]) in [
elif type(cs[cs.at(variable)]) in [
CSH.UniformFloatHyperparameter,
CSH.NormalFloatHyperparameter,
]:
cont_dims.append(cs.get_hyperparameter_by_idx(variable))
cont_dims.append(cs.at(variable))
cont_dims_idx.append(i)
if return_indices:
return cat_dims_idx, cont_dims_idx, int_dims_idx
Expand All @@ -239,21 +239,21 @@ def sample_discrete_neighbour(cs: CS.ConfigurationSpace, x, frozen_dims: list[in
config = CS.Configuration(cs, vector=x.detach().numpy() if isinstance(x, torch.Tensor) else x)

try:
cs.check_configuration(config)
config.check_valid_configuration()
except ValueError:
# there seems to be a bug with ConfigSpace that raises error even when a config is valid
# Issue #196: https://github.com/automl/ConfigSpace/issues/196
# print(config)
config = CS.Configuration(cs, config.get_dictionary())
config = CS.Configuration(cs, dict(config))

# print(config)
config_pert = deepcopy(config)
rng = np.random.default_rng()
selected_dim = str(rng.choice(cat_dims + int_dims, 1)[0])
index_in_array = cs.get_idx_by_hyperparameter_name(selected_dim)
index_in_array = cs.index_of(selected_dim)
while config_pert[selected_dim] is None or (frozen_dims is not None and index_in_array in frozen_dims):
selected_dim = str(rng.choice(cat_dims + int_dims, 1)[0])
index_in_array = cs.get_idx_by_hyperparameter_name(selected_dim)
index_in_array = cs.index_of(selected_dim)

# if the selected dimension is categorical, change the value to another variable
if selected_dim in cat_dims:
Expand Down Expand Up @@ -630,12 +630,10 @@ def __init__(
# extract the dim indices of the continuous dimensions (incl. integers)
self.cont_dims = [
i
for i, dim in enumerate(self.cs.get_hyperparameters())
for i, dim in enumerate(self.cs.values())
if type(dim) in [CSH.UniformIntegerHyperparameter, CSH.UniformFloatHyperparameter]
]
self.cat_dims = [
i for i, dim in enumerate(self.cs.get_hyperparameters()) if type(dim) == CSH.CategoricalHyperparameter
]
self.cat_dims = [i for i, dim in enumerate(self.cs.values()) if isinstance(dim, CSH.CategoricalHyperparameter)]

# initialise the kernels
self.continuous_kern = ConditionalMatern(
Expand Down
2 changes: 1 addition & 1 deletion hydra_plugins/hyper_pbt/pb2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def exp3_get_cat(row, data, num_rounds, index):
sum_w = np.sum(weights)
weights = [w / sum_w for w in weights]

count += 1
count += 1 # noqa: SIM113

# now we select our arm!

Expand Down
2 changes: 1 addition & 1 deletion hydra_plugins/hypersweeper/search_space_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def search_space_to_config_space(search_space: str | DictConfig) -> Configuratio

jason_string = json.dumps(search_space, cls=JSONCfgEncoder)
cs = csjson.read(jason_string)
elif type(search_space) == ConfigurationSpace:
elif isinstance(search_space, ConfigurationSpace):
cs = search_space
else:
raise ValueError(f"search_space must be of type str or DictConfig. Got {type(search_space)}.")
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ lint.ignore = [
"N812",
"N817",
"S307",
"S603",
"S607"
]

exclude = [
Expand Down
22 changes: 16 additions & 6 deletions tests/test_maximize.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
#!/usr/bin/env python
"""Tests for `hypersweeper` package."""
from __future__ import annotations

import json
import shutil
import subprocess
import pandas as pd
import numpy as np
from pathlib import Path

import numpy as np
import pandas as pd


def test_non_max_incumbent():
subprocess.call(["python", "examples/branin.py", "--config-name=branin_rs", "-m"])
assert Path("./tmp/branin_rs").exists(), "Run directory not created"
runhistory = pd.read_csv("./tmp/branin_rs/runhistory.csv")
with open(Path("./tmp/branin_rs/incumbent.json")) as f:
last_line = f.readlines()[-1]
incumbent = json.loads(last_line)
assert np.round(incumbent["score"], decimals=3) == np.round(runhistory["performance"].min(), decimals=3), "Incumbent is not the minimum score in the runhistory"
assert np.round(incumbent["score"], decimals=3) == np.round(
runhistory["performance"].min(), decimals=3
), "Incumbent is not the minimum score in the runhistory"
shutil.rmtree("./tmp")


def test_max_incumbent():
subprocess.call(["python", "examples/branin.py", "--config-name=branin_rs", "-m", "+hydra.sweeper.sweeper_kwargs.maximize=True"])
subprocess.call(
["python", "examples/branin.py", "--config-name=branin_rs", "-m", "+hydra.sweeper.sweeper_kwargs.maximize=True"]
)
assert Path("./tmp/branin_rs").exists(), "Run directory not created"
runhistory = pd.read_csv("./tmp/branin_rs/runhistory.csv")
with open(Path("./tmp/branin_rs/incumbent.json")) as f:
last_line = f.readlines()[-1]
incumbent = json.loads(last_line)
print(incumbent["score"], runhistory["performance"].max(), runhistory.performance.min())
print(runhistory.values)
assert np.round(incumbent["score"], decimals=3) == np.round(runhistory["performance"].max(), decimals=3), "Incumbent is not the maximum score in the runhistory even though maximize is enabled"
shutil.rmtree("./tmp")
assert np.round(incumbent["score"], decimals=3) == np.round(
runhistory["performance"].max(), decimals=3
), "Incumbent is not the maximum score in the runhistory even though maximize is enabled"
shutil.rmtree("./tmp")

0 comments on commit a802df0

Please sign in to comment.