224 def run(self, exposure, catalog):
225 """Measure aperture correction
229 exposure : `lsst.afw.image.Exposure`
230 Exposure aperture corrections are being measured on. The
231 bounding box is retrieved from it, and it is passed to the
232 sourceSelector. The output aperture correction map is *not*
233 added to the exposure; this is left to the caller.
234 catalog : `lsst.afw.table.SourceCatalog`
235 SourceCatalog containing measurements to be used to
236 compute aperture corrections.
240 Struct : `lsst.pipe.base.Struct`
241 Contains the following:
244 aperture correction map (`lsst.afw.image.ApCorrMap`)
245 that contains two entries for each flux field:
246 - flux field (e.g. base_PsfFlux_instFlux): 2d model
247 - flux sigma field (e.g. base_PsfFlux_instFluxErr): 2d model of error
249 bbox = exposure.getBBox()
254 self.log.info(
"Measuring aperture corrections for %d flux fields", len(self.
toCorrect))
258 selected = self.sourceSelector.
run(catalog, exposure=exposure)
262 & (np.isfinite(selected.sourceCat[self.
refFluxNames.fluxName]))
264 goodRefCat = selected.sourceCat[use].copy()
269 for name, fluxNames
in self.
toCorrect.items():
272 fluxes = goodRefCat[fluxNames.fluxName]
273 with np.errstate(invalid=
"ignore"):
275 (~goodRefCat[fluxNames.flagName])
276 & (np.isfinite(fluxes))
282 if (isGood.sum() - 1) < self.config.minDegreesOfFreedom:
283 if name
in self.config.allowFailure:
284 self.log.warning(
"Unable to measure aperture correction for '%s': "
285 "only %d sources, but require at least %d.",
286 name, isGood.sum(), self.config.minDegreesOfFreedom + 1)
290 ndof=self.config.minDegreesOfFreedom + 1)
292 goodCat = goodRefCat[isGood].copy()
294 x = goodCat[
'slot_Centroid_x']
295 y = goodCat[
'slot_Centroid_y']
296 z = goodCat[self.
refFluxNames.fluxName]/goodCat[fluxNames.fluxName]
301 fitValues = np.median(z)
303 ctrl = self.config.fitConfig.makeControl()
306 for iteration
in range(self.config.numIter):
307 resid = z - fitValues
311 apCorrErr = median_abs_deviation(resid, scale=
"normal") + 1e-7
312 keep = np.abs(resid) <= self.config.numSigmaClip * apCorrErr
314 self.log.debug(
"Removing %d sources as outliers.", len(resid) - keep.sum())
321 while (len(x) - ctrl.computeSize()) < self.config.minDegreesOfFreedom:
334 if name
in self.config.allowFailure:
335 self.log.warning(
"Unable to measure aperture correction for '%s': "
336 "only %d sources remain, but require at least %d." %
337 (name, keep.sum(), self.config.minDegreesOfFreedom + 1))
341 ndof=self.config.minDegreesOfFreedom + 1,
342 iteration=iteration+1)
344 apCorrField = ChebyshevBoundedField.fit(bbox, x, y, z, ctrl)
345 fitValues = apCorrField.evaluate(x, y)
350 if self.config.doFinalMedianShift:
351 med = np.median(fitValues - z)
352 coeffs = apCorrField.getCoefficients().copy()
355 fitValues = apCorrField.evaluate(x, y)
358 "Aperture correction for %s from %d stars: med %f, MAD %f, RMS %f",
361 np.median(fitValues - z),
362 median_abs_deviation(fitValues - z, scale=
"normal"),
363 np.mean((fitValues - z)**2.)**0.5,
367 plotApCorr(bbox, x, y, z, apCorrField,
"%s, final" % (name,), doPause)
370 used = np.zeros(len(catalog), dtype=bool)
371 used[np.searchsorted(catalog[
'id'], ids)] =
True
372 catalog[fluxNames.usedName] = used
378 apCorrMap[fluxNames.fluxName] = apCorrField
381 np.array([[apCorrErr]]),