Coverage for tests/test_HtmPixelization.py: 22%
65 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:12 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:12 -0700
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 Angle, Circle, ConvexPolygon, HtmPixelization, RangeSet, UnitVector3d
40class HtmPixelizationTestCase(unittest.TestCase):
41 """Test HTM pixels."""
43 def test_construction(self):
44 with self.assertRaises(ValueError):
45 HtmPixelization(-1)
46 with self.assertRaises(ValueError):
47 HtmPixelization(HtmPixelization.MAX_LEVEL + 1)
48 h1 = HtmPixelization(0)
49 self.assertEqual(h1.getLevel(), 0)
50 h2 = HtmPixelization(1)
51 h3 = HtmPixelization(h2)
52 self.assertNotEqual(h1, h2)
53 self.assertEqual(h2, h3)
55 def test_indexing(self):
56 h = HtmPixelization(1)
57 self.assertEqual(h.index(UnitVector3d(1, 1, 1)), 63)
59 def test_pixel(self):
60 h = HtmPixelization(1)
61 self.assertIsInstance(h.pixel(10), ConvexPolygon)
63 def test_level(self):
64 for index in (0, 16 * 4**HtmPixelization.MAX_LEVEL):
65 self.assertEqual(HtmPixelization.level(index), -1)
66 for level in range(HtmPixelization.MAX_LEVEL + 1):
67 for root in range(8, 16):
68 self.assertEqual(HtmPixelization.level(root * 4**level), level)
70 def test_envelope_and_interior(self):
71 pixelization = HtmPixelization(3)
72 c = Circle(UnitVector3d(1, 1, 1), Angle.fromDegrees(0.1))
73 rs = pixelization.envelope(c)
74 self.assertTrue(rs == RangeSet(0x3FF))
75 rs = pixelization.envelope(c, 1)
76 self.assertTrue(rs == RangeSet(0x3FF))
77 self.assertTrue(rs.isWithin(pixelization.universe()))
78 rs = pixelization.interior(c)
79 self.assertTrue(rs.empty())
81 def test_index_to_string(self):
82 strings = ["S0", "S1", "S2", "S3", "N0", "N1", "N2", "N3"]
83 for i in range(8, 16):
84 s0 = strings[i - 8]
85 self.assertEqual(HtmPixelization.asString(i), s0)
86 self.assertEqual(HtmPixelization(0).toString(i), s0)
87 for j in range(4):
88 s1 = s0 + str(j)
89 self.assertEqual(HtmPixelization.asString(i * 4 + j), s1)
90 self.assertEqual(HtmPixelization(1).asString(i * 4 + j), s1)
92 def test_string(self):
93 p = HtmPixelization(3)
94 self.assertEqual(str(p), "HtmPixelization(3)")
95 self.assertEqual(str(p), repr(p))
96 self.assertEqual(p, eval(repr(p), {"HtmPixelization": HtmPixelization}))
98 def test_pickle(self):
99 a = HtmPixelization(20)
100 b = pickle.loads(pickle.dumps(a))
101 self.assertEqual(a, b)
103 @unittest.skipIf(not yaml, "YAML module can not be imported")
104 def test_yaml(self):
105 a = HtmPixelization(20)
106 b = yaml.safe_load(yaml.dump(a))
107 self.assertEqual(a, b)
110if __name__ == "__main__":
111 unittest.main()