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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

# This file is part of ap_association. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

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

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

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

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

 

import numpy as np 

import os 

import unittest 

 

from lsst.ap.association import ( 

MapApDataConfig, 

MapApDataTask, 

MapDiaSourceConfig, 

MapDiaSourceTask, 

UnpackPpdbFlags) 

from lsst.afw.cameraGeom.testUtils import DetectorWrapper 

import lsst.afw.table as afwTable 

import lsst.daf.base as dafBase 

import lsst.afw.geom as afwGeom 

import lsst.afw.image as afwImage 

import lsst.afw.image.utils as afwImageUtils 

import lsst.geom as geom 

from lsst.utils import getPackageDir 

import lsst.utils.tests 

 

 

def make_input_source_catalog(n_objects, add_flags=False): 

"""Create tests objects to map into apData products. 

 

Parameters 

---------- 

n_objects: `int` 

Number of objects to create. 

""" 

schema = afwTable.SourceTable.makeMinimalSchema() 

schema.addField("base_NaiveCentroid_x", type="D") 

schema.addField("base_NaiveCentroid_y", type="D") 

schema.addField("base_PsfFlux_instFlux", type="D") 

schema.addField("base_PsfFlux_instFluxErr", type="D") 

if add_flags: 

schema.addField("base_PixelFlags_flag", type="Flag") 

schema.addField("base_PixelFlags_flag_offimage", type="Flag") 

 

objects = afwTable.SourceCatalog(schema) 

objects.preallocate(n_objects) 

objects.definePsfFlux("base_PsfFlux") 

objects.defineCentroid("base_NaiveCentroid") 

 

for obj_idx in range(n_objects): 

obj = objects.addNew() 

for subSchema in schema: 

if isinstance(obj.get(subSchema.getKey()), geom.Angle): 

obj.set(subSchema.getKey(), 1. * geom.degrees) 

else: 

obj.set(subSchema.getKey(), 1) 

return objects 

 

 

class TestAPDataMapperTask(unittest.TestCase): 

 

def setUp(self): 

# CFHT Filters from the camera mapper. 

afwImageUtils.resetFilters() 

afwImageUtils.defineFilter('u', lambdaEff=374, alias="u.MP9301") 

afwImageUtils.defineFilter('g', lambdaEff=487, alias="g.MP9401") 

afwImageUtils.defineFilter('r', lambdaEff=628, alias="r.MP9601") 

afwImageUtils.defineFilter('i', lambdaEff=778, alias="i.MP9701") 

afwImageUtils.defineFilter('z', lambdaEff=1170, alias="z.MP9801") 

 

self.metadata = dafBase.PropertySet() 

 

self.metadata.set("SIMPLE", "T") 

self.metadata.set("BITPIX", -32) 

self.metadata.set("NAXIS", 2) 

self.metadata.set("NAXIS1", 1024) 

self.metadata.set("NAXIS2", 1153) 

self.metadata.set("RADECSYS", 'FK5') 

self.metadata.set("EQUINOX", 2000.) 

 

self.metadata.setDouble("CRVAL1", 215.604025685476) 

self.metadata.setDouble("CRVAL2", 53.1595451514076) 

self.metadata.setDouble("CRPIX1", 1109.99981456774) 

self.metadata.setDouble("CRPIX2", 560.018167811613) 

self.metadata.set("CTYPE1", 'RA---SIN') 

self.metadata.set("CTYPE2", 'DEC--SIN') 

 

self.metadata.setDouble("CD1_1", 5.10808596133527E-05) 

self.metadata.setDouble("CD1_2", 1.85579539217196E-07) 

self.metadata.setDouble("CD2_2", -5.10281493481982E-05) 

self.metadata.setDouble("CD2_1", -8.27440751733828E-07) 

 

self.wcs = afwGeom.makeSkyWcs(self.metadata) 

self.exposure = afwImage.makeExposure( 

afwImage.makeMaskedImageFromArrays(np.ones((1024, 1153))), 

self.wcs) 

detector = DetectorWrapper(id=23, bbox=self.exposure.getBBox()).detector 

visit = afwImage.VisitInfo( 

exposureId=4321, 

exposureTime=200., 

date=dafBase.DateTime(nsecs=1400000000 * 10**9)) 

self.exposure.setDetector(detector) 

self.exposure.getInfo().setVisitInfo(visit) 

self.exposure.setFilter(afwImage.Filter('g')) 

scale = 2 

scaleErr = 1 

self.photoCalib = afwImage.PhotoCalib(scale, scaleErr) 

self.exposure.setPhotoCalib(self.photoCalib) 

 

self.inputCatalogNoFlags = make_input_source_catalog(10, False) 

self.inputCatalog = make_input_source_catalog(10, True) 

 

def test_run(self): 

"""Test the generic data product mapper. 

""" 

outSchema = afwTable.SourceTable.makeMinimalSchema() 

outSchema.addField("psFlux", type="D") 

outSchema.addField("psFluxErr", type="D") 

 

mapApDConfig = MapApDataConfig() 

mapApDConfig.copyColumns = { 

"id": "id", 

"parent": "parent", 

"coord_ra": "coord_ra", 

"coord_dec": "coord_dec", 

"slot_PsfFlux_instFlux": "psFlux", 

"slot_PsfFlux_instFluxErr": "psFluxErr" 

} 

 

mapApD = MapApDataTask(inputSchema=self.inputCatalog.schema, 

outputSchema=outSchema, 

config=mapApDConfig) 

