def real_coeffs(order, complex_coeffs): pos_index = [ ssht.elm2ind(el, m) for el in range(order) for m in range(1, el + 1) ] neg_index = [ ssht.elm2ind(el, -m) for el in range(order) for m in range(1, el + 1) ] complex_coeffs[neg_index] = np.conjugate(complex_coeffs[pos_index]) return complex_coeffs
def sYlm(s, l, m, L, rotation=None): flm = np.zeros(L*L, dtype=complex) ind = ssht.elm2ind(l, m) flm[ind] = 1.0 if rotation: flm = ssht.rotate_flms(flm, rotation[0], rotation[1], rotation[2], L) f = ssht.inverse(flm, L, Spin=s) return f
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)
# % # % 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; el = 4 m = 2 gamma = np.pi/2 beta = np.pi/4 alpha = -np.pi/2 # Generate spherical harmonics. flm = np.zeros((L*L),dtype=complex); ind = ssht.elm2ind(el, m); flm[ind] = 1.0 + 1j*0.0; # Compute function on the sphere. f = ssht.inverse(flm, L) # Rotate spherical harmonic flm_rot = ssht.rotate_flms(flm, alpha, beta, gamma, L) # Compute rotated function on the sphere. f_rot = ssht.inverse(flm_rot, L) # Plot
# % 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 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]) # 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
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))