Coverage for tests/test_HealpixPixelization.py: 22%
90 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-02 18:10 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-02 18:10 -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 unittest
23import numpy as np
24import healpy as hp
26import pickle
27try:
28 import yaml
29except ImportError:
30 yaml = None
32from lsst.sphgeom import (Angle, Circle, HealpixPixelization,
33 UnitVector3d, ConvexPolygon, LonLat, Box)
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 = hp.ang2pix(h.nside,
56 lonlat.getLon().asDegrees(),
57 lonlat.getLat().asDegrees(),
58 lonlat=True,
59 nest=True)
60 self.assertEqual(h.index(UnitVector3d(1, 1, 1)), pix)
62 def test_pixel(self):
63 """Test pixel polygon of HealpixPixelization."""
64 h = HealpixPixelization(5)
65 pix_poly = h.pixel(10)
66 self.assertIsInstance(pix_poly, ConvexPolygon)
68 def test_envelope(self):
69 """Test envelope method of HealpixPixelization."""
70 # Make the hardest intersection: a region that _just_
71 # touches a healpix pixel.
72 h = HealpixPixelization(5)
73 pix = hp.ang2pix(h.nside, 50.0, 20.0, lonlat=True, nest=True)
75 corners = hp.boundaries(h.nside, pix, step=1, nest=True)
76 corners_ra, corners_dec = hp.vec2ang(corners.T, lonlat=True)
78 # Take the southernmost corner...
79 smost = np.argmin(corners_dec)
81 # Choose challenging comparison box corners:
82 ra_range = np.array([corners_ra[smost] - 0.5,
83 corners_ra[smost] + 0.5])
84 dec_range = np.array([corners_dec[smost] - 0.5,
85 corners_dec[smost] + 1e-8])
87 # Test the box region
88 box = Box(point1=LonLat.fromDegrees(ra_range[0], dec_range[0]),
89 point2=LonLat.fromDegrees(ra_range[1], dec_range[1]))
90 # These pixels have been checked to completely overlap the region
91 self._check_envelope(h, box, [98, 99, 104, 105])
93 # Try a polygon region:
94 poly = ConvexPolygon([UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])),
95 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[0])),
96 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[1])),
97 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[1])),
98 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0]))])
99 self._check_envelope(h, poly, [98, 99, 104, 105])
101 # Try a circle region
102 circle = Circle(center=UnitVector3d(LonLat.fromDegrees((ra_range[0] + ra_range[1])/2.,
103 (dec_range[0] + dec_range[1])/2.)),
104 angle=Angle.fromDegrees((dec_range[1] - dec_range[0])/2.))
105 self._check_envelope(h, circle, [98, 99, 104, 105])
107 def _check_envelope(self, pixelization, region, check_pixels):
108 """Check the envelope from a region.
110 Parameters
111 ----------
112 pixelization : `lsst.sphgeom.HealpixPixelization`
113 region : `lsst.sphgeom.Region`
114 check_pixels : `list` [`int`]
115 """
116 pixel_range = pixelization.envelope(region)
118 pixels = []
119 for r in pixel_range.ranges():
120 pixels.extend(range(r[0], r[1]))
122 self.assertEqual(pixels, check_pixels)
124 def test_interior(self):
125 """Test interior method of HealpixPixelization."""
127 h = HealpixPixelization(5)
128 pix = hp.ang2pix(h.nside, 50.0, 20.0, lonlat=True, nest=True)
130 corners = hp.boundaries(h.nside, pix, step=1, nest=True)
131 corners_ra, corners_dec = hp.vec2ang(corners.T, lonlat=True)
133 ra_range = np.array([corners_ra.min() - 1.0, corners_ra.max() + 1.0])
134 dec_range = np.array([corners_dec.min() - 1.0, corners_dec.max() + 1.0])
136 # Test the box region
137 box = Box(point1=LonLat.fromDegrees(ra_range[0], dec_range[0]),
138 point2=LonLat.fromDegrees(ra_range[1], dec_range[1]))
139 # These pixels have been checked to completely overlap the region
140 self._check_interior(h, box, [pix])
142 # Try a polygon region:
143 poly = ConvexPolygon([UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])),
144 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[0])),
145 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[1])),
146 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[1])),
147 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0]))])
148 self._check_interior(h, poly, [pix])
150 # Try a circle region
151 circle = Circle(center=UnitVector3d(LonLat.fromDegrees((ra_range[0] + ra_range[1])/2.,
152 (dec_range[0] + dec_range[1])/2.)),
153 angle=Angle.fromDegrees(2.5))
154 self._check_interior(h, circle, [pix])
156 def _check_interior(self, pixelization, region, check_pixels):
157 """Check the interior from a region.
159 Parameters
160 ----------
161 pixelization : `lsst.sphgeom.HealpixPixelization`
162 region : `lsst.sphgeom.Region`
163 check_pixels : `list` [`int`]
164 """
165 pixel_range = pixelization.interior(region)
167 pixels = []
168 for r in pixel_range.ranges():
169 pixels.extend(range(r[0], r[1]))
171 self.assertEqual(pixels, check_pixels)
173 def test_index_to_string(self):
174 """Test converting index to string of HealpixPixelization."""
175 h = HealpixPixelization(5)
176 self.assertEqual(h.toString(0), str(0))
177 self.assertEqual(h.toString(100), str(100))
179 def test_string(self):
180 """Test string representation of HealpixPixelization."""
181 h = HealpixPixelization(5)
182 self.assertEqual(str(h), 'HealpixPixelization(5)')
183 self.assertEqual(str(h), repr(h))
184 self.assertEqual(
185 h, eval(repr(h), dict(HealpixPixelization=HealpixPixelization)))
187 def test_pickle(self):
188 """Test pickling of HealpixPixelization."""
189 a = HealpixPixelization(5)
190 b = pickle.loads(pickle.dumps(a))
191 self.assertEqual(a, b)
193 @unittest.skipIf(not yaml, "YAML module can not be imported")
194 def test_yaml(self):
195 """Test yaml representation of HealpixPixelization."""
196 a = HealpixPixelization(5)
197 b = yaml.safe_load(yaml.dump(a))
198 self.assertEqual(a, b)
201if __name__ == "__main__": 201 ↛ 202line 201 didn't jump to line 202, because the condition on line 201 was never true
202 unittest.main()