From 27dc372e0c898d7519f082c0f379bf35218e4bb5 Mon Sep 17 00:00:00 2001 From: Eli Rykoff Date: Tue, 22 Oct 2024 13:12:10 -0700 Subject: [PATCH 1/2] Add test when getting an envelope with a neghborless pixel. This happens primarily near the pole. --- tests/test_HealpixPixelization.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_HealpixPixelization.py b/tests/test_HealpixPixelization.py index 895876d..17ced07 100644 --- a/tests/test_HealpixPixelization.py +++ b/tests/test_HealpixPixelization.py @@ -114,6 +114,32 @@ def test_envelope(self): ) self._check_envelope(h, circle, [98, 99, 104, 105]) + def test_envelope_missing_neighbors(self): + """Test envelope, with a pixel with missing neighbors. + + Testing DM-47043. + """ + h = HealpixPixelization(0) + h_highres = HealpixPixelization(4) + + self.assertEqual(h._nside_highres, h_highres._nside) + + # Find a pixel with a missing neighbor at high res. + n = hpg.neighbors(h_highres._nside, np.arange(hpg.nside_to_npixel(h_highres._nside))) + ind = np.argmin(n) + self.assertEqual(n.ravel()[ind], -1) + + # This is the pixel with a bad (-1) neighbor. + badpix = ind // 8 + ra, dec = hpg.pixel_to_angle(h_highres._nside, badpix) + + box = Box( + point1=LonLat.fromDegrees(ra - 0.1, dec - 0.1), + point2=LonLat.fromDegrees(ra + 0.1, dec + 0.1), + ) + + self._check_envelope(h, box, [0, 1, 5]) + def _check_envelope(self, pixelization, region, check_pixels): """Check the envelope from a region. From e18d690ccd536990898756ee7702638f35e93faf Mon Sep 17 00:00:00 2001 From: Eli Rykoff Date: Tue, 22 Oct 2024 13:12:52 -0700 Subject: [PATCH 2/2] Filter out negative (missing) neighbors when computing healpix envelope. --- python/lsst/sphgeom/_healpixPixelization.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/lsst/sphgeom/_healpixPixelization.py b/python/lsst/sphgeom/_healpixPixelization.py index dca6157..abb0880 100644 --- a/python/lsst/sphgeom/_healpixPixelization.py +++ b/python/lsst/sphgeom/_healpixPixelization.py @@ -89,9 +89,9 @@ def envelope(self, region: Region, maxRanges: int = 0): # Dilate the high resolution pixels by one to ensure that the full # region is completely covered at high resolution. - neighbors = hpg.neighbors(self._nside_highres, pixels_highres) + neighbors = hpg.neighbors(self._nside_highres, pixels_highres).ravel() # Shift back to the original resolution and uniquify - pixels = np.unique(np.right_shift(neighbors.ravel(), self._bit_shift)) + pixels = np.unique(np.right_shift(neighbors[neighbors >= 0], self._bit_shift)) return RangeSet(pixels)