healpix_test.py 4.27 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2019-2020 Max-Planck-Society


Martin Reinecke's avatar
Martin Reinecke committed
17
import ducc0.healpix as ph
18
19
import numpy as np

20
21
rng = np.random.default_rng(42)

Martin Reinecke's avatar
Martin Reinecke committed
22

23
def random_ptg(vlen):
Martin Reinecke's avatar
Martin Reinecke committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    res = np.empty((vlen, 2), dtype=np.float64)
    res[:, 0] = np.arccos((rng.random(vlen)-0.5)*2)
    res[:, 1] = rng.random(vlen)*2*np.pi
    return res


def check_pixangpix(vlen, ntry, nside, isnest):
    base = ph.Healpix_Base(nside, "NEST" if isnest else "RING")
    for cnt in range(ntry):
        inp = rng.integers(low=0, high=12*nside*nside-1, size=vlen)
        out = base.ang2pix(base.pix2ang(inp))
        if not np.array_equal(inp, out):
            raise ValueError("Test failed")

38
39

def check_vecpixvec(vlen, ntry, nside, isnest):
Martin Reinecke's avatar
Martin Reinecke committed
40
41
42
43
44
45
46
    base = ph.Healpix_Base(nside, "NEST" if isnest else "RING")
    for cnt in range(ntry):
        inp = ph.ang2vec(random_ptg(vlen))
        out = base.pix2vec(base.vec2pix(inp))
        if np.any(ph.v_angle(inp, out) > base.max_pixrad()):
            raise ValueError("Test failed")

47
48

def check_pixangvecpix(vlen, ntry, nside, isnest):
Martin Reinecke's avatar
Martin Reinecke committed
49
50
51
52
53
54
55
    base = ph.Healpix_Base(nside, "NEST" if isnest else "RING")
    for cnt in range(ntry):
        inp = rng.integers(low=0, high=12*nside*nside-1, size=vlen)
        out = base.vec2pix(ph.ang2vec(base.pix2ang(inp)))
        if not np.array_equal(inp, out):
            raise ValueError("Test failed")

56
57

def check_pixvecangpix(vlen, ntry, nside, isnest):
Martin Reinecke's avatar
Martin Reinecke committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    base = ph.Healpix_Base(nside, "NEST" if isnest else "RING")
    for cnt in range(ntry):
        inp = rng.integers(low=0, high=12*nside*nside-1, size=vlen)
        out = base.ang2pix(ph.vec2ang(base.pix2vec(inp)))
        if not np.array_equal(inp, out):
            raise ValueError("Test failed")


def check_pixvecpix(vlen, ntry, nside, isnest):
    base = ph.Healpix_Base(nside, "NEST" if isnest else "RING")
    for cnt in range(ntry):
        inp = rng.integers(low=0, high=12*nside*nside-1, size=vlen)
        out = base.vec2pix(base.pix2vec(inp))
        if not np.array_equal(inp, out):
            raise ValueError("Test failed")


def check_ringnestring(vlen, ntry, nside):
    base = ph.Healpix_Base(nside, "NEST")
    for cnt in range(ntry):
        inp = rng.integers(low=0, high=12*nside*nside-1, size=vlen)
        out = base.nest2ring(base.ring2nest(inp))
        if not np.array_equal(inp, out):
            raise ValueError("Test failed")


def check_pixxyfpix(vlen, ntry, nside, isnest):
    base = ph.Healpix_Base(nside, "NEST" if isnest else "RING")
    for cnt in range(ntry):
        inp = rng.integers(low=0, high=12*nside*nside-1, size=vlen)
        out = base.xyf2pix(base.pix2xyf(inp))
        if not np.array_equal(inp, out):
            raise ValueError("Test failed")


def check_vecangvec(vlen, ntry):
    for cnt in range(ntry):
        inp = random_ptg(vlen)
        out = ph.vec2ang(ph.ang2vec(inp))
        if np.any(np.greater(np.abs(inp-out), 1e-10)):
            raise ValueError("Test failed")


check_vecangvec(1000, 1000)

for nside in (1, 32, 512, 8192, 32768*8):
    check_ringnestring(1000, 1000, nside)
    for isnest in (False, True):
        check_vecpixvec(1000, 1000, nside, isnest)
        check_pixangpix(1000, 1000, nside, isnest)
        check_pixvecpix(1000, 1000, nside, isnest)
        check_pixxyfpix(1000, 1000, nside, isnest)
        check_pixangvecpix(1000, 1000, nside, isnest)
        check_pixvecangpix(1000, 1000, nside, isnest)


isnest = False
for nside in (3, 7, 514, 8167, 32768*8+7):
    check_vecpixvec(1000, 1000, nside, isnest)
    check_pixangpix(1000, 1000, nside, isnest)
    check_pixvecpix(1000, 1000, nside, isnest)
    check_pixxyfpix(1000, 1000, nside, isnest)
    check_pixangvecpix(1000, 1000, nside, isnest)
    check_pixvecangpix(1000, 1000, nside, isnest)