Coverage for tests/test_ssoAssociationTask.py: 24%
74 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-20 02:20 -0800
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-20 02:20 -0800
1# This file is part of ap_association.
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# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22import numpy as np
23from astropy.table import Table
24import astropy.table as tb
25import unittest
27from lsst.pipe.tasks.ssoAssociation import SolarSystemAssociationTask
28import astshim as ast
29import lsst.geom as geom
30import lsst.afw.geom as afwGeom
31import lsst.meas.base.tests as measTests
32import lsst.utils.tests
35class TestSolarSystemAssociation(unittest.TestCase):
37 def setUp(self):
38 # Make fake sources
39 self.nSources = 10
40 self.bbox = geom.Box2I(geom.Point2I(0, 0),
41 geom.Extent2I(1024, 1153))
42 self.xyLoc = 100
43 dataset = measTests.TestDataset(self.bbox)
44 for srcIdx in range(self.nSources):
45 dataset.addSource(100000.0,
46 geom.Point2D(srcIdx*self.xyLoc,
47 srcIdx*self.xyLoc))
48 schema = dataset.makeMinimalSchema()
49 schema.addField("base_PixelFlags_flag", type="Flag")
50 schema.addField("base_PixelFlags_flag_offimage", type="Flag")
51 self.exposure, catalog = dataset.realize(
52 10.0, schema, randomSeed=1234)
53 for src in catalog:
54 src.setCoord(self.exposure.getWcs().pixelToSky(src.getCentroid()))
55 # Non-invertible WCS to test robustness to distortions.
56 # Coefficients transform (x - 1e-7*x^3 -> x, y -> y); see docs for PolyMap.
57 pixelCoeffs = np.array([[-1.0e-7, 1, 3, 0],
58 [1.0, 1, 1, 0],
59 [1.0, 2, 0, 1],
60 ])
61 self.exposure.setWcs(afwGeom.makeModifiedWcs(
62 afwGeom.TransformPoint2ToPoint2(ast.PolyMap(pixelCoeffs, 2, options="IterInverse=1")),
63 self.exposure.wcs,
64 modifyActualPixels=False
65 ))
67 # Convert to task required format
68 self.testDiaSources = catalog.asAstropy()
69 self.testDiaSources.rename_columns(["coord_ra", "coord_dec"], ["ra", "dec"])
70 self.testDiaSources["ra"] = np.rad2deg(self.testDiaSources["ra"])
71 self.testDiaSources["dec"] = np.rad2deg(self.testDiaSources["dec"])
72 self.testDiaSources = tb.vstack([self.testDiaSources, Table([[45], [45]], names=["ra", "dec"])])
73 self.testDiaSources["ssObjectId"] = 0
74 self.testDiaSources["diaSourceId"] = [i for i in range(len(self.testDiaSources))]
76 # Grab a subset to treat as solar system objects
77 self.testSsObjects = Table()
78 self.testSsObjects["ObjID"] = ["test_ob"]
79 self.testSsObjects["ssObjectId"] = [1234]
80 self.testSsObjects["ra"] = [45]
81 self.testSsObjects["dec"] = [45]
82 self.testSsObjects["tmin"] = [-1]
83 self.testSsObjects["tmax"] = [1]
84 self.testSsObjects["obs_x_poly"] = [np.array([0])]
85 self.testSsObjects["obs_y_poly"] = [np.array([0])]
86 self.testSsObjects["obs_z_poly"] = [np.array([0])]
87 self.testSsObjects["obj_x_poly"] = [np.array([1])]
88 self.testSsObjects["obj_y_poly"] = [np.array([1])]
89 self.testSsObjects["obj_z_poly"] = [np.array([2 ** 0.5])]
90 self.testSsObjects["Err(arcsec)"] = np.ones(len(self.testSsObjects))
92 def test_run(self):
93 """Test that association and id assignment work as expected.
94 """
95 ssAssocTask = SolarSystemAssociationTask()
96 results = ssAssocTask.run(self.testDiaSources,
97 self.testSsObjects,
98 self.exposure.visitInfo,
99 self.exposure.getBBox(),
100 self.exposure.wcs)
101 self.assertEqual(len(results.ssoAssocDiaSources), 1)
102 self.assertEqual(results.ssoAssocDiaSources['ra'][0], 45.0)
103 self.assertEqual(results.ssoAssocDiaSources['dec'][0], 45.0)
104 self.assertEqual(results.ssoAssocDiaSources['ssObjectId'][0], 1234)
106 def test_mask(self):
107 """Test that masking against the CCD bounding box works as expected.
108 """
109 ssAssocTask = SolarSystemAssociationTask()
110 # Test will all inside ccd
111 maskedObjects = ssAssocTask._maskToCcdRegion(self.testSsObjects,
112 self.exposure.getBBox(),
113 self.exposure.wcs,
114 1.0)
115 self.assertEqual(len(maskedObjects), len(self.testSsObjects))
117 # Add a new SolarSystemObjects outside of the bbox and test that it
118 # is excluded.
119 testObjects = tb.vstack([self.testSsObjects[:1] for i in range(3)])
120 testObjects[0]["ra"] = 150
121 testObjects[0]["dec"] = 80
122 testObjects[1]["ra"] = 150
123 testObjects[1]["dec"] = -80
124 # Coordinates are chosen so that the inverse WCS erroneously maps them
125 # to inside the box (to (74.5, 600.6) instead of around (1745, 600.6)).
126 testObjects[2]["ra"] = 44.91215199831453
127 testObjects[2]["dec"] = 45.001331943391406
128 maskedObjects = ssAssocTask._maskToCcdRegion(
129 tb.vstack([self.testSsObjects, testObjects]),
130 self.exposure.getBBox(),
131 self.exposure.wcs,
132 1.0)
133 self.assertEqual(len(maskedObjects), len(self.testSsObjects))
136class MemoryTester(lsst.utils.tests.MemoryTestCase):
137 pass
140def setup_module(module):
141 lsst.utils.tests.init()
144if __name__ == "__main__": 144 ↛ 145line 144 didn't jump to line 145 because the condition on line 144 was never true
145 lsst.utils.tests.init()
146 unittest.main()