outputCatalog = mapApD.run(self.inputCatalog) 

 

for inObj, outObj in zip(self.inputCatalog, outputCatalog): 

for inputName, outputName in mapApDConfig.copyColumns.items(): 

self.assertEqual(inObj[inputName], outObj[outputName]) 

 

def test_run_dia_source(self): 

"""Test the DiaSource specific data product mapper/calibrator. 

""" 

mapApDConfig = self._create_map_dia_source_config() 

mapApD = MapDiaSourceTask(inputSchema=self.inputCatalog.schema, 

config=mapApDConfig) 

outputCatalog = mapApD.run(self.inputCatalog, self.exposure) 

 

for inObj, outObj in zip(self.inputCatalog, outputCatalog): 

self.assertEqual( 

outObj["ccdVisitId"], 

self.exposure.getInfo().getVisitInfo().getExposureId()) 

self.assertEqual( 

outObj["midPointTai"], 

self.exposure.getInfo().getVisitInfo().getDate().get( 

system=dafBase.DateTime.MJD)) 

self.assertEqual( 

outObj["flags"], 

1 * 2 ** 0 + 1 * 2 ** 1) 

for inputName, outputName in mapApDConfig.copyColumns.items(): 

if inputName.startswith("slot"): 

self._test_calibrated_flux(inObj, outObj) 

else: 

self.assertEqual(inObj[inputName], outObj[outputName]) 

 

def _create_map_dia_source_config(self): 

"""Create a test config for use in MapDiaSourceTask. 

 

Returns 

------- 

configurable : `lsst.pex.config.Config` 

Configurable for use in MapDiaSourceTask. 

""" 

configurable = MapDiaSourceConfig() 

configurable.copyColumns = { 

"id": "id", 

"parent": "parent", 

"coord_ra": "coord_ra", 

"coord_dec": "coord_dec", 

"slot_PsfFlux_instFlux": "psFlux", 

"slot_PsfFlux_instFluxErr": "psFluxErr" 

} 

configurable.calibrateColumns = ["slot_PsfFlux"] 

configurable.flagMap = os.path.join( 

getPackageDir("ap_association"), 

"tests", 

"test-flag-map.yaml") 

 

return configurable 

 

def test_run_dia_source_wrong_flags(self): 

"""Test that the proper errors are thrown when requesting flag columns 

that are not in the input schema. 

""" 

mapApDConfig = self._create_map_dia_source_config() 

with self.assertRaises(KeyError): 

MapDiaSourceTask(inputSchema=self.inputCatalogNoFlags.schema, 

config=mapApDConfig) 

 

def test_calibrateFluxes(self): 

"""Test that flux calibration works as expected. 

""" 

outSchema = afwTable.SourceTable.makeMinimalSchema() 

outSchema.addField("psFlux", type="D") 

outSchema.addField("psFluxErr", type="D") 

 

outputCatalog = afwTable.SourceCatalog(outSchema) 

outRecord = outputCatalog.addNew() 

 

mapApDConfig = self._create_map_dia_source_config() 

mapApD = MapDiaSourceTask(inputSchema=self.inputCatalog.schema, 

config=mapApDConfig) 

 

mapApD.calibrateFluxes(self.inputCatalog[0], 

outRecord, 

self.photoCalib) 

self._test_calibrated_flux(self.inputCatalog[0], outRecord) 

 

def _test_calibrated_flux(self, inputRecord, outputRecord): 

"""Compare calibrated fluxes to expectation from zero point. 

 

Parameters 

---------- 

inputRecord: `lsst.afw.table.SourceRecord` 

Input source record with uncalibrated flux values. 

outputRecord: `lsst.afw.table.SourceRecord` 

Source record with calibrated fluxes. 

""" 

expected = self.photoCalib.instFluxToNanojansky(inputRecord["slot_PsfFlux_instFlux"], 

inputRecord["slot_PsfFlux_instFluxErr"]) 

self.assertAlmostEqual(outputRecord["psFlux"], expected.value) 

self.assertAlmostEqual(outputRecord["psFluxErr"], expected.error) 

 

def test_bit_unpacker(self): 

"""Test that the integer bit packer is functioning correctly. 

""" 

mapApDConfig = self._create_map_dia_source_config() 

mapApD = MapDiaSourceTask(inputSchema=self.inputCatalog.schema, 

config=mapApDConfig) 

for idx, obj in enumerate(self.inputCatalog): 

if idx in [1, 3, 5]: 

obj.set("base_PixelFlags_flag", 0) 

if idx in [1, 4, 6]: 

obj.set("base_PixelFlags_flag_offimage", 0) 

outputCatalog = mapApD.run(self.inputCatalog, self.exposure) 

 

unpacker = UnpackPpdbFlags(mapApDConfig.flagMap, "DiaSource") 

flag_values = unpacker.unpack(outputCatalog.get("flags"), "flags") 

 

for idx, flag in enumerate(flag_values): 

if idx in [1, 3, 5]: 

self.assertFalse(flag['base_PixelFlags_flag']) 

else: 

self.assertTrue(flag['base_PixelFlags_flag']) 

 

if idx in [1, 4, 6]: 

self.assertFalse(flag['base_PixelFlags_flag_offimage']) 

else: 

self.assertTrue(flag['base_PixelFlags_flag_offimage']) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

283 ↛ 284line 283 didn't jump to line 284, because the condition on line 283 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()