def test_forward_unitary_ordinary(): a, b = 0, 2 * np.pi Fx, freq, grid = fft(fx, L=L, a=a, b=b, ret_cubegrid=True) Fx_anl_fc = lambda k: (1. / a_squared) * np.exp(-np.pi * k**2 / a_squared) # Analytic transform Fx_anl = Fx_anl_fc(grid) Fx_circ = angular_average(np.abs(Fx), grid, int(N / 2.2))[0] Fx_anl_circ = angular_average(Fx_anl, grid, int(N / 2.2))[0] assert np.max(np.abs(Fx_circ - Fx_anl_circ)) < 1e-10
def test_sum(): x = np.linspace(-3, 3, 200) X, Y = np.meshgrid(x, x) r2 = X ** 2 + Y ** 2 P = r2 ** -1. ave, coord = angular_average(P, np.sqrt(r2), bins=20, bin_ave=False, average=False) assert np.sum(P[r2 < 18.]) == np.sum(ave)
def test_complex_variance(): x = np.linspace(-3, 3, 400) X, Y = np.meshgrid(x, x) r2 = X ** 2 + Y ** 2 P = np.ones_like(r2) + np.ones_like(r2)*1j with pytest.raises(NotImplementedError): ave, coord, var = angular_average(P, np.sqrt(r2), bins=np.linspace(0, x.max(), 20), get_variance=True)
def test_null_variance_2d(): x = np.linspace(-3, 3, 400) X, Y = np.meshgrid(x, x) r2 = X ** 2 + Y ** 2 P = np.ones_like(r2) ave, coord, var = angular_average(P, np.sqrt(r2), bins=np.linspace(0,x.max(), 20), get_variance=True) assert np.all(var==0)
def test_bin_edges(): x = np.linspace(-3, 3, 200) X, Y = np.meshgrid(x, x) r2 = X**2 + Y**2 P = r2**-1. bins = np.linspace(0, x.max(), 20) ave, coord = angular_average(P, np.sqrt(r2), bins=bins, bin_ave=False) assert np.all(coord == bins)
def test_logbins(): x = np.linspace(-3, 3, 400) X, Y = np.meshgrid(x, x) r2 = X ** 2 + Y ** 2 P = np.ones_like(r2) ave, coord = angular_average(P, np.sqrt(r2), bins=10, bin_ave=False, log_bins=True) assert np.all(np.isclose(np.diff(coord[1:]/coord[:-1]), 0))
def test_var_trivial_weights(): x = np.linspace(-3, 3, 400) X, Y = np.meshgrid(x, x) r2 = X ** 2 + Y ** 2 P = np.ones_like(r2) P += np.random.normal(scale=1, size=(len(x), len(x))) ave, coord, var = angular_average(P, np.sqrt(r2), bins=np.linspace(0,x.max(), 20), get_variance=True, weights=np.ones_like(r2)) print(np.diff(var)) assert np.all(np.diff(var)<=0)
def test_against_multirealisation(): x = np.linspace(-3, 3, 1000) X, Y = np.meshgrid(x, x) r2 = X ** 2 + Y ** 2 bins = np.linspace(0, x.max(), 20) # Get the variance from several realisations ave = [0]*50 for j in range(50): P = np.ones_like(r2) + np.random.normal(scale=1, size=(len(x), len(x))) ave[j], coord = angular_average(P, np.sqrt(r2), bins=bins) var = np.var(np.array(ave), axis=0) # Get the variance from a single realisation ave, coord, var2 = angular_average(P, np.sqrt(r2), bins=bins, get_variance=True) print(var) print(var2) assert(np.all(np.isclose(var, var2, 1e-2)))
def test_mixed_unitary_ordinary_unitary_angular_2d_bf(): N = 1000 Lk = 10. dk = Lk / N k = np.arange(-Lk / 2, Lk / 2, dk)[:N] KX, KY = np.meshgrid(k, k) alpha = np.pi Fk = np.exp(-alpha * (KX**2 + KY**2)) fx, x = ifft(Fk, Lk=Lk, a=0, b=1) Fk_, k_, kgrid = fft(fx, -2 * np.min(x), a=0, b=2 * np.pi, ret_cubegrid=True) Fk_, bins = angular_average(Fk_, kgrid, 200) assert np.max( np.abs(Fk_ - 2 * np.pi * np.exp(-alpha * ((2 * np.pi) * bins)**2)))
def test_mixed_unitary_ordinary_unitary_angular_2d_fb(): N = 1000 L = 10. dx = L / N x = np.arange(-L / 2, L / 2, dx)[:N] X, Y = np.meshgrid(x, x) a_squared = 1 fx = np.exp(-np.pi * a_squared * (X**2 + Y**2)) Fk, freq = fft(fx, L=L, a=0, b=2 * np.pi) Lk = -2 * np.min(freq) fx_, x_, xgrid = ifft(Fk, Lk=Lk, a=0, b=1, ret_cubegrid=True) fx_, bins = angular_average(fx_, xgrid, 200) print( np.max( np.abs(fx_ - np.exp(-np.pi * (bins / (2 * np.pi))**2) / (2 * np.pi)))) assert np.max( np.abs(fx_ - np.exp(-np.pi * (bins / (2 * np.pi))**2) / (2 * np.pi))) < 1e-4