def process_boosters(dag): dag_nx = utils.dag_to_nx(dag) processed_dag = dict() sub_dags = [] for k, spec in dag.items(): if spec[1][0] == 'booBegin': input_name = spec[0] for node in nx.dfs_preorder_nodes(dag_nx, k): node_type = dag[node][1][0] if node == k: continue if node_type == 'booster': sub_dags.append(dag[node][1][2]) if node_type == 'booEnd': sub_dags = [normalize_dag(sd) for sd in sub_dags] processed_dag[k] = (input_name, ['booster', {'sub_dags': sub_dags}], dag[node][2]) sub_dags = [] break elif spec[1][0] in ['booster', 'booEnd']: continue else: processed_dag[k] = spec return processed_dag
def process_boosters(dag): dag_nx = utils.dag_to_nx(dag) processed_dag = dict() sub_dags = [] for k, spec in dag.items(): if spec[1][0] == 'booBegin': input_name = spec[0] for node in nx.dfs_preorder_nodes(dag_nx, k): node_type = dag[node][1][0] if node == k: continue if node_type == 'booster': sub_dags.append(dag[node][1][2]) if node_type == 'booEnd': sub_dags = [normalize_dag(sd) for sd in sub_dags] processed_dag[k] = (input_name, ['booster', { 'sub_dags': sub_dags }], dag[node][2]) sub_dags = [] break elif spec[1][0] in ['booster', 'booEnd']: continue else: processed_dag[k] = spec return processed_dag
def extract_subgraphs(dag, node): out = [] dag_nx = utils.dag_to_nx(dag) reverse_dag_nx = dag_nx.reverse() for p in dag_nx.predecessors(node): out.append({ k: v for k, v in dag.items() if k in list(nx.dfs_preorder_nodes(reverse_dag_nx, p)) }) common_nodes = [n for n in out[0] if all((n in o for o in out))] toposort = list(nx.topological_sort(dag_nx)) sorted_common = sorted(common_nodes, key=lambda k: -toposort.index(k)) inputs = np.unique([ dag[n][0] for n in dag_nx.successors(sorted_common[0]) if any([n in o for o in out]) ]) assert len(inputs) == 1 input_id = inputs[0] remove_common = sorted_common nout = [] for o in out: no = dict() no['input'] = ([], 'input', input_id) for k, v in o.items(): if k in remove_common: continue ins = v[2] if not isinstance(ins, list): ins = [ins] if ins[0] in dag[node][0]: no[k] = v[0], v[1], 'output' continue no[k] = v nout.append(no) initial_dag = {k: v for k, v in dag.items() if k in common_nodes} for k, v in initial_dag.items(): if isinstance(v[2], list) and input_id in v[2]: initial_dag[k] = (v[0], v[1], [x if x != input_id else 'output' for x in v[2]]) break if v[2] == input_id: initial_dag[k] = (v[0], v[1], 'output') return nout, initial_dag, input_id
def extract_subgraphs(dag, node): out = [] dag_nx = utils.dag_to_nx(dag) reverse_dag_nx = dag_nx.reverse() for p in dag_nx.predecessors(node): out.append({k: v for k, v in dag.items() if k in list(nx.dfs_preorder_nodes(reverse_dag_nx, p))}) common_nodes = [n for n in out[0] if all((n in o for o in out))] toposort = list(nx.topological_sort(dag_nx)) sorted_common = sorted(common_nodes, key=lambda k: -toposort.index(k)) inputs = np.unique([dag[n][0] for n in dag_nx.successors(sorted_common[0]) if any([n in o for o in out])]) assert len(inputs) == 1 input_id = inputs[0] remove_common = sorted_common nout = [] for o in out: no = dict() no['input'] = ([], 'input', input_id) for k, v in o.items(): if k in remove_common: continue ins = v[2] if not isinstance(ins, list): ins = [ins] if ins[0] in dag[node][0]: no[k] = v[0], v[1], 'output' continue no[k] = v nout.append(no) initial_dag = {k: v for k, v in dag.items() if k in common_nodes} for k, v in initial_dag.items(): if isinstance(v[2], list) and input_id in v[2]: initial_dag[k] = (v[0], v[1], [x if x != input_id else 'output' for x in v[2]]) break if v[2] == input_id: initial_dag[k] = (v[0], v[1], 'output') return nout, initial_dag, input_id