class_docstr: Unit test for NiftyregImageResamplingLayer
class_docstr_tok: ["Unit","test","for","NiftyregImageResamplingLayer"]
code: def _test_differential(self, use_gpu, interpolations):
      for floating in self._get_images(True):
          floating_data = floating.get_data()
          nof_dims = len(floating_data.shape)
          nof_mods = 2
  
          if nof_dims == 3:
              # 3D test takes forever (at least with debug builds)
              continue
  
          floating_shape = list(floating_data.shape)
          image_batch_shape = [1] + floating_shape + [nof_mods]
          disp_batch_shape = [1] + floating_shape + [nof_dims]
  
          disp_template = self._make_constant_displacement_image(
              0, 0, floating_data
          )
          u = 2.5001
  
          floating_data = np.stack([floating_data] * nof_mods, axis=-1)
          for m in range(nof_mods):
              floating_data[..., m] = (1 + m) * floating_data[..., m]
  
          for iname in interpolations:
              d = 1
              with self.session(use_gpu=use_gpu) as sess:
                  img = tf.constant(
                      floating_data.reshape(image_batch_shape),
                      dtype=tf.float32,
                  )
                  disp = tf.constant(u, dtype=tf.float32)
  
                  base_field = tf.constant(
                      disp_template.reshape(disp_batch_shape),
                      dtype=tf.float32,
                  )
  
                  disp_field = []
                  for i in range(nof_dims):
                      if i == d:
                          disp_field.append(base_field[..., i] + disp)
                      else:
                          disp_field.append(base_field[..., i])
                  disp_field = tf.stack(disp_field, axis=-1)
  
                  warped = NiftyregImageResamplingLayer(
                      interpolation=iname, boundary="ZERO"
                  )
                  warped = warped(img, disp_field)
  
                  tgrad, refgrad = tft.compute_gradient(
                      disp, (), warped, tuple(image_batch_shape)
                  )
  
                  error = np.power(tgrad - refgrad, 2).sum()
                  refmag = np.power(refgrad, 2).sum()
  
                  self.assertLessEqual(error, 1e-2 * refmag)
  
file_path: NiftyNet/niftynet/contrib/niftyreg_image_resampling/tests/test_python_wrapper.py
imports: from niftynet.contrib.niftyreg_image_resampling.niftyreg_image_resampling import NiftyregImageResamplingLayer
  from niftynet.contrib.niftyreg_image_resampling.tests.test_resampler import ResamplerTest
  import numpy
  import tensorflow
  import tensorflow.test
label_desc: Use image node_id 1 for calling the WrapperResamplerTest obj's underlying member method code with example usage: obj._test_differential(use_gpu, interpolations) without return types