class_docstr: Unit test for NiftyregImageResamplingLayer
class_docstr_tok: ["Unit","test","for","NiftyregImageResamplingLayer"]
code: def test_image_gradient(self):
      for img in self._get_images(True):
          basedata = img.get_data()
  
          imgshape = list(basedata.shape)
          nof_dims = len(imgshape)
          for nof_mods in range(1, 3):
              image_batch_shape = [1] + imgshape + [nof_mods]
              disp_batch_shape = [1] + imgshape + [nof_dims]
  
              multimod_data = []
              for j in range(nof_mods):
                  multimod_data.append((j + 1) * basedata)
              imgdata = np.stack(multimod_data, axis=-1)
  
              for _, inter in self.INTERPOLATIONS:
                  for bdy in ("REPLICATE", "ZERO", "SYMMETRIC"):
                      u = 3.5001
                      d = 1
                      disp = self._make_constant_displacement_image(
                          u, d, imgdata[..., 0].reshape(imgshape)
                      )
  
                      with self.session() as sess:
                          tfimg = tf.constant(
                              imgdata.reshape(image_batch_shape),
                              dtype=tf.float32,
                          )
                          tfdisp = tf.constant(
                              disp.reshape(disp_batch_shape),
                              dtype=tf.float32,
                          )
  
                          warped = NiftyregImageResamplingLayer(
                              interpolation=inter, boundary=bdy
                          )
                          warped = warped(tfimg, tfdisp)
                          dummy_cost = tf.reduce_mean(tf.pow(warped, 2))
  
                          tgrad, refgrad = tft.compute_gradient(
                              tfimg, image_batch_shape, dummy_cost, ()
                          )
  
                          error = np.power(tgrad - refgrad, 2).sum()
                          refmag = np.power(refgrad, 2).sum()
  
                          self.assertLessEqual(error, 5e-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 4 for calling the WrapperResamplerTest obj's underlying member method code with example usage: obj.test_image_gradient() without return types