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# 

2# Developed for the LSST Data Management System. 

3# This product includes software developed by the LSST Project 

4# (http://www.lsst.org). 

5# See the COPYRIGHT file at the top-level directory of this distribution 

6# for details of code ownership. 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the GNU General Public License 

19# along with this program. If not, see <http://www.gnu.org/licenses/>. 

20# 

21 

22"""Classes for taking science pipeline outputs and creating data products for 

23use in ap_association and the alert production database (APDB). 

24""" 

25 

26__all__ = ["MapApDataConfig", "MapApDataTask", 

27 "MapDiaSourceConfig", "MapDiaSourceTask", 

28 "UnpackApdbFlags"] 

29 

30import numpy as np 

31import os 

32import yaml 

33 

34import lsst.afw.table as afwTable 

35from lsst.daf.base import DateTime 

36import lsst.pipe.base as pipeBase 

37import lsst.pex.config as pexConfig 

38from lsst.pex.exceptions import RuntimeError 

39from lsst.utils import getPackageDir 

40from .afwUtils import make_dia_source_schema 

41 

42 

43class MapApDataConfig(pexConfig.Config): 

44 """Configuration for the generic MapApDataTask class. 

45 """ 

46 copyColumns = pexConfig.DictField( 

47 keytype=str, 

48 itemtype=str, 

49 doc="Mapping of input SciencePipelines columns to output DPDD " 

50 "columns.", 

51 default={"id": "id", 

52 "parent": "parent", 

53 "coord_ra": "coord_ra", 

54 "coord_dec": "coord_dec"} 

55 ) 

56 

57 

58class MapApDataTask(pipeBase.Task): 

59 """Generic mapper class for copying values from a science pipelines catalog 

60 into a product for use in ap_association or the APDB. 

61 """ 

62 ConfigClass = MapApDataConfig 

63 _DefaultName = "mapApDataTask" 

64 

65 def __init__(self, inputSchema, outputSchema, **kwargs): 

66 pipeBase.Task.__init__(self, **kwargs) 

67 self.inputSchema = inputSchema 

68 self.outputSchema = outputSchema 

69 

70 self.mapper = afwTable.SchemaMapper(inputSchema, outputSchema) 

71 

72 for inputName, outputName in self.config.copyColumns.items(): 

73 self.mapper.addMapping( 

74 self.inputSchema.find(inputName).key, 

75 outputName, 

76 True) 

77 

78 def run(self, inputCatalog, exposure=None): 

79 """Copy data from the inputCatalog into an output catalog with 

80 requested columns. 

81 

82 Parameters 

83 ---------- 

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

85 Input catalog with data to be copied into new output catalog. 

86 

87 Returns 

88 ------- 

89 outputCatalog: `lsst.afw.table.SourceCatalog` 

90 Output catalog with data copied from input and new column names. 

91 """ 

92 outputCatalog = afwTable.SourceCatalog(self.outputSchema) 

93 outputCatalog.extend(inputCatalog, self.mapper) 

94 

95 if not outputCatalog.isContiguous(): 

96 raise RuntimeError("Output catalogs must be contiguous.") 

97 

98 return outputCatalog 

99 

100 

101class MapDiaSourceConfig(pexConfig.Config): 

102 """Config for the DiaSourceMapperTask 

103 """ 

104 copyColumns = pexConfig.DictField( 

105 keytype=str, 

106 itemtype=str, 

107 doc="Mapping of input SciencePipelines columns to output DPDD " 

108 "columns.", 

109 default={"id": "id", 

110 "parent": "parent", 

111 "coord_ra": "coord_ra", 

112 "coord_dec": "coord_dec", 

113 "slot_Centroid_x": "x", 

114 "slot_Centroid_xErr": "xErr", 

115 "slot_Centroid_y": "y", 

116 "slot_Centroid_yErr": "yErr", 

117 "slot_ApFlux_instFlux": "apFlux", 

118 "slot_ApFlux_instFluxErr": "apFluxErr", 

119 "slot_PsfFlux_instFlux": "psFlux", 

120 "slot_PsfFlux_instFluxErr": "psFluxErr", 

121 "ip_diffim_DipoleFit_orientation": "dipAngle", 

122 "ip_diffim_DipoleFit_chi2dof": "dipChi2", 

123 "ip_diffim_forced_PsfFlux_instFlux": "totFlux", 

124 "ip_diffim_forced_PsfFlux_instFluxErr": "totFluxErr", 

125 "ip_diffim_DipoleFit_flag_classification": "isDipole", 

126 "slot_Shape_xx": "ixx", 

127 "slot_Shape_xxErr": "ixxErr", 

128 "slot_Shape_yy": "iyy", 

129 "slot_Shape_yyErr": "iyyErr", 

130 "slot_Shape_xy": "ixy", 

131 "slot_Shape_xyErr": "ixyErr", 

132 "slot_PsfShape_xx": "ixxPSF", 

133 "slot_PsfShape_yy": "iyyPSF", 

134 "slot_PsfShape_xy": "ixyPSF"} 

135 ) 

