def gradient_weights(L, Method="MW"):

    thetas, phis = ssht.sample_positions(L, Method=Method, Grid=True)

    theta_weights = np.ones(thetas.shape)
    phi_weights = 1.0 / np.sin(thetas)

    return theta_weights, phi_weights
Beispiel #2
0
import numpy as np
import pyssht as ssht
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)
Beispiel #3
0
Method = "MW"
save_figs = False  #True
show_figs = False  #True
do_cylindrical = True
do_full_sky_proj = True
do_half_sky_proj = True
Cyl_Projection_array = ["MERCATOR", "SINE"]
Projection_array = ["OP", "SP", "GP"]
zoom_region_array = [np.pi / 2 - 1E-6, np.pi / 2, np.pi / 4]
N_real = 10
#Projection_array = ["OP"]

print "calculate errors"
N_angle = 12 * orth_resolution / 500
angle, angle_step = np.linspace(0.0, np.pi / 2, N_angle, retstep=True)
thetas, phis = ssht.sample_positions(L, Method=Method)
x = np.linspace(-1., 1., orth_resolution)
y = np.linspace(-1., 1., orth_resolution)
x, y = np.meshgrid(x, y)
rho = np.sqrt(x * x + y * y)

error_cylindrical_E = np.zeros((N_angle, N_real))
error_mercator_E = np.zeros((N_angle, N_real))
error_sine_E = np.zeros((N_angle, N_real))
error_orthographic_E = np.zeros((N_angle, N_real))
error_steriographic_E = np.zeros((N_angle, N_real))
error_gnomic_E = np.zeros((N_angle, N_real))

error_cylindrical_B = np.zeros((N_angle, N_real))
error_mercator_B = np.zeros((N_angle, N_real))
error_sine_B = np.zeros((N_angle, N_real))
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))
Beispiel #5
0
def ReadFingerprint(fname):

    # Calculates the number of real spherical harmonic orders that belong to an
    # expansion from degree l=0 to L.
    def addmup(x):
        x = np.array(x)
        res = 0.5 * (x + 1)**2 + 0.5 * x + 0.5
        return (res.astype(int))

    # Read in the file header
    with open(fname) as f:
        header = f.readline()
        (L, age) = header.split()
        L = int(L)

    # Read in the rest of the file
    test = np.loadtxt(fname, skiprows=1)
    my_C = test[:, [0, 2]].flatten()
    my_S = test[:, [1, 3]].flatten()

    # Calculate the indices that are expected to have '0' as coefficients
    mods = L - np.arange(0, L + 1) + 1
    modu = np.cumsum(mods + np.mod(mods, 2))
    zero_inds = modu[np.mod(mods, 2) == 1] - 1

    # Remove the zero coefficients
    my_C = np.delete(my_C, zero_inds)
    my_S = np.delete(my_S, zero_inds)

    # Indices to line up coefficients in hlm
    modm = np.append(
        np.cumsum(np.append(0, mods[np.arange(0, mods.size - 1)])),
        int(addmup(L)))

    #### Produces "dems' and 'dels' from addmon
    dems = np.empty(0, np.int8)
    dels = np.empty(0, np.int8)

    for i in np.arange(0, L + 1):
        dems = np.append(dems, np.arange(0, i + 1))
        dels = np.append(dels, np.repeat(i, i + 1))

    # Initialize variable to hold coefficients
    hlm = np.zeros((dels.size, 2))

    # Populate hlm with the coefficients
    for m in np.arange(0, L + 1):
        hlm[addmup(np.arange(m - 1, L)) + m, 0] = my_C[modm[m]:modm[m + 1]]
        hlm[addmup(np.arange(m - 1, L)) + m, 1] = my_S[modm[m]:modm[m + 1]]

    # Realign conventions
    CSC = (-1)**dems
    hlm[:, 0] = hlm[:, 0] * CSC
    hlm[:, 1] = hlm[:, 1] * CSC

    # Convert hlm into a form that can be used by the ssht library
    counter = 0
    flm = np.zeros((L + 1)**2, dtype=np.complex_)
    for ii in np.arange(0, L + 1):
        sub = np.where(dels == ii)[0]
        if sub.size > 1:
            sub2 = np.append(sub[-1:0:-1], sub)
        else:
            sub2 = sub
        flm[counter:(counter + sub2.size)] = hlm[sub2, 0] + 1j * hlm[sub2, 1]
        counter = counter + sub2.size

    fels = np.floor(np.sqrt(np.arange(0, flm.size)))
    fems = np.arange(0, flm.size) - fels * fels - fels
    sub = np.where(fems < 0)[0]
    flm[sub] = (-1)**fems[sub] * np.conj(flm[sub])

    # Invert the harmonics
    f = ssht.inverse(flm, L + 1, Method="MWSS", Reality=True)
    f = f * np.sqrt(4 * np.pi)
    (thetas, phis) = ssht.sample_positions(L + 1, "MWSS")
    lat = 90 - np.rad2deg(thetas)
    lon = np.mod(np.rad2deg(phis) + 180, 360)
    sort_inds = np.argsort(lon)
    lon = lon[sort_inds]
    f = f[:, sort_inds]

    return (f, lat, lon)