def test_filter(): g = DGLGraph() g.add_nodes(4) g.add_edges([0,1,2,3], [1,2,3,0]) n_repr = F.zeros((4, 5)) e_repr = F.zeros((4, 5)) n_repr[[1, 3]] = 1 e_repr[[1, 3]] = 1 g.ndata['a'] = n_repr g.edata['a'] = e_repr def predicate(r): return F.max(r.data['a'], 1) > 0 # full node filter n_idx = g.filter_nodes(predicate) assert set(F.zerocopy_to_numpy(n_idx)) == {1, 3} # partial node filter n_idx = g.filter_nodes(predicate, [0, 1]) assert set(F.zerocopy_to_numpy(n_idx)) == {1} # full edge filter e_idx = g.filter_edges(predicate) assert set(F.zerocopy_to_numpy(e_idx)) == {1, 3} # partial edge filter e_idx = g.filter_edges(predicate, [0, 1]) assert set(F.zerocopy_to_numpy(e_idx)) == {1}
def test_random_walk_with_restart(): edge_list = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 3), (3, 2), (2, 1), (1, 0)] seeds = [0, 1] max_nodes = 10 g = dgl.DGLGraph(edge_list) # test normal RWR traces = dgl.contrib.sampling.random_walk_with_restart( g, seeds, 0.2, max_nodes) assert len(traces) == len(seeds) for traces_per_seed in traces: total_nodes = 0 for t in traces_per_seed: total_nodes += len(t) trace_diff = np.diff(F.zerocopy_to_numpy(t), axis=-1) assert (np.abs(trace_diff) == 1).all() assert total_nodes >= max_nodes # test RWR with early stopping traces = dgl.contrib.sampling.random_walk_with_restart( g, seeds, 1, 100, max_nodes, 1) assert len(traces) == len(seeds) for traces_per_seed in traces: assert sum(len(t) for t in traces_per_seed) < 100 # test bipartite RWR traces = dgl.contrib.sampling.bipartite_single_sided_random_walk_with_restart( g, seeds, 0.2, max_nodes) assert len(traces) == len(seeds) for traces_per_seed in traces: for t in traces_per_seed: trace_diff = np.diff(F.zerocopy_to_numpy(t), axis=-1) assert (trace_diff % 2 == 0).all()
def test_send_recv_after_conversion(): # test send and recv after converting from a graph with edges g = generate_graph() # nx graph nxg = g.to_networkx(node_attrs=['h']) g1 = DGLGraph() # some random node and edges g1.add_nodes(4) g1.add_edges([1, 2], [2, 3]) g1.set_n_initializer(dgl.init.zero_initializer) g1.from_networkx(nxg, node_attrs=['h']) # sparse matrix row, col = g.all_edges() data = range(len(row)) n = g.number_of_nodes() a = sp.coo_matrix( (data, (F.zerocopy_to_numpy(row), F.zerocopy_to_numpy(col))), shape=(n, n)) g2 = DGLGraph() # some random node and edges g2.add_nodes(5) g2.add_edges([1, 2, 4], [2, 3, 0]) g2.set_n_initializer(dgl.init.zero_initializer) g2.from_scipy_sparse_matrix(a) g2.ndata['h'] = g.ndata['h'] # on dgl graph g.send(message_func=message_func) g.recv([0, 1, 3, 5], reduce_func=reduce_func, apply_node_func=apply_node_func) g.recv([0, 2, 4, 8], reduce_func=reduce_func, apply_node_func=apply_node_func) # nx g1.send(message_func=message_func) g1.recv([0, 1, 3, 5], reduce_func=reduce_func, apply_node_func=apply_node_func) g1.recv([0, 2, 4, 8], reduce_func=reduce_func, apply_node_func=apply_node_func) # sparse matrix g2.send(message_func=message_func) g2.recv([0, 1, 3, 5], reduce_func=reduce_func, apply_node_func=apply_node_func) g2.recv([0, 2, 4, 8], reduce_func=reduce_func, apply_node_func=apply_node_func) assert F.allclose(g.ndata['h'], g1.ndata['h']) assert F.allclose(g.ndata['h'], g2.ndata['h'])
def test_random_walk(): edge_list = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 3), (3, 2), (2, 1), (1, 0)] seeds = [0, 1] n_traces = 3 n_hops = 4 g = dgl.DGLGraph(edge_list, readonly=True) traces = dgl.contrib.sampling.random_walk(g, seeds, n_traces, n_hops) traces = F.zerocopy_to_numpy(traces) assert traces.shape == (len(seeds), n_traces, n_hops + 1) for i, seed in enumerate(seeds): assert (traces[i, :, 0] == seeds[i]).all() trace_diff = np.diff(traces, axis=-1) # only nodes with adjacent IDs are connected assert (np.abs(trace_diff) == 1).all()
def test_basics(): g = generate_graph() h = g.ndata['h'] l = g.edata['l'] nid = [0, 2, 3, 6, 7, 9] sg = g.subgraph(nid) eid = {2, 3, 4, 5, 10, 11, 12, 13, 16} assert set(F.zerocopy_to_numpy(sg.parent_eid)) == eid eid = sg.parent_eid # the subgraph is empty initially assert len(sg.ndata) == 0 assert len(sg.edata) == 0 # the data is copied after explict copy from sg.copy_from_parent() assert len(sg.ndata) == 1 assert len(sg.edata) == 1 sh = sg.ndata['h'] assert F.allclose(h[nid], sh) ''' s, d, eid 0, 1, 0 1, 9, 1 0, 2, 2 1 2, 9, 3 1 0, 3, 4 1 3, 9, 5 1 0, 4, 6 4, 9, 7 0, 5, 8 5, 9, 9 3 0, 6, 10 1 6, 9, 11 1 3 0, 7, 12 1 7, 9, 13 1 3 0, 8, 14 8, 9, 15 3 9, 0, 16 1 ''' assert F.allclose(F.gather_row(l, eid), sg.edata['l']) # update the node/edge features on the subgraph should NOT # reflect to the parent graph. sg.ndata['h'] = F.zeros((6, D)) assert F.allclose(h, g.ndata['h'])
def _pfc(x): return list(F.zerocopy_to_numpy(x)[:, 0])
def random_block(size): g = dgl.graph(nx.erdos_renyi_graph(size, 0.1)) return dgl.to_block(g, np.unique(F.zerocopy_to_numpy(g.edges()[1])))
def toset(x): return set(F.zerocopy_to_numpy(x).tolist())
def toset(x): # F.zerocopy_to_numpy may return a int return set(F.zerocopy_to_numpy(x).tolist())