Coverage for tests/test_Ellipse.py: 22%
91 statements
« prev ^ index » next coverage.py v6.4, created at 2022-06-02 10:45 +0000
« 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#
23import pickle
24try:
25 import yaml
26except ImportError:
27 yaml = None
29import math
30import unittest
32import numpy as np
34from lsst.sphgeom import (Angle, CONTAINS, Circle, Ellipse, Region,
35 UnitVector3d, WITHIN)
38class EllipseTestCase(unittest.TestCase):
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))
57 def test_comparison_operators(self):
58 e = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3))
59 f = Ellipse(UnitVector3d.X(),
60 Angle(math.pi / 3), Angle(math.pi / 6), Angle(0))
61 self.assertEqual(e, e)
62 self.assertNotEqual(e, f)
64 def test_center_and_dimensions(self):
65 e = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3))
66 self.assertAlmostEqual(e.getF1().dot(UnitVector3d.X()), 1.0)
67 self.assertAlmostEqual(e.getF2().dot(UnitVector3d.Y()), 1.0)
68 self.assertAlmostEqual(e.getAlpha(), Angle(2 * math.pi / 3))
69 f = Ellipse(UnitVector3d.X(),
70 Angle(math.pi / 3), Angle(math.pi / 6), Angle(0))
71 self.assertEqual(f.getCenter(), UnitVector3d.X())
73 def test_relationships(self):
74 e = Ellipse(UnitVector3d.X(),
75 Angle(math.pi / 3), Angle(math.pi / 6), Angle(0))
76 self.assertTrue(e.contains(UnitVector3d.X()))
77 self.assertTrue(UnitVector3d.X() in e)
78 c = Circle(UnitVector3d.X(), Angle(math.pi / 2))
79 self.assertEqual(c.relate(e), CONTAINS)
80 self.assertEqual(e.relate(c), WITHIN)
82 def test_vectorized_contains(self):
83 e = Ellipse(UnitVector3d.X(),
84 Angle(math.pi / 3), Angle(math.pi / 6), Angle(0))
85 x = np.random.rand(5, 3)
86 y = np.random.rand(5, 3)
87 z = np.random.rand(5, 3)
88 c = e.contains(x, y, z)
89 lon = np.arctan2(y, x)
90 lat = np.arctan2(z, np.hypot(x, y))
91 c2 = e.contains(lon, lat)
92 for i in range(x.shape[0]):
93 for j in range(x.shape[1]):
94 u = UnitVector3d(x[i, j], y[i, j], z[i, j])
95 self.assertEqual(c[i, j], e.contains(u))
96 self.assertEqual(c2[i, j], e.contains(u))
97 # test with non-contiguous memory
98 c3 = e.contains(x[::2], y[::2], z[::2])
99 c4 = e.contains(lon[::2], lat[::2])
100 for i in range(x.shape[0], 2):
101 for j in range(x.shape[1]):
102 u = UnitVector3d(x[i, j], y[i, j], z[i, j])
103 self.assertEqual(c3[i//2, j], e.contains(u))
104 self.assertEqual(c4[i//2, j], e.contains(u))
106 def test_complement(self):
107 e = Ellipse(UnitVector3d.X(),
108 Angle(math.pi / 3), Angle(math.pi / 6), Angle(0))
109 f = e.complemented().complement()
110 self.assertEqual(e, f)
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)
118 def test_string(self):
119 c = Ellipse(UnitVector3d.Z(), Angle(1.0))
120 self.assertEqual(str(c),
121 'Ellipse([0.0, 0.0, 1.0], [0.0, 0.0, 1.0], 1.0)')
122 self.assertEqual(repr(c),
123 'Ellipse(UnitVector3d(0.0, 0.0, 1.0), '
124 'UnitVector3d(0.0, 0.0, 1.0), Angle(1.0))')
125 self.assertEqual(c, eval(repr(c), dict(
126 Angle=Angle, Ellipse=Ellipse, UnitVector3d=UnitVector3d)))
128 def test_pickle(self):
129 a = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3))
130 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL))
131 self.assertEqual(a, b)
133 @unittest.skipIf(not yaml, "YAML module can not be imported")
134 def test_yaml(self):
135 a = Ellipse(UnitVector3d.X(), UnitVector3d.Y(), Angle(2 * math.pi / 3))
136 b = yaml.safe_load(yaml.dump(a))
137 self.assertEqual(a, b)
140if __name__ == '__main__': 140 ↛ 141line 140 didn't jump to line 141, because the condition on line 140 was never true
141 unittest.main()