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
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...
 

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

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.pex.logging as pexLog
2 pexLog.Trace_setVerbosity('lsst.ip.diffim', 5)

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

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  def __init__(self, *args, **kwargs):
4  ModelPsfMatchTask.__init__(self, *args, **kwargs)
5 
6  def run(self, templateExp, scienceExp):
7  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 >> sys.stderr, e

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, e:
11  raise Exception, "Cannot read template image %s" % (args.template)
12  try:
13  scienceExp = afwImage.ExposureF(args.science)
14  except pexExcept.LsstCppException, 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 69 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 227 of file modelPsfMatch.py.

228  def __init__(self, *args, **kwargs):
229  """!Create a ModelPsfMatchTask
230 
231  \param *args arguments to be passed to lsst.ip.diffim.PsfMatchTask.__init__
232  \param **kwargs keyword arguments to be passed to lsst.ip.diffim.PsfMatchTask.__init__
233 
234  Upon initialization, the kernel configuration is defined by self.config.kernel.active. This Task
235  does have a run() method, which is the default way to call the Task.
236  """
237  PsfMatchTask.__init__(self, *args, **kwargs)
238  self.kConfig = self.config.kernel.active
def __init__
Create a ModelPsfMatchTask.

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

Raise a RuntimeError if the reference Psf model and science Psf model have different dimensions

Definition at line 308 of file modelPsfMatch.py.

