Coverage for tests/test_loadDiaCatalogs.py: 24%

108 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-04 15:25 -0700

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/>. 

21 

22import os 

23import numpy as np 

24import tempfile 

25import unittest 

26import yaml 

27 

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 

37 

38 

39def _data_file_name(basename, module_name): 

40 """Return path name of a data file. 

41 

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. 

48 

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) 

56 

57 

58def makeExposure(flipX=False, flipY=False): 

59 """Create an exposure and flip the x or y (or both) coordinates. 

60 

61 Returns bounding boxes that are right or left handed around the bounding 

62 polygon. 

63 

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. 

70 

71 Returns 

72 ------- 

73 exposure : `lsst.afw.image.Exposure` 

74 Exposure with a valid bounding box and wcs. 

75 """ 

76 metadata = dafBase.PropertySet() 

77 

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.) 

85 

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') 

92 

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) 

103 

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 exposureId=1234, 

110 exposureTime=200., 

111 date=dafBase.DateTime("2014-05-13T17:00:00.000000000", 

112 dafBase.DateTime.Timescale.TAI)) 

113 exposure.info.id = 1234 

114 exposure.setDetector(detector) 

115 exposure.info.setVisitInfo(visit) 

116 exposure.setFilter(afwImage.FilterLabel(band='g')) 

117 

118 return exposure 

119 

120 

121class TestLoadDiaCatalogs(unittest.TestCase): 

122 

123 def setUp(self): 

124 np.random.seed(1234) 

125 

126 self.db_file_fd, self.db_file = tempfile.mkstemp( 

127 dir=os.path.dirname(__file__)) 

128 

129 self.apdbConfig = ApdbSqlConfig() 

130 self.apdbConfig.db_url = "sqlite:///" + self.db_file 

131 self.apdbConfig.dia_object_index = "baseline" 

132 self.apdbConfig.dia_object_columns = [] 

133 

134 self.apdb = ApdbSql(config=self.apdbConfig) 

135 self.apdb.makeSchema() 

136 

137 self.exposure = makeExposure(False, False) 

138 

139 self.diaObjects = utils_tests.makeDiaObjects(20, self.exposure) 

140 self.diaSources = utils_tests.makeDiaSources( 

141 100, 

142 self.diaObjects["diaObjectId"].to_numpy(), 

143 self.exposure) 

144 self.diaForcedSources = utils_tests.makeDiaForcedSources( 

145 200, 

146 self.diaObjects["diaObjectId"].to_numpy(), 

147 self.exposure) 

148 

149 self.dateTime = self.exposure.visitInfo.date 

150 self.apdb.store(self.dateTime, 

151 self.diaObjects, 

152 self.diaSources, 

153 self.diaForcedSources) 

154 

155 # These columns are not in the DPDD, yet do appear in DiaSource.yaml. 

156 # We don't need to check them against the default APDB schema. 

157 self.ignoreColumns = ["band", "bboxSize", "isDipole"] 

158 

159 def tearDown(self): 

160 os.close(self.db_file_fd) 

161 os.remove(self.db_file) 

162 

163 def testRun(self): 

164 """Test the full run method for the loader. 

165 """ 

166 diaLoader = LoadDiaCatalogsTask() 

167 result = diaLoader.run(self.exposure, self.apdb) 

168 

169 self.assertEqual(len(result.diaObjects), len(self.diaObjects)) 

170 self.assertEqual(len(result.diaSources), len(self.diaSources)) 

171 self.assertEqual(len(result.diaForcedSources), 

172 len(self.diaForcedSources)) 

173 

174 def testLoadDiaObjects(self): 

175 """Test that the correct number of diaObjects are loaded. 

176 """ 

177 diaLoader = LoadDiaCatalogsTask() 

178 region = diaLoader._getRegion(self.exposure) 

179 diaObjects = diaLoader.loadDiaObjects(region, 

180 self.apdb) 

181 self.assertEqual(len(diaObjects), len(self.diaObjects)) 

182 

183 def testLoadDiaForcedSources(self): 

184 """Test that the correct number of diaForcedSources are loaded. 

185 """ 

186 diaLoader = LoadDiaCatalogsTask() 

187 region = diaLoader._getRegion(self.exposure) 

188 diaForcedSources = diaLoader.loadDiaForcedSources( 

189 self.diaObjects, 

190 region, 

191 self.dateTime, 

192 self.apdb) 

193 self.assertEqual(len(diaForcedSources), len(self.diaForcedSources)) 

194 

195 def testLoadDiaSources(self): 

196 """Test that the correct number of diaSources are loaded. 

197 

198 Also check that they can be properly loaded both by location and 

199 ``diaObjectId``. 

200 """ 

201 diaConfig = LoadDiaCatalogsConfig() 

202 diaLoader = LoadDiaCatalogsTask(config=diaConfig) 

203 

204 region = diaLoader._getRegion(self.exposure) 

205 diaSources = diaLoader.loadDiaSources(self.diaObjects, 

206 region, 

207 self.dateTime, 

208 self.apdb) 

209 self.assertEqual(len(diaSources), len(self.diaSources)) 

210 

211 def test_apdbSchema(self): 

212 """Test that the default DiaSource schema from dax_apdb agrees with the 

213 column names defined here in ap_association/data/DiaSource.yaml. 

214 """ 

215 tableDef = self.apdb.tableDef(ApdbTables.DiaSource) 

216 apdbSchemaColumns = [column.name for column in tableDef.columns] 

217 

218 functorFile = _data_file_name("DiaSource.yaml", "ap_association") 

219 with open(functorFile) as yaml_stream: 

220 diaSourceFunctor = yaml.safe_load_all(yaml_stream) 

221 for functor in diaSourceFunctor: 

222 diaSourceColumns = [column for column in list(functor['funcs'].keys()) 

223 if column not in self.ignoreColumns] 

224 self.assertLess(set(diaSourceColumns), set(apdbSchemaColumns)) 

225 

226 

227class MemoryTester(lsst.utils.tests.MemoryTestCase): 

228 pass 

229 

230 

231def setup_module(module): 

232 lsst.utils.tests.init() 

233 

234 

235if __name__ == "__main__": 235 ↛ 236line 235 didn't jump to line 236, because the condition on line 235 was never true

236 lsst.utils.tests.init() 

237 unittest.main()