Example #1
0
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
Example #2
0
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
Example #3
0
def test_manhattan_norm(coord1, coord2, epsilon):
    node0 = Node(0, LabelNodeLetter(*coord1))
    node1 = Node(1, LabelNodeLetter(*coord2))

    edit_cost = EditCostLetter(1., 1., 1., 1., 'manhattan')
    result = edit_cost.cost_substitute_node(node0, node1)
    arr1 = np.array(coord1)
    arr2 = np.array(coord2)

    assert abs(result - np.linalg.norm(arr1 - arr2, 1)) < epsilon
    assert result == np.linalg.norm(arr1 - arr2, 1)
Example #4
0
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)
Example #5
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
Example #6
0
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
Example #7
0
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
Example #8
0
def test_add_node(num_nodes):
    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, 1))
        nodes.append(tmp_node)
        my_graph.add_node(tmp_node)

    assert my_graph.get_nodes() == nodes
    assert len(my_graph) == num_nodes
Example #9
0
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)
Example #10
0
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
Example #11
0
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)
Example #12
0
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
Example #13
0
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
Example #14
0
def my_node():
    my_node = Node(1, LabelNodeLetter(1., 2.))
    my_node.label.get_attributes()
    return my_node
Example #15
0
# -*- coding: utf-8 -*-

import pytest

from graph_pkg.graph.label.label_node_letter import LabelNodeLetter
from graph_pkg.graph.node import Node


@pytest.fixture()
def my_node():
    my_node = Node(1, LabelNodeLetter(1., 2.))
    my_node.label.get_attributes()
    return my_node


def test_simple_node(my_node):
    assert my_node.idx == 1
    assert my_node.label.get_attributes() == (1., 2.)


@pytest.mark.parametrize('node1, node2, expected', [
    (Node(1, LabelNodeLetter(1., 2.)), Node(1, LabelNodeLetter(1., 2.)), True),
    (Node(1, LabelNodeLetter(1., 2.)), Node(3, LabelNodeLetter(1.,
                                                               2.)), False),
    (Node(1, LabelNodeLetter(1., 2.)), Node(1, LabelNodeLetter(3, 5)), False)
])
def test_equality_node(node1, node2, expected):
    equality = node1 == node2

    assert equality == expected
Example #16
0
def test_label_letter(in_args):
    label = LabelNodeLetter(*in_args)

    assert label.get_attributes() == in_args
Example #17
0
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)}'
Example #18
0
        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


@pytest.mark.parametrize('num_nodes, nodes, edge, expected_error_msg',
                         [(5, [], Edge(7, 1, LabelEdge(0)), 'The starting node 7 does not exist!'),
                          (5, [Node(0, LabelNodeLetter(1, 1))], Edge(0, 23, LabelEdge(0)), 'The ending node 23 does not exist!')
                          ])
def test_insert_invalid_edge(num_nodes, nodes, edge, expected_error_msg):
    my_graph = Graph(f'gr{num_nodes}', f'gr{num_nodes}.gxl', num_nodes)

    for node in nodes:
        my_graph.add_node(node)

    with pytest.raises(AssertionError) as execinfo:
        my_graph.add_edge(edge)

    error_msg = execinfo.value.args[0]

    assert error_msg == expected_error_msg

@pytest.mark.parametrize('num_nodes, expected_matrix',