Coverage for tests/test_Ellipse.py: 21%

89 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-02 03:12 -0700

1# This file is part of sphgeom. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28import pickle 

29 

30try: 

31 import yaml 

32except ImportError: 

33 yaml = None 

34 

35import math 

36import unittest 

37 

38import numpy as np 

39from lsst.sphgeom import CONTAINS, WITHIN, Angle, Circle, Ellipse, Region, UnitVector3d 

40 

41 

42class EllipseTestCase(unittest.TestCase): 

43 """Test Ellipse.""" 

44 

45 def test_construction(self): 

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

47 self.assertTrue(Ellipse().isEmpty()) 

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

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

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

51 self.assertEqual(e, f) 

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

53 self.assertTrue(e.isCircle()) 

54 self.assertTrue(e.isGreatCircle()) 

55 g = Ellipse(e) 

56 h = e.clone() 

57 self.assertEqual(e, g) 

58 self.assertEqual(g, h) 

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

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

61 

62 def test_comparison_operators(self): 

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

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

65 self.assertEqual(e, e) 

66 self.assertNotEqual(e, f) 

67 

68 def test_center_and_dimensions(self): 

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

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

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

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

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

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

75 

76 def test_relationships(self): 

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

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

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

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

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

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

83 

84 def test_vectorized_contains(self): 

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

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

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

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

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

90 lon = np.arctan2(y, x) 

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

92 c2 = e.contains(lon, lat) 

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

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

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

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

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

98 # test with non-contiguous memory 

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

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

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

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

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

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

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

106 

107 def test_complement(self): 

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

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

110 self.assertEqual(e, f) 

111 

112 def test_codec(self): 

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

114 s = e.encode() 

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

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

117 

118 def test_string(self): 

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

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

121 self.assertEqual( 

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

123 ) 

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

125 

126 def test_pickle(self): 

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

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

129 self.assertEqual(a, b) 

130 

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

132 def test_yaml(self): 

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

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

135 self.assertEqual(a, b) 

136 

137 

138if __name__ == "__main__": 

139 unittest.main()