code: def subdomain_from_position_and_image(nim, pos):
      """Keep the set of labels of the image corresponding to a certain index
      so that their position is closest to the prescribed one.
  
      Parameters
      ----------
      mim: NiftiIImage instance, or string path toward such an image
           supposedly a label image
      pos: array of shape(3) or list of length 3,
           the prescribed position
  
      """
      tmp = subdomain_from_image(nim)
      coord = np.array(
          [
              tmp.domain.coord[tmp.label == k].mean(0)
              for k in range(tmp.k)
          ]
      )
      idx = ((coord - pos) ** 2).sum(1).argmin()
      return subdomain_from_array(
          nim.get_fdata() == idx, get_affine(nim)
      )
  
imports: import numpy
  from nibabel import Nifti1Image, load
  from nipy.io.nibcompat import get_affine, get_header
  from .None import discrete_domain
label_desc: Use image node_id 3 for calling a global function with example usage: subdomain_from_position_and_image(nim, pos) and returns: subdomain_from_array