code: def generate_labeled_regions(
      shape,
      n_regions,
      random_state=0,
      labels=None,
      affine=np.eye(4),
      dtype="int32",
  ):
      """Generate a 3D volume with labeled regions.
  
      Parameters
      ----------
      shape : :obj:`tuple`
          Shape of returned array.
  
      n_regions : :obj:`int`, default=None
          Number of regions to generate. By default (if "labels" is None),
          add a background with value zero.
  
      random_state : :obj:`int` or :obj:`numpy.random.RandomState` instance, \
                     default=0
          Random number generator, or seed.
  
      labels : iterable, optional
          Labels to use for each zone. If provided, n_regions is unused.
  
      affine : :obj:`numpy.ndarray`, default=np.eye(4)
          Affine of returned image.
  
      dtype : :obj:`type`, default='int32'
          Data type of image.
  
      Returns
      -------
      Niimg-like object
          Data has shape "shape", containing region labels.
  
      """
      n_voxels = shape[0] * shape[1] * shape[2]
      if labels is None:
          labels = range(0, n_regions + 1)
          n_regions += 1
      else:
          n_regions = len(labels)
  
      regions = generate_regions_ts(
          n_voxels, n_regions, random_state=random_state
      )
      # replace weights with labels
      for n, row in zip(labels, regions):
          row[row > 0] = n
      data = np.zeros(shape, dtype=dtype)
      data[np.ones(shape, dtype=bool)] = regions.sum(axis=0).T
      return Nifti1Image(data, affine)
  
imports: from __future__ import annotations
  import itertools
  import json
  import string
  from pathlib import Path
  import numpy
  import pandas
  import scipy.linalg
  import scipy.signal
  from nibabel import Nifti1Image
  from scipy.ndimage import binary_dilation
  from nilearn import datasets, image, maskers, masking
  from nilearn._utils import as_ndarray, logger
  from nilearn.interfaces.bids.utils import bids_entities, check_bids_label, create_bids_filename
  from nilearn.interfaces.fmriprep.tests._testing import get_legal_confound
label_desc: Use image node_id 5 for calling a global function with example usage: generate_labeled_regions(shape, n_regions, random_state, labels, affine, dtype) and returns: Nifti1Image