290 exposure : `lsst.afw.image.Exposure`
291 Exposure to be processed
292 srcs : `lsst.afw.table.SourceCatalog`
293 SourceCatalog containing sources detected on this exposure
294 psf : `lsst.afw.detection.Psf`
295 Point source function
302 if self.config.useCiLimits:
303 self.log.info(f
"Using CI catalog limits, "
304 f
"the original number of sources to deblend was {len(srcs)}.")
307 minChildren, maxChildren = self.config.ciDeblendChildRange
308 nPeaks = np.array([len(src.getFootprint().peaks)
for src
in srcs])
309 childrenInRange = np.where((nPeaks >= minChildren) & (nPeaks <= maxChildren))[0]
310 numParentsToDeblend = self.config.ciNumParentsToDeblend
311 if len(childrenInRange) < numParentsToDeblend:
313 if len(childrenInRange) == 0:
314 raise ValueError(
"No children to deblend; cannot continue with CI testing.")
315 numParentsToDeblend = len(childrenInRange)
318 parents = nPeaks == 1
319 children = np.zeros((len(srcs),), dtype=bool)
320 children[childrenInRange[:numParentsToDeblend]] =
True
321 srcs = srcs[parents | children]
324 idFactory = srcs.getIdFactory()
325 maxId = np.max(srcs[
"id"])
326 idFactory.notify(maxId)
328 self.log.info(
"Deblending %d sources", len(srcs))
333 mi = exposure.getMaskedImage()
335 statsCtrl.setAndMask(mi.getMask().getPlaneBitMask(self.config.maskPlanes))
337 sigma1 = math.sqrt(stats.getValue(afwMath.MEDIAN))
338 self.log.trace(
'sigma1: %g', sigma1)
342 for i, src
in enumerate(srcs):
345 fp = src.getFootprint()
358 self.log.debug(
'Parent %i: skipping large footprint (area: %i)',
359 int(src.getId()), int(fp.getArea()))
361 if self.
isMasked(fp, exposure.getMaskedImage().getMask()):
364 self.log.debug(
'Parent %i: skipping masked footprint (area: %i)',
365 int(src.getId()), int(fp.getArea()))
369 center = fp.getCentroid()
372 if not (psf_fwhm > 0):
373 if self.config.catchFailures:
374 self.log.warning(
"Unable to deblend source %d: because PSF FWHM=%f is invalid.",
375 src.getId(), psf_fwhm)
379 raise ValueError(f
"PSF at {center} has an invalid FWHM value of {psf_fwhm}")
381 self.log.trace(
'Parent %i: deblending %i peaks', int(src.getId()), len(pks))
387 src.set(self.
tooManyPeaksKey, len(fp.getPeaks()) > self.config.maxNumberOfPeaks)
391 fp, mi, psf, psf_fwhm, sigma1=sigma1,
392 psfChisqCut1=self.config.psfChisq1,
393 psfChisqCut2=self.config.psfChisq2,
394 psfChisqCut2b=self.config.psfChisq2b,
395 maxNumberOfPeaks=self.config.maxNumberOfPeaks,
396 strayFluxToPointSources=self.config.strayFluxToPointSources,
397 assignStrayFlux=self.config.assignStrayFlux,
398 strayFluxAssignment=self.config.strayFluxRule,
399 rampFluxAtEdge=(self.config.edgeHandling ==
'ramp'),
400 patchEdges=(self.config.edgeHandling ==
'noclip'),
401 tinyFootprintSize=self.config.tinyFootprintSize,
402 clipStrayFluxFraction=self.config.clipStrayFluxFraction,
403 weightTemplates=self.config.weightTemplates,
404 removeDegenerateTemplates=self.config.removeDegenerateTemplates,
405 maxTempDotProd=self.config.maxTempDotProd,
406 medianSmoothTemplate=self.config.medianSmoothTemplate
408 if self.config.catchFailures:
410 except Exception
as e:
411 if self.config.catchFailures:
412 self.log.warning(
"Unable to deblend source %d: %s", src.getId(), e)
415 traceback.print_exc()
422 for j, peak
in enumerate(res.deblendedParents[0].peaks):
423 heavy = peak.getFluxPortion()
424 if heavy
is None or peak.skip:
426 if not self.config.propagateAllPeaks:
431 self.log.trace(
"Peak at (%i,%i) failed. Using minimal default info for child.",
432 pks[j].getIx(), pks[j].getIy())
436 peakList = foot.getPeaks()
438 peakList.append(peak.peak)
439 zeroMimg = afwImage.MaskedImageF(foot.getBBox())
441 if peak.deblendedAsPsf:
442 if peak.psfFitFlux
is None:
443 peak.psfFitFlux = 0.0
444 if peak.psfFitCenter
is None:
445 peak.psfFitCenter = (peak.peak.getIx(), peak.peak.getIy())
447 assert len(heavy.getPeaks()) == 1
450 child = srcs.addNew()
453 child.set(key, src.get(key))
455 child.setParent(src.getId())
456 child.setFootprint(heavy)
457 child.set(self.
psfKey, peak.deblendedAsPsf)
459 if peak.deblendedAsPsf:
460 (cx, cy) = peak.psfFitCenter
472 child.set(self.
peakIdKey, pks[j].getId())
486 spans = src.getFootprint().spans
488 spans = spans.union(child.getFootprint().spans)
489 src.getFootprint().setSpans(spans)
497 self.log.info(
'Deblended: of %i sources, %i were deblended, creating %i children, total %i sources',
498 n0, nparents, n1-n0, n1)