|
| 1 | +import numpy as np |
| 2 | +import numpy.testing as npt |
| 3 | + |
| 4 | +import pybind_isce3 as isce3 |
| 5 | + |
| 6 | +def get_dims(): |
| 7 | + ''' |
| 8 | + Return data and kernel dimenstions |
| 9 | + ''' |
| 10 | + length = 200 |
| 11 | + width = 310 |
| 12 | + kernel_length = 3 |
| 13 | + kernel_width = 3 |
| 14 | + return length, width, kernel_length, kernel_width |
| 15 | + |
| 16 | +def make_inputs(length, width, kernel_length, kernel_width): |
| 17 | + ''' |
| 18 | + Create real and imag data |
| 19 | + ''' |
| 20 | + # Calculate padding |
| 21 | + pad_cols = kernel_width - 1 |
| 22 | + pad_rows = kernel_length - 1 |
| 23 | + width_pad = width + pad_cols |
| 24 | + length_pad = length + pad_rows |
| 25 | + |
| 26 | + # Populate real and imag data |
| 27 | + data_real = np.zeros([length_pad, width_pad], dtype=np.float64) |
| 28 | + data_imag = np.zeros([length_pad, width_pad], dtype=np.complex128) |
| 29 | + for line in range(pad_rows//2, length + pad_rows//2): |
| 30 | + for col in range(pad_cols//2, width + pad_cols//2): |
| 31 | + data_real[line, col] = line + col |
| 32 | + data_imag[line, col] = np.cos(line * col) + np.sin(line * col) * 1.0j |
| 33 | + |
| 34 | + return data_real, data_imag |
| 35 | + |
| 36 | +def make_expected_output(out_shape, data, kernel_width, kernel_length): |
| 37 | + ''' |
| 38 | + Calculate expected decimated output |
| 39 | + ''' |
| 40 | + decimated = np.zeros(out_shape, dtype=data.dtype) |
| 41 | + for i in range(out_shape[0]): |
| 42 | + for j in range(out_shape[1]): |
| 43 | + decimated[i,j] = np.mean(data[i*kernel_width+1:(i+1)*kernel_width+1,\ |
| 44 | + j*kernel_length+1:(j+1)*kernel_length+1]) |
| 45 | + return decimated |
| 46 | + |
| 47 | +def test_ea_convolve2d_with_mask(): |
| 48 | + ''' |
| 49 | + Test convolve2D without mask |
| 50 | + ''' |
| 51 | + length, width, kernel_length, kernel_width = get_dims() |
| 52 | + |
| 53 | + # Create data |
| 54 | + input_real, input_imag = make_inputs(length, width, kernel_length, kernel_width) |
| 55 | + |
| 56 | + # Create mask |
| 57 | + mask = np.ones(input_real.shape, dtype=np.float64) |
| 58 | + |
| 59 | + # Create kernels |
| 60 | + kernel_cols = np.ones([1, kernel_width], dtype=np.float64)/kernel_width |
| 61 | + kernel_rows = np.ones([kernel_length, 1], dtype=np.float64)/kernel_length |
| 62 | + |
| 63 | + # Convolve |
| 64 | + pybind_real = isce3.signal.convolve2D(input_real, mask, kernel_cols, kernel_rows, True) |
| 65 | + pybind_imag = isce3.signal.convolve2D(input_imag, mask, kernel_cols, kernel_rows, True) |
| 66 | + |
| 67 | + # Calculate expected output |
| 68 | + out_shape = (length//kernel_width, width//kernel_length) |
| 69 | + expected_real = make_expected_output(out_shape, input_real, |
| 70 | + kernel_length, kernel_width) |
| 71 | + expected_imag = make_expected_output(out_shape, input_imag, |
| 72 | + kernel_length, kernel_width) |
| 73 | + |
| 74 | + # Check outputs |
| 75 | + npt.assert_allclose(pybind_real, expected_real, rtol=0.0, atol=1e-12) |
| 76 | + npt.assert_allclose(np.angle(pybind_imag), np.angle(expected_imag), |
| 77 | + rtol=0.0, atol=1e-12) |
| 78 | + |
| 79 | +def test_ea_convolve2d_no_mask(): |
| 80 | + ''' |
| 81 | + Test convolve2D without mask |
| 82 | + ''' |
| 83 | + length, width, kernel_length, kernel_width = get_dims() |
| 84 | + |
| 85 | + # Create data |
| 86 | + input_real, input_imag = make_inputs(length, width, kernel_length, kernel_width) |
| 87 | + |
| 88 | + # Create kernels |
| 89 | + kernel_cols = np.ones([1, kernel_width], dtype=np.float64)/kernel_width |
| 90 | + kernel_rows = np.ones([kernel_length, 1], dtype=np.float64)/kernel_length |
| 91 | + |
| 92 | + # Convolve |
| 93 | + pybind_real = isce3.signal.convolve2D(input_real, kernel_cols, kernel_rows, True) |
| 94 | + pybind_imag = isce3.signal.convolve2D(input_imag, kernel_cols, kernel_rows, True) |
| 95 | + |
| 96 | + # Calculate expected output |
| 97 | + out_shape = (length//kernel_width, width//kernel_length) |
| 98 | + expected_real = make_expected_output(out_shape, input_real, |
| 99 | + kernel_length, kernel_width) |
| 100 | + expected_imag = make_expected_output(out_shape, input_imag, |
| 101 | + kernel_length, kernel_width) |
| 102 | + |
| 103 | + # Check outputs |
| 104 | + npt.assert_allclose(pybind_real, expected_real, rtol=0.0, atol=1e-12) |
| 105 | + npt.assert_allclose(np.real(pybind_imag), np.real(expected_imag), |
| 106 | + rtol=0.0, atol=1e-12) |
| 107 | + npt.assert_allclose(np.imag(pybind_imag), np.imag(expected_imag), |
| 108 | + rtol=0.0, atol=1e-12) |
| 109 | + |
| 110 | +def test_scipy(): |
| 111 | + |
| 112 | + from scipy import signal |
| 113 | + |
| 114 | + data = np.ones((8,13)) |
| 115 | + kernel = np.ones((3,3))/9.0 |
| 116 | + |
| 117 | + filt_data_scipy = signal.convolve2d(data, kernel, mode='same') |
| 118 | + |
| 119 | + data_padded = np.ones((10,15)) |
| 120 | + data_padded[0,:] = 0 |
| 121 | + data_padded[:,0] = 0 |
| 122 | + data_padded[:,-1] = 0 |
| 123 | + data_padded[-1,:] = 0 |
| 124 | + |
| 125 | + kernel_cols = np.ones((1,3))/3.0 |
| 126 | + kernel_rows = np.ones((3,1))/3.0 |
| 127 | + filt_data_isce3 = isce3.signal.convolve2D(data_padded, kernel_cols, kernel_rows, False) |
| 128 | + |
| 129 | + npt.assert_allclose(filt_data_isce3, filt_data_scipy, rtol=0.0, atol=1e-12) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + test_ea_convolve2d_no_mask() |
| 134 | + test_ea_convolve2d_with_mask() |
0 commit comments