1# This program is free software: you can redistribute it and/or modify 2# it under the terms of the GNU General Public License as published by 3# the Free Software Foundation, either version 3 of the License, or 4# (at your option) any later version. 5# 6# This program is distributed in the hope that it will be useful, 7# but WITHOUT ANY WARRANTY; without even the implied warranty of 8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9# GNU General Public License for more details. 10# 11# You should have received a copy of the GNU General Public License 12# along with this program. If not, see <http://www.gnu.org/licenses/>. 13# 14# Copyright(C) 2020 Max-Planck-Society 15 16 17import ducc0.healpix as ph 18import numpy as np 19import math 20import pytest 21from numpy.testing import assert_equal 22 23pmp = pytest.mark.parametrize 24 25 26def list2fixture(lst): 27 @pytest.fixture(params=lst) 28 def myfixture(request): 29 return request.param 30 31 return myfixture 32 33 34pow2 = [1 << shift for shift in range(29)] 35nonpow2 = [i+7 for i in pow2] 36nside_nest = list2fixture(pow2) 37nside_ring = list2fixture(pow2+nonpow2) 38 39vlen = list2fixture([1, 10, 100, 1000, 10000]) 40 41 42def random_ptg(rng, vlen): 43 res = np.empty((vlen, 2), dtype=np.float64) 44 res[:, 0] = np.arccos((rng.random(vlen)-0.5)*2) 45# res[:, 0] = math.pi*rng.random(vlen) 46 res[:, 1] = rng.random(vlen)*2*math.pi 47 return res 48 49 50def test_pixangpix_nest(vlen, nside_nest): 51 base = ph.Healpix_Base(nside_nest, "NEST") 52 rng = np.random.default_rng(42) 53 inp = rng.integers(low=0, high=12*nside_nest*nside_nest-1, size=vlen) 54 out = base.ang2pix(base.pix2ang(inp)) 55 assert_equal(inp, out) 56 57 58def test_pixangpix_ring(vlen, nside_ring): 59 base = ph.Healpix_Base(nside_ring, "RING") 60 rng = np.random.default_rng(42) 61 inp = rng.integers(low=0, high=12*nside_ring*nside_ring-1, size=vlen) 62 out = base.ang2pix(base.pix2ang(inp)) 63 assert_equal(inp, out) 64 65 66def test_vecpixvec_nest(vlen, nside_nest): 67 base = ph.Healpix_Base(nside_nest, "NEST") 68 rng = np.random.default_rng(42) 69 inp = ph.ang2vec(random_ptg(rng, vlen)) 70 out = base.pix2vec(base.vec2pix(inp)) 71 assert_equal(np.all(ph.v_angle(inp, out) < base.max_pixrad()), True) 72 73 74def test_vecpixvec_ring(vlen, nside_ring): 75 base = ph.Healpix_Base(nside_ring, "RING") 76 rng = np.random.default_rng(42) 77 inp = ph.ang2vec(random_ptg(rng, vlen)) 78 out = base.pix2vec(base.vec2pix(inp)) 79 assert_equal(np.all(ph.v_angle(inp, out) < base.max_pixrad()), True) 80 81 82def test_ringnestring(vlen, nside_nest): 83 base = ph.Healpix_Base(nside_nest, "NEST") 84 rng = np.random.default_rng(42) 85 inp = rng.integers(low=0, high=12*nside_nest*nside_nest-1, size=vlen) 86 out = base.ring2nest(base.nest2ring(inp)) 87 assert_equal(np.all(out == inp), True) 88 89 90def test_vecangvec(vlen): 91 rng = np.random.default_rng(42) 92 inp = random_ptg(rng, vlen) 93 out = ph.vec2ang(ph.ang2vec(inp)) 94 assert_equal(np.all(np.abs(out-inp) < 1e-14), True) 95