Coverage for tests / test_Box.py: 17%

119 statements  

« 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/>. 

27 

28import pickle 

29 

30try: 

31 import yaml 

32except ImportError: 

33 yaml = None 

34 

35import math 

36import unittest 

37 

38import numpy as np 

39 

40from lsst.sphgeom import ( 

41 CONTAINS, 

42 DISJOINT, 

43 Angle, 

44 AngleInterval, 

45 Box, 

46 LonLat, 

47 NormalizedAngle, 

48 NormalizedAngleInterval, 

49 Region, 

50 UnitVector3d, 

51) 

52 

53 

54class BoxTestCase(unittest.TestCase): 

55 """Test Box.""" 

56 

57 def setUp(self): 

58 np.random.seed(1) 

59 

60 def test_construction(self): 

61 b = Box(Box.allLongitudes(), Box.allLatitudes()) 

62 self.assertTrue(b.isFull()) 

63 b = Box.fromDegrees(-90, -45, 90, 45) 

64 self.assertEqual(b, Box(b.getLon(), b.getLat())) 

65 a = Box.fromRadians(-0.5 * math.pi, -0.25 * math.pi, 0.5 * math.pi, 0.25 * math.pi) 

66 b = Box( 

67 LonLat.fromRadians(-0.5 * math.pi, -0.25 * math.pi), 

68 LonLat.fromRadians(0.5 * math.pi, 0.25 * math.pi), 

69 ) 

70 c = Box(LonLat.fromRadians(0, 0), Angle(0.5 * math.pi), Angle(0.25 * math.pi)) 

71 d = c.clone() 

72 self.assertEqual(a, b) 

73 self.assertEqual(b, c) 

74 self.assertEqual(c, d) 

75 self.assertNotEqual(id(c), id(d)) 

76 b = Box() 

77 self.assertTrue(b.isEmpty()) 

78 self.assertTrue(Box.empty().isEmpty()) 

79 self.assertTrue(Box.full().isFull()) 

80 

81 def test_comparison_operators(self): 

82 self.assertEqual(Box(LonLat.fromDegrees(45, 45)), LonLat.fromDegrees(45, 45)) 

83 self.assertEqual( 

84 Box.fromDegrees(90, -45, 180, 45), 

85 Box(NormalizedAngleInterval.fromDegrees(90, 180), AngleInterval.fromDegrees(-45, 45)), 

86 ) 

87 self.assertNotEqual(Box(LonLat.fromDegrees(45, 45)), LonLat.fromDegrees(45, 90)) 

88 self.assertNotEqual(Box.fromDegrees(90, -45, 180, 45), Box.fromDegrees(90, -45, 180, 90)) 

89 

90 def test_center_and_dimensions(self): 

91 b = Box.fromDegrees(-90, -45, 90, 45) 

92 self.assertEqual(b.getCenter(), LonLat.fromDegrees(0, 0)) 

93 self.assertEqual(b.getWidth(), Angle.fromDegrees(180)) 

94 self.assertEqual(b.getHeight(), Angle.fromDegrees(90)) 

95 self.assertEqual(b.getLon().getA(), NormalizedAngle.fromDegrees(-90)) 

96 self.assertEqual(b.getLat().getB(), Angle.fromDegrees(45)) 

97 

98 def test_relationships(self): 

99 b1 = Box.fromDegrees(90, 0, 180, 45) 

100 p = LonLat.fromDegrees(135, 10) 

101 self.assertTrue(p in b1) 

102 self.assertTrue(b1.contains(p)) 

103 b2 = Box.fromDegrees(135, 15, 135, 30) 

104 self.assertTrue(b1.contains(b2)) 

105 self.assertTrue(b2.isWithin(b1)) 

106 b3 = Box.fromDegrees(0, -45, 90, 0) 

107 u = UnitVector3d(1, 1, -1) 

108 self.assertTrue(b1.intersects(b3)) 

109 self.assertTrue(u in b3) 

110 self.assertTrue(b3.contains(u)) 

111 b4 = Box.fromDegrees(200, 10, 300, 20) 

112 self.assertTrue(b1.isDisjointFrom(b4)) 

113 self.assertEqual(b1.overlaps(b4), False) 

114 r = b1.relate(LonLat.fromDegrees(135, 10)) 

115 self.assertEqual(r, CONTAINS) 

