Coverage for python/lsst/pipe/tasks/coaddBase.py: 51%

57 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-14 04:33 -0800

1# This file is part of pipe_tasks. 

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 

22__all__ = ["CoaddBaseTask", "getSkyInfo", "makeSkyInfo"] 

23 

24import lsst.pex.config as pexConfig 

25import lsst.afw.image as afwImage 

26import lsst.pipe.base as pipeBase 

27import lsst.meas.algorithms as measAlg 

28 

29from lsst.meas.algorithms import ScaleVarianceTask 

30from .selectImages import PsfWcsSelectImagesTask 

31from .coaddInputRecorder import CoaddInputRecorderTask 

32 

33 

34class CoaddBaseConfig(pexConfig.Config): 

35 """Configuration parameters for CoaddBaseTask 

36 

37 Configuration parameters shared between MakeCoaddTempExp and AssembleCoadd 

38 """ 

39 

40 coaddName = pexConfig.Field( 

41 doc="Coadd name: typically one of deep or goodSeeing.", 

42 dtype=str, 

43 default="deep", 

44 ) 

45 select = pexConfig.ConfigurableField( 

46 doc="Image selection subtask.", 

47 target=PsfWcsSelectImagesTask, 

48 ) 

49 badMaskPlanes = pexConfig.ListField( 

50 dtype=str, 

51 doc="Mask planes that, if set, the associated pixel should not be included in the coaddTempExp.", 

52 default=("NO_DATA",), 

53 ) 

54 inputRecorder = pexConfig.ConfigurableField( 

55 doc="Subtask that helps fill CoaddInputs catalogs added to the final Exposure", 

56 target=CoaddInputRecorderTask 

57 ) 

58 doPsfMatch = pexConfig.Field( 

59 dtype=bool, 

60 doc="Match to modelPsf? Deprecated. Sets makePsfMatched=True, makeDirect=False", 

61 default=False 

62 ) 

63 modelPsf = measAlg.GaussianPsfFactory.makeField(doc="Model Psf factory") 

64 doApplyExternalPhotoCalib = pexConfig.Field( 

65 dtype=bool, 

66 default=False, 

67 doc=("Whether to apply external photometric calibration via an " 

68 "`lsst.afw.image.PhotoCalib` object. Uses the " 

69 "`externalPhotoCalibName` field to determine which calibration " 

70 "to load.") 

71 ) 

72 useGlobalExternalPhotoCalib = pexConfig.Field( 

73 dtype=bool, 

74 default=True, 

75 doc=("When using doApplyExternalPhotoCalib, use 'global' calibrations " 

76 "that are not run per-tract. When False, use per-tract photometric " 

77 "calibration files.") 

78 ) 

79 externalPhotoCalibName = pexConfig.ChoiceField( 

80 # TODO: Remove this config with the removal of Gen2 in DM-20572. 

81 dtype=str, 

82 doc=("Type of external PhotoCalib if `doApplyExternalPhotoCalib` is True. " 

83 "This field is only used for Gen2 middleware."), 

84 default="jointcal", 

85 allowed={ 

86 "jointcal": "Use jointcal_photoCalib", 

87 "fgcm": "Use fgcm_photoCalib", 

88 "fgcm_tract": "Use fgcm_tract_photoCalib" 

89 }, 

90 deprecated="This configuration is no longer used, and will be removed after v25.0", 

91 ) 

92 doApplyExternalSkyWcs = pexConfig.Field( 

93 dtype=bool, 

94 default=False, 

95 doc=("Whether to apply external astrometric calibration via an " 

96 "`lsst.afw.geom.SkyWcs` object. Uses `externalSkyWcsName` " 

97 "field to determine which calibration to load.") 

98 ) 

99 useGlobalExternalSkyWcs = pexConfig.Field( 

100 dtype=bool, 

101 default=False, 

102 doc=("When using doApplyExternalSkyWcs, use 'global' calibrations " 

103 "that are not run per-tract. When False, use per-tract wcs " 

104 "files.") 

105 ) 

106 externalSkyWcsName = pexConfig.ChoiceField( 

107 # TODO: Remove this config with the removal of Gen2 in DM-20572. 

108 dtype=str, 

109 doc=("Type of external SkyWcs if `doApplyExternalSkyWcs` is True. " 

110 "This field is only used for Gen2 middleware."), 

111 default="jointcal", 

112 allowed={ 

113 "jointcal": "Use jointcal_wcs" 

114 }, 

115 deprecated="This configuration is no longer used, and will be removed after v25.0", 

116 ) 

