예제 #1
0
def test_permute(inplace):
    nxg = nx.Graph(title='Simple')
    f = [np.pi, np.e, np.sqrt(2), -1.0, 2.0]
    nxg.add_node(0, f=f[0])
    nxg.add_node(1, f=f[1])
    nxg.add_node(2, f=f[2])
    nxg.add_node(3, f=f[3])
    nxg.add_node(4, f=f[4])
    nxg.add_edge(0, 1, h=f[0] * f[1])
    nxg.add_edge(0, 2, h=f[0] * f[2])
    nxg.add_edge(0, 4, h=f[0] * f[4])
    nxg.add_edge(1, 2, h=f[1] * f[2])
    nxg.add_edge(1, 3, h=f[1] * f[3])
    nxg.add_edge(2, 3, h=f[2] * f[3])
    nxg.add_edge(3, 4, h=f[3] * f[4])

    g = Graph.from_networkx(nxg)

    for perm in it.permutations(range(len(g.nodes))):
        g1 = g.copy(deep=True)
        g2 = g1.permute(perm, inplace=inplace)

        if inplace:
            assert (g1 is g2)

        m = {idx: i for i, idx in enumerate(g2.nodes['!i'])}
        for i in range(len(g2.edges)):
            i1 = m[g2.edges['!i'][i]]
            i2 = m[g2.edges['!j'][i]]
            assert (g2.edges.h[i] == pytest.approx(g2.nodes.f[i1] *
                                                   g2.nodes.f[i2]))
예제 #2
0
def test_copy(deep):
    nxg = nx.Graph(title='Simple')
    nxg.add_node(0, f=0, g='a')
    nxg.add_node(1, f=1, g='b')
    nxg.add_node(2, f=2, g='c')
    nxg.add_edge(0, 1, h=1.0)
    nxg.add_edge(0, 2, h=2.0)

    g1 = Graph.from_networkx(nxg)
    g1.custom_attributes = (1.5, 'hello', False)
    g2 = g1.copy(deep=deep)
    assert (hasattr(g2, 'custom_attributes'))
    assert (g1.custom_attributes == g2.custom_attributes)

    assert (g1.title == g2.title)
    assert (len(g1.nodes) == len(g2.nodes))
    assert (len(g1.edges) == len(g2.edges))
    for n1, n2 in zip(g1.nodes.rows(), g2.nodes.rows()):
        assert (n1 == n2)
    for e1, e2 in zip(g1.edges.rows(), g2.edges.rows()):
        assert (e1 == e2)

    # Changes in one graph should reflect in its shallow copy, but should not
    # affect its deep copy.
    for i in range(len(g1.nodes)):
        g1.nodes.f[i] += 10.0
    for n1, n2 in zip(g1.nodes.rows(), g2.nodes.rows()):
        if deep:
            assert (n1 != n2)
        else:
            assert (n1 == n2)
예제 #3
0
def test_attribute_consistency_from_networkx():
    nxg1 = nx.Graph(title='H2O')
    nxg1.add_node(0, label1=3.14)
    nxg1.add_node(1, label2=True)

    with pytest.raises(TypeError):
        Graph.from_networkx(nxg1)

    nxg2 = nx.Graph(title='H2O')
    nxg2.add_node(0)
    nxg2.add_node(1)
    nxg2.add_node(2)
    nxg2.add_edge(0, 1, weight=1)
    nxg2.add_edge(0, 2, multi=2)

    with pytest.raises(TypeError):
        Graph.from_networkx(nxg2)
예제 #4
0
파일: test_rcm.py 프로젝트: yhtang/GraphDot
def test_rcm_fancy_graphs(n, gen):
    nxg = gen(n)
    if nxg.number_of_edges() > 0:
        g = Graph.from_networkx(nxg)
        p = rcm(g)
        assert (np.min(p) == 0)
        assert (np.max(p) == n - 1)
        assert (len(np.unique(p)) == n)
예제 #5
0
def test_graph_cache():
    G = [Graph.from_networkx(nx.cycle_graph(4)) for _ in range(2)]
    backend = CUDABackend()
    i1, cached1 = backend._register_graph(G[0])
    i2, cached2 = backend._register_graph(G[0])
    i3, cached3 = backend._register_graph(G[1])
    assert (i1 == i2)
    assert (cached1 is cached2)
    assert (i1 != i3)
    assert (cached1 is not cached3)
예제 #6
0
파일: test_pbr.py 프로젝트: yhtang/GraphDot
def test_rcm_fancy_graphs(n, gen):
    nxg = gen(n)
    if nxg.number_of_edges() > 0:
        g = Graph.from_networkx(nxg)
        p = pbr(g)
        assert(np.min(p) == 0)
        assert(np.max(p) == n - 1)
        assert(len(np.unique(p)) == n)

        g_perm = g.permute(p)
        assert(n_tiles(g.adjacency_matrix) >= n_tiles(g_perm.adjacency_matrix))
예제 #7
0
def test_empty_from_networkx():
    nxg = nx.Graph(title='Null')

    G = Graph.from_networkx(nxg)

    for g in [G, eval(repr(G).strip('><'))]:
        assert (g.title == 'Null')
        assert (len(g.nodes) == 0)
        assert (len(g.nodes.columns) == 0)
        assert (len(g.edges) == 0)
        assert (len(g.edges.columns) == 1)  # +1 for the hidden edge index
예제 #8
0
def test_adjacency_matrix_full():
    for n in [2, 3, 5, 7, 8, 11, 13, 16, 17, 23, 25]:
        g = Graph.from_networkx(nx.complete_graph(n))
        A = g.adjacency_matrix.todense()
        assert (A.shape[0] == n)
        assert (A.shape[1] == n)
        for i in range(n):
            for j in range(n):
                if i == j:
                    assert (A[i, j] == 0)
                else:
                    assert (A[i, j] == 1)
