Coverage for tests / test_polygon.py: 30%
46 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-01 08:36 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-01 08:36 +0000
1# This file is part of lsst-images.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12from __future__ import annotations
14import unittest
16import numpy as np
18from lsst.images import Box, Polygon
20try:
21 import lsst.afw.geom # noqa: F401
23 have_legacy = True
24except ImportError:
25 have_legacy = False
28class SimplePolygonTestCase(unittest.TestCase):
29 """Tests for the SimplePolygon class."""
31 def setUp(self) -> None:
32 # A quadrilateral that's almost a box, so it's easy to reason about.
33 self.x_vertices = [32.0, 31.0, 50.0, 53.0]
34 self.y_vertices = [-5.0, 7.0, 7.2, -4.8]
35 self.polygon = Polygon(x_vertices=self.x_vertices, y_vertices=self.y_vertices)
37 def test_vertices(self) -> None:
38 """Test the vertices accessors."""
39 self.assertEqual(self.polygon.n_vertices, 4)
40 np.testing.assert_array_equal(self.polygon.x_vertices, np.asarray(self.x_vertices))
41 np.testing.assert_array_equal(self.polygon.y_vertices, np.asarray(self.y_vertices))
42 with self.assertRaises(ValueError):
43 self.polygon.x_vertices[0] = 0.0
44 with self.assertRaises(ValueError):
45 self.polygon.y_vertices[0] = 0.0
47 def test_boxes(self) -> None:
48 """Test 'from_box', the `area` property, and the 'contains' method
49 with polygon arguments.
50 """
51 small = Polygon.from_box(Box.factory[-3:3, 40:45])
52 self.assertEqual(small.area, 30.0)
53 self.assertTrue(self.polygon.contains(small))
54 self.assertFalse(small.contains(self.polygon))
55 big = Polygon.from_box(Box.factory[-10:10, 20:60])
56 self.assertEqual(big.area, 800.0)
57 self.assertFalse(self.polygon.contains(big))
58 self.assertTrue(big.contains(self.polygon))
59 medium = Polygon.from_box(Box.factory[-4:8, 31:52])
60 self.assertEqual(medium.area, 252.0)
61 self.assertFalse(self.polygon.contains(medium))
62 self.assertFalse(medium.contains(self.polygon))
63 self.assertTrue(self.polygon.contains(self.polygon))
65 def test_contains_points(self) -> None:
66 """Test the 'contains' method with points."""
67 self.assertTrue(self.polygon.contains(x=40.0, y=0.0))
68 self.assertFalse(self.polygon.contains(x=0.0, y=0.0))
69 self.assertFalse(self.polygon.contains(x=40.0, y=10.0))
70 np.testing.assert_array_equal(
71 self.polygon.contains(x=np.array([40.0, 0.0, 40.0]), y=np.array([0.0, 0.0, 10.0])),
72 np.array([True, False, False]),
73 )
75 @unittest.skipUnless(have_legacy, "lsst legacy packages could not be imported.")
76 def test_legacy(self) -> None:
77 """Test conversion to/from lsst.afw.geom.Polygon."""
78 legacy_polygon = self.polygon.to_legacy()
79 self.assertEqual(legacy_polygon.calculateArea(), self.polygon.area)
80 self.assertEqual(Polygon.from_legacy(legacy_polygon), self.polygon)
83if __name__ == "__main__":
84 unittest.main()