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

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

# This file is part of meas_base. 

# 

# 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 math 

 

import numpy as np 

 

import lsst.pex.config 

import lsst.pex.exceptions 

import lsst.afw.image 

import lsst.pipe.base 

from .apCorrRegistry import getApCorrNameSet 

 

# If True then scale instFlux error by apCorr; if False then use a more complex computation 

# that over-estimates instFlux error (often grossly so) because it double-counts photon noise. 

# This flag is intended to be temporary until we figure out a better way to compute 

# the effects of aperture correction on instFlux uncertainty 

UseNaiveFluxErr = True 

 

__all__ = ("ApplyApCorrConfig", "ApplyApCorrTask") 

 

 

class ApCorrInfo: 

"""Catalog field names and keys needed to aperture correct a particular 

instrument flux. 

 

Parameters 

---------- 

schema : `lsst.afw.table` 

Source catalog schema. Three fields are used to generate keys: 

- ``{name}_instFlux`` 

- ``{name}_instFluxErr`` 

- ``{name}_flag`` 

Three fields are added: 

- ``{name}_apCorr`` (only if not already added by proxy) 

- ``{name}_apCorrErr`` (only if not already added by proxy) 

- ``{name}_flag_apCorr`` 

model : `str` 

Field name prefix for instFlux with aperture correction model, e.g. 

"base_PsfFlux" 

name : `str` 

Field name prefix for instFlux needing aperture correction; may be 

`None` if it is the same as ``model`` 

 

Notes 

----- 

The aperture correction can be derived from the meaasurements in the 

column being aperture-corrected or from measurements in a different 

column (a "proxy"). In the first case, we will add columns to contain 

the aperture correction values; in the second case (using a proxy), 

we will add an alias to the proxy's aperture correction values. In 

all cases, we add a flag. 

""" 

 

name = None 

"""Field name prefix for flux needing aperture correction (`str`). 

""" 

 

modelName = None 

"""Field name for aperture correction model for flux (`str`). 

""" 

 

modelSigmaName = None 

"""Field name for aperture correction model for fluxErr (`str`). 

""" 

 

doApCorrColumn = None 

"""Should we write the aperture correction values (`bool`)? 

 

They should not be written if they're already being written by a proxy. 

""" 

 

instFluxName = None 

"""Name of ``instFlux`` field (`str`). 

""" 

 

instFluxErrName = None 

"""Name of ``instFlux`` sigma field (`str`). 

""" 

 

instFluxKey = None 

"""Key to ``instFlux`` field (`lsst.afw.table.schema.Key`). 

""" 

 

instFluxErrKey = None 

"""Key to ``instFlux`` sigma field (`lsst.afw.table.schema.Key`). 

""" 

 

fluxFlagKey = None 

"""Key to the flux flag field (`lsst.afw.table.schema.Key`). 

""" 

 

apCorrKey = None 

"""Key to new aperture correction field (`lsst.afw.table.schema.Key`). 

""" 

 

apCorrErrKey = None 

"""Key to new aperture correction sigma field (`lsst.afw.table.schema.Key`). 

""" 

 

apCorrFlagKey = None 

"""Key to new aperture correction flag field (`lsst.afw.table.schema.Key`). 

""" 

 

def __init__(self, schema, model, name=None): 

if name is None: 

name = model 

self.name = name 

self.modelName = model + "_instFlux" 

self.modelSigmaName = model + "_instFluxErr" 

self.instFluxName = name + "_instFlux" 

self.instFluxErrName = name + "_instFluxErr" 

self.instFluxKey = schema.find(self.instFluxName).key 

self.instFluxErrKey = schema.find(self.instFluxErrName).key 

self.fluxFlagKey = schema.find(name + "_flag").key 

 

# No need to write the same aperture corrections multiple times 

self.doApCorrColumn = (name == model or model + "_apCorr" not in schema) 

if self.doApCorrColumn: 

self.apCorrKey = schema.addField( 

name + "_apCorr", 

doc="aperture correction applied to %s" % (name,), 

type=np.float64, 

) 

self.apCorrErrKey = schema.addField( 

name + "_apCorrErr", 

doc="standard deviation of aperture correction applied to %s" % (name,), 

type=np.float64, 

) 

else: 

aliases = schema.getAliasMap() 

aliases.set(name + "_apCorr", model + "_apCorr") 

aliases.set(name + "_apCorrErr", model + "_apCorrErr") 

self.apCorrKey = schema.find(name + "_apCorr").key 

self.apCorrErrKey = schema.find(name + "_apCorrErr").key 

 

self.apCorrFlagKey = schema.addField( 

name + "_flag_apCorr", 

doc="set if unable to aperture correct %s" % (name,), 

type="Flag", 

) 

 

 

class ApplyApCorrConfig(lsst.pex.config.Config): 

"""Aperture correction configuration. 

""" 

 

ignoreList = lsst.pex.config.ListField( 

doc="flux measurement algorithms in getApCorrNameSet() to ignore; " 

"if a name is listed that does not appear in getApCorrNameSet() then a warning is logged", 

dtype=str, 

optional=False, 

default=(), 

) 

doFlagApCorrFailures = lsst.pex.config.Field( 

doc="set the general failure flag for a flux when it cannot be aperture-corrected?", 

dtype=bool, 

default=True, 

) 

