def shortTest(): gedlibpy.restart_env() print("Here is the mini Python function !") gedlibpy.load_GXL_graphs( "include/gedlib-master/data/datasets/Mutagenicity/data/", "include/gedlib-master/data/collections/Mutagenicity.xml") listID = gedlibpy.get_all_graph_ids() gedlibpy.set_edit_cost("CHEM_1") gedlibpy.init() gedlibpy.set_method("BIPARTITE", "") gedlibpy.init_method() g = listID[0] h = listID[1] gedlibpy.run_method(g, h) print("Node Map : ", gedlibpy.get_node_map(g, h)) print("Assignment Matrix : ") afficheMatrix(gedlibpy.get_assignment_matrix(g, h)) print("Upper Bound = " + str(gedlibpy.get_upper_bound(g, h)) + ", Lower Bound = " + str(gedlibpy.get_lower_bound(g, h)) + ", Runtime = " + str(gedlibpy.get_runtime(g, h)))
def classiqueTest(): gedlibpy.restart_env() gedlibpy.load_GXL_graphs( 'include/gedlib-master/data/datasets/Mutagenicity/data/', 'collections/MUTA_10.xml') listID = gedlibpy.get_all_graph_ids() afficheId = "" for i in listID: afficheId += str(i) + " " print("Number of graphs = " + str(len(listID)) + ", list of Ids = " + afficheId) gedlibpy.set_edit_cost("CHEM_1") gedlibpy.init() gedlibpy.set_method("IPFP", "") gedlibpy.init_method() g = listID[0] h = listID[0] gedlibpy.run_method(g, h) liste = gedlibpy.get_all_map(g, h) print("Forward map : ", gedlibpy.get_forward_map(g, h), ", Backward map : ", gedlibpy.get_backward_map(g, h)) print("Node Map : ", gedlibpy.get_node_map(g, h)) print("Upper Bound = " + str(gedlibpy.get_upper_bound(g, h)) + ", Lower Bound = " + str(gedlibpy.get_lower_bound(g, h)) + ", Runtime = " + str(gedlibpy.get_runtime(g, h)))
def nxTest(dataset): gedlibpy.restart_env() for graph in dataset: gedlibpy.add_nx_graph(graph, "") listID = gedlibpy.get_all_graph_ids() gedlibpy.set_edit_cost("CHEM_1") gedlibpy.init() gedlibpy.set_method("IPFP", "") gedlibpy.init_method() print(listID) g = listID[0] h = listID[1] gedlibpy.run_method(g, h) print("Node Map : ", gedlibpy.get_node_map(g, h)) print("Upper Bound = " + str(gedlibpy.get_upper_bound(g, h)) + ", Lower Bound = " + str(gedlibpy.get_lower_bound(g, h)) + ", Runtime = " + str(gedlibpy.get_runtime(g, h)))
def GED(g1, g2, lib='gedlibpy', cost='CHEM_1', method='IPFP', edit_cost_constant=[], stabilizer='min', repeat=50): """ Compute GED for 2 graphs. """ if lib == 'gedlibpy': def convertGraph(G): """Convert a graph to the proper NetworkX format that can be recognized by library gedlibpy. """ G_new = nx.Graph() for nd, attrs in G.nodes(data=True): G_new.add_node(str(nd), chem=attrs['atom_symbol']) # G_new.add_node(str(nd), x=str(attrs['attributes'][0]), # y=str(attrs['attributes'][1])) for nd1, nd2, attrs in G.edges(data=True): G_new.add_edge(str(nd1), str(nd2), valence=attrs['bond_type']) # G_new.add_edge(str(nd1), str(nd2)) return G_new gedlibpy.restart_env() gedlibpy.add_nx_graph(convertGraph(g1), "") gedlibpy.add_nx_graph(convertGraph(g2), "") listID = gedlibpy.get_all_graph_ids() gedlibpy.set_edit_cost(cost, edit_cost_constant=edit_cost_constant) gedlibpy.init() gedlibpy.set_method(method, "") gedlibpy.init_method() g = listID[0] h = listID[1] if stabilizer == None: gedlibpy.run_method(g, h) pi_forward = gedlibpy.get_forward_map(g, h) pi_backward = gedlibpy.get_backward_map(g, h) upper = gedlibpy.get_upper_bound(g, h) lower = gedlibpy.get_lower_bound(g, h) elif stabilizer == 'mean': # @todo: to be finished... upper_list = [np.inf] * repeat for itr in range(repeat): gedlibpy.run_method(g, h) upper_list[itr] = gedlibpy.get_upper_bound(g, h) pi_forward = gedlibpy.get_forward_map(g, h) pi_backward = gedlibpy.get_backward_map(g, h) lower = gedlibpy.get_lower_bound(g, h) upper = np.mean(upper_list) elif stabilizer == 'median': if repeat % 2 == 0: repeat += 1 upper_list = [np.inf] * repeat pi_forward_list = [0] * repeat pi_backward_list = [0] * repeat for itr in range(repeat): gedlibpy.run_method(g, h) upper_list[itr] = gedlibpy.get_upper_bound(g, h) pi_forward_list[itr] = gedlibpy.get_forward_map(g, h) pi_backward_list[itr] = gedlibpy.get_backward_map(g, h) lower = gedlibpy.get_lower_bound(g, h) upper = np.median(upper_list) idx_median = upper_list.index(upper) pi_forward = pi_forward_list[idx_median] pi_backward = pi_backward_list[idx_median] elif stabilizer == 'min': upper = np.inf for itr in range(repeat): gedlibpy.run_method(g, h) upper_tmp = gedlibpy.get_upper_bound(g, h) if upper_tmp < upper: upper = upper_tmp pi_forward = gedlibpy.get_forward_map(g, h) pi_backward = gedlibpy.get_backward_map(g, h) lower = gedlibpy.get_lower_bound(g, h) if upper == 0: break elif stabilizer == 'max': upper = 0 for itr in range(repeat): gedlibpy.run_method(g, h) upper_tmp = gedlibpy.get_upper_bound(g, h) if upper_tmp > upper: upper = upper_tmp pi_forward = gedlibpy.get_forward_map(g, h) pi_backward = gedlibpy.get_backward_map(g, h) lower = gedlibpy.get_lower_bound(g, h) elif stabilizer == 'gaussian': pass dis = upper # make the map label correct (label remove map as np.inf) nodes1 = [n for n in g1.nodes()] nodes2 = [n for n in g2.nodes()] nb1 = nx.number_of_nodes(g1) nb2 = nx.number_of_nodes(g2) pi_forward = [nodes2[pi] if pi < nb2 else np.inf for pi in pi_forward] pi_backward = [nodes1[pi] if pi < nb1 else np.inf for pi in pi_backward] return dis, pi_forward, pi_backward
def GED_n(Gn, lib='gedlibpy', cost='CHEM_1', method='IPFP', edit_cost_constant=[], stabilizer='min', repeat=50): """ Compute GEDs for a group of graphs. """ if lib == 'gedlibpy': def convertGraph(G): """Convert a graph to the proper NetworkX format that can be recognized by library gedlibpy. """ G_new = nx.Graph() for nd, attrs in G.nodes(data=True): G_new.add_node(str(nd), chem=attrs['atom_symbol']) for nd1, nd2, attrs in G.edges(data=True): # G_new.add_edge(str(nd1), str(nd2), valence=attrs['bond_type']) G_new.add_edge(str(nd1), str(nd2)) return G_new # gedlibpy.restart_env() gedlibpy.add_nx_graph(convertGraph(g1), "") gedlibpy.add_nx_graph(convertGraph(g2), "") listID = gedlibpy.get_all_graph_ids() gedlibpy.set_edit_cost(cost, edit_cost_constant=edit_cost_constant) gedlibpy.init() gedlibpy.set_method(method, "") gedlibpy.init_method() g = listID[0] h = listID[1] if stabilizer == None: gedlibpy.run_method(g, h) pi_forward = gedlibpy.get_forward_map(g, h) pi_backward = gedlibpy.get_backward_map(g, h) upper = gedlibpy.get_upper_bound(g, h) lower = gedlibpy.get_lower_bound(g, h) elif stabilizer == 'min': upper = np.inf for itr in range(repeat): gedlibpy.run_method(g, h) upper_tmp = gedlibpy.get_upper_bound(g, h) if upper_tmp < upper: upper = upper_tmp pi_forward = gedlibpy.get_forward_map(g, h) pi_backward = gedlibpy.get_backward_map(g, h) lower = gedlibpy.get_lower_bound(g, h) if upper == 0: break dis = upper # make the map label correct (label remove map as np.inf) nodes1 = [n for n in g1.nodes()] nodes2 = [n for n in g2.nodes()] nb1 = nx.number_of_nodes(g1) nb2 = nx.number_of_nodes(g2) pi_forward = [nodes2[pi] if pi < nb2 else np.inf for pi in pi_forward] pi_backward = [nodes1[pi] if pi < nb1 else np.inf for pi in pi_backward] return dis, pi_forward, pi_backward