code: def _get_labels_data(
      target_img,
      labels_img,
      mask_img=None,
      background_label=0,
      dim=None,
      keep_masked_labels=True,
  ):
      """Get the label data.
  
      Ensures that labels, imgs and mask shapes and affines fit,
      then extracts the data from it.
  
      Parameters
      ----------
      target_img : Niimg-like object
          See :ref:`extracting_data`.
          Image to extract the data from.
  
      labels_img : Niimg-like object
          See :ref:`extracting_data`.
          Regions definition as labels.
          By default, the label zero is used to denote an absence of region.
          Use background_label to change it.
  
      mask_img : Niimg-like object, optional
          See :ref:`extracting_data`.
          Mask to apply to labels before extracting signals.
          Every point outside the mask is considered as background
          (i.e. no region).
  
      background_label : number, default=0
          Number representing background in labels_img.
  
      dim : :obj:`int`, optional
          Integer slices mask for a specific dimension.
      %(keep_masked_labels)s
  
      Returns
      -------
      labels : :obj:`list` or :obj:`tuple`
          Corresponding labels for each signal.
          signal[:, n] was extracted from the region with label labels[n].
  
      labels_data : numpy.ndarray
          Extracted data for each region within the mask.
          Data outside the mask are assigned to the background
          label to restrict signal extraction
  
      See Also
      --------
      nilearn.regions.signals_to_img_labels
      nilearn.regions.img_to_signals_labels
  
      """
      _check_shape_and_affine_compatibility(target_img, labels_img)
  
      labels_data = safe_get_data(labels_img, ensure_finite=True)
  
      if keep_masked_labels:
          labels = list(np.unique(labels_data))
          warnings.warn(
              'Applying "mask_img" before '
              "signal extraction may result in empty region signals in the "
              "output. These are currently kept. "
              "Starting from version 0.13, the default behavior will be "
              "changed to remove them by setting "
              '"keep_masked_labels=False". '
              '"keep_masked_labels" parameter will be removed '
              "in version 0.15.",
              DeprecationWarning,
              stacklevel=3,
          )
  
      # Consider only data within the mask
      use_mask = _check_shape_and_affine_compatibility(
          target_img, mask_img, dim
      )
      if use_mask:
          mask_img = _utils.check_niimg_3d(mask_img)
          mask_data = safe_get_data(mask_img, ensure_finite=True)
          labels_data = labels_data.copy()
          labels_before_mask = {
              int(label) for label in np.unique(labels_data)
          }
          # Applying mask on labels_data
          labels_data[np.logical_not(mask_data)] = background_label
          labels_after_mask = {
              int(label) for label in np.unique(labels_data)
          }
          labels_diff = labels_before_mask.difference(labels_after_mask)
          # Raising a warning if any label is removed due to the mask
          if len(labels_diff) > 0 and (not keep_masked_labels):
              warnings.warn(
                  "After applying mask to the labels image, "
                  "the following labels were "
                  f"removed: {labels_diff}. "
                  f"Out of {len(labels_before_mask)} labels, the "
                  "masked labels image only contains "
                  f"{len(labels_after_mask)} labels "
                  "(including background).",
                  stacklevel=3,
              )
  
      if not keep_masked_labels:
          labels = list(np.unique(labels_data))
  
      if background_label in labels:
          labels.remove(background_label)
  
      return labels, labels_data
  
file_path: nilearn/nilearn/regions/signal_extraction.py
imports: import warnings
  import numpy
  from nibabel import Nifti1Image
  from scipy import linalg, ndimage
  from ..None import _utils, masking
  from .._utils.niimg import safe_get_data
  from ..image import new_img_like
label_desc: Use image node_id 4 for calling a global function with example usage: _get_labels_data(target_img, labels_img, mask_img, background_label, dim, keep_masked_labels) and returns: labels,  labels_data