test_traversal.py 3.31 KB
Newer Older
GaiYu0's avatar
GaiYu0 committed
1
2
3
4
5
6
7
8
9
import random
import sys
import time

import dgl
import networkx as nx
import numpy as np
import scipy.sparse as sp
import torch as th
10
import utils as U
GaiYu0's avatar
GaiYu0 committed
11

Gan Quan's avatar
Gan Quan committed
12
13
import itertools

GaiYu0's avatar
GaiYu0 committed
14
15
np.random.seed(42)

Gan Quan's avatar
Gan Quan committed
16
17
18
def toset(x):
    return set(x.tolist())

19
def test_bfs_nodes(n=1000):
Gan Quan's avatar
Gan Quan committed
20
    g_nx = nx.random_tree(n)
GaiYu0's avatar
GaiYu0 committed
21
    g = dgl.DGLGraph()
Gan Quan's avatar
Gan Quan committed
22
    g.from_networkx(g_nx)
GaiYu0's avatar
GaiYu0 committed
23
24
25
26
27

    src = random.choice(range(n))

    edges = nx.bfs_edges(g_nx, src)
    layers_nx = [set([src])]
Gan Quan's avatar
Gan Quan committed
28
    edges_nx = []
GaiYu0's avatar
GaiYu0 committed
29
    frontier = set()
Gan Quan's avatar
Gan Quan committed
30
    edge_frontier = set()
GaiYu0's avatar
GaiYu0 committed
31
32
33
    for u, v in edges:
        if u in layers_nx[-1]:
            frontier.add(v)
Gan Quan's avatar
Gan Quan committed
34
            edge_frontier.add(g.edge_id(u, v))
GaiYu0's avatar
GaiYu0 committed
35
36
        else:
            layers_nx.append(frontier)
Gan Quan's avatar
Gan Quan committed
37
            edges_nx.append(edge_frontier)
GaiYu0's avatar
GaiYu0 committed
38
            frontier = set([v])
Gan Quan's avatar
Gan Quan committed
39
            edge_frontier = set([g.edge_id(u, v)])
GaiYu0's avatar
GaiYu0 committed
40
    layers_nx.append(frontier)
Gan Quan's avatar
Gan Quan committed
41
    edges_nx.append(edge_frontier)
GaiYu0's avatar
GaiYu0 committed
42

43
44
    layers_dgl = dgl.bfs_nodes_generator(g, src)

GaiYu0's avatar
GaiYu0 committed
45
46
47
    assert len(layers_dgl) == len(layers_nx)
    assert all(toset(x) == y for x, y in zip(layers_dgl, layers_nx))

Gan Quan's avatar
Gan Quan committed
48
49
50
51
52
    edges_dgl = dgl.bfs_edges_generator(g, src)

    assert len(edges_dgl) == len(edges_nx)
    assert all(toset(x) == y for x, y in zip(edges_dgl, edges_nx))

53
def test_topological_nodes(n=1000):
GaiYu0's avatar
GaiYu0 committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    g = dgl.DGLGraph()
    a = sp.random(n, n, 10 / n, data_rvs=lambda n: np.ones(n))
    b = sp.tril(a, -1).tocoo()
    g.from_scipy_sparse_matrix(b)

    layers_dgl = dgl.topological_nodes_generator(g)

    adjmat = g.adjacency_matrix()
    def tensor_topo_traverse():
        n = g.number_of_nodes()
        mask = th.ones((n, 1))
        degree = th.spmm(adjmat, mask)
        while th.sum(mask) != 0.:
            v = (degree == 0.).float()
            v = v * mask
            mask = mask - v
            frontier = th.squeeze(th.squeeze(v).nonzero(), 1)
            yield frontier
            degree -= th.spmm(adjmat, v)

    layers_spmv = list(tensor_topo_traverse())

    assert len(layers_dgl) == len(layers_spmv)
    assert all(toset(x) == toset(y) for x, y in zip(layers_dgl, layers_spmv))

DFS_LABEL_NAMES = ['forward', 'reverse', 'nontree']
def test_dfs_labeled_edges(n=1000, example=False):
    dgl_g = dgl.DGLGraph()
Gan Quan's avatar
Gan Quan committed
82
83
    dgl_g.add_nodes(6)
    dgl_g.add_edges([0, 1, 0, 3, 3], [1, 2, 2, 4, 5])
GaiYu0's avatar
GaiYu0 committed
84
    dgl_edges, dgl_labels = dgl.dfs_labeled_edges_generator(
Gan Quan's avatar
Gan Quan committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
            dgl_g, [0, 3], has_reverse_edge=True, has_nontree_edge=True)
    dgl_edges = [toset(t) for t in dgl_edges]
    dgl_labels = [toset(t) for t in dgl_labels]

    g1_solutions = [
            # edges           labels
            [[0, 1, 1, 0, 2], [0, 0, 1, 1, 2]],
            [[2, 2, 0, 1, 0], [0, 1, 0, 2, 1]],
    ]
    g2_solutions = [
            # edges        labels
            [[3, 3, 4, 4], [0, 1, 0, 1]],
            [[4, 4, 3, 3], [0, 1, 0, 1]],
    ]

    def combine_frontiers(sol):
        es, ls = zip(*sol)
        es = [set(i for i in t if i is not None)
              for t in itertools.zip_longest(*es)]
        ls = [set(i for i in t if i is not None)
              for t in itertools.zip_longest(*ls)]
        return es, ls

    for sol_set in itertools.product(g1_solutions, g2_solutions):
        es, ls = combine_frontiers(sol_set)
        if es == dgl_edges and ls == dgl_labels:
            break
    else:
        assert False

GaiYu0's avatar
GaiYu0 committed
115
116
117
118
119

if __name__ == '__main__':
    test_bfs_nodes()
    test_topological_nodes()
    test_dfs_labeled_edges()