Coverage for tests/test_HtmPixelization.py: 20%

65 statements  

« 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# 

22 

23import pickle 

24 

25try: 

26 import yaml 

27except ImportError: 

28 yaml = None 

29 

30import unittest 

31 

32from lsst.sphgeom import Angle, Circle, ConvexPolygon, HtmPixelization, RangeSet, UnitVector3d 

33 

34 

35class HtmPixelizationTestCase(unittest.TestCase): 

36 """Test HTM pixels.""" 

37 

38 def test_construction(self): 

39 with self.assertRaises(ValueError): 

40 HtmPixelization(-1) 

41 with self.assertRaises(ValueError): 

42 HtmPixelization(HtmPixelization.MAX_LEVEL + 1) 

43 h1 = HtmPixelization(0) 

44 self.assertEqual(h1.getLevel(), 0) 

45 h2 = HtmPixelization(1) 

46 h3 = HtmPixelization(h2) 

47 self.assertNotEqual(h1, h2) 

48 self.assertEqual(h2, h3) 

49 

50 def test_indexing(self): 

51 h = HtmPixelization(1) 

52 self.assertEqual(h.index(UnitVector3d(1, 1, 1)), 63) 

53 

54 def test_pixel(self): 

55 h = HtmPixelization(1) 

56 self.assertIsInstance(h.pixel(10), ConvexPolygon) 

57 

58 def test_level(self): 

59 for index in (0, 16 * 4**HtmPixelization.MAX_LEVEL): 

60 self.assertEqual(HtmPixelization.level(index), -1) 

61 for level in range(HtmPixelization.MAX_LEVEL + 1): 

62 for root in range(8, 16): 

63 self.assertEqual(HtmPixelization.level(root * 4**level), level) 

64 

65 def test_envelope_and_interior(self): 

66 pixelization = HtmPixelization(3) 

67 c = Circle(UnitVector3d(1, 1, 1), Angle.fromDegrees(0.1)) 

68 rs = pixelization.envelope(c) 

69 self.assertTrue(rs == RangeSet(0x3FF)) 

70 rs = pixelization.envelope(c, 1) 

71 self.assertTrue(rs == RangeSet(0x3FF)) 

72 self.assertTrue(rs.isWithin(pixelization.universe())) 

73 rs = pixelization.interior(c) 

74 self.assertTrue(rs.empty()) 

75 

76 def test_index_to_string(self): 

77 strings = ["S0", "S1", "S2", "S3", "N0", "N1", "N2", "N3"] 

78 for i in range(8, 16): 

79 s0 = strings[i - 8] 

80 self.assertEqual(HtmPixelization.asString(i), s0) 

81 self.assertEqual(HtmPixelization(0).toString(i), s0) 

82 for j in range(4): 

83 s1 = s0 + str(j) 

84 self.assertEqual(HtmPixelization.asString(i * 4 + j), s1) 

85 self.assertEqual(HtmPixelization(1).asString(i * 4 + j), s1) 

86 

87 def test_string(self): 

88 p = HtmPixelization(3) 

89 self.assertEqual(str(p), "HtmPixelization(3)") 

90 self.assertEqual(str(p), repr(p)) 

91 self.assertEqual(p, eval(repr(p), {"HtmPixelization": HtmPixelization})) 

92 

93 def test_pickle(self): 

94 a = HtmPixelization(20) 

95 b = pickle.loads(pickle.dumps(a)) 

96 self.assertEqual(a, b) 

97 

98 @unittest.skipIf(not yaml, "YAML module can not be imported") 

99 def test_yaml(self): 

100 a = HtmPixelization(20) 

101 b = yaml.safe_load(yaml.dump(a)) 

102 self.assertEqual(a, b) 

103 

104 

105if __name__ == "__main__": 

106 unittest.main()