proxies = lsst.pex.config.DictField( 

doc="flux measurement algorithms to be aperture-corrected by reference to another algorithm; " 

"this is a mapping alg1:alg2, where 'alg1' is the algorithm being corrected, and 'alg2' " 

"is the algorithm supplying the corrections", 

keytype=str, 

itemtype=str, 

default={}, 

) 

 

 

class ApplyApCorrTask(lsst.pipe.base.Task): 

"""Apply aperture corrections. 

 

Parameters 

---------- 

schema : `lsst.afw.table.Schema` 

""" 

ConfigClass = ApplyApCorrConfig 

_DefaultName = "applyApCorr" 

 

def __init__(self, schema, **kwds): 

lsst.pipe.base.Task.__init__(self, **kwds) 

 

self.apCorrInfoDict = dict() 

apCorrNameSet = getApCorrNameSet() 

ignoreSet = set(self.config.ignoreList) 

missingNameSet = ignoreSet - set(apCorrNameSet) 

if missingNameSet: 

self.log.warn("Fields in ignoreList that are not in fluxCorrectList: %s", 

sorted(list(missingNameSet))) 

for name in sorted(list(apCorrNameSet - ignoreSet)): 

if name + "_instFlux" not in schema: 

# if a field in the registry is missing from the schema, silently ignore it. 

continue 

self.apCorrInfoDict[name] = ApCorrInfo(schema=schema, model=name) 

 

for name, model in self.config.proxies.items(): 

if name in apCorrNameSet: 

# Already done or ignored 

continue 

if name + "_instFlux" not in schema: 

# Silently ignore 

continue 

self.apCorrInfoDict[name] = ApCorrInfo(schema=schema, model=model, name=name) 

 

def run(self, catalog, apCorrMap): 

"""Apply aperture corrections to a catalog of sources. 

 

Parameters 

---------- 

catalog : `lsst.afw.table.SourceCatalog` 

Catalog of sources. Will be updated in place. 

apCorrMap : `lsst.afw.image.ApCorrMap` 

Aperture correction map 

 

Notes 

----- 

If you show debug-level log messages then you will see statistics for 

the effects of aperture correction. 

""" 

self.log.info("Applying aperture corrections to %d instFlux fields", len(self.apCorrInfoDict)) 

if UseNaiveFluxErr: 

self.log.debug("Use naive instFlux sigma computation") 

else: 

self.log.debug("Use complex instFlux sigma computation that double-counts photon noise " 

"and thus over-estimates instFlux uncertainty") 

for apCorrInfo in self.apCorrInfoDict.values(): 

apCorrModel = apCorrMap.get(apCorrInfo.modelName) 

apCorrErrModel = apCorrMap.get(apCorrInfo.modelSigmaName) 

if None in (apCorrModel, apCorrErrModel): 

missingNames = [(apCorrInfo.modelName, apCorrInfo.modelSigmaName)[i] 

for i, model in enumerate((apCorrModel, apCorrErrModel)) if model is None] 

self.log.warn("Cannot aperture correct %s because could not find %s in apCorrMap" % 

(apCorrInfo.name, " or ".join(missingNames),)) 

for source in catalog: 

source.set(apCorrInfo.apCorrFlagKey, True) 

continue 

 

for source in catalog: 

center = source.getCentroid() 

# say we've failed when we start; we'll unset these flags when we succeed 

source.set(apCorrInfo.apCorrFlagKey, True) 

oldFluxFlagState = False 

if self.config.doFlagApCorrFailures: 

oldFluxFlagState = source.get(apCorrInfo.fluxFlagKey) 

source.set(apCorrInfo.fluxFlagKey, True) 

 

apCorr = 1.0 

apCorrErr = 0.0 

try: 

apCorr = apCorrModel.evaluate(center) 

if not UseNaiveFluxErr: 

apCorrErr = apCorrErrModel.evaluate(center) 

except lsst.pex.exceptions.DomainError: 

continue 

 

if apCorrInfo.doApCorrColumn: 

source.set(apCorrInfo.apCorrKey, apCorr) 

source.set(apCorrInfo.apCorrErrKey, apCorrErr) 

 

if apCorr <= 0.0 or apCorrErr < 0.0: 

continue 

 

instFlux = source.get(apCorrInfo.instFluxKey) 

instFluxErr = source.get(apCorrInfo.instFluxErrKey) 

source.set(apCorrInfo.instFluxKey, instFlux*apCorr) 

if UseNaiveFluxErr: 

source.set(apCorrInfo.instFluxErrKey, instFluxErr*apCorr) 

else: 

a = instFluxErr/instFlux 

b = apCorrErr/apCorr 

source.set(apCorrInfo.instFluxErrKey, abs(instFlux*apCorr)*math.sqrt(a*a + b*b)) 

source.set(apCorrInfo.apCorrFlagKey, False) 

if self.config.doFlagApCorrFailures: 

source.set(apCorrInfo.fluxFlagKey, oldFluxFlagState) 

 

if self.log.getLevel() <= self.log.DEBUG: 

# log statistics on the effects of aperture correction 

apCorrArr = np.array([s.get(apCorrInfo.apCorrKey) for s in catalog]) 

apCorrErrArr = np.array([s.get(apCorrInfo.apCorrErrKey) for s in catalog]) 

self.log.debug("For instFlux field %r: mean apCorr=%s, stdDev apCorr=%s, " 

"mean apCorrErr=%s, stdDev apCorrErr=%s for %s sources", 

apCorrInfo.name, apCorrArr.mean(), apCorrArr.std(), 

apCorrErrArr.mean(), apCorrErrArr.std(), len(catalog))