def test_specific_wavelet(): a = dtwavexfm2_np(mandrill, biort=biort('antonini'), qshift=qshift('qshift_06')) b = dtwavexfm2_cl(mandrill, biort=biort('antonini'), qshift=qshift('qshift_06')) _compare_transforms(a, b)
def test_equal_numpy_qshift1(): ha = qshift('qshift_c')[0] hb = qshift('qshift_c')[1] ref = np_coldfilt(mandrill.T, ha, hb).T y_op = rowdfilt(mandrill_t, ha, hb) with tf.Session() as sess: y = sess.run(y_op) np.testing.assert_array_almost_equal(y[0], ref, decimal=4)
def test_modified(): a = dtwavexfm2_np(mandrill, biort=biort('near_sym_b_bp'), qshift=qshift('qshift_b_bp')) b = dtwavexfm2_cl(mandrill, biort=biort('near_sym_b_bp'), qshift=qshift('qshift_b_bp')) _compare_transforms(a, b)
def test_equal_numpy_qshift1(): ha = qshift('qshift_c')[0] hb = qshift('qshift_c')[1] ref = np_colifilt(mandrill, ha, hb) y_op = colifilt(mandrill_t, ha, hb) with tf.Session() as sess: y = sess.run(y_op) np.testing.assert_array_almost_equal(y[0], ref, decimal=4)
def test_equal_small_in(): ha = qshift('qshift_b')[0] hb = qshift('qshift_b')[1] im = mandrill[0:4,0:4] im_t = tf.expand_dims(tf.constant(im, tf.float32), axis=0) ref = np_coldfilt(im, ha, hb) y_op = coldfilt(im_t, ha, hb) with tf.Session() as sess: y = sess.run(y_op) np.testing.assert_array_almost_equal(y[0], ref, decimal=4)
def test_equal_small_in(): ha = qshift('qshift_b')[0] hb = qshift('qshift_b')[1] im = mandrill[0:4, 0:4] im_t = tf.expand_dims(tf.constant(im, tf.float32), axis=0) ref = np_colifilt(im, ha, hb) y_op = colifilt(im_t, ha, hb) with tf.Session() as sess: y = sess.run(y_op) np.testing.assert_array_almost_equal(y[0], ref, decimal=4)
def test_equal_numpy_qshift2(): ha = qshift('qshift_c')[0] hb = qshift('qshift_c')[1] im = mandrill[:508, :504] im_t = tf.expand_dims(tf.constant(im, tf.float32), axis=0) ref = np_coldfilt(im.T, ha, hb).T y_op = rowdfilt(im_t, ha, hb) with tf.Session() as sess: y = sess.run(y_op) np.testing.assert_array_almost_equal(y[0], ref, decimal=4)
def test_equal_numpy_qshift1(): h = qshift('qshift_c')[0] ref = np_colfilter(mandrill.T, h).T y_op = rowfilter(mandrill_t, h) with tf.Session() as sess: y = sess.run(y_op) np.testing.assert_array_almost_equal(y[0], ref, decimal=4)
def test_coldfilt(): h0o, g0o, h1o, g1o = biort('near_sym_b') h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d') A = colifilt(mandrill, g0b, g0a) assert_almost_equal_to_summary(A, verif['mandrill_colifilt'], tolerance=TOLERANCE)
def test_simple_level_4_recon_custom_wavelets(): # Test for perfect reconstruction with 3 levels b = biort('legall') q = qshift('qshift_06') Yl, Yh = dtwavexfm3(ellipsoid, 4, biort=b, qshift=q) ellipsoid_recon = dtwaveifm3(Yl, Yh, biort=b, qshift=q) assert ellipsoid.size == ellipsoid_recon.size assert np.max(np.abs(ellipsoid - ellipsoid_recon)) < TOLERANCE
def test_equal_numpy_qshift2(): h = qshift('qshift_c')[0] im = mandrill[52:407, 30:401] im_t = tf.expand_dims(tf.constant(im, tf.float32), axis=0) ref = np_colfilter(im, h) y_op = colfilter(im_t, h) with tf.Session() as sess: y = sess.run(y_op) np.testing.assert_array_almost_equal(y[0], ref, decimal=4)
def test_equal_numpy_qshift2(): h = qshift('qshift_c')[0] im = mandrill[15:307, 40:267] im_t = tf.expand_dims(tf.constant(im, tf.float32), axis=0) ref = np_colfilter(im.T, h).T y_op = rowfilter(im_t, h) with tf.Session() as sess: y = sess.run(y_op) np.testing.assert_array_almost_equal(y[0], ref, decimal=4)
def main(): GRID_SIZE = 128 SPHERE_RAD = int(0.45 * GRID_SIZE) + 0.5 # Compute an image of the sphere grid = np.arange(-(GRID_SIZE>>1), GRID_SIZE>>1) X, Y, Z = np.meshgrid(grid, grid, grid) r = np.sqrt(X*X + Y*Y + Z*Z) sphere = (0.5 + np.clip(SPHERE_RAD-r, -0.5, 0.5)).astype(np.float32) # Specify number of levels and wavelet family to use nlevels = 2 b = biort('near_sym_a') q = qshift('qshift_a') # Form the DT-CWT of the sphere. We use discard_level_1 since we're # uninterested in the inverse transform and this saves us some memory. Yl, Yh = dtwavexfm3(sphere, nlevels, b, q, discard_level_1=False) # Plot maxima figure(figsize=(8,8)) ax = gcf().add_subplot(1,1,1, projection='3d') ax.set_aspect('equal') ax.view_init(35, 75) # Plot unit sphere +ve octant thetas = np.linspace(0, np.pi/2, 10) phis = np.linspace(0, np.pi/2, 10) tris = [] rad = 0.99 # so that points plotted latter are not z-clipped for t1, t2 in zip(thetas[:-1], thetas[1:]): for p1, p2 in zip(phis[:-1], phis[1:]): tris.append([ sphere_to_xyz(rad, t1, p1), sphere_to_xyz(rad, t1, p2), sphere_to_xyz(rad, t2, p2), sphere_to_xyz(rad, t2, p1), ]) sphere = Poly3DCollection(tris, facecolor='w', edgecolor=(0.6,0.6,0.6)) ax.add_collection3d(sphere) locs = [] scale = 1.1 for idx in range(Yh[-1].shape[3]): Z = Yh[-1][:,:,:,idx] C = np.abs(Z) max_loc = np.asarray(np.unravel_index(np.argmax(C), C.shape)) - np.asarray(C.shape)*0.5 max_loc /= np.sqrt(np.sum(max_loc * max_loc)) # Only record directions in the +ve octant (or those from the -ve quadrant # which can be flipped). if np.all(np.sign(max_loc) == 1): locs.append(max_loc) ax.text(max_loc[0] * scale, max_loc[1] * scale, max_loc[2] * scale, str(idx+1)) elif np.all(np.sign(max_loc) == -1): locs.append(-max_loc) ax.text(-max_loc[0] * scale, -max_loc[1] * scale, -max_loc[2] * scale, str(idx+1)) # Plot all directions as a scatter plot locs = np.asarray(locs) ax.scatter(locs[:,0], locs[:,1], locs[:,2], c=np.arange(locs.shape[0])) w = 1.1 ax.auto_scale_xyz([0, w], [0, w], [0, w]) legend() title('3D DT-CWT subband directions for +ve hemisphere quadrant') tight_layout() show()
def test_wrong_type_b(): with raises(ValueError): qshift('antonini')
def test_qshift(): h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d') y = coldfilt(lena, h1b, h1b) z = coldfilt_gold(lena, h1b, h1a) assert_almost_equal(y, z)
def test_qshift_d(): coeffs = qshift('qshift_d') assert len(coeffs) == 8 for v in coeffs: assert v.shape[0] == 18
def test_qshift_odd_len_input_2(): h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d') y = colifilt(mandrill, h1a[1:-1], h1b[1:-1]) z = colifilt_gold(mandrill, h1a[1:-1], h1b[1:-1]) assert_almost_equal(y, z)
def test_qshift(): y = colfilter(mandrill, qshift('qshift_a')[0]) assert y.shape == (mandrill.shape[0] + 1, mandrill.shape[1]) z = colfilter_gold(mandrill, qshift('qshift_a')[0]) assert_almost_equal(y, z)
def test_qshift(): h = qshift('qshift_a')[0] y_op = colfilter(mandrill_t, h) assert y_op.get_shape()[1:] == (mandrill.shape[0]+1, mandrill.shape[1])
def test_simple_custom_filter(): vec = np.random.rand(630) Yl, Yh = dtwavexfm(vec, 4, biort('legall'), qshift('qshift_06')) vec_recon = dtwaveifm(Yl, Yh, biort('legall'), qshift('qshift_06')) assert np.max(np.abs(vec_recon - vec)) < TOLERANCE
def test_reconstruct_custom_filter(): # Reconstruction up to tolerance Yl, Yh = dtwavexfm2(lena, 4, biort('legall'), qshift('qshift_06')) lena_recon = dtwaveifm2(Yl, Yh, biort('legall'), qshift('qshift_06')) assert np.all(np.abs(lena_recon - lena) < TOLERANCE)
def main(): GRID_SIZE = 128 SPHERE_RAD = int(0.45 * GRID_SIZE) + 0.5 # Compute an image of the sphere grid = np.arange(-(GRID_SIZE >> 1), GRID_SIZE >> 1) X, Y, Z = np.meshgrid(grid, grid, grid) r = np.sqrt(X * X + Y * Y + Z * Z) sphere = (0.5 + np.clip(SPHERE_RAD - r, -0.5, 0.5)).astype(np.float32) # Specify number of levels and wavelet family to use nlevels = 2 b = biort('near_sym_a') q = qshift('qshift_a') # Form the DT-CWT of the sphere. We use discard_level_1 since we're # uninterested in the inverse transform and this saves us some memory. Yl, Yh = dtwavexfm3(sphere, nlevels, b, q, discard_level_1=False) # Plot maxima figure(figsize=(8, 8)) ax = gcf().add_subplot(1, 1, 1, projection='3d') ax.set_aspect('equal') ax.view_init(35, 75) # Plot unit sphere +ve octant thetas = np.linspace(0, np.pi / 2, 10) phis = np.linspace(0, np.pi / 2, 10) tris = [] rad = 0.99 # so that points plotted latter are not z-clipped for t1, t2 in zip(thetas[:-1], thetas[1:]): for p1, p2 in zip(phis[:-1], phis[1:]): tris.append([ sphere_to_xyz(rad, t1, p1), sphere_to_xyz(rad, t1, p2), sphere_to_xyz(rad, t2, p2), sphere_to_xyz(rad, t2, p1), ]) sphere = Poly3DCollection(tris, facecolor='w', edgecolor=(0.6, 0.6, 0.6)) ax.add_collection3d(sphere) locs = [] scale = 1.1 for idx in range(Yh[-1].shape[3]): Z = Yh[-1][:, :, :, idx] C = np.abs(Z) max_loc = np.asarray(np.unravel_index( np.argmax(C), C.shape)) - np.asarray(C.shape) * 0.5 max_loc /= np.sqrt(np.sum(max_loc * max_loc)) # Only record directions in the +ve octant (or those from the -ve quadrant # which can be flipped). if np.all(np.sign(max_loc) == 1): locs.append(max_loc) ax.text(max_loc[0] * scale, max_loc[1] * scale, max_loc[2] * scale, str(idx + 1)) elif np.all(np.sign(max_loc) == -1): locs.append(-max_loc) ax.text(-max_loc[0] * scale, -max_loc[1] * scale, -max_loc[2] * scale, str(idx + 1)) # Plot all directions as a scatter plot locs = np.asarray(locs) ax.scatter(locs[:, 0], locs[:, 1], locs[:, 2], c=np.arange(locs.shape[0])) w = 1.1 ax.auto_scale_xyz([0, w], [0, w], [0, w]) legend() title('3D DT-CWT subband directions for +ve hemisphere quadrant') tight_layout() show()
def test_specific_wavelet(): Yl, Yh = dtwavexfm2(mandrill, biort=biort('antonini'), qshift=qshift('qshift_06'))
def test_qshift(): y = colfilter(lena, qshift('qshift_a')[0]) assert y.shape == (lena.shape[0]+1, lena.shape[1]) z = colfilter_gold(lena, qshift('qshift_a')[0]) assert_almost_equal(y, z)
def test_reconstruct_custom_filter(): # Reconstruction up to tolerance Yl, Yh = dtwavexfm2(mandrill, 4, biort('legall'), qshift('qshift_06')) mandrill_recon = dtwaveifm2(Yl, Yh, biort('legall'), qshift('qshift_06')) assert np.all(np.abs(mandrill_recon - mandrill) < TOLERANCE)
def test_real_wavelet(): h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d') A = coldfilt(mandrill[:,:511], h1b, h1a) B = coldfilt_gold(mandrill[:,:511], h1b, h1a) assert_almost_equal(A, B)
def test_specific_wavelet(): Yl, Yh = dtwavexfm2(lena, biort=biort('antonini'), qshift=qshift('qshift_06'))
def test_qshift(): h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d') y = coldfilt(mandrill, h1b, h1b) z = coldfilt_gold(mandrill, h1b, h1a) assert_almost_equal(y, z)
def test_reconstruct_custom_filter(): # Reconstruction up to tolerance Yl, Yh = dtwavexfm2(mandrill, 4, biort("legall"), qshift("qshift_06")) mandrill_recon = dtwaveifm2(Yl, Yh, biort("legall"), qshift("qshift_06")) assert np.all(np.abs(mandrill_recon - mandrill) < TOLERANCE)
def test_qshift(): y = colfilter(lena, qshift('qshift_a')[0]) assert y.shape == (lena.shape[0]+1, lena.shape[1])
def test_non_exist_qshift(): with raises(IOError): qshift('this-does-not-exist')
def test_real_wavelet(): h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d') A = coldfilt(lena[:,:511], h1b, h1a) B = coldfilt_gold(lena[:,:511], h1b, h1a) assert_almost_equal(A, B)
def test_wrong_type_b(): qshift('antonini')
def test_specific_wavelet(): Yl, Yh = dtwavexfm2(mandrill, biort=biort("antonini"), qshift=qshift("qshift_06"))
def test_qshift(): h = qshift('qshift_a')[0] y_op = rowfilter(mandrill_t, h) assert y_op.get_shape()[1:] == (mandrill.shape[0], mandrill.shape[1]+1)
def test_qshift(): y = colfilter(mandrill, qshift('qshift_a')[0]) assert y.shape == (mandrill.shape[0] + 1, mandrill.shape[1])
def test_qshift(): y = colfilter(mandrill, qshift('qshift_a')[0]) assert y.shape == (mandrill.shape[0]+1, mandrill.shape[1])
""" from __future__ import print_function, division import os import timeit import numpy as np from dtcwt.coeffs import biort, qshift from dtcwt.opencl.lowlevel import NoCLPresentError, get_default_queue lena = np.load(os.path.join(os.path.dirname(__file__), '..', 'tests', 'lena.npz'))['lena'] h0o, g0o, h1o, g1o = biort('near_sym_b') h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d') def format_time(t): units = ( (60*60, 'hr'), (60, 'min'), (1, 's'), (1e-3, 'ms') ) for scale, unit in units: if t >= scale: return '{0:.2f} {1}'.format(t/scale, unit) return '{0:.2f} {1}'.format(t*1e6, 'us') def benchmark(statement='pass', setup='pass'): number, repeat = (1, 3) min_time = 0
""" from __future__ import print_function, division import os import timeit import numpy as np from dtcwt.coeffs import biort, qshift from dtcwt.opencl.lowlevel import NoCLPresentError, get_default_queue mandrill = np.load(os.path.join(os.path.dirname(__file__), '..', 'tests', 'mandrill.npz'))['mandrill'] h0o, g0o, h1o, g1o = biort('near_sym_b') h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d') def format_time(t): units = ( (60*60, 'hr'), (60, 'min'), (1, 's'), (1e-3, 'ms') ) for scale, unit in units: if t >= scale: return '{0:.2f} {1}'.format(t/scale, unit) return '{0:.2f} {1}'.format(t*1e6, 'us') def benchmark(statement='pass', setup='pass'): number, repeat = (1, 3) min_time = 0
def test_qshift(): y = colfilter(mandrill, qshift('qshift_a')[0]) assert y.shape == (mandrill.shape[0]+1, mandrill.shape[1]) z = colfilter_gold(mandrill, qshift('qshift_a')[0]) assert_almost_equal(y, z)
def test_non_exist_qshift(): qshift('this-does-not-exist')