[docs]@dataclassclassNode:node_type:strstrategy:Dictnode_params:Dictmodules:List[Module]run_node:Callable=field(init=False)def__post_init__(self):self.run_node=get_support_nodes(self.node_type)ifself.run_nodeisNone:raiseValueError(f"Node type {self.node_type} is not supported.")
[docs]defget_param_combinations(self)->Tuple[List[Callable],List[Dict]]:""" This method returns a combination of module and node parameters, also corresponding modules. :return: Each module and its module parameters. :rtype: Tuple[List[Callable], List[Dict]] """defmake_single_combination(module:Module)->List[Dict]:input_dict={**self.node_params,**module.module_param}returnmake_combinations(input_dict)combinations=list(map(make_single_combination,self.modules))module_list,combination_list=explode(self.modules,combinations)returnlist(map(lambdax:x.module,module_list)),combination_list
[docs]defextract_values(node:Node,key:str)->List[str]:""" This function extract values from node's modules' module_param. :param node: The node you want to extract values from. :param key: The key of module_param that you want to extract. :return: The list of extracted values. It removes duplicated elements automatically. """defextract_module_values(module:Module):ifkeynotinmodule.module_param:return[]value=module.module_param[key]ifisinstance(value,str)orisinstance(value,int):return[value]elifisinstance(value,list):returnvalueelse:raiseValueError(f"{key} must be str,list or int, but got {type(value)}")values=list(map(extract_module_values,node.modules))returnlist(set(list(itertools.chain.from_iterable(values))))
[docs]defextract_values_from_nodes(nodes:List[Node],key:str)->List[str]:""" This function extract values from nodes' modules' module_param. :param nodes: The nodes you want to extract values from. :param key: The key of module_param that you want to extract. :return: The list of extracted values. It removes duplicated elements automatically. """values=list(map(lambdanode:extract_values(node,key),nodes))returnlist(set(list(itertools.chain.from_iterable(values))))
[docs]defextract_values_from_nodes_strategy(nodes:List[Node],key:str)->List[Any]:""" This function extract values from nodes' strategy. :param nodes: The nodes you want to extract values from. :param key: The key string that you want to extract. :return: The list of extracted values. It removes duplicated elements automatically. """values=[]fornodeinnodes:value_list=find_key_values(node.strategy,key)ifvalue_list:values.extend(value_list)returnvalues
[docs]defmodule_type_exists(nodes:List[Node],module_type:str)->bool:""" This function check if the module type exists in the nodes. :param nodes: The nodes you want to check. :param module_type: The module type you want to check. :return: True if the module type exists in the nodes. """returnany(list(map(lambdanode:any(list(map(lambdamodule:module.module_type==module_type,node.modules,))),nodes,)))