code: def ideal_spacing(data, npoints):
      """
      Tune spacing factors so that the number of voxels in the
      output block matches a given number.
  
      Parameters
      ----------
      data : ndarray or sequence
        Data image to subsample
      npoints : number
        Target number of voxels (negative values will be ignored)
  
      Returns
      -------
      spacing: ndarray
        Spacing factors
      """
      dims = data.shape
      actual_npoints = (data >= 0).sum()
      spacing = np.ones(3, dtype="uint")
  
      while actual_npoints > npoints:
          # Subsample the direction with the highest number of samples
          ddims = dims / spacing
          if ddims[0] >= ddims[1] and ddims[0] >= ddims[2]:
              dir = 0
          elif ddims[1] > ddims[0] and ddims[1] >= ddims[2]:
              dir = 1
          else:
              dir = 2
          spacing[dir] += 1
          subdata = data[:: spacing[0], :: spacing[1], :: spacing[2]]
          actual_npoints = (subdata >= 0).sum()
  
      return spacing
  
imports: import numpy
  import scipy.ndimage
  from ...core.image.image_spaces import as_xyz_image, make_xyz_image, xyz_affine
  from ._registration import _joint_histogram
  from .affine import affine_transforms, inverse_affine, subgrid_affine
  from .chain_transform import ChainTransform
  from .optimizer import configure_optimizer
  from .similarity_measures import similarity_measures
label_desc: Use image node_id 3 for calling a global function with example usage: ideal_spacing(data, npoints) and returns: spacing