lsst.ip.diffim  14.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | List of all members
lsst.ip.diffim.modelPsfMatch.ModelPsfMatchTask Class Reference

Matching of two model Psfs, and application of the Psf-matching kernel to an input Exposure. More...

Inheritance diagram for lsst.ip.diffim.modelPsfMatch.ModelPsfMatchTask:

Public Member Functions

def __init__
 Create a ModelPsfMatchTask. More...
 
def run
 Psf-match an exposure to a model Psf. More...
 

Public Attributes

 kConfig
 

Static Public Attributes

 ConfigClass = ModelPsfMatchConfig
 

Private Member Functions

def _diagnostic
 Print diagnostic information on spatial kernel and background fit. More...
 
def _buildCellSet
 Build a SpatialCellSet for use with the solve method. More...
 
def _makePsfMaskedImage
 Return a MaskedImage of the a PSF Model of specified dimensions. More...
 

Detailed Description

Matching of two model Psfs, and application of the Psf-matching kernel to an input Exposure.

Contents

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Description

This Task differs from ImagePsfMatchTask in that it matches two Psf models, by realizing them in an Exposure-sized SpatialCellSet and then inserting each Psf-image pair into KernelCandidates. Because none of the pairs of sources that are to be matched should be invalid, all sigma clipping is turned off in ModelPsfMatchConfig. And because there is no tracked variance in the Psf images, the debugging and logging QA info should be interpreted with caution.

One item of note is that the sizes of Psf models are fixed (e.g. its defined as a 21x21 matrix). When the Psf-matching kernel is being solved for, the Psf "image" is convolved with each kernel basis function, leading to a loss of information around the borders. This pixel loss will be problematic for the numerical stability of the kernel solution if the size of the convolution kernel (set by ModelPsfMatchConfig.kernelSize) is much bigger than: psfSize//2. Thus the sizes of Psf-model matching kernels are typically smaller than their image-matching counterparts. If the size of the kernel is too small, the convolved stars will look "boxy"; if the kernel is too large, the kernel solution will be "noisy". This is a trade-off that needs careful attention for a given dataset.

The primary use case for this Task is in matching an Exposure to a constant-across-the-sky Psf model for the purposes of image coaddition. It is important to note that in the code, the "template" Psf is the Psf that the science image gets matched to. In this sense the order of template and science image are reversed, compared to ImagePsfMatchTask, which operates on the template image.

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Task initialization

Create a ModelPsfMatchTask.

Parameters
*argsarguments to be passed to lsst.ip.diffim.PsfMatchTask.__init__
**kwargskeyword arguments to be passed to lsst.ip.diffim.PsfMatchTask.__init__

Upon initialization, the kernel configuration is defined by self.config.kernel.active. This Task does have a run() method, which is the default way to call the Task.

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Invoking the Task

Psf-match an exposure to a model Psf.

Parameters
exposure,:Exposure to Psf-match to the reference Psf model; it must return a valid PSF model via exposure.getPsf()
referencePsfModel,:The Psf model to match to (an lsst.afw.detection.Psf)
kernelSum,:A multipicative factor to apply to the kernel sum (default=1.0)
Returns
  • psfMatchedExposure: the Psf-matched Exposure. This has the same parent bbox, Wcs, Calib and Filter as the input Exposure but no Psf. In theory the Psf should equal referencePsfModel but the match is likely not exact.
  • psfMatchingKernel: the spatially varying Psf-matching kernel
  • kernelCellSet: SpatialCellSet used to solve for the Psf-matching kernel
  • referencePsfModel: Validated and/or modified reference model used

Raise a RuntimeError if the Exposure does not contain a Psf model

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Configuration parameters

See ModelPsfMatchConfig

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Quantities set in Metadata

See PsfMatchTask

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Debug variables

The command line task interface supports a flag -d/–debug to import debug.py from your PYTHONPATH. The relevant contents of debug.py for this Task include:

