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 if len(childrenInRange) < self.config.ciNumParentsToDeblend:
311 raise ValueError(
"Fewer than ciNumParentsToDeblend children were contained in the range "
312 "indicated by ciDeblendChildRange. Adjust this range to include more "
316 parents = nPeaks == 1
317 children = np.zeros((len(srcs),), dtype=bool)
318 children[childrenInRange[:self.config.ciNumParentsToDeblend]] =
True
319 srcs = srcs[parents | children]
322 idFactory = srcs.getIdFactory()
323 maxId = np.max(srcs[
"id"])
324 idFactory.notify(maxId)
326 self.log.info(
"Deblending %d sources", len(srcs))
331 mi = exposure.getMaskedImage()
333 statsCtrl.setAndMask(mi.getMask().getPlaneBitMask(self.config.maskPlanes))
335 sigma1 = math.sqrt(stats.getValue(afwMath.MEDIAN))
336 self.log.trace(
'sigma1: %g', sigma1)
340 for i, src
in enumerate(srcs):
343 fp = src.getFootprint()
356 self.log.debug(
'Parent %i: skipping large footprint (area: %i)',
357 int(src.getId()), int(fp.getArea()))
359 if self.
isMasked(fp, exposure.getMaskedImage().getMask()):
362 self.log.debug(
'Parent %i: skipping masked footprint (area: %i)',
363 int(src.getId()), int(fp.getArea()))
367 center = fp.getCentroid()
370 if not (psf_fwhm > 0):
371 if self.config.catchFailures:
372 self.log.warning(
"Unable to deblend source %d: because PSF FWHM=%f is invalid.",
373 src.getId(), psf_fwhm)
377 raise ValueError(f
"PSF at {center} has an invalid FWHM value of {psf_fwhm}")
379 self.log.trace(
'Parent %i: deblending %i peaks', int(src.getId()), len(pks))
385 src.set(self.
tooManyPeaksKey, len(fp.getPeaks()) > self.config.maxNumberOfPeaks)
389 fp, mi, psf, psf_fwhm, sigma1=sigma1,
390 psfChisqCut1=self.config.psfChisq1,
391 psfChisqCut2=self.config.psfChisq2,
392 psfChisqCut2b=self.config.psfChisq2b,
393 maxNumberOfPeaks=self.config.maxNumberOfPeaks,
394 strayFluxToPointSources=self.config.strayFluxToPointSources,
395 assignStrayFlux=self.config.assignStrayFlux,
396 strayFluxAssignment=self.config.strayFluxRule,
397 rampFluxAtEdge=(self.config.edgeHandling ==
'ramp'),
398 patchEdges=(self.config.edgeHandling ==
'noclip'),
399 tinyFootprintSize=self.config.tinyFootprintSize,
400 clipStrayFluxFraction=self.config.clipStrayFluxFraction,
401 weightTemplates=self.config.weightTemplates,
402 removeDegenerateTemplates=self.config.removeDegenerateTemplates,
403 maxTempDotProd=self.config.maxTempDotProd,
404 medianSmoothTemplate=self.config.medianSmoothTemplate
406 if self.config.catchFailures:
408 except Exception
as e:
409 if self.config.catchFailures:
410 self.log.warning(
"Unable to deblend source %d: %s", src.getId(), e)
413 traceback.print_exc()
420 for j, peak
in enumerate(res.deblendedParents[0].peaks):
421 heavy = peak.getFluxPortion()
422 if heavy
is None or peak.skip:
424 if not self.config.propagateAllPeaks:
429 self.log.trace(
"Peak at (%i,%i) failed. Using minimal default info for child.",
430 pks[j].getIx(), pks[j].getIy())
434 peakList = foot.getPeaks()
436 peakList.append(peak.peak)
437 zeroMimg = afwImage.MaskedImageF(foot.getBBox())
439 if peak.deblendedAsPsf:
440 if peak.psfFitFlux
is None:
441 peak.psfFitFlux = 0.0
442 if peak.psfFitCenter
is None:
443 peak.psfFitCenter = (peak.peak.getIx(), peak.peak.getIy())
445 assert len(heavy.getPeaks()) == 1
448 child = srcs.addNew()
451 child.set(key, src.get(key))
453 child.setParent(src.getId())
454 child.setFootprint(heavy)
455 child.set(self.
psfKey, peak.deblendedAsPsf)
457 if peak.deblendedAsPsf:
458 (cx, cy) = peak.psfFitCenter
470 child.set(self.
peakIdKey, pks[j].getId())
484 spans = src.getFootprint().spans
486 spans = spans.union(child.getFootprint().spans)
487 src.getFootprint().setSpans(spans)
495 self.log.info(
'Deblended: of %i sources, %i were deblended, creating %i children, total %i sources',
496 n0, nparents, n1-n0, n1)