def test_betweenness_by_hand(): graph = Graph('gr', 'gr.xml', 4) graph.add_node(Node(0, LabelNodeLetter(0, 0))) graph.add_node(Node(1, LabelNodeLetter(0, 0))) graph.add_node(Node(2, LabelNodeLetter(0, 0))) graph.add_node(Node(3, LabelNodeLetter(0, 0))) graph.add_edge(Edge(0, 1, LabelEdge(0))) graph.add_edge(Edge(1, 2, LabelEdge(0))) graph.add_edge(Edge(2, 3, LabelEdge(0))) betweenness = Betweenness() results = betweenness.calc_centrality_score(graph) results = np.asarray(results) graph2 = nx.Graph() graph2.add_node(1) graph2.add_node(2) graph2.add_node(3) graph2.add_node(4) graph2.add_edge(1, 2) graph2.add_edge(2, 3) graph2.add_edge(3, 4) expected_dict = nx.betweenness_centrality(graph2, normalized=False) expected = np.array([val for _, val in expected_dict.items()]) print(results) assert np.linalg.norm(results - expected) < 1e-6
def test_pagerank_by_hand(): graph = Graph('gr', 'gr.xml', 4) graph.add_node(Node(0, LabelNodeLetter(0, 0))) graph.add_node(Node(1, LabelNodeLetter(0, 0))) graph.add_node(Node(2, LabelNodeLetter(0, 0))) graph.add_node(Node(3, LabelNodeLetter(0, 0))) graph.add_edge(Edge(0, 1, LabelEdge(0))) graph.add_edge(Edge(1, 2, LabelEdge(0))) graph.add_edge(Edge(2, 3, LabelEdge(0))) pagerank = PageRank() results = pagerank.calc_centrality_score(graph) results = np.asarray(results) graph2 = nx.Graph() graph2.add_node(1) graph2.add_node(2) graph2.add_node(3) graph2.add_node(4) graph2.add_edge(1, 2) graph2.add_edge(2, 3) graph2.add_edge(3, 4) expected = np.array([val for _, val in nx.pagerank_scipy(graph2).items()]) print(results) assert np.linalg.norm(results - expected) < 1e-6
def test_mutagenicity_with_deleted_nodes(mutagenicity_graphs, dataframe_mutagenicity, graph_name_source_target): gr_name_src, gr_name_trgt = ['mutagen/' + name for name in graph_name_source_target] graph_name_source, graph_name_target = graph_name_source_target graph_source = [graph for graph in mutagenicity_graphs if graph.name == graph_name_source][0] graph_target = [graph for graph in mutagenicity_graphs if graph.name == graph_name_target][0] cst_cost_node = 11.0 cst_cost_edge = 1.1 ged = GED(EditCostMutagenicity(cst_cost_node, cst_cost_node, cst_cost_edge, cst_cost_edge, 'dirac')) # Reproduce the source graph with more nodes new_gr_src = Graph(gr_name_src, 'gr.xls', len(graph_source)+2) for node in graph_source.nodes: new_gr_src.add_node(node) for idx, edges in graph_source.get_edges().items(): for edge in edges: if edge is None: continue new_gr_src.add_edge(edge) new_gr_src.add_node(Node(len(graph_source), LabelNodeMutagenicity('C'))) new_gr_src.add_node(Node(len(graph_source)+1, LabelNodeMutagenicity('N'))) # Add random Edges for _ in range(4): new_gr_src.add_edge(Edge(len(graph_source), random.randint(0, len(graph_source)-1), LabelEdge(0))) for _ in range(6): new_gr_src.add_edge(Edge(len(graph_source) + 1, random.randint(0, len(graph_source)), LabelEdge(0))) new_gr_src.remove_node_by_idx(len(graph_source)) new_gr_src.remove_node_by_idx(len(graph_source)) results = ged.compute_edit_distance(new_gr_src, graph_target) expected = dataframe_mutagenicity.loc[gr_name_src, gr_name_trgt] # import numpy as np # np.savetxt(f'_c_{"X".join(graph_name_source_target)}.csv', np.asarray(ged.C), fmt='%10.3f', delimiter=';') # np.savetxt(f'c_star_{"X".join(graph_name_source_target)}.csv', np.asarray(ged.C_star), fmt='%10.3f', delimiter=';') print(f'###### diff {results - expected}') print(f'{graph_name_source_target}: new dist {results} - old dist {expected}') print(f'exp {expected}') assert results == expected
def test_remove_node(num_nodes, idx_to_remove, expected_adj): my_graph = Graph(f'gr{num_nodes}', f'gr{num_nodes}.gxl', num_nodes) nodes = [] for i in range(num_nodes): tmp_node = Node(i, LabelNodeLetter(1+i, 1)) nodes.append(tmp_node) my_graph.add_node(tmp_node) for idx_start, idx_end in combinations(range(num_nodes), 2): tmp_edge = Edge(idx_start, idx_end, LabelEdge(0)) my_graph.add_edge(tmp_edge) my_graph.remove_node_by_idx(idx_to_remove) # nodes.pop(0) # expected edges # expected_adjacency_mat = np.array([[0, 1, 1], # [1, 0, 1], # [1, 1, 0]]) print(f'--{nodes}') print(my_graph.get_nodes()) # assert my_graph.get_nodes() == nodes assert len(my_graph) == num_nodes - 1
def my_edge(): idx_start = 1 idx_end = 5 label = LabelEdge(0) edge = Edge(idx_start, idx_end, label) return edge
def test_simple_edge(idx_start, idx_end, label_weight): label = LabelEdge(label_weight) my_edge = Edge(idx_start, idx_end, label) assert my_edge.idx_node_start == idx_start assert my_edge.idx_node_end == idx_end assert my_edge.weight == label assert str( my_edge) == f'Edge: {idx_start} --> {idx_end}, weight {label_weight}'
def test_3_nodes_1_node(ged): gr_src = Graph('gr1', 'gr1.gxl', 3) gr_trgt = Graph('gr2', 'gr2.gxl', 1) gr_src.add_node(Node(0, LabelNodeAIDS('O', 1, 1, 2., 2.))) gr_src.add_node(Node(1, LabelNodeAIDS('C', 1, 1, 2., 2.))) gr_src.add_node(Node(2, LabelNodeAIDS('O', 1, 1, 2., 2.))) gr_src.add_edge(Edge(0, 1, LabelEdge(0))) gr_src.add_edge(Edge(1, 2, LabelEdge(0))) gr_trgt.add_node(Node(0, LabelNodeAIDS('H', 1, 1, 2., 2.))) dist = ged.compute_edit_distance(gr_src, gr_trgt) print(ged.C.base) print(ged.C_star.base) expected_dist = 4.6 assert round(dist, 2) == expected_dist
def test_copy_graph(my_graph): my_graph.add_node(Node(0, LabelNodeLetter(1, 1))) my_graph.add_node(Node(1, LabelNodeLetter(2, 3))) my_graph.add_edge(Edge(0, 1, LabelEdge(0))) import copy new_graph = copy.deepcopy(my_graph) my_graph.remove_node_by_idx(0) print(new_graph) print(my_graph)
def test_out_in_degrees(num_nodes, expected_matrix): my_graph = Graph(f'gr{num_nodes}', f'gr{num_nodes}.gxl', num_nodes) for i in range(num_nodes): tmp_node = Node(i, LabelNodeLetter(i, i)) my_graph.add_node(tmp_node) for idx_start, idx_end in combinations(range(num_nodes), 2): tmp_edge = Edge(idx_start, idx_end, LabelEdge(0)) my_graph.add_edge(tmp_edge) assert np.array_equal(np.asarray(my_graph.out_degrees()), expected_matrix)
def define_graphs(): n, m = 4, 3 graph_source = Graph('gr_source', 'gr_source.gxl', n) graph_target = Graph('gr_target', 'gr_targe.gxl', m) # Init graph source: add nodes and edges graph_source.add_node(Node(0, LabelNodeLetter(1, 0))) graph_source.add_node(Node(1, LabelNodeLetter(2, 0))) graph_source.add_node(Node(2, LabelNodeLetter(1, 0))) graph_source.add_node(Node(3, LabelNodeLetter(3, 0))) graph_source.add_edge(Edge(0, 1, LabelEdge(0))) graph_source.add_edge(Edge(1, 2, LabelEdge(0))) graph_source.add_edge(Edge(1, 3, LabelEdge(0))) graph_source.add_edge(Edge(2, 3, LabelEdge(0))) # Init graph target: add nodes and edges graph_target.add_node(Node(0, LabelNodeLetter(3, 0))) graph_target.add_node(Node(1, LabelNodeLetter(2, 0))) graph_target.add_node(Node(2, LabelNodeLetter(2, 0))) graph_target.add_edge(Edge(0, 1, LabelEdge(0))) graph_target.add_edge(Edge(1, 2, LabelEdge(0))) return graph_source, graph_target
def test_pagerank_by_hand_big(): graph = Graph('gr', 'gr.xml', 6) graph2 = nx.Graph() for i in range(6): graph.add_node(Node(i, LabelNodeLetter(0, 0))) graph2.add_node(i) ### Add edge to graph graph.add_edge(Edge(0, 1, LabelEdge(0))) graph.add_edge(Edge(0, 2, LabelEdge(0))) graph.add_edge(Edge(0, 4, LabelEdge(0))) graph.add_edge(Edge(0, 5, LabelEdge(0))) graph.add_edge(Edge(1, 2, LabelEdge(0))) graph.add_edge(Edge(2, 3, LabelEdge(0))) graph.add_edge(Edge(2, 5, LabelEdge(0))) graph.add_edge(Edge(3, 4, LabelEdge(0))) graph.add_edge(Edge(3, 5, LabelEdge(0))) pagerank = PageRank() results = pagerank.calc_centrality_score(graph) results = np.asarray(results) ### Add edge to nx.graph graph2.add_edge(0, 1) graph2.add_edge(0, 2) graph2.add_edge(0, 4) graph2.add_edge(0, 5) graph2.add_edge(1, 2) graph2.add_edge(2, 3) graph2.add_edge(2, 5) graph2.add_edge(3, 4) graph2.add_edge(3, 5) expected = np.array([val for _, val in nx.pagerank_scipy(graph2).items()]) assert np.linalg.norm(results - expected) < 1e-6
def test_add_clique_edge(num_nodes, expected_edges): my_graph = Graph(f'gr{num_nodes}', f'gr{num_nodes}.gxl', num_nodes) for i in range(num_nodes): tmp_node = Node(i, LabelNodeLetter(i, i)) my_graph.add_node(tmp_node) for idx_start, idx_end in combinations(range(num_nodes), 2): tmp_edge = Edge(idx_start, idx_end, LabelEdge(0)) my_graph.add_edge(tmp_edge) assert my_graph.get_edges() == expected_edges assert my_graph.has_edge(0, num_nodes - 1) == True assert my_graph.has_edge(num_nodes - 1, 0) == True assert my_graph.has_edge(0, num_nodes + 1) == False
def test_adjacency_matrix(num_nodes, expected_matrix): my_graph = Graph(f'gr{num_nodes}', f'gr{num_nodes}.gxl', num_nodes) for i in range(num_nodes): tmp_node = Node(i, LabelNodeLetter(i, i)) my_graph.add_node(tmp_node) for idx_start, idx_end in combinations(range(num_nodes), 2): tmp_edge = Edge(idx_start, idx_end, LabelEdge(0)) my_graph.add_edge(tmp_edge) # transform memoryview to np.array # my_graph.adjacency_matrix.base # np.asarray(my_graph.adjacency_matrix) assert np.array_equal(np.asarray(my_graph.adjacency_matrix), expected_matrix)
def test_with_deleted_node(ged): gr_src = Graph('gr1', 'gr1.gxl', 3) gr_trgt = Graph('gr2', 'gr2.gxl', 3) gr_src = Graph('gr1', 'gr1.gxl', 5) gr_src.add_node(Node(0, LabelNodeAIDS('O', 1, 1, 2., 2.))) gr_src.add_node(Node(1, LabelNodeAIDS('C', 1, 1, 2., 2.))) gr_src.add_node(Node(2, LabelNodeAIDS('O', 1, 1, 2., 2.))) # print(gr_src) gr_src.add_node(Node(3, LabelNodeAIDS('Cl', 1, 1, 2., 2.))) gr_src.add_node(Node(4, LabelNodeAIDS('N', 1, 1, 2.4, 2.))) gr_src.add_edge(Edge(0, 3, LabelEdge(0))) gr_src.add_edge(Edge(1, 3, LabelEdge(0))) gr_src.add_edge(Edge(4, 2, LabelEdge(0))) gr_src.add_edge(Edge(3, 4, LabelEdge(0))) gr_src.remove_node_by_idx(4) gr_src.remove_node_by_idx(3) print(gr_src) gr_trgt.add_node(Node(0, LabelNodeAIDS('H', 1, 1, 2., 2.))) gr_trgt.add_node(Node(1, LabelNodeAIDS('Ca', 1, 1, 2., 2.))) gr_trgt.add_node(Node(2, LabelNodeAIDS('C', 1, 1, 2., 2.))) gr_trgt.add_edge(Edge(0, 1, LabelEdge(0))) gr_trgt.add_edge(Edge(1, 2, LabelEdge(0))) gr_trgt.add_edge(Edge(2, 0, LabelEdge(0))) dist = ged.compute_edit_distance(gr_src, gr_trgt) print(ged.C.base) print(ged.C_star.base) expected_dist = 4.7 print(dist) assert round(dist, 2) == expected_dist
def test_label_edge(in_args, expected): label = LabelEdge(in_args) assert label.get_attributes() == (expected, )
def test_label_NCI1(): expected = (3, ) label = LabelNodeNCI1(*expected) assert label.get_attributes() == expected @pytest.mark.parametrize('in_args', [(5., 6.), (1, 5.), (43., 4)]) def test_label_lettre_to_string(in_args): label = LabelNodeLetter(*in_args) assert str( label ) == f'Label attributes: {", ".join(str(float(element)) for element in in_args)}' @pytest.mark.parametrize( 'lbl_1, lbl_2, expected', [(LabelNodeLetter(1, 2), LabelNodeLetter(1, 2), True), (LabelNodeLetter(1., 2.), LabelEdge(0), False), (LabelEdge(0), LabelEdge(0), True), (LabelEdge(12), LabelEdge(2), False), (LabelNodeMutagenicity('C'), LabelNodeMutagenicity('C'), True), (LabelNodeMutagenicity('Cl'), LabelNodeMutagenicity('O'), False), (LabelNodeNCI1(3), LabelNodeNCI1(1), False), (LabelNodeNCI1(2), LabelNodeNCI1(2), True)]) def test_label_equality(lbl_1, lbl_2, expected): equality = lbl_1 == lbl_2 assert equality == expected
@pytest.fixture() def my_edge(): idx_start = 1 idx_end = 5 label = LabelEdge(0) edge = Edge(idx_start, idx_end, label) return edge @pytest.mark.parametrize( 'edge1, edge2, expected_result', [(Edge(1, 2, LabelEdge(0)), Edge(1, 2, LabelEdge(0)), True), (Edge(2, 1, LabelEdge(0)), Edge(1, 2, LabelEdge(0)), False), (Edge(1, 2, LabelEdge(0)), Edge(3, 4, LabelEdge(0)), False), (Edge(1, 2, LabelEdge(23)), Edge(1, 2, LabelEdge(1)), False)]) def test_edge_equality(edge1, edge2, expected_result): equality = edge1 == edge2 assert equality == expected_result @pytest.mark.parametrize('edge1, expected_edge', [ (Edge(1, 2, LabelEdge(0)), Edge(2, 1, LabelEdge(0))), (Edge(2, 1, LabelEdge(0)), Edge(1, 2, LabelEdge(0))), (Edge(3, 6, LabelEdge(3)), Edge(6, 3, LabelEdge(3))), ]) def test_edge_reverse(edge1, expected_edge):
(5, 8),]) def test_add_node_higher_than_num_nodes(num_nodes, error_idx): my_graph = Graph(f'gr{num_nodes}', f'gr{num_nodes}.gxl', num_nodes) tmp_node = Node(error_idx, LabelNodeLetter(1, 1)) with pytest.raises(AssertionError) as execinfo: my_graph.add_node(tmp_node) error_msg = execinfo.value.args[0] expected_error_msg = f'The idx of the node {error_idx} exceed the number of nodes {num_nodes} authorized!' assert error_msg == expected_error_msg @pytest.mark.parametrize('num_nodes, expected_edges', [(2, {0: [None, Edge(0, 1, LabelEdge(0))], 1: [Edge(1, 0, LabelEdge(0)), None]}), (3, {0: [None, Edge(0, 1, LabelEdge(0)), Edge(0, 2, LabelEdge(0))], 1: [Edge(1, 0, LabelEdge(0)), None, Edge(1, 2, LabelEdge(0))], 2: [Edge(2, 0, LabelEdge(0)), Edge(2, 1, LabelEdge(0)), None]}) ]) def test_add_clique_edge(num_nodes, expected_edges): my_graph = Graph(f'gr{num_nodes}', f'gr{num_nodes}.gxl', num_nodes) for i in range(num_nodes): tmp_node = Node(i, LabelNodeLetter(i, i)) my_graph.add_node(tmp_node) for idx_start, idx_end in combinations(range(num_nodes), 2): tmp_edge = Edge(idx_start, idx_end, LabelEdge(0)) my_graph.add_edge(tmp_edge)