Coverage for tests/test_Box.py: 18%

117 statements  

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

27 

28import pickle 

29 

30try: 

31 import yaml 

32except ImportError: 

33 yaml = None 

34 

35import math 

36import unittest 

37 

38import numpy as np 

39from lsst.sphgeom import ( 

40 CONTAINS, 

41 DISJOINT, 

42 Angle, 

43 AngleInterval, 

44 Box, 

45 LonLat, 

46 NormalizedAngle, 

47 NormalizedAngleInterval, 

48 Region, 

49 UnitVector3d, 

50) 

51 

52 

53class BoxTestCase(unittest.TestCase): 

54 """Test Box.""" 

55 

56 def setUp(self): 

57 np.random.seed(1) 

58 

59 def test_construction(self): 

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

61 self.assertTrue(b.isFull()) 

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

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

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

65 b = Box( 

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

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

68 ) 

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

70 d = c.clone() 

71 self.assertEqual(a, b) 

72 self.assertEqual(b, c) 

73 self.assertEqual(c, d) 

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

75 b = Box() 

76 self.assertTrue(b.isEmpty()) 

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

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

79 

80 def test_comparison_operators(self): 

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

82 self.assertEqual( 

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

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

85 ) 

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

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

88 

89 def test_center_and_dimensions(self): 

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

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

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

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

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

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

96 

97 def test_relationships(self): 

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

99 p = LonLat.fromDegrees(135, 10) 

100 self.assertTrue(p in b1) 

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

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

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

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

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

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

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

108 self.assertTrue(u in b3) 

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

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

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

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

113 self.assertEqual(r, CONTAINS) 

114 r = b4.relate(b1) 

115 self.assertEqual(r, DISJOINT) 

116 

117 def test_vectorized_contains(self): 

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

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

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

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

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

123 lon = np.arctan2(y, x) 

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

125 c2 = b.contains(lon, lat) 

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

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

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

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

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

131 # test with non-contiguous memory 

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

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

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

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

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

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

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

139 

140 def test_expanding_and_clipping(self): 

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

142 b = ( 

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

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

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

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

147 ) 

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

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

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

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

152 self.assertEqual(a, b) 

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

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

155 self.assertTrue(a.isEmpty()) 

156 

157 def test_dilation_and_erosion(self): 

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

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

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

161 self.assertEqual(a, b) 

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

163 

164 def test_codec(self): 

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

166 s = b.encode() 

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

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

169 

170 def test_string(self): 

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

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

173 self.assertEqual( 

174 repr(b), 

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

176 ) 

177 self.assertEqual( 

178 b, 

179 eval( 

180 repr(b), 

181 { 

182 "AngleInterval": AngleInterval, 

183 "Box": Box, 

184 "NormalizedAngleInterval": NormalizedAngleInterval, 

185 }, 

186 ), 

187 ) 

188 

189 def test_pickle(self): 

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

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

192 self.assertEqual(a, b) 

193 

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

195 def test_yaml(self): 

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

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

198 self.assertEqual(a, b) 

199 

200 

201if __name__ == "__main__": 

202 unittest.main()