1 import sys
2 import lsstDebug
3 def DebugInfo(name):
4  di = lsstDebug.getInfo(name)
5  if name == "lsst.ip.diffim.psfMatch":
6  di.display = True # global
7  di.maskTransparency = 80 # ds9 mask transparency
8  di.displayCandidates = True # show all the candidates and residuals
9  di.displayKernelBasis = False # show kernel basis functions
10  di.displayKernelMosaic = True # show kernel realized across the image
11  di.plotKernelSpatialModel = False # show coefficients of spatial model
12  di.showBadCandidates = True # show the bad candidates (red) along with good (green)
13  elif name == "lsst.ip.diffim.modelPsfMatch":
14  di.display = True # global
15  di.maskTransparency = 30 # ds9 mask transparency
16  di.displaySpatialCells = True # show spatial cells before the fit
17  return di
18 lsstDebug.Info = DebugInfo
19 lsstDebug.frame = 1

Note that if you want addional logging info, you may add to your scripts:

1 import lsst.log.utils as logUtils
2 logUtils.traceSetAt("ip.diffim", 4)

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

A complete example of using ModelPsfMatchTask

This code is modelPsfMatchTask.py in the examples directory, and can be run as e.g.

1 examples/modelPsfMatchTask.py
2 examples/modelPsfMatchTask.py --debug
3 examples/modelPsfMatchTask.py --debug --template /path/to/templateExp.fits --science /path/to/scienceExp.fits
Create a subclass of ModelPsfMatchTask that accepts two exposures. Note that the "template" exposure contains the Psf that will get matched to, and the "science" exposure is the one that will be convolved:
1 class MyModelPsfMatchTask(ModelPsfMatchTask):
2  """An override for ModelPsfMatchTask"""
3 
4  def __init__(self, *args, **kwargs):
5  ModelPsfMatchTask.__init__(self, *args, **kwargs)
6 
7  def run(self, templateExp, scienceExp):
8  return ModelPsfMatchTask.run(self, scienceExp, templateExp.getPsf())

And allow the user the freedom to either run the script in default mode, or point to their own images on disk. Note that these images must be readable as an lsst.afw.image.Exposure:

1 if __name__ == "__main__":
2  import argparse
3  parser = argparse.ArgumentParser(description="Demonstrate the use of ModelPsfMatchTask")
4 
5  parser.add_argument("--debug", "-d", action="store_true", help="Load debug.py?", default=False)
6  parser.add_argument("--template", "-t", help="Template Exposure to use", default=None)
7  parser.add_argument("--science", "-s", help="Science Exposure to use", default=None)
8 
9  args = parser.parse_args()

We have enabled some minor display debugging in this script via the –debug option. However, if you have an lsstDebug debug.py in your PYTHONPATH you will get additional debugging displays. The following block checks for this script:

1  if args.debug:
2  try:
3  import debug
4  # Since I am displaying 2 images here, set the starting frame number for the LSST debug LSST
5  debug.lsstDebug.frame = 3
6  except ImportError as e:
7  print(e, file=sys.stderr)

Finally, we call a run method that we define below. First set up a Config and modify some of the parameters. In particular we don't want to "grow" the sizes of the kernel or KernelCandidates, since we are operating with fixed–size images (i.e. the size of the input Psf models).
1 def run(args):
2  #
3  # Create the Config and use sum of gaussian basis
4  #
5  config = ModelPsfMatchTask.ConfigClass()
6  config.kernel.active.scaleByFwhm = False

Make sure the images (if any) that were sent to the script exist on disk and are readable. If no images are sent, make some fake data up for the sake of this example script (have a look at the code if you want more details on generateFakeData):

1  # Run the requested method of the Task
2  if args.template is not None and args.science is not None:
3  if not os.path.isfile(args.template):
4  raise Exception("Template image %s does not exist" % (args.template))
5  if not os.path.isfile(args.science):
6  raise Exception("Science image %s does not exist" % (args.science))
7 
8  try:
9  templateExp = afwImage.ExposureF(args.template)
10  except pexExcept.LsstCppException as e:
11  raise Exception("Cannot read template image %s" % (args.template))
12  try:
13  scienceExp = afwImage.ExposureF(args.science)
14  except pexExcept.LsstCppException as e:
15  raise Exception("Cannot read science image %s" % (args.science))
16  else:
17  templateExp, scienceExp = generateFakeData()
18  config.kernel.active.sizeCellX = 128
19  config.kernel.active.sizeCellY = 128

Display the two images if –debug:

1  if args.debug:
2  ds9.mtv(templateExp, frame=1, title="Example script: Input Template")
3  ds9.mtv(scienceExp, frame=2, title="Example script: Input Science Image")

Create and run the Task:

