code: def crop_sampling_map(input_map, win_spatial_size):
      """
      Utility function for generating a cropped version of the
      input sampling prior map (the input weight map where the centre of
      the window might be). If the centre of the window was outside of
      this crop area, the patch would be outside of the field of view
  
      :param input_map: the input weight map where the centre of
                        the window might be
      :param win_spatial_size: size of the borders to be cropped
      :return: cropped sampling map
      """
  
      # prepare cropping indices
      _start, _end = [], []
      for win_size, img_size in zip(
          win_spatial_size[:N_SPATIAL], input_map.shape[:N_SPATIAL]
      ):
          # cropping floor of the half window
          d_start = int(win_size / 2.0)
          # using ceil of half window
          d_end = img_size - win_size + int(win_size / 2.0 + 0.6)
  
          _start.append(d_start)
          _end.append(d_end + 1 if d_start == d_end else d_end)
  
      try:
          assert len(_start) == 3
          cropped_map = input_map[
              _start[0] : _end[0],
              _start[1] : _end[1],
              _start[2] : _end[2],
              0,
              0,
          ]
          assert np.all(cropped_map.shape) > 0
      except (IndexError, KeyError, TypeError, AssertionError):
          tf.logging.fatal(
              "incompatible map: %s and window size: %s\n"
              "try smaller (fully-specified) spatial window sizes?",
              input_map.shape,
              win_spatial_size,
          )
          raise
      return cropped_map
  
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 2 for calling a global function with example usage: crop_sampling_map(input_map, win_spatial_size) and returns: cropped_map