Skip to content

Commit

Permalink
Added whoami function and a test case for searcher as BaseOptimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekAbouChakra committed Nov 14, 2023
1 parent 375bee8 commit 053f66a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ jobs:

- name: Run pytest
timeout-minutes: 15
run: poetry run pytest -m "all_examples or metahyper"
run: poetry run pytest -m "all_examples or metahyper or yaml_api"
10 changes: 6 additions & 4 deletions src/neps/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,12 @@ def run(

if isinstance(searcher, BaseOptimizer):
searcher_instance = searcher
searcher_alg = "custom"
searcher_name = "custom"
searcher_alg = searcher.whoami()
user_defined_searcher = True
else:
(
(
searcher_name,
searcher_instance,
searcher_alg,
searcher_config,
Expand Down Expand Up @@ -238,7 +240,7 @@ def run(
# Used to create the yaml holding information about the searcher.
# Also important for testing and debugging the api.
searcher_info = {
"searcher_name": str(searcher),
"searcher_name": searcher_name,
"searcher_alg": searcher_alg,
"user_defined_searcher": user_defined_searcher,
"searcher_args_user_modified": False,
Expand Down Expand Up @@ -430,4 +432,4 @@ def _run_args(
**searcher_config,
)

return searcher_instance, searcher_alg, searcher_config, searcher_info, user_defined_searcher
return searcher, searcher_instance, searcher_alg, searcher_config, searcher_info, user_defined_searcher
3 changes: 3 additions & 0 deletions src/neps/optimizers/base_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ def get_learning_curve(self, result: str | dict | float) -> float | Any:
learning_curve_on_error=self.learning_curve_on_error,
ignore_errors=self.ignore_errors,
)

def whoami(self):
return type(self).__name__
15 changes: 15 additions & 0 deletions tests/test_neps_api/examples_test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging

import neps
from neps.optimizers.bayesian_optimization.optimizer import BayesianOptimization
from neps.search_spaces.search_space import SearchSpace

pipeline_space_fidelity_priors = dict(
val1=neps.FloatParameter(lower=-10, upper=10, default=1),
Expand Down Expand Up @@ -80,3 +82,16 @@ def run_pipeline(val1, val2):
max_evaluations_total=1,
eta=2,
)

# Testing neps when the user creates their own custom searcher
search_space = SearchSpace(**pipeline_space_fidelity)
my_custom_searcher = BayesianOptimization(
pipeline_space=search_space, initial_design_size=5
)
neps.run(
run_pipeline=run_pipeline,
pipeline_space=pipeline_space_not_fidelity,
root_directory="bo_custom_created",
max_evaluations_total=1,
searcher=my_custom_searcher,
)
6 changes: 6 additions & 0 deletions tests/test_neps_api/test_yaml_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def no_logs_gte_error(caplog):
"user_defined_searcher": False,
"searcher_args_user_modified": False,
},
"bo_custom_created": {
"searcher_name": "custom",
"searcher_alg": "BayesianOptimization",
"user_defined_searcher": True,
"searcher_args_user_modified": False,
},
}


Expand Down

0 comments on commit 053f66a

Please sign in to comment.