Coverage for tests/test_transformDiaSourceCatalog.py: 20%
103 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-11 03:25 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-11 03:25 -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 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
35from lsst.utils import getPackageDir
36import lsst.utils.tests
38from lsst.ap.association.transformDiaSourceCatalog import UnpackApdbFlags
41class TestTransformDiaSourceCatalogTask(unittest.TestCase):
42 def setUp(self):
43 # The first source will be a sky source.
44 self.nSources = 10
45 # Default PSF size (psfDim in makeEmptyExposure) in TestDataset results
46 # in an 18 pixel wide source box.
47 self.bboxSize = 18
48 self.yLoc = 100
49 self.bbox = geom.Box2I(geom.Point2I(0, 0),
50 geom.Extent2I(1024, 1153))
51 dataset = measTests.TestDataset(self.bbox)
52 for srcIdx in range(self.nSources-1):
53 # Place sources at (index, yLoc), so we can distinguish them later.
54 dataset.addSource(100000.0, geom.Point2D(srcIdx, self.yLoc))
55 # Ensure the last source has no peak `significance` field.
56 dataset.addSource(100000.0, geom.Point2D(srcIdx+1, self.yLoc), setPeakSignificance=False)
57 schema = dataset.makeMinimalSchema()
58 schema.addField("base_PixelFlags_flag", type="Flag")
59 schema.addField("base_PixelFlags_flag_offimage", type="Flag")
60 schema.addField("sky_source", type="Flag", doc="Sky objects.")
61 self.exposure, self.inputCatalog = dataset.realize(10.0, schema, randomSeed=1234)
62 self.inputCatalog[0]['sky_source'] = True
63 # Create schemas for use in initializing the TransformDiaSourceCatalog task.
64 self.initInputs = {"diaSourceSchema": Struct(schema=schema)}
65 self.initInputsBadFlags = {"diaSourceSchema": Struct(schema=dataset.makeMinimalSchema())}
67 self.expId = 4321
68 self.date = dafBase.DateTime(nsecs=1400000000 * 10**9)
69 detector = DetectorWrapper(id=23, bbox=self.exposure.getBBox()).detector
70 visit = afwImage.VisitInfo(
71 exposureId=self.expId,
72 exposureTime=200.,
73 date=self.date)
74 self.exposure.info.id = self.expId
75 self.exposure.setDetector(detector)
76 self.exposure.getInfo().setVisitInfo(visit)
77 self.filterName = 'g'
78 self.exposure.setFilter(afwImage.FilterLabel(band=self.filterName, physical='g.MP9401'))
79 scale = 2
80 scaleErr = 1
81 self.photoCalib = afwImage.PhotoCalib(scale, scaleErr)
82 self.exposure.setPhotoCalib(self.photoCalib)
84 self.config = TransformDiaSourceCatalogConfig()
85 self.config.flagMap = os.path.join(getPackageDir("ap_association"),
86 "tests", "data", "test-flag-map.yaml")
87 self.config.functorFile = os.path.join(getPackageDir("ap_association"),
88 "tests",
89 "data",
90 "testDiaSource.yaml")
92 def test_run(self):
93 """Test output dataFrame is created and values are correctly inserted
94 from the exposure.
95 """
96 transformTask = TransformDiaSourceCatalogTask(initInputs=self.initInputs,
97 config=self.config)
98 result = transformTask.run(self.inputCatalog,
99 self.exposure,
100 self.filterName,
101 ccdVisitId=self.expId)
103 self.assertEqual(len(result.diaSourceTable), len(self.inputCatalog))
104 np.testing.assert_array_equal(result.diaSourceTable["bboxSize"], [self.bboxSize]*self.nSources)
105 np.testing.assert_array_equal(result.diaSourceTable["ccdVisitId"], [self.expId]*self.nSources)
106 np.testing.assert_array_equal(result.diaSourceTable["filterName"], [self.filterName]*self.nSources)
107 np.testing.assert_array_equal(result.diaSourceTable["midPointTai"],
108 [self.date.get(system=dafBase.DateTime.MJD)]*self.nSources)
109 np.testing.assert_array_equal(result.diaSourceTable["diaObjectId"], [0]*self.nSources)
110 np.testing.assert_array_equal(result.diaSourceTable["x"], np.arange(self.nSources))
111 # The final snr value should be NaN because it doesn't have a peak significance field.
112 expect_snr = [397.887353515625]*9
113 expect_snr.append(np.nan)
114 # Have to use allclose because assert_array_equal doesn't support equal_nan.
115 np.testing.assert_allclose(result.diaSourceTable["snr"], expect_snr, equal_nan=True, rtol=0)
117 def test_run_doSkySources(self):
118 """Test that we get the correct output with doSkySources=True; the one
119 sky source should be missing, but the other records should be the same.
121 We only test the fields here that could be different, not the ones that
122 are the same for all sources.
123 """
124 # Make the sky source have a different significance value, to distinguish it.
125 self.inputCatalog[0].getFootprint().updatePeakSignificance(5.0)
127 self.config.doRemoveSkySources = True
128 task = TransformDiaSourceCatalogTask(initInputs=self.initInputs, config=self.config)
129 result = task.run(self.inputCatalog, self.exposure, self.filterName, ccdVisitId=self.expId)
131 self.assertEqual(len(result.diaSourceTable), self.nSources-1)
132 # 0th source was removed, so x positions of the remaining sources are at x=1,2,3...
133 np.testing.assert_array_equal(result.diaSourceTable["x"], np.arange(self.nSources-1)+1)
134 # The final snr value should be NaN because it doesn't have a peak significance field.
135 expect_snr = [397.887353515625]*8
136 expect_snr.append(np.nan)
137 # Have to use allclose because assert_array_equal doesn't support equal_nan.
138 np.testing.assert_allclose(result.diaSourceTable["snr"], expect_snr, equal_nan=True, rtol=0)
140 def test_run_dia_source_wrong_flags(self):
141 """Test that the proper errors are thrown when requesting flag columns
142 that are not in the input schema.
143 """
144 with self.assertRaises(KeyError):
145 TransformDiaSourceCatalogTask(initInputs=self.initInputsBadFlags)
147 def test_computeBBoxSize(self):
148 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs,
149 config=self.config)
150 boxSizes = transform.computeBBoxSizes(self.inputCatalog)
152 for size in boxSizes:
153 self.assertEqual(size, self.bboxSize)
154 self.assertEqual(len(boxSizes), self.nSources)
156 def test_bit_unpacker(self):
157 """Test that the integer bit packer is functioning correctly.
158 """
159 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs,
160 config=self.config)
161 for idx, obj in enumerate(self.inputCatalog):
162 if idx in [1, 3, 5]:
163 obj.set("base_PixelFlags_flag", 1)
164 if idx in [1, 4, 6]:
165 obj.set("base_PixelFlags_flag_offimage", 1)
166 outputCatalog = transform.run(self.inputCatalog,
167 self.exposure,
168 self.filterName,
169 ccdVisitId=self.expId).diaSourceTable
171 unpacker = UnpackApdbFlags(self.config.flagMap, "DiaSource")
172 flag_values = unpacker.unpack(outputCatalog["flags"], "flags")
174 for idx, flag in enumerate(flag_values):
175 if idx in [1, 3, 5]:
176 self.assertTrue(flag['base_PixelFlags_flag'])
177 else:
178 self.assertFalse(flag['base_PixelFlags_flag'])
180 if idx in [1, 4, 6]:
181 self.assertTrue(flag['base_PixelFlags_flag_offimage'])
182 else:
183 self.assertFalse(flag['base_PixelFlags_flag_offimage'])
186class MemoryTester(lsst.utils.tests.MemoryTestCase):
187 pass
190def setup_module(module):
191 lsst.utils.tests.init()
194if __name__ == "__main__": 194 ↛ 195line 194 didn't jump to line 195, because the condition on line 194 was never true
195 lsst.utils.tests.init()
196 unittest.main()