code: def test_subgraph1(idtype):
      g = create_test_heterograph(idtype)
      g_graph = g["follows"]
      g_bipartite = g["plays"]
  
      x = F.randn((3, 5))
      y = F.randn((2, 4))
      g.nodes["user"].data["h"] = x
      g.edges["follows"].data["h"] = y
  
      def _check_subgraph(g, sg):
          assert sg.idtype == g.idtype
          assert sg.device == g.device
          assert sg.ntypes == g.ntypes
          assert sg.etypes == g.etypes
          assert sg.canonical_etypes == g.canonical_etypes
          assert F.array_equal(
              F.tensor(sg.nodes["user"].data[dgl.NID]),
              F.tensor([1, 2], g.idtype),
          )
          assert F.array_equal(
              F.tensor(sg.nodes["game"].data[dgl.NID]),
              F.tensor([0], g.idtype),
          )
          assert F.array_equal(
              F.tensor(sg.edges["follows"].data[dgl.EID]),
              F.tensor([1], g.idtype),
          )
          assert F.array_equal(
              F.tensor(sg.edges["plays"].data[dgl.EID]),
              F.tensor([1], g.idtype),
          )
          assert F.array_equal(
              F.tensor(sg.edges["wishes"].data[dgl.EID]),
              F.tensor([1], g.idtype),
          )
          assert sg.num_nodes("developer") == 0
          assert sg.num_edges("develops") == 0
          assert F.array_equal(
              sg.nodes["user"].data["h"], g.nodes["user"].data["h"][1:3]
          )
          assert F.array_equal(
              sg.edges["follows"].data["h"],
              g.edges["follows"].data["h"][1:2],
          )
  
      sg1 = g.subgraph({"user": [1, 2], "game": [0]})
      _check_subgraph(g, sg1)
      sg2 = g.edge_subgraph(
          {"follows": [1], "plays": [1], "wishes": [1]}
      )
      _check_subgraph(g, sg2)
  
      # backend tensor input
      sg1 = g.subgraph(
          {
              "user": F.tensor([1, 2], dtype=idtype),
              "game": F.tensor([0], dtype=idtype),
          }
      )
      _check_subgraph(g, sg1)
      sg2 = g.edge_subgraph(
          {
              "follows": F.tensor([1], dtype=idtype),
              "plays": F.tensor([1], dtype=idtype),
              "wishes": F.tensor([1], dtype=idtype),
          }
      )
      _check_subgraph(g, sg2)
  
      # numpy input
      sg1 = g.subgraph(
          {"user": np.array([1, 2]), "game": np.array([0])}
      )
      _check_subgraph(g, sg1)
      sg2 = g.edge_subgraph(
          {
              "follows": np.array([1]),
              "plays": np.array([1]),
              "wishes": np.array([1]),
          }
      )
      _check_subgraph(g, sg2)
  
      def _check_subgraph_single_ntype(g, sg, preserve_nodes=False):
          assert sg.idtype == g.idtype
          assert sg.device == g.device
          assert sg.ntypes == g.ntypes
          assert sg.etypes == g.etypes
          assert sg.canonical_etypes == g.canonical_etypes
  
          if not preserve_nodes:
              assert F.array_equal(
                  F.tensor(sg.nodes["user"].data[dgl.NID]),
                  F.tensor([1, 2], g.idtype),
              )
          else:
              for ntype in sg.ntypes:
                  assert g.num_nodes(ntype) == sg.num_nodes(ntype)
  
          assert F.array_equal(
              F.tensor(sg.edges["follows"].data[dgl.EID]),
              F.tensor([1], g.idtype),
          )
  
          if not preserve_nodes:
              assert F.array_equal(
                  sg.nodes["user"].data["h"],
                  g.nodes["user"].data["h"][1:3],
              )
          assert F.array_equal(
              sg.edges["follows"].data["h"],
              g.edges["follows"].data["h"][1:2],
          )
  
      def _check_subgraph_single_etype(g, sg, preserve_nodes=False):
          assert sg.ntypes == g.ntypes
          assert sg.etypes == g.etypes
          assert sg.canonical_etypes == g.canonical_etypes
  
          if not preserve_nodes:
              assert F.array_equal(
                  F.tensor(sg.nodes["user"].data[dgl.NID]),
                  F.tensor([0, 1], g.idtype),
              )
              assert F.array_equal(
                  F.tensor(sg.nodes["game"].data[dgl.NID]),
                  F.tensor([0], g.idtype),
              )
          else:
              for ntype in sg.ntypes:
                  assert g.num_nodes(ntype) == sg.num_nodes(ntype)
  
          assert F.array_equal(
              F.tensor(sg.edges["plays"].data[dgl.EID]),
              F.tensor([0, 1], g.idtype),
          )
  
      sg1_graph = g_graph.subgraph([1, 2])
      _check_subgraph_single_ntype(g_graph, sg1_graph)
      sg1_graph = g_graph.edge_subgraph([1])
      _check_subgraph_single_ntype(g_graph, sg1_graph)
      sg1_graph = g_graph.edge_subgraph([1], relabel_nodes=False)
      _check_subgraph_single_ntype(g_graph, sg1_graph, True)
      sg2_bipartite = g_bipartite.edge_subgraph([0, 1])
      _check_subgraph_single_etype(g_bipartite, sg2_bipartite)
      sg2_bipartite = g_bipartite.edge_subgraph(
          [0, 1], relabel_nodes=False
      )
      _check_subgraph_single_etype(g_bipartite, sg2_bipartite, True)
  
      def _check_typed_subgraph1(g, sg):
          assert g.idtype == sg.idtype
          assert g.device == sg.device
          assert set(sg.ntypes) == {"user", "game"}
          assert set(sg.etypes) == {"follows", "plays", "wishes"}
          for ntype in sg.ntypes:
              assert sg.num_nodes(ntype) == g.num_nodes(ntype)
          for etype in sg.etypes:
              src_sg, dst_sg = sg.all_edges(etype=etype, order="eid")
              src_g, dst_g = g.all_edges(etype=etype, order="eid")
              assert F.array_equal(src_sg, src_g)
              assert F.array_equal(dst_sg, dst_g)
          assert F.array_equal(
              sg.nodes["user"].data["h"], g.nodes["user"].data["h"]
          )
          assert F.array_equal(
              sg.edges["follows"].data["h"],
              g.edges["follows"].data["h"],
          )
          g.nodes["user"].data["h"] = F.scatter_row(
              g.nodes["user"].data["h"], F.tensor([2]), F.randn((1, 5))
          )
          g.edges["follows"].data["h"] = F.scatter_row(
              g.edges["follows"].data["h"],
              F.tensor([1]),
              F.randn((1, 4)),
          )
          assert F.array_equal(
              sg.nodes["user"].data["h"], g.nodes["user"].data["h"]
          )
          assert F.array_equal(
              sg.edges["follows"].data["h"],
              g.edges["follows"].data["h"],
          )
  
      def _check_typed_subgraph2(g, sg):
          assert set(sg.ntypes) == {"developer", "game"}
          assert set(sg.etypes) == {"develops"}
          for ntype in sg.ntypes:
              assert sg.num_nodes(ntype) == g.num_nodes(ntype)
          for etype in sg.etypes:
              src_sg, dst_sg = sg.all_edges(etype=etype, order="eid")
              src_g, dst_g = g.all_edges(etype=etype, order="eid")
              assert F.array_equal(src_sg, src_g)
              assert F.array_equal(dst_sg, dst_g)
  
      sg3 = g.node_type_subgraph(["user", "game"])
      _check_typed_subgraph1(g, sg3)
      sg4 = g.edge_type_subgraph(["develops"])
      _check_typed_subgraph2(g, sg4)
      sg5 = g.edge_type_subgraph(["follows", "plays", "wishes"])
      _check_typed_subgraph1(g, sg5)
  
      # Test for restricted format
      for fmt in ["csr", "csc", "coo"]:
          g = dgl.graph(([0, 1], [1, 2])).formats(fmt)
          sg = g.subgraph({g.ntypes[0]: [1, 0]})
          nids = F.asnumpy(sg.ndata[dgl.NID])
          assert np.array_equal(nids, np.array([1, 0]))
          src, dst = sg.edges(order="eid")
          src = F.asnumpy(src)
          dst = F.asnumpy(dst)
          assert np.array_equal(src, np.array([1]))
  
imports: import unittest
  import backend
  import dgl
  import networkx
  import numpy
  import pytest
  import scipy.sparse
  from utils import parametrize_idtype
label_desc: Use image node_id 8 for calling a global function with example usage: test_subgraph1(idtype) without return types