Coverage for tests/test_loadDiaCatalogs.py: 24%
108 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-01 12:43 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-01 12:43 +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 numpy as np
24import tempfile
25import unittest
26import yaml
28from lsst.afw.cameraGeom.testUtils import DetectorWrapper
29import lsst.afw.geom as afwGeom
30import lsst.afw.image as afwImage
31from lsst.ap.association import LoadDiaCatalogsTask, LoadDiaCatalogsConfig
32import lsst.daf.base as dafBase
33from lsst.dax.apdb import ApdbSql, ApdbSqlConfig, ApdbTables
34from lsst.utils import getPackageDir
35import lsst.utils.tests
36import utils_tests
39def _data_file_name(basename, module_name):
40 """Return path name of a data file.
42 Parameters
43 ----------
44 basename : `str`
45 Name of the file to add to the path string.
46 module_name : `str`
47 Name of lsst stack package environment variable.
49 Returns
50 -------
51 data_file_path : `str`
52 Full path of the file to load from the "data" directory in a given
53 repository.
54 """
55 return os.path.join(getPackageDir(module_name), "data", basename)
58def makeExposure(flipX=False, flipY=False):
59 """Create an exposure and flip the x or y (or both) coordinates.
61 Returns bounding boxes that are right or left handed around the bounding
62 polygon.
64 Parameters
65 ----------
66 flipX : `bool`
67 Flip the x coordinate in the WCS.
68 flipY : `bool`
69 Flip the y coordinate in the WCS.
71 Returns
72 -------
73 exposure : `lsst.afw.image.Exposure`
74 Exposure with a valid bounding box and wcs.
75 """
76 metadata = dafBase.PropertySet()
78 metadata.set("SIMPLE", "T")
79 metadata.set("BITPIX", -32)
80 metadata.set("NAXIS", 2)
81 metadata.set("NAXIS1", 1024)
82 metadata.set("NAXIS2", 1153)
83 metadata.set("RADECSYS", 'FK5')
84 metadata.set("EQUINOX", 2000.)
86 metadata.setDouble("CRVAL1", 215.604025685476)
87 metadata.setDouble("CRVAL2", 53.1595451514076)
88 metadata.setDouble("CRPIX1", 1109.99981456774)
89 metadata.setDouble("CRPIX2", 560.018167811613)
90 metadata.set("CTYPE1", 'RA---SIN')
91 metadata.set("CTYPE2", 'DEC--SIN')
93 xFlip = 1
94 if flipX:
95 xFlip = -1
96 yFlip = 1
97 if flipY:
98 yFlip = -1
99 metadata.setDouble("CD1_1", xFlip * 5.10808596133527E-05)
100 metadata.setDouble("CD1_2", yFlip * 1.85579539217196E-07)
101 metadata.setDouble("CD2_2", yFlip * -5.10281493481982E-05)
102 metadata.setDouble("CD2_1", xFlip * -8.27440751733828E-07)
104 wcs = afwGeom.makeSkyWcs(metadata)
105 exposure = afwImage.makeExposure(
106 afwImage.makeMaskedImageFromArrays(np.ones((1024, 1153))), wcs)
107 detector = DetectorWrapper(id=23, bbox=exposure.getBBox()).detector
108 visit = afwImage.VisitInfo(
109 exposureTime=200.,
110 date=dafBase.DateTime("2014-05-13T17:00:00.000000000",
111 dafBase.DateTime.Timescale.TAI))
112 exposure.info.id = 1234
113 exposure.setDetector(detector)
114 exposure.info.setVisitInfo(visit)
115 exposure.setFilter(afwImage.FilterLabel(band='g'))
117 return exposure
120class TestLoadDiaCatalogs(unittest.TestCase):
122 def setUp(self):
123 np.random.seed(1234)
125 self.db_file_fd, self.db_file = tempfile.mkstemp(
126 dir=os.path.dirname(__file__))
128 self.apdbConfig = ApdbSqlConfig()
129 self.apdbConfig.db_url = "sqlite:///" + self.db_file
130 self.apdbConfig.dia_object_index = "baseline"
131 self.apdbConfig.dia_object_columns = []
133 self.apdb = ApdbSql(config=self.apdbConfig)
134 self.apdb.makeSchema()
136 self.exposure = makeExposure(False, False)
138 self.diaObjects = utils_tests.makeDiaObjects(20, self.exposure)
139 self.diaSources = utils_tests.makeDiaSources(
140 100,
141 self.diaObjects["diaObjectId"].to_numpy(),
142 self.exposure)
143 self.diaForcedSources = utils_tests.makeDiaForcedSources(
144 200,
145 self.diaObjects["diaObjectId"].to_numpy(),
146 self.exposure)
148 self.dateTime = self.exposure.visitInfo.date
149 self.apdb.store(self.dateTime,
150 self.diaObjects,
151 self.diaSources,
152 self.diaForcedSources)
154 # These columns are not in the DPDD, yet do appear in DiaSource.yaml.
155 # We don't need to check them against the default APDB schema.
156 self.ignoreColumns = ["band", "bboxSize", "isDipole"]
158 def tearDown(self):
159 os.close(self.db_file_fd)
160 os.remove(self.db_file)
162 def testRun(self):
163 """Test the full run method for the loader.
164 """
165 diaLoader = LoadDiaCatalogsTask()
166 result = diaLoader.run(self.exposure, self.apdb)
168 self.assertEqual(len(result.diaObjects), len(self.diaObjects))
169 self.assertEqual(len(result.diaSources), len(self.diaSources))
170 self.assertEqual(len(result.diaForcedSources),
171 len(self.diaForcedSources))
173 def testLoadDiaObjects(self):
174 """Test that the correct number of diaObjects are loaded.
175 """
176 diaLoader = LoadDiaCatalogsTask()
177 region = diaLoader._getRegion(self.exposure)
178 diaObjects = diaLoader.loadDiaObjects(region,
179 self.apdb)
180 self.assertEqual(len(diaObjects), len(self.diaObjects))
182 def testLoadDiaForcedSources(self):
183 """Test that the correct number of diaForcedSources are loaded.
184 """
185 diaLoader = LoadDiaCatalogsTask()
186 region = diaLoader._getRegion(self.exposure)
187 diaForcedSources = diaLoader.loadDiaForcedSources(
188 self.diaObjects,
189 region,
190 self.dateTime,
191 self.apdb)
192 self.assertEqual(len(diaForcedSources), len(self.diaForcedSources))
194 def testLoadDiaSources(self):
195 """Test that the correct number of diaSources are loaded.
197 Also check that they can be properly loaded both by location and
198 ``diaObjectId``.
199 """
200 diaConfig = LoadDiaCatalogsConfig()
201 diaLoader = LoadDiaCatalogsTask(config=diaConfig)
203 region = diaLoader._getRegion(self.exposure)
204 diaSources = diaLoader.loadDiaSources(self.diaObjects,
205 region,
206 self.dateTime,
207 self.apdb)
208 self.assertEqual(len(diaSources), len(self.diaSources))
210 def test_apdbSchema(self):
211 """Test that the default DiaSource schema from dax_apdb agrees with the
212 column names defined here in ap_association/data/DiaSource.yaml.
213 """
214 tableDef = self.apdb.tableDef(ApdbTables.DiaSource)
215 apdbSchemaColumns = [column.name for column in tableDef.columns]
217 functorFile = _data_file_name("DiaSource.yaml", "ap_association")
218 with open(functorFile) as yaml_stream:
219 diaSourceFunctor = yaml.safe_load_all(yaml_stream)
220 for functor in diaSourceFunctor:
221 diaSourceColumns = [column for column in list(functor['funcs'].keys())
222 if column not in self.ignoreColumns]
223 self.assertLess(set(diaSourceColumns), set(apdbSchemaColumns))
226class MemoryTester(lsst.utils.tests.MemoryTestCase):
227 pass
230def setup_module(module):
231 lsst.utils.tests.init()
234if __name__ == "__main__": 234 ↛ 235line 234 didn't jump to line 235, because the condition on line 234 was never true
235 lsst.utils.tests.init()
236 unittest.main()