code: def connected_regions(
      maps_img,
      min_region_size=1350,
      extract_type="local_regions",
      smoothing_fwhm=6,
      mask_img=None,
  ):
      """Extract brain connected regions into separate regions.
  
      .. note::
          The region size should be defined in mm^3.
          See the documentation for more details.
  
      .. versionadded:: 0.2
  
      Parameters
      ----------
      maps_img : Niimg-like object
          An image of brain activation or atlas maps to be extracted into set of
          separate brain regions.
  
      min_region_size : :obj:`float`, default=1350
          Minimum volume in mm3 for a region to be kept.
          For example, if the :term:`voxel` size is 3x3x3 mm
          then the volume of the :term:`voxel` is 27mm^3.
          Default=1350mm^3, which means
          we take minimum size of 1350 / 27 = 50 voxels.
      %(extract_type)s
      %(smoothing_fwhm)s
          Use this parameter to smooth an image to extract most sparser regions.
  
          .. note::
  
              This parameter is passed to `nilearn.image.image.smooth_array`.
              It will be used only if ``extract_type='local_regions'``.
  
          Default=6.
  
      mask_img : Niimg-like object, optional
          If given, mask image is applied to input data.
          If None, no masking is applied.
  
      Returns
      -------
      regions_extracted_img : :class:`nibabel.nifti1.Nifti1Image`
          Gives the image in 4D of extracted brain regions.
          Each 3D image consists of only one separated region.
  
      index_of_each_map : :class:`numpy.ndarray`
          An array of list of indices where each index denotes the identity
          of each extracted region to their family of brain maps.
  
      See Also
      --------
      nilearn.regions.connected_label_regions : A function can be used for
          extraction of regions on labels based atlas images.
  
      nilearn.regions.RegionExtractor : A class can be used for both
          region extraction on continuous type atlas images and
          also time series signals extraction from regions extracted.
      """
      all_regions_imgs = []
      index_of_each_map = []
      maps_img = check_niimg(maps_img, atleast_4d=True)
      maps = safe_get_data(maps_img, copy_data=True)
      affine = maps_img.affine
      min_region_size = min_region_size / np.abs(
          np.linalg.det(affine[:3, :3])
      )
  
      allowed_extract_types = ["connected_components", "local_regions"]
      if extract_type not in allowed_extract_types:
          message = (
              "'extract_type' should be given "
              f"either of these {allowed_extract_types} "
              f"You provided extract_type='{extract_type}'"
          )
          raise ValueError(message)
  
      if mask_img is not None:
          if not check_same_fov(maps_img, mask_img):
              mask_img = resample_img(
                  mask_img,
                  target_affine=maps_img.affine,
                  target_shape=maps_img.shape[:3],
                  interpolation="nearest",
              )
          mask_data, _ = masking.load_mask_img(mask_img)
          # Set as 0 to the values which are outside of the mask
          maps[mask_data == 0.0] = 0.0
  
      for index in range(maps.shape[-1]):
          regions = []
          map_3d = maps[..., index]
          # Mark the seeds using random walker
          if extract_type == "local_regions":
              smooth_map = smooth_array(
                  map_3d, affine=affine, fwhm=smoothing_fwhm
              )
              seeds = peak_local_max(smooth_map)
              seeds_label, _ = label(seeds)
              # Assign -1 to values which are 0. to indicate to ignore
              seeds_label[map_3d == 0.0] = -1
              rw_maps = random_walker(map_3d, seeds_label)
              # Now simply replace "-1" with "0" for regions separation
              rw_maps[rw_maps == -1] = 0.0
              label_maps = rw_maps
          else:
              # Connected component extraction
              label_maps, n_labels = label(map_3d)
  
          # Takes the size of each labelized region data
          labels_size = np.bincount(label_maps.ravel())
          # set background labels sitting in zero index to zero
          labels_size[0] = 0.0
          for label_id, label_size in enumerate(labels_size):
              if label_size > min_region_size:
                  region_data = (label_maps == label_id) * map_3d
                  region_img = new_img_like(maps_img, region_data)
                  regions.append(region_img)
  
          index_of_each_map.extend([index] * len(regions))
          all_regions_imgs.extend(regions)
  
      regions_extracted_img = concat_imgs(all_regions_imgs)
  
      return regions_extracted_img, index_of_each_map
  
imports: import collections.abc
  import numbers
  import numpy
  from joblib import Memory
  from scipy.ndimage import label
  from scipy.stats import scoreatpercentile
  from nilearn.maskers import NiftiMapsMasker
  from ..None import masking
  from .._utils import check_niimg, check_niimg_3d, check_niimg_4d, fill_doc
  from .._utils.ndimage import peak_local_max
  from .._utils.niimg import safe_get_data
  from .._utils.niimg_conversions import check_same_fov
  from .._utils.segmentation import random_walker
  from ..image import new_img_like, resample_img
  from ..image.image import concat_imgs, smooth_array, threshold_img
label_desc: Use image node_id 3 for calling a global function with example usage: connected_regions(maps_img, min_region_size, extract_type, smoothing_fwhm, mask_img) and returns: regions_extracted_img,  index_of_each_map