code: def rand_choice_coordinates(
      subject_id,
      img_sizes,
      win_sizes,
      candidates,
      mean_counts_size=None,
      n_samples=1,
      n_samples_rand=0,
  ):
      """
      win_sizes could be different, for example in segmentation network
      input image window size is 32x32x10,
      training label window is 16x16x10, the network reduces x-y plane
      spatial resolution.
      This function handles this situation by first find the largest
      window across these window definitions, and generate the coordinates.
      These coordinates are then adjusted for each of the
      smaller window sizes (the output windows are concentric).
      """
      print(n_samples)
      n_samples_rand = np.min([n_samples_rand, n_samples - 1])
      n_samples_cand = n_samples - n_samples_rand
      uniq_spatial_size = set(
          [
              img_size[:N_SPATIAL]
              for img_size in list(img_sizes.values())
          ]
      )
      if len(uniq_spatial_size) > 1:
          tf.logging.fatal(
              "Don't know how to generate sampling "
              "locations: Spatial dimensions of the "
              "grouped input sources are not "
              "consistent. %s",
              uniq_spatial_size,
          )
          raise NotImplementedError
      uniq_spatial_size = uniq_spatial_size.pop()
  
      # find spatial window location based on the largest spatial window
      spatial_win_sizes = [
          win_size[:N_SPATIAL] for win_size in win_sizes.values()
      ]
      spatial_win_sizes = np.asarray(spatial_win_sizes, dtype=np.int32)
      max_spatial_win = np.max(spatial_win_sizes, axis=0)
      for i in range(0, N_SPATIAL):
          assert (
              uniq_spatial_size[i] >= max_spatial_win[i]
          ), "window size {} is larger than image size {}".format(
              max_spatial_win[i], uniq_spatial_size[i]
          )
  
      # randomly choose n_samples from candidate locations
      candidates_indices = np.vstack(np.where(candidates == 1)).T
      # print(np.min(candidates_indices), np.max(candidates_indices),
      # len(candidates_indices))
      list_indices_fin = np.arange(len(candidates_indices))
      if mean_counts_size is not None:
          # Probability weighting considered
          print(np.sum(mean_counts_size), "proba weighting considered")
          proba = [
              p
              for (c, p) in zip(
                  candidates.flatten(), mean_counts_size.flatten()
              )
              if c >= 1
          ]
          list_indices_fin = np.random.choice(
              list_indices_fin, n_samples_cand, replace=False, p=proba
          )
      else:
          np.random.shuffle(list_indices_fin)
          list_indices_fin = list_indices_fin[:n_samples_cand]
      if n_samples_rand > 0:
          spatial_win_sizes = np.asarray(
              win_sizes[:N_SPATIAL], dtype=np.int32
          )
          max_spatial_win = spatial_win_sizes[0]
          # Create segmentation for this label
          # list_counts = []
          shape_ones = np.asarray(candidates.shape)
          # print(shape_ones, max_spatial_win)
          half_max_size = np.floor(max_spatial_win / 2)
          padding = []
          for i in range(0, len(shape_ones)):
              if i < N_SPATIAL:
                  shape_ones[i] -= 2 * half_max_size
                  padding = padding + [
                      [half_max_size, half_max_size],
                  ]
              else:
                  padding = padding + [
                      [0, 0],
                  ]
          # print(shape_ones, padding)
          rand_one = np.pad(
              np.ones(shape_ones),
              np.asarray(padding, dtype=np.int32),
              "constant",
          )
          # rand_one = np.ones_like(candidates)
          list_possible_rand = np.vstack(np.where(rand_one == 1)).T
          list_indices_rand = np.random.choice(
              list_possible_rand, n_samples_rand, replace=False
          )
          list_indices_fin = np.concatenate(
              (list_indices_fin, list_indices_rand)
          )
  
      max_coords = np.zeros((n_samples, N_SPATIAL), dtype=np.int32)
      half_win = np.floor(np.asarray(win_sizes["image"]) / 2).astype(
          np.int
      )
      for i_sample, ind in enumerate(list_indices_fin):
          max_coords[i_sample, :N_SPATIAL] = (
              candidates_indices[ind][:N_SPATIAL] - half_win[:N_SPATIAL]
          )
      # print(np.min(max_coords), np.max(max_coords))
      # adjust max spatial coordinates based on each spatial window size
      all_coordinates = {}
      for mod in list(win_sizes):
          win_size = win_sizes[mod][:N_SPATIAL]
          half_win_diff = np.floor((max_spatial_win - win_size) / 2.0)
          # print(win_size, half_win_diff, 'win and half_Win_diff')
          # shift starting coords of the window
          # so that smaller windows are centred within the large windows
          spatial_coords = np.zeros(
              (n_samples, N_SPATIAL * 2), dtype=np.int32
          )
          spatial_coords[:, :N_SPATIAL] = (
              max_coords[:, :N_SPATIAL] + half_win_diff[:N_SPATIAL]
          )
          spatial_coords[:, N_SPATIAL:] = (
              spatial_coords[:, :N_SPATIAL] + win_size[:N_SPATIAL]
          )
          # include the subject id
          subject_id = (
              np.ones((n_samples,), dtype=np.int32) * subject_id
          )
          spatial_coords = np.append(
              subject_id[:, None], spatial_coords, axis=1
          )
          all_coordinates[mod] = spatial_coords
      # print(all_coordinates)
      return all_coordinates
  
file_path: NiftyNet/niftynet/contrib/segmentation_selective_sampler/sampler_selective.py
imports: from __future__ import absolute_import, print_function, division
  import numpy
  import tensorflow
  from scipy import ndimage
  from scipy.signal import fftconvolve
  from niftynet.engine.image_window import N_SPATIAL
  from niftynet.engine.sampler_uniform_v2 import UniformSampler
label_desc: Use image node_id 4 for calling a global function with example usage: rand_choice_coordinates(subject_id, img_sizes, win_sizes, candidates, mean_counts_size, n_samples, n_samples_rand) and returns: all_coordinates