116 r = b4.relate(b1) 

117 self.assertEqual(r, DISJOINT) 

118 self.assertEqual(b4.overlaps(b1), False) 

119 

120 def test_vectorized_contains(self): 

121 b = Box.fromDegrees(200, 10, 300, 20) 

122 x = np.random.rand(5, 3) 

123 y = np.random.rand(5, 3) 

124 z = np.random.rand(5, 3) 

125 c = b.contains(x, y, z) 

126 lon = np.arctan2(y, x) 

127 lat = np.arctan2(z, np.hypot(x, y)) 

128 c2 = b.contains(lon, lat) 

129 for i in range(x.shape[0]): 

130 for j in range(x.shape[1]): 

131 u = UnitVector3d(x[i, j], y[i, j], z[i, j]) 

132 self.assertEqual(c[i, j], b.contains(u)) 

133 self.assertEqual(c2[i, j], b.contains(u)) 

134 # test with non-contiguous memory 

135 c3 = b.contains(x[::2], y[::2], z[::2]) 

136 c4 = b.contains(lon[::2], lat[::2]) 

137 for i in range(x.shape[0], 2): 

138 for j in range(x.shape[1]): 

139 u = UnitVector3d(x[i, j], y[i, j], z[i, j]) 

140 self.assertEqual(c3[i // 2, j], b.contains(u)) 

141 self.assertEqual(c4[i // 2, j], b.contains(u)) 

142 

143 def test_expanding_and_clipping(self): 

144 a = Box.fromDegrees(0, 0, 10, 10) 

145 b = ( 

146 a.expandedTo(LonLat.fromDegrees(20, 20)) 

147 .expandedTo(Box.fromDegrees(0, 0, 30, 10)) 

148 .clippedTo(Box.fromDegrees(10, 10, 15, 15)) 

149 .clippedTo(LonLat.fromDegrees(11, 11)) 

150 ) 

151 a.expandTo(LonLat.fromDegrees(20, 20)) 

152 a.expandTo(Box.fromDegrees(0, 0, 30, 10)) 

153 a.clipTo(Box.fromDegrees(10, 10, 15, 15)) 

154 a.clipTo(LonLat.fromDegrees(11, 11)) 

155 self.assertEqual(a, b) 

156 self.assertEqual(a, LonLat.fromDegrees(11, 11)) 

157 a.clipTo(LonLat.fromDegrees(0, 0)) 

158 self.assertTrue(a.isEmpty()) 

159 

160 def test_dilation_and_erosion(self): 

161 a = Box.fromRadians(0.5, -0.5, 1.5, 0.5) 

162 b = a.dilatedBy(Angle(0.5), Angle(0.5)).erodedBy(Angle(1), Angle(1)) 

163 a.dilateBy(Angle(0.5), Angle(0.5)).erodeBy(Angle(1), Angle(1)) 

164 self.assertEqual(a, b) 

165 self.assertEqual(a, LonLat.fromRadians(1, 0)) 

166 

167 def test_codec(self): 

168 b = Box.fromRadians(0, 0, 1, 1) 

169 s = b.encode() 

170 self.assertEqual(Box.decode(s), b) 

171 self.assertEqual(Region.decode(s), b) 

172 

173 def test_string(self): 

174 b = Box.fromRadians(0, 0, 1, 1) 

175 self.assertEqual(str(b), "Box([0.0, 1.0], [0.0, 1.0])") 

176 self.assertEqual( 

177 repr(b), 

178 "Box(NormalizedAngleInterval.fromRadians(0.0, 1.0), AngleInterval.fromRadians(0.0, 1.0))", 

179 ) 

180 self.assertEqual( 

181 b, 

182 eval( 

183 repr(b), 

184 { 

185 "AngleInterval": AngleInterval, 

186 "Box": Box, 

187 "NormalizedAngleInterval": NormalizedAngleInterval, 

188 }, 

189 ), 

190 ) 

191 

192 def test_pickle(self): 

193 a = Box.fromDegrees(0, 0, 10, 10) 

194 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL)) 

195 self.assertEqual(a, b) 

196 

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

198 def test_yaml(self): 

199 a = Box.fromDegrees(0, 0, 10, 10) 

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

201 self.assertEqual(a, b) 

202 

203 

204if __name__ == "__main__": 

205 unittest.main()