def _test_shift(self, x, tf_shift_fn, np_shift_fn): def tf_fn(): return tf_shift_fn(tf.constant(x)) x_shift_actual = test_utils.exec_tf(tf_fn) x_shift_expect = np_shift_fn(x) assert_array_equal(x_shift_actual, x_shift_expect)
def _test_convolution(self, d, k, mode, actual=None): def tf_fn(): dt = tf.constant(d, dtype=tf.float32) kt = tf.constant(k, dtype=tf.float32) # Determine FFT dimensions and functions pad_dims = fft_utils_tf.get_fft_pad_dims(dt, kt) optim_dims = fft_utils_tf.optimize_dims(pad_dims, mode) fft_fwd, fft_rev = fft_utils_tf.get_fft_tf_fns(dt.shape.ndims) # Run convolution of data 'd' with kernel 'k' dk_fft = fft_fwd(kt, fft_length=optim_dims) dconv = fft_utils_tf.convolve(dt, dk_fft, optim_dims, fft_fwd, fft_rev) # Extract patch from result matching dimensions of original data array return fft_utils_tf.extract(dconv, tf.shape(dt), pad_dims) tf_res = test_utils.exec_tf(tf_fn) np_res = fft_utils_np.convolve(d, k) assert_almost_equal(tf_res, np_res, decimal=3) self.assertEqual(tf_res.shape, np_res.shape) if actual is not None: assert_almost_equal(tf_res, actual, decimal=6)
def _test_padding(self, x, **kwargs): def tf_fn(): xt = tf.constant(x) yt = tf_ops.pad_around_center(xt, **kwargs) zt = tf_ops.unpad_around_center(yt, tf.shape(xt)) return zt x_actual = test_utils.exec_tf(tf_fn) assert_array_equal(x, x_actual, 'Padded vs un-padded array not equal')
def _test_padding(self, d, k, actual): def tf_fn(): dt = tf.constant(d, dtype=tf.float32) kt = tf.constant(k, dtype=tf.float32) return fft_utils_tf.get_fft_pad_dims(dt, kt) tf_res = test_utils.exec_tf(tf_fn) np_res = fft_utils_np.get_fft_pad_dims(d, k) self.assertTrue(type(tf_res) is np.ndarray) self.assertTrue(type(np_res) is np.ndarray) self.assertTrue(np.array_equal(tf_res, np_res)) self.assertTrue(np.array_equal(tf_res, actual))
def _test_optimize_padding(self, d, k, mode, actual): def tf_fn(): dt = tf.constant(d, dtype=tf.float32) kt = tf.constant(k, dtype=tf.float32) pad_dims = fft_utils_tf.get_fft_pad_dims(dt, kt) return fft_utils_tf.optimize_dims(pad_dims, mode) tf_res = test_utils.exec_tf(tf_fn) np_res = fft_utils_np.optimize_dims(fft_utils_np.get_fft_pad_dims(d, k), mode) self.assertTrue(type(tf_res) is np.ndarray) self.assertTrue(type(np_res) is np.ndarray) assert_array_equal(tf_res, np_res) assert_array_equal(tf_res, actual)