code: def _as_pairs(x, ndim, as_index=False):
      """
      Broadcast `x` to an array with the shape (`ndim`, 2).
  
      A helper function for `pad` that prepares and validates arguments like
      `pad_width` for iteration in pairs.
  
      Parameters
      ----------
      x : {None, scalar, array-like}
          The object to broadcast to the shape (`ndim`, 2).
      ndim : int
          Number of pairs the broadcasted `x` will have.
      as_index : bool, optional
          If `x` is not None, try to round each element of `x` to an integer
          (dtype `np.intp`) and ensure every element is positive.
  
      Returns
      -------
      pairs : nested iterables, shape (`ndim`, 2)
          The broadcasted version of `x`.
  
      Raises
      ------
      ValueError
          If `as_index` is True and `x` contains negative elements.
          Or if `x` is not broadcastable to the shape (`ndim`, 2).
      """
      if x is None:
          # Pass through None as a special case, otherwise np.round(x) fails
          # with an AttributeError
          return ((None, None),) * ndim
  
      x = np.array(x)
      if as_index:
          x = np.round(x).astype(np.intp, copy=False)
  
      if x.ndim < 3:
          # Optimization: Possibly use faster paths for cases where `x` has
          # only 1 or 2 elements. `np.broadcast_to` could handle these as well
          # but is currently slower
  
          if x.size == 1:
              # x was supplied as a single value
              x = x.ravel()  # Ensure x[0] works for x.ndim == 0, 1, 2
              if as_index and x < 0:
                  raise ValueError(
                      "index can't contain negative values"
                  )
              return ((x[0], x[0]),) * ndim
  
          if x.size == 2 and x.shape != (2, 1):
              # x was supplied with a single value for each side
              # but except case when each dimension has a single value
              # which should be broadcasted to a pair,
              # e.g. [[1], [2]] -> [[1, 1], [2, 2]] not [[1, 2], [1, 2]]
              x = x.ravel()  # Ensure x[0], x[1] works
              if as_index and (x[0] < 0 or x[1] < 0):
                  raise ValueError(
                      "index can't contain negative values"
                  )
              return ((x[0], x[1]),) * ndim
  
      if as_index and x.min() < 0:
          raise ValueError("index can't contain negative values")
  
      # Converting the array with `tolist` seems to improve performance
      # when iterating and indexing the result (see usage in `pad`)
      return np.broadcast_to(x, (ndim, 2)).tolist()
  
imports: from __future__ import division, absolute_import, print_function
  import numpy
  from numpy.core.overrides import array_function_dispatch
label_desc: Use image node_id 25 for calling a global function with example usage: _as_pairs(x, ndim, as_index) and returns: np, unknown, unknown, unknown