1  # Create the Task
2  psfMatchTask = MyModelPsfMatchTask(config=config)
3 
4  # Run the Task
5  result = psfMatchTask.run(templateExp, scienceExp)

And finally provide optional debugging display of the Psf-matched (via the Psf models) science image:

1  if args.debug:
2  # See if the LSST debug has incremented the frame number; if not start with frame 3
3  try:
4  frame = debug.lsstDebug.frame + 1
5  except Exception:
6  frame = 3
7  ds9.mtv(result.psfMatchedExposure, frame=frame, title="Example script: Matched Science Image")

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Definition at line 103 of file modelPsfMatch.py.

Constructor & Destructor Documentation

def lsst.ip.diffim.modelPsfMatch.ModelPsfMatchTask.__init__ (   self,
  args,
  kwargs 
)

Create a ModelPsfMatchTask.

Parameters
*argsarguments to be passed to lsst.ip.diffim.PsfMatchTask.__init__
**kwargskeyword arguments to be passed to lsst.ip.diffim.PsfMatchTask.__init__

Upon initialization, the kernel configuration is defined by self.config.kernel.active. This Task does have a run() method, which is the default way to call the Task.

Definition at line 261 of file modelPsfMatch.py.

Member Function Documentation

def lsst.ip.diffim.modelPsfMatch.ModelPsfMatchTask._buildCellSet (   self,
  exposure,
  referencePsfModel 
)
private

Build a SpatialCellSet for use with the solve method.

Parameters
exposure,:The science exposure that will be convolved; must contain a Psf
referencePsfModel,:Psf model to match to
Returns
-kernelCellSet: a SpatialCellSet to be used by self._solve -referencePsfModel: Validated and/or modified reference model used to populate the SpatialCellSet

If the reference Psf model and science Psf model have different dimensions, adjust the referencePsfModel (the model to which the exposure PSF will be matched) to match that of the science Psf. If the science Psf dimensions vary across the image, as is common with a WarpedPsf, either pad or clip (depending on config.padPsf) the dimensions to be constant.

Definition at line 342 of file modelPsfMatch.py.

def lsst.ip.diffim.modelPsfMatch.ModelPsfMatchTask._diagnostic (   self,
  kernelCellSet,
  spatialSolution,
  spatialKernel,
  spatialBg 
)
private

Print diagnostic information on spatial kernel and background fit.

The debugging diagnostics are not really useful here, since the images we are matching have no variance. Thus override the _diagnostic method to generate no logging information

Definition at line 335 of file modelPsfMatch.py.

def lsst.ip.diffim.modelPsfMatch.ModelPsfMatchTask._makePsfMaskedImage (   self,
  psfModel,
  posX,
  posY,
  dimensions = None 
)
private

Return a MaskedImage of the a PSF Model of specified dimensions.

Definition at line 477 of file modelPsfMatch.py.

def lsst.ip.diffim.modelPsfMatch.ModelPsfMatchTask.run (   self,
  exposure,
  referencePsfModel,
  kernelSum = 1.0 
)

Psf-match an exposure to a model Psf.

Parameters
exposure,:Exposure to Psf-match to the reference Psf model; it must return a valid PSF model via exposure.getPsf()
referencePsfModel,:The Psf model to match to (an lsst.afw.detection.Psf)
kernelSum,:A multipicative factor to apply to the kernel sum (default=1.0)
Returns
  • psfMatchedExposure: the Psf-matched Exposure. This has the same parent bbox, Wcs, Calib and Filter as the input Exposure but no Psf. In theory the Psf should equal referencePsfModel but the match is likely not exact.
  • psfMatchingKernel: the spatially varying Psf-matching kernel
  • kernelCellSet: SpatialCellSet used to solve for the Psf-matching kernel
  • referencePsfModel: Validated and/or modified reference model used

Raise a RuntimeError if the Exposure does not contain a Psf model

Definition at line 274 of file modelPsfMatch.py.

Member Data Documentation

lsst.ip.diffim.modelPsfMatch.ModelPsfMatchTask.ConfigClass = ModelPsfMatchConfig
static

Definition at line 259 of file modelPsfMatch.py.

lsst.ip.diffim.modelPsfMatch.ModelPsfMatchTask.kConfig

Definition at line 271 of file modelPsfMatch.py.


The documentation for this class was generated from the following file: