LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Classes | Functions | Variables
lsst.meas.algorithms.detection Namespace Reference

Classes

class  BackgroundConfig
 Config for background estimation. More...
 
class  SourceDetectionConfig
 Configuration parameters for the SourceDetectionTask. More...
 
class  SourceDetectionTask
 Detect positive and negative sources on an exposure and return a new table.SourceCatalog. More...
 

Functions

def addExposures
 Add a set of exposures together. More...
 
def getBackground
 Estimate the background of an image (a thin layer on lsst.afw.math.makeBackground) More...
 
def estimateBackground
 Estimate exposure's background using parameters in backgroundConfig. More...
 

Variables

tuple __all__
 

Function Documentation

def lsst.meas.algorithms.detection.addExposures (   exposureList)

Add a set of exposures together.

Parameters
[in]exposureListsequence of exposures to add
Returns
an exposure of the same size as each exposure in exposureList, with the metadata from exposureList[0] and a masked image equal to the sum of all the exposure's masked images.
Exceptions
LsstExceptionif the exposures do not all have the same dimensions (but does not check xy0)

Definition at line 531 of file detection.py.

532 def addExposures(exposureList):
533  """!Add a set of exposures together.
534 
535  \param[in] exposureList sequence of exposures to add
536 
537  \return an exposure of the same size as each exposure in exposureList,
538  with the metadata from exposureList[0] and a masked image equal to the
539  sum of all the exposure's masked images.
540 
541  \throw LsstException if the exposures do not all have the same dimensions (but does not check xy0)
542  """
543  exposure0 = exposureList[0]
544  image0 = exposure0.getMaskedImage()
545 
546  addedImage = image0.Factory(image0, True)
547  addedImage.setXY0(image0.getXY0())
548 
549  for exposure in exposureList[1:]:
550  image = exposure.getMaskedImage()
551  addedImage += image
552 
553  addedExposure = exposure0.Factory(addedImage, exposure0.getWcs())
554  return addedExposure
def addExposures
Add a set of exposures together.
Definition: detection.py:531
def lsst.meas.algorithms.detection.estimateBackground (   exposure,
  backgroundConfig,
  subtract = True,
  stats = True,
  statsKeys = None 
)

Estimate exposure's background using parameters in backgroundConfig.

If subtract is true, make a copy of the exposure and subtract the background. If stats is True, measure the mean and variance of the background and add them to the background-subtracted exposure's metadata with keys "BGMEAN" and "BGVAR", or the keys given in statsKeys (2-tuple of strings).

Return background, backgroundSubtractedExposure

Definition at line 639 of file detection.py.

640  statsKeys=None):
641  """!Estimate exposure's background using parameters in backgroundConfig.
642 
643  If subtract is true, make a copy of the exposure and subtract the background.
644  If `stats` is True, measure the mean and variance of the background and
645  add them to the background-subtracted exposure's metadata with keys
646  "BGMEAN" and "BGVAR", or the keys given in `statsKeys` (2-tuple of strings).
647 
648  Return background, backgroundSubtractedExposure
649  """
650 
651  displayBackground = lsstDebug.Info(__name__).displayBackground
652 
653  maskedImage = exposure.getMaskedImage()
654 
655  background = getBackground(maskedImage, backgroundConfig)
656 
657  if not background:
658  raise RuntimeError, "Unable to estimate background for exposure"
659 
660  bgimg = None
661 
662  if displayBackground > 1:
663  bgimg = background.getImageF()
664  ds9.mtv(bgimg, title="background", frame=1)
665 
666  if not subtract:
667  return background, None
668 
669  bbox = maskedImage.getBBox()
670  backgroundSubtractedExposure = exposure.Factory(exposure, bbox, afwImage.PARENT, True)
671  copyImage = backgroundSubtractedExposure.getMaskedImage().getImage()
672  if bgimg is None:
673  bgimg = background.getImageF()
674  copyImage -= bgimg
675 
676  # Record statistics of the background in the bgsub exposure metadata.
677  # (lsst.daf.base.PropertySet)
678  if stats:
679  if statsKeys is None:
680  mnkey = 'BGMEAN'
681  varkey = 'BGVAR'
682  else:
683  mnkey, varkey = statsKeys
684  meta = backgroundSubtractedExposure.getMetadata()
685  s = afwMath.makeStatistics(bgimg, afwMath.MEAN | afwMath.VARIANCE)
686  bgmean = s.getValue(afwMath.MEAN)
687  bgvar = s.getValue(afwMath.VARIANCE)
688  meta.addDouble(mnkey, bgmean)
689  meta.addDouble(varkey, bgvar)
690 
691  if displayBackground:
692  ds9.mtv(backgroundSubtractedExposure, title="subtracted")
693 
694  return background, backgroundSubtractedExposure
695 estimateBackground.ConfigClass = BackgroundConfig
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
def getBackground
Estimate the background of an image (a thin layer on lsst.afw.math.makeBackground) ...
Definition: detection.py:555
def lsst.meas.algorithms.detection.getBackground (   image,
  backgroundConfig,
  nx = 0,
  ny = 0,
  algorithm = None 
)

Estimate the background of an image (a thin layer on lsst.afw.math.makeBackground)

Parameters
[in]imageimage whose background is to be computed
[in]backgroundConfigconfiguration (a BackgroundConfig)
[in]nxnumber of x bands; 0 for default
[in]nynumber of y bands; 0 for default
[in]algorithmname of interpolation algorithm; see lsst.afw.math.BackgroundControl for details

Definition at line 555 of file detection.py.