117 includeCalibVar = pexConfig.Field( 

118 dtype=bool, 

119 doc="Add photometric calibration variance to warp variance plane.", 

120 default=False 

121 ) 

122 matchingKernelSize = pexConfig.Field( 122 ↛ exitline 122 didn't jump to the function exit

123 dtype=int, 

124 doc="Size in pixels of matching kernel. Must be odd.", 

125 default=21, 

126 check=lambda x: x % 2 == 1 

127 ) 

128 

129 

130class CoaddBaseTask(pipeBase.PipelineTask): 

131 """Base class for coaddition. 

132 

133 Subclasses must specify _DefaultName 

134 """ 

135 

136 ConfigClass = CoaddBaseConfig 

137 

138 def __init__(self, **kwargs): 

139 super().__init__(**kwargs) 

140 self.makeSubtask("select") 

141 self.makeSubtask("inputRecorder") 

142 

143 def getSkyInfo(self, patchRef): 

144 """Use getSkyinfo to return the skyMap, tract and patch information, wcs and the outer bbox 

145 of the patch. 

146 

147 Parameters 

148 ---------- 

149 patchRef : `Unknown` 

150 Data reference for sky map. Must include keys "tract" and "patch". 

151 

152 Returns 

153 ------- 

154 getSkyInfo : `lsst.pipe.base.Struct` 

155 Sky Info as a struct with attributes: 

156 

157 ``skyMap`` 

158 sky map (`lsst.skyMap.SkyMap`). 

159 ``tractInfo`` 

160 Information for chosen tract of sky map (`lsst.skymap.TractInfo`). 

161 ``patchInfo`` 

162 Information about chosen patch of tract (`lsst.skymap.PatchInfo`). 

163 ``wcs`` 

164 WCS of tract (`lsst.afw.image.SkyWcs`). 

165 ``bbox`` 

166 Outer bbox of patch, as an geom Box2I (`lsst.afw.geom.Box2I`). 

167 """ 

168 return getSkyInfo(coaddName=self.config.coaddName, patchRef=patchRef) 

169 

170 def getTempExpDatasetName(self, warpType="direct"): 

171 """Return warp name for given warpType and task config 

172 

173 Parameters 

174 ---------- 

175 warpType : `str` 

176 Either 'direct' or 'psfMatched'. 

177 

178 Returns 

179 ------- 

180 WarpDatasetName : `str` 

181 """ 

182 return self.config.coaddName + "Coadd_" + warpType + "Warp" 

183 

184 def getBadPixelMask(self): 

185 """Convenience method to provide the bitmask from the mask plane names 

186 """ 

187 return afwImage.Mask.getPlaneBitMask(self.config.badMaskPlanes) 

188 

189 

190def getSkyInfo(coaddName, patchRef): 

191 """Return the SkyMap, tract and patch information, wcs, and outer bbox of the patch to be coadded. 

192 

193 Parameters 

194 ---------- 

195 coaddName : `str` 

196 Coadd name; typically one of deep or goodSeeing. 

197 patchRef : `Unknown` 

198 Data reference for sky map. Must include keys "tract" and "patch". 

199 

200 Returns 

201 ------- 

202 makeSkyInfo : `lsst.pipe.base.Struct` 

203 pipe_base Struct with attributes: 

204 

205 ``skyMap`` 

206 Sky map (`lsst.skyMap.SkyMap`). 

207 ``tractInfo`` 

208 Information for chosen tract of sky map (`lsst.skyMap.TractInfo`). 

209 ``patchInfo`` 

210 Information about chosen patch of tract (`lsst.skyMap.PatchInfo`). 

211 ``wcs`` 

212 WCS of tract (`lsst.afw.image.SkyWcs`). 

213 ``bbox`` 

214 Outer bbox of patch, as an geom Box2I (`lsst.afw.geom.Box2I`). 

215 """ 

216 skyMap = patchRef.get(coaddName + "Coadd_skyMap") 

217 return makeSkyInfo(skyMap, patchRef.dataId["tract"], patchRef.dataId["patch"]) 

