def test_pywt_coeff_to_array_and_array_to_pywt_coeff(): # Verify that the helper function does indeed work as expected wbasis = pywt.Wavelet('db1') mode = 'zpd' nscales = 2 n = 16 # 1D test size_list = coeff_size_list((n,), nscales, wbasis, mode) x = np.random.rand(n) coeff_list = pywt.wavedec(x, wbasis, mode, nscales) coeff_arr = pywt_coeff_to_array(coeff_list, size_list) assert isinstance(coeff_arr, (np.ndarray)) length_of_array = np.prod(size_list[0]) length_of_array += sum(np.prod(shape) for shape in size_list[1:-1]) assert all_equal(len(coeff_arr), length_of_array) coeff_list2 = array_to_pywt_coeff(coeff_arr, size_list) assert all_equal(coeff_list, coeff_list2) reconstruction = pywt.waverec(coeff_list2, wbasis, mode) assert all_almost_equal(reconstruction, x) # 2D test size_list = coeff_size_list((n, n), nscales, wbasis, mode) x = np.random.rand(n, n) coeff_list = pywt.wavedec2(x, wbasis, mode, nscales) coeff_arr = pywt_coeff_to_array(coeff_list, size_list) assert isinstance(coeff_arr, (np.ndarray)) length_of_array = np.prod(size_list[0]) length_of_array += sum(3 * np.prod(shape) for shape in size_list[1:-1]) assert all_equal(len(coeff_arr), length_of_array) coeff_list2 = array_to_pywt_coeff(coeff_arr, size_list) assert all_equal(coeff_list, coeff_list2) reconstruction = pywt.waverec2(coeff_list2, wbasis, mode) assert all_almost_equal(reconstruction, x) # 3D test size_list = coeff_size_list((n, n, n), nscales, wbasis, mode) x = np.random.rand(n, n, n) coeff_dict = wavelet_decomposition3d(x, wbasis, mode, nscales) coeff_arr = pywt_coeff_to_array(coeff_dict, size_list) assert isinstance(coeff_arr, (np.ndarray)) length_of_array = np.prod(size_list[0]) length_of_array += sum(7 * np.prod(shape) for shape in size_list[1:-1]) assert len(coeff_arr) == length_of_array coeff_dict2 = array_to_pywt_coeff(coeff_arr, size_list) reconstruction = wavelet_reconstruction3d(coeff_dict2, wbasis, mode, nscales) assert all_equal(coeff_dict, coeff_dict) assert all_almost_equal(reconstruction, x)
def test_coeff_size_list(): # Verify that the helper function does indeed work as expected shape = (16,) nscale = 3 wbasis = pywt.Wavelet('db1') mode = 'per' size_list1d = coeff_size_list(shape, nscale, wbasis, mode) S1d = [(2,), (2,), (4,), (8,), (16,)] shape = (16, 16) size_list2d = coeff_size_list(shape, nscale, wbasis, mode) S2d = [(2, 2), (2, 2), (4, 4), (8, 8), (16, 16)] shape = (16, 16, 16) size_list3d = coeff_size_list(shape, nscale, wbasis, mode) S3d = [(2, 2, 2), (2, 2, 2), (4, 4, 4), (8, 8, 8), (16, 16, 16)] assert all_equal(size_list1d, S1d) assert all_equal(size_list2d, S2d) assert all_equal(size_list3d, S3d)
def test_dwt(): # Verify that the operator works as axpected # 1D test n = 16 x = np.zeros(n) x[5:10] = 1 wbasis = pywt.Wavelet('db1') nscales = 2 mode = 'sym' size_list = coeff_size_list((n,), nscales, wbasis, mode) # Define a discretized domain domain = odl.FunctionSpace(odl.Interval([-1], [1])) nPoints = np.array([n]) disc_domain = odl.uniform_discr_fromspace(domain, nPoints) disc_phantom = disc_domain.element(x) # Create the discrete wavelet transform operator. # Only the domain of the operator needs to be defined Wop = DiscreteWaveletTransform(disc_domain, nscales, wbasis, mode) # Compute the discrete wavelet transform of discrete imput image coeffs = Wop(disc_phantom) # Determine the correct range for Wop and verify that coeffs # is an element of it ran_size = np.prod(size_list[0]) ran_size += sum(np.prod(shape) for shape in size_list[1:-1]) disc_range = disc_domain.dspace_type(ran_size, dtype=disc_domain.dtype) assert coeffs in disc_range # Compute the inverse wavelet transform reconstruction1 = Wop.inverse(coeffs) # With othogonal wavelets the inverse is the adjoint reconstruction2 = Wop.adjoint(coeffs) # Verify that the output of Wop.inverse and Wop.adjoint are the same assert all_almost_equal(reconstruction1.asarray(), reconstruction2.asarray()) # Verify that reconstructions lie in correct discretized domain assert reconstruction1 in disc_domain assert reconstruction2 in disc_domain assert all_almost_equal(reconstruction1.asarray(), x) assert all_almost_equal(reconstruction2.asarray(), x) # --------------------------------------------------------------- # 2D test n = 16 x = np.zeros((n, n)) x[5:10, 5:10] = 1 wbasis = pywt.Wavelet('db1') nscales = 2 mode = 'sym' size_list = coeff_size_list((n, n), nscales, wbasis, mode) # Define a discretized domain domain = odl.FunctionSpace(odl.Rectangle([-1, -1], [1, 1])) nPoints = np.array([n, n]) disc_domain = odl.uniform_discr_fromspace(domain, nPoints) disc_phantom = disc_domain.element(x) # Create the discrete wavelet transform operator. # Only the domain of the operator needs to be defined Wop = DiscreteWaveletTransform(disc_domain, nscales, wbasis, mode) # Compute the discrete wavelet transform of discrete imput image coeffs = Wop(disc_phantom) # Determine the correct range for Wop and verify that coeffs # is an element of it ran_size = np.prod(size_list[0]) ran_size += sum(3 * np.prod(shape) for shape in size_list[1:-1]) disc_range = disc_domain.dspace_type(ran_size, dtype=disc_domain.dtype) assert coeffs in disc_range # Compute the inverse wavelet transform reconstruction1 = Wop.inverse(coeffs) # With othogonal wavelets the inverse is the adjoint reconstruction2 = Wop.adjoint(coeffs) # Verify that the output of Wop.inverse and Wop.adjoint are the same assert all_almost_equal(reconstruction1.asarray(), reconstruction2.asarray()) # Verify that reconstructions lie in correct discretized domain assert reconstruction1 in disc_domain assert reconstruction2 in disc_domain assert all_almost_equal(reconstruction1.asarray(), x) assert all_almost_equal(reconstruction2.asarray(), x) # ------------------------------------------------------------- # 3D test n = 16 x = np.zeros((n, n, n)) x[5:10, 5:10, 5:10] = 1 wbasis = pywt.Wavelet('db2') nscales = 1 mode = 'per' size_list = coeff_size_list((n, n, n), nscales, wbasis, mode) # Define a discretized domain domain = odl.FunctionSpace(odl.Cuboid([-1, -1, -1], [1, 1, 1])) nPoints = np.array([n, n, n]) disc_domain = odl.uniform_discr_fromspace(domain, nPoints) disc_phantom = disc_domain.element(x) # Create the discrete wavelet transform operator related to 3D transform. Wop = DiscreteWaveletTransform(disc_domain, nscales, wbasis, mode) # Compute the discrete wavelet transform of discrete imput image coeffs = Wop(disc_phantom) # Determine the correct range for Wop and verify that coeffs # is an element of it ran_size = np.prod(size_list[0]) ran_size += sum(7 * np.prod(shape) for shape in size_list[1:-1]) disc_range = disc_domain.dspace_type(ran_size, dtype=disc_domain.dtype) assert coeffs in disc_range # Compute the inverse wavelet transform reconstruction1 = Wop.inverse(coeffs) # With othogonal wavelets the inverse is the adjoint reconstruction2 = Wop.adjoint(coeffs) # Verify that the output of Wop.inverse and Wop.adjoint are the same assert all_almost_equal(reconstruction1, reconstruction2) # Verify that reconstructions lie in correct discretized domain assert reconstruction1 in disc_domain assert reconstruction2 in disc_domain assert all_almost_equal(reconstruction1.asarray(), x) assert all_almost_equal(reconstruction2, disc_phantom)
def test_dwt(): # Verify that the operator works as axpected # 1D test n = 16 x = np.zeros(n) x[5:10] = 1 wbasis = pywt.Wavelet('db1') nscales = 2 mode = 'sym' size_list = coeff_size_list((n,), nscales, wbasis, mode) # Define a discretized domain domain = odl.FunctionSpace(odl.Interval([-1], [1])) nPoints = np.array([n]) disc_domain = odl.uniform_discr_fromspace(domain, nPoints) disc_phantom = disc_domain.element(x) # Create the discrete wavelet transform operator. # Only the domain of the operator needs to be defined Wop = WaveletTransform(disc_domain, nscales, wbasis, mode) # Compute the discrete wavelet transform of discrete imput image coeffs = Wop(disc_phantom) # Determine the correct range for Wop and verify that coeffs # is an element of it ran_size = np.prod(size_list[0]) ran_size += sum(np.prod(shape) for shape in size_list[1:-1]) disc_range = disc_domain.dspace_type(ran_size, dtype=disc_domain.dtype) assert coeffs in disc_range # Compute the inverse wavelet transform reconstruction1 = Wop.inverse(coeffs) # With othogonal wavelets the inverse is the adjoint reconstruction2 = Wop.adjoint(coeffs) # Verify that the output of Wop.inverse and Wop.adjoint are the same assert all_almost_equal(reconstruction1.asarray(), reconstruction2.asarray()) # Verify that reconstructions lie in correct discretized domain assert reconstruction1 in disc_domain assert reconstruction2 in disc_domain assert all_almost_equal(reconstruction1.asarray(), x) assert all_almost_equal(reconstruction2.asarray(), x) # --------------------------------------------------------------- # 2D test n = 16 x = np.zeros((n, n)) x[5:10, 5:10] = 1 wbasis = pywt.Wavelet('db1') nscales = 2 mode = 'sym' size_list = coeff_size_list((n, n), nscales, wbasis, mode) # Define a discretized domain domain = odl.FunctionSpace(odl.Rectangle([-1, -1], [1, 1])) nPoints = np.array([n, n]) disc_domain = odl.uniform_discr_fromspace(domain, nPoints) disc_phantom = disc_domain.element(x) # Create the discrete wavelet transform operator. # Only the domain of the operator needs to be defined Wop = WaveletTransform(disc_domain, nscales, wbasis, mode) # Compute the discrete wavelet transform of discrete imput image coeffs = Wop(disc_phantom) # Determine the correct range for Wop and verify that coeffs # is an element of it ran_size = np.prod(size_list[0]) ran_size += sum(3 * np.prod(shape) for shape in size_list[1:-1]) disc_range = disc_domain.dspace_type(ran_size, dtype=disc_domain.dtype) assert coeffs in disc_range # Compute the inverse wavelet transform reconstruction1 = Wop.inverse(coeffs) # With othogonal wavelets the inverse is the adjoint reconstruction2 = Wop.adjoint(coeffs) # Verify that the output of Wop.inverse and Wop.adjoint are the same assert all_almost_equal(reconstruction1.asarray(), reconstruction2.asarray()) # Verify that reconstructions lie in correct discretized domain assert reconstruction1 in disc_domain assert reconstruction2 in disc_domain assert all_almost_equal(reconstruction1.asarray(), x) assert all_almost_equal(reconstruction2.asarray(), x) # ------------------------------------------------------------- # 3D test n = 16 x = np.zeros((n, n, n)) x[5:10, 5:10, 5:10] = 1 wbasis = pywt.Wavelet('db2') nscales = 1 mode = 'per' size_list = coeff_size_list((n, n, n), nscales, wbasis, mode) # Define a discretized domain domain = odl.FunctionSpace(odl.Cuboid([-1, -1, -1], [1, 1, 1])) nPoints = np.array([n, n, n]) disc_domain = odl.uniform_discr_fromspace(domain, nPoints) disc_phantom = disc_domain.element(x) # Create the discrete wavelet transform operator related to 3D transform. Wop = WaveletTransform(disc_domain, nscales, wbasis, mode) # Compute the discrete wavelet transform of discrete imput image coeffs = Wop(disc_phantom) # Determine the correct range for Wop and verify that coeffs # is an element of it ran_size = np.prod(size_list[0]) ran_size += sum(7 * np.prod(shape) for shape in size_list[1:-1]) disc_range = disc_domain.dspace_type(ran_size, dtype=disc_domain.dtype) assert coeffs in disc_range # Compute the inverse wavelet transform reconstruction1 = Wop.inverse(coeffs) # With othogonal wavelets the inverse is the adjoint reconstruction2 = Wop.adjoint(coeffs) # Verify that the output of Wop.inverse and Wop.adjoint are the same assert all_almost_equal(reconstruction1, reconstruction2) # Verify that reconstructions lie in correct discretized domain assert reconstruction1 in disc_domain assert reconstruction2 in disc_domain assert all_almost_equal(reconstruction1.asarray(), x) assert all_almost_equal(reconstruction2, disc_phantom)