Coverage for tests/test_HealpixPixelization.py: 22%
90 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-31 02:34 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-31 02:34 -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 def test_construction(self):
38 """Test construction of a HealpixPixelization."""
39 with self.assertRaises(ValueError):
40 HealpixPixelization(-1)
41 h1 = HealpixPixelization(5)
42 self.assertEqual(h1.level, 5)
43 self.assertEqual(h1.getLevel(), 5)
44 self.assertEqual(h1.nside, 32)
45 h2 = HealpixPixelization(6)
46 h3 = HealpixPixelization(h2.level)
47 self.assertNotEqual(h1, h2)
48 self.assertEqual(h2, h3)
50 def test_indexing(self):
51 """Test indexing of HealpixPixelization."""
52 h = HealpixPixelization(5)
53 vec = UnitVector3d(1, 1, 1)
54 lonlat = LonLat(vec)
55 pix = hpg.angle_to_pixel(h.nside, lonlat.getLon().asDegrees(), lonlat.getLat().asDegrees())
56 self.assertEqual(h.index(UnitVector3d(1, 1, 1)), pix)
58 def test_pixel(self):
59 """Test pixel polygon of HealpixPixelization."""
60 h = HealpixPixelization(5)
61 pix_poly = h.pixel(10)
62 self.assertIsInstance(pix_poly, ConvexPolygon)
64 def test_envelope(self):
65 """Test envelope method of HealpixPixelization."""
66 # Make the hardest intersection: a region that _just_
67 # touches a healpix pixel.
68 h = HealpixPixelization(5)
69 pix = hpg.angle_to_pixel(h.nside, 50.0, 20.0)
71 corners_ra, corners_dec = hpg.boundaries(h.nside, pix, step=1)
73 # Take the southernmost corner...
74 smost = np.argmin(corners_dec)
76 # Choose challenging comparison box corners:
77 ra_range = np.array([corners_ra[smost] - 0.5, corners_ra[smost] + 0.5])
78 dec_range = np.array([corners_dec[smost] - 0.5, corners_dec[smost] + 1e-8])
80 # Test the box region
81 box = Box(
82 point1=LonLat.fromDegrees(ra_range[0], dec_range[0]),
83 point2=LonLat.fromDegrees(ra_range[1], dec_range[1]),
84 )
85 # These pixels have been checked to completely overlap the region
86 self._check_envelope(h, box, [98, 99, 104, 105])
88 # Try a polygon region:
89 poly = ConvexPolygon(
90 [
91 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])),
92 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[0])),
93 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[1])),
94 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[1])),
95 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])),
96 ]
97 )
98 self._check_envelope(h, poly, [98, 99, 104, 105])
100 # Try a circle region
101 circle = Circle(
102 center=UnitVector3d(
103 LonLat.fromDegrees((ra_range[0] + ra_range[1]) / 2.0, (dec_range[0] + dec_range[1]) / 2.0)
104 ),
105 angle=Angle.fromDegrees((dec_range[1] - dec_range[0]) / 2.0),
106 )
107 self._check_envelope(h, circle, [98, 99, 104, 105])
109 def _check_envelope(self, pixelization, region, check_pixels):
110 """Check the envelope from a region.
112 Parameters
113 ----------
114 pixelization : `lsst.sphgeom.HealpixPixelization`
115 region : `lsst.sphgeom.Region`
116 check_pixels : `list` [`int`]
117 """
118 pixel_range = pixelization.envelope(region)
120 pixels = []
121 for r in pixel_range.ranges():
122 pixels.extend(range(r[0], r[1]))
124 self.assertEqual(pixels, check_pixels)
126 def test_interior(self):
127 """Test interior method of HealpixPixelization."""
129 h = HealpixPixelization(5)
130 pix = hpg.angle_to_pixel(h.nside, 50.0, 20.0)
132 corners_ra, corners_dec = hpg.boundaries(h.nside, pix, step=1)
134 ra_range = np.array([corners_ra.min() - 1.0, corners_ra.max() + 1.0])
135 dec_range = np.array([corners_dec.min() - 1.0, corners_dec.max() + 1.0])
137 # Test the box region
138 box = Box(
139 point1=LonLat.fromDegrees(ra_range[0], dec_range[0]),
140 point2=LonLat.fromDegrees(ra_range[1], dec_range[1]),
141 )
142 # These pixels have been checked to completely overlap the region
143 self._check_interior(h, box, [pix])
145 # Try a polygon region:
146 poly = ConvexPolygon(
147 [
148 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])),
149 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[0])),
150 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[1])),
151 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[1])),
152 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])),
153 ]
154 )
155 self._check_interior(h, poly, [pix])
157 # Try a circle region
158 circle = Circle(
159 center=UnitVector3d(
160 LonLat.fromDegrees((ra_range[0] + ra_range[1]) / 2.0, (dec_range[0] + dec_range[1]) / 2.0)
161 ),
162 angle=Angle.fromDegrees(2.5),
163 )
164 self._check_interior(h, circle, [pix])
166 # Try an ellipse region
167 ellipse = Ellipse(
168 center=UnitVector3d(
169 LonLat.fromDegrees((ra_range[0] + ra_range[1]) / 2.0, (dec_range[0] + dec_range[1]) / 2.0)
170 ),
171 alpha=Angle.fromDegrees(1.5),
172 beta=Angle.fromDegrees(2.5),
173 orientation=Angle.fromDegrees(45.0),
174 )
175 self._check_interior(h, ellipse, [pix])
177 def _check_interior(self, pixelization, region, check_pixels):
178 """Check the interior from a region.
180 Parameters
181 ----------
182 pixelization : `lsst.sphgeom.HealpixPixelization`
183 region : `lsst.sphgeom.Region`
184 check_pixels : `list` [`int`]
185 """
186 pixel_range = pixelization.interior(region)
188 pixels = []
189 for r in pixel_range.ranges():
190 pixels.extend(range(r[0], r[1]))
192 self.assertEqual(pixels, check_pixels)
194 def test_index_to_string(self):
195 """Test converting index to string of HealpixPixelization."""
196 h = HealpixPixelization(5)
197 self.assertEqual(h.toString(0), str(0))
198 self.assertEqual(h.toString(100), str(100))
200 def test_string(self):
201 """Test string representation of HealpixPixelization."""
202 h = HealpixPixelization(5)
203 self.assertEqual(str(h), "HealpixPixelization(5)")
204 self.assertEqual(str(h), repr(h))
205 self.assertEqual(h, eval(repr(h), dict(HealpixPixelization=HealpixPixelization)))
207 def test_pickle(self):
208 """Test pickling of HealpixPixelization."""
209 a = HealpixPixelization(5)
210 b = pickle.loads(pickle.dumps(a))
211 self.assertEqual(a, b)
213 @unittest.skipIf(not yaml, "YAML module can not be imported")
214 def test_yaml(self):
215 """Test yaml representation of HealpixPixelization."""
216 a = HealpixPixelization(5)
217 b = yaml.safe_load(yaml.dump(a))
218 self.assertEqual(a, b)
221if __name__ == "__main__": 221 ↛ 222line 221 didn't jump to line 222, because the condition on line 221 was never true
222 unittest.main()