def test_make_scattering_matrix(): numpoints = 5 inc_theta, out_theta = scat.make_angles_grid(numpoints) scattering_matrix = _scattering_function(inc_theta, out_theta) assert inc_theta.shape == (numpoints, numpoints) assert out_theta.shape == (numpoints, numpoints) theta = scat.make_angles(numpoints) for i in range(numpoints): for j in range(numpoints): assert np.allclose(scattering_matrix[i, j], _scattering_function(theta[j], theta[i])) assert scattering_matrix.shape == (numpoints, numpoints) idx = (1, 3) np.testing.assert_allclose( scattering_matrix[idx], _scattering_function(inc_theta[idx], out_theta[idx])) np.testing.assert_allclose(_scattering_function(np.pi, np.pi), _scattering_function(-np.pi, -np.pi)) x = 0.1 np.testing.assert_allclose(_scattering_function(np.pi + x, np.pi), _scattering_function(-np.pi + x, -np.pi))
def test_interpolate_matrix(): numpoints = 5 dtheta = 2 * np.pi / numpoints inc_theta, out_theta = scat.make_angles_grid(numpoints) scattering_matrix = _scattering_function(inc_theta, out_theta) scat_fn = scat.interpolate_matrix(scattering_matrix) # raise Exception(scattering_matrix) np.testing.assert_allclose(scat_fn(inc_theta, out_theta), _scattering_function(inc_theta, out_theta)) # add multiple of 2pi np.testing.assert_allclose( scat_fn(inc_theta + 10 * np.pi, out_theta - 6 * np.pi), _scattering_function(inc_theta, out_theta), ) # remove last column because edge effect x = (inc_theta + dtheta / 4)[:, :-1] y = out_theta[:, :-1] np.testing.assert_allclose(scat_fn(x, y), _scattering_function(x, y)) # remove last line because edge effect x = inc_theta[:-1] y = (out_theta + dtheta / 4)[:-1] np.testing.assert_allclose(scat_fn(x, y), _scattering_function(x, y)) x = inc_theta[:-1, :-1] + dtheta / 3 y = (out_theta + dtheta / 4)[:-1, :-1] np.testing.assert_allclose(scat_fn(x, y), _scattering_function(x, y))
def test_scattering_angles_grid(): n = 10 theta = scat.make_angles(n) inc_theta, out_theta = scat.make_angles_grid(n) for i in range(n): for j in range(n): assert inc_theta[i, j] == theta[j] assert out_theta[i, j] == theta[i]
def test_scat_data(self, scat_data_obj): scat_keys = scat.SCAT_KEYS scat_obj = scat_data_obj # alias assert scat_obj.frequencies.ndim == 1 assert scat_obj.numfreq == scat_obj.frequencies.shape[0] numangles = scat_obj.numangles # asking values at the known values must be the known values matrices = scat_obj.as_multi_freq_matrices(scat_obj.frequencies, numangles) for scat_key in scat_keys: np.testing.assert_allclose(matrices[scat_key], scat_obj.orig_matrices[scat_key], atol=1e-14) # another way to ask the known values frequency = scat_obj.frequencies[0] phi_in, phi_out = scat.make_angles_grid(numangles) matrices = scat_obj(phi_in, phi_out, frequency) np.testing.assert_allclose(matrices[scat_key], scat_obj.orig_matrices[scat_key][0], atol=1e-14) # check angle interpolation phi = phi_in[0] a, b = 0.25, 0.75 matrices = scat_obj( [phi[0], phi[1], a * phi[0] + b * phi[1]], [phi[2], phi[2], phi[2]], frequency, ) for scat_key in scat_keys: val = matrices[scat_key] np.testing.assert_allclose(a * val[0] + b * val[1], val[2]) # check frequency interpolation if scat_obj.numfreq == 1: # This should be the same value all the time with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=arim.exceptions.ArimWarning) funcs = scat_obj.as_freq_angles_funcs() for scat_key, func in funcs.items(): assert func(2.0, 3.0, frequency) == func(2.0, 3.0, frequency * 2.0) assert func(2.0, 3.0, frequency) == func(2.0, 3.0, frequency / 2.0) else: # This should be a linear interpolation funcs = scat_obj.as_freq_angles_funcs() for scat_key, func in funcs.items(): x = a * func(2.0, 3.0, scat_obj.frequencies[0]) + b * func( 2.0, 3.0, scat_obj.frequencies[1]) y = func( 2.0, 3.0, a * scat_obj.frequencies[0] + b * scat_obj.frequencies[1]) np.testing.assert_allclose(x, y) # extrapolation: there should be not bound error func(2.0, 3.0, np.min(scat_obj.frequencies) / 2) func(2.0, 3.0, np.max(scat_obj.frequencies) * 2)
def test_rotate_matrix(): # n = 10 # scat_matrix = np.random.uniform(size=(n, n)) + 1j * np.random.uniform(size=(n, n)) # scat_func = ut.interpolate_matrix(scat_matrix) n = 72 inc_angles, out_angles = scat.make_angles_grid(n) scat_matrix = np.exp(-(inc_angles - np.pi / 6)**2 - (out_angles + np.pi / 4)**2 ) + 1j * np.exp(-(inc_angles + np.pi / 2)**2 - (out_angles - np.pi / 10)**2) scat_func = scat.interpolate_matrix(scat_matrix) # rotation of 0° rotated_scat_matrix = scat.rotate_matrix(scat_matrix, 0.0) np.testing.assert_allclose(scat_matrix, rotated_scat_matrix, rtol=1e-6) # rotation of 360° rotated_scat_matrix = scat.rotate_matrix(scat_matrix, 2 * np.pi) np.testing.assert_allclose(scat_matrix, rotated_scat_matrix, rtol=1e-6) # rotation of pi/ 6 # Ensure that S'(theta_1, theta_2) = S(theta_1 - phi, theta_2 - phi) # No interpolation is involded here, this should be perfectly equal phi = np.pi / 6 rotated_scat_matrix = scat.rotate_matrix(scat_matrix, phi) rotated_scat_scat_func = scat.interpolate_matrix(rotated_scat_matrix) theta_1 = np.linspace(0, 2 * np.pi, n) theta_2 = np.linspace(0, np.pi, n) np.testing.assert_allclose( rotated_scat_scat_func(theta_1, theta_2), scat_func(theta_1 - phi, theta_2 - phi), ) # rotation of pi/ 5 # Ensure that S'(theta_1, theta_2) = S(theta_1 - phi, theta_2 - phi) # Because of interpolation, this is not exactly equal. phi = np.pi / 5 rotated_scat_matrix = scat.rotate_matrix(scat_matrix, phi) rotated_scat_scat_func = scat.interpolate_matrix(rotated_scat_matrix) theta_1 = np.linspace(0, 2 * np.pi, 15) theta_2 = np.linspace(0, np.pi, 15) # import matplotlib.pyplot as plt # plt.figure() # plt.plot(np.real(rotated_scat_scat_func(theta_1, theta_2))) # plt.plot(np.real(scat_func(theta_1 - phi, theta_2 - phi))) # plt.show() # plt.figure() # plt.plot(np.imag(rotated_scat_scat_func(theta_1, theta_2))) # plt.plot(np.imag(scat_func(theta_1 - phi, theta_2 - phi))) # plt.show() np.testing.assert_allclose( rotated_scat_scat_func(theta_1, theta_2), scat_func(theta_1 - phi, theta_2 - phi), atol=5e-3, ) # unrotate np.testing.assert_allclose(scat.rotate_matrix(rotated_scat_matrix, -phi), scat_matrix, rtol=1e-6) # test rotate_matrices matrices = dict(LL=scat_matrix, TT=np.ones((10, 10))) rotated_matrices = scat.rotate_matrices(matrices, np.pi / 6) assert "LL" in rotated_matrices assert "TT" in rotated_matrices