How to use the dgl.utils.toindex function in dgl

To help you get started, we’ve selected a few dgl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dmlc / dgl / tests / graph_index / test_hetero.py View on Github external
assert g.has_node(0, 0)
        assert not g.has_node(0, 10)
        assert _array_equal(g.has_nodes(0, toindex([0, 10])), [1, 0])
        assert g.has_edge_between(R1, 3, 0)
        assert not g.has_edge_between(R1, 4, 0)
        assert _array_equal(g.has_edges_between(R1, toindex([3, 4]), toindex([0, 0])), [1, 0])
        assert _array_equal(g.predecessors(R1, 0), [0, 1, 2, 3])
        assert _array_equal(g.predecessors(R1, 1), [])
        assert _array_equal(g.successors(R1, 3), [0])
        assert _array_equal(g.successors(R1, 4), [])
        assert _array_equal(g.edge_id(R1, 0, 0), [0])
        src, dst, eid = g.edge_ids(R1, toindex([0, 2, 1, 3]), toindex([0, 0, 0, 0]))
        assert _array_equal(src, [0, 2, 1, 3])
        assert _array_equal(dst, [0, 0, 0, 0])
        assert _array_equal(eid, [0, 2, 1, 3])
        src, dst, eid = g.find_edges(R1, toindex([3, 0]))
        assert _array_equal(src, [3, 0])
        assert _array_equal(dst, [0, 0])
        assert _array_equal(eid, [3, 0])
        src, dst, eid = g.in_edges(R1, toindex([0, 1]))
        assert _array_equal(src, [0, 1, 2, 3])
        assert _array_equal(dst, [0, 0, 0, 0])
        assert _array_equal(eid, [0, 1, 2, 3])
        src, dst, eid = g.out_edges(R1, toindex([1, 0, 4]))
        assert _array_equal(src, [1, 0])
        assert _array_equal(dst, [0, 0])
        assert _array_equal(eid, [1, 0])
        src, dst, eid = g.edges(R1, 'eid')
        assert _array_equal(src, [0, 1, 2, 3])
        assert _array_equal(dst, [0, 0, 0, 0])
        assert _array_equal(eid, [0, 1, 2, 3])
        assert g.in_degree(R1, 0) == 4
github dmlc / dgl / tests / mxnet / test_graph_index.py View on Github external
# node_subgraph
    randv1 = np.random.randint(0, num_vertices, 20)
    randv = np.unique(randv1)
    subg = g.node_subgraph(utils.toindex(randv))
    subig = ig.node_subgraph(utils.toindex(randv))
    check_graph_equal(subg, subig)
    assert mx.nd.sum(map_to_subgraph_nid(subg, utils.toindex(randv1[0:10])).tousertensor()
            == map_to_subgraph_nid(subig, utils.toindex(randv1[0:10])).tousertensor()) == 10

    # node_subgraphs
    randvs = []
    subgs = []
    for i in range(4):
        randv = np.unique(np.random.randint(0, num_vertices, 20))
        randvs.append(utils.toindex(randv))
        subgs.append(g.node_subgraph(utils.toindex(randv)))
    subigs= ig.node_subgraphs(randvs)
    for i in range(4):
        check_graph_equal(subgs[i], subigs[i])
github dmlc / dgl / tests / graph_index / test_hetero.py View on Github external
assert not g.has_edge_between(R1, 4, 0)
        assert _array_equal(g.has_edges_between(R1, toindex([3, 4]), toindex([0, 0])), [1, 0])
        assert _array_equal(g.predecessors(R1, 0), [0, 1, 2, 3])
        assert _array_equal(g.predecessors(R1, 1), [])
        assert _array_equal(g.successors(R1, 3), [0])
        assert _array_equal(g.successors(R1, 4), [])
        assert _array_equal(g.edge_id(R1, 0, 0), [0])
        src, dst, eid = g.edge_ids(R1, toindex([0, 2, 1, 3]), toindex([0, 0, 0, 0]))
        assert _array_equal(src, [0, 2, 1, 3])
        assert _array_equal(dst, [0, 0, 0, 0])
        assert _array_equal(eid, [0, 2, 1, 3])
        src, dst, eid = g.find_edges(R1, toindex([3, 0]))
        assert _array_equal(src, [3, 0])
        assert _array_equal(dst, [0, 0])
        assert _array_equal(eid, [3, 0])
        src, dst, eid = g.in_edges(R1, toindex([0, 1]))
        assert _array_equal(src, [0, 1, 2, 3])
        assert _array_equal(dst, [0, 0, 0, 0])
        assert _array_equal(eid, [0, 1, 2, 3])
        src, dst, eid = g.out_edges(R1, toindex([1, 0, 4]))
        assert _array_equal(src, [1, 0])
        assert _array_equal(dst, [0, 0])
        assert _array_equal(eid, [1, 0])
        src, dst, eid = g.edges(R1, 'eid')
        assert _array_equal(src, [0, 1, 2, 3])
        assert _array_equal(dst, [0, 0, 0, 0])
        assert _array_equal(eid, [0, 1, 2, 3])
        assert g.in_degree(R1, 0) == 4
        assert g.in_degree(R1, 1) == 0
        assert _array_equal(g.in_degrees(R1, toindex([0, 1])), [4, 0])
        assert g.out_degree(R1, 2) == 1
        assert g.out_degree(R1, 4) == 0
