Skip to content

Commit

Permalink
rm neps argument searcher_path
Browse files Browse the repository at this point in the history
  • Loading branch information
danrgll committed Jun 7, 2024
1 parent a2f259b commit f8f36ae
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 40 deletions.
28 changes: 17 additions & 11 deletions neps/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
from neps.utils.common import get_searcher_data, get_value
from neps.utils.data_loading import _get_loss

VALID_SEARCHER = [
"default",
"bayesian_optimization",
"random_search",
"hyperband",
"priorband",
"mobster",
"asha",
"regularized_evolution",
]


def _post_evaluation_hook_function(
_loss_value_on_error: None | float, _ignore_errors: bool
Expand Down Expand Up @@ -132,9 +143,8 @@ def run(
"asha",
"regularized_evolution",
]
| BaseOptimizer
| BaseOptimizer | Path
) = "default",
searcher_path: Path | str | None = None,
**searcher_kwargs,
) -> None:
"""Run a neural pipeline search.
Expand Down Expand Up @@ -176,9 +186,8 @@ def run(
cost_value_on_error: Setting this and loss_value_on_error to any float will
supress any error and will use given cost value instead. default: None
pre_load_hooks: List of functions that will be called before load_results().
searcher: Which optimizer to use. This is usually only needed by neps developers.
searcher_path: The path to the user created searcher. None when the user
is using NePS designed searchers.
searcher: Which optimizer to use. Can be a string identifier, an
instance of BaseOptimizer, or a Path to a custom optimizer.
**searcher_kwargs: Will be passed to the searcher. This is usually only needed by
neps develolpers.
Expand Down Expand Up @@ -250,7 +259,6 @@ def run(
cost_value_on_error)
pre_load_hooks = optim_settings.get("pre_load_hooks", pre_load_hooks)
searcher = optim_settings.get("searcher", searcher)
searcher_path = optim_settings.get("searcher_path", searcher_path)
for key, value in optim_settings.get("searcher_kwargs", searcher_kwargs).items():
searcher_kwargs[key] = value

Expand Down Expand Up @@ -310,7 +318,6 @@ def run(
cost_value_on_error=cost_value_on_error,
logger=logger,
searcher=searcher,
searcher_path=searcher_path,
**searcher_kwargs,
)

Expand Down Expand Up @@ -380,7 +387,6 @@ def _run_args(
]
| BaseOptimizer
) = "default",
searcher_path: Path | str | None = None,
**searcher_kwargs,
) -> tuple[BaseOptimizer, dict]:
try:
Expand Down Expand Up @@ -412,11 +418,11 @@ def _run_args(
message = f"The pipeline_space has invalid type: {type(pipeline_space)}"
raise TypeError(message) from e

if isinstance(searcher, str) and searcher_path is not None:
if isinstance(searcher, (str, Path)) and searcher not in VALID_SEARCHER:
# The users has their own custom searcher.
logging.info("Preparing to run user created searcher")

config = get_searcher_data(searcher, searcher_path)
config, searcher = get_searcher_data(searcher, loading_custom_searcher=True)
searcher_info["searcher_selection"] = "user-yaml"
searcher_info["neps_decision_tree"] = False
else:
Expand All @@ -436,7 +442,7 @@ def _run_args(
searcher_info["neps_decision_tree"] = False
searcher_info["searcher_selection"] = "neps-default"
# Fetching the searcher data, throws an error when the searcher is not found
config = get_searcher_data(searcher)
config, searcher = get_searcher_data(searcher)

if "algorithm" in config:
searcher_alg = config.pop("algorithm")
Expand Down
19 changes: 10 additions & 9 deletions neps/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,19 @@ def get_initial_directory(pipeline_directory: Path | str | None = None) -> Path:


def get_searcher_data(
searcher: str,
searcher_path: Path | str | None = None,
) -> dict[str, Any]:
searcher: str | Path, loading_custom_searcher: bool = False
) -> (dict[str, Any], str):
"""Returns the data from the YAML file associated with the specified searcher.
Args:
searcher: The name of the searcher.
searcher_path: The path to the directory where the searcher defined YAML file
is located.
loading_custom_searcher: Flag if searcher contains a custom yaml
Returns:
The content of the YAML file.
The content of the YAML file and searcher name.
"""
if searcher_path is not None:
user_yaml_path = Path(searcher_path, searcher).with_suffix(".yaml")
if loading_custom_searcher:
user_yaml_path = Path(searcher).with_suffix(".yaml")

if not user_yaml_path.exists():
raise FileNotFoundError(
Expand All @@ -218,6 +216,9 @@ def get_searcher_data(
with user_yaml_path.open("r") as file:
data = yaml.safe_load(file)

file_name = user_yaml_path.stem
searcher = data.get("name", file_name)

else:
# TODO(eddiebergman): This is a bad idea as it relies on folder structure to be
# correct, we should either have a dedicated resource folder or at least have
Expand Down Expand Up @@ -246,7 +247,7 @@ def get_searcher_data(
with resource_path.open() as file:
data = yaml.safe_load(file)

return data # type: ignore
return data, searcher # type: ignore


# TODO(eddiebergman): This seems like a bad function name, I guess this is used for a
Expand Down
3 changes: 0 additions & 3 deletions neps/utils/run_args_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
COST_VALUE_ON_ERROR = "cost_value_on_error"
IGNORE_ERROR = "ignore_errors"
SEARCHER = "searcher"
SEARCHER_PATH = "searcher_path"
PRE_LOAD_HOOKS = "pre_load_hooks"
SEARCHER_KWARGS = "searcher_kwargs"
MAX_EVALUATIONS_PER_RUN = "max_evaluations_per_run"
Expand Down Expand Up @@ -73,7 +72,6 @@ def get_run_args_from_yaml(path: str) -> dict:
LOSS_VALUE_ON_ERROR,
COST_VALUE_ON_ERROR,
IGNORE_ERROR,
SEARCHER_PATH,
]

# Flatten the YAML file's structure to separate flat parameters (flat_config) and
Expand Down Expand Up @@ -412,7 +410,6 @@ def check_run_args(settings: Dict) -> None:
LOSS_VALUE_ON_ERROR: float,
COST_VALUE_ON_ERROR: float,
IGNORE_ERROR: bool,
SEARCHER_PATH: str,
SEARCHER_KWARGS: dict,
}
for param, value in settings.items():
Expand Down
5 changes: 2 additions & 3 deletions tests/test_neps_api/testing_scripts/user_yaml_neps.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ def run_pipeline(val1, val2):
# Testing using created yaml with api
script_directory = os.path.dirname(os.path.abspath(__file__))
parent_directory = os.path.join(script_directory, os.pardir)
searcher_path = os.path.join(parent_directory, "testing_yaml")
searcher_path = os.path.join(parent_directory, "testing_yaml/optimizer_test")
neps.run(
run_pipeline=run_pipeline,
pipeline_space=pipeline_space,
root_directory="user_yaml_bo",
max_evaluations_total=1,
searcher="optimizer_test",
searcher_path=searcher_path,
searcher=searcher_path,
initial_design_size=5,
)
1 change: 0 additions & 1 deletion tests/test_yaml_run_args/run_args_empty.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ parallelization_setup:

search:
searcher:
searcher_path:
searcher_kwargs:
initial_design_size:
surrogate_model:
Expand Down
1 change: 0 additions & 1 deletion tests/test_yaml_run_args/run_args_full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ error_handling:

search:
searcher: "bayesian_optimization"
searcher_path: "/path/to/model"
searcher_kwargs:
initial_design_size: 5
surrogate_model: gp
Expand Down
1 change: 0 additions & 1 deletion tests/test_yaml_run_args/run_args_full_same_level.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ loss_value_on_error: 2.4
cost_value_on_error: 2.1
ignore_errors: False
searcher: "bayesian_optimization"
searcher_path: "/path/to/searcher"
searcher_kwargs:
initial_design_size: 5
surrogate_model: gp
Expand Down
1 change: 0 additions & 1 deletion tests/test_yaml_run_args/run_args_invalid_key.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ error_handling:

search:
searcher: "bayesian_optimization"
searcher_path: "/path/to/model"
searcher_kwargs:
initial_design_size: 5
surrogate_model: gp
Expand Down
1 change: 0 additions & 1 deletion tests/test_yaml_run_args/run_args_invalid_type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ error_handling:

search:
searcher: "bayesian_optimization"
searcher_path:
searcher_kwargs:
initial_design_size: 5
surrogate_model: gp
Expand Down
1 change: 0 additions & 1 deletion tests/test_yaml_run_args/run_args_key_missing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ loss_value_on_error: 2.4
cost_value_on_error: 2.1
ignore_errors: False
searcher: "bayesian_optimization"
searcher_path: "/path/to/searcher"
searcher_kwargs:
initial_design_size: 5
surrogate_model: gp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ ignore_errors: False
searcher: # Optional Loading
path: "neps/optimizers/bayesian_optimization/optimizer.py"
name: BayesianOptimization
searcher_path: "/path/to/searcher"
searcher_kwargs:
initial_design_size: 5
surrogate_model: gp
Expand Down
1 change: 0 additions & 1 deletion tests/test_yaml_run_args/run_args_partial.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ parallelization_setup:

search:
searcher: "bayesian_optimization"
searcher_path:
searcher_kwargs:
initial_design_size: 5
surrogate_model: gp
Expand Down
1 change: 0 additions & 1 deletion tests/test_yaml_run_args/run_args_partial_same_level.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ continue_until_max_evaluation_completed: True
loss_value_on_error: None
ignore_errors: True
searcher:
searcher_path:
pre_load_hooks: None
1 change: 0 additions & 1 deletion tests/test_yaml_run_args/run_args_wrong_name.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ error_handling:

search:
searcher: "bayesian_optimization"
searcher_path: "/path/to/model"
searcher_kwargs:
initial_design_size: 5
surrogate_model: gp
Expand Down
1 change: 0 additions & 1 deletion tests/test_yaml_run_args/run_args_wrong_path.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ error_handling:

search:
searcher: "bayesian_optimization"
searcher_path: "/path/to/model"
searcher_kwargs:
initial_design_size: 5
surrogate_model: gp
Expand Down
3 changes: 0 additions & 3 deletions tests/test_yaml_run_args/test_yaml_run_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def are_functions_equivalent(f1: Union[Callable, List[Callable]],
"cost_value_on_error": 3.7,
"ignore_errors": True,
"searcher": "bayesian_optimization",
"searcher_path": "/path/to/model",
"searcher_kwargs": {"initial_design_size": 5, "surrogate_model": "gp"},
"pre_load_hooks": [hook1, hook2],
},
Expand All @@ -135,7 +134,6 @@ def are_functions_equivalent(f1: Union[Callable, List[Callable]],
"cost_value_on_error": 2.1,
"ignore_errors": False,
"searcher": "bayesian_optimization",
"searcher_path": "/path/to/searcher",
"searcher_kwargs": {"initial_design_size": 5, "surrogate_model": "gp"},
"pre_load_hooks": [hook1],
},
Expand Down Expand Up @@ -181,7 +179,6 @@ def are_functions_equivalent(f1: Union[Callable, List[Callable]],
"cost_value_on_error": 2.1,
"ignore_errors": False,
"searcher": BayesianOptimization,
"searcher_path": "/path/to/searcher",
"searcher_kwargs": {
"initial_design_size": 5,
"surrogate_model": "gp"
Expand Down

0 comments on commit f8f36ae

Please sign in to comment.