Skip to content

Commit

Permalink
fix test + add run_args: constraint structure for yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
danrgll committed Apr 8, 2024
1 parent dd79361 commit 2d7eadb
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 85 deletions.
17 changes: 13 additions & 4 deletions neps/utils/run_args_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,32 @@ def get_run_args_from_yaml(path):

def config_loader(path):
"""
Loads and parses a YAML configuration file.
Loads and parses a YAML configuration file. Checks if the loaded YAML starts with
'run_args' key.
Args:
path (str): The filesystem path to the YAML file to be loaded.
Returns:
dict: The parsed YAML file as a dictionary.
dict: The parsed YAML file as a dictionary, specifically the content under
'run_args' key.
Raises:
ValueError: If the file cannot be parsed as YAML.
KeyError: If the 'run_args' key is not found at the top level of the YAML file.
"""
try:
with open(path) as file:
config = yaml.safe_load(file)
except yaml.YAMLError as e:
raise yaml.YAMLError(f"The file at {str(path)} is not a valid YAML file.") from e
return config
raise ValueError(f"The file at {path} is not a valid YAML file.") from e

# Check if 'run_args' is the top-level key in the loaded YAML
if 'run_args' not in config:
raise KeyError(f"The 'run_args' key is missing at the top level of the YAML "
f"file: {path}")

return config['run_args']


def extract_leaf_keys(d, special_keys=None):
Expand Down
49 changes: 25 additions & 24 deletions tests/test_yaml_run_args/run_args_full_same_level.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
run_pipeline:
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: "run_pipeline"
pipeline_space: "pipeline_space.yaml"
root_directory: "test_yaml"
max_evaluations_total: 20
max_cost_total: 4.2
overwrite_working_directory: True
post_run_summary: False
development_stage_id: 9
task_id: 2.0
max_evaluations_per_run: 5
continue_until_max_evaluation_completed: True
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
pre_load_hooks:
hook1:
run_args:
run_pipeline:
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: hook1
name: "run_pipeline"
pipeline_space: "pipeline_space.yaml"
root_directory: "test_yaml"
max_evaluations_total: 20
max_cost_total: 4.2
overwrite_working_directory: True
post_run_summary: False
development_stage_id: 9
task_id: 2.0
max_evaluations_per_run: 5
continue_until_max_evaluation_completed: True
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
pre_load_hooks:
hook1:
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: hook1
49 changes: 25 additions & 24 deletions tests/test_yaml_run_args/run_args_key_missing.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
run_pipeline:
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
# key name is missing
pipeline_space: "pipeline_space.yaml"
root_directory: "test_yaml"
max_evaluations_total: 20
max_cost_total: 4.2
overwrite_working_directory: True
post_run_summary: False
development_stage_id: 9
task_id: 2.0
max_evaluations_per_run: 5
continue_until_max_evaluation_completed: True
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
pre_load_hooks:
hook1:
run_args:
run_pipeline:
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: hook1
# key name is missing
pipeline_space: "pipeline_space.yaml"
root_directory: "test_yaml"
max_evaluations_total: 20
max_cost_total: 4.2
overwrite_working_directory: True
post_run_summary: False
development_stage_id: 9
task_id: 2.0
max_evaluations_per_run: 5
continue_until_max_evaluation_completed: True
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
pre_load_hooks:
hook1:
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: hook1
57 changes: 29 additions & 28 deletions tests/test_yaml_run_args/run_args_optional_loading_format.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
run_pipeline:
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: "run_pipeline"
pipeline_space: # Optional loading
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: "pipeline_space"
root_directory: "test_yaml"
max_evaluations_total: 20
max_cost_total: 4.2
overwrite_working_directory: True
post_run_summary: False
development_stage_id: 9
task_id:
max_evaluations_per_run: 5
continue_until_max_evaluation_completed: True
loss_value_on_error: 2.4
cost_value_on_error: 2.1
ignore_errors: False
searcher: # Optional Loading
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: base_optimizer
searcher_path: "/path/to/searcher"
searcher_kwargs:
initial_design_size: 5
surrogate_model: gp
pre_load_hooks:
hook1:
run_args:
run_pipeline:
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: hook1
name: "run_pipeline"
pipeline_space: # Optional loading
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: "pipeline_space"
root_directory: "test_yaml"
max_evaluations_total: 20
max_cost_total: 4.2
overwrite_working_directory: True
post_run_summary: False
development_stage_id: 9
task_id:
max_evaluations_per_run: 5
continue_until_max_evaluation_completed: True
loss_value_on_error: 2.4
cost_value_on_error: 2.1
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
pre_load_hooks:
hook1:
path: "tests/test_yaml_run_args/test_yaml_run_args.py"
name: hook1
6 changes: 1 addition & 5 deletions tests/test_yaml_run_args/test_yaml_run_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
pipeline_space = dict(lr=neps.FloatParameter(lower=1.2, upper=4.2),
epochs=neps.IntegerParameter(lower=1, upper=10))

# This is needed for a test case where the argument 'searcher' is 'base_optimizer'.
search_space = SearchSpace(**pipeline_space)
base_optimizer = BayesianOptimization(pipeline_space=search_space)


def run_pipeline():
"""func to test loading of run_pipeline"""
Expand Down Expand Up @@ -181,7 +177,7 @@ def are_functions_equivalent(f1, f2):
"loss_value_on_error": 2.4,
"cost_value_on_error": 2.1,
"ignore_errors": False,
"searcher": base_optimizer,
"searcher": BayesianOptimization,
"searcher_path": "/path/to/searcher",
"searcher_kwargs": {
"initial_design_size": 5,
Expand Down

0 comments on commit 2d7eadb

Please sign in to comment.