flexcv.yaml_parser
flexcv.yaml_parser.cat_constructor(loader, node)
Construct an optuna CategoricalDistribution from a yaml node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loader |
SafeLoader
|
The yaml loader. |
required |
node |
ScalarNode
|
The yaml node. |
required |
Returns:
Type | Description |
---|---|
CategoricalDistribution
|
The constructed CategoricalDistribution. |
Source code in flexcv/yaml_parser.py
flexcv.yaml_parser.float_constructor(loader, node)
Construct an optuna FloatDistribution from a yaml node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loader |
SafeLoader
|
The yaml loader. |
required |
node |
ScalarNode
|
The yaml node. |
required |
Returns:
Type | Description |
---|---|
FloatDistribution
|
The constructed FloatDistribution. |
Source code in flexcv/yaml_parser.py
flexcv.yaml_parser.get_loader()
This function returns a yaml loader with the custom constructors for the optuna distributions. Custom and safe constructors are added to the following tags:
- !Int
- !Float
- !Cat
Source code in flexcv/yaml_parser.py
flexcv.yaml_parser.int_constructor(loader, node)
Construct an optuna IntDistribution from a yaml node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loader |
SafeLoader
|
The yaml loader. |
required |
node |
ScalarNode
|
The yaml node. |
required |
Returns:
Type | Description |
---|---|
IntDistribution
|
The constructed IntDistribution. |
Source code in flexcv/yaml_parser.py
flexcv.yaml_parser.parse_yaml_output_to_mapping_dict(yaml_dict)
This function parses the output of the yaml parser to a ModelMappingDict object. Models and post processors are imported automatically.
Note
Despite of automatically importing the classes, no arbitrary code is executed. The yaml parser uses the safe loader and a custom constructors for the optuna distributions. The imports are done by the importlib module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yaml_dict |
dict
|
The output of the yaml parser. |
required |
Returns:
Type | Description |
---|---|
ModelMappingDict
|
A dictionary of ModelConfigDict objects. |
Source code in flexcv/yaml_parser.py
flexcv.yaml_parser.read_mapping_from_yaml_file(yaml_file_path)
This function reads in a yaml file and returns a ModelMappingDict object. Use the yaml tags !Int, !Float, !Cat to specify the type of the hyperparameter distributions. The parser takes care of importing the classes specified in the yaml file in the fields model and post_processor.
Note
Despite of automatically importing the classes, no arbitrary code is executed. The yaml parser uses the safe loader and a custom constructors for the optuna distributions. The imports are done by the importlib module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yaml_file_path |
str
|
The yaml file path. |
required |
Returns:
Type | Description |
---|---|
ModelMappingDict
|
A dictionary of ModelConfigDict objects. |
Example
Your file.yaml file could look like this:
And you would read it in like this:Source code in flexcv/yaml_parser.py
flexcv.yaml_parser.read_mapping_from_yaml_string(yaml_code)
This function reads a yaml string and returns a ModelMappingDict object. Use the yaml tags !Int, !Float, !Cat to specify the type of the hyperparameter distributions. The parser takes care of importing the classes specified in the yaml file in the fields model and post_processor.
Note
Despite of automatically importing the classes, no arbitrary code is executed. The yaml parser uses the safe loader and a custom constructors for the optuna distributions. The imports are done by the importlib module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yaml_code |
str
|
The yaml code. |
required |
Returns:
Type | Description |
---|---|
ModelMappingDict
|
A dictionary of ModelConfigDict objects. |
Example:
```python
yaml_code = '''
RandomForest:
model: sklearn.ensemble.RandomForestRegressor
post_processor: flexcv.model_postprocessing.MixedEffectsPostProcessor
requires_inner_cv: True
params:
max_depth: !Int
low: 1
high: 10
'''
model_mapping = read_mapping_from_yaml_string(yaml_code)
```
Source code in flexcv/yaml_parser.py
flexcv.yaml_parser.split_import(import_string)
This function splits an import string into the class name and the module path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
import_string |
str
|
The import string. |
required |
Returns:
Type | Description |
---|---|
tuple[str, str]
|
The class name and the module path. |