136 calibrateColumns = pexConfig.ListField( 

137 dtype=str, 

138 doc="Flux columns in the input catalog to calibrate.", 

139 default=["slot_ApFlux", "slot_PsfFlux", "ip_diffim_forced_PsfFlux"] 

140 ) 

141 flagMap = pexConfig.Field( 

142 dtype=str, 

143 doc="Yaml file specifying SciencePipelines flag fields to bit packs.", 

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

145 "data", 

146 "association-flag-map.yaml"), 

147 ) 

148 dipFluxPrefix = pexConfig.Field( 

149 dtype=str, 

150 doc="Prefix of the Dipole measurement column containing negative and " 

151 "positive flux lobes.", 

152 default="ip_diffim_DipoleFit", 

153 ) 

154 dipSepColumn = pexConfig.Field( 

155 dtype=str, 

156 doc="Column of the separation of the negative and positive poles of " 

157 "the dipole.", 

158 default="ip_diffim_DipoleFit_separation" 

159 ) 

160 

161 

162class MapDiaSourceTask(MapApDataTask): 

163 """Task specific for copying columns from science pipelines catalogs, 

164 calibrating them, for use in ap_association and the APDB. 

165 

166 This task also copies information from the exposure such as the ExpsoureId 

167 and the exposure date as specified in the DPDD. 

168 """ 

169 

170 ConfigClass = MapDiaSourceConfig 

171 _DefaultName = "mapDiaSourceTask" 

172 

173 def __init__(self, inputSchema, **kwargs): 

174 MapApDataTask.__init__(self, 

175 inputSchema=inputSchema, 

176 outputSchema=make_dia_source_schema(), 

177 **kwargs) 

178 self._create_bit_pack_mappings() 

179 

180 def _create_bit_pack_mappings(self): 

181 """Setup all flag bit packings. 

182 """ 

183 self.bit_pack_columns = [] 

184 with open(self.config.flagMap) as yaml_stream: 

185 table_list = list(yaml.safe_load_all(yaml_stream)) 

186 for table in table_list: 

187 if table['tableName'] == 'DiaSource': 

188 self.bit_pack_columns = table['columns'] 

189 break 

190 

191 # Test that all flags requested are present in both the input and 

192 # output schemas. 

193 for outputFlag in self.bit_pack_columns: 

194 try: 

195 self.outputSchema.find(outputFlag['columnName']) 

196 except KeyError: 

197 raise KeyError( 

198 "Requested column %s not found in MapDiaSourceTask output " 

199 "schema. Please check that the requested output column " 

200 "exists." % outputFlag['columnName']) 

201 bitList = outputFlag['bitList'] 

202 for bit in bitList: 

203 try: 

204 self.inputSchema.find(bit['name']) 

205 except KeyError: 

206 raise KeyError( 

207 "Requested column %s not found in MapDiaSourceTask input " 

208 "schema. Please check that the requested input column " 

209 "exists." % outputFlag['columnName']) 

210 

211 def run(self, inputCatalog, exposure, return_pandas=False): 

212 """Copy data from the inputCatalog into an output catalog with 

213 requested columns. 

214 

215 Parameters 

216 ---------- 

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

218 Input catalog with data to be copied into new output catalog. 

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

220 Exposure with containing the PhotoCalib object relevant to this 

221 catalog. 

222 return_pandas : `bool` 

223 Return `pandas.DataFrame` instead of `lsst.afw.table.SourceCatalog` 

224 

225 Returns 

226 ------- 

227 outputCatalog: `lsst.afw.table.SourceCatalog` or `pandas.DataFrame` 

228 Output catalog with data copied from input and new column names. 

229 """ 

