Coverage for tests/test_transformDiaSourceCatalog.py: 18%
131 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-24 12:28 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-24 12:28 +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 # The first source will be a sky source.
45 self.nSources = 10
46 # Default PSF size (psfDim in makeEmptyExposure) in TestDataset results
47 # in an 18 pixel wide source box.
48 self.bboxSize = 18
49 self.yLoc = 100
50 self.bbox = geom.Box2I(geom.Point2I(0, 0),
51 geom.Extent2I(1024, 1153))
52 dataset = measTests.TestDataset(self.bbox)
53 for srcIdx in range(self.nSources-1):
54 # Place sources at (index, yLoc), so we can distinguish them later.
55 dataset.addSource(100000.0, geom.Point2D(srcIdx, self.yLoc))
56 # Ensure the last source has no peak `significance` field.
57 dataset.addSource(100000.0, geom.Point2D(srcIdx+1, self.yLoc), setPeakSignificance=False)
58 schema = dataset.makeMinimalSchema()
59 schema.addField("base_PixelFlags_flag", type="Flag")
60 schema.addField("base_PixelFlags_flag_offimage", type="Flag")
61 schema.addField("sky_source", type="Flag", doc="Sky objects.")
62 self.exposure, self.inputCatalog = dataset.realize(10.0, schema, randomSeed=1234)
63 self.inputCatalog[0]['sky_source'] = True
64 # Create schemas for use in initializing the TransformDiaSourceCatalog task.
65 self.initInputs = {"diaSourceSchema": Struct(schema=schema)}
66 self.initInputsBadFlags = {"diaSourceSchema": Struct(schema=dataset.makeMinimalSchema())}
68 # Separate real/bogus score table, indexed on the above catalog ids.
69 reliabilitySchema = lsst.afw.table.Schema()
70 reliabilitySchema.addField(self.inputCatalog.schema["id"].asField())
71 reliabilitySchema.addField("score", doc="real/bogus score of this source", type=float)
72 self.reliability = lsst.afw.table.BaseCatalog(reliabilitySchema)
73 self.reliability.resize(len(self.inputCatalog))
74 self.reliability["id"] = self.inputCatalog["id"]
75 self.reliability["score"] = np.random.random(len(self.inputCatalog))
77 self.expId = 4321
78 self.date = dafBase.DateTime(nsecs=1400000000 * 10**9)
79 detector = DetectorWrapper(id=23, bbox=self.exposure.getBBox()).detector
80 visit = afwImage.VisitInfo(
81 exposureTime=200.,
82 date=self.date)
83 self.exposure.info.id = self.expId
84 self.exposure.setDetector(detector)
85 self.exposure.info.setVisitInfo(visit)
86 self.band = 'g'
87 self.exposure.setFilter(afwImage.FilterLabel(band=self.band, physical='g.MP9401'))
88 scale = 2
89 scaleErr = 1
90 self.photoCalib = afwImage.PhotoCalib(scale, scaleErr)
91 self.exposure.setPhotoCalib(self.photoCalib)
93 self.config = TransformDiaSourceCatalogConfig()
94 self.config.flagMap = os.path.join(TESTDIR, "data", "test-flag-map.yaml")
95 self.config.functorFile = os.path.join(TESTDIR,
96 "data",
97 "testDiaSource.yaml")
99 def test_run(self):
100 """Test output dataFrame is created and values are correctly inserted
101 from the exposure.
102 """
103 transformTask = TransformDiaSourceCatalogTask(initInputs=self.initInputs,
104 config=self.config)
105 result = transformTask.run(self.inputCatalog,
106 self.exposure,
107 self.band,
108 ccdVisitId=self.expId)
110 self.assertEqual(len(result.diaSourceTable), len(self.inputCatalog))
111 np.testing.assert_array_equal(result.diaSourceTable["bboxSize"], [self.bboxSize]*self.nSources)
112 np.testing.assert_array_equal(result.diaSourceTable["ccdVisitId"], [self.expId]*self.nSources)
113 np.testing.assert_array_equal(result.diaSourceTable["band"], [self.band]*self.nSources)
114 np.testing.assert_array_equal(result.diaSourceTable["midpointMjdTai"],
115 [self.date.get(system=dafBase.DateTime.MJD)]*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_reliability(self):
125 self.config.doIncludeReliability = True
126 transformTask = TransformDiaSourceCatalogTask(initInputs=self.initInputs,
127 config=self.config)
128 result = transformTask.run(self.inputCatalog,
129 self.exposure,
130 self.band,
131 reliability=self.reliability,
132 ccdVisitId=self.expId)
133 self.assertEqual(len(result.diaSourceTable), len(self.inputCatalog))
134 np.testing.assert_array_equal(result.diaSourceTable["reliability"], self.reliability["score"])
136 def test_run_doSkySources(self):
137 """Test that we get the correct output with doSkySources=True; the one
138 sky source should be missing, but the other records should be the same.
140 We only test the fields here that could be different, not the ones that
141 are the same for all sources.
142 """
143 # Make the sky source have a different significance value, to distinguish it.
144 self.inputCatalog[0].getFootprint().updatePeakSignificance(5.0)
146 self.config.doRemoveSkySources = True
147 task = TransformDiaSourceCatalogTask(initInputs=self.initInputs, config=self.config)
148 result = task.run(self.inputCatalog, self.exposure, self.band, ccdVisitId=self.expId)
150 self.assertEqual(len(result.diaSourceTable), self.nSources-1)
151 # 0th source was removed, so x positions of the remaining sources are at x=1,2,3...
152 np.testing.assert_array_equal(result.diaSourceTable["x"], np.arange(self.nSources-1)+1)
153 # The final snr value should be NaN because it doesn't have a peak significance field.
154 expect_snr = [397.887353515625]*8
155 expect_snr.append(np.nan)
156 # Have to use allclose because assert_array_equal doesn't support equal_nan.
157 np.testing.assert_allclose(result.diaSourceTable["snr"], expect_snr, equal_nan=True, rtol=0)
159 def test_run_dia_source_wrong_flags(self):
160 """Test that the proper errors are thrown when requesting flag columns
161 that are not in the input schema.
162 """
163 with self.assertRaises(KeyError):
164 TransformDiaSourceCatalogTask(initInputs=self.initInputsBadFlags)
166 def test_computeBBoxSize(self):
167 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs,
168 config=self.config)
169 boxSizes = transform.computeBBoxSizes(self.inputCatalog)
171 for size in boxSizes:
172 self.assertEqual(size, self.bboxSize)
173 self.assertEqual(len(boxSizes), self.nSources)
175 def test_bit_unpacker(self):
176 """Test that the integer bit packer is functioning correctly.
177 """
178 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs,
179 config=self.config)
180 for idx, obj in enumerate(self.inputCatalog):
181 if idx in [1, 3, 5]:
182 obj.set("base_PixelFlags_flag", 1)
183 if idx in [1, 4, 6]:
184 obj.set("base_PixelFlags_flag_offimage", 1)
185 outputCatalog = transform.run(self.inputCatalog,
186 self.exposure,
187 self.band,
188 ccdVisitId=self.expId).diaSourceTable
190 unpacker = UnpackApdbFlags(self.config.flagMap, "DiaSource")
191 flag_values = unpacker.unpack(outputCatalog["flags"], "flags")
193 for idx, flag in enumerate(flag_values):
194 if idx in [1, 3, 5]:
195 self.assertTrue(flag['base_PixelFlags_flag'])
196 else:
197 self.assertFalse(flag['base_PixelFlags_flag'])
199 if idx in [1, 4, 6]:
200 self.assertTrue(flag['base_PixelFlags_flag_offimage'])
201 else:
202 self.assertFalse(flag['base_PixelFlags_flag_offimage'])
204 def test_flag_existence_check(self):
205 unpacker = UnpackApdbFlags(self.config.flagMap, "DiaSource")
207 self.assertTrue(unpacker.flagExists('base_PixelFlags_flag'))
208 self.assertFalse(unpacker.flagExists(''))
209 with self.assertRaisesRegex(ValueError, 'column doesNotExist not in flag map'):
210 unpacker.flagExists('base_PixelFlags_flag', columnName='doesNotExist')
212 def test_flag_bitmask(self):
213 """Test that we get the expected bitmask back from supplied flag names.
214 """
215 unpacker = UnpackApdbFlags(self.config.flagMap, "DiaSource")
217 with self.assertRaisesRegex(ValueError, "flag '' not included"):
218 unpacker.makeFlagBitMask([''])
219 with self.assertRaisesRegex(ValueError, 'column doesNotExist not in flag map'):
220 unpacker.makeFlagBitMask(['base_PixelFlags_flag'], columnName='doesNotExist')
221 self.assertEqual(unpacker.makeFlagBitMask(['base_PixelFlags_flag']), np.uint64(1))
222 self.assertEqual(unpacker.makeFlagBitMask(['base_PixelFlags_flag_offimage']), np.uint64(2))
223 self.assertEqual(unpacker.makeFlagBitMask(['base_PixelFlags_flag',
224 'base_PixelFlags_flag_offimage']),
225 np.uint64(3))
228class MemoryTester(lsst.utils.tests.MemoryTestCase):
229 pass
232def setup_module(module):
233 lsst.utils.tests.init()
236if __name__ == "__main__": 236 ↛ 237line 236 didn't jump to line 237, because the condition on line 236 was never true
237 lsst.utils.tests.init()
238 unittest.main()