code: def from_scipy(sp_mat, eweight_name=None, idtype=None, device=None):
      """Create a graph from a SciPy sparse matrix and return.
  
      Parameters
      ----------
      sp_mat : scipy.sparse.spmatrix
          The graph adjacency matrix. Each nonzero entry ``sp_mat[i, j]`` represents an edge from
          node ``i`` to ``j``. The matrix must have square shape ``(N, N)``, where ``N`` is the
          number of nodes in the graph.
      eweight_name : str, optional
          The edata name for storing the nonzero values of :attr:`sp_mat`. If given, DGL will
          store the nonzero values of :attr:`sp_mat` in ``edata[eweight_name]`` of the returned
          graph.
      idtype : int32 or int64, optional
          The data type for storing the structure-related graph information such as node and
          edge IDs. It should be a framework-specific data type object (e.g., ``torch.int32``).
          By default, DGL uses int64.
      device : device context, optional
          The device of the resulting graph. It should be a framework-specific device object
          (e.g., ``torch.device``). By default, DGL stores the graph on CPU.
  
      Returns
      -------
      DGLGraph
          The created graph.
  
      Notes
      -----
      1. The function supports all kinds of SciPy sparse matrix classes (e.g.,
         :class:`scipy.sparse.csr.csr_matrix`). It converts the input matrix to the COOrdinate
         format using :func:`scipy.sparse.spmatrix.tocoo` before creates a :class:`DGLGraph`.
         Creating from a :class:`scipy.sparse.coo.coo_matrix` is hence the most efficient way.
      2. DGL internally maintains multiple copies of the graph structure in different sparse
         formats and chooses the most efficient one depending on the computation invoked.
         If memory usage becomes an issue in the case of large graphs, use
         :func:`dgl.DGLGraph.formats` to restrict the allowed formats.
  
      Examples
      --------
  
      The following example uses PyTorch backend.
  
      >>> import dgl
      >>> import numpy as np
      >>> import torch
      >>> from scipy.sparse import coo_matrix
  
      Create a small three-edge graph.
  
      >>> # Source nodes for edges (2, 1), (3, 2), (4, 3)
      >>> src_ids = np.array([2, 3, 4])
      >>> # Destination nodes for edges (2, 1), (3, 2), (4, 3)
      >>> dst_ids = np.array([1, 2, 3])
      >>> # Weight for edges (2, 1), (3, 2), (4, 3)
      >>> eweight = np.array([0.2, 0.3, 0.5])
      >>> sp_mat = coo_matrix((eweight, (src_ids, dst_ids)), shape=(5, 5))
      >>> g = dgl.from_scipy(sp_mat)
  
      Retrieve the edge weights.
  
      >>> g = dgl.from_scipy(sp_mat, eweight_name='w')
      >>> g.edata['w']
      tensor([0.2000, 0.3000, 0.5000], dtype=torch.float64)
  
      Create a graph on the first GPU with data type int32.
  
      >>> g = dgl.from_scipy(sp_mat, idtype=torch.int32, device='cuda:0')
  
      See Also
      --------
      graph
      from_networkx
      """
      # Sanity check
      num_rows = sp_mat.shape[0]
      num_cols = sp_mat.shape[1]
      if num_rows != num_cols:
          raise DGLError(
              "Expect the number of rows to be the same as the number of columns for "
              "sp_mat, got {:d} and {:d}.".format(num_rows, num_cols)
          )
  
      (sparse_fmt, arrays), urange, vrange = utils.graphdata2tensors(
          sp_mat, idtype
      )
      g = create_from_edges(
          sparse_fmt, arrays, "_N", "_E", "_N", urange, vrange
      )
      if eweight_name is not None:
          g.edata[eweight_name] = F.tensor(sp_mat.data)
      return g.to(device)
  
imports: from collections import defaultdict
  from collections.abc import Mapping
  import networkx
  import numpy
  from scipy.sparse import spmatrix
  from .None import backend, graph_index, heterograph_index, utils
  from .base import DGLError, EID, ETYPE, NID, NTYPE
  from .heterograph import combine_frames, DGLBlock, DGLGraph
label_desc: Use image node_id 8 for calling a global function with example usage: from_scipy(sp_mat, eweight_name, idtype, device) and returns: g