309  def _buildCellSet(self, exposure, referencePsfModel):
310  """!Build a SpatialCellSet for use with the solve method
311 
312  @param exposure: The science exposure that will be convolved; must contain a Psf
313  @param referencePsfModel: Psf model to match to
314 
315  @return kernelCellSet: a SpatialCellSet to be used by self._solve
316 
317  Raise a RuntimeError if the reference Psf model and science Psf model have different dimensions
318  """
319  scienceBBox = exposure.getBBox()
320  sciencePsfModel = exposure.getPsf()
321  # The Psf base class does not support getKernel() in general, as there are some Psf
322  # classes for which this is not meaningful.
323  # Many Psfs we use in practice are KernelPsfs, and this algorithm will work fine for them,
324  # but someday it should probably be modified to support arbitrary Psfs.
325  referencePsfModel = measAlg.KernelPsf.swigConvert(referencePsfModel)
326  sciencePsfModel = measAlg.KernelPsf.swigConvert(sciencePsfModel)
327  if referencePsfModel is None or sciencePsfModel is None:
328  raise RuntimeError("ERROR: Psf matching is only implemented for KernelPsfs")
329  if (referencePsfModel.getKernel().getDimensions() != sciencePsfModel.getKernel().getDimensions()):
330  pexLog.Trace(self.log.getName(), 1,
331  "ERROR: Dimensions of reference Psf and science Psf different; exiting")
332  raise RuntimeError, "ERROR: Dimensions of reference Psf and science Psf different; exiting"
333 
334  psfWidth, psfHeight = referencePsfModel.getKernel().getDimensions()
335  maxKernelSize = min(psfWidth, psfHeight) - 1
336  if maxKernelSize % 2 == 0:
337  maxKernelSize -= 1
338  if self.kConfig.kernelSize > maxKernelSize:
339  raise ValueError, "Kernel size (%d) too big to match Psfs of size %d; reduce to at least %d" % (
340  self.kConfig.kernelSize, psfWidth, maxKernelSize)
341 
342  # Infer spatial order of Psf model!
343  #
344  # Infer from the number of spatial parameters.
345  # (O + 1) * (O + 2) / 2 = N
346  # O^2 + 3 * O + 2 * (1 - N) = 0
347  #
348  # Roots are [-3 +/- sqrt(9 - 8 * (1 - N))] / 2
349  #
350  nParameters = sciencePsfModel.getKernel().getNSpatialParameters()
351  root = num.sqrt(9 - 8 * (1 - nParameters))
352  if (root != root // 1): # We know its an integer solution
353  pexLog.Trace(self.log.getName(), 3, "Problem inferring spatial order of image's Psf")
354  else:
355  order = (root - 3) / 2
356  if (order != order // 1):
357  pexLog.Trace(self.log.getName(), 3, "Problem inferring spatial order of image's Psf")
358  else:
359  pexLog.Trace(self.log.getName(), 2, "Spatial order of Psf = %d; matching kernel order = %d" % (
360  order, self.kConfig.spatialKernelOrder))
361 
362  regionSizeX, regionSizeY = scienceBBox.getDimensions()
363  scienceX0, scienceY0 = scienceBBox.getMin()
364 
365  sizeCellX = self.kConfig.sizeCellX
366  sizeCellY = self.kConfig.sizeCellY
367 
368  kernelCellSet = afwMath.SpatialCellSet(
369  afwGeom.Box2I(afwGeom.Point2I(scienceX0, scienceY0),
370  afwGeom.Extent2I(regionSizeX, regionSizeY)),
371  sizeCellX, sizeCellY
372  )
373 
374  nCellX = regionSizeX // sizeCellX
375  nCellY = regionSizeY // sizeCellY
376  dimenR = referencePsfModel.getKernel().getDimensions()
377  dimenS = sciencePsfModel.getKernel().getDimensions()
378 
379  policy = pexConfig.makePolicy(self.kConfig)
380  for row in range(nCellY):
381  # place at center of cell
382  posY = sizeCellY * row + sizeCellY // 2 + scienceY0
383 
384  for col in range(nCellX):
385  # place at center of cell
386  posX = sizeCellX * col + sizeCellX // 2 + scienceX0
387 
388  pexLog.Trace(self.log.getName(), 5, "Creating Psf candidate at %.1f %.1f" % (posX, posY))
389 
390  # reference kernel image, at location of science subimage
391  kernelImageR = referencePsfModel.computeImage(afwGeom.Point2D(posX, posY)).convertF()
392  kernelMaskR = afwImage.MaskU(dimenR)
393  kernelMaskR.set(0)
394  kernelVarR = afwImage.ImageF(dimenR)
395  kernelVarR.set(1.0)
396  referenceMI = afwImage.MaskedImageF(kernelImageR, kernelMaskR, kernelVarR)
397 
398  # kernel image we are going to convolve
399  kernelImageS = sciencePsfModel.computeImage(afwGeom.Point2D(posX, posY)).convertF()
400  kernelMaskS = afwImage.MaskU(dimenS)
401  kernelMaskS.set(0)
402  kernelVarS = afwImage.ImageF(dimenS)
403  kernelVarS.set(1.0)
404  scienceMI = afwImage.MaskedImageF(kernelImageS, kernelMaskS, kernelVarS)
405 
406  # The image to convolve is the science image, to the reference Psf.
407  kc = diffimLib.makeKernelCandidate(posX, posY, scienceMI, referenceMI, policy)
408  kernelCellSet.insertCandidate(kc)
409 
410  import lsstDebug
411  display = lsstDebug.Info(__name__).display
412  displaySpatialCells = lsstDebug.Info(__name__).displaySpatialCells
413  maskTransparency = lsstDebug.Info(__name__).maskTransparency
414  if not maskTransparency:
415  maskTransparency = 0
416  if display:
417  ds9.setMaskTransparency(maskTransparency)
418  if display and displaySpatialCells:
419  diUtils.showKernelSpatialCells(exposure.getMaskedImage(), kernelCellSet,
420  symb="o", ctype=ds9.CYAN, ctypeUnused=ds9.YELLOW, ctypeBad=ds9.RED,
421  size=4, frame=lsstDebug.frame, title="Image to be convolved")
422  lsstDebug.frame += 1
423  return kernelCellSet
limited backward compatibility to the DC2 run-time trace facilities
Definition: Trace.h:93
An integer coordinate rectangle.
Definition: Box.h:53
A collection of SpatialCells covering an entire image.
Definition: SpatialCell.h:378
def _buildCellSet
Build a SpatialCellSet for use with the solve method.
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 301 of file modelPsfMatch.py.

302  def _diagnostic(self, kernelCellSet, spatialSolution, spatialKernel, spatialBg):
303  """!Print diagnostic information on spatial kernel and background fit
304 
305  The debugging diagnostics are not really useful here, since the images we are matching have
306  no variance. Thus override the _diagnostic method to generate no logging information"""
307  return
def _diagnostic
Print diagnostic information on spatial kernel and background fit.
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

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

Definition at line 240 of file modelPsfMatch.py.

241  def run(self, exposure, referencePsfModel, kernelSum=1.0):
242  """!Psf-match an exposure to a model Psf
243 
244  @param exposure: Exposure to Psf-match to the reference Psf model;
245  it must return a valid PSF model via exposure.getPsf()
246  @param referencePsfModel: The Psf model to match to (an lsst.afw.detection.Psf)
247  @param kernelSum: A multipicative factor to apply to the kernel sum (default=1.0)
248 
249  @return
250  - psfMatchedExposure: the Psf-matched Exposure. This has the same parent bbox, Wcs, Calib and
251  Filter as the input Exposure but no Psf. In theory the Psf should equal referencePsfModel but
252  the match is likely not exact.
253  - psfMatchingKernel: the spatially varying Psf-matching kernel
254  - kernelCellSet: SpatialCellSet used to solve for the Psf-matching kernel
255 
256  Raise a RuntimeError if the Exposure does not contain a Psf model
257  """
258  if not exposure.hasPsf():
259  raise RuntimeError("exposure does not contain a Psf model")
260 
261  maskedImage = exposure.getMaskedImage()
262 
263  self.log.log(pexLog.Log.INFO, "compute Psf-matching kernel")
264  kernelCellSet = self._buildCellSet(exposure, referencePsfModel)
265  width, height = referencePsfModel.getLocalKernel().getDimensions()
266  psfAttr1 = measAlg.PsfAttributes(exposure.getPsf(), width//2, height//2)
267  psfAttr2 = measAlg.PsfAttributes(referencePsfModel, width//2, height//2)
268  s1 = psfAttr1.computeGaussianWidth(psfAttr1.ADAPTIVE_MOMENT) # gaussian sigma in pixels
269  s2 = psfAttr2.computeGaussianWidth(psfAttr2.ADAPTIVE_MOMENT) # gaussian sigma in pixels
270  fwhm1 = s1 * sigma2fwhm # science Psf
271  fwhm2 = s2 * sigma2fwhm # template Psf
272 
273  basisList = makeKernelBasisList(self.kConfig, fwhm1, fwhm2, metadata = self.metadata)
274  spatialSolution, psfMatchingKernel, backgroundModel = self._solve(kernelCellSet, basisList)
275 
276  if psfMatchingKernel.isSpatiallyVarying():
277  sParameters = num.array(psfMatchingKernel.getSpatialParameters())
278  sParameters[0][0] = kernelSum
279  psfMatchingKernel.setSpatialParameters(sParameters)
280  else:
281  kParameters = num.array(psfMatchingKernel.getKernelParameters())
282  kParameters[0] = kernelSum
283  psfMatchingKernel.setKernelParameters(kParameters)
284 
285  self.log.log(pexLog.Log.INFO, "Psf-match science exposure to reference")
286  psfMatchedExposure = afwImage.ExposureF(exposure.getBBox(), exposure.getWcs())
287  psfMatchedExposure.setFilter(exposure.getFilter())
288  psfMatchedExposure.setCalib(exposure.getCalib())
289  psfMatchedMaskedImage = psfMatchedExposure.getMaskedImage()
290 
291  # Normalize the psf-matching kernel while convolving since its magnitude is meaningless
292  # when PSF-matching one model to another.
293  doNormalize = True
294  afwMath.convolve(psfMatchedMaskedImage, maskedImage, psfMatchingKernel, doNormalize)
295 
296  self.log.log(pexLog.Log.INFO, "done")
297  return pipeBase.Struct(psfMatchedExposure=psfMatchedExposure,
298  psfMatchingKernel=psfMatchingKernel,
299  kernelCellSet=kernelCellSet,
300  metadata=self.metadata)
def _buildCellSet
Build a SpatialCellSet for use with the solve method.
def run
Psf-match an exposure to a model Psf.
void convolve(OutImageT &convolvedImage, InImageT const &inImage, KernelT const &kernel, bool doNormalize, bool doCopyEdge=false)
Old, deprecated version of convolve.

Member Data Documentation

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

Definition at line 225 of file modelPsfMatch.py.

lsst.ip.diffim.modelPsfMatch.ModelPsfMatchTask.kConfig

Definition at line 237 of file modelPsfMatch.py.


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