code: def _to_graph(
      n_x,
      n_y,
      n_z,
      mask=None,
      img=None,
      return_as=sparse.coo_matrix,
      dtype=None,
  ):
      """Auxiliary function for img_to_graph and grid_to_graph"""
      edges = _make_edges_3d(n_x, n_y, n_z)
  
      if dtype is None:  # To not overwrite input dtype
          if img is None:
              dtype = int
          else:
              dtype = img.dtype
  
      if img is not None:
          img = np.atleast_3d(img)
          weights = _compute_gradient_3d(edges, img)
          if mask is not None:
              edges, weights = _mask_edges_weights(mask, edges, weights)
              diag = img.squeeze()[mask]
          else:
              diag = img.ravel()
          n_voxels = diag.size
      else:
          if mask is not None:
              mask = mask.astype(dtype=bool, copy=False)
              edges = _mask_edges_weights(mask, edges)
              n_voxels = np.sum(mask)
          else:
              n_voxels = n_x * n_y * n_z
          weights = np.ones(edges.shape[1], dtype=dtype)
          diag = np.ones(n_voxels, dtype=dtype)
  
      diag_idx = np.arange(n_voxels)
      i_idx = np.hstack((edges[0], edges[1]))
      j_idx = np.hstack((edges[1], edges[0]))
      graph = sparse.coo_matrix(
          (
              np.hstack((weights, weights, diag)),
              (
                  np.hstack((i_idx, diag_idx)),
                  np.hstack((j_idx, diag_idx)),
              ),
          ),
          (n_voxels, n_voxels),
          dtype=dtype,
      )
      if return_as is np.ndarray:
          return graph.toarray()
      return return_as(graph)
  
imports: from itertools import product
  from numbers import Integral, Number, Real
  import numpy
  from numpy.lib.stride_tricks import as_strided
  from scipy import sparse
  from ..base import BaseEstimator, TransformerMixin, _fit_context
  from ..utils import check_array, check_random_state
  from ..utils._param_validation import Hidden, Interval, RealNotInt, validate_params
label_desc: Use image node_id 4 for calling a global function with example usage: _to_graph(n_x, n_y, n_z, mask, img, return_as, dtype) and returns: return_as, graph