Coverage for tests/test_Box.py: 17%

117 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 math 

31import unittest 

32 

33import numpy as np 

34from lsst.sphgeom import ( 

35 CONTAINS, 

36 DISJOINT, 

37 Angle, 

38 AngleInterval, 

39 Box, 

40 LonLat, 

41 NormalizedAngle, 

42 NormalizedAngleInterval, 

43 Region, 

44 UnitVector3d, 

45) 

46 

47 

48class BoxTestCase(unittest.TestCase): 

49 """Test Box.""" 

50 

51 def setUp(self): 

52 np.random.seed(1) 

53 

54 def test_construction(self): 

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

56 self.assertTrue(b.isFull()) 

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

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

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

60 b = Box( 

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

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

63 ) 

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

65 d = c.clone() 

66 self.assertEqual(a, b) 

67 self.assertEqual(b, c) 

68 self.assertEqual(c, d) 

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

70 b = Box() 

71 self.assertTrue(b.isEmpty()) 

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

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

74 

75 def test_comparison_operators(self): 

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

77 self.assertEqual( 

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

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

80 ) 

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

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

83 

84 def test_center_and_dimensions(self): 

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

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

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

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

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

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

91 

92 def test_relationships(self): 

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

94 p = LonLat.fromDegrees(135, 10) 

95 self.assertTrue(p in b1) 

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

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

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

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

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

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

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

103 self.assertTrue(u in b3) 

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

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

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

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

108 self.assertEqual(r, CONTAINS) 

109 r = b4.relate(b1) 

110 self.assertEqual(r, DISJOINT) 

111 

112 def test_vectorized_contains(self): 

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

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

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

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

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

118 lon = np.arctan2(y, x) 

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

120 c2 = b.contains(lon, lat) 

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

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

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

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

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

126 # test with non-contiguous memory 

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

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

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

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

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

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

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

134 

135 def test_expanding_and_clipping(self): 

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

137 b = ( 

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

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

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

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

142 ) 

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

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

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

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

147 self.assertEqual(a, b) 

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

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

150 self.assertTrue(a.isEmpty()) 

151 

152 def test_dilation_and_erosion(self): 

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

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

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

156 self.assertEqual(a, b) 

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

158 

159 def test_codec(self): 

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

161 s = b.encode() 

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

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

164 

165 def test_string(self): 

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

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

168 self.assertEqual( 

169 repr(b), 

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

171 ) 

172 self.assertEqual( 

173 b, 

174 eval( 

175 repr(b), 

176 { 

177 "AngleInterval": AngleInterval, 

178 "Box": Box, 

179 "NormalizedAngleInterval": NormalizedAngleInterval, 

180 }, 

181 ), 

182 ) 

183 

184 def test_pickle(self): 

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

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

187 self.assertEqual(a, b) 

188 

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

190 def test_yaml(self): 

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

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

193 self.assertEqual(a, b) 

194 

195 

196if __name__ == "__main__": 

197 unittest.main()