code: def safe_get_data(img, ensure_finite=False, copy_data=False):
      """Get the data in the image without having a side effect \
      on the Nifti1Image object.
  
      Parameters
      ----------
      img: Nifti image/object
          Image to get data.
  
      ensure_finite: bool
          If True, non-finite values such as (NaNs and infs) found in the
          image will be replaced by zeros.
  
      copy_data: bool, default=False
          If true, the returned data is a copy of the img data.
  
      Returns
      -------
      data: numpy array
          nilearn.image.get_data return from Nifti image.
      """
      if copy_data:
          img = copy.deepcopy(img)
  
      # typically the line below can double memory usage
      # that's why we invoke a forced call to the garbage collector
      gc.collect()
  
      data = _get_data(img)
      if ensure_finite:
          non_finite_mask = np.logical_not(np.isfinite(data))
          if non_finite_mask.sum() > 0:  # any non_finite_mask values?
              warn(
                  "Non-finite values detected. "
                  "These values will be replaced with zeros."
              )
              data[non_finite_mask] = 0
  
      return data
  
file_path: nilearn/nilearn/_utils/niimg.py
imports: import collections.abc
  import copy
  import gc
  from pathlib import Path
  from warnings import warn
  import nibabel
  import numpy
  from .helpers import stringify_path
label_desc: Use image node_id 2 for calling a global function with example usage: safe_get_data(img, ensure_finite, copy_data) and returns: data