Coverage for tests/test_Ellipse.py: 20%

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

35 

36 

37class EllipseTestCase(unittest.TestCase): 

38 """Test Ellipse.""" 

39 

40 def test_construction(self): 

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

42 self.assertTrue(Ellipse().isEmpty()) 

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

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

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

46 self.assertEqual(e, f) 

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

48 self.assertTrue(e.isCircle()) 

49 self.assertTrue(e.isGreatCircle()) 

50 g = Ellipse(e) 

51 h = e.clone() 

52 self.assertEqual(e, g) 

53 self.assertEqual(g, h) 

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

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

56 

57 def test_comparison_operators(self): 

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

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

60 self.assertEqual(e, e) 

61 self.assertNotEqual(e, f) 

62 

63 def test_center_and_dimensions(self): 

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

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

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

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

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

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

70 

71 def test_relationships(self): 

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

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

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

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

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

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

78 

79 def test_vectorized_contains(self): 

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

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

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

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

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

85 lon = np.arctan2(y, x) 

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

87 c2 = e.contains(lon, lat) 

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

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

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

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

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

93 # test with non-contiguous memory 

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

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

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

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

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

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

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

101 

102 def test_complement(self): 

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

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

105 self.assertEqual(e, f) 

106 

107 def test_codec(self): 

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

109 s = e.encode() 

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

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

112 

113 def test_string(self): 

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

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

116 self.assertEqual( 

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

118 ) 

119 self.assertEqual(c, eval(repr(c), {"Angle": Angle, "Ellipse": Ellipse, "UnitVector3d": UnitVector3d})) 

120 

121 def test_pickle(self): 

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

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

124 self.assertEqual(a, b) 

125 

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

127 def test_yaml(self): 

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

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

130 self.assertEqual(a, b) 

131 

132 

133if __name__ == "__main__": 

134 unittest.main()