556 def getBackground(image, backgroundConfig, nx=0, ny=0, algorithm=None):
557  """!Estimate the background of an image (a thin layer on lsst.afw.math.makeBackground)
558 
559  \param[in] image image whose background is to be computed
560  \param[in] backgroundConfig configuration (a BackgroundConfig)
561  \param[in] nx number of x bands; 0 for default
562  \param[in] ny number of y bands; 0 for default
563  \param[in] algorithm name of interpolation algorithm; see lsst.afw.math.BackgroundControl for details
564  """
565  backgroundConfig.validate();
566 
567  logger = pexLogging.getDefaultLog()
568  logger = pexLogging.Log(logger,"lsst.meas.algorithms.detection.getBackground")
569 
570  if not nx:
571  nx = image.getWidth()//backgroundConfig.binSize + 1
572  if not ny:
573  ny = image.getHeight()//backgroundConfig.binSize + 1
574 
575  sctrl = afwMath.StatisticsControl()
576  sctrl.setAndMask(reduce(lambda x, y: x | image.getMask().getPlaneBitMask(y),
577  backgroundConfig.ignoredPixelMask, 0x0))
578  sctrl.setNanSafe(backgroundConfig.isNanSafe)
579 
580  pl = pexLogging.Debug("lsst.meas.algorithms.detection.getBackground")
581  pl.debug(3, "Ignoring mask planes: %s" % ", ".join(backgroundConfig.ignoredPixelMask))
582 
583  if not algorithm:
584  algorithm = backgroundConfig.algorithm
585 
586  bctrl = afwMath.BackgroundControl(algorithm, nx, ny,
587  backgroundConfig.undersampleStyle, sctrl,
588  backgroundConfig.statisticsProperty)
589 
590  # TODO: The following check should really be done within afw/math. With the
591  # current code structure, it would need to be accounted for in the
592  # doGetImage() funtion in BackgroundMI.cc (which currently only checks
593  # against the interpoation settings which is not appropriate when
594  # useApprox=True) and/or the makeApproximate() function in
595  # afw/Approximate.cc.
596  # See ticket DM-2920: "Clean up code in afw for Approximate background
597  # estimation" (which includes a note to remove the following and the
598  # similar checks in pipe_tasks/matchBackgrounds.py once implemented)
599  #
600  # Check that config setting of approxOrder/binSize make sense
601  # (i.e. ngrid (= shortDimension/binSize) > approxOrderX) and perform
602  # appropriate undersampleStlye behavior.
603  if backgroundConfig.useApprox:
604  if not backgroundConfig.approxOrderY in (backgroundConfig.approxOrderX,-1):
605  raise ValueError("Error: approxOrderY not in (approxOrderX, -1)")
606  order = backgroundConfig.approxOrderX
607  minNumberGridPoints = backgroundConfig.approxOrderX + 1
608  if min(nx,ny) <= backgroundConfig.approxOrderX:
609  logger.warn("Too few points in grid to constrain fit: min(nx, ny) < approxOrder) "+
610  "[min(%d, %d) < %d]" % (nx, ny, backgroundConfig.approxOrderX))
611  if backgroundConfig.undersampleStyle == "THROW_EXCEPTION":
612  raise ValueError("Too few points in grid (%d, %d) for order (%d) and binsize (%d)" % (
613  nx, ny, backgroundConfig.approxOrderX, backgroundConfig.binSize))
614  elif backgroundConfig.undersampleStyle == "REDUCE_INTERP_ORDER":
615  if order < 1:
616  raise ValueError("Cannot reduce approxOrder below 0. " +
617  "Try using undersampleStyle = \"INCREASE_NXNYSAMPLE\" instead?")
618  order = min(nx, ny) - 1
619  logger.warn("Reducing approxOrder to %d" % order)
620  elif backgroundConfig.undersampleStyle == "INCREASE_NXNYSAMPLE":
621  newBinSize = min(image.getWidth(),image.getHeight())//(minNumberGridPoints-1)
622  if newBinSize < 1:
623  raise ValueError("Binsize must be greater than 0")
624  newNx = image.getWidth()//newBinSize + 1
625  newNy = image.getHeight()//newBinSize + 1
626  bctrl.setNxSample(newNx)
627  bctrl.setNySample(newNy)
628  logger.warn("Decreasing binSize from %d to %d for a grid of (%d, %d)" %
629  (backgroundConfig.binSize, newBinSize, newNx, newNy))
630 
631  actrl = afwMath.ApproximateControl(afwMath.ApproximateControl.CHEBYSHEV, order, order,
632  backgroundConfig.weighting)
633  bctrl.setApproximateControl(actrl)
634 
635  return afwMath.makeBackground(image, bctrl)
636 
637 getBackground.ConfigClass = BackgroundConfig
boost::shared_ptr< Background > makeBackground(ImageT const &img, BackgroundControl const &bgCtrl)
A convenience function that uses function overloading to make the correct type of Background...
Definition: Background.h:467
Pass parameters to a Background object.
Definition: Background.h:61
a place to record messages and descriptions of the state of processing.
Definition: Log.h:154
Control how to make an approximation.
Definition: Approximate.h:47
Pass parameters to a Statistics objectA class to pass parameters which control how the stats are calc...
Definition: Statistics.h:92
def getBackground
Estimate the background of an image (a thin layer on lsst.afw.math.makeBackground) ...
Definition: detection.py:555

Variable Documentation

tuple lsst.meas.algorithms.detection.__all__
Initial value:
1 = ("SourceDetectionConfig", "SourceDetectionTask", "getBackground",
2  "estimateBackground", "BackgroundConfig", "addExposures")

Definition at line 33 of file detection.py.