Coverage for tests / test_HtmPixelization.py: 16%
93 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:29 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:29 +0000
1# This file is part of sphgeom.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28import pickle
30try:
31 import yaml
32except ImportError:
33 yaml = None
35import unittest
37from lsst.sphgeom import (
38 Angle,
39 Circle,
40 ConvexPolygon,
41 HtmPixelization,
42 IntersectionRegion,
43 RangeSet,
44 UnionRegion,
45 UnitVector3d,
46)
49class HtmPixelizationTestCase(unittest.TestCase):
50 """Test HTM pixels."""
52 def test_construction(self):
53 with self.assertRaises(ValueError):
54 HtmPixelization(-1)
55 with self.assertRaises(ValueError):
56 HtmPixelization(HtmPixelization.MAX_LEVEL + 1)
57 h1 = HtmPixelization(0)
58 self.assertEqual(h1.getLevel(), 0)
59 h2 = HtmPixelization(1)
60 h3 = HtmPixelization(h2)
61 self.assertNotEqual(h1, h2)
62 self.assertEqual(h2, h3)
64 def test_indexing(self):
65 h = HtmPixelization(1)
66 self.assertEqual(h.index(UnitVector3d(1, 1, 1)), 63)
68 def test_pixel(self):
69 h = HtmPixelization(1)
70 self.assertIsInstance(h.pixel(10), ConvexPolygon)
72 def test_level(self):
73 for index in (0, 16 * 4**HtmPixelization.MAX_LEVEL):
74 self.assertEqual(HtmPixelization.level(index), -1)
75 for level in range(HtmPixelization.MAX_LEVEL + 1):
76 for root in range(8, 16):
77 self.assertEqual(HtmPixelization.level(root * 4**level), level)
79 def test_envelope_and_interior(self):
80 pixelization = HtmPixelization(3)
81 c = Circle(UnitVector3d(1, 1, 1), Angle.fromDegrees(0.1))
82 rs1 = pixelization.envelope(c)
83 self.assertEqual(rs1, RangeSet(0x3FF))
84 rs = pixelization.envelope(c, 1)
85 self.assertEqual(rs, RangeSet(0x3FF))
86 self.assertTrue(rs.isWithin(pixelization.universe()))
87 rs = pixelization.interior(c)
88 self.assertTrue(rs.empty())
90 # Create a second region for a union.
91 s3 = UnitVector3d(1.0, -1.0, 1.0) # Center of S3
92 c2 = Circle(s3, 1e-8)
93 rs2 = pixelization.envelope(c2)
94 self.assertEqual(rs2.ranges(), [(831, 832)])
96 # Try again with a union.
97 union = UnionRegion(c, c2)
98 rsu = pixelization.envelope(union)
99 self.assertEqual(rsu, rs1 | rs2)
101 # Check that nested unions also work.
102 c3 = Circle(s3, 2e-8)
103 union2 = UnionRegion(union, c3)
104 rsu2 = pixelization.envelope(union2)
105 self.assertEqual(rsu2, rsu2 | rsu)
107 # Check with intersection
108 c4 = Circle(UnitVector3d(1.0, 1.0, 2.0), 1)
109 c5 = Circle(UnitVector3d(1.0, 1.0, 2.5), 0.5)
110 rs4 = pixelization.envelope(c4)
111 rs5 = pixelization.envelope(c5)
112 intersection = IntersectionRegion(c4, c5)
113 rsi = pixelization.envelope(intersection)
114 self.assertEqual(rsi, rs4 & rs5)
116 # Check that nested intersection also work.
117 c6 = Circle(UnitVector3d(1.0, 1.0, 2.0), 2)
118 intersection2 = IntersectionRegion(intersection, c6)
119 rsi2 = pixelization.envelope(intersection2)
120 self.assertEqual(rsi2, rsi2 & rsi)
122 # Check with empty union.
123 union3 = UnionRegion()
124 rsu3 = pixelization.envelope(union3)
125 self.assertTrue(rsu3.empty())
127 # Check with empty intersection, which is the same as the full sky.
128 intersection3 = IntersectionRegion()
129 rsi3 = pixelization.envelope(intersection3)
130 self.assertEqual(rsi3, pixelization.universe())
132 def test_index_to_string(self):
133 strings = ["S0", "S1", "S2", "S3", "N0", "N1", "N2", "N3"]
134 for i in range(8, 16):
135 s0 = strings[i - 8]
136 self.assertEqual(HtmPixelization.asString(i), s0)
137 self.assertEqual(HtmPixelization(0).toString(i), s0)
138 for j in range(4):
139 s1 = s0 + str(j)
140 self.assertEqual(HtmPixelization.asString(i * 4 + j), s1)
141 self.assertEqual(HtmPixelization(1).asString(i * 4 + j), s1)
143 def test_string(self):
144 p = HtmPixelization(3)
145 self.assertEqual(str(p), "HtmPixelization(3)")
146 self.assertEqual(str(p), repr(p))
147 self.assertEqual(p, eval(repr(p), {"HtmPixelization": HtmPixelization}))
149 def test_pickle(self):
150 a = HtmPixelization(20)
151 b = pickle.loads(pickle.dumps(a))
152 self.assertEqual(a, b)
154 @unittest.skipIf(not yaml, "YAML module can not be imported")
155 def test_yaml(self):
156 a = HtmPixelization(20)
157 b = yaml.safe_load(yaml.dump(a))
158 self.assertEqual(a, b)
161if __name__ == "__main__":
162 unittest.main()