230 visit_info = exposure.getInfo().getVisitInfo() 

231 ccdVisitId = visit_info.getExposureId() 

232 midPointTaiMJD = visit_info.getDate().get(system=DateTime.MJD) 

233 filterId = exposure.getFilter().getId() 

234 filterName = exposure.getFilter().getName() 

235 wcs = exposure.getWcs() 

236 

237 photoCalib = exposure.getPhotoCalib() 

238 

239 outputCatalog = afwTable.SourceCatalog(self.outputSchema) 

240 outputCatalog.reserve(len(inputCatalog)) 

241 

242 for inputRecord in inputCatalog: 

243 outputRecord = outputCatalog.addNew() 

244 outputRecord.assign(inputRecord, self.mapper) 

245 self.calibrateFluxes(inputRecord, outputRecord, photoCalib) 

246 self.computeDipoleFluxes(inputRecord, outputRecord, photoCalib) 

247 self.computeDipoleSep(inputRecord, outputRecord, wcs) 

248 self.bitPackFlags(inputRecord, outputRecord) 

249 outputRecord.set("ccdVisitId", ccdVisitId) 

250 outputRecord.set("midPointTai", midPointTaiMJD) 

251 outputRecord.set("filterId", filterId) 

252 outputRecord.set("filterName", filterName) 

253 

254 if not outputCatalog.isContiguous(): 

255 raise RuntimeError("Output catalogs must be contiguous.") 

256 

257 if return_pandas: 

258 return self._convert_to_pandas(outputCatalog) 

259 return outputCatalog 

260 

261 def calibrateFluxes(self, inputRecord, outputRecord, photoCalib): 

262 """Copy flux values into an output record and calibrate them. 

263 

264 Parameters 

265 ---------- 

266 inputRecord : `lsst.afw.table.SourceRecord` 

267 Record to copy flux values from. 

268 outputRecord : `lsst.afw.table.SourceRecord` 

269 Record to copy and calibrate values into. 

270 photoCalib : `lsst.afw.image.PhotoCalib` 

271 Calibration object from the difference exposure. 

272 """ 

273 for col_name in self.config.calibrateColumns: 

274 meas = photoCalib.instFluxToNanojansky(inputRecord, col_name) 

275 outputRecord.set(self.config.copyColumns[col_name + "_instFlux"], 

276 meas.value) 

277 outputRecord.set( 

278 self.config.copyColumns[col_name + "_instFluxErr"], 

279 meas.error) 

280 

281 def computeDipoleFluxes(self, inputRecord, outputRecord, photoCalib): 

282 """Calibrate and compute dipole mean flux and diff flux. 

283 

284 Parameters 

285 ---------- 

286 inputRecord : `lsst.afw.table.SourceRecord` 

287 Record to copy flux values from. 

288 outputRecord : `lsst.afw.table.SourceRecord` 

289 Record to copy and calibrate values into. 

290 photoCalib `lsst.afw.image.PhotoCalib` 

291 Calibration object from the difference exposure. 

292 """ 

293 

294 neg_meas = photoCalib.instFluxToNanojansky( 

295 inputRecord, self.config.dipFluxPrefix + "_neg") 

296 pos_meas = photoCalib.instFluxToNanojansky( 

297 inputRecord, self.config.dipFluxPrefix + "_pos") 

298 outputRecord.set( 

299 "dipMeanFlux", 

300 0.5 * (np.abs(neg_meas.value) + np.abs(pos_meas.value))) 

301 outputRecord.set( 

302 "dipMeanFluxErr", 

303 0.5 * np.sqrt(neg_meas.error ** 2 + pos_meas.error ** 2)) 

304 outputRecord.set( 

305 "dipFluxDiff", 

306 np.abs(pos_meas.value) - np.abs(neg_meas.value)) 

307 outputRecord.set( 

308 "dipFluxDiffErr", 

309 np.sqrt(neg_meas.error ** 2 + pos_meas.error ** 2)) 

310 

311 def computeDipoleSep(self, inputRecord, outputRecord, wcs): 

