コード例 #1
0
ファイル: test_graph.py プロジェクト: xiangsheng1325/dgl
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
    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.]]))
コード例 #2
0
def test_adj():
    g = create_test_heterograph()
    adj = F.sparse_to_numpy(g.adj(etype='follows'))
    assert np.allclose(adj, np.array([[0., 0., 0.], [1., 0., 0.], [0., 1.,
                                                                   0.]]))
    adj = F.sparse_to_numpy(g.adj(transpose=True, etype='follows'))
    assert np.allclose(adj, np.array([[0., 1., 0.], [0., 0., 1.], [0., 0.,
                                                                   0.]]))
    adj = F.sparse_to_numpy(g.adj(etype='plays'))
    assert np.allclose(adj, np.array([[1., 1., 0.], [0., 1., 1.]]))
    adj = F.sparse_to_numpy(g.adj(transpose=True, etype='plays'))
    assert np.allclose(adj, np.array([[1., 0.], [1., 1.], [0., 1.]]))

    adj = g.adj(scipy_fmt='csr', etype='follows')
    assert np.allclose(adj.todense(),
                       np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.]]))
    adj = g.adj(scipy_fmt='coo', etype='follows')
    assert np.allclose(adj.todense(),
                       np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.]]))
    adj = g.adj(scipy_fmt='csr', etype='plays')
    assert np.allclose(adj.todense(), np.array([[1., 1., 0.], [0., 1., 1.]]))
    adj = g.adj(scipy_fmt='coo', etype='plays')
    assert np.allclose(adj.todense(), np.array([[1., 1., 0.], [0., 1., 1.]]))
    adj = F.sparse_to_numpy(g['follows'].adj())
    assert np.allclose(adj, np.array([[0., 0., 0.], [1., 0., 0.], [0., 1.,
                                                                   0.]]))
コード例 #3
0
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)
コード例 #4
0
def test_block_incidence_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):
        typestrs = ["in", "out"]  # todo need fix for "both"
        adjs = []
        for typestr in typestrs:
            adj, _ = nf.block_incidence_matrix(i, typestr, F.cpu())
            adj = F.sparse_to_numpy(adj)
            adjs.append(adj)

        # should work for negative block ids
        adjs_by_neg = []
        for typestr in typestrs:
            adj_by_neg, _ = nf.block_incidence_matrix(
                -nf.num_blocks + i, typestr, F.cpu())
            adj_by_neg = F.sparse_to_numpy(adj_by_neg)
            adjs_by_neg.append(adj_by_neg)

        u, v, e = nf.block_edges(i, remap_local=True)
        u = utils.toindex(u)
        v = utils.toindex(v)
        e = utils.toindex(e)

        expected = []
        data_in_and_out = np.ones((len(u)), dtype=np.float32)
        expected.append(
            sp.sparse.coo_matrix((data_in_and_out, (v.tonumpy(), e.tonumpy())),
                                 shape=adjs[0].shape).todense()
        )
        expected.append(
            sp.sparse.coo_matrix((data_in_and_out, (u.tonumpy(), e.tonumpy())),
                                 shape=adjs[1].shape).todense()
        )
        for i in range(len(typestrs)):
            assert_array_equal(adjs[i], expected[i])
            assert_array_equal(adjs_by_neg[i], expected[i])
コード例 #5
0
def test_inc():
    g = create_test_heterograph()
    #follows_g = dgl.graph([(0, 1), (1, 2)], 'user', 'follows')
    adj = F.sparse_to_numpy(g['follows'].inc('in'))
    assert np.allclose(adj, np.array([[0., 0.], [1., 0.], [0., 1.]]))
    adj = F.sparse_to_numpy(g['follows'].inc('out'))
    assert np.allclose(adj, np.array([[1., 0.], [0., 1.], [0., 0.]]))
    adj = F.sparse_to_numpy(g['follows'].inc('both'))
    assert np.allclose(adj, np.array([[-1., 0.], [1., -1.], [0., 1.]]))
    adj = F.sparse_to_numpy(g.inc('in', etype='plays'))
    assert np.allclose(adj, np.array([[1., 1., 0., 0.], [0., 0., 1., 1.]]))
    adj = F.sparse_to_numpy(g.inc('out', etype='plays'))
    assert np.allclose(
        adj, np.array([[1., 0., 0., 0.], [0., 1., 0., 1.], [0., 0., 1., 0.]]))
    adj = F.sparse_to_numpy(g.inc('both', etype='follows'))
    assert np.allclose(adj, np.array([[-1., 0.], [1., -1.], [0., 1.]]))
