1 from __future__
import absolute_import, division
31 from .apCorrRegistry
import getApCorrNameSet
37 UseNaiveFluxSigma =
True
39 __all__ = (
"ApplyApCorrConfig",
"ApplyApCorrTask")
42 """!Catalog field names and keys needed to aperture correct a particular flux
45 """!Construct an ApCorrInfo and add fields to the schema
47 @param[in,out] schema source catalog schema;
48 three fields are used to generate keys:
52 three fields are added:
56 @param[in] name field name prefix for flux needing aperture correction, e.g. "base_PsfFlux"
58 ApCorrInfo has the following attributes:
59 - name: field name prefix for flux needing aperture correction
60 - fluxName: name of flux field
61 - fluxSigmaName: name of flux sigma field
62 - fluxKey: key to flux field
63 - fluxSigmaKey: key to flux sigma field
64 - fluxFlagKey: key to flux flag field
65 - apCorrKey: key to new aperture correction field
66 - apCorrSigmaKey: key to new aperture correction sigma field
67 - apCorrFlagKey: key to new aperture correction flag field
77 doc =
"aperture correction applied to %s" % (name,),
81 name +
"_apCorrSigma",
82 doc =
"aperture correction applied to %s" % (name,),
86 name +
"_flag_apCorr",
87 doc =
"set if unable to aperture correct %s" % (name,),
92 ignoreList = lsst.pex.config.ListField(
93 doc =
"flux measurement algorithms in getApCorrNameSet() to ignore;" +
94 " if a name is listed that does not appear in getApCorrNameSet() then a warning is logged",
99 doFlagApCorrFailures = lsst.pex.config.Field(
100 doc =
"set the general failure flag for a flux when it cannot be aperture-corrected?",
107 """!Apply aperture corrections
109 ConfigClass = ApplyApCorrConfig
110 _DefaultName =
"applyApCorr"
113 """Construct an instance of this task
115 lsst.pipe.base.Task.__init__(self, **kwds)
119 ignoreSet = set(self.config.ignoreList)
120 missingNameSet = ignoreSet - set(apCorrNameSet)
122 self.log.warn(
"Fields in ignoreList that are not in fluxCorrectList: %s" %
123 (sorted(list(missingNameSet)),))
124 for name
in apCorrNameSet - ignoreSet:
125 if name +
"_flux" in schema:
131 def run(self, catalog, apCorrMap):
132 """Apply aperture corrections to a catalog of sources
134 @param[in,out] catalog catalog of sources
135 @param[in] apCorrMap aperture correction map (an lsst.afw.image.ApCorrMap)
137 If you show debug-level log messages then you will see statistics for the effects of
140 self.log.info(
"Applying aperture corrections to %d flux fields" % (len(self.
apCorrInfoDict),))
141 if UseNaiveFluxSigma:
142 self.log.info(
"Use naive flux sigma computation")
144 self.log.info(
"Use complex flux sigma computation that double-counts photon noise "
145 " and thus over-estimates flux uncertainty")
146 for apCorrInfo
in self.apCorrInfoDict.itervalues():
147 apCorrModel = apCorrMap.get(apCorrInfo.fluxName)
148 apCorrSigmaModel = apCorrMap.get(apCorrInfo.fluxSigmaName)
149 if None in (apCorrModel, apCorrSigmaModel):
150 missingNames = [(apCorrInfo.fluxName, apCorrInfo.fluxSigmaName)[i]
151 for i, model
in enumerate((apCorrModel, apCorrSigmaModel))
if model
is None]
152 self.log.warn(
"Could not find %s in apCorrMap" % (
" or ".join(missingNames),))
153 for source
in catalog:
154 source.set(apCorrInfo.apCorrFlagKey,
True)
157 for source
in catalog:
158 center = source.getCentroid()
160 source.set(apCorrInfo.apCorrFlagKey,
True)
161 oldFluxFlagState =
False
162 if self.config.doFlagApCorrFailures:
163 oldFluxFlagState = source.get(apCorrInfo.fluxFlagKey)
164 source.set(apCorrInfo.fluxFlagKey,
True)
169 apCorr = apCorrModel.evaluate(center)
170 if not UseNaiveFluxSigma:
171 apCorrSigma = apCorrSigmaModel.evaluate(center)
172 except lsst.pex.exceptions.DomainError:
175 source.set(apCorrInfo.apCorrKey, apCorr)
176 source.set(apCorrInfo.apCorrSigmaKey, apCorrSigma)
177 if apCorr <= 0.0
or apCorrSigma < 0.0:
180 flux = source.get(apCorrInfo.fluxKey)
181 fluxSigma = source.get(apCorrInfo.fluxSigmaKey)
182 source.set(apCorrInfo.fluxKey, flux*apCorr)
183 if UseNaiveFluxSigma:
184 source.set(apCorrInfo.fluxSigmaKey, fluxSigma*apCorr)
187 b = apCorrSigma/apCorr
188 source.set(apCorrInfo.fluxSigmaKey, abs(flux*apCorr)*math.sqrt(a*a + b*b))
189 source.set(apCorrInfo.apCorrFlagKey,
False)
190 if self.config.doFlagApCorrFailures:
191 source.set(apCorrInfo.fluxFlagKey, oldFluxFlagState)
193 if self.log.getThreshold() <= self.log.DEBUG:
195 apCorrArr = numpy.array([s.get(apCorrInfo.apCorrKey)
for s
in catalog])
196 apCorrSigmaArr = numpy.array([s.get(apCorrInfo.apCorrSigmaKey)
for s
in catalog])
197 self.log.logdebug(
"For flux field %r: mean apCorr=%s, stdDev apCorr=%s,"
198 " mean apCorrSigma=%s, stdDev apCorrSigma=%s for %s sources" %
199 (apCorrInfo.name, apCorrArr.mean(), apCorrArr.std(),
200 apCorrSigmaArr.mean(), apCorrSigmaArr.std(), len(catalog)))
Apply aperture corrections.
def __init__
Construct an ApCorrInfo and add fields to the schema.
def getApCorrNameSet
Return a copy of the set of field name prefixes for fluxes that should be aperture corrected...
Catalog field names and keys needed to aperture correct a particular flux.