def test_real_forward_ssht_vs_ducc0(real_image, order, method, nthreads=1): ssht_coeffs = ssht.forward(real_image, order, Reality=True, Method=method, Spin=0) ducc0_coeffs = ssht.forward( real_image, order, Reality=True, Method=method, Spin=0, backend="ducc", nthreads=nthreads, ) assert ssht_coeffs == approx(ducc0_coeffs)
def test_real_forward_adjoint(rng: np.random.Generator, method, order): shape = ssht.sample_shape(order, Method=method) f = rng.standard_normal(shape, dtype="float64") flm = ssht.forward(f, order, Reality=True, Method=method) f = ssht.inverse(flm, order, Reality=True, Method=method) f_prime = rng.standard_normal(shape, dtype="float64") flm_prime = ssht.forward(f_prime, order, Reality=True, Method=method) f_prime = ssht.forward_adjoint(flm_prime, order, Reality=True, Method=method, backend="ducc") assert flm_prime.conj() @ flm == approx( f_prime.flatten().conj() @ f.flatten())
def test_SHT(L, Method, Reality, Spin, nthreads=1): if Reality: # Generate random flms (of real signal). flm = np.zeros((L * L), dtype=complex) # Impose reality on flms. for el in range(L): m = 0 ind = ssht.elm2ind(el, m) flm[ind] = np.random.randn() for m in range(1, el + 1): ind_pm = ssht.elm2ind(el, m) ind_nm = ssht.elm2ind(el, -m) flm[ind_pm] = np.random.randn() + 1j * np.random.randn() flm[ind_nm] = (-1)**m * np.conj(flm[ind_pm]) else: flm = np.random.randn(L * L) + 1j * np.random.randn(L * L) t0 = time() f = ssht.inverse( flm, L, Reality=Reality, Method=Method, Spin=Spin, backend="ducc", nthreads=nthreads, ) flm_syn = ssht.forward( f, L, Reality=Reality, Method=Method, Spin=Spin, backend="ducc", nthreads=nthreads, ) tducc = time() - t0 t0 = time() f2 = ssht.inverse(flm, L, Reality=Reality, Method=Method, Spin=Spin) flm_syn2 = ssht.forward(f2, L, Reality=Reality, Method=Method, Spin=Spin) tssht = time() - t0 return (_l2error(f, f2) + _l2error(flm_syn, flm_syn2), tssht / tducc)
def test_complex_forward_ssht_vs_ducc0(complex_image, order, method, spin, nthreads=1): ssht_coeffs = ssht.forward(complex_image, order, Reality=False, Method=method, Spin=spin) ducc0_coeffs = ssht.forward( complex_image, order, Reality=False, Method=method, Spin=spin, backend="ducc", nthreads=nthreads, ) assert ssht_coeffs == approx(ducc0_coeffs)
# % function, using simplest interface with default options. # % # % Author: Christopher G R Wallis & Jason McEwen (www.christophergrwallis.org & www.jasonmcewen.org) # % # % pyssht python package to perform spin spherical harmonic transforms # Define parameters. L = 64 # Generate random flms (of complex signal). flm = np.random.randn(L*L) + 1j*np.random.randn(L*L) # Compute inverse then forward transform. f = ssht.inverse(flm, L); flm_syn = ssht.forward(f, L); # Compute max error in harmonic space. maxerr = np.abs(flm_syn - flm).max() print "Max error: ", maxerr # Plot function on sphere using mollweide projection f_plot, mask_array = ssht.mollweide_projection(np.abs(f), L, resolution=200) plt.figure() imgplot = plt.imshow(f_plot,interpolation='nearest') plt.colorbar(imgplot,fraction=0.025, pad=0.04) plt.imshow(mask_array, interpolation='nearest', cmap=cm.gray, vmin=-1., vmax=1.) plt.gca().set_aspect("equal") plt.title("|f|") plt.axis('off')
def test_inverse_spin_reality(self): with self.assertRaises(ssht.ssht_spin_error) as context: ssht.forward(f,L,Reality=True,Spin=2) self.assertTrue('Reality set to True and Spin is not 0. However, spin signals must be complex.' in context.exception)
def test_forward_method_type(self): with self.assertRaises(ssht.ssht_input_error) as context: ssht.forward(f,L,Method="DJ") self.assertTrue('Method is not recognised, Methods are: MW, MW_pole, MWSS, DH and GL' in context.exception)
def test_forward_ndim(self): with self.assertRaises(ssht.ssht_input_error) as context: ssht.forward(flm,L) self.assertTrue('f must be 2D numpy array' in context.exception)
import matplotlib.pyplot as plt # s2c test L=256 thetas, phis = ssht.sample_positions(L, Grid=True) f = np.zeros((L,2*L-1), dtype=np.float_) + np.random.randn(L,2*L-1) #ssht.plot_sphere(phis, L,Parametric=False, Output_File='test.pdf',Show=False, Color_Bar=True, Units='Radians') (x, y, z) = ssht.s2_to_cart(thetas, phis) (x, y, z) = ssht.spherical_to_cart( np.ones(thetas.shape), thetas, phis) #test rotations flm = ssht.forward(phis, L, Reality=True) f = ssht.inverse(flm,L, Reality=True) flm_prime = ssht.rotate_flms(flm, np.pi/4, np.pi/4, np.pi/4, L) f_prime = ssht.inverse(flm_prime, L, Reality=True) #ssht.plot_sphere(f, L,Parametric=False, Output_File='test_phi_sphere.pdf',Show=False, Color_Bar=True, Units='Radians') #ssht.plot_sphere(f_prime, L,Parametric=False, Output_File='test_phi_rot_sphere.pdf',Show=False, Color_Bar=True, Units='Radians') #plot = ssht.plot_mollweide(f, L, Close=True) #plt.show() #plot2 = ssht.plot_mollweide(f_prime, L, Close=True) #plt.show()
flm = np.zeros((L * L), dtype=complex) # Impose reality on flms. for el in range(L): m = 0 ind = ssht.elm2ind(el, m) flm[ind] = np.random.randn() for m in range(1, el + 1): ind_pm = ssht.elm2ind(el, m) ind_nm = ssht.elm2ind(el, -m) flm[ind_pm] = np.random.randn() + 1j * np.random.randn() flm[ind_nm] = (-1)**m * np.conj(flm[ind_pm]) # Compute inverse then forward transform. f = ssht.inverse(flm, L, Reality=True) flm_syn = ssht.forward(f, L, Reality=True) # Compute max error in harmonic space. maxerr = np.abs(flm_syn - flm).max() print "Max error: ", maxerr # Plot function on sphere using mollweide projection f_plot, mask_array = ssht.mollweide_projection(np.abs(f), L, resolution=200) plt.figure() imgplot = plt.imshow(f_plot, interpolation='nearest') plt.colorbar(imgplot, fraction=0.025, pad=0.04) plt.imshow(mask_array, interpolation='nearest', cmap=cm.gray, vmin=-1.,
# % pyssht_demo_3 - Run demo3 # % # % Demo to compute inverse and forward transform of spin function, using # % standard interface with various options. # % # % Author: Christopher G R Wallis & Jason McEwen (www.christophergrwallis.org & www.jasonmcewen.org) # % # % pyssht python package to perform spin spherical harmonic transforms # Define parameters. L = 64 spin = 4 methods = ["MW", "MWSS", "GL", "DH"] # Generate random flms (of complex signal). flm = np.random.randn(L * L) + 1j * np.random.randn(L * L) # Zero harmonic coefficients with el<|spin|. ind_min = np.abs(spin)**2 flm[0:ind_min] = 0.0 + 1j * 0.0 # Compute inverse then forward transform. for method in methods: f = ssht.inverse(flm, L, Method=method, Spin=spin, Reality=False) flm_syn = ssht.forward(f, L, Method=method, Spin=spin, Reality=False) # Compute max error in harmonic space. maxerr = np.abs(flm_syn - flm).max() print("Method:", method, "\nMax error:", maxerr)
# % pyssht_demo_4 - Run demo4 # % # % Demo to compute inverse and forward transform of spin function, using # % polar interface. # % # % Author: Christopher G R Wallis & Jason McEwen (www.christophergrwallis.org & www.jasonmcewen.org) # % # % pyssht python package to perform spin spherical harmonic transforms # Define parameters. L = 64 Spin = 0 method = "MW_pole" # Generate random flms (of complex signal). flm = np.random.randn(L * L) + 1j * np.random.randn(L * L) # Zero harmonic coefficients with el<|spin|. ind_min = np.abs(Spin)**2 flm[0:ind_min] = 0.0 + 1j * 0.0 # Compute inverse then forward transform. f, f_sp, phi_sp = ssht.inverse(flm, L, Spin=Spin, Method="MW_pole") flm_syn = ssht.forward((f, f_sp, phi_sp), L, Spin=Spin, Method="MW_pole") # Compute max error in harmonic space. maxerr = np.abs(flm_syn - flm).max() print("Max error:", maxerr)
def test_everything(): # Test indexing functions ind2elm_check = [ pyssht.ind2elm(i) == sshtn.ind2elm(i) for i in range(L * L) ] assert all(ind2elm_check), "ind2elm functions do not match" elm2ind_check = [ pyssht.elm2ind(el, m) == sshtn.elm2ind(el, m) for el in range(L) for m in range(-el, el) ] assert all(elm2ind_check), "elm2ind functions do not match" assert pyssht.sample_shape(L, Method="MW") == sshtn.mw_sample_shape(L) assert pyssht.sample_shape(L, Method="MWSS") == sshtn.mwss_sample_shape(L) py_theta, py_phi = pyssht.sample_positions(L, Method="MW", Grid=False) nb_theta, nb_phi = sshtn.mw_sample_positions(L) assert np.allclose(py_theta, nb_theta) assert np.allclose(py_phi, nb_phi) py_theta, py_phi = pyssht.sample_positions(L, Method="MWSS", Grid=False) nb_theta, nb_phi = sshtn.mwss_sample_positions(L) assert np.allclose(py_theta, nb_theta) assert np.allclose(py_phi, nb_phi) py_ttheta, py_pphi = pyssht.sample_positions(L, Method="MW", Grid=True) nb_ttheta, nb_pphi = sshtn.mw_sample_grid(L) assert np.allclose(py_ttheta, nb_ttheta) assert np.allclose(py_pphi, nb_pphi) py_ttheta, py_pphi = pyssht.sample_positions(L, Method="MWSS", Grid=True) nb_ttheta, nb_pphi = sshtn.mwss_sample_grid(L) assert np.allclose(py_ttheta, nb_ttheta) assert np.allclose(py_pphi, nb_pphi) # Generate random flms (of complex signal). np.random.seed(89834) flm = np.random.randn(L * L) + 1j * np.random.randn(L * L) # Zero harmonic coefficients with el<|spin|. ind_min = np.abs(s)**2 flm[0:ind_min] = 0.0 + 1j * 0.0 # MW inverse complex transform f_py_mw = pyssht.inverse(flm, L, Spin=s, Method="MW") f_nb_mw = np.empty(sshtn.mw_sample_shape(L), dtype=np.complex128) sshtn.mw_inverse_sov_sym(flm, L, s, f_nb_mw) assert np.allclose(f_py_mw, f_nb_mw) # MW forward complex transform, recovering input rec_flm_py_mw = pyssht.forward(f_py_mw, L, Spin=s, Method="MW") rec_flm_nb_mw = np.empty(L * L, dtype=np.complex128) sshtn.mw_forward_sov_conv_sym(f_nb_mw, L, s, rec_flm_nb_mw) assert np.allclose(rec_flm_py_mw, rec_flm_nb_mw) assert np.allclose(rec_flm_nb_mw, flm) # MW forward real transform f_re = np.random.randn(*sshtn.mw_sample_shape(L)) flm_py_re_mw = pyssht.forward(f_re, L, Spin=0, Method="MW", Reality=True) flm_nb_re_mw = np.empty(L * L, dtype=np.complex128) sshtn.mw_forward_sov_conv_sym_real(f_re, L, flm_nb_re_mw) assert np.allclose(flm_py_re_mw, flm_nb_re_mw) # MW inverse real transform rec_f_re_py = pyssht.inverse(flm_py_re_mw, L, Spin=0, Method="MW", Reality=True) rec_f_re_nb = np.empty(sshtn.mw_sample_shape(L), dtype=np.float64) sshtn.mw_inverse_sov_sym_real(flm_nb_re_mw, L, rec_f_re_nb) assert np.allclose(rec_f_re_py, rec_f_re_nb) # Note that rec_f_re_{py,nb} != f_re since f_re is not band-limited at L # MWSS invserse complex transform f_py_mwss = pyssht.inverse(flm, L, Spin=s, Method="MWSS", Reality=False) f_nb_mwss = np.empty(sshtn.mwss_sample_shape(L), dtype=np.complex128) sshtn.mw_inverse_sov_sym_ss(flm, L, s, f_nb_mwss) assert np.allclose(f_py_mwss, f_nb_mwss) # MWSS forward complex transform rec_flm_py_mwss = pyssht.forward(f_py_mwss, L, Spin=s, Method="MWSS", Reality=False) rec_flm_nb_mwss = np.empty(L * L, dtype=np.complex128) sshtn.mw_forward_sov_conv_sym_ss(f_nb_mwss, L, s, rec_flm_nb_mwss) assert np.allclose(rec_flm_py_mwss, rec_flm_nb_mwss) assert np.allclose(rec_flm_nb_mwss, flm) # MWSS forward real transform f_re2 = np.random.randn(*sshtn.mwss_sample_shape(L)) flm_py_re_mwss = pyssht.forward(f_re2, L, Spin=0, Method="MWSS", Reality=True) flm_nb_re_mwss = np.empty(L * L, dtype=np.complex128) sshtn.mw_forward_sov_conv_sym_ss_real(f_re2, L, flm_nb_re_mwss) assert np.allclose(flm_py_re_mwss, flm_nb_re_mwss) # MWSS inverse real transform rec_f_re_py_mwss = pyssht.inverse(flm_py_re_mwss, L, Spin=0, Method="MWSS", Reality=True) rec_f_re_nb_mwss = np.empty(sshtn.mwss_sample_shape(L), dtype=np.float64) sshtn.mw_inverse_sov_sym_ss_real(flm_nb_re_mwss, L, rec_f_re_nb_mwss) assert np.allclose(rec_f_re_py_mwss, rec_f_re_nb_mwss) assert np.allclose(pyssht.generate_dl(np.pi / 2, 10), sshtn.generate_dl(np.pi / 2, 10))