src.fairreckitlib.model.pipeline.model_config_parsing
This module contains a parser for the model configuration(s).
Functions:
parse_models_config: parse models from the experiment configuration.
parse_api_models_config: parse multiple API model configurations.
This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)
1"""This module contains a parser for the model configuration(s). 2 3Functions: 4 5 parse_models_config: parse models from the experiment configuration. 6 parse_api_models_config: parse multiple API model configurations. 7 8This program has been developed by students from the bachelor Computer Science at 9Utrecht University within the Software Project course. 10© Copyright Utrecht University (Department of Information and Computing Sciences) 11""" 12 13from typing import Any, Dict, List, Optional 14 15from ...core.config.config_factories import GroupFactory 16from ...core.events.event_dispatcher import EventDispatcher 17from ...core.parsing.parse_assert import assert_is_type, assert_is_container_not_empty 18from ...core.parsing.parse_assert import assert_is_key_in_dict, assert_is_one_of_list 19from ...core.parsing.parse_config_object import parse_config_object_list 20from ..model_factory import KEY_MODELS 21from .model_config import ModelConfig 22 23 24def parse_models_config( 25 experiment_config: Dict[str, Any], 26 model_type_factory: GroupFactory, 27 event_dispatcher: EventDispatcher) -> Optional[Dict[str, List[ModelConfig]]]: 28 """Parse the experiment KEY_MODELS section of the experiment configuration. 29 30 Args: 31 experiment_config: the experiment's total configuration. 32 model_type_factory: the model type factory containing the available models. 33 event_dispatcher: to dispatch the parse event on failure. 34 35 Returns: 36 a dictionary of parsed ModelConfig's keyed by API name or None when empty. 37 """ 38 # assert EXP_KEY_MODELS is present 39 if not assert_is_key_in_dict( 40 KEY_MODELS, 41 experiment_config, 42 event_dispatcher, 43 'PARSE ERROR: missing experiment key \'' + KEY_MODELS + '\' (required)' 44 ): return None 45 46 return parse_api_models_config( 47 experiment_config[KEY_MODELS], 48 model_type_factory, 49 event_dispatcher 50 ) 51 52 53def parse_api_models_config( 54 models_config: Any, 55 model_type_factory: GroupFactory, 56 event_dispatcher: EventDispatcher) -> Optional[Dict[str, List[ModelConfig]]]: 57 """Parse all model configurations. 58 59 Args: 60 models_config: the KEY_MODELS configuration. 61 model_type_factory: the model type factory containing the available models. 62 event_dispatcher: to dispatch the parse event on failure. 63 64 Returns: 65 a dictionary of parsed ModelConfig's keyed by API name or None when empty. 66 """ 67 parsed_config = None 68 69 # assert models_config is a dict 70 if not assert_is_type( 71 models_config, 72 dict, 73 event_dispatcher, 74 'PARSE ERROR: invalid experiment value for key \'' + KEY_MODELS + '\'', 75 default_value=parsed_config 76 ): return parsed_config 77 78 # assert models_config has entries 79 if not assert_is_container_not_empty( 80 models_config, 81 event_dispatcher, 82 'PARSE ERROR: experiment \'' + KEY_MODELS + '\' is empty', 83 default_value=parsed_config 84 ): return parsed_config 85 86 # parse models_config entries 87 parsed_config = {} 88 for api_name, models in models_config.items(): 89 # attempt to parse a list of ModelConfig's for this API 90 if not assert_is_one_of_list( 91 api_name, 92 model_type_factory.get_available_names(), 93 event_dispatcher, 94 'PARSE WARNING: unknown model API \'' + api_name + '\'' 95 ): continue 96 97 parsed_config_objs = parse_config_object_list( 98 api_name, 99 'model', 100 models, 101 model_type_factory.get_factory(api_name), 102 event_dispatcher 103 ) 104 105 # skip when no configurations are actually parsed 106 if not assert_is_container_not_empty( 107 parsed_config_objs, 108 event_dispatcher, 109 'PARSE WARNING: skipping models for API: ' + api_name 110 ): continue 111 112 parsed_config[api_name] = [ModelConfig(m.name, m.params) for (m, _) in parsed_config_objs] 113 114 # final check to verify at least one model got parsed 115 if not assert_is_container_not_empty( 116 parsed_config, 117 event_dispatcher, 118 'PARSE ERROR: no experiment ' + KEY_MODELS + ' specified' 119 ): return None 120 121 return parsed_config
25def parse_models_config( 26 experiment_config: Dict[str, Any], 27 model_type_factory: GroupFactory, 28 event_dispatcher: EventDispatcher) -> Optional[Dict[str, List[ModelConfig]]]: 29 """Parse the experiment KEY_MODELS section of the experiment configuration. 30 31 Args: 32 experiment_config: the experiment's total configuration. 33 model_type_factory: the model type factory containing the available models. 34 event_dispatcher: to dispatch the parse event on failure. 35 36 Returns: 37 a dictionary of parsed ModelConfig's keyed by API name or None when empty. 38 """ 39 # assert EXP_KEY_MODELS is present 40 if not assert_is_key_in_dict( 41 KEY_MODELS, 42 experiment_config, 43 event_dispatcher, 44 'PARSE ERROR: missing experiment key \'' + KEY_MODELS + '\' (required)' 45 ): return None 46 47 return parse_api_models_config( 48 experiment_config[KEY_MODELS], 49 model_type_factory, 50 event_dispatcher 51 )
Parse the experiment KEY_MODELS section of the experiment configuration.
Args: experiment_config: the experiment's total configuration. model_type_factory: the model type factory containing the available models. event_dispatcher: to dispatch the parse event on failure.
Returns: a dictionary of parsed ModelConfig's keyed by API name or None when empty.
54def parse_api_models_config( 55 models_config: Any, 56 model_type_factory: GroupFactory, 57 event_dispatcher: EventDispatcher) -> Optional[Dict[str, List[ModelConfig]]]: 58 """Parse all model configurations. 59 60 Args: 61 models_config: the KEY_MODELS configuration. 62 model_type_factory: the model type factory containing the available models. 63 event_dispatcher: to dispatch the parse event on failure. 64 65 Returns: 66 a dictionary of parsed ModelConfig's keyed by API name or None when empty. 67 """ 68 parsed_config = None 69 70 # assert models_config is a dict 71 if not assert_is_type( 72 models_config, 73 dict, 74 event_dispatcher, 75 'PARSE ERROR: invalid experiment value for key \'' + KEY_MODELS + '\'', 76 default_value=parsed_config 77 ): return parsed_config 78 79 # assert models_config has entries 80 if not assert_is_container_not_empty( 81 models_config, 82 event_dispatcher, 83 'PARSE ERROR: experiment \'' + KEY_MODELS + '\' is empty', 84 default_value=parsed_config 85 ): return parsed_config 86 87 # parse models_config entries 88 parsed_config = {} 89 for api_name, models in models_config.items(): 90 # attempt to parse a list of ModelConfig's for this API 91 if not assert_is_one_of_list( 92 api_name, 93 model_type_factory.get_available_names(), 94 event_dispatcher, 95 'PARSE WARNING: unknown model API \'' + api_name + '\'' 96 ): continue 97 98 parsed_config_objs = parse_config_object_list( 99 api_name, 100 'model', 101 models, 102 model_type_factory.get_factory(api_name), 103 event_dispatcher 104 ) 105 106 # skip when no configurations are actually parsed 107 if not assert_is_container_not_empty( 108 parsed_config_objs, 109 event_dispatcher, 110 'PARSE WARNING: skipping models for API: ' + api_name 111 ): continue 112 113 parsed_config[api_name] = [ModelConfig(m.name, m.params) for (m, _) in parsed_config_objs] 114 115 # final check to verify at least one model got parsed 116 if not assert_is_container_not_empty( 117 parsed_config, 118 event_dispatcher, 119 'PARSE ERROR: no experiment ' + KEY_MODELS + ' specified' 120 ): return None 121 122 return parsed_config
Parse all model configurations.
Args: models_config: the KEY_MODELS configuration. model_type_factory: the model type factory containing the available models. event_dispatcher: to dispatch the parse event on failure.
Returns: a dictionary of parsed ModelConfig's keyed by API name or None when empty.