test_healpix.py 2.26 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
import ducc_0_1.healpix as ph
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np
import math
import pytest
from numpy.testing import assert_equal, assert_allclose

pmp = pytest.mark.parametrize


def list2fixture(lst):
    @pytest.fixture(params=lst)
    def myfixture(request):
        return request.param

    return myfixture


pow2 = [1 << shift for shift in range(29)]
nonpow2 = [i+7 for i in pow2]
nside_nest = list2fixture(pow2)
nside_ring = list2fixture(pow2+nonpow2)

vlen = list2fixture([1, 10, 100, 1000, 10000])


26
def random_ptg(rng, vlen):
27
    res = np.empty((vlen, 2), dtype=np.float64)
28
29
30
    res[:, 0] = np.arccos((rng.random(vlen)-0.5)*2)
#    res[:, 0] = math.pi*rng.random(vlen)
    res[:, 1] = rng.random(vlen)*2*math.pi
31
32
33
34
35
    return res


def test_pixangpix_nest(vlen, nside_nest):
    base = ph.Healpix_Base(nside_nest, "NEST")
36
37
    rng = np.random.default_rng(42)
    inp = rng.integers(low=0, high=12*nside_nest*nside_nest-1, size=vlen)
38
39
40
41
42
43
    out = base.ang2pix(base.pix2ang(inp))
    assert_equal(inp, out)


def test_pixangpix_ring(vlen, nside_ring):
    base = ph.Healpix_Base(nside_ring, "RING")
44
45
    rng = np.random.default_rng(42)
    inp = rng.integers(low=0, high=12*nside_ring*nside_ring-1, size=vlen)
46
47
48
49
50
51
    out = base.ang2pix(base.pix2ang(inp))
    assert_equal(inp, out)


def test_vecpixvec_nest(vlen, nside_nest):
    base = ph.Healpix_Base(nside_nest, "NEST")
52
53
    rng = np.random.default_rng(42)
    inp = ph.ang2vec(random_ptg(rng, vlen))
54
55
56
57
58
59
    out = base.pix2vec(base.vec2pix(inp))
    assert_equal(np.all(ph.v_angle(inp, out) < base.max_pixrad()), True)


def test_vecpixvec_ring(vlen, nside_ring):
    base = ph.Healpix_Base(nside_ring, "RING")
60
61
    rng = np.random.default_rng(42)
    inp = ph.ang2vec(random_ptg(rng, vlen))
62
63
64
65
66
67
    out = base.pix2vec(base.vec2pix(inp))
    assert_equal(np.all(ph.v_angle(inp, out) < base.max_pixrad()), True)


def test_ringnestring(vlen, nside_nest):
    base = ph.Healpix_Base(nside_nest, "NEST")
68
69
    rng = np.random.default_rng(42)
    inp = rng.integers(low=0, high=12*nside_nest*nside_nest-1, size=vlen)
70
71
72
73
74
    out = base.ring2nest(base.nest2ring(inp))
    assert_equal(np.all(out == inp), True)


def test_vecangvec(vlen):
75
76
    rng = np.random.default_rng(42)
    inp = random_ptg(rng, vlen)
77
78
    out = ph.vec2ang(ph.ang2vec(inp))
    assert_equal(np.all(np.abs(out-inp) < 1e-14), True)