コード例 #6
0
ファイル: test_nodeflow.py プロジェクト: zwvews/dgl
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):
        src, dst, eid = nf.block_edges(i)
        dest_nodes = utils.toindex(nf.layer_nid(i + 1))
        u, v, _ = nf._graph.in_edges(dest_nodes)
        u = nf._glb2lcl_nid(u.tousertensor(), i)
        v = nf._glb2lcl_nid(v.tousertensor(), i + 1)
        assert F.array_equal(src, u)
        assert F.array_equal(dst, v)

        adj, _ = nf.block_adjacency_matrix(i, F.cpu())
        adj = F.sparse_to_numpy(adj)
        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 np.array_equal(adj, coo)
コード例 #7
0
ファイル: test_graph.py プロジェクト: xiangsheng1325/dgl
    def _test_one(g):
        assert g.number_of_nodes() == 10
        assert g.number_of_edges() == 20

        for i in range(10):
            assert g.has_node(i)
            assert i in g
        assert not g.has_node(11)
        assert not 11 in g
        assert F.allclose(g.has_nodes([0,2,10,11]), F.tensor([1,1,0,0]))

        src, dst = edge_pair_input()
        for u, v in zip(src, dst):
            assert g.has_edge_between(u, v)
        assert not g.has_edge_between(0, 0)
        assert F.allclose(g.has_edges_between([0, 0, 3], [0, 9, 8]), F.tensor([0,1,1]))
        assert set(F.asnumpy(g.predecessors(9))) == set([0,5,7,4])
        assert set(F.asnumpy(g.successors(2))) == set([7,3])

        assert g.edge_id(4,4) == 5
        assert F.allclose(g.edge_ids([4,0], [4,9]), F.tensor([5,0]))

        src, dst = g.find_edges([3, 6, 5])
        assert F.allclose(src, F.tensor([5, 7, 4]))
        assert F.allclose(dst, F.tensor([9, 9, 4]))

        src, dst, eid = g.in_edges(9, form='all')
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set([(0,9,0),(5,9,3),(7,9,6),(4,9,7)])
        src, dst, eid = g.in_edges([9,0,8], form='all')  # test node#0 has no in edges
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set([(0,9,0),(5,9,3),(7,9,6),(4,9,7),(3,8,9),(7,8,12)])

        src, dst, eid = g.out_edges(0, form='all')
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set([(0,9,0),(0,6,1),(0,4,4)])
        src, dst, eid = g.out_edges([0,4,8], form='all')  # test node#8 has no out edges
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set([(0,9,0),(0,6,1),(0,4,4),(4,3,2),(4,4,5),(4,9,7),(4,1,8)])

        src, dst, eid = g.edges('all', 'eid')
        t_src, t_dst = edge_pair_input()
        t_tup = list(zip(t_src, t_dst, list(range(20))))
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set(t_tup)
        assert list(F.asnumpy(eid)) == list(range(20))

        src, dst, eid = g.edges('all', 'srcdst')
        t_src, t_dst = edge_pair_input()
        t_tup = list(zip(t_src, t_dst, list(range(20))))
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set(t_tup)
        assert list(F.asnumpy(src)) == sorted(list(F.asnumpy(src)))

        assert g.in_degree(0) == 0
        assert g.in_degree(9) == 4
        assert F.allclose(g.in_degrees([0, 9]), F.tensor([0, 4]))
        assert g.out_degree(8) == 0
        assert g.out_degree(9) == 1
        assert F.allclose(g.out_degrees([8, 9]), F.tensor([0, 1]))

        assert np.array_equal(
                F.sparse_to_numpy(g.adjacency_matrix(transpose=False)), scipy_coo_input().toarray().T)
        assert np.array_equal(
                F.sparse_to_numpy(g.adjacency_matrix(transpose=True)), scipy_coo_input().toarray())
