code: def gradient_id(img, l1_ratio=0.5):
      """Compute gradient + id of an image.
  
      Parameters
      ----------
      img : ndarray, shape (nx, ny, nz, ...)
          N-dimensional image
  
      l1_ratio : float in the interval [0, 1]; optional (default .5)
          Constant that mixes L1 and spatial prior terms in the penalization.
  
      Returns
      -------
      gradient : ndarray, shape (4, nx, ny, nz, ...).
          Spatial gradient of the image: the i-th component along the first
          axis is the gradient along the i-th axis of the original array img.
  
      Raises
      ------
      RuntimeError
  
      """
      if not (0.0 <= l1_ratio <= 1.0):
          raise RuntimeError(
              f"l1_ratio must be in the interval [0, 1]; got {l1_ratio}"
          )
  
      shape = [img.ndim + 1] + list(img.shape)
      gradient = np.zeros(shape, dtype=np.float64)
  
      # the gradient part: 'Clever' code to have a view of the gradient
      # with dimension i stop at -1
      slice_all = [0, slice(None, -1)]
      for d in range(img.ndim):
          gradient[tuple(slice_all)] = np.diff(img, axis=d)
          slice_all[0] = d + 1
          slice_all.insert(1, slice(None))
  
      gradient[:-1] *= 1.0 - l1_ratio
  
      # the identity part
      gradient[-1] = l1_ratio * img
  
      return gradient
  
file_path: nilearn/nilearn/decoding/_objective_functions.py
imports: from functools import partial
  import numpy
  from scipy import linalg
label_desc: Use image node_id 6 for calling a global function with example usage: gradient_id(img, l1_ratio) and returns: gradient