Coverage for tests/test_Box.py: 18%

119 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-15 02:22 -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 def setUp(self): 

50 np.random.seed(1) 

51 

52 def test_construction(self): 

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

54 self.assertTrue(b.isFull()) 

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

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

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

58 b = Box( 

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

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

61 ) 

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

63 d = c.clone() 

64 self.assertEqual(a, b) 

65 self.assertEqual(b, c) 

66 self.assertEqual(c, d) 

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

68 b = Box() 

69 self.assertTrue(b.isEmpty()) 

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

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

72 

73 def test_comparison_operators(self): 

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

75 self.assertEqual( 

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

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

78 ) 

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

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

81 

82 def test_center_and_dimensions(self): 

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

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

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

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

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

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

89 

90 def test_relationships(self): 

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

92 p = LonLat.fromDegrees(135, 10) 

93 self.assertTrue(p in b1) 

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

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

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

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

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

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

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

101 self.assertTrue(u in b3) 

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

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

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

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

106 self.assertEqual(r, CONTAINS) 

107 r = b4.relate(b1) 

108 self.assertEqual(r, DISJOINT) 

109 

110 def test_vectorized_contains(self): 

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

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

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

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

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

116 lon = np.arctan2(y, x) 

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

118 c2 = b.contains(lon, lat) 

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

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

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

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

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

124 # test with non-contiguous memory 

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

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

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

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

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

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

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

132 

133 def test_expanding_and_clipping(self): 

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

135 b = ( 

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

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

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

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

140 ) 

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

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

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

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

145 self.assertEqual(a, b) 

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

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

148 self.assertTrue(a.isEmpty()) 

149 

150 def test_dilation_and_erosion(self): 

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

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

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

154 self.assertEqual(a, b) 

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

156 

157 def test_codec(self): 

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

159 s = b.encode() 

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

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

162 

163 def test_string(self): 

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

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

166 self.assertEqual( 

167 repr(b), 

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

169 ) 

170 self.assertEqual( 

171 b, 

172 eval( 

173 repr(b), 

174 dict(AngleInterval=AngleInterval, Box=Box, NormalizedAngleInterval=NormalizedAngleInterval), 

175 ), 

176 ) 

177 

178 def test_pickle(self): 

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

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

181 self.assertEqual(a, b) 

182 

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

184 def test_yaml(self): 

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

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

187 self.assertEqual(a, b) 

188 

189 

190if __name__ == "__main__": 190 ↛ 191line 190 didn't jump to line 191, because the condition on line 190 was never true

191 unittest.main()