class_docstr: resample inputs according to ``sample_coords``
class_docstr_tok: ["resample","inputs","according","to","``","sample_coords","``"]
code: def _resample_inv_dst_weighting(self, inputs, sample_coords):
      # inverse distance weighting using 2^(sptial_rank) neighbours
      in_size = inputs.shape
      partial_shape = not in_size.is_fully_defined()
      try:
          batch_size = int(in_size[0])
          n_coords = int(sample_coords.shape[0])
          in_spatial_rank = infer_spatial_rank(inputs)
          in_spatial_size = (
              None if partial_shape else in_size.as_list()[1:-1]
          )
      except (TypeError, AssertionError, ValueError):
          tf.logging.fatal(
              "Unknown input shape, at least batch size "
              "and rank of the inputs are required."
          )
          raise
  
      out_rank = len(sample_coords.get_shape())
      binary_neighbour_ids = _binary_neighbour_ids(in_spatial_rank)
      weight_id = [
          [[c, i] for i, c in enumerate(bc)]
          for bc in binary_neighbour_ids
      ]
      sample_coords_shape = [out_rank - 1, 0] + list(
          range(1, out_rank - 1)
      )
      sample_coords = tf.transpose(sample_coords, sample_coords_shape)
  
      if partial_shape or in_spatial_size is None:
          all_coords_f = tf.stack(
              [tf.floor(sample_coords), tf.ceil(sample_coords)]
          )
      else:
          # broadcasting input spatial size for boundary functions
          expanded_spatial_size = [len(in_spatial_size)] + [1] * (
              out_rank - 1
          )
          b_size = tf.reshape(in_spatial_size, expanded_spatial_size)
          # find floor and ceil coordinates
          all_coords_f = tf.stack(
              [
                  self.boundary_func(tf.floor(sample_coords), b_size),
                  self.boundary_func(tf.ceil(sample_coords), b_size),
              ]
          )
  
      # find N weights associated to each output point
      diff = tf.stack(
          [
              tf.squared_difference(
                  sample_coords - EPS, all_coords_f[0]
              ),
              tf.squared_difference(
                  sample_coords + EPS, all_coords_f[1]
              ),
          ]
      )
  
      # gather_nd for both matrices, the same as:
      # point_weights = tf.gather_nd(diff, weight_id)
      # knots_id = tf.gather_nd(all_coords_f, weight_id)
      n_val = tf.gather_nd(
          tf.stack([diff, all_coords_f], axis=-1), weight_id
      )
      n_val = tf.unstack(n_val, axis=-1)
      point_weights, knots_id = n_val[0], n_val[1]
  
      # inverse distance weighting
      # sum_i (w_i*p_i/(sum_j w_j)) where w_i = 1/((p-p_i)^2)
      # point_weights has the shape:100
      # `[N, input_rank, b, sp_dim_0, ..., sp_dim_K]`
      # where:
      #  `N` is 2**source data spatial rank
      #  `b` is batch size,
      #  `sp_dim_0` is the output spatial output 0,
      #
      # `point_weights` represents (p - p_i)^2
      #      with i= 0...2**source_rank neighbours
      # (to do: these operations could be refactored as a resampling kernel)
      point_weights = tf.reduce_sum(point_weights, axis=1)
      # skip this as power = 2.0:
      # self.power = 2.0
      # point_weights = tf.pow(point_weights, self.power / 2.0)
      point_weights = tf.reciprocal(point_weights)
      point_weights = point_weights / tf.reduce_sum(
          point_weights, axis=0
      )
  
      # find N neighbours associated to each output point
      # knots_shape = tf.concat([[0], tf.range(2, out_rank + 1), [1]], 0)
      knots_shape = [0] + list(range(2, out_rank + 1)) + [1]
      knots_id = tf.cast(knots_id, COORDINATES_TYPE)
      knots_id = tf.transpose(knots_id, knots_shape)
  
      # get values of N neighbours
      batch_inputs = tf.unstack(inputs, axis=0)
      batch_knots = tf.unstack(knots_id, axis=1)
      if batch_size == n_coords:
          samples = [
              tf.gather_nd(img, knot)
              for (img, knot) in zip(batch_inputs, batch_knots)
          ]
      elif n_coords == 1 and batch_size > 1:
          samples = [
              tf.gather_nd(img, batch_knots[0]) for img in batch_inputs
          ]
      else:
          raise NotImplementedError
      samples = tf.stack(samples, axis=1)
      # weighted average over N neighbours
      return tf.reduce_sum(
          samples * tf.expand_dims(point_weights, axis=-1), axis=0
      )
  
file_path: NiftyNet/niftynet/layer/resampler.py
imports: from __future__ import absolute_import, division, print_function
  import tensorflow
  from niftynet.layer.base_layer import Layer
  from niftynet.layer.layer_util import infer_spatial_rank
  from niftynet.utilities.util_common import look_up_operations
label_desc: Use image node_id 6 for calling the ResamplerLayer obj's underlying member method code with example usage: obj._resample_inv_dst_weighting(inputs, sample_coords) and returns: tf