code: def find_cut_slices(img, direction="z", n_cuts=7, spacing="auto"):
      """Find 'good' cross-section slicing positions along a given axis.
  
      Parameters
      ----------
      img : 3D Niimg-like object
          See :ref:`extracting_data`.
          The brain map.
  
      direction : string, default='z'
          Sectional direction; possible values are "x", "y", or "z".
  
      n_cuts : int, default=7
          Number of cuts in the plot.
  
      spacing : 'auto' or int, default='auto'
          Minimum spacing between cuts (in voxels, not millimeters)
          if 'auto', the spacing is .5 / n_cuts * img_length.
  
      Returns
      -------
      cut_coords : 1D array of length n_cuts
          The computed cut_coords.
  
      Notes
      -----
      This code works by iteratively locating peak activations that are
      separated by a distance of at least 'spacing'. If n_cuts is very
      large and all the activated regions are covered, cuts with a spacing
      less than 'spacing' will be returned.
  
      Warnings
      --------
      If a non-diagonal img is given. This function automatically reorders
      img to get it back to diagonal. This is to avoid finding same cuts in
      the slices.
  
      """
      # misc
      if direction not in "xyz":
          raise ValueError(
              f"'direction' must be one of 'x', 'y', or 'z'. Got '{direction}'"
          )
      axis = "xyz".index(direction)
      img = check_niimg_3d(img)
      affine = img.affine
      if not np.all(np.diag(affine)[:3]):
          warnings.warn(
              "A non-diagonal affine is found in the given "
              "image. Reordering the image to get diagonal affine "
              "for finding cuts in the slices.",
              stacklevel=2,
          )
          # resample is set to avoid issues with an image having a non-diagonal
          # affine and rotation.
          img = reorder_img(img, resample="nearest")
          affine = img.affine
      # note: orig_data is a copy of img._data_cache thanks to np.abs
      orig_data = np.abs(safe_get_data(img))
      this_shape = orig_data.shape[axis]
  
      if not isinstance(n_cuts, numbers.Number):
          raise ValueError(
              "The number of cuts (n_cuts) must be an integer "
              "greater than or equal to 1. "
              f"You provided a value of n_cuts={n_cuts}."
          )
  
      # BF issue #575: Return all the slices along and axis if this axis
      # is the display mode and there are at least as many requested
      # n_slices as there are slices.
      if n_cuts > this_shape:
          warnings.warn(
              "Too many cuts requested for the data: "
              f"n_cuts={n_cuts}, data size={this_shape}."
          )
          return _transform_cut_coords(
              np.arange(this_shape), direction, affine
          )
  
      # To smooth data that might be np.int or np.uint,
      # first convert it to float.
      data = orig_data.copy()
      if data.dtype.kind in ("i", "u"):
          data = data.astype(np.float64)
  
      data = smooth_array(data, affine, fwhm="fast")
  
      # to control floating point error problems
      # during given input value "n_cuts"
      epsilon = np.finfo(np.float32).eps
      difference = abs(round(n_cuts) - n_cuts)
      if round(n_cuts) < 1.0 or difference > epsilon:
          message = (
              f"Image has {this_shape} slices in direction {direction}. "
              "Therefore, the number of cuts "
              f"must be between 1 and {this_shape}. "
              f"You provided n_cuts={n_cuts}."
          )
          raise ValueError(message)
      else:
          n_cuts = int(round(n_cuts))
  
      if spacing == "auto":
          spacing = max(int(0.5 / n_cuts * data.shape[axis]), 1)
  
      slices = [slice(None, None), slice(None, None), slice(None, None)]
  
      cut_coords = list()
  
      for _ in range(n_cuts):
          # Find a peak
          max_along_axis = np.unravel_index(
              np.abs(data).argmax(), data.shape
          )[axis]
  
          # cancel out the surroundings of the peak
          start = max(0, max_along_axis - spacing)
          stop = max_along_axis + spacing
          slices[axis] = slice(start, stop)
          # We don't actually fully zero the neighborhood, to avoid ending
          # up with fully zeros if n_cuts is too big: we can do multiple
          # passes on the data
          data[tuple(slices)] *= 1.0e-3
  
          cut_coords.append(max_along_axis)
  
      # We sometimes get duplicated cuts, so we add cuts at the beginning
      # and the end
      cut_coords = np.unique(cut_coords).tolist()
      while len(cut_coords) < n_cuts:
          # Candidates for new cuts:
          slice_below = min(cut_coords) - 2
          slice_above = max(cut_coords) + 2
          candidates = [slice_above]
          # One slice where there is the biggest gap in the existing
          # cut_coords
          if len(cut_coords) > 1:
              middle_idx = np.argmax(np.diff(cut_coords))
              slice_middle = int(
                  0.5
                  * (
                      cut_coords[middle_idx]
                      + cut_coords[middle_idx + 1]
                  )
              )
              if slice_middle not in cut_coords:
                  candidates.append(slice_middle)
          if slice_below >= 0:
              # We need positive slice to avoid having negative
              # indices, which would work, but not the way we think of them
              candidates.append(slice_below)
          best_weight = -10
          for candidate in candidates:
              if candidate >= this_shape:
                  this_weight = 0
              else:
                  this_weight = np.sum(
                      np.rollaxis(orig_data, axis)[candidate]
                  )
              if this_weight > best_weight:
                  best_candidate = candidate
                  best_weight = this_weight
  
          cut_coords.append(best_candidate)
          cut_coords = np.unique(cut_coords).tolist()
  
      cut_coords = np.array(cut_coords)
      cut_coords.sort()
  
      return _transform_cut_coords(cut_coords, direction, affine)
  
file_path: nilearn/nilearn/plotting/find_cuts.py
imports: import numbers
  import warnings
  import numpy
  from scipy.ndimage import center_of_mass, find_objects, label
  from nilearn.image import get_data
  from .._utils import check_niimg_3d, check_niimg_4d
  from .._utils.extmath import fast_abs_percentile
  from .._utils.ndimage import largest_connected_component
  from .._utils.niimg import safe_get_data
  from .._utils.numpy_conversions import as_ndarray
  from ..image import iter_img, new_img_like, reorder_img
  from ..image.image import smooth_array
  from ..image.resampling import coord_transform, get_mask_bounds
label_desc: Use image node_id 4 for calling a global function with example usage: find_cut_slices(img, direction, n_cuts, spacing) and returns: _transform_cut_coords, _transform_cut_coords