Coverage for tests/test_Ellipse.py: 22%

91 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, WITHIN, Angle, Circle, Ellipse, Region, UnitVector3d 

35 

36 

37class EllipseTestCase(unittest.TestCase): 

38 def test_construction(self): 

39 self.assertTrue(Ellipse.empty().isEmpty()) 

40 self.assertTrue(Ellipse().isEmpty()) 

41 self.assertTrue(Ellipse.full().isFull()) 

42 e = Ellipse(Circle(UnitVector3d.X(), Angle(math.pi / 2))) 

43 f = Ellipse(UnitVector3d.X(), Angle(math.pi / 2)) 

44 self.assertEqual(e, f) 

45 self.assertEqual(e.getAlpha(), e.getBeta()) 

46 self.assertTrue(e.isCircle()) 

47 self.assertTrue(e.isGreatCircle()) 

48 g = Ellipse(e) 

49 h = e.clone() 

50 self.assertEqual(e, g) 

51 self.assertEqual(g, h) 

52 self.assertNotEqual(id(e), id(g)) 

53 self.assertNotEqual(id(g), id(h)) 

54 

55 def test_comparison_operators(self): 

56 e = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3)) 

57 f = Ellipse(UnitVector3d.X(), Angle(math.pi / 3), Angle(math.pi / 6), Angle(0)) 

58 self.assertEqual(e, e) 

59 self.assertNotEqual(e, f) 

60 

61 def test_center_and_dimensions(self): 

62 e = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3)) 

63 self.assertAlmostEqual(e.getF1().dot(UnitVector3d.X()), 1.0) 

64 self.assertAlmostEqual(e.getF2().dot(UnitVector3d.Y()), 1.0) 

65 self.assertAlmostEqual(e.getAlpha(), Angle(2 * math.pi / 3)) 

66 f = Ellipse(UnitVector3d.X(), Angle(math.pi / 3), Angle(math.pi / 6), Angle(0)) 

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

68 

69 def test_relationships(self): 

70 e = Ellipse(UnitVector3d.X(), Angle(math.pi / 3), Angle(math.pi / 6), Angle(0)) 

71 self.assertTrue(e.contains(UnitVector3d.X())) 

72 self.assertTrue(UnitVector3d.X() in e) 

73 c = Circle(UnitVector3d.X(), Angle(math.pi / 2)) 

74 self.assertEqual(c.relate(e), CONTAINS) 

75 self.assertEqual(e.relate(c), WITHIN) 

76 

77 def test_vectorized_contains(self): 

78 e = Ellipse(UnitVector3d.X(), Angle(math.pi / 3), Angle(math.pi / 6), Angle(0)) 

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

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

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

82 c = e.contains(x, y, z) 

83 lon = np.arctan2(y, x) 

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

85 c2 = e.contains(lon, lat) 

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

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

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

89 self.assertEqual(c[i, j], e.contains(u)) 

90 self.assertEqual(c2[i, j], e.contains(u)) 

91 # test with non-contiguous memory 

92 c3 = e.contains(x[::2], y[::2], z[::2]) 

93 c4 = e.contains(lon[::2], lat[::2]) 

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

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

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

97 self.assertEqual(c3[i // 2, j], e.contains(u)) 

98 self.assertEqual(c4[i // 2, j], e.contains(u)) 

99 

100 def test_complement(self): 

101 e = Ellipse(UnitVector3d.X(), Angle(math.pi / 3), Angle(math.pi / 6), Angle(0)) 

102 f = e.complemented().complement() 

103 self.assertEqual(e, f) 

104 

105 def test_codec(self): 

106 e = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3)) 

107 s = e.encode() 

108 self.assertEqual(Ellipse.decode(s), e) 

109 self.assertEqual(Region.decode(s), e) 

110 

111 def test_string(self): 

112 c = Ellipse(UnitVector3d.Z(), Angle(1.0)) 

113 self.assertEqual(str(c), "Ellipse([0.0, 0.0, 1.0], [0.0, 0.0, 1.0], 1.0)") 

114 self.assertEqual( 

115 repr(c), "Ellipse(UnitVector3d(0.0, 0.0, 1.0), " "UnitVector3d(0.0, 0.0, 1.0), Angle(1.0))" 

116 ) 

117 self.assertEqual(c, eval(repr(c), dict(Angle=Angle, Ellipse=Ellipse, UnitVector3d=UnitVector3d))) 

118 

119 def test_pickle(self): 

120 a = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3)) 

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

122 self.assertEqual(a, b) 

123 

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

125 def test_yaml(self): 

126 a = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3)) 

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

128 self.assertEqual(a, b) 

129 

130 

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

132 unittest.main()