218 

219 

220def makeSkyInfo(skyMap, tractId, patchId): 

221 """Constructs SkyInfo used by coaddition tasks for multiple 

222 patchId formats. 

223 

224 Parameters 

225 ---------- 

226 skyMap : `lsst.skyMap.SkyMap` 

227 Sky map. 

228 tractId : `int` 

229 The ID of the tract. 

230 patchId : `str` or `int` or `tuple` of `int` 

231 Either Gen2-style comma delimited string (e.g. '4,5'), 

232 tuple of integers (e.g (4, 5), Gen3-style integer. 

233 

234 Returns 

235 ------- 

236 makeSkyInfo : `lsst.pipe.base.Struct` 

237 pipe_base Struct with attributes: 

238 

239 ``skyMap`` 

240 Sky map (`lsst.skyMap.SkyMap`). 

241 ``tractInfo`` 

242 Information for chosen tract of sky map (`lsst.skyMap.TractInfo`). 

243 ``patchInfo`` 

244 Information about chosen patch of tract (`lsst.skyMap.PatchInfo`). 

245 ``wcs`` 

246 WCS of tract (`lsst.afw.image.SkyWcs`). 

247 ``bbox`` 

248 Outer bbox of patch, as an geom Box2I (`lsst.afw.geom.Box2I`). 

249 """ 

250 tractInfo = skyMap[tractId] 

251 

252 if isinstance(patchId, str) and ',' in patchId: 

253 # patch format is "xIndex,yIndex" 

254 patchIndex = tuple(int(i) for i in patchId.split(",")) 

255 else: 

256 patchIndex = patchId 

257 

258 patchInfo = tractInfo.getPatchInfo(patchIndex) 

259 

260 return pipeBase.Struct( 

261 skyMap=skyMap, 

262 tractInfo=tractInfo, 

263 patchInfo=patchInfo, 

264 wcs=tractInfo.getWcs(), 

265 bbox=patchInfo.getOuterBBox(), 

266 ) 

267 

268 

269def scaleVariance(maskedImage, maskPlanes, log=None): 

270 """Scale the variance in a maskedImage 

271 

272 This is deprecated. Use the ScaleVarianceTask instead. 

273 

274 Parameters 

275 ---------- 

276 maskedImage : `lsst.afw.image.MaskedImage` 

277 MaskedImage to operate on; variance will be scaled. 

278 maskPlanes : `list` 

279 List of mask planes for pixels to reject. 

280 log : `Unknown` 

281 Log for reporting the renormalization factor; or None. 

282 

283 Returns 

284 ------- 

285 task.run : `Unknown` 

286 Renormalization factor. 

287 

288 Notes 

289 ----- 

290 The variance plane in a convolved or warped image (or a coadd derived 

291 from warped images) does not accurately reflect the noise properties of 

292 the image because variance has been lost to covariance. This function 

293 attempts to correct for this by scaling the variance plane to match 

294 the observed variance in the image. This is not perfect (because we're 

295 not tracking the covariance) but it's simple and is often good enough. 

296 """ 

297 config = ScaleVarianceTask.ConfigClass() 

298 config.maskPlanes = maskPlanes 

299 task = ScaleVarianceTask(config=config, name="scaleVariance", log=log) 

300 return task.run(maskedImage) 

301 

302 

303def reorderAndPadList(inputList, inputKeys, outputKeys, padWith=None): 

304 """Match the order of one list to another, padding if necessary 

305 

306 Parameters 

307 ---------- 

308 inputList : `list` 

309 List to be reordered and padded. Elements can be any type. 

310 inputKeys : `iterable` 

311 Iterable of values to be compared with outputKeys. Length must match `inputList`. 

312 outputKeys : `iterable` 

313 Iterable of values to be compared with inputKeys. 

314 padWith : `Unknown` 

315 Any value to be inserted where inputKey not in outputKeys. 

316 

317 Returns 

318 ------- 

319 outputList : `list` 

320 Copy of inputList reordered per outputKeys and padded with `padWith` 

321 so that the length matches length of outputKeys. 

322 """ 

323 outputList = [] 

324 for d in outputKeys: 

325 if d in inputKeys: 

326 outputList.append(inputList[inputKeys.index(d)]) 

327 else: 

328 outputList.append(padWith) 

329 return outputList