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

# 

# LSST Data Management System 

# Copyright 2015 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# 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 LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

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 flux sigma by apCorr; if False then use a more complex computation 

# that over-estimates flux 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 flux uncertainty 

UseNaiveFluxSigma = True 

 

__all__ = ("ApplyApCorrConfig", "ApplyApCorrTask") 

 

 

class ApCorrInfo: 

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

""" 

 

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

"""!Construct an ApCorrInfo and add fields to the schema 

 

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. 

 

@param[in,out] schema source catalog schema; 

three fields are used to generate keys: 

- {name}_flux 

- {name}_fluxSigma 

- {name}_flag 

three fields are added: 

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

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

- {name}_flag_apCorr 

@param[in] model field name prefix for flux with aperture correction model, e.g. "base_PsfFlux" 

@param[in] name field name prefix for flux needing aperture correction; may be None if it's the 

same as for the 'model' parameter 

 

ApCorrInfo has the following attributes: 

- name: field name prefix for flux needing aperture correction 

- modelName: field name for aperture correction model for flux 

- modelSigmaName: field name for aperture correction model for fluxSigma 

- doApCorrColumn: should we write the aperture correction values? (not if they're already being 

written by a proxy) 

- fluxName: name of flux field 

- fluxSigmaName: name of flux sigma field 

- fluxKey: key to flux field 

- fluxSigmaKey: key to flux sigma field 

- fluxFlagKey: key to flux flag field 

- apCorrKey: key to new aperture correction field 

- apCorrSigmaKey: key to new aperture correction sigma field 

- apCorrFlagKey: key to new aperture correction flag field 

""" 

if name is None: 

name = model 

self.name = name 

self.modelName = model + "_flux" 

self.modelSigmaName = model + "_fluxSigma" 

self.fluxName = name + "_flux" 

self.fluxSigmaName = name + "_fluxSigma" 

self.fluxKey = schema.find(self.fluxName).key 

self.fluxSigmaKey = schema.find(self.fluxSigmaName).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.apCorrSigmaKey = schema.addField( 

name + "_apCorrSigma", 

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

type=np.float64, 

) 

else: 

aliases = schema.getAliasMap() 

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

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

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

self.apCorrSigmaKey = schema.find(name + "_apCorrSigma").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): 

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 

""" 

ConfigClass = ApplyApCorrConfig 

_DefaultName = "applyApCorr" 

 

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

"""Construct an instance of this task 

""" 

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

 

self.apCorrInfoDict = dict() 

apCorrNameSet = getApCorrNameSet() 

ignoreSet = set(self.config.ignoreList) 

missingNameSet = ignoreSet - set(apCorrNameSet) 

159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never true if missingNameSet: 

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

sorted(list(missingNameSet))) 

for name in apCorrNameSet - ignoreSet: 

if name + "_flux" 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(): 

169 ↛ 171line 169 didn't jump to line 171, because the condition on line 169 was never true if name in apCorrNameSet: 

# Already done or ignored 

continue 

172 ↛ 174line 172 didn't jump to line 174, because the condition on line 172 was never true if name + "_flux" 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 

 

@param[in,out] catalog catalog of sources 

@param[in] apCorrMap aperture correction map (an lsst.afw.image.ApCorrMap) 

 

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 flux fields", len(self.apCorrInfoDict)) 

187 ↛ 190line 187 didn't jump to line 190, because the condition on line 187 was never false if UseNaiveFluxSigma: 

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

else: 

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

"and thus over-estimates flux uncertainty") 

for apCorrInfo in self.apCorrInfoDict.values(): 

apCorrModel = apCorrMap.get(apCorrInfo.modelName) 

apCorrSigmaModel = apCorrMap.get(apCorrInfo.modelSigmaName) 

195 ↛ 196line 195 didn't jump to line 196, because the condition on line 195 was never true if None in (apCorrModel, apCorrSigmaModel): 

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

for i, model in enumerate((apCorrModel, apCorrSigmaModel)) 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 

209 ↛ 213line 209 didn't jump to line 213, because the condition on line 209 was never false if self.config.doFlagApCorrFailures: 

oldFluxFlagState = source.get(apCorrInfo.fluxFlagKey) 

source.set(apCorrInfo.fluxFlagKey, True) 

 

apCorr = 1.0 

apCorrSigma = 0.0 

try: 

apCorr = apCorrModel.evaluate(center) 

217 ↛ 218line 217 didn't jump to line 218, because the condition on line 217 was never true if not UseNaiveFluxSigma: 

apCorrSigma = apCorrSigmaModel.evaluate(center) 

except lsst.pex.exceptions.DomainError: 

continue 

 

if apCorrInfo.doApCorrColumn: 

source.set(apCorrInfo.apCorrKey, apCorr) 

source.set(apCorrInfo.apCorrSigmaKey, apCorrSigma) 

 

226 ↛ 227line 226 didn't jump to line 227, because the condition on line 226 was never true if apCorr <= 0.0 or apCorrSigma < 0.0: 

continue 

 

flux = source.get(apCorrInfo.fluxKey) 

fluxSigma = source.get(apCorrInfo.fluxSigmaKey) 

source.set(apCorrInfo.fluxKey, flux*apCorr) 

232 ↛ 235line 232 didn't jump to line 235, because the condition on line 232 was never false if UseNaiveFluxSigma: 

source.set(apCorrInfo.fluxSigmaKey, fluxSigma*apCorr) 

else: 

a = fluxSigma/flux 

b = apCorrSigma/apCorr 

source.set(apCorrInfo.fluxSigmaKey, abs(flux*apCorr)*math.sqrt(a*a + b*b)) 

source.set(apCorrInfo.apCorrFlagKey, False) 

239 ↛ 204line 239 didn't jump to line 204, because the condition on line 239 was never false if self.config.doFlagApCorrFailures: 

source.set(apCorrInfo.fluxFlagKey, oldFluxFlagState) 

 

242 ↛ 192line 242 didn't jump to line 192, because the condition on line 242 was never false 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]) 

apCorrSigmaArr = np.array([s.get(apCorrInfo.apCorrSigmaKey) for s in catalog]) 

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

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

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

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