code: def test_to_simple(idtype):
      # homogeneous graph
      g = dgl.graph((F.tensor([0, 1, 2, 1]), F.tensor([1, 2, 0, 2])))
      g.ndata["h"] = F.tensor([[0.0], [1.0], [2.0]])
      g.edata["h"] = F.tensor([[3.0], [4.0], [5.0], [6.0]])
      sg, wb = dgl.to_simple(g, writeback_mapping=True)
      u, v = g.all_edges(form="uv", order="eid")
      u = F.asnumpy(u).tolist()
      v = F.asnumpy(v).tolist()
      uv = list(zip(u, v))
      eid_map = F.asnumpy(wb)
  
      su, sv = sg.all_edges(form="uv", order="eid")
      su = F.asnumpy(su).tolist()
      sv = F.asnumpy(sv).tolist()
      suv = list(zip(su, sv))
      sc = F.asnumpy(sg.edata["count"])
      assert set(uv) == set(suv)
      for i, e in enumerate(suv):
          assert sc[i] == sum(e == _e for _e in uv)
      for i, e in enumerate(uv):
          assert eid_map[i] == suv.index(e)
      # shared ndata
      assert F.array_equal(sg.ndata["h"], g.ndata["h"])
      assert "h" not in sg.edata
      # new ndata to sg
      sg.ndata["hh"] = F.tensor([[0.0], [1.0], [2.0]])
      assert "hh" not in g.ndata
  
      sg = dgl.to_simple(g, writeback_mapping=False, copy_ndata=False)
      assert "h" not in sg.ndata
      assert "h" not in sg.edata
  
      # test coalesce edge feature
      sg = dgl.to_simple(g, copy_edata=True, aggregator="arbitrary")
      assert F.allclose(sg.edata["h"][1], F.tensor([4.0]))
      sg = dgl.to_simple(g, copy_edata=True, aggregator="sum")
      assert F.allclose(sg.edata["h"][1], F.tensor([10.0]))
      sg = dgl.to_simple(g, copy_edata=True, aggregator="mean")
      assert F.allclose(sg.edata["h"][1], F.tensor([5.0]))
  
      # heterogeneous graph
      g = dgl.heterograph(
          {
              ("user", "follow", "user"): (
                  [0, 1, 2, 1, 1, 1],
                  [1, 3, 2, 3, 4, 4],
              ),
              ("user", "plays", "game"): (
                  [3, 2, 1, 1, 3, 2, 2],
                  [5, 3, 4, 4, 5, 3, 3],
              ),
          },
          idtype=idtype,
          device=F.ctx(),
      )
      g.nodes["user"].data["h"] = F.tensor([0, 1, 2, 3, 4])
      g.nodes["user"].data["hh"] = F.tensor([0, 1, 2, 3, 4])
      g.edges["follow"].data["h"] = F.tensor([0, 1, 2, 3, 4, 5])
      sg, wb = dgl.to_simple(
          g,
          return_counts="weights",
          writeback_mapping=True,
          copy_edata=True,
      )
      g.nodes["game"].data["h"] = F.tensor([0, 1, 2, 3, 4, 5])
  
      for etype in g.canonical_etypes:
          u, v = g.all_edges(form="uv", order="eid", etype=etype)
          u = F.asnumpy(u).tolist()
          v = F.asnumpy(v).tolist()
          uv = list(zip(u, v))
          eid_map = F.asnumpy(wb[etype])
  
          su, sv = sg.all_edges(form="uv", order="eid", etype=etype)
          su = F.asnumpy(su).tolist()
          sv = F.asnumpy(sv).tolist()
          suv = list(zip(su, sv))
          sw = F.asnumpy(sg.edges[etype].data["weights"])
  
          assert set(uv) == set(suv)
          for i, e in enumerate(suv):
              assert sw[i] == sum(e == _e for _e in uv)
          for i, e in enumerate(uv):
              assert eid_map[i] == suv.index(e)
      # shared ndata
      assert F.array_equal(
          sg.nodes["user"].data["h"], g.nodes["user"].data["h"]
      )
      assert F.array_equal(
          sg.nodes["user"].data["hh"], g.nodes["user"].data["hh"]
      )
      assert "h" not in sg.nodes["game"].data
      # new ndata to sg
      sg.nodes["user"].data["hhh"] = F.tensor([0, 1, 2, 3, 4])
      assert "hhh" not in g.nodes["user"].data
      # share edata
      feat_idx = F.asnumpy(wb[("user", "follow", "user")])
      _, indices = np.unique(feat_idx, return_index=True)
      assert np.array_equal(
          F.asnumpy(sg.edges["follow"].data["h"]),
          F.asnumpy(g.edges["follow"].data["h"])[indices],
      )
  
      sg = dgl.to_simple(g, writeback_mapping=False, copy_ndata=False)
      for ntype in g.ntypes:
          assert g.num_nodes(ntype) == sg.num_nodes(ntype)
      assert "h" not in sg.nodes["user"].data
      assert "hh" not in sg.nodes["user"].data
  
      # verify DGLGraph.edge_ids() after dgl.to_simple()
      # in case ids are not initialized in underlying coo2csr()
      u = F.tensor([0, 1, 2])
      v = F.tensor([1, 2, 3])
      eids = F.tensor([0, 1, 2])
      g = dgl.graph((u, v))
      assert F.array_equal(g.edge_ids(u, v), eids)
      sg = dgl.to_simple(g)
      assert F.array_equal(sg.edge_ids(u, v), eids)
  
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 23 for calling a global function with example usage: test_to_simple(idtype) without return types