Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 

22__all__ = ("TransformDiaSourceCatalogConnections", 

23 "TransformDiaSourceCatalogConfig", 

24 "TransformDiaSourceCatalogTask") 

25 

26import numpy as np 

27import os 

28 

29from lsst.daf.base import DateTime 

30import lsst.pex.config as pexConfig 

31import lsst.pipe.base as pipeBase 

32import lsst.pipe.base.connectionTypes as connTypes 

33from lsst.pipe.tasks.postprocess import TransformCatalogBaseTask 

34from lsst.pipe.tasks.parquetTable import ParquetTable 

35from lsst.utils import getPackageDir 

36 

37 

38class TransformDiaSourceCatalogConnections(pipeBase.PipelineTaskConnections, 

39 dimensions=("instrument", "visit", "detector"), 

40 defaultTemplates={"coaddName": "deep", "fakesType": ""}): 

41 """Butler connections for TransformDiaSourceCatalogTask. 

42 """ 

43 diaSourceCat = connTypes.Input( 

44 doc="Catalog of DiaSources produced during image differencing.", 

45 name="{fakesType}{coaddName}Diff_diaSrc", 

46 storageClass="SourceCatalog", 

47 dimensions=("instrument", "visit", "detector"), 

48 ) 

49 diffIm = connTypes.Input( 

50 doc="Difference image on which the DiaSources were detected.", 

51 name="{fakesType}{coaddName}Diff_differenceExp", 

52 storageClass="ExposureF", 

53 dimensions=("instrument", "visit", "detector"), 

54 ) 

55 diaSourceTable = connTypes.Output( 

56 doc=".", 

57 name="{fakesType}{coaddName}Diff_diaSrcTable", 

58 storageClass="DataFrame", 

59 dimensions=("instrument", "visit", "detector"), 

60 ) 

61 

62 

63class TransformDiaSourceCatalogConfig(pipeBase.PipelineTaskConfig, 

64 pipelineConnections=TransformDiaSourceCatalogConnections): 

65 """ 

66 """ 

67 functorFile = pexConfig.Field( 

68 dtype=str, 

69 doc='Path to YAML file specifying Science DataModel functors to use ' 

70 'when copying columns and computing calibrated values.', 

71 default=os.path.join(getPackageDir("ap_association"), 

72 "data", 

73 "DiaSource.yaml") 

74 ) 

75 

76 

77class TransformDiaSourceCatalogTask(TransformCatalogBaseTask): 

78 """Apply Science DataModel-ification on the DiaSource afw table. 

79 

80 This task calibrates and renames columns in the DiaSource catalog 

81 to ready the catalog for insertion into the Apdb. 

82 

83 This is a Gen3 Butler only task. It will not run in Gen2. 

84 """ 

85 

86 ConfigClass = TransformDiaSourceCatalogConfig 

87 _DefaultName = "transformDiaSourceCatalog" 

88 

89 def __init__(self, **kwargs): 

90 super().__init__(**kwargs) 

91 self.funcs = self.getFunctors() 

92 

93 def runQuantum(self, butlerQC, inputRefs, outputRefs): 

94 inputs = butlerQC.get(inputRefs) 

95 expId, expBits = butlerQC.quantum.dataId.pack("visit_detector", 

96 returnMaxBits=True) 

97 inputs["ccdVisitId"] = expId 

98 inputs["band"] = butlerQC.quantum.dataId["band"] 

99 

100 outputs = self.run(**inputs) 

101 

102 butlerQC.put(outputs, outputRefs) 

103 

104 def run(self, 

105 diaSourceCat, 

106 diffIm, 

107 band, 

108 ccdVisitId, 

109 funcs=None): 

110 """Convert input catalog to ParquetTable/Pandas and run functors. 

111 

112 Additionally, add new columns for stripping information from the 

113 exposure and into the DiaSource catalog. 

114 

115 Parameters 

116 ---------- 

117 

118 Returns 

119 ------- 

120 results : `lsst.pipe.base.Struct` 

121 Results struct with components. 

122 

123 - ``diaSourceTable`` : Catalog of DiaSources with calibrated values 

124 and renamed columns. 

125 (`lsst.pipe.tasks.ParquetTable` or `pandas.DataFrame`) 

126 """ 

127 self.log.info( 

128 "Transforming/standardizing the DiaSource table ccdVisitId: %i", 

129 ccdVisitId) 

130 

131 diaSourceDf = diaSourceCat.asAstropy().to_pandas() 

132 diaSourceDf["bboxSize"] = self.computeBBoxSizes(diaSourceCat) 

133 diaSourceDf["ccdVisitId"] = ccdVisitId 

134 diaSourceDf["filterName"] = band 

135 diaSourceDf["midPointTai"] = diffIm.getInfo().getVisitInfo().getDate().get(system=DateTime.MJD) 

136 diaSourceDf["diaObjectId"] = 0 

137 diaSourceDf["pixelId"] = 0 

138 

139 df = self.transform(band, 

140 ParquetTable(dataFrame=diaSourceDf), 

141 self.funcs, 

142 dataId=None).df 

143 return pipeBase.Struct( 

144 diaSourceTable=df 

145 ) 

146 

147 def computeBBoxSizes(self, inputCatalog): 

148 """Compute the size of a square bbox that fully contains the detection 

149 footprint. 

150 

151 Parameters 

152 ---------- 

153 inputCatalog : `lsst.afw.table.SourceCatalog` 

154 Catalog containing detected footprints. 

155 

156 Returns 

157 ------- 

158 outputBBoxSizes : `numpy.ndarray`, (N,) 

159 Array of bbox sizes. 

160 """ 

161 outputBBoxSizes = np.empty(len(inputCatalog), dtype=int) 

162 for idx, record in enumerate(inputCatalog): 

163 footprintBBox = record.getFootprint().getBBox() 

164 # Compute twice the size of the largest dimension of the footprint 

165 # bounding box. This is the largest footprint we should need to cover 

166 # the complete DiaSource assuming the centroid is withing the bounding 

167 # box. 

168 maxSize = 2 * np.max([footprintBBox.getWidth(), 

169 footprintBBox.getHeight()]) 

170 recX = record.getCentroid().x 

171 recY = record.getCentroid().y 

172 bboxSize = int( 

173 np.ceil(2 * np.max(np.fabs([footprintBBox.maxX - recX, 

174 footprintBBox.minX - recX, 

175 footprintBBox.maxY - recY, 

176 footprintBBox.minY - recY])))) 

177 if bboxSize > maxSize: 

178 bboxSize = maxSize 

179 outputBBoxSizes[idx] = bboxSize 

180 

181 return outputBBoxSizes