def wrapped_phase_difference(x, ax): padding = [[0, 0] for _ in range(2)] padding[ax][1] = 1 x1 = np.pad(np.diff(x, n=1, axis=ax), padding, "edge") x2 = np.pad(np.diff(dv.wrap_phase(x + np.pi), n=1, axis=ax), padding, "edge") out = x1 out[np.absolute(x1) > np.absolute(x2)] = x2[np.absolute(x1) > np.absolute(x2)] return out
def test_wrap_phase(): # Scalars assert np.allclose(wrap_phase(0.0), 0.0) assert np.allclose(wrap_phase(+1.0), +1.0) assert np.allclose(wrap_phase(-1.0), -1.0) assert np.allclose(wrap_phase(-6.0), (-6.0 + 2.0 * np.pi)) assert np.allclose(wrap_phase(+6.0), (+6.0 - 2.0 * np.pi)) # ndarray assert np.allclose(wrap_phase(np.zeros((3, 3))), np.zeros((3, 3))) assert np.allclose(wrap_phase(np.ones((3, 3))), np.ones((3, 3))) assert np.allclose(wrap_phase(np.ones((3, 3)) + 2 * np.pi), np.ones( (3, 3))) # ND Array assert np.allclose(wrap_phase(np.array(np.zeros((3, 3)))), np.array(np.zeros((3, 3))))
def test_tf_phase(): a = np.expand_dims(np.linspace(-np.pi, np.pi, 101, "float32"), -1).transpose() b = -a c = dv.wrap_phase(b - a) a_tf = tf.convert_to_tensor(a, "float32") b_tf = tf.convert_to_tensor(b, "float32") c_tf = dvpy.tf.wrapped_phase_difference(a_tf, b_tf) with tf.Session() as s: c_tf_eval = s.run(c_tf) assert np.allclose(c_tf_eval, c)
def test_wrapped_phase_difference(): A = dv.wrap_phase(np.reshape([x for x in range(128)] * 128, (128, 128))) pos_phase_grad = dv.wrapped_phase_difference(A, 1) neg_phase_grad = dv.wrapped_phase_difference(-1 * A, 1) assert np.allclose(pos_phase_grad, np.ones_like(pos_phase_grad)) assert np.allclose(neg_phase_grad, -1 * np.ones_like(neg_phase_grad)) plt.imshow(A, "gray") plt.show() plt.imshow(dv.wrapped_phase_difference(A, 1)) plt.show() plt.imshow(dv.wrapped_phase_difference(-1 * A, 1)) plt.show()
xy_a = (xy_t + xy_p) / 2.0 xy_d = xy_t - xy_p ## Rotation rt_t = [ np.load(os.path.join(tdir, 'true_rt_mat_batch_%d_hgd_3.npy' % (i))) for i in range(3) ] rt_p = [ np.load(os.path.join(tdir, 'prediction_rt_mat_batch_%d_hgd_3.npy' % (i))) for i in range(3) ] rt_t = np.concatenate(rt_t) + np.pi rt_p = np.concatenate(rt_p) + np.pi rt_t = dv.wrap_phase(rt_t).squeeze() rt_p = dv.wrap_phase(rt_p).squeeze() rt_a = dv.wrap_phase(rt_t + rt_p) / 2.0 rt_d = dv.wrap_phase(rt_t - rt_p) ## Zoom zm_t = [ np.load(os.path.join(tdir, 'true_zm_mat_batch_%d_hgd_3.npy' % (i))) for i in range(3) ] zm_p = [ np.load(os.path.join(tdir, 'prediction_zm_mat_batch_%d_hgd_3.npy' % (i))) for i in range(3) ] zm_t = np.concatenate(zm_t).squeeze()
import pylab as plt import dvpy as dv d = np.arange(-10, +10, 0.1) w = dv.wrap_phase(d) plt.plot(d) plt.plot(w) plt.show()
g = np.linspace(-np.pi, np.pi, s) p = np.linspace(-np.pi, np.pi, s) G, P = np.meshgrid(g, p) ## ## ## fig1 = plt.figure(figsize=(6, 5)) fig2 = plt.figure(figsize=(6, 5)) ax1 = fig1.add_subplot(1, 1, 1, projection = '3d') ax2 = fig2.add_subplot(1, 1, 1, projection = '3d') ax1.plot_surface(G, P, (P - G)**2, cmap=cm.jet, rcount = s, ccount = s) ax2.plot_surface(G, P, (dv.wrap_phase(P - G))**2, cmap=cm.jet, rcount = s, ccount = s) for ax in [ax1, ax2]: ax.set_xlabel(r'$\hat{\theta}$', labelpad = 10) ax.set_ylabel(r'$\theta$' , labelpad = 10) ax.invert_yaxis() ax.view_init(elev = 20.0, azim = -80.0) ax.set_zlim([0,45]) ax.set_xlim([-np.pi, +np.pi]) ax.set_ylim([-np.pi, +np.pi]) ax.set_xticks([-np.pi, 0.0, np.pi]) ax.set_yticks([-np.pi, 0.0, np.pi]) ax.set_xticklabels(['$-\pi$', '$0$', '$+\pi$']) ax.set_yticklabels(['$-\pi$', '$0$', '$+\pi$']) ax1.set_zlabel(r'$\frac{1}{2} (\theta - \hat{\theta})^2$' , labelpad = 10)