Coverage for tests / test_Ellipse.py: 19%
91 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 08:41 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 08:41 +0000
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/>.
28import pickle
30try:
31 import yaml
32except ImportError:
33 yaml = None
35import math
36import unittest
38import numpy as np
40from lsst.sphgeom import CONTAINS, WITHIN, Angle, Circle, Ellipse, Region, UnitVector3d
43class EllipseTestCase(unittest.TestCase):
44 """Test Ellipse."""
46 def test_construction(self):
47 self.assertTrue(Ellipse.empty().isEmpty())
48 self.assertTrue(Ellipse().isEmpty())
49 self.assertTrue(Ellipse.full().isFull())
50 e = Ellipse(Circle(UnitVector3d.X(), Angle(math.pi / 2)))
51 f = Ellipse(UnitVector3d.X(), Angle(math.pi / 2))
52 self.assertEqual(e, f)
53 self.assertEqual(e.getAlpha(), e.getBeta())
54 self.assertTrue(e.isCircle())
55 self.assertTrue(e.isGreatCircle())
56 g = Ellipse(e)
57 h = e.clone()
58 self.assertEqual(e, g)
59 self.assertEqual(g, h)
60 self.assertNotEqual(id(e), id(g))
61 self.assertNotEqual(id(g), id(h))
63 def test_comparison_operators(self):
64 e = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3))
65 f = Ellipse(UnitVector3d.X(), Angle(math.pi / 3), Angle(math.pi / 6), Angle(0))
66 self.assertEqual(e, e)
67 self.assertNotEqual(e, f)
69 def test_center_and_dimensions(self):
70 e = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3))
71 self.assertAlmostEqual(e.getF1().dot(UnitVector3d.X()), 1.0)
72 self.assertAlmostEqual(e.getF2().dot(UnitVector3d.Y()), 1.0)
73 self.assertAlmostEqual(e.getAlpha(), Angle(2 * math.pi / 3))
74 f = Ellipse(UnitVector3d.X(), Angle(math.pi / 3), Angle(math.pi / 6), Angle(0))
75 self.assertEqual(f.getCenter(), UnitVector3d.X())
77 def test_relationships(self):
78 e = Ellipse(UnitVector3d.X(), Angle(math.pi / 3), Angle(math.pi / 6), Angle(0))
79 self.assertTrue(e.contains(UnitVector3d.X()))
80 self.assertTrue(UnitVector3d.X() in e)
81 c = Circle(UnitVector3d.X(), Angle(math.pi / 2))
82 self.assertEqual(c.relate(e), CONTAINS)
83 self.assertEqual(c.overlaps(e), True)
84 self.assertEqual(e.relate(c), WITHIN)
85 self.assertEqual(e.overlaps(c), True)
87 def test_vectorized_contains(self):
88 e = Ellipse(UnitVector3d.X(), Angle(math.pi / 3), Angle(math.pi / 6), Angle(0))
89 x = np.random.rand(5, 3)
90 y = np.random.rand(5, 3)
91 z = np.random.rand(5, 3)
92 c = e.contains(x, y, z)
93 lon = np.arctan2(y, x)
94 lat = np.arctan2(z, np.hypot(x, y))
95 c2 = e.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], e.contains(u))
100 self.assertEqual(c2[i, j], e.contains(u))
101 # test with non-contiguous memory
102 c3 = e.contains(x[::2], y[::2], z[::2])
103 c4 = e.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], e.contains(u))
108 self.assertEqual(c4[i // 2, j], e.contains(u))
110 def test_complement(self):
111 e = Ellipse(UnitVector3d.X(), Angle(math.pi / 3), Angle(math.pi / 6), Angle(0))
112 f = e.complemented().complement()
113 self.assertEqual(e, f)
115 def test_codec(self):
116 e = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3))
117 s = e.encode()
118 self.assertEqual(Ellipse.decode(s), e)
119 self.assertEqual(Region.decode(s), e)
121 def test_string(self):
122 c = Ellipse(UnitVector3d.Z(), Angle(1.0))
123 self.assertEqual(str(c), "Ellipse([0.0, 0.0, 1.0], [0.0, 0.0, 1.0], 1.0)")
124 self.assertEqual(
125 repr(c), "Ellipse(UnitVector3d(0.0, 0.0, 1.0), UnitVector3d(0.0, 0.0, 1.0), Angle(1.0))"
126 )
127 self.assertEqual(c, eval(repr(c), {"Angle": Angle, "Ellipse": Ellipse, "UnitVector3d": UnitVector3d}))
129 def test_pickle(self):
130 a = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3))
131 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL))
132 self.assertEqual(a, b)
134 @unittest.skipIf(not yaml, "YAML module can not be imported")
135 def test_yaml(self):
136 a = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3))
137 b = yaml.safe_load(yaml.dump(a))
138 self.assertEqual(a, b)
141if __name__ == "__main__":
142 unittest.main()