github dmlc / dgl / tests / graph_index / test_graph_index.py View on Github external
# multiedges
    gi.add_edge(0, 1)
    eid = gi.edge_id(0, 1).tonumpy()
    assert len(eid) == 2
    assert eid[0] == 0
    assert eid[1] == 1

    gi.add_edges(toindex([0, 1, 1, 2]), toindex([2, 2, 2, 3]))
    src, dst, eid = gi.edge_ids(toindex([0, 0, 2, 1]), toindex([2, 1, 3, 2]))
    eid_answer = [2, 0, 1, 5, 3, 4]
    assert len(eid) == 6
    assert all(e == ea for e, ea in zip(eid, eid_answer))

    # find edges
    src, dst, eid = gi.find_edges(toindex([1, 3, 5]))
    assert len(src) == len(dst) == len(eid) == 3
    assert src[0] == 0 and src[1] == 1 and src[2] == 2
    assert dst[0] == 1 and dst[1] == 2 and dst[2] == 3
    assert eid[0] == 1 and eid[1] == 3 and eid[2] == 5

    # source broadcasting
    src, dst, eid = gi.edge_ids(toindex([0]), toindex([1, 2]))
    eid_answer = [0, 1, 2]
    assert len(eid) == 3
    assert all(e == ea for e, ea in zip(eid, eid_answer))

    # destination broadcasting
    src, dst, eid = gi.edge_ids(toindex([1, 0]), toindex([2]))
    eid_answer = [3, 4, 2]
    assert len(eid) == 3
    assert all(e == ea for e, ea in zip(eid, eid_answer))
github dmlc / dgl / tests / graph_index / test_hetero.py View on Github external
def rel2_from_csr():
    indptr = toindex([0, 1, 3])
    indices = toindex([0, 1, 2])
    edge_ids = toindex([0, 1, 2])
    return dgl_hgidx.create_bipartite_from_csr(2, 3, indptr, indices, edge_ids)
github dmlc / dgl / tests / mxnet / test_graph_index.py View on Github external
assert mx.nd.sum(out_eids1.tousertensor() == out_eids2.tousertensor()).asnumpy() == nnz

    num_v = len(randv)
    assert mx.nd.sum(g.in_degrees(randv).tousertensor() == ig.in_degrees(randv).tousertensor()).asnumpy() == num_v
    assert mx.nd.sum(g.out_degrees(randv).tousertensor() == ig.out_degrees(randv).tousertensor()).asnumpy() == num_v
    randv = randv.tousertensor()
    for v in randv.asnumpy():
        assert g.in_degree(v) == ig.in_degree(v)
        assert g.out_degree(v) == ig.out_degree(v)

    for u in randv.asnumpy():
        for v in randv.asnumpy():
            if len(g.edge_id(u, v)) == 1:
                assert g.edge_id(u, v).tonumpy() == ig.edge_id(u, v).tonumpy()
            assert g.has_edge_between(u, v) == ig.has_edge_between(u, v)
    randv = utils.toindex(randv)
    ids = g.edge_ids(randv, randv)[2].tonumpy()
    assert sum(ig.edge_ids(randv, randv)[2].tonumpy() == ids) == len(ids)
    assert sum(g.has_edges_between(randv, randv).tonumpy() == ig.has_edges_between(randv, randv).tonumpy()) == len(randv)
github dmlc / dgl / tests / mxnet / test_graph_index.py View on Github external
def check_basics(g, ig):
    assert g.number_of_nodes() == ig.number_of_nodes()
    assert g.number_of_edges() == ig.number_of_edges()

    edges = g.edges()
    iedges = ig.edges()

    for i in range(g.number_of_nodes()):
        assert g.has_node(i) == ig.has_node(i)

    for i in range(g.number_of_nodes()):
        assert mx.nd.sum(g.predecessors(i).tousertensor()).asnumpy() == mx.nd.sum(ig.predecessors(i).tousertensor()).asnumpy()
        assert mx.nd.sum(g.successors(i).tousertensor()).asnumpy() == mx.nd.sum(ig.successors(i).tousertensor()).asnumpy()

    randv = np.random.randint(0, g.number_of_nodes(), 10)
    randv = utils.toindex(randv)
    in_src1, in_dst1, in_eids1 = g.in_edges(randv)
    in_src2, in_dst2, in_eids2 = ig.in_edges(randv)
    nnz = in_src2.tousertensor().shape[0]
    assert mx.nd.sum(in_src1.tousertensor() == in_src2.tousertensor()).asnumpy() == nnz
    assert mx.nd.sum(in_dst1.tousertensor() == in_dst2.tousertensor()).asnumpy() == nnz
    assert mx.nd.sum(in_eids1.tousertensor() == in_eids2.tousertensor()).asnumpy() == nnz

    out_src1, out_dst1, out_eids1 = g.out_edges(randv)
    out_src2, out_dst2, out_eids2 = ig.out_edges(randv)
    nnz = out_dst2.tousertensor().shape[0]
    assert mx.nd.sum(out_dst1.tousertensor() == out_dst2.tousertensor()).asnumpy() == nnz
    assert mx.nd.sum(out_src1.tousertensor() == out_src2.tousertensor()).asnumpy() == nnz
    assert mx.nd.sum(out_eids1.tousertensor() == out_eids2.tousertensor()).asnumpy() == nnz

    num_v = len(randv)
    assert mx.nd.sum(g.in_degrees(randv).tousertensor() == ig.in_degrees(randv).tousertensor()).asnumpy() == num_v
