Debian Codegraph @

astropy_healpix/tests/test_healpy.py

raw ◀ ▶
1 Debian patch touch this file
1 # Licensed under a 3-clause BSD style license - see LICENSE.rst
2 from itertools import product
3
4 from astropy.coordinates import angular_separation
5 from astropy import units as u
6 import pytest
7 import numpy as np
8
9 from numpy.testing import assert_equal, assert_allclose
10
11 from .. import healpy as hp_compat
12 from ..high_level import xyz_to_healpix
13
14 from hypothesis import given, settings, example
15 from hypothesis.strategies import integers, floats, booleans, tuples
16 from hypothesis.extra.numpy import arrays
17
18 # NOTE: If healpy is installed, we use it in these tests, but healpy is not a
19 # formal dependency of astropy-healpix.
20 hp = pytest.importorskip('healpy')
21
22 NSIDE_VALUES = [2 ** n for n in range(1, 6)]
23
24
25 @pytest.mark.parametrize(('nside', 'degrees'), product(NSIDE_VALUES, (False, True)))
26 def test_nside2pixarea(nside, degrees):
27 actual = hp_compat.nside2pixarea(nside=nside, degrees=degrees)
28 expected = hp.nside2pixarea(nside=nside, degrees=degrees)
29 assert_allclose(actual, expected)
30
31
32 @pytest.mark.parametrize(('nside', 'arcmin'), product(NSIDE_VALUES, (False, True)))
33 def test_nside2resol(nside, arcmin):
34 actual = hp_compat.nside2resol(nside=nside, arcmin=arcmin)
35 expected = hp.nside2resol(nside=nside, arcmin=arcmin)
36 assert_allclose(actual, expected)
37
38
39 @pytest.mark.parametrize('nside', NSIDE_VALUES)
40 def test_nside2npix(nside):
41 actual = hp_compat.nside2npix(nside)
42 expected = hp.nside2npix(nside)
43 assert_equal(actual, expected)
44
45
46 @pytest.mark.parametrize('level', [0, 3, 7])
47 def test_order2nside(level):
48 actual = hp_compat.order2nside(level)
49 expected = hp.order2nside(level)
50 assert_equal(actual, expected)
51
52
53 @pytest.mark.parametrize('npix', [12 * 2 ** (2 * n) for n in range(1, 6)])
54 def test_npix2nside(npix):
55 actual = hp_compat.npix2nside(npix)
56 expected = hp.npix2nside(npix)
57 assert_equal(actual, expected)
58
59
60 # For the test below, we exclude latitudes that fall exactly on the pole or
61 # the equator since points that fall at exact boundaries are ambiguous.
62
63 @given(nside_pow=integers(0, 29), nest=booleans(), lonlat=booleans(),
64 lon=floats(0, 360, allow_nan=False, allow_infinity=False).filter(
65 lambda lon: abs(lon) > 1e-10),
66 lat=floats(-90, 90, allow_nan=False, allow_infinity=False).filter(
67 lambda lat: abs(lat) < 89.99 and abs(lat) > 1e-10))
68 @settings(max_examples=2000, derandomize=True, deadline=None)
69 def test_ang2pix(nside_pow, lon, lat, nest, lonlat):
70 nside = 2 ** nside_pow
71 if lonlat:
72 theta, phi = lon, lat
73 else:
74 theta, phi = np.pi / 2. - np.radians(lat), np.radians(lon)
75 ipix1 = hp_compat.ang2pix(nside, theta, phi, nest=nest, lonlat=lonlat)
76 ipix2 = hp.ang2pix(nside, theta, phi, nest=nest, lonlat=lonlat)
77 assert ipix1 == ipix2
78
79
80 def test_ang2pix_shape():
81 ipix = hp_compat.ang2pix(8, 1., 2.)
82 assert np.can_cast(ipix, np.int64)
83
84 ipix = hp_compat.ang2pix(8, [[1., 2.], [3., 4.]], [[1., 2.], [3., 4.]])
85 assert ipix.shape == (2, 2)
86
87
88 def test_pix2ang_shape():
89 lon, lat = hp_compat.pix2ang(8, 1)
90 assert isinstance(lon, float)
91 assert isinstance(lat, float)
92
93 lon, lat = hp_compat.pix2ang(8, [[1, 2, 3], [4, 5, 6]])
94 assert lon.shape == (2, 3)
95 assert lat.shape == (2, 3)
96
97
98 @given(nside_pow=integers(0, 29), nest=booleans(), lonlat=booleans(),
99 frac=floats(0, 1, allow_nan=False, allow_infinity=False).filter(lambda x: x < 1))
100 @settings(max_examples=2000, derandomize=True, deadline=None)
101 @example(nside_pow=29, frac=0.1666666694606345, nest=False, lonlat=False)
102 @example(nside_pow=27, frac=2./3., nest=True, lonlat=False)
103 def test_pix2ang(nside_pow, frac, nest, lonlat):
104 nside = 2 ** nside_pow
105 ipix = int(frac * 12 * nside ** 2)
106 theta1, phi1 = hp_compat.pix2ang(nside, ipix, nest=nest, lonlat=lonlat)
107 theta2, phi2 = hp.pix2ang(nside, ipix, nest=nest, lonlat=lonlat)
108 if lonlat:
109 assert_allclose(phi1, phi2, atol=1e-8)
110 if abs(phi1) < 90:
111 assert_allclose(theta1, theta2, atol=1e-10)
112 else:
113 assert_allclose(theta1, theta2, atol=1e-8)
114 if theta1 > 0:
115 assert_allclose(phi1, phi2, atol=1e-10)
116
117
118 def not_on_boundaries(args):
119 """Skip vectors that are on the boundary of a pixel where ipix is ambiguous."""
120 nside_pow, nest, x, y, z = args
121 nside = 2 ** nside_pow
122 _, dx, dy = xyz_to_healpix(
123 x, y, z, nside, return_offsets=True, order="nested" if nest else "ring"
124 )
125 return 0 < dx < 1 and 0 < dy < 1
126
127
128 @given(
129 args=tuples(
130 integers(0, 29), booleans(),
131 floats(-1, 1, allow_nan=False, allow_infinity=False).filter(lambda x: abs(x) > 1e-10),
132 floats(-1, 1, allow_nan=False, allow_infinity=False).filter(lambda y: abs(y) > 1e-10),
133 floats(-1, 1, allow_nan=False, allow_infinity=False).filter(lambda z: abs(z) > 1e-10)
134 ).filter(not_on_boundaries)
135 )
136 @settings(max_examples=2000, derandomize=True, deadline=None)
137 def test_vec2pix(args):
138 nside_pow, nest, x, y, z = args
139 nside = 2 ** nside_pow
140 ipix1 = hp_compat.vec2pix(nside, x, y, z, nest=nest)
141 ipix2 = hp.vec2pix(nside, x, y, z, nest=nest)
142 assert ipix1 == ipix2
143
144
145 @given(nside_pow=integers(0, 29), nest=booleans(),
146 frac=floats(0, 1, allow_nan=False, allow_infinity=False).filter(lambda x: x < 1))
147 @settings(max_examples=2000, derandomize=True, deadline=None)
148 @example(nside_pow=29, frac=0.1666666694606345, nest=False)
149 def test_pix2vec(nside_pow, frac, nest):
150 nside = 2 ** nside_pow
151 ipix = int(frac * 12 * nside ** 2)
152 xyz1 = hp_compat.pix2vec(nside, ipix, nest=nest)
153 xyz2 = hp.pix2vec(nside, ipix, nest=nest)
154 assert_allclose(xyz1, xyz2, atol=1e-8)
155
156
157 def test_vec2pix_shape():
158 ipix = hp_compat.vec2pix(8, 1., 2., 3.)
159 assert np.can_cast(ipix, np.int64)
160
161 ipix = hp_compat.vec2pix(8, [[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]])
162 assert ipix.shape == (2, 2)
163
164
165 def test_pix2vec_shape():
166 x, y, z = hp_compat.pix2vec(8, 1)
167 assert isinstance(x, float)
168 assert isinstance(y, float)
169 assert isinstance(z, float)
170
171 x, y, z = hp_compat.pix2vec(8, [[1, 2, 3], [4, 5, 6]])
172 assert x.shape == (2, 3)
173 assert y.shape == (2, 3)
174 assert z.shape == (2, 3)
175
176
177 @given(nside_pow=integers(0, 29),
178 frac=floats(0, 1, allow_nan=False, allow_infinity=False).filter(lambda x: x < 1))
179 @settings(max_examples=2000, derandomize=True, deadline=None)
180 def test_nest2ring(nside_pow, frac):
181 nside = 2 ** nside_pow
182 nest = int(frac * 12 * nside ** 2)
183 ring1 = hp_compat.nest2ring(nside, nest)
184 ring2 = hp.nest2ring(nside, nest)
185 assert ring1 == ring2
186
187
188 @given(nside_pow=integers(0, 29),
189 frac=floats(0, 1, allow_nan=False, allow_infinity=False).filter(lambda x: x < 1))
190 @settings(max_examples=2000, derandomize=True, deadline=None)
191 @example(nside_pow=29, frac=0.16666666697710755)
192 def test_ring2nest(nside_pow, frac):
193 nside = 2 ** nside_pow
194 ring = int(frac * 12 * nside ** 2)
195 nest1 = hp_compat.ring2nest(nside, ring)
196 nest2 = hp.ring2nest(nside, ring)
197 assert nest1 == nest2
198
199
200 @given(nside_pow=integers(0, 29), step=integers(1, 10), nest=booleans(),
201 frac=floats(0, 1, allow_nan=False, allow_infinity=False).filter(lambda x: x < 1))
202 @settings(max_examples=500, derandomize=True, deadline=None)
203 def test_boundaries(nside_pow, frac, step, nest):
204 nside = 2 ** nside_pow
205 pix = int(frac * 12 * nside ** 2)
206 b1 = hp_compat.boundaries(nside, pix, step=step, nest=nest)
207 b2 = hp.boundaries(nside, pix, step=step, nest=nest)
208 assert_allclose(b1, b2, atol=1e-8)
209
210
211 def test_boundaries_shape():
212 pix = 1
213 b1 = hp_compat.boundaries(8, pix, step=4)
214 b2 = hp.boundaries(8, pix, step=4)
215 assert b1.shape == b2.shape
216
217 pix = [1, 2, 3, 4, 5]
218 b1 = hp_compat.boundaries(8, pix, step=4)
219 b2 = hp.boundaries(8, pix, step=4)
220 assert b1.shape == b2.shape
221
222
223 def not_at_origin(vec):
224 return np.linalg.norm(vec) > 0
225
226
227 @given(vectors=arrays(float, (3,), elements=floats(-1, 1)).filter(not_at_origin),
228 lonlat=booleans(), ndim=integers(0, 4))
229 @settings(max_examples=500, derandomize=True, deadline=None)
230 def test_vec2ang(vectors, lonlat, ndim):
231 vectors = np.broadcast_to(vectors, (2,) * ndim + (3,))
232 lon1, lat1 = hp_compat.vec2ang(vectors, lonlat=lonlat)
233 lon2, lat2 = hp.vec2ang(vectors, lonlat=lonlat)
234 if not lonlat:
235 lon1, lat1 = np.rad2deg(lat1), 90 - np.rad2deg(lon1)
236 lon2, lat2 = np.rad2deg(lat2), 90 - np.rad2deg(lon2)
237 sep = angular_separation(
238 lon1 * u.deg, lat1 * u.deg, lon2 * u.deg, lat2 * u.deg
239 ).to_value(u.rad)
240 assert_allclose(sep, 0, atol=1e-8)
241
242
243 @given(lonlat=booleans(),
244 lon=floats(0, 360, allow_nan=False, allow_infinity=False).filter(
245 lambda lon: abs(lon) > 1e-10),
246 lat=floats(-90, 90, allow_nan=False, allow_infinity=False).filter(
247 lambda lat: abs(lat) < 89.99 and abs(lat) > 1e-10))
248 @settings(max_examples=2000, derandomize=True, deadline=None)
249 def test_ang2vec(lon, lat, lonlat):
250 if lonlat:
251 theta, phi = lon, lat
252 else:
253 theta, phi = np.pi / 2. - np.radians(lat), np.radians(lon)
254 xyz1 = hp_compat.ang2vec(theta, phi, lonlat=lonlat)
255 xyz2 = hp.ang2vec(theta, phi, lonlat=lonlat)
256 assert_allclose(xyz1, xyz2, atol=1e-10)
257
258
259 # The following fails, need to investigate:
260 # @example(nside_pow=29, lon=1.0000000028043134e-05, lat=1.000000000805912e-05,
261 # nest=False, lonlat=False)
262 #
263
264 @given(nside_pow=integers(0, 28), nest=booleans(), lonlat=booleans(),
265 lon=floats(0, 360, allow_nan=False, allow_infinity=False).filter(
266 lambda lon: abs(lon) > 1e-5),
267 lat=floats(-90, 90, allow_nan=False, allow_infinity=False).filter(
268 lambda lat: abs(lat) < 89.99 and abs(lat) > 1e-5))
269 @settings(max_examples=500, derandomize=True, deadline=None)
270 @example(nside_pow=27, lon=1.0000000028043134e-05, lat=-41.81031451395941, nest=False, lonlat=False)
271 @example(nside_pow=6, lon=1.6345238095238293, lat=69.42254649458224, nest=False, lonlat=False)
272 @example(nside_pow=15, lon=1.0000000028043134e-05, lat=1.000000000805912e-05,
273 nest=False, lonlat=False)
274 @example(nside_pow=0, lon=315.0000117809725, lat=1.000000000805912e-05, nest=False, lonlat=False)
275 @example(nside_pow=0, lon=1.0000000028043134e-05, lat=-41.81031489577861, nest=False, lonlat=False)
276 @example(nside_pow=0, lon=35.559942143736414, lat=-41.8103252622604, nest=False, lonlat=False)
277 @example(nside_pow=28, lon=359.9999922886491, lat=-41.81031470486902, nest=False, lonlat=False)
278 @example(nside_pow=0, lon=1.0000000028043134e-05, lat=-41.81031489577861, nest=False, lonlat=False)
279 @example(nside_pow=27, lon=1.0000000028043134e-05, lat=-41.81031451395941, nest=False, lonlat=False)
280 @example(nside_pow=26, lon=359.9999986588955, lat=41.81031489577861, nest=False, lonlat=False)
281 @example(nside_pow=27, lon=359.999997317791, lat=-41.81031451395943, nest=False, lonlat=False)
282 @example(nside_pow=27, lon=1.0000000028043134e-05, lat=89.80224636153702, nest=False, lonlat=False)
283 def test_interp_weights(nside_pow, lon, lat, nest, lonlat):
284 nside = 2 ** nside_pow
285 if lonlat:
286 theta, phi = lon, lat
287 else:
288 theta, phi = np.pi / 2. - np.radians(lat), np.radians(lon)
289 indices1, weights1 = hp_compat.get_interp_weights(nside, theta, phi, nest=nest, lonlat=lonlat)
290 indices2, weights2 = hp.get_interp_weights(nside, theta, phi, nest=nest, lonlat=lonlat)
291
292 # Ignore neighbours with weights < 1e-6 - we have to exclude these otherwise
293 # in some corner cases there will be different low-probability neighbours.
294
295 keep = weights1 > 1e-6
296 indices1, weights1 = indices1[keep], weights1[keep]
297
298 keep = weights2 > 1e-6
299 indices2, weights2 = indices2[keep], weights2[keep]
300
301 order1 = np.argsort(indices1)
302 order2 = np.argsort(indices2)
303
304 assert_equal(indices1[order1], indices2[order2])
•305 assert_allclose(weights1[order1], weights2[order2], atol=1e-4)
306
307
308 # Make an array that can be useful up to the highest nside tested below
309 NSIDE_POW_MAX = 8
310 VALUES = np.random.random(12 * NSIDE_POW_MAX ** 2)
311
312
313 @given(nside_pow=integers(0, NSIDE_POW_MAX), nest=booleans(), lonlat=booleans(),
314 lon=floats(0, 360, allow_nan=False, allow_infinity=False).filter(
315 lambda lon: abs(lon) > 1e-5),
316 lat=floats(-90, 90, allow_nan=False, allow_infinity=False).filter(
317 lambda lat: abs(lat) < 89.99 and abs(lat) > 1e-5))
318 @settings(max_examples=500, derandomize=True, deadline=None)
319 def test_interp_val(nside_pow, lon, lat, nest, lonlat):
320 nside = 2 ** nside_pow
321 if lonlat:
322 theta, phi = lon, lat
323 else:
324 theta, phi = np.pi / 2. - np.radians(lat), np.radians(lon)
325 m = VALUES[:12 * nside ** 2]
326 value1 = hp_compat.get_interp_val(m, theta, phi, nest=nest, lonlat=lonlat)
327 value2 = hp.get_interp_val(m, theta, phi, nest=nest, lonlat=lonlat)
328 assert_allclose(value1, value2, rtol=0.1, atol=1.e-10)