code: def test_compact(idtype):
      g1 = dgl.heterograph(
          {
              ("user", "follow", "user"): ([1, 3], [3, 5]),
              ("user", "plays", "game"): ([2, 3, 2], [4, 4, 5]),
              ("game", "wished-by", "user"): ([6, 5], [7, 7]),
          },
          {"user": 20, "game": 10},
          idtype=idtype,
          device=F.ctx(),
      )
  
      g2 = dgl.heterograph(
          {
              ("game", "clicked-by", "user"): ([3], [1]),
              ("user", "likes", "user"): ([1, 8], [8, 9]),
          },
          {"user": 20, "game": 10},
          idtype=idtype,
          device=F.ctx(),
      )
  
      g3 = dgl.heterograph(
          {("user", "_E", "user"): ((0, 1), (1, 2))},
          {"user": 10},
          idtype=idtype,
          device=F.ctx(),
      )
      g4 = dgl.heterograph(
          {("user", "_E", "user"): ((1, 3), (3, 5))},
          {"user": 10},
          idtype=idtype,
          device=F.ctx(),
      )
  
      def _check(g, new_g, induced_nodes):
          assert g.ntypes == new_g.ntypes
          assert g.canonical_etypes == new_g.canonical_etypes
  
          for ntype in g.ntypes:
              assert -1 not in induced_nodes[ntype]
  
          for etype in g.canonical_etypes:
              g_src, g_dst = g.all_edges(order="eid", etype=etype)
              g_src = F.asnumpy(g_src)
              g_dst = F.asnumpy(g_dst)
              new_g_src, new_g_dst = new_g.all_edges(
                  order="eid", etype=etype
              )
              new_g_src_mapped = induced_nodes[etype[0]][
                  F.asnumpy(new_g_src)
              ]
              new_g_dst_mapped = induced_nodes[etype[2]][
                  F.asnumpy(new_g_dst)
              ]
              assert (g_src == new_g_src_mapped).all()
              assert (g_dst == new_g_dst_mapped).all()
  
      # Test default
      new_g1 = dgl.compact_graphs(g1)
      induced_nodes = {
          ntype: new_g1.nodes[ntype].data[dgl.NID]
          for ntype in new_g1.ntypes
      }
      induced_nodes = {
          k: F.asnumpy(v) for k, v in induced_nodes.items()
      }
      assert new_g1.idtype == idtype
      assert set(induced_nodes["user"]) == set([1, 3, 5, 2, 7])
      assert set(induced_nodes["game"]) == set([4, 5, 6])
      _check(g1, new_g1, induced_nodes)
  
      # Test with always_preserve given a dict
      new_g1 = dgl.compact_graphs(
          g1, always_preserve={"game": F.tensor([4, 7], idtype)}
      )
      assert new_g1.idtype == idtype
      induced_nodes = {
          ntype: new_g1.nodes[ntype].data[dgl.NID]
          for ntype in new_g1.ntypes
      }
      induced_nodes = {
          k: F.asnumpy(v) for k, v in induced_nodes.items()
      }
      assert set(induced_nodes["user"]) == set([1, 3, 5, 2, 7])
      assert set(induced_nodes["game"]) == set([4, 5, 6, 7])
      _check(g1, new_g1, induced_nodes)
  
      # Test with always_preserve given a tensor
      new_g3 = dgl.compact_graphs(
          g3, always_preserve=F.tensor([1, 7], idtype)
      )
      induced_nodes = {
          ntype: new_g3.nodes[ntype].data[dgl.NID]
          for ntype in new_g3.ntypes
      }
      induced_nodes = {
          k: F.asnumpy(v) for k, v in induced_nodes.items()
      }
  
      assert new_g3.idtype == idtype
      assert set(induced_nodes["user"]) == set([0, 1, 2, 7])
      _check(g3, new_g3, induced_nodes)
  
      # Test multiple graphs
      new_g1, new_g2 = dgl.compact_graphs([g1, g2])
      induced_nodes = {
          ntype: new_g1.nodes[ntype].data[dgl.NID]
          for ntype in new_g1.ntypes
      }
      induced_nodes = {
          k: F.asnumpy(v) for k, v in induced_nodes.items()
      }
      assert new_g1.idtype == idtype
      assert new_g2.idtype == idtype
      assert set(induced_nodes["user"]) == set([1, 3, 5, 2, 7, 8, 9])
      assert set(induced_nodes["game"]) == set([3, 4, 5, 6])
      _check(g1, new_g1, induced_nodes)
      _check(g2, new_g2, induced_nodes)
  
      # Test multiple graphs with always_preserve given a dict
      new_g1, new_g2 = dgl.compact_graphs(
          [g1, g2],
          always_preserve={"game": F.tensor([4, 7], dtype=idtype)},
      )
      induced_nodes = {
          ntype: new_g1.nodes[ntype].data[dgl.NID]
          for ntype in new_g1.ntypes
      }
      induced_nodes = {
          k: F.asnumpy(v) for k, v in induced_nodes.items()
      }
      assert new_g1.idtype == idtype
      assert new_g2.idtype == idtype
      assert set(induced_nodes["user"]) == set([1, 3, 5, 2, 7, 8, 9])
      assert set(induced_nodes["game"]) == set([3, 4, 5, 6, 7])
      _check(g1, new_g1, induced_nodes)
      _check(g2, new_g2, induced_nodes)
  
      # Test multiple graphs with always_preserve given a tensor
      new_g3, new_g4 = dgl.compact_graphs(
          [g3, g4], always_preserve=F.tensor([1, 7], dtype=idtype)
      )
      induced_nodes = {
          ntype: new_g3.nodes[ntype].data[dgl.NID]
          for ntype in new_g3.ntypes
      }
      induced_nodes = {
          k: F.asnumpy(v) for k, v in induced_nodes.items()
      }
  
      assert new_g3.idtype == idtype
      assert new_g4.idtype == idtype
  
      assert set(induced_nodes["user"]) == set([0, 1, 2, 3, 5, 7])
      _check(g3, new_g3, induced_nodes)
      _check(g4, new_g4, induced_nodes)
  
imports: import math
  import os
  import unittest
  import backend
  import dgl
  import dgl.function
  import dgl.partition
  import networkx
  import numpy
  import pytest
  from scipy import sparse
  from utils import parametrize_idtype
  from utils.graph_cases import get_cases
label_desc: Use image node_id 22 for calling a global function with example usage: test_compact(idtype) without return types