From a802df0235c308c5258e1e3681ccfba48d552365 Mon Sep 17 00:00:00 2001 From: Theresa Eimer Date: Mon, 30 Sep 2024 17:30:46 +0200 Subject: [PATCH] fix format --- hydra_plugins/hyper_pbt/bg_pbt_utils.py | 52 +++++++++---------- hydra_plugins/hyper_pbt/pb2_utils.py | 2 +- .../hypersweeper/search_space_encoding.py | 2 +- pyproject.toml | 2 + tests/test_maximize.py | 22 +++++--- 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/hydra_plugins/hyper_pbt/bg_pbt_utils.py b/hydra_plugins/hyper_pbt/bg_pbt_utils.py index 1a0f4f3..01c3edc 100644 --- a/hydra_plugins/hyper_pbt/bg_pbt_utils.py +++ b/hydra_plugins/hyper_pbt/bg_pbt_utils.py @@ -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] @@ -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 @@ -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 @@ -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 @@ -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: @@ -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( diff --git a/hydra_plugins/hyper_pbt/pb2_utils.py b/hydra_plugins/hyper_pbt/pb2_utils.py index fbda663..7bb2a42 100644 --- a/hydra_plugins/hyper_pbt/pb2_utils.py +++ b/hydra_plugins/hyper_pbt/pb2_utils.py @@ -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! diff --git a/hydra_plugins/hypersweeper/search_space_encoding.py b/hydra_plugins/hypersweeper/search_space_encoding.py index 34230ca..a78f123 100644 --- a/hydra_plugins/hypersweeper/search_space_encoding.py +++ b/hydra_plugins/hypersweeper/search_space_encoding.py @@ -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)}.") diff --git a/pyproject.toml b/pyproject.toml index 384f74b..8bf222b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -185,6 +185,8 @@ lint.ignore = [ "N812", "N817", "S307", + "S603", + "S607" ] exclude = [ diff --git a/tests/test_maximize.py b/tests/test_maximize.py index 885ad3e..7618167 100644 --- a/tests/test_maximize.py +++ b/tests/test_maximize.py @@ -1,13 +1,16 @@ #!/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" @@ -15,11 +18,16 @@ def test_non_max_incumbent(): 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: @@ -27,5 +35,7 @@ def test_max_incumbent(): 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") \ No newline at end of file + 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")