Deblend.
241 def deblend(self, exposure, srcs, psf):
245 @param[in] exposure Exposure to process
246 @param[in,out] srcs SourceCatalog containing sources detected on this exposure.
251 self.log.info(
"Deblending %d sources" % len(srcs))
257 mi = exposure.getMaskedImage()
259 statsCtrl.setAndMask(mi.getMask().getPlaneBitMask(self.config.maskPlanes))
261 sigma1 = math.sqrt(stats.getValue(afwMath.MEDIAN))
262 self.log.logdebug(
'sigma1: %g' % sigma1)
264 schema = srcs.getSchema()
268 for i,src
in enumerate(srcs):
271 fp = src.getFootprint()
279 self.log.logdebug(
'Parent %i: skipping large footprint' % (int(src.getId()),))
281 if self.
isMasked(fp, exposure.getMaskedImage().getMask()):
284 self.log.logdebug(
'Parent %i: skipping masked footprint' % (int(src.getId()),))
291 self.log.logdebug(
'Parent %i: deblending %i peaks' % (int(src.getId()), len(pks)))
297 src.set(self.
tooManyPeaksKey, len(fp.getPeaks()) > self.config.maxNumberOfPeaks)
301 fp, mi, psf, psf_fwhm, sigma1=sigma1,
302 psfChisqCut1 = self.config.psfChisq1,
303 psfChisqCut2 = self.config.psfChisq2,
304 psfChisqCut2b= self.config.psfChisq2b,
305 maxNumberOfPeaks=self.config.maxNumberOfPeaks,
306 strayFluxToPointSources=self.config.strayFluxToPointSources,
307 assignStrayFlux=self.config.assignStrayFlux,
308 findStrayFlux=(self.config.assignStrayFlux
or self.config.findStrayFlux),
309 strayFluxAssignment=self.config.strayFluxRule,
310 rampFluxAtEdge=(self.config.edgeHandling ==
'ramp'),
311 patchEdges=(self.config.edgeHandling ==
'noclip'),
312 tinyFootprintSize=self.config.tinyFootprintSize,
313 clipStrayFluxFraction=self.config.clipStrayFluxFraction,
315 if self.config.catchFailures:
317 except Exception
as e:
318 if self.config.catchFailures:
319 self.log.warn(
"Unable to deblend source %d: %s" % (src.getId(), e))
322 traceback.print_exc()
329 for j, peak
in enumerate(res.peaks):
334 msg =
'Skipping out-of-bounds peak at (%i,%i)' % (pks[j].getIx(), pks[j].getIy())
339 heavy = peak.getFluxPortion()
342 msg =
'Skipping peak at (%i,%i), child %i of %i: no flux portion' \
343 % (pks[j].getIx(), pks[j].getIy(), j+1, len(res.peaks))
349 if self.config.propagateAllPeaks:
354 peakList = foot.getPeaks()
356 peakList.append(peak.peak)
357 zeroMimg = afwImage.MaskedImageF(foot.getBBox())
359 if peak.deblendedAsPsf:
360 if peak.psfFitFlux
is None:
361 peak.psfFitFlux = 0.0
362 if peak.psfFitCenter
is None:
363 peak.psfFitCenter = (peak.peak.getIx(), peak.peak.getIy())
364 self.log.warn(
"Peak failed. Using minimal default info for child.")
368 assert(len(heavy.getPeaks()) == 1)
371 child = srcs.addNew(); nchild += 1
373 child.setParent(src.getId())
374 child.setFootprint(heavy)
375 child.set(self.
psfKey, peak.deblendedAsPsf)
377 if peak.deblendedAsPsf:
378 (cx,cy) = peak.psfFitCenter
390 src.getFootprint().include([child.getFootprint()
for child
in kids])
400 self.log.info(
'Deblended: of %i sources, %i were deblended, creating %i children, total %i sources'
401 % (n0, nparents, n1-n0, n1))