test_graph.py 4.19 KB
Newer Older
1
2
3
4
import time
import math
import numpy as np
import scipy.sparse as sp
5
import networkx as nx
6
import dgl
7
import backend as F
8
9
10
11
12

def test_graph_creation():
    g = dgl.DGLGraph()
    # test add nodes with data
    g.add_nodes(5)
13
14
15
16
17
    g.add_nodes(5, {'h' : F.ones((5, 2))})
    ans = F.cat([F.zeros((5, 2)), F.ones((5, 2))], 0)
    assert F.allclose(ans, g.ndata['h'])
    g.ndata['w'] = 2 * F.ones((10, 2))
    assert F.allclose(2 * F.ones((10, 2)), g.ndata['w'])
18
19
    # test add edges with data
    g.add_edges([2, 3], [3, 4])
20
21
22
    g.add_edges([0, 1], [1, 2], {'m' : F.ones((2, 2))})
    ans = F.cat([F.zeros((2, 2)), F.ones((2, 2))], 0)
    assert F.allclose(ans, g.edata['m'])
23
24
25
    # test clear and add again
    g.clear()
    g.add_nodes(5)
26
27
    g.ndata['h'] = 3 * F.ones((5, 2))
    assert F.allclose(3 * F.ones((5, 2)), g.ndata['h'])
28

29
30
31
32
33
34
def test_create_from_elist():
    elist = [(2, 1), (1, 0), (2, 0), (3, 0), (0, 2)]
    g = dgl.DGLGraph(elist)
    for i, (u, v) in enumerate(elist):
        assert g.edge_id(u, v) == i
    # immutable graph
Minjie Wang's avatar
Minjie Wang committed
35
36
37
38
    # XXX: not enabled for pytorch
    #g = dgl.DGLGraph(elist, readonly=True)
    #for i, (u, v) in enumerate(elist):
    #    assert g.edge_id(u, v) == i
39

40
def test_adjmat_cache():
41
42
43
44
45
46
    n = 1000
    p = 10 * math.log(n) / n
    a = sp.random(n, n, p, data_rvs=lambda n: np.ones(n))
    g = dgl.DGLGraph(a)
    # the first call should contruct the adj
    t0 = time.time()
47
    adj1 = g.adjacency_matrix()
48
49
50
    dur1 = time.time() - t0
    # the second call should be cached and should be very fast
    t0 = time.time()
51
    adj2 = g.adjacency_matrix()
52
    dur2 = time.time() - t0
53
54
    print('first time {}, second time {}'.format(dur1, dur2))
    assert dur2 < dur1
55
56
57
58
59
60
61
62
63
64
65
66
    assert id(adj1) == id(adj2)
    # different arg should result in different cache
    adj3 = g.adjacency_matrix(transpose=True)
    assert id(adj3) != id(adj2)
    # manually clear the cache
    g.clear_cache()
    adj35 = g.adjacency_matrix()
    assert id(adj35) != id(adj2)
    # mutating the graph should invalidate the cache
    g.add_nodes(10)
    adj4 = g.adjacency_matrix()
    assert id(adj4) != id(adj35)
67
68
69
70
71
72
73
74
75

def test_incmat():
    g = dgl.DGLGraph()
    g.add_nodes(4)
    g.add_edge(0, 1) # 0
    g.add_edge(0, 2) # 1
    g.add_edge(0, 3) # 2
    g.add_edge(2, 3) # 3
    g.add_edge(1, 1) # 4
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    inc_in = F.sparse_to_numpy(g.incidence_matrix('in'))
    inc_out = F.sparse_to_numpy(g.incidence_matrix('out'))
    inc_both = F.sparse_to_numpy(g.incidence_matrix('both'))
    print(inc_in)
    print(inc_out)
    print(inc_both)
    assert np.allclose(
            inc_in,
            np.array([[0., 0., 0., 0., 0.],
                      [1., 0., 0., 0., 1.],
                      [0., 1., 0., 0., 0.],
                      [0., 0., 1., 1., 0.]]))
    assert np.allclose(
            inc_out,
            np.array([[1., 1., 1., 0., 0.],
                      [0., 0., 0., 0., 1.],
                      [0., 0., 0., 1., 0.],
                      [0., 0., 0., 0., 0.]]))
    assert np.allclose(
            inc_both,
            np.array([[-1., -1., -1., 0., 0.],
                      [1., 0., 0., 0., 0.],
                      [0., 1., 0., -1., 0.],
                      [0., 0., 1., 1., 0.]]))
100

101
def test_incmat_cache():
102
    n = 1000
103
    p = 10 * math.log(n) / n
104
105
    a = sp.random(n, n, p, data_rvs=lambda n: np.ones(n))
    g = dgl.DGLGraph(a)
106
    # the first call should contruct the inc
107
    t0 = time.time()
108
    inc1 = g.incidence_matrix("in")
109
110
111
    dur1 = time.time() - t0
    # the second call should be cached and should be very fast
    t0 = time.time()
112
    inc2 = g.incidence_matrix("in")
113
    dur2 = time.time() - t0
114
    print('first time {}, second time {}'.format(dur1, dur2))
115
    assert dur2 < dur1
116
117
    assert id(inc1) == id(inc2)
    # different arg should result in different cache
Minjie Wang's avatar
Minjie Wang committed
118
    inc3 = g.incidence_matrix("both")
119
120
121
122
123
124
125
126
127
    assert id(inc3) != id(inc2)
    # manually clear the cache
    g.clear_cache()
    inc35 = g.incidence_matrix("in")
    assert id(inc35) != id(inc2)
    # mutating the graph should invalidate the cache
    g.add_nodes(10)
    inc4 = g.incidence_matrix("in")
    assert id(inc4) != id(inc35)
128
129

if __name__ == '__main__':
130
    test_graph_creation()
131
    test_create_from_elist()
132
    test_adjmat_cache()
133
    test_incmat()
134
    test_incmat_cache()