Skip to content

Commit 285b993

Browse files
committed
clean up code and docsctrings
1 parent 736b58a commit 285b993

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

neps/utils/run_args.py

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import logging
1010
import sys
1111
from pathlib import Path
12-
from typing import Any, Callable
12+
from typing import Callable
1313

1414
import yaml
1515

@@ -45,13 +45,13 @@ def get_run_args_from_yaml(path: str) -> dict:
4545
"""Load and validate NEPS run arguments from a specified YAML configuration file
4646
provided via run_args.
4747
48-
This function reads a YAML file, extracts the arguments required by NEPS,
48+
This function reads a YAML file, extracts the arguments required by NePS,
4949
validates these arguments, and then returns them in a dictionary. It checks for the
5050
presence and validity of expected parameters, and distinctively handles more complex
5151
configurations, specifically those that are dictionaries(e.g. pipeline_space) or
5252
objects(e.g. run_pipeline) requiring loading.
5353
54-
Parameters:
54+
Args:
5555
path (str): The file path to the YAML configuration file.
5656
5757
Returns:
@@ -66,7 +66,7 @@ def get_run_args_from_yaml(path: str) -> dict:
6666
# Initialize an empty dictionary to hold the extracted settings
6767
settings = {}
6868

69-
# List allowed NEPS run arguments with simple types (e.g., string, int). Parameters
69+
# List allowed NePS run arguments with simple types (e.g., string, int). Parameters
7070
# like 'run_pipeline', 'preload_hooks', 'pipeline_space',
7171
# and 'searcher' are excluded due to needing specialized processing.
7272
expected_parameters = [
@@ -119,22 +119,15 @@ def get_run_args_from_yaml(path: str) -> dict:
119119
def config_loader(path: str) -> dict:
120120
"""Loads a YAML file and returns the contents under the 'run_args' key.
121121
122-
Validates the existence and format of the YAML file and checks for the presence of
123-
the 'run_args' as the only top level key. If any conditions are not met,
124-
raises an
125-
exception with a helpful message.
126-
127122
Args:
128123
path (str): Path to the YAML file.
129124
130125
Returns:
131-
dict: Contents under the 'run_args' key.
126+
Content of the yaml (dict)
132127
133128
Raises:
134129
FileNotFoundError: If the file at 'path' does not exist.
135130
ValueError: If the file is not a valid YAML.
136-
KeyError: If 'run_args' key is missing.
137-
KeyError: If 'run_args' is not the only top level key
138131
"""
139132
try:
140133
with open(path) as file: # noqa: PTH123
@@ -156,10 +149,13 @@ def extract_leaf_keys(d: dict, special_keys: dict | None = None) -> tuple[dict,
156149
Special keys (e.g.'run_pipeline') are also extracted if present
157150
and their corresponding values (dict) at any level in the nested structure.
158151
159-
:param d: The dictionary to extract values from.
160-
:param special_keys: A dictionary to store values of special keys.
161-
:return: A tuple containing the leaf keys dictionary and the dictionary for
162-
special keys.
152+
Args:
153+
d (dict): The dictionary to extract values from.
154+
special_keys (dict|None): A dictionary to store values of special keys.
155+
156+
Returns:
157+
A tuple containing the leaf keys dictionary and the dictionary for
158+
special keys.
163159
"""
164160
if special_keys is None:
165161
special_keys = {
@@ -191,14 +187,11 @@ def handle_special_argument_cases(settings: dict, special_configs: dict) -> None
191187
configurations like 'pre_load_hooks' which need individual processing or function
192188
loading.
193189
194-
Parameters:
195-
- settings (dict): The dictionary to be updated with processed configurations.
196-
- special_configs (dict): A dictionary containing configuration keys and values
190+
Args:
191+
settings (dict): The dictionary to be updated with processed configurations.
192+
special_configs (dict): A dictionary containing configuration keys and values
197193
that require special processing.
198194
199-
Returns:
200-
- None: The function modifies 'settings' in place.
201-
202195
"""
203196
# process special configs
204197
process_run_pipeline(RUN_PIPELINE, special_configs, settings)
@@ -261,13 +254,13 @@ def process_searcher(key: str, special_configs: dict, settings: dict) -> None:
261254
Checks if the key exists in special_configs. If found, it processes the
262255
value based on its type. Updates settings with the processed searcher.
263256
264-
Parameters:
265-
key (str): Key to look up in special_configs.
266-
special_configs (dict): Dictionary of special configurations.
267-
settings (dict): Dictionary to update with the processed searcher.
257+
Args:
258+
key (str): Key to look up in special_configs.
259+
special_configs (dict): Dictionary of special configurations.
260+
settings (dict): Dictionary to update with the processed searcher.
268261
269262
Raises:
270-
TypeError: If the value for the key is neither a string, Path, nor a dictionary.
263+
TypeError: If the value for the key is neither a string, Path, nor a dictionary.
271264
"""
272265
if special_configs.get(key) is not None:
273266
searcher = special_configs[key]
@@ -294,13 +287,13 @@ def process_searcher(key: str, special_configs: dict, settings: dict) -> None:
294287
def process_run_pipeline(key: str, special_configs: dict, settings: dict) -> None:
295288
"""Processes the run pipeline configuration and updates the settings dictionary.
296289
297-
Parameters:
298-
key (str): Key to look up in special_configs.
299-
special_configs (dict): Dictionary of special configurations.
300-
settings (dict): Dictionary to update with the processed function.
290+
Args:
291+
key (str): Key to look up in special_configs.
292+
special_configs (dict): Dictionary of special configurations.
293+
settings (dict): Dictionary to update with the processed function.
301294
302295
Raises:
303-
KeyError: If required keys ('path' and 'name') are missing in the config.
296+
KeyError: If required keys ('path' and 'name') are missing in the config.
304297
"""
305298
if special_configs.get(key) is not None:
306299
config = special_configs[key]
@@ -403,7 +396,7 @@ def check_run_args(settings: dict) -> None:
403396
TypeError for type mismatches.
404397
405398
Args:
406-
settings (dict): NEPS configuration settings.
399+
settings (dict): NePS configuration settings.
407400
408401
Raises:
409402
TypeError: For mismatched setting value types.
@@ -462,14 +455,14 @@ def check_essential_arguments(
462455
searcher: BaseOptimizer | None,
463456
run_args: str | None,
464457
) -> None:
465-
"""Validates essential NEPS configuration arguments.
458+
"""Validates essential NePS configuration arguments.
466459
467460
Ensures 'run_pipeline', 'root_directory', 'pipeline_space', and either
468461
'max_cost_total' or 'max_evaluation_total' are provided for NePS execution.
469462
Raises ValueError with missing argument details. Additionally, checks 'searcher'
470463
is a BaseOptimizer if 'pipeline_space' is absent.
471464
472-
Parameters:
465+
Args:
473466
run_pipeline: Function for the pipeline execution.
474467
root_directory (str): Directory path for data storage.
475468
pipeline_space: search space for this run.
@@ -499,19 +492,19 @@ def check_essential_arguments(
499492

500493
def check_double_reference(
501494
func: Callable, func_arguments: dict, yaml_arguments: dict
502-
) -> Any:
495+
) -> None:
503496
"""Checks if no argument is defined both via function arguments and YAML.
504497
505-
Parameters:
506-
- func (Callable): The function to check arguments against.
507-
- func_arguments (Dict): A dictionary containing the provided arguments to the
508-
function and their values.
509-
- yaml_arguments (Dict): A dictionary containing the arguments provided via a YAML
510-
file.
498+
Args:
499+
func (Callable): The function to check arguments against.
500+
func_arguments (Dict): A dictionary containing the provided arguments to the
501+
function and their values.
502+
yaml_arguments (Dict): A dictionary containing the arguments provided via a YAML
503+
file.
511504
512505
Raises:
513-
- ValueError: If any provided argument is defined both via function arguments and the
514-
YAML file.
506+
ValueError: If any provided argument is defined both via function arguments and
507+
the YAML file.
515508
"""
516509
sig = inspect.signature(func)
517510

0 commit comments

Comments
 (0)