Coverage for tests/test_Circle.py: 20%

122 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-06-02 10:45 +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 

24try: 

25 import yaml 

26except ImportError: 

27 yaml = None 

28 

29import math 

30import unittest 

31 

32import numpy as np 

33 

34from lsst.sphgeom import (Angle, CONTAINS, Circle, DISJOINT, Region, 

35 UnitVector3d) 

36 

37 

38class CircleTestCase(unittest.TestCase): 

39 

40 def setUp(self): 

41 np.random.seed(1) 

42 

43 def test_construction(self): 

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

45 self.assertTrue(Circle().isEmpty()) 

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

47 c = Circle(UnitVector3d.X()) 

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

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

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

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

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

53 self.assertTrue(c.isFull()) 

54 d = c.clone() 

55 self.assertEqual(c, d) 

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

57 e = Circle(d) 

58 self.assertEqual(d, e) 

59 

60 def test_comparison_operators(self): 

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

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

63 self.assertEqual(c, d) 

64 self.assertTrue(c.isFull()) 

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

66 

67 def test_center_and_dimensions(self): 

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

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

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

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

72 

73 def test_relationships(self): 

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

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

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

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

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

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

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

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

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

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

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

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

86 

87 def test_vectorized_contains(self): 

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

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

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

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

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

93 lon = np.arctan2(y, x) 

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

95 c2 = b.contains(lon, lat) 

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

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

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

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

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

101 # test with non-contiguous memory 

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

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

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

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

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

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

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

109 

110 def test_expanding_and_clipping(self): 

111 a = Circle.empty() 

112 b = (a.expandedTo(UnitVector3d.X()) 

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

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

115 .clippedTo(UnitVector3d.Y())) 

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), 

155 'Circle(UnitVector3d(0.0, 0.0, 1.0), Angle(1.0))') 

156 self.assertEqual(c, eval(repr(c), dict( 

157 Angle=Angle, Circle=Circle, UnitVector3d=UnitVector3d))) 

158 

159 def test_pickle(self): 

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

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

162 self.assertEqual(a, b) 

163 

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

165 def test_yaml(self): 

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

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

168 self.assertEqual(a, b) 

169 

170 

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

172 unittest.main()