Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with artifacts in reproject_to_healpix #459

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions reproject/healpix/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from astropy.coordinates import SkyCoord
from astropy_healpix import HEALPix, npix_to_nside

from reproject.array_utils import map_coordinates

__all__ = ["healpix_to_image", "image_to_healpix"]

ORDER = {}
Expand Down Expand Up @@ -121,7 +123,6 @@ def image_to_healpix(data, wcs_in, coord_system_out, nside, order="bilinear", ne
no coverage or valid values in the input image, while values of 1
indicate valid values.
"""
from scipy.ndimage import map_coordinates

hp = HEALPix(nside=nside, order="nested" if nested else "ring")

Expand All @@ -134,13 +135,19 @@ def image_to_healpix(data, wcs_in, coord_system_out, nside, order="bilinear", ne
world_out = SkyCoord(lon_out, lat_out, frame=coord_system_out)

# Look up pixels in input WCS
yinds, xinds = wcs_in.world_to_pixel(world_out)
xinds, yinds = wcs_in.world_to_pixel(world_out)

# Interpolate

if isinstance(order, str):
order = ORDER[order]

healpix_data = map_coordinates(data, [xinds, yinds], order=order, mode="constant", cval=np.nan)
healpix_data = map_coordinates(
data,
np.array([yinds, xinds]),
order=order,
mode="constant",
cval=np.nan,
)

return healpix_data, (~np.isnan(healpix_data)).astype(float)
30 changes: 29 additions & 1 deletion reproject/healpix/tests/test_healpix.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_reproject_to_healpix_input_types(valid_celestial_input_data):

# Make sure there are some valid values

assert np.sum(~np.isnan(healpix_data_ref)) == 3
assert np.sum(~np.isnan(healpix_data_ref)) == 4

np.testing.assert_allclose(healpix_data_ref, healpix_data_test)
np.testing.assert_allclose(footprint_ref, footprint_test)
Expand All @@ -180,3 +180,31 @@ def test_reproject_from_healpix_output_types(valid_celestial_output_projections)

np.testing.assert_allclose(output_ref, output_test)
np.testing.assert_allclose(footprint_ref, footprint_test)


def test_reproject_to_healpix_exact_allsky():

# Regression test for a bug that caused artifacts in the final image if the
# WCS covered the whole sky - this was due to using scipy's map_coordinates
# one instead of our built-in one which deals properly with the pixels
# around the rim.

shape_out = (160, 320)
wcs = WCS(naxis=2)
wcs.wcs.crpix = [(shape_out[1] + 1) / 2, (shape_out[0] + 1) / 2]
wcs.wcs.cdelt = np.array([-360.0 / shape_out[1], 180.0 / shape_out[0]])
wcs.wcs.crval = [0, 0]
wcs.wcs.ctype = ["RA---CAR", "DEC--CAR"]

array = np.ones(shape_out)

healpix_array, footprint = reproject_to_healpix(
(array, wcs),
coord_system_out="galactic",
nside=64,
nested=False,
order="bilinear",
)

assert np.all(footprint > 0)
assert not np.any(np.isnan(healpix_array))
Loading