Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #3
0
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