Yaml search space utils
neps.search_spaces.yaml_search_space_utils
#
SearchSpaceFromYamlFileError
#
Bases: Exception
Exception raised for errors occurring during the initialization of the search space from a YAML file.
ATTRIBUTE | DESCRIPTION |
---|---|
exception_type |
The type of the original exception.
TYPE:
|
message |
A detailed message that includes the type of the original exception and the error description.
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
exception |
The original exception that was raised during the initialization of the search space from the YAML file.
TYPE:
|
Example Usage
try: # Code to initialize search space from YAML file except (KeyError, TypeError, ValueError) as e: raise SearchSpaceFromYamlFileError(e)
Source code in neps/search_spaces/yaml_search_space_utils.py
check_keys
#
check_keys(
name: str,
details: dict[str, str | int | float],
allowed_keys: set,
mandatory_keys: set,
)
Checks if all keys in 'my_dict' are contained in the set 'allowed_keys' and if all keys in 'mandatory_keys' are present in 'my_dict'. Raises an exception if an unallowed key is found or if a mandatory key is missing.
Source code in neps/search_spaces/yaml_search_space_utils.py
convert_scientific_notation
#
convert_scientific_notation(
value: str | int | float, show_usage_flag=False
) -> float | (float, bool)
Convert a given value to a float if it's a string that matches scientific e notation. This is especially useful for numbers like "3.3e-5" which YAML parsers may not directly interpret as floats.
If the 'show_usage_flag' is set to True, the function returns a tuple of the float conversion and a boolean flag indicating whether scientific notation was detected.
PARAMETER | DESCRIPTION |
---|---|
value |
The value to convert. Can be an integer, float, or a string representing a number, possibly in scientific notation. |
show_usage_flag |
Optional; defaults to False. If True, the function also returns a flag indicating whether scientific notation was detected in the string.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
float
|
The value converted to float if 'show_usage_flag' is False. (float, bool): A tuple containing the value converted to float and a flag indicating scientific notation detection if 'show_usage_flag' is True. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the value is a string and does not represent a valid number. |
Source code in neps/search_spaces/yaml_search_space_utils.py
deduce_and_validate_param_type
#
Deduces the parameter type from details and validates them.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the parameter.
TYPE:
|
details |
A dictionary containing parameter specifications.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
The deduced parameter type ('int', 'float', 'categorical', or 'constant').
TYPE:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the type cannot be deduced or the details don't align with expected |
Source code in neps/search_spaces/yaml_search_space_utils.py
deduce_param_type
#
Deduces the parameter type based on the provided details.
The function interprets the 'details' dictionary to determine the parameter type. The dictionary should include key-value pairs that describe the parameter's characteristics, such as lower, upper, default value, or possible choices.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the parameter.
TYPE:
|
details |
A dictionary containing parameter |
RETURNS | DESCRIPTION |
---|---|
str
|
The deduced parameter type ('int', 'float', 'categorical', or 'constant').
TYPE:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the parameter type cannot be deduced from the details, or if the |
Example
param_type = deduce_param_type('example_param', {'lower': 0, 'upper': 10})
Source code in neps/search_spaces/yaml_search_space_utils.py
validate_categorical_parameter
#
Validates a categorical parameter, including conversion of scientific notation values to floats within the choices.
This function ensures that the 'choices' key in the details is a list and attempts to convert any elements in scientific notation to floats. It also handles the 'default' value, converting it from scientific notation if necessary.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the categorical parameter.
TYPE:
|
details |
A dictionary containing the parameter's specifications. Required key is 'choices', with 'default' being optional. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If 'choices' is not a list |
Source code in neps/search_spaces/yaml_search_space_utils.py
validate_constant_parameter
#
Validates a constant parameter, including conversion of values in scientific notation to floats.
This function checks the 'value' key in the details dictionary and converts any value expressed in scientific notation to a float. It ensures that the mandatory 'value' key is provided and appropriately formatted.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the constant parameter.
TYPE:
|
details |
A dictionary containing the parameter's specifications. The required key is 'value'. |
Source code in neps/search_spaces/yaml_search_space_utils.py
validate_float_parameter
#
Validates and processes a float parameter's details, converting scientific notation values to float where necessary.
This function checks the type of 'lower' and 'upper', and the 'default' value (if present) for a float parameter. It handles conversion of values in scientific notation (e.g., 1e-5) to float.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the float parameter.
TYPE:
|
details |
A dictionary containing the parameter's specifications. Expected keys include 'lower', 'upper', and optionally 'default', among others. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If 'lower', 'upper', or 'default' are not valid floats or cannot be converted from scientific notation to floats. |
Source code in neps/search_spaces/yaml_search_space_utils.py
validate_integer_parameter
#
Validates and processes an integer parameter's details, converting scientific notation to integers where necessary.
This function checks the type of 'lower' and 'upper', and the 'default' value (if present) for an integer parameter. It also handles conversion of values in scientific notation (e.g., 1e2) to integers.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the integer parameter.
TYPE:
|
details |
A dictionary containing the parameter's specifications. Expected keys include 'lower', 'upper', and optionally 'default', among others. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If 'lower', 'upper', or 'default' are not valid integers or cannot be converted from scientific notation to integers. |
Source code in neps/search_spaces/yaml_search_space_utils.py
validate_param_details
#
Validates the details of a parameter based on its type.
This function checks the format and type-specific details of a parameter specified in a YAML file. It ensures that the 'name' of the parameter is a string and its 'details' are provided as a dictionary. Depending on the parameter type, it delegates the validation to the appropriate type-specific validation function.
Parameters: name (str): The name of the parameter. It should be a string. param_type (str): The type of the parameter. Supported types are 'int' (or 'integer'), 'float', 'cat' (or 'categorical'), and 'const' (or 'constant'). details (dict): The detailed configuration of the parameter, which includes its attributes like 'lower', 'upper', 'default', etc.
KeyError: If the 'name' is not a string or 'details' is not a dictionary, or if the necessary keys in the 'details' are missing based on the parameter type. TypeError: If the 'param_type' is not one of the supported types.
Example Usage: validate_param_details("learning_rate", "float", {"lower": 0.01, "upper": 0.1, "default": 0.05})