def take_gradient(f, L, Method="MW"):

    n_theta, n_phi = ssht.sample_shape(L, Method=Method)
    theta_weights, phi_weights = gradient_weights(L, Method=Method)

    g_theta = np.zeros((n_theta, n_phi))
    g_phi = np.zeros((n_theta, n_phi))

    g_theta[:n_theta - 1, :] = f[1:, :] - f[:n_theta - 1, :]
    g_theta[n_theta - 1, :] = 0

    g_phi[:, :n_phi - 1] = f[:, 1:] - f[:, :n_phi - 1]
    g_phi[:, n_phi - 1] = 0

    return theta_weights * g_theta, phi_weights * g_phi
Exemplo n.º 2
0
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())
Exemplo n.º 3
0
flm = np.random.randn(L*L) + 1j*np.random.randn(L*L)

f = ssht.inverse(flm,L, Method="MWSS")

flm_rec = ssht.forward(f, L, Method="MWSS")

f_rec  = ssht.inverse(flm,L, Method="MWSS")


if (np.mean(np.abs(flm_rec-flm))<1E-14):
    print "MWSS complex transform test passed, max error: ", np.max(np.abs(flm_rec-flm))
else:
    print "MWSS complex transform ***NOT*** test passed, max error: ", np.max(np.abs(flm_rec-flm))

    
n_theta, n_phi = ssht.sample_shape(L,Method="MWSS") 

f_prime = np.zeros(ssht.sample_shape(L,Method="MWSS"), dtype=np.complex_) + np.random.randn(n_theta,n_phi)\
    + 1j*np.random.randn(n_theta,n_phi)

flm_prime = ssht.inverse_adjoint(f_prime,L, Method="MWSS")

error =  np.abs(np.vdot(flm_prime,flm) - np.vdot(f_prime,f))


flm_prime = np.random.randn(L*L) + 1j*np.random.randn(L*L)
f_prime = ssht.forward_adjoint(flm_prime,L, Method="MWSS")

error += np.abs(np.vdot(flm_prime,flm) - np.vdot(f_prime,f))

if (error<1E-5):
Exemplo n.º 4
0
for i, j in enumerate(range(2 * L - 2, L - 1, -1)):
    q[i] = q[i] + wr[j]

# Integral of function given by rescaled (el,m)=(0,0) harmonic coefficient.
I0 = flm[0] * np.sqrt(4 * np.pi)
print("Integration using input flm:", I0)

# Integrate function on sphere using all points.
f = ssht.inverse(flm, L, Method=method, Reality=reality)
Q = np.outer(q, np.ones(2 * L - 1))
I1 = sum(sum(Q[:] * f[:]))
print("Integration using all points:", I1)

# Integration function on sphere using L points in phi.
# (Zero-pad to evalue the band-limited function on the correct L points.)
ntheta, nphi = ssht.sample_shape(L, Method=method)

f_up = np.zeros((L, 2 * L), dtype=complex)
f_down = np.zeros((L, int((2 * L) / 2)), dtype=complex)
for r in range(ntheta):
    dum = np.fft.fftshift(np.fft.fft(f[r, :]))
    dum = np.insert(dum, 0, 0.0)
    f_up[r, :] = np.fft.ifft(np.fft.ifftshift(dum)) / (2 * L - 1) * (2 * L)

for i in range(int((nphi + 1) / 2)):
    f_down[:, i] = f_up[:, i * 2]
Q_down = np.outer(q, np.ones(L))
I2 = sum(sum(Q_down * f_down)) * (2 * L - 1) / L
print("Integration using L points in phi:", I2)

# Compute integration errors.
    phi_weights = 1.0 / np.sin(thetas)

    return theta_weights, phi_weights


def take_gradient(f, L, Method="MW"):

    n_theta, n_phi = ssht.sample_shape(L, Method=Method)
    theta_weights, phi_weights = gradient_weights(L, Method=Method)

    g_theta = np.zeros((n_theta, n_phi))
    g_phi = np.zeros((n_theta, n_phi))

    g_theta[:n_theta - 1, :] = f[1:, :] - f[:n_theta - 1, :]
    g_theta[n_theta - 1, :] = 0

    g_phi[:, :n_phi - 1] = f[:, 1:] - f[:, :n_phi - 1]
    g_phi[:, n_phi - 1] = 0

    return theta_weights * g_theta, phi_weights * g_phi


L = 4

print gradient_weights(L)

n_theta, n_phi = ssht.sample_shape(L)
f = np.ones((n_theta, n_phi))

print take_gradient(f, L)
Exemplo n.º 6
0
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))