def test_wavelet_decomposition3d_and_reconstruction3d(): # Test 3D wavelet decomposition and reconstruction and verify that # they perform as expected x = np.random.rand(16, 16, 16) mode = 'sym' wbasis = pywt.Wavelet('db5') nscales = 1 wavelet_coeffs = wavelet_decomposition3d(x, wbasis, mode, nscales) aaa = wavelet_coeffs[0] reference = pywt.dwtn(x, wbasis, mode) aaa_reference = reference['aaa'] assert all_almost_equal(aaa, aaa_reference) reconstruction = wavelet_reconstruction3d(wavelet_coeffs, wbasis, mode, nscales) reconstruction_reference = pywt.idwtn(reference, wbasis, mode) assert all_almost_equal(reconstruction, reconstruction_reference) assert all_almost_equal(reconstruction, x) assert all_almost_equal(reconstruction_reference, x) wbasis = pywt.Wavelet('db1') nscales = 3 wavelet_coeffs = wavelet_decomposition3d(x, wbasis, mode, nscales) shape_true = (nscales + 1, ) assert all_equal(np.shape(wavelet_coeffs), shape_true) reconstruction = wavelet_reconstruction3d(wavelet_coeffs, wbasis, mode, nscales) assert all_almost_equal(reconstruction, x)
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)