Coverage for tests/test_Circle.py: 20%

122 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-19 11:56 +0000

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 CONTAINS, DISJOINT, Angle, Circle, Region, UnitVector3d 

35 

36 

37class CircleTestCase(unittest.TestCase): 

38 def setUp(self): 

39 np.random.seed(1) 

40 

41 def test_construction(self): 

42 self.assertTrue(Circle.empty().isEmpty()) 

43 self.assertTrue(Circle().isEmpty()) 

44 self.assertTrue(Circle.full().isFull()) 

45 c = Circle(UnitVector3d.X()) 

46 self.assertEqual(c.getOpeningAngle(), Angle(0)) 

47 self.assertEqual(c.getSquaredChordLength(), 0) 

48 c = Circle(UnitVector3d.Z(), 2.0) 

49 self.assertTrue(c.contains(UnitVector3d.Z())) 

50 c = Circle(UnitVector3d.Z(), Angle(math.pi)) 

51 self.assertTrue(c.isFull()) 

52 d = c.clone() 

53 self.assertEqual(c, d) 

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

55 e = Circle(d) 

56 self.assertEqual(d, e) 

57 

58 def test_comparison_operators(self): 

59 c = Circle(UnitVector3d.X(), 4.0) 

60 d = Circle(UnitVector3d.Y(), 4.0) 

61 self.assertEqual(c, d) 

62 self.assertTrue(c.isFull()) 

63 self.assertNotEqual(c, Circle(UnitVector3d.Z())) 

64 

65 def test_center_and_dimensions(self): 

66 c = Circle(UnitVector3d.X(), 1) 

67 self.assertEqual(c.getCenter(), UnitVector3d.X()) 

68 self.assertEqual(c.getSquaredChordLength(), 1) 

69 self.assertAlmostEqual(c.getOpeningAngle().asRadians(), math.pi / 3) 

70 

71 def test_relationships(self): 

72 c = Circle(UnitVector3d.X(), Angle.fromDegrees(0.1)) 

73 d = Circle(UnitVector3d(1, 1, 1), Angle(math.pi / 2)) 

74 e = Circle(-UnitVector3d.X()) 

75 self.assertTrue(c.contains(UnitVector3d.X())) 

76 self.assertTrue(UnitVector3d.X() in c) 

77 self.assertTrue(d.contains(c)) 

78 self.assertTrue(c.isWithin(d)) 

79 self.assertTrue(c.intersects(d)) 

80 self.assertTrue(c.intersects(UnitVector3d.X())) 

81 self.assertTrue(e.isDisjointFrom(d)) 

82 self.assertEqual(d.relate(c), CONTAINS) 

83 self.assertEqual(e.relate(d), DISJOINT) 

84 

85 def test_vectorized_contains(self): 

86 b = Circle(UnitVector3d(*np.random.randn(3)), Angle(0.4 * math.pi)) 

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

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

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

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

91 lon = np.arctan2(y, x) 

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

93 c2 = b.contains(lon, lat) 

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

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

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

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

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

99 # test with non-contiguous memory 

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

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

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

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

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

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

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

107 

108 def test_expanding_and_clipping(self): 

109 a = Circle.empty() 

110 b = ( 

111 a.expandedTo(UnitVector3d.X()) 

112 .expandedTo(Circle(UnitVector3d.Y(), 1)) 

113 .clippedTo(Circle(UnitVector3d(1, 1, 0), 1)) 

114 .clippedTo(UnitVector3d.Y()) 

115 ) 

116 a.expandTo(UnitVector3d.X()) 

117 a.expandTo(Circle(UnitVector3d.Y(), 1)) 

118 a.clipTo(Circle(UnitVector3d(1, 1, 0), 1)) 

119 a.clipTo(UnitVector3d.Y()) 

120 self.assertEqual(a, b) 

121 self.assertEqual(a, Circle(UnitVector3d.Y())) 

122 a.clipTo(UnitVector3d.Z()) 

123 self.assertTrue(a.isEmpty()) 

124 

125 def test_dilation_and_erosion(self): 

126 a = Angle(math.pi / 2) 

127 c = Circle(UnitVector3d.X()) 

128 d = c.dilatedBy(a).erodedBy(a) 

129 c.dilateBy(a).erodeBy(a) 

130 self.assertEqual(c, d) 

131 self.assertEqual(c, Circle(UnitVector3d.X())) 

132 

133 def test_complement(self): 

134 c = Circle(UnitVector3d.X(), 2.0) 

135 d = c.complemented() 

136 c.complement() 

137 self.assertEqual(c, d) 

138 self.assertEqual(c.getCenter(), -UnitVector3d.X()) 

139 self.assertEqual(c.getSquaredChordLength(), 2.0) 

140 

141 def test_area(self): 

142 c = Circle(UnitVector3d(1, 1, 1), 2.0) 

143 self.assertAlmostEqual(c.getArea(), 2 * math.pi) 

144 

145 def test_codec(self): 

146 c = Circle(UnitVector3d.Y(), 1.0) 

147 s = c.encode() 

148 self.assertEqual(Circle.decode(s), c) 

149 self.assertEqual(Region.decode(s), c) 

150 

151 def test_string(self): 

152 c = Circle(UnitVector3d.Z(), Angle(1.0)) 

153 self.assertEqual(str(c), "Circle([0.0, 0.0, 1.0], 1.0)") 

154 self.assertEqual(repr(c), "Circle(UnitVector3d(0.0, 0.0, 1.0), Angle(1.0))") 

155 self.assertEqual(c, eval(repr(c), dict(Angle=Angle, Circle=Circle, UnitVector3d=UnitVector3d))) 

156 

157 def test_pickle(self): 

158 a = Circle(UnitVector3d(1, -1, 1), 1.0) 

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

160 self.assertEqual(a, b) 

161 

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

163 def test_yaml(self): 

164 a = Circle(UnitVector3d(1, -1, 1), 1.0) 

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

166 self.assertEqual(a, b) 

167 

168 

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

170 unittest.main()