Coverage for tests/test_transformDiaSourceCatalog.py: 97%
112 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-09-29 10:47 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-09-29 10:47 +0000
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 os
23import unittest
25import numpy as np
27from lsst.ap.association.transformDiaSourceCatalog import (TransformDiaSourceCatalogConfig,
28 TransformDiaSourceCatalogTask)
29from lsst.afw.cameraGeom.testUtils import DetectorWrapper
30import lsst.daf.base as dafBase
31import lsst.afw.image as afwImage
32import lsst.geom as geom
33import lsst.meas.base.tests as measTests
34from lsst.pipe.base import Struct
35import lsst.utils.tests
37from lsst.ap.association.transformDiaSourceCatalog import UnpackApdbFlags
39TESTDIR = os.path.abspath(os.path.dirname(__file__))
42class TestTransformDiaSourceCatalogTask(unittest.TestCase):
43 def setUp(self):
44 # Create an instance of random generator with fixed seed.
45 rng = np.random.default_rng(1234)
47 self.nSources = 10
48 # Default PSF size (psfDim in makeEmptyExposure) in TestDataset results
49 # in an 18 pixel wide source box.
50 self.bboxSize = 18
51 self.yLoc = 100
52 self.bbox = geom.Box2I(geom.Point2I(0, 0),
53 geom.Extent2I(1024, 1153))
54 dataset = measTests.TestDataset(self.bbox)
55 for srcIdx in range(self.nSources-1):
56 # Place sources at (index, yLoc), so we can distinguish them later.
57 dataset.addSource(100000.0, geom.Point2D(srcIdx, self.yLoc))
58 # Ensure the last source has no peak `significance` field.
59 dataset.addSource(100000.0, geom.Point2D(srcIdx+1, self.yLoc), setPeakSignificance=False)
60 schema = dataset.makeMinimalSchema()
61 schema.addField("base_PixelFlags_flag", type="Flag")
62 schema.addField("base_PixelFlags_flag_offimage", type="Flag")
63 schema.addField("sky_source", type="Flag", doc="Sky objects.")
64 schema.addField("reliability", doc="real/bogus score of this source", type=float)
65 self.exposure, self.inputCatalog = dataset.realize(10.0, schema, randomSeed=1234)
66 # Create schemas for use in initializing the TransformDiaSourceCatalog task.
67 self.initInputs = {"diaSourceSchema": Struct(schema=schema)}
68 self.initInputsBadFlags = {"diaSourceSchema": Struct(schema=dataset.makeMinimalSchema())}
70 self.inputCatalog["reliability"] = rng.random(self.nSources, dtype=np.float32)
72 self.expId = 4321
73 self.date = dafBase.DateTime(nsecs=1400000000 * 10**9)
74 detector = DetectorWrapper(id=23, bbox=self.exposure.getBBox()).detector
75 visit = afwImage.VisitInfo(
76 id=self.expId,
77 exposureTime=200.,
78 date=self.date)
79 self.exposure.info.id = self.expId + (10000*detector.getId())
80 self.exposure.setDetector(detector)
81 self.exposure.info.setVisitInfo(visit)
82 self.band = 'g'
83 self.exposure.setFilter(afwImage.FilterLabel(band=self.band, physical='g.MP9401'))
84 scale = 2
85 scaleErr = 1
86 self.photoCalib = afwImage.PhotoCalib(scale, scaleErr)
87 self.exposure.setPhotoCalib(self.photoCalib)
89 self.config = TransformDiaSourceCatalogConfig()
90 self.config.flagMap = os.path.join(TESTDIR, "data", "test-flag-map.yaml")
91 self.config.functorFile = os.path.join(TESTDIR,
92 "data",
93 "testDiaSource.yaml")
95 def test_run(self):
96 """Test output dataFrame is created and values are correctly inserted
97 from the exposure.
98 """
99 transformTask = TransformDiaSourceCatalogTask(initInputs=self.initInputs,
100 config=self.config)
101 result = transformTask.run(self.inputCatalog,
102 self.exposure,
103 self.band)
105 self.assertEqual(len(result.diaSourceTable), len(self.inputCatalog))
106 np.testing.assert_array_equal(result.diaSourceTable["bboxSize"], [self.bboxSize]*self.nSources)
107 np.testing.assert_array_equal(result.diaSourceTable["visit"],
108 [self.exposure.visitInfo.id]*self.nSources)
109 np.testing.assert_array_equal(result.diaSourceTable["detector"],
110 [self.exposure.detector.getId()]*self.nSources)
111 np.testing.assert_array_equal(result.diaSourceTable["band"], [self.band]*self.nSources)
112 np.testing.assert_array_equal(result.diaSourceTable["midpointMjdTai"],
113 [self.date.get(system=dafBase.DateTime.MJD)]*self.nSources)
114 np.testing.assert_array_equal(result.diaSourceTable["exposureTime"],
115 [self.exposure.visitInfo.exposureTime]*self.nSources)
116 np.testing.assert_array_equal(result.diaSourceTable["diaObjectId"], [0]*self.nSources)
117 np.testing.assert_array_equal(result.diaSourceTable["x"], np.arange(self.nSources))
118 # The final snr value should be NaN because it doesn't have a peak significance field.
119 expect_snr = [397.887353515625]*9
120 expect_snr.append(np.nan)
121 # Have to use allclose because assert_array_equal doesn't support equal_nan.
122 np.testing.assert_allclose(result.diaSourceTable["snr"], expect_snr, equal_nan=True, rtol=0)
124 def test_run_with_apdb_schema(self):
125 """Test output dataFrame is created and values are correctly inserted
126 from the exposure.
127 """
128 self.config.doUseSchema = True
129 transformTask = TransformDiaSourceCatalogTask(initInputs=self.initInputs,
130 config=self.config)
131 result = transformTask.run(self.inputCatalog,
132 self.exposure,
133 self.band)
135 self.assertEqual(len(result.diaSourceTable), len(self.inputCatalog))
136 np.testing.assert_array_equal(result.diaSourceTable["bboxSize"], [self.bboxSize]*self.nSources)
137 np.testing.assert_array_equal(result.diaSourceTable["visit"],
138 [self.exposure.visitInfo.id]*self.nSources)
139 np.testing.assert_array_equal(result.diaSourceTable["detector"],
140 [self.exposure.detector.getId()]*self.nSources)
141 np.testing.assert_array_equal(result.diaSourceTable["band"], [self.band]*self.nSources)
142 np.testing.assert_array_equal(result.diaSourceTable["midpointMjdTai"],
143 [self.date.get(system=dafBase.DateTime.MJD)]*self.nSources)
144 np.testing.assert_array_equal(result.diaSourceTable["exposureTime"],
145 [self.exposure.visitInfo.exposureTime]*self.nSources)
146 np.testing.assert_array_equal(result.diaSourceTable["diaObjectId"], [0]*self.nSources)
147 np.testing.assert_array_equal(result.diaSourceTable["x"], np.arange(self.nSources))
148 # The final snr value should be NaN because it doesn't have a peak significance field.
149 expect_snr = [397.887353515625]*9
150 expect_snr.append(np.nan)
151 # Downcast to single precision to match `sdm_schemas`
152 expect_snr = np.array(expect_snr, dtype=np.float32)
153 # Have to use allclose because assert_array_equal doesn't support equal_nan.
154 np.testing.assert_allclose(result.diaSourceTable["snr"], expect_snr, equal_nan=True, rtol=0)
156 def test_run_dia_source_wrong_flags(self):
157 """Test that the proper errors are thrown when requesting flag columns
158 that are not in the input schema.
159 """
160 with self.assertRaises(KeyError):
161 TransformDiaSourceCatalogTask(initInputs=self.initInputsBadFlags)
163 def test_computeBBoxSize(self):
164 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs,
165 config=self.config)
166 boxSizes = transform.computeBBoxSizes(self.inputCatalog)
168 for size in boxSizes:
169 self.assertEqual(size, self.bboxSize)
170 self.assertEqual(len(boxSizes), self.nSources)
172 def test_flag_existence_check(self):
173 unpacker = UnpackApdbFlags(self.config.flagMap, "DiaSource")
175 self.assertTrue(unpacker.flagExists('base_PixelFlags_flag'))
176 self.assertFalse(unpacker.flagExists(''))
177 with self.assertRaisesRegex(ValueError, 'column doesNotExist not in flag map'):
178 unpacker.flagExists('base_PixelFlags_flag', columnName='doesNotExist')
180 def test_flag_bitmask(self):
181 """Test that we get the expected bitmask back from supplied flag names.
182 """
183 unpacker = UnpackApdbFlags(self.config.flagMap, "DiaSource")
185 with self.assertRaisesRegex(ValueError, "flag '' not included"):
186 unpacker.makeFlagBitMask([''])
187 with self.assertRaisesRegex(ValueError, 'column doesNotExist not in flag map'):
188 unpacker.makeFlagBitMask(['base_PixelFlags_flag'], columnName='doesNotExist')
189 self.assertEqual(unpacker.makeFlagBitMask(['base_PixelFlags_flag']), np.uint64(1))
190 self.assertEqual(unpacker.makeFlagBitMask(['base_PixelFlags_flag_offimage']), np.uint64(4))
191 self.assertEqual(unpacker.makeFlagBitMask(['base_PixelFlags_flag',
192 'base_PixelFlags_flag_offimage']),
193 np.uint64(5))
196class MemoryTester(lsst.utils.tests.MemoryTestCase):
197 pass
200def setup_module(module):
201 lsst.utils.tests.init()
204if __name__ == "__main__": 204 ↛ 205line 204 didn't jump to line 205 because the condition on line 204 was never true
205 lsst.utils.tests.init()
206 unittest.main()