code: def weighted_spatial_coordinates(
      n_samples, img_spatial_size, win_spatial_size, sampler_map
  ):
      """
      Weighted sampling from a map.
      This function uses a cumulative histogram for fast sampling.
  
      see also `sampler_uniform.rand_spatial_coordinates`
  
      :param n_samples: number of random coordinates to generate
      :param img_spatial_size: input image size
      :param win_spatial_size: input window size
      :param sampler_map: sampling prior map, it's spatial shape should be
              consistent with `img_spatial_size`
      :return: (n_samples, N_SPATIAL) coordinates representing sampling
                window centres relative to img_spatial_size
      """
      assert sampler_map is not None, (
          "sampling prior map is not specified, "
          "please check `sampler=` option in the config."
      )
      # Get the cumulative sum of the normalised sorted intensities
      # i.e. first sort the sampling frequencies, normalise them
      # to sum to one, and then accumulate them in order
      assert np.all(
          img_spatial_size[:N_SPATIAL] == sampler_map.shape[:N_SPATIAL]
      ), "image and sampling map shapes do not match"
      win_spatial_size = np.asarray(win_spatial_size, dtype=np.int32)
      cropped_map = crop_sampling_map(sampler_map, win_spatial_size)
      flatten_map = cropped_map.flatten()
      flatten_map_min = np.min(flatten_map)
      if flatten_map_min < 0:
          flatten_map -= flatten_map_min
      normaliser = flatten_map.sum()
      # get the sorting indexes to that we can invert the sorting later on.
      sorted_indexes = np.argsort(flatten_map)
      sorted_data = np.cumsum(
          np.true_divide(flatten_map[sorted_indexes], normaliser)
      )
  
      middle_coords = np.zeros((n_samples, N_SPATIAL), dtype=np.int32)
      for sample in range(n_samples):
          # get n_sample from the cumulative histogram, spaced by 1/n_samples,
          # plus a random perturbation to give us a stochastic sampler
          sample_ratio = 1 - (np.random.random() + sample) / n_samples
          # find the index where the cumulative it above the sample threshold
          try:
              if normaliser == 0:
                  # constant map? reducing to a uniform sampling
                  sample_index = np.random.randint(len(sorted_data))
              else:
                  sample_index = np.argmax(sorted_data >= sample_ratio)
          except ValueError:
              tf.logging.fatal(
                  "unable to choose sampling window based on "
                  "the current frequency map."
              )
              raise
          # invert the sample index to the pre-sorted index
          inverted_sample_index = sorted_indexes[sample_index]
          # get the x,y,z coordinates on the cropped_map
          middle_coords[sample, :N_SPATIAL] = np.unravel_index(
              inverted_sample_index, cropped_map.shape
          )[:N_SPATIAL]
  
      # re-shift coords due to the crop
      half_win = np.floor(win_spatial_size / 2).astype(np.int32)
      middle_coords[:, :N_SPATIAL] = (
          middle_coords[:, :N_SPATIAL] + half_win[:N_SPATIAL]
      )
      return middle_coords
  
file_path: NiftyNet/niftynet/engine/sampler_weighted_v2.py
imports: from __future__ import absolute_import, division, print_function
  import numpy
  import tensorflow
  from niftynet.engine.sampler_uniform_v2 import UniformSampler
  from niftynet.engine.image_window import N_SPATIAL
label_desc: Use image node_id 1 for calling a global function with example usage: weighted_spatial_coordinates(n_samples, img_spatial_size, win_spatial_size, sampler_map) and returns: middle_coords