예제 #9
0
def test_adjacency_matrix_regular():
    for d in [2, 3, 4]:
        for n in [8, 12, 16]:
            g = Graph.from_networkx(nx.random_regular_graph(d, n))
            A = g.adjacency_matrix.todense()
            assert (A.shape[0] == n)
            assert (A.shape[1] == n)
            D = A.sum(axis=0)
            for deg in D.flat:
                assert (deg == d)
            for i in range(n):
                for j in range(n):
                    assert (A[i, j] == A[j, i])
예제 #10
0
def test_graph_cache_invalidation(inplace):
    G = [Graph.from_networkx(nx.cycle_graph(4)) for _ in range(2)]
    backend = CUDABackend()
    cached = [backend._register_graph(g) for g in G]

    if inplace:
        Graph.unify_datatype(G, inplace=inplace)
        H = G
    else:
        H = Graph.unify_datatype(G, inplace=inplace)
    for h in H:
        _, h_cached = backend._register_graph(h)
        for _, g_cached in cached:
            assert (h_cached is not g_cached)
예제 #11
0
def test_weighted_from_networkx():
    nxg = nx.Graph(title='Simple')
    nxg.add_node(0)
    nxg.add_node(1)
    nxg.add_edge(0, 1, w=1.0)

    G = Graph.from_networkx(nxg, weight='w')

    for g in [G, eval(repr(G).strip('><'))]:
        assert (g.title == 'Simple')
        assert (len(g.nodes) == 2)
        assert (len(g.nodes.columns) == 0)
        assert (len(g.edges) == 1)
        assert (len(g.edges.columns) == 2)  # +2 for edge index and weight
        assert ('!ij' in g.edges.columns)
        assert ('!w' in g.edges.columns)
예제 #12
0
def test_molecule_from_networkx():
    nxg = nx.Graph(title='H2O')
    nxg.add_node('O1', charge=1, conjugate=False, mass=8.0)
    nxg.add_node('H1', charge=-1, conjugate=True, mass=1.0)
    nxg.add_node('H2', charge=2, conjugate=True, mass=1.0)
    nxg.add_edge('O1', 'H1', order=1, length=0.5, weight=3.2)
    nxg.add_edge('O1', 'H2', order=2, length=1.0, weight=0.5)

    G = Graph.from_networkx(nxg)

    for g in [G, eval(repr(G).strip('><'))]:
        assert (g.title == 'H2O')
        assert (len(g.nodes) == 3)
        assert (len(g.nodes.columns) == 3)
        assert (len(g.edges) == 2)
        assert (len(g.edges.columns) == 3 + 1)  # +1 for the hidden edge index
예제 #13
0
def test_simple_from_networkx():
    nxg = nx.Graph(title='Simple')
    nxg.add_node(0)
    nxg.add_node('c')
    nxg.add_node('X')
    nxg.add_node(3.14)
    nxg.add_edge(0, 'c')
    nxg.add_edge(3.14, 'X')

    G = Graph.from_networkx(nxg)

    for g in [G, eval(repr(G).strip('><'))]:
        assert (g.title == 'Simple')
        assert (len(g.nodes) == 4)
        assert (len(g.nodes.columns) == 0)
        assert (len(g.edges) == 2)
        assert (len(g.edges.columns) == 1)  # +1 for the hidden edge index
예제 #14
0
def test_large_from_networkx():
    nxg = nx.Graph(title='Large')
    nn = 1000
    ne = 100000
    np.random.seed(0)
    for i in range(nn):
        nxg.add_node(i, label=np.random.randn())
    for _ in range(ne):
        i, j = np.random.randint(nn, size=2)
        nxg.add_edge(i, j, weight=np.random.rand())

    G = Graph.from_networkx(nxg)

    for g in [G, eval(repr(G).strip('><'))]:
        assert (g.title == 'Large')
        assert (len(g.nodes) == nn)
        assert (len(g.edges) <= ne)
예제 #15
0
def test_adjacency_matrix_cycle():
    for n in [2, 3, 5, 7, 8, 11, 13, 16, 17, 23, 25]:
        g = Graph.from_networkx(nx.cycle_graph(n))
        A = g.adjacency_matrix.todense()
        assert (A.shape[0] == n)
        assert (A.shape[1] == n)
        for i in range(n):
            for j in range(n):
                d = i - j
                if d > n / 2:
                    d -= n
                elif d < -n / 2:
                    d += n
                if abs(d) == 1:
                    assert (A[i, j] == 1)
                else:
                    assert (A[i, j] == 0)
예제 #16
0
 def fun(nxg):
     return Graph.from_networkx(nxg)
예제 #17
0
def test_laplacian(g):
    L = Graph.from_networkx(g).laplacian.todense()
    for s in L.sum(axis=0):
        assert (s == pytest.approx(0))
    w = np.linalg.eigvalsh(L)
    assert (w.min() == pytest.approx(0))
예제 #18
0
파일: test_rcm.py 프로젝트: yhtang/GraphDot
def test_rcm_grid_graph(n):
    g = Graph.from_networkx(nx.grid_graph((n, ), periodic=False))
    p = rcm(g)
    for i, j in enumerate(p[-1::-1]):
        assert (i == j)
예제 #19
0
파일: test_rcm.py 프로젝트: yhtang/GraphDot
def test_rcm_complete_graph(n):
    g = Graph.from_networkx(nx.complete_graph(n))
    p = rcm(g)
    for i, j in enumerate(p[-1::-1]):
        assert (i == j)