def testImageDctPreservesVolume(self):
     """Tests that image_dct() is volume preserving."""
     for _ in range(4):
         im = np.float32(np.random.uniform(size=(4, 4, 2)))
         jacobian = util.compute_jacobian(util.image_dct, im)
         # Assert that the determinant of the Jacobian is close to 1.
         det = np.linalg.det(jacobian)
         self.assertAllClose(det, 1., atol=1e-5, rtol=1e-5)
 def testRgbToSyuvPreservesVolume(self):
     """Tests that rgb_to_syuv() is volume preserving."""
     for _ in range(4):
         im = np.float32(np.random.uniform(size=(1, 1, 3)))
         jacobian = util.compute_jacobian(util.rgb_to_syuv, im)
         # Assert that the determinant of the Jacobian is close to 1.
         det = np.linalg.det(jacobian)
         self.assertAllClose(det, 1., atol=1e-5, rtol=1e-5)
  def testWaveletTransformationIsVolumePreserving(self):
    """Tests that construct() is volume preserving when size is a power of 2."""
    sz = (1, 4, 4)
    num_levels = 2
    im = np.float32(np.random.uniform(0., 1., sz))
    for wavelet_type in wavelet.generate_filters():
      # Construct the Jacobian of construct().
      def fun(z):
        # pylint: disable=cell-var-from-loop
        return wavelet.flatten(wavelet.construct(z, num_levels, wavelet_type))

      jacobian = util.compute_jacobian(fun, im)
      # Assert that the determinant of the Jacobian is close to 1.
      det = np.linalg.det(jacobian)
      self.assertAllClose(det, 1., atol=1e-5, rtol=1e-5)