def test_get_connected_graph(): G = graph_from_edges_string("A - B ; B - C") Gs = get_connected_graphs(G) assert len(Gs) == 1 assert Gs[0] == G def gequal(g1, g2): if set(g1.nodes) != set(g2.nodes): return False if set(g1.edges) != set(g2.edges): return False return True G = graph_from_edges_string("A - B; C - D") Gs = get_connected_graphs(G) assert len(Gs) == 2 g1, g2 = graph_from_edges_string("A - B"), graph_from_edges_string("C - D") G1, G2 = Gs assert (gequal(g1, G1) and gequal(g2, G2)) or (gequal(g1, G2) and gequal(g2, G1))
def _rec_convert_graph_to_code_OLD2(Graph, all_models_params, models_dico, model_name_mapping=None): """ recursive function used to convert a Graph into a json code See convert_graph_to_code """ ### ** only one node in Graph : I'll return what was saved in models_dico ** ### if len(Graph.nodes) == 1: node = list(Graph.nodes)[0] return models_dico[node] node = _find_first_composition_node(Graph) if node is not None: predecessors = gh.get_all_predecessors(Graph, node) successors = gh.get_all_successors(Graph, node) if not gh.is_it_a_partition(list(Graph.nodes), [predecessors, [node], successors]): raise ValueError("Incorrect graph, wrong split around node %s" % str(node)) else: predecessors = [] successors = [] if node is None or len(successors) == 0: ### ** It's means I'll return a GraphPipeline ** ### edges = gh.edges_from_graph(Graph) if model_name_mapping is None: model_name_mapping = _create_name_mapping(list(Graph.nodes)) # each node in graph will be mapped to a name within the GraphPipeline models = {model_name_mapping[n]: models_dico[n] for n in Graph.nodes} edges = [ tuple((model_name_mapping[e] for e in edge)) for edge in edges ] return (SpecialModels.GraphPipeline, { "models": models, "edges": edges }) Graph_bellow = Graph.subgraph(successors) connected_Gbellow = gh.get_connected_graphs(Graph_bellow) if len(predecessors) == 0 and len(connected_Gbellow) > 1: return ( _klass_from_node(node), [ _rec_convert_graph_to_code_OLD2(Gb, all_models_params, models_dico, model_name_mapping) for Gb in connected_Gbellow ], all_models_params[node], ) elif len(predecessors) == 0 and len(connected_Gbellow) == 1: return ( _klass_from_node(node), _rec_convert_graph_to_code_OLD2(Graph_bellow, all_models_params, models_dico, model_name_mapping), all_models_params[node], ) else: G_bellow_and_node = Graph.subgraph([node] + successors) G_above = Graph.subgraph(predecessors + [node]) models_dico[node] = _rec_convert_graph_to_code_OLD2( G_bellow_and_node, all_models_params, models_dico, model_name_mapping) return _rec_convert_graph_to_code(G_above, all_models_params, models_dico, model_name_mapping)
def _rec_convert_graph_to_code_OLD(G, all_params): """ recursive function to convert a graph into a json representation """ if len(G.nodes) == 0: return {} ### 1) Find First composition node has_composition = False for node in gh.iter_graph(G): if StepCategories.is_composition_step(node[0]): has_composition = True break return_gpipe = not has_composition if has_composition: ### If there is a composition node, I need to split between what is above and what is bellow predecessors = gh.get_all_predecessors(G, node) successors = gh.get_all_successors(G, node) if not gh.is_it_a_partition(list(G.nodes), [predecessors, [node], successors]): raise ValueError("Incorrect graph, wrong split around node %s" % str(node)) if len(successors) == 0: # If nothing bellow, I'll be able to return something return_gpipe = True if return_gpipe: if len(G.nodes) > 1: ### I'll create a GraphPipeline object edges = gh.edges_from_graph(G) model_name_mapping = _create_name_mapping(list(G.nodes)) # each node in graph will be mapped to a name within the GraphPipeline models = {model_name_mapping[n]: all_params[n] for n in G.nodes} edges = [ tuple((model_name_mapping[e] for e in edge)) for edge in edges ] return (SpecialModels.GraphPipeline, { "models": models, "edges": edges }) else: ### Otherwise it is just the model_name with its parameters return node[1][1], all_params[list(G.nodes)[0]] G_above = G.subgraph(predecessors + [node]) G_bellow = G.subgraph(successors) connected_Gbellow = gh.get_connected_graphs(G_bellow) if len(connected_Gbellow) == 1: # what is bellow is a 'connected graph' : it means that the composition need should be applied to One model all_params[node] = _rec_convert_graph_to_code_OLD(G_bellow, all_params) else: # otherwise, the composition will be applied to a list of models all_params[node] = [ _rec_convert_graph_to_code_OLD(g, all_params) for g in connected_Gbellow ] return _rec_convert_graph_to_code_OLD(G_above, all_params)