Coverage for tests/test_HealpixPixelization.py: 21%
88 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:50 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:50 -0700
1#
2# LSST Data Management System
3# See COPYRIGHT file at the top of the source tree.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <https://www.lsstcorp.org/LegalNotices/>.
21#
22import pickle
23import unittest
25import hpgeom as hpg
26import numpy as np
28try:
29 import yaml
30except ImportError:
31 yaml = None
33from lsst.sphgeom import Angle, Box, Circle, ConvexPolygon, Ellipse, HealpixPixelization, LonLat, UnitVector3d
36class HealpixPixelizationTestCase(unittest.TestCase):
37 """Test HEALPix pixelization."""
39 def test_construction(self):
40 """Test construction of a HealpixPixelization."""
41 with self.assertRaises(ValueError):
42 HealpixPixelization(-1)
43 h1 = HealpixPixelization(5)
44 self.assertEqual(h1.level, 5)
45 self.assertEqual(h1.getLevel(), 5)
46 self.assertEqual(h1.nside, 32)
47 h2 = HealpixPixelization(6)
48 h3 = HealpixPixelization(h2.level)
49 self.assertNotEqual(h1, h2)
50 self.assertEqual(h2, h3)
52 def test_indexing(self):
53 """Test indexing of HealpixPixelization."""
54 h = HealpixPixelization(5)
55 vec = UnitVector3d(1, 1, 1)
56 lonlat = LonLat(vec)
57 pix = hpg.angle_to_pixel(h.nside, lonlat.getLon().asDegrees(), lonlat.getLat().asDegrees())
58 self.assertEqual(h.index(UnitVector3d(1, 1, 1)), pix)
60 def test_pixel(self):
61 """Test pixel polygon of HealpixPixelization."""
62 h = HealpixPixelization(5)
63 pix_poly = h.pixel(10)
64 self.assertIsInstance(pix_poly, ConvexPolygon)
66 def test_envelope(self):
67 """Test envelope method of HealpixPixelization."""
68 # Make the hardest intersection: a region that _just_
69 # touches a healpix pixel.
70 h = HealpixPixelization(5)
71 pix = hpg.angle_to_pixel(h.nside, 50.0, 20.0)
73 corners_ra, corners_dec = hpg.boundaries(h.nside, pix, step=1)
75 # Take the southernmost corner...
76 smost = np.argmin(corners_dec)
78 # Choose challenging comparison box corners:
79 ra_range = np.array([corners_ra[smost] - 0.5, corners_ra[smost] + 0.5])
80 dec_range = np.array([corners_dec[smost] - 0.5, corners_dec[smost] + 1e-8])
82 # Test the box region
83 box = Box(
84 point1=LonLat.fromDegrees(ra_range[0], dec_range[0]),
85 point2=LonLat.fromDegrees(ra_range[1], dec_range[1]),
86 )
87 # These pixels have been checked to completely overlap the region
88 self._check_envelope(h, box, [98, 99, 104, 105])
90 # Try a polygon region:
91 poly = ConvexPolygon(
92 [
93 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])),
94 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[0])),
95 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[1])),
96 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[1])),
97 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])),
98 ]
99 )
100 self._check_envelope(h, poly, [98, 99, 104, 105])
102 # Try a circle region
103 circle = Circle(
104 center=UnitVector3d(
105 LonLat.fromDegrees((ra_range[0] + ra_range[1]) / 2.0, (dec_range[0] + dec_range[1]) / 2.0)
106 ),
107 angle=Angle.fromDegrees((dec_range[1] - dec_range[0]) / 2.0),
108 )
109 self._check_envelope(h, circle, [98, 99, 104, 105])
111 def _check_envelope(self, pixelization, region, check_pixels):
112 """Check the envelope from a region.
114 Parameters
115 ----------
116 pixelization : `lsst.sphgeom.HealpixPixelization`
117 region : `lsst.sphgeom.Region`
118 check_pixels : `list` [`int`]
119 """
120 pixel_range = pixelization.envelope(region)
122 pixels = []
123 for r in pixel_range.ranges():
124 pixels.extend(range(r[0], r[1]))
126 self.assertEqual(pixels, check_pixels)
128 def test_interior(self):
129 """Test interior method of HealpixPixelization."""
130 h = HealpixPixelization(5)
131 pix = hpg.angle_to_pixel(h.nside, 50.0, 20.0)
133 corners_ra, corners_dec = hpg.boundaries(h.nside, pix, step=1)
135 ra_range = np.array([corners_ra.min() - 1.0, corners_ra.max() + 1.0])
136 dec_range = np.array([corners_dec.min() - 1.0, corners_dec.max() + 1.0])
138 # Test the box region
139 box = Box(
140 point1=LonLat.fromDegrees(ra_range[0], dec_range[0]),
141 point2=LonLat.fromDegrees(ra_range[1], dec_range[1]),
142 )
143 # These pixels have been checked to completely overlap the region
144 self._check_interior(h, box, [pix])
146 # Try a polygon region:
147 poly = ConvexPolygon(
148 [
149 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])),
150 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[0])),
151 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[1])),
152 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[1])),
153 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])),
154 ]
155 )
156 self._check_interior(h, poly, [pix])
158 # Try a circle region
159 circle = Circle(
160 center=UnitVector3d(
161 LonLat.fromDegrees((ra_range[0] + ra_range[1]) / 2.0, (dec_range[0] + dec_range[1]) / 2.0)
162 ),
163 angle=Angle.fromDegrees(2.5),
164 )
165 self._check_interior(h, circle, [pix])
167 # Try an ellipse region
168 ellipse = Ellipse(
169 center=UnitVector3d(
170 LonLat.fromDegrees((ra_range[0] + ra_range[1]) / 2.0, (dec_range[0] + dec_range[1]) / 2.0)
171 ),
172 alpha=Angle.fromDegrees(1.5),
173 beta=Angle.fromDegrees(2.5),
174 orientation=Angle.fromDegrees(45.0),
175 )
176 self._check_interior(h, ellipse, [pix])
178 def _check_interior(self, pixelization, region, check_pixels):
179 """Check the interior from a region.
181 Parameters
182 ----------
183 pixelization : `lsst.sphgeom.HealpixPixelization`
184 region : `lsst.sphgeom.Region`
185 check_pixels : `list` [`int`]
186 """
187 pixel_range = pixelization.interior(region)
189 pixels = []
190 for r in pixel_range.ranges():
191 pixels.extend(range(r[0], r[1]))
193 self.assertEqual(pixels, check_pixels)
195 def test_index_to_string(self):
196 """Test converting index to string of HealpixPixelization."""
197 h = HealpixPixelization(5)
198 self.assertEqual(h.toString(0), str(0))
199 self.assertEqual(h.toString(100), str(100))
201 def test_string(self):
202 """Test string representation of HealpixPixelization."""
203 h = HealpixPixelization(5)
204 self.assertEqual(str(h), "HealpixPixelization(5)")
205 self.assertEqual(str(h), repr(h))
206 self.assertEqual(h, eval(repr(h), {"HealpixPixelization": HealpixPixelization}))
208 def test_pickle(self):
209 """Test pickling of HealpixPixelization."""
210 a = HealpixPixelization(5)
211 b = pickle.loads(pickle.dumps(a))
212 self.assertEqual(a, b)
214 @unittest.skipIf(not yaml, "YAML module can not be imported")
215 def test_yaml(self):
216 """Test yaml representation of HealpixPixelization."""
217 a = HealpixPixelization(5)
218 b = yaml.safe_load(yaml.dump(a))
219 self.assertEqual(a, b)
222if __name__ == "__main__":
223 unittest.main()