| 1 |
# Licensed under a 3-clause BSD style license - see LICENSE.rst |
| 2 |
from itertools import product |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
import numpy as np |
| 7 |
from numpy.testing import assert_allclose, assert_equal |
| 8 |
|
| 9 |
from astropy import units as u |
| 10 |
from astropy.coordinates import Longitude, Latitude |
| 11 |
|
| 12 |
from ..core import (nside_to_pixel_area, nside_to_pixel_resolution, pixel_resolution_to_nside, |
| 13 |
nside_to_npix, npix_to_nside, healpix_to_lonlat, |
| 14 |
lonlat_to_healpix, interpolate_bilinear_lonlat, |
| 15 |
neighbours, healpix_cone_search, boundaries_lonlat, |
| 16 |
level_to_nside, nside_to_level, |
| 17 |
nested_to_ring, ring_to_nested, |
| 18 |
level_ipix_to_uniq, uniq_to_level_ipix, |
| 19 |
bilinear_interpolation_weights) |
| 20 |
|
| 21 |
|
| 22 |
def test_level_to_nside(): |
| 23 |
assert level_to_nside(5) == 2 ** 5 |
| 24 |
with pytest.raises(ValueError) as exc: |
| 25 |
level_to_nside(-1) |
| 26 |
assert exc.value.args[0] == 'level must be positive' |
| 27 |
|
| 28 |
|
| 29 |
def test_nside_to_level(): |
| 30 |
assert nside_to_level(1024) == 10 |
| 31 |
with pytest.raises(ValueError) as exc: |
| 32 |
nside_to_level(511) |
| 33 |
assert exc.value.args[0] == 'nside must be a power of two' |
| 34 |
|
| 35 |
|
| 36 |
def test_level_ipix_to_uniq(): |
| 37 |
assert 11 + 4*4**0 == level_ipix_to_uniq(0, 11) |
| 38 |
assert 62540 + 4*4**15 == level_ipix_to_uniq(15, 62540) |
| 39 |
with pytest.raises(ValueError) as exc: |
| 40 |
level_ipix_to_uniq(1, 49) |
| 41 |
assert exc.value.args[0] == 'ipix for a specific level must be inferior to npix' |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.parametrize("level", [ |
| 45 |
0, 5, 10, 15, 20, 22, 25, 26, 27, 28, 29 |
| 46 |
]) |
| 47 |
def test_uniq_to_level_ipix(level): |
| 48 |
npix = 3 << 2*(level + 1) |
| 49 |
# Take 10 pixel indices between 0 and npix - 1 |
| 50 |
size = 10 |
| 51 |
|
| 52 |
ipix = np.arange(size, dtype=np.int64) * (npix // size) |
| 53 |
level = np.ones(size) * level |
| 54 |
|
| 55 |
level_res, ipix_res = uniq_to_level_ipix(level_ipix_to_uniq(level, ipix)) |
| 56 |
assert np.all(level_res == level) & np.all(ipix_res == ipix) |
| 57 |
|
| 58 |
|
| 59 |
def test_nside_to_pixel_area(): |
| 60 |
resolution = nside_to_pixel_area(256) |
| 61 |
assert_allclose(resolution.value, 1.5978966540475428e-05) |
| 62 |
assert resolution.unit == u.sr |
| 63 |
|
| 64 |
|
| 65 |
def test_nside_to_pixel_resolution(): |
| 66 |
resolution = nside_to_pixel_resolution(256) |
| 67 |
assert_allclose(resolution.value, 13.741945647269624) |
| 68 |
assert resolution.unit == u.arcmin |
| 69 |
|
| 70 |
|
| 71 |
def test_pixel_resolution_to_nside(): |
| 72 |
|
| 73 |
# Check the different rounding options |
| 74 |
nside = pixel_resolution_to_nside(13 * u.arcmin, round='nearest') |
| 75 |
assert nside == 256 |
| 76 |
|
| 77 |
nside = pixel_resolution_to_nside(13 * u.arcmin, round='up') |
| 78 |
assert nside == 512 |
| 79 |
|
| 80 |
nside = pixel_resolution_to_nside(13 * u.arcmin, round='down') |
| 81 |
assert nside == 256 |
| 82 |
|
| 83 |
# Check that it works with arrays |
| 84 |
nside = pixel_resolution_to_nside([1e3, 10, 1e-3] * u.deg, round='nearest') |
| 85 |
assert_equal(nside, [1, 8, 65536]) |
| 86 |
|
| 87 |
with pytest.raises(ValueError) as exc: |
| 88 |
pixel_resolution_to_nside(13 * u.arcmin, round='peaches') |
| 89 |
assert exc.value.args[0] == "Invalid value for round: 'peaches'" |
| 90 |
|
| 91 |
with pytest.raises(AttributeError) as exc: |
| 92 |
pixel_resolution_to_nside(13) |
| 93 |
assert exc.value.args[0] == "'int' object has no attribute 'to'" |
| 94 |
|
| 95 |
|
| 96 |
def test_nside_to_npix(): |
| 97 |
npix = nside_to_npix(4) |
| 98 |
assert npix == 192 |
| 99 |
|
| 100 |
npix = nside_to_npix([4, 4]) |
| 101 |
assert_equal(npix, 192) |
| 102 |
|
| 103 |
with pytest.raises(ValueError) as exc: |
| 104 |
nside_to_npix(15) |
| 105 |
assert exc.value.args[0] == 'nside must be a power of two' |
| 106 |
|
| 107 |
|
| 108 |
def test_npix_to_nside(): |
| 109 |
nside = npix_to_nside(192) |
| 110 |
assert nside == 4 |
| 111 |
|
| 112 |
nside = npix_to_nside([192, 192]) |
| 113 |
assert_equal(nside, 4) |
| 114 |
|
| 115 |
with pytest.raises(ValueError) as exc: |
| 116 |
npix_to_nside(7) |
| 117 |
assert exc.value.args[0] == 'Number of pixels must be divisible by 12' |
| 118 |
|
| 119 |
with pytest.raises(ValueError) as exc: |
| 120 |
npix_to_nside(12 * 8 * 9) |
| 121 |
assert exc.value.args[0] == 'Number of pixels is not of the form 12 * nside ** 2' |
| 122 |
|
| 123 |
|
| 124 |
# For the following tests, the numerical accuracy of this function is already |
| 125 |
# tested in test_cython_api.py, so we focus here on functionality specific to |
| 126 |
# the Python functions. |
| 127 |
|
| 128 |
|
| 129 |
@pytest.mark.parametrize('order', ['nested', 'ring']) |
| 130 |
def test_healpix_to_lonlat(order): |
| 131 |
lon, lat = healpix_to_lonlat([1, 2, 3], 4, order=order) |
| 132 |
|
| 133 |
assert isinstance(lon, Longitude) |
| 134 |
assert isinstance(lat, Latitude) |
| 135 |
|
| 136 |
index = lonlat_to_healpix(lon, lat, 4, order=order) |
| 137 |
|
| 138 |
assert_equal(index, [1, 2, 3]) |
| 139 |
|
| 140 |
lon, lat = healpix_to_lonlat([1, 2, 3], 4, |
| 141 |
dx=[0.1, 0.2, 0.3], |
| 142 |
dy=[0.5, 0.4, 0.7], order=order) |
| 143 |
|
| 144 |
assert isinstance(lon, Longitude) |
| 145 |
assert isinstance(lat, Latitude) |
| 146 |
|
| 147 |
index, dx, dy = lonlat_to_healpix(lon, lat, 4, order=order, return_offsets=True) |
| 148 |
|
| 149 |
assert_equal(index, [1, 2, 3]) |
| 150 |
assert_allclose(dx, [0.1, 0.2, 0.3]) |
| 151 |
assert_allclose(dy, [0.5, 0.4, 0.7]) |
| 152 |
|
| 153 |
|
| 154 |
def test_healpix_to_lonlat_invalid(): |
| 155 |
dx = [0.1, 0.4, 0.9] |
| 156 |
dy = [0.4, 0.3, 0.2] |
| 157 |
|
| 158 |
with pytest.warns(RuntimeWarning, match='invalid value'): |
| 159 |
lon, lat = healpix_to_lonlat([-1, 2, 3], 4) |
| 160 |
|
| 161 |
with pytest.warns(RuntimeWarning, match='invalid value'): |
| 162 |
lon, lat = healpix_to_lonlat([192, 2, 3], 4) |
| 163 |
|
| 164 |
with pytest.raises(ValueError) as exc: |
| 165 |
lon, lat = healpix_to_lonlat([1, 2, 3], 5) |
| 166 |
assert exc.value.args[0] == 'nside must be a power of two' |
| 167 |
|
| 168 |
with pytest.raises(ValueError) as exc: |
| 169 |
lon, lat = healpix_to_lonlat([1, 2, 3], 4, order='banana') |
| 170 |
assert exc.value.args[0] == "order must be 'nested' or 'ring'" |
| 171 |
|
| 172 |
with pytest.raises(ValueError) as exc: |
| 173 |
lon, lat = healpix_to_lonlat([1, 2, 3], 4, dx=[-0.1, 0.4, 0.5], dy=dy) |
| 174 |
assert exc.value.args[0] == 'dx must be in the range [0:1]' |
| 175 |
|
| 176 |
with pytest.raises(ValueError) as exc: |
| 177 |
lon, lat = healpix_to_lonlat([1, 2, 3], 4, dx=dx, dy=[-0.1, 0.4, 0.5]) |
| 178 |
assert exc.value.args[0] == 'dy must be in the range [0:1]' |
| 179 |
|
| 180 |
|
| 181 |
def test_healpix_to_lonlat_shape(): |
| 182 |
lon, lat = healpix_to_lonlat(2, 8) |
| 183 |
assert lon.isscalar and lat.isscalar |
| 184 |
|
| 185 |
lon, lat = healpix_to_lonlat([[1, 2, 3], [3, 4, 4]], 8) |
| 186 |
assert lon.shape == (2, 3) and lat.shape == (2, 3) |
| 187 |
|
| 188 |
lon, lat = healpix_to_lonlat([[1], [2], [3]], nside=8, dx=0.2, dy=[[0.1, 0.3]]) |
| 189 |
assert lon.shape == (3, 2) and lat.shape == (3, 2) |
| 190 |
|
| 191 |
|
| 192 |
def test_lonlat_to_healpix_shape(): |
| 193 |
healpix_index = lonlat_to_healpix(2 * u.deg, 3 * u.deg, 8) |
| 194 |
assert np.can_cast(healpix_index, np.int64) |
| 195 |
|
| 196 |
lon, lat = np.ones((2, 4)) * u.deg, np.zeros((2, 4)) * u.deg |
| 197 |
healpix_index = lonlat_to_healpix(lon, lat, 8) |
| 198 |
assert healpix_index.shape == (2, 4) |
| 199 |
|
| 200 |
healpix_index, dx, dy = lonlat_to_healpix(2 * u.deg, 3 * u.deg, 8, return_offsets=True) |
| 201 |
assert np.can_cast(healpix_index, np.int64) |
| 202 |
assert isinstance(dx, float) |
| 203 |
assert isinstance(dy, float) |
| 204 |
|
| 205 |
lon, lat = np.ones((2, 4)) * u.deg, np.zeros((2, 4)) * u.deg |
| 206 |
healpix_index, dx, dy = lonlat_to_healpix(lon, lat, 8, return_offsets=True) |
| 207 |
assert healpix_index.shape == (2, 4) |
| 208 |
assert dx.shape == (2, 4) |
| 209 |
assert dy.shape == (2, 4) |
| 210 |
|
| 211 |
|
| 212 |
def test_lonlat_to_healpix_invalid(): |
| 213 |
"""Check that if we pass NaN values for example, the index is set to -1""" |
| 214 |
with pytest.warns(RuntimeWarning, match='invalid value'): |
| 215 |
ipix = lonlat_to_healpix(np.nan * u.deg, np.nan * u.deg, |
| 216 |
nside=1, order='nested') |
| 217 |
assert ipix == -1 |
| 218 |
|
| 219 |
|
| 220 |
@pytest.mark.parametrize('function', [nested_to_ring, ring_to_nested]) |
| 221 |
def test_nested_ring_shape(function): |
| 222 |
index = function(1, 8) |
| 223 |
assert np.can_cast(index, np.int64) |
| 224 |
|
| 225 |
index = function([[1, 2, 3], [2, 3, 4]], 8) |
| 226 |
assert index.shape == (2, 3) |
| 227 |
|
| 228 |
|
| 229 |
@pytest.mark.parametrize('order', ['nested', 'ring']) |
| 230 |
def test_bilinear_interpolation_weights(order): |
| 231 |
|
| 232 |
indices, weights = bilinear_interpolation_weights(100 * u.deg, 10 * u.deg, |
| 233 |
nside=4, order=order) |
| 234 |
if order == 'nested': |
| 235 |
indices = nested_to_ring(indices, nside=4) |
| 236 |
assert_equal(indices, [76, 77, 60, 59]) |
| 237 |
assert_allclose(weights, [0.532723, 0.426179, 0.038815, 0.002283], atol=1e-6) |
| 238 |
|
| 239 |
|
| 240 |
def test_bilinear_interpolation_weights_invalid(): |
| 241 |
with pytest.raises(ValueError) as exc: |
| 242 |
bilinear_interpolation_weights(1 * u.deg, 2 * u.deg, nside=5) |
| 243 |
assert exc.value.args[0] == 'nside must be a power of two' |
| 244 |
|
| 245 |
with pytest.raises(ValueError) as exc: |
| 246 |
bilinear_interpolation_weights(3 * u.deg, 4 * u.deg, |
| 247 |
nside=4, order='banana') |
| 248 |
assert exc.value.args[0] == "order must be 'nested' or 'ring'" |
| 249 |
|
| 250 |
|
| 251 |
def test_bilinear_interpolation_weights_shape(): |
| 252 |
|
| 253 |
indices, weights = bilinear_interpolation_weights(3 * u.deg, 4 * u.deg, nside=8) |
| 254 |
assert indices.shape == (4,) |
| 255 |
assert weights.shape == (4,) |
| 256 |
|
| 257 |
indices, weights = bilinear_interpolation_weights([[1, 2, 3], [2, 3, 4]] * u.deg, |
| 258 |
[[1, 2, 3], [2, 3, 4]] * u.deg, nside=8) |
| 259 |
assert indices.shape == (4, 2, 3) |
| 260 |
assert weights.shape == (4, 2, 3) |
| 261 |
|
| 262 |
|
| 263 |
@pytest.mark.parametrize('order', ['nested', 'ring']) |
| 264 |
def test_interpolate_bilinear_lonlat(order): |
| 265 |
values = np.ones(192) * 3 |
| 266 |
result = interpolate_bilinear_lonlat([1, 3, 4] * u.deg, [3, 2, 6] * u.deg, |
| 267 |
values, order=order) |
| 268 |
assert_allclose(result, [3, 3, 3]) |
| 269 |
|
| 270 |
|
| 271 |
def test_interpolate_bilinear_invalid(): |
| 272 |
values = np.ones(133) |
| 273 |
with pytest.raises(ValueError) as exc: |
| 274 |
interpolate_bilinear_lonlat([1, 3, 4] * u.deg, [3, 2, 6] * u.deg, values) |
| 275 |
assert exc.value.args[0] == 'Number of pixels must be divisible by 12' |
| 276 |
|
| 277 |
values = np.ones(192) |
| 278 |
with pytest.raises(ValueError) as exc: |
| 279 |
interpolate_bilinear_lonlat([1, 3, 4] * u.deg, [3, 2, 6] * u.deg, |
| 280 |
values, order='banana') |
| 281 |
assert exc.value.args[0] == "order must be 'nested' or 'ring'" |
| 282 |
|
| 283 |
with pytest.warns(RuntimeWarning, match='invalid value'): |
| 284 |
result = interpolate_bilinear_lonlat([0, np.nan] * u.deg, |
| 285 |
[0, np.nan] * u.deg, values, |
| 286 |
order='nested') |
| 287 |
assert result.shape == (2,) |
| 288 |
assert result[0] == 1 |
| 289 |
assert np.isnan(result[1]) |
| 290 |
|
| 291 |
|
| 292 |
def test_interpolate_bilinear_lonlat_shape(): |
| 293 |
|
| 294 |
values = np.ones(192) * 3 |
| 295 |
|
| 296 |
result = interpolate_bilinear_lonlat(3 * u.deg, 4 * u.deg, values) |
| 297 |
assert isinstance(result, float) |
| 298 |
|
| 299 |
result = interpolate_bilinear_lonlat([[1, 2, 3], [2, 3, 4]] * u.deg, |
| 300 |
[[1, 2, 3], [2, 3, 4]] * u.deg, values) |
| 301 |
assert result.shape == (2, 3) |
| 302 |
|
| 303 |
values = np.ones((192, 50)) * 3 |
| 304 |
|
| 305 |
lon = np.ones((3, 6, 5)) * u.deg |
| 306 |
lat = np.ones((3, 6, 5)) * u.deg |
| 307 |
|
| 308 |
result = interpolate_bilinear_lonlat(lon, lat, values) |
| 309 |
assert result.shape == (3, 6, 5, 50) |
| 310 |
|
| 311 |
|
| 312 |
@pytest.mark.parametrize('order', ['nested', 'ring']) |
| 313 |
def test_neighbours(order): |
| 314 |
neigh = neighbours([1, 2, 3], 4, order=order) |
| 315 |
|
| 316 |
if order == 'nested': |
| 317 |
expected = [[0, 71, 2], |
| 318 |
[2, 77, 8], |
| 319 |
[3, 8, 9], |
| 320 |
[6, 9, 12], |
| 321 |
[4, 3, 6], |
| 322 |
[94, 1, 4], |
| 323 |
[91, 0, 1], |
| 324 |
[90, 69, 0]] |
| 325 |
else: |
| 326 |
|
| 327 |
expected = [[6, 8, 10], |
| 328 |
[5, 7, 9], |
| 329 |
[0, 1, 2], |
| 330 |
[3, 0, 1], |
| 331 |
[2, 3, 0], |
| 332 |
[8, 10, 4], |
| 333 |
[7, 9, 11], |
| 334 |
[16, 19, 22]] |
| 335 |
|
| 336 |
assert_equal(neigh, expected) |
| 337 |
|
| 338 |
|
| 339 |
def test_neighbours_invalid(): |
| 340 |
with pytest.warns(RuntimeWarning, match='invalid value'): |
| 341 |
neighbours([-1, 2, 3], 4) |
| 342 |
|
| 343 |
with pytest.warns(RuntimeWarning, match='invalid value'): |
| 344 |
neighbours([192, 2, 3], 4) |
| 345 |
|
| 346 |
with pytest.raises(ValueError) as exc: |
| 347 |
neighbours([1, 2, 3], 5) |
| 348 |
assert exc.value.args[0] == 'nside must be a power of two' |
| 349 |
|
| 350 |
with pytest.raises(ValueError) as exc: |
| 351 |
neighbours([1, 2, 3], 4, order='banana') |
| 352 |
assert exc.value.args[0] == "order must be 'nested' or 'ring'" |
| 353 |
|
| 354 |
|
| 355 |
def test_neighbours_shape(): |
| 356 |
neigh = neighbours([[1, 2, 3], [2, 3, 4]], 4) |
| 357 |
assert neigh.shape == (8, 2, 3) |
| 358 |
|
| 359 |
|
| 360 |
@pytest.mark.parametrize('order', ['nested', 'ring']) |
| 361 |
def test_healpix_cone_search(order): |
| 362 |
indices = healpix_cone_search(10 * u.deg, 20 * u.deg, 1 * u.deg, |
| 363 |
nside=256, order=order) |
| 364 |
|
| 365 |
assert len(indices) == 80 |
| 366 |
|
| 367 |
|
| 368 |
@pytest.mark.parametrize(('step', 'order'), product([1, 4, 10], ['nested', 'ring'])) |
| 369 |
def test_boundaries_lonlat(step, order): |
| 370 |
lon, lat = boundaries_lonlat([10, 20, 30], step, 256, order=order) |
| 371 |
assert lon.shape == (3, 4 * step) |
| 372 |
assert lat.shape == (3, 4 * step) |