コード例 #8
0
    def _test_csr_one(g):
        assert g.number_of_nodes() == 10
        assert g.number_of_edges() == 20
        assert len(g) == 10
        assert not g.is_multigraph

        for i in range(10):
            assert g.has_node(i)
            assert i in g
        assert not g.has_node(11)
        assert not 11 in g
        assert F.allclose(g.has_nodes([0, 2, 10, 11]), F.tensor([1, 1, 0, 0]))

        src, dst = edge_pair_input(sort=True)
        for u, v in zip(src, dst):
            assert g.has_edge_between(u, v)
        assert not g.has_edge_between(0, 0)
        assert F.allclose(g.has_edges_between([0, 0, 3], [0, 9, 8]),
                          F.tensor([0, 1, 1]))
        assert set(F.asnumpy(g.predecessors(9))) == set([0, 5, 7, 4])
        assert set(F.asnumpy(g.successors(2))) == set([7, 3])

        # src = [0 0 0 1 1 2 2 3 3 4 4 4 4 5 5 6 7 7 7 9]
        # dst = [4 6 9 3 5 3 7 5 8 1 3 4 9 1 9 6 2 8 9 2]
        # eid = [0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
        assert g.edge_id(4, 4) == 11
        assert F.allclose(g.edge_ids([4, 0], [4, 9]), F.tensor([11, 2]))

        src, dst = g.find_edges([3, 6, 5])
        assert F.allclose(src, F.tensor([1, 2, 2]))
        assert F.allclose(dst, F.tensor([3, 7, 3]))

        src, dst, eid = g.in_edges(9, form='all')
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set([(0, 9, 2), (5, 9, 14), (7, 9, 18), (4, 9, 12)])
        src, dst, eid = g.in_edges([9, 0, 8],
                                   form='all')  # test node#0 has no in edges
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set([(0, 9, 2), (5, 9, 14), (7, 9, 18), (4, 9, 12),
                                (3, 8, 8), (7, 8, 17)])

        src, dst, eid = g.out_edges(0, form='all')
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set([(0, 9, 2), (0, 6, 1), (0, 4, 0)])
        src, dst, eid = g.out_edges([0, 4, 8],
                                    form='all')  # test node#8 has no out edges
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set([(0, 9, 2), (0, 6, 1), (0, 4, 0), (4, 3, 10),
                                (4, 4, 11), (4, 9, 12), (4, 1, 9)])

        src, dst, eid = g.edges('all', 'eid')
        t_src, t_dst = edge_pair_input(sort=True)
        t_tup = list(zip(t_src, t_dst, list(range(20))))
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set(t_tup)
        assert list(F.asnumpy(eid)) == list(range(20))

        src, dst, eid = g.edges('all', 'srcdst')
        t_src, t_dst = edge_pair_input(sort=True)
        t_tup = list(zip(t_src, t_dst, list(range(20))))
        tup = list(zip(F.asnumpy(src), F.asnumpy(dst), F.asnumpy(eid)))
        assert set(tup) == set(t_tup)
        assert list(F.asnumpy(src)) == sorted(list(F.asnumpy(src)))

        assert g.in_degree(0) == 0
        assert g.in_degree(9) == 4
        assert F.allclose(g.in_degrees([0, 9]), F.tensor([0, 4]))
        assert g.out_degree(8) == 0
        assert g.out_degree(9) == 1
        assert F.allclose(g.out_degrees([8, 9]), F.tensor([0, 1]))

        assert np.array_equal(
            F.sparse_to_numpy(g.adjacency_matrix(transpose=False)),
            scipy_coo_input().toarray().T)
        assert np.array_equal(
            F.sparse_to_numpy(g.adjacency_matrix(transpose=True)),
            scipy_coo_input().toarray())
コード例 #9
0
ファイル: test_hetero.py プロジェクト: zemingd/dgl
 def _test_g(g):
     assert g.number_of_ntypes() == 3
     assert g.number_of_etypes() == 3
     assert g.meta_graph.number_of_nodes() == 3
     assert g.meta_graph.number_of_edges() == 3
     assert g.ctx() == nd.cpu(0)
     assert g.nbits() == 64
     assert not g.is_multigraph()
     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')
     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
     assert _array_equal(g.out_degrees(R1, toindex([4, 2])), [0, 1])
     # adjmat
     adj = g.adjacency_matrix(R1, True, F.cpu())[0]
     assert np.allclose(F.sparse_to_numpy(adj),
             np.array([[1., 0.],
                       [1., 0.],
                       [1., 0.],
                       [1., 0.],
                       [0., 0.]]))
     adj = g.adjacency_matrix(R2, True, F.cpu())[0]
     assert np.allclose(F.sparse_to_numpy(adj),
             np.array([[1., 0., 0.],
                       [0., 1., 1.]]))
     adj = g.adjacency_matrix(R3, True, F.cpu())[0]
     assert np.allclose(F.sparse_to_numpy(adj),
             np.array([[0., 1.],
                       [0., 1.],
                       [0., 1.]]))
