code: def new_img_like(ref_niimg, data, affine=None, copy_header=False):
      """Create a new image of the same class as the reference image.
  
      Parameters
      ----------
      ref_niimg : Niimg-like object
          Reference image. The new image will be of the same type.
  
      data : :class:`numpy.ndarray`
          Data to be stored in the image. If data dtype is a boolean, then data
          is cast to 'uint8' by default.
  
      .. versionchanged:: 0.9.2
          Changed default dtype casting of booleans from 'int8' to 'uint8'.
  
      affine : 4x4 :class:`numpy.ndarray`, optional
          Transformation matrix.
  
      copy_header : :obj:`bool`, default=False
          Indicated if the header of the reference image should be used to
          create the new image.
  
      Returns
      -------
      Niimg-like object
          A loaded image with the same file type (and, optionally, header)
          as the reference image.
  
      """
      # Hand-written loading code to avoid too much memory consumption
      orig_ref_niimg = ref_niimg
      ref_niimg = stringify_path(ref_niimg)
      is_str = isinstance(ref_niimg, str)
      has_get_data = hasattr(ref_niimg, "get_data")
      has_get_fdata = hasattr(ref_niimg, "get_fdata")
      has_iter = hasattr(ref_niimg, "__iter__")
      has_affine = hasattr(ref_niimg, "affine")
      if has_iter and not any([is_str, has_get_data, has_get_fdata]):
          ref_niimg = ref_niimg[0]
          ref_niimg = stringify_path(ref_niimg)
          is_str = isinstance(ref_niimg, str)
          has_get_data = hasattr(ref_niimg, "get_data")
          has_get_fdata = hasattr(ref_niimg, "get_fdata")
          has_affine = hasattr(ref_niimg, "affine")
      if not ((has_get_data or has_get_fdata) and has_affine):
          if is_str:
              ref_niimg = nibabel.load(ref_niimg)
          else:
              raise TypeError(
                  "The reference image should be a niimg."
                  f" {orig_ref_niimg!r} was passed"
              )
  
      if affine is None:
          affine = ref_niimg.affine
      if data.dtype == bool:
          data = as_ndarray(data, dtype=np.uint8)
      data = _downcast_from_int64_if_possible(data)
      header = None
      if copy_header:
          header = copy.deepcopy(ref_niimg.header)
          try:
              "something" in header
          except TypeError:
              pass
          else:
              if "scl_slope" in header:
                  header["scl_slope"] = 0.0
              if "scl_inter" in header:
                  header["scl_inter"] = 0.0
              # 'glmax' is removed for Nifti2Image. Modify only if 'glmax' is
              # available in header. See issue #1611
              if "glmax" in header:
                  header["glmax"] = 0.0
              if "cal_max" in header:
                  header["cal_max"] = (
                      np.max(data) if data.size > 0 else 0.0
                  )
              if "cal_min" in header:
                  header["cal_min"] = (
                      np.min(data) if data.size > 0 else 0.0
                  )
      klass = ref_niimg.__class__
      if klass is nibabel.Nifti1Pair:
          # Nifti1Pair is an internal class, without a to_filename,
          # we shouldn't return it
          klass = nibabel.Nifti1Image
      return klass(data, affine, header=header)
  
imports: import collections.abc
  import copy
  import itertools
  import warnings
  import nibabel
  import numpy
  from joblib import Memory, Parallel, delayed
  from scipy.ndimage import gaussian_filter1d, generate_binary_structure, label
  from scipy.stats import scoreatpercentile
  from ..None import signal
  from .._utils import _repr_niimgs, as_ndarray, check_niimg, check_niimg_3d, check_niimg_4d, fill_doc
  from .._utils.exceptions import DimensionError
  from .._utils.helpers import rename_parameters, stringify_path
  from .._utils.niimg import _get_data, safe_get_data
  from .._utils.niimg_conversions import _index_img, check_same_fov, iter_check_niimg
  from .._utils.param_validation import check_threshold
  from .._utils.path_finding import resolve_globbing
label_desc: Use image node_id 15 for calling a global function with example usage: new_img_like(ref_niimg, data, affine, copy_header) and returns: klass