312 """Convert the dipole separation from pixels to arcseconds. 

313 

314 Parameters 

315 ---------- 

316 inputRecord : `lsst.afw.table.SourceRecord` 

317 Record to copy flux values from. 

318 outputRecord : `lsst.afw.table.SourceRecord` 

319 Record to copy and calibrate values into. 

320 wcs : `lsst.afw.geom.SkyWcs` 

321 Wcs of image inputRecords was observed. 

322 """ 

323 pixScale = wcs.getPixelScale(inputRecord.getCentroid()) 

324 dipSep = pixScale * inputRecord.get(self.config.dipSepColumn) 

325 outputRecord.set("dipLength", dipSep.asArcseconds()) 

326 

327 def bitPackFlags(self, inputRecord, outputRecord): 

328 """Pack requested flag columns in inputRecord into single columns in 

329 outputRecord. 

330 

331 Parameters 

332 ---------- 

333 inputRecord : `lsst.afw.table.SourceRecord` 

334 Record to copy flux values from. 

335 outputRecord : `lsst.afw.table.SourceRecord` 

336 Record to copy and calibrate values into. 

337 """ 

338 for outputFlag in self.bit_pack_columns: 

339 bitList = outputFlag['bitList'] 

340 value = 0 

341 for bit in bitList: 

342 value += inputRecord[bit['name']] * 2 ** bit['bit'] 

343 outputRecord.set(outputFlag['columnName'], value) 

344 

345 def _convert_to_pandas(self, inputCatalog): 

346 """Convert input afw table to pandas. 

347 

348 Using afwTable.toAstropy().to_pandas() alone is not sufficient to 

349 properly store data in the Apdb. We must also convert the RA/DEC values 

350 from radians to degrees and rename several columns. 

351 

352 Parameters 

353 ---------- 

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

355 Catalog to convert to panads and rename columns. 

356 

357 Returns 

358 ------- 

359 catalog : `pandas.DataFrame` 

360 """ 

361 catalog = inputCatalog.asAstropy().to_pandas() 

362 catalog.rename(columns={"coord_ra": "ra", 

363 "coord_dec": "decl", 

364 "id": "diaSourceId", 

365 "parent": "parentDiaSourceId"}, 

366 inplace=True) 

367 catalog["ra"] = np.degrees(catalog["ra"]) 

368 catalog["decl"] = np.degrees(catalog["decl"]) 

369 

370 return catalog 

371 

372 

373class UnpackApdbFlags: 

374 """Class for unpacking bits from integer flag fields stored in the Apdb. 

375 

376 Attributes 

377 ---------- 

378 flag_map_file : `str` 

379 Absolute or relative path to a yaml file specifiying mappings of flags 

380 to integer bits. 

381 table_name : `str` 

382 Name of the Apdb table the integer bit data are coming from. 

383 """ 

384 

385 def __init__(self, flag_map_file, table_name): 

386 self.bit_pack_columns = [] 

387 with open(flag_map_file) as yaml_stream: 

388 table_list = list(yaml.safe_load_all(yaml_stream)) 

389 for table in table_list: 

390 if table['tableName'] == table_name: 

391 self.bit_pack_columns = table['columns'] 

392 break 

393 

394 self.output_flag_columns = {} 

395 

396 for column in self.bit_pack_columns: 

397 names = [] 

398 for bit in column["bitList"]: 

399 names.append((bit["name"], np.bool)) 

400 self.output_flag_columns[column["columnName"]] = names 

401 

402 def unpack(self, input_flag_values, flag_name): 

403 """Determine individual boolean flags from an input array of unsigned 

404 ints. 

405 

406 Parameters 

407 ---------- 

408 input_flag_values : array-like of type uint 

409 Input integer flags to unpack. 

410 flag_name : `str` 

411 Apdb column name of integer flags to unpack. Names of packed int 

412 flags are given by the flag_map_file. 

413 

414 Returns 

415 ------- 

416 output_flags : `numpy.ndarray` 

417 Numpy named tuple of booleans. 

418 """ 

419 bit_names_types = self.output_flag_columns[flag_name] 

420 output_flags = np.zeros(len(input_flag_values), dtype=bit_names_types) 

421 

422 for bit_idx, (bit_name, dtypes) in enumerate(bit_names_types): 

423 masked_bits = np.bitwise_and(input_flag_values, 2 ** bit_idx) 

424 output_flags[bit_name] = masked_bits 

425 

426 return output_flags