コード例 #10
0
ファイル: test_hetero.py プロジェクト: zemingd/dgl
    def _test_g(g):
        # node subgraph
        induced_nodes = [toindex([0, 1, 4]), toindex([0]), toindex([0, 2])]
        sub = g.node_subgraph(induced_nodes)
        subg = sub.graph
        assert subg.number_of_ntypes() == 3
        assert subg.number_of_etypes() == 3
        assert subg.number_of_nodes(0) == 3
        assert subg.number_of_nodes(1) == 1
        assert subg.number_of_nodes(2) == 2
        assert subg.number_of_edges(R1) == 2
        assert subg.number_of_edges(R2) == 1
        assert subg.number_of_edges(R3) == 0
        adj = subg.adjacency_matrix(R1, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[1.],
                          [1.],
                          [0.]]))
        adj = subg.adjacency_matrix(R2, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[1., 0.]]))
        adj = subg.adjacency_matrix(R3, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[0.],
                          [0.]]))
        assert len(sub.induced_nodes) == 3
        assert _array_equal(sub.induced_nodes[0], induced_nodes[0])
        assert _array_equal(sub.induced_nodes[1], induced_nodes[1])
        assert _array_equal(sub.induced_nodes[2], induced_nodes[2])
        assert len(sub.induced_edges) == 3
        assert _array_equal(sub.induced_edges[0], [0, 1])
        assert _array_equal(sub.induced_edges[1], [0])
        assert _array_equal(sub.induced_edges[2], [])

        # node subgraph with empty type graph
        induced_nodes = [toindex([0, 1, 4]), toindex([0]), toindex([])]
        sub = g.node_subgraph(induced_nodes)
        subg = sub.graph
        assert subg.number_of_ntypes() == 3
        assert subg.number_of_etypes() == 3
        assert subg.number_of_nodes(0) == 3
        assert subg.number_of_nodes(1) == 1
        assert subg.number_of_nodes(2) == 0
        assert subg.number_of_edges(R1) == 2
        assert subg.number_of_edges(R2) == 0
        assert subg.number_of_edges(R3) == 0
        adj = subg.adjacency_matrix(R1, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[1.],
                          [1.],
                          [0.]]))
        adj = subg.adjacency_matrix(R2, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([]))
        adj = subg.adjacency_matrix(R3, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([]))

        # edge subgraph (preserve_nodes=False)
        induced_edges = [toindex([0, 2]), toindex([0]), toindex([0, 1, 2])]
        sub = g.edge_subgraph(induced_edges, False)
        subg = sub.graph
        assert subg.number_of_ntypes() == 3
        assert subg.number_of_etypes() == 3
        assert subg.number_of_nodes(0) == 2
        assert subg.number_of_nodes(1) == 2
        assert subg.number_of_nodes(2) == 3
        assert subg.number_of_edges(R1) == 2
        assert subg.number_of_edges(R2) == 1
        assert subg.number_of_edges(R3) == 3
        adj = subg.adjacency_matrix(R1, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[1., 0.],
                          [1., 0.]]))
        adj = subg.adjacency_matrix(R2, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[1., 0., 0.],
                          [0., 0., 0.]]))
        adj = subg.adjacency_matrix(R3, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[0., 1.],
                          [0., 1.],
                          [0., 1.]]))
        assert len(sub.induced_nodes) == 3
        assert _array_equal(sub.induced_nodes[0], [0, 2])
        assert _array_equal(sub.induced_nodes[1], [0, 1])
        assert _array_equal(sub.induced_nodes[2], [0, 1, 2])
        assert len(sub.induced_edges) == 3
        assert _array_equal(sub.induced_edges[0], induced_edges[0])
        assert _array_equal(sub.induced_edges[1], induced_edges[1])
        assert _array_equal(sub.induced_edges[2], induced_edges[2])

        # edge subgraph (preserve_nodes=True)
        induced_edges = [toindex([0, 2]), toindex([0]), toindex([0, 1, 2])]
        sub = g.edge_subgraph(induced_edges, True)
        subg = sub.graph
        assert subg.number_of_ntypes() == 3
        assert subg.number_of_etypes() == 3
        assert subg.number_of_nodes(0) == 5
        assert subg.number_of_nodes(1) == 2
        assert subg.number_of_nodes(2) == 3
        assert subg.number_of_edges(R1) == 2
        assert subg.number_of_edges(R2) == 1
        assert subg.number_of_edges(R3) == 3
        adj = subg.adjacency_matrix(R1, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[1., 0.],
                          [0., 0.],
                          [1., 0.],
                          [0., 0.],
                          [0., 0.]]))
        adj = subg.adjacency_matrix(R2, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[1., 0., 0.],
                          [0., 0., 0.]]))
        adj = subg.adjacency_matrix(R3, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[0., 1.],
                          [0., 1.],
                          [0., 1.]]))
        assert len(sub.induced_nodes) == 3
        assert _array_equal(sub.induced_nodes[0], [0, 1, 2, 3, 4])
        assert _array_equal(sub.induced_nodes[1], [0, 1])
        assert _array_equal(sub.induced_nodes[2], [0, 1, 2])
        assert len(sub.induced_edges) == 3
        assert _array_equal(sub.induced_edges[0], induced_edges[0])
        assert _array_equal(sub.induced_edges[1], induced_edges[1])
        assert _array_equal(sub.induced_edges[2], induced_edges[2])

        # edge subgraph with empty induced edges (preserve_nodes=False)
        induced_edges = [toindex([0, 2]), toindex([]), toindex([0, 1, 2])]
        sub = g.edge_subgraph(induced_edges, False)
        subg = sub.graph
        assert subg.number_of_ntypes() == 3
        assert subg.number_of_etypes() == 3
        assert subg.number_of_nodes(0) == 2
        assert subg.number_of_nodes(1) == 2
        assert subg.number_of_nodes(2) == 3
        assert subg.number_of_edges(R1) == 2
        assert subg.number_of_edges(R2) == 0
        assert subg.number_of_edges(R3) == 3
        adj = subg.adjacency_matrix(R1, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[1., 0.],
                          [1., 0.]]))
        adj = subg.adjacency_matrix(R2, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[0., 0., 0.],
                          [0., 0., 0.]]))
        adj = subg.adjacency_matrix(R3, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[0., 1.],
                          [0., 1.],
                          [0., 1.]]))
        assert len(sub.induced_nodes) == 3
        assert _array_equal(sub.induced_nodes[0], [0, 2])
        assert _array_equal(sub.induced_nodes[1], [0, 1])
        assert _array_equal(sub.induced_nodes[2], [0, 1, 2])
        assert len(sub.induced_edges) == 3
        assert _array_equal(sub.induced_edges[0], induced_edges[0])
        assert _array_equal(sub.induced_edges[1], induced_edges[1])
        assert _array_equal(sub.induced_edges[2], induced_edges[2])

        # edge subgraph with empty induced edges (preserve_nodes=True)
        induced_edges = [toindex([0, 2]), toindex([]), toindex([0, 1, 2])]
        sub = g.edge_subgraph(induced_edges, True)
        subg = sub.graph
        assert subg.number_of_ntypes() == 3
        assert subg.number_of_etypes() == 3
        assert subg.number_of_nodes(0) == 5
        assert subg.number_of_nodes(1) == 2
        assert subg.number_of_nodes(2) == 3
        assert subg.number_of_edges(R1) == 2
        assert subg.number_of_edges(R2) == 0
        assert subg.number_of_edges(R3) == 3
        adj = subg.adjacency_matrix(R1, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[1., 0.],
                          [0., 0.],
                          [1., 0.],
                          [0., 0.],
                          [0., 0.]]))
        adj = subg.adjacency_matrix(R2, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[0., 0., 0.],
                          [0., 0., 0.]]))
        adj = subg.adjacency_matrix(R3, True, F.cpu())[0]
        assert np.allclose(F.sparse_to_numpy(adj),
                np.array([[0., 1.],
                          [0., 1.],
                          [0., 1.]]))
        assert len(sub.induced_nodes) == 3
        assert _array_equal(sub.induced_nodes[0], [0, 1, 2, 3, 4])
        assert _array_equal(sub.induced_nodes[1], [0, 1])
        assert _array_equal(sub.induced_nodes[2], [0, 1, 2])
        assert len(sub.induced_edges) == 3
        assert _array_equal(sub.induced_edges[0], induced_edges[0])
        assert _array_equal(sub.induced_edges[1], induced_edges[1])
        assert _array_equal(sub.induced_edges[2], induced_edges[2])