github dmlc / dgl / tests / graph_index / test_hetero.py View on Github external
assert g.is_readonly()
        # relation graph 1
        assert g.number_of_nodes(R1) == 5
        assert g.number_of_edges(R1) == 4
        assert g.has_node(0, 0)
        assert not g.has_node(0, 10)
        assert _array_equal(g.has_nodes(0, toindex([0, 10])), [1, 0])
        assert g.has_edge_between(R1, 3, 0)
        assert not g.has_edge_between(R1, 4, 0)
        assert _array_equal(g.has_edges_between(R1, toindex([3, 4]), toindex([0, 0])), [1, 0])
        assert _array_equal(g.predecessors(R1, 0), [0, 1, 2, 3])
        assert _array_equal(g.predecessors(R1, 1), [])
        assert _array_equal(g.successors(R1, 3), [0])
        assert _array_equal(g.successors(R1, 4), [])
        assert _array_equal(g.edge_id(R1, 0, 0), [0])
        src, dst, eid = g.edge_ids(R1, toindex([0, 2, 1, 3]), toindex([0, 0, 0, 0]))
        assert _array_equal(src, [0, 2, 1, 3])
        assert _array_equal(dst, [0, 0, 0, 0])
        assert _array_equal(eid, [0, 2, 1, 3])
        src, dst, eid = g.find_edges(R1, toindex([3, 0]))
        assert _array_equal(src, [3, 0])
        assert _array_equal(dst, [0, 0])
        assert _array_equal(eid, [3, 0])
        src, dst, eid = g.in_edges(R1, toindex([0, 1]))
        assert _array_equal(src, [0, 1, 2, 3])
        assert _array_equal(dst, [0, 0, 0, 0])
        assert _array_equal(eid, [0, 1, 2, 3])
        src, dst, eid = g.out_edges(R1, toindex([1, 0, 4]))
        assert _array_equal(src, [1, 0])
        assert _array_equal(dst, [0, 0])
        assert _array_equal(eid, [1, 0])
        src, dst, eid = g.edges(R1, 'eid')
github dmlc / dgl / tests / compute / test_graph_index.py View on Github external
assert F.array_equal(out_eids1, out_eids2)

    num_v = len(randv)
    assert F.array_equal(g.in_degrees(randv).tousertensor(), ig.in_degrees(randv).tousertensor())
    assert F.array_equal(g.out_degrees(randv).tousertensor(), ig.out_degrees(randv).tousertensor())
    randv = randv.tousertensor()
    for v in F.asnumpy(randv):
        assert g.in_degree(v) == ig.in_degree(v)
        assert g.out_degree(v) == ig.out_degree(v)

    for u in F.asnumpy(randv):
        for v in F.asnumpy(randv):
            if len(g.edge_id(u, v)) == 1:
                assert g.edge_id(u, v).tonumpy() == ig.edge_id(u, v).tonumpy()
            assert g.has_edge_between(u, v) == ig.has_edge_between(u, v)
    randv = utils.toindex(randv)
    ids = g.edge_ids(randv, randv)[2].tonumpy()
    assert sum(ig.edge_ids(randv, randv)[2].tonumpy() == ids, 0) == len(ids)
    assert sum(g.has_edges_between(randv, randv).tonumpy() == ig.has_edges_between(randv, randv).tonumpy(), 0) == len(randv)
github dmlc / dgl / tests / compute / test_nodeflow.py View on Github external
def test_block_adj_matrix():
    num_layers = 3
    g = generate_rand_graph(100)
    nf = create_mini_batch(g, num_layers)
    assert nf.num_layers == num_layers + 1
    for i in range(nf.num_blocks):
        u, v, _ = nf.block_edges(i, remap_local=True)
        adj, _ = nf.block_adjacency_matrix(i, F.cpu())
        adj = F.sparse_to_numpy(adj)

        # should also work for negative block ids
        adj_by_neg, _ = nf.block_adjacency_matrix(-nf.num_blocks + i, F.cpu())
        adj_by_neg = F.sparse_to_numpy(adj_by_neg)

        data = np.ones((len(u)), dtype=np.float32)
        v = utils.toindex(v)
        u = utils.toindex(u)
        coo = sp.sparse.coo_matrix((data, (v.tonumpy(), u.tonumpy())),
                                   shape=adj.shape).todense()
        assert_array_equal(adj, coo)
        assert_array_equal(adj_by_neg, coo)