LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
Classes | Functions
lsst.ip.diffim.diffimTools Namespace Reference

Classes

class  NbasisEvaluator
 

Functions

def makeFlatNoiseImage
 Add noise. More...
 
def makePoissonNoiseImage
 
def fakeCoeffs
 Make fake images for testing; one is a delta function (or narrow gaussian) and the other is a convolution of this with a spatially varying kernel. More...
 
def makeFakeKernelSet
 
def backgroundSubtract
 Background subtraction for ip_diffim. More...
 
def writeKernelCellSet
 More coarse debugging. More...
 
def sourceToFootprintList
 Converting types. More...
 
def sourceTableToCandidateList
 

Function Documentation

def lsst.ip.diffim.diffimTools.backgroundSubtract (   config,
  maskedImages 
)

Background subtraction for ip_diffim.

Definition at line 222 of file diffimTools.py.

223 def backgroundSubtract(config, maskedImages):
224  backgrounds = []
225  t0 = time.time()
226  algorithm = config.algorithm
227  binsize = config.binSize
228  undersample = config.undersampleStyle
229  bctrl = afwMath.BackgroundControl(algorithm)
230  bctrl.setUndersampleStyle(undersample)
231  for maskedImage in maskedImages:
232  bctrl.setNxSample(maskedImage.getWidth()//binsize + 1)
233  bctrl.setNySample(maskedImage.getHeight()//binsize + 1)
234  image = maskedImage.getImage()
235  backobj = afwMath.makeBackground(image, bctrl)
236 
237  image -= backobj.getImageF()
238  backgrounds.append(backobj.getImageF())
239  del backobj
240 
241  t1 = time.time()
242  pexLog.Trace("lsst.ip.diffim.backgroundSubtract", 1,
243  "Total time for background subtraction : %.2f s" % (t1-t0))
244  return backgrounds
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
def backgroundSubtract
Background subtraction for ip_diffim.
Definition: diffimTools.py:222
limited backward compatibility to the DC2 run-time trace facilities
Definition: Trace.h:93
Pass parameters to a Background object.
Definition: Background.h:61
def lsst.ip.diffim.diffimTools.fakeCoeffs ( )

Make fake images for testing; one is a delta function (or narrow gaussian) and the other is a convolution of this with a spatially varying kernel.

Definition at line 82 of file diffimTools.py.

82 
83 def fakeCoeffs():
84  kCoeffs = (( 1.0, 0.0, 0.0),
85  ( 0.005, -0.000001, 0.000001),
86  ( 0.005, 0.000004, 0.000004),
87  ( -0.001, -0.000030, 0.000030),
88  ( -0.001, 0.000015, 0.000015),
89  ( -0.005, -0.000050, 0.000050))
90  return kCoeffs
def fakeCoeffs
Make fake images for testing; one is a delta function (or narrow gaussian) and the other is a convolu...
Definition: diffimTools.py:82
def lsst.ip.diffim.diffimTools.makeFakeKernelSet (   sizeCell = 128,
  nCell = 3,
  deltaFunctionCounts = 1.e4,
  tGaussianWidth = 1.0,
  addNoise = True,
  bgValue = 100.,
  display = False 
)

Definition at line 93 of file diffimTools.py.

93 
94  addNoise = True, bgValue = 100., display = False):
95 
96  from . import imagePsfMatch
98  configFake.kernel.name = "AL"
99  subconfigFake = configFake.kernel.active
100  subconfigFake.alardNGauss = 1
101  subconfigFake.alardSigGauss = [2.5,]
102  subconfigFake.alardDegGauss = [2,]
103  subconfigFake.sizeCellX = sizeCell
104  subconfigFake.sizeCellY = sizeCell
105  subconfigFake.spatialKernelOrder = 1
106  subconfigFake.spatialModelType = "polynomial"
107  subconfigFake.singleKernelClipping = False # variance is a hack
108  subconfigFake.spatialKernelClipping = False # variance is a hack
109  if bgValue > 0.0:
110  subconfigFake.fitForBackground = True
111 
112  policyFake = pexConfig.makePolicy(subconfigFake)
113 
114  basisList = makeKernelBasisList(subconfigFake)
115  kSize = subconfigFake.kernelSize
116 
117  # This sets the final extent of each convolved delta function
118  gaussKernelWidth = sizeCell // 2
119 
120  # This sets the scale over which pixels are correlated in the
121  # spatial convolution; should be at least as big as the kernel you
122  # are trying to fit for
123  spatialKernelWidth = kSize
124 
125  # Number of bad pixels due to convolutions
126  border = (gaussKernelWidth + spatialKernelWidth)//2
127 
128  # Make a fake image with a matrix of delta functions
129  totalSize = nCell * sizeCell + 2 * border
130  tim = afwImage.ImageF(afwGeom.Extent2I(totalSize, totalSize))
131  for x in range(nCell):
132  for y in range(nCell):
133  tim.set(x * sizeCell + sizeCell // 2 + border - 1,
134  y * sizeCell + sizeCell // 2 + border - 1,
135  deltaFunctionCounts)
136 
137  # Turn this into stars with a narrow width; conserve counts
138  gaussFunction = afwMath.GaussianFunction2D(tGaussianWidth, tGaussianWidth)
139  gaussKernel = afwMath.AnalyticKernel(gaussKernelWidth, gaussKernelWidth, gaussFunction)
140  cim = afwImage.ImageF(tim.getDimensions())
141  afwMath.convolve(cim, tim, gaussKernel, True)
142  tim = cim
143 
144  # Trim off border pixels
145  bbox = gaussKernel.shrinkBBox(tim.getBBox(afwImage.LOCAL))
146  tim = afwImage.ImageF(tim, bbox, afwImage.LOCAL)
147 
148  # Now make a science image which is this convolved with some
149  # spatial function. Use input basis list.
150  polyFunc = afwMath.PolynomialFunction2D(1)
151  kCoeffs = fakeCoeffs()
152  nToUse = min(len(kCoeffs), len(basisList))
153 
154  # Make the full convolved science image
155  sKernel = afwMath.LinearCombinationKernel(afwMath.KernelList(basisList[:nToUse]), polyFunc)
156  sKernel.setSpatialParameters(kCoeffs[:nToUse])
157  sim = afwImage.ImageF(tim.getDimensions())
158  afwMath.convolve(sim, tim, sKernel, True)
159 
160  # Get the good subregion
161  bbox = sKernel.shrinkBBox(sim.getBBox(afwImage.LOCAL))
162 
163  # Add background
164  sim += bgValue
165 
166  # Watch out for negative values
167  tim += 2 * np.abs(np.min(tim.getArray()))
168 
169  # Add noise?
170  if addNoise:
171  sim = makePoissonNoiseImage(sim)
172  tim = makePoissonNoiseImage(tim)
173 
174  # And turn into MaskedImages
175  sim = afwImage.ImageF(sim, bbox, afwImage.LOCAL)
176  svar = afwImage.ImageF(sim, True)
177  smask = afwImage.MaskU(sim.getDimensions())
178  smask.set(0x0)
179  sMi = afwImage.MaskedImageF(sim, smask, svar)
180 
181  tim = afwImage.ImageF(tim, bbox, afwImage.LOCAL)
182  tvar = afwImage.ImageF(tim, True)
183  tmask = afwImage.MaskU(tim.getDimensions())
184  tmask.set(0x0)
185  tMi = afwImage.MaskedImageF(tim, tmask, tvar)
186 
187  if display:
188  import lsst.afw.display.ds9 as ds9
189  ds9.mtv(tMi, frame=1)
190  ds9.mtv(sMi, frame=2)
191 
192  # Finally, make a kernelSet from these 2 images
194  afwGeom.Extent2I(sizeCell * nCell,
195  sizeCell * nCell)),
196  sizeCell,
197  sizeCell)
198  stampHalfWidth = 2 * kSize
199  for x in range(nCell):
200  for y in range(nCell):
201  xCoord = x * sizeCell + sizeCell // 2
202  yCoord = y * sizeCell + sizeCell // 2
203  p0 = afwGeom.Point2I(xCoord - stampHalfWidth,
204  yCoord - stampHalfWidth)
205  p1 = afwGeom.Point2I(xCoord + stampHalfWidth,
206  yCoord + stampHalfWidth)
207  bbox = afwGeom.Box2I(p0, p1)
208  tsi = afwImage.MaskedImageF(tMi, bbox, afwImage.LOCAL)
209  ssi = afwImage.MaskedImageF(sMi, bbox, afwImage.LOCAL)
210 
211  kc = diffimLib.makeKernelCandidate(xCoord, yCoord, tsi, ssi, policyFake)
212  kernelCellSet.insertCandidate(kc)
213 
214  tMi.setXY0(0,0)
215  sMi.setXY0(0,0)
216  return tMi, sMi, sKernel, kernelCellSet, configFake
217 
Configuration for image-to-image Psf matching.
An integer coordinate rectangle.
Definition: Box.h:53
A collection of SpatialCells covering an entire image.
Definition: SpatialCell.h:378
A kernel that is a linear combination of fixed basis kernels.
Definition: Kernel.h:814
def fakeCoeffs
Make fake images for testing; one is a delta function (or narrow gaussian) and the other is a convolu...
Definition: diffimTools.py:82
void convolve(OutImageT &convolvedImage, InImageT const &inImage, KernelT const &kernel, bool doNormalize, bool doCopyEdge=false)
Old, deprecated version of convolve.
std::vector< boost::shared_ptr< Kernel > > KernelList
Definition: Kernel.h:542
A kernel described by a function.
Definition: Kernel.h:628
def lsst.ip.diffim.diffimTools.makeFlatNoiseImage (   mi,
  seedStat = afwMath.MAX 
)

Add noise.

Definition at line 49 of file diffimTools.py.

49 
50 def makeFlatNoiseImage(mi, seedStat = afwMath.MAX):
51  img = mi.getImage()
52  seed = int(10. * afwMath.makeStatistics(mi.getImage(), seedStat).getValue() + 1)
53  rdm = afwMath.Random(afwMath.Random.MT19937, seed)
54  rdmImage = img.Factory(img.getDimensions())
55  afwMath.randomGaussianImage(rdmImage, rdm)
56  return rdmImage
void randomGaussianImage(ImageT *image, Random &rand)
Definition: RandomImage.cc:155
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
def makeFlatNoiseImage
Add noise.
Definition: diffimTools.py:49
def lsst.ip.diffim.diffimTools.makePoissonNoiseImage (   im)
Return a Poisson noise image based on im

Uses numpy.random; you may wish to call numpy.random.seed first.

@warning This uses an undocumented numpy API (the documented API
uses a single float expectation value instead of an array).

@param[in] im image; the output image has the same dimensions and shape
    and its expectation value is the value of im at each pixel

Definition at line 57 of file diffimTools.py.

57 
58 def makePoissonNoiseImage(im):
59  """Return a Poisson noise image based on im
60 
61  Uses numpy.random; you may wish to call numpy.random.seed first.
62 
63  @warning This uses an undocumented numpy API (the documented API
64  uses a single float expectation value instead of an array).
65 
66  @param[in] im image; the output image has the same dimensions and shape
67  and its expectation value is the value of im at each pixel
68  """
69  import numpy.random as rand
70  imArr = im.getArray()
71  noiseIm = im.Factory(im.getBBox())
72  noiseArr = noiseIm.getArray()
73 
74  intNoiseArr = rand.poisson(imArr)
75  noiseArr[:, :] = intNoiseArr.astype(noiseArr.dtype)
76  return noiseIm
def lsst.ip.diffim.diffimTools.sourceTableToCandidateList (   sourceTable,
  templateExposure,
  scienceExposure,
  kConfig,
  dConfig,
  log,
  basisList,
  doBuild = False 
)
Takes an input list of Sources, and turns them into
KernelCandidates for fitting of the Psf-matching kernel.

Definition at line 355 of file diffimTools.py.

356  basisList, doBuild=False):
357  """Takes an input list of Sources, and turns them into
358  KernelCandidates for fitting of the Psf-matching kernel."""
359  kernelSize = basisList[0].getWidth()
360  footprintList = sourceToFootprintList(list(sourceTable), templateExposure, scienceExposure,
361  kernelSize, dConfig, log)
362  candList = []
363 
364  if doBuild and not basisList:
365  doBuild = False
366  else:
367  policy = pexConfig.makePolicy(kConfig)
368  visitor = diffimLib.BuildSingleKernelVisitorF(basisList, policy)
369 
370  policy = pexConfig.makePolicy(kConfig)
371  for cand in footprintList:
372  bbox = cand['footprint'].getBBox()
373  tmi = afwImage.MaskedImageF(templateExposure.getMaskedImage(), bbox)
374  smi = afwImage.MaskedImageF(scienceExposure.getMaskedImage(), bbox)
375  kCand = diffimLib.makeKernelCandidate(cand['source'], tmi, smi, policy)
376  if doBuild:
377  visitor.processCandidate(kCand)
378  kCand.setStatus(afwMath.SpatialCellCandidate.UNKNOWN)
379  candList.append(kCand)
380  return candList
381 
def sourceToFootprintList
Converting types.
Definition: diffimTools.py:277
def lsst.ip.diffim.diffimTools.sourceToFootprintList (   candidateInList,
  templateExposure,
  scienceExposure,
  kernelSize,
  config,
  log 
)

Converting types.

Takes an input list of Sources that were selected to constrain
the Psf-matching Kernel and turns them into a List of Footprints,
which are used to seed a set of KernelCandidates.  The function
checks both the template and science image for masked pixels,
rejecting the Source if certain Mask bits (defined in config) are
set within the Footprint.

@param candidateInList: Input list of Sources
@param templateExposure: Template image, to be checked for Mask bits in Source Footprint
@param scienceExposure: Science image, to be checked for Mask bits in Source Footprint
@param config: Config that defines the Mask planes that indicate an invalid Source and Bbox grow radius
@param log: Log for output

@return a list of dicts having a "source" and "footprint" field, to be used for Psf-matching

Definition at line 277 of file diffimTools.py.

278 def sourceToFootprintList(candidateInList, templateExposure, scienceExposure, kernelSize, config, log):
279  """ Takes an input list of Sources that were selected to constrain
280  the Psf-matching Kernel and turns them into a List of Footprints,
281  which are used to seed a set of KernelCandidates. The function
282  checks both the template and science image for masked pixels,
283  rejecting the Source if certain Mask bits (defined in config) are
284  set within the Footprint.
285 
286  @param candidateInList: Input list of Sources
287  @param templateExposure: Template image, to be checked for Mask bits in Source Footprint
288  @param scienceExposure: Science image, to be checked for Mask bits in Source Footprint
289  @param config: Config that defines the Mask planes that indicate an invalid Source and Bbox grow radius
290  @param log: Log for output
291 
292  @return a list of dicts having a "source" and "footprint" field, to be used for Psf-matching
293  """
294 
295  candidateOutList = []
296  fsb = diffimLib.FindSetBitsU()
297  badBitMask = 0
298  for mp in config.badMaskPlanes:
299  badBitMask |= afwImage.MaskU.getPlaneBitMask(mp)
300  bbox = scienceExposure.getBBox()
301 
302  # Size to grow Sources
303  if config.scaleByFwhm:
304  fpGrowPix = int(config.fpGrowKernelScaling * kernelSize + 0.5)
305  else:
306  fpGrowPix = config.fpGrowPix
307  log.info("Growing %d kernel candidate stars by %d pixels" % (len(candidateInList), fpGrowPix))
308 
309  for kernelCandidate in candidateInList:
310  if not type(kernelCandidate) == afwTable.SourceRecord:
311  raise RuntimeError, ("Candiate not of type afwTable.SourceRecord")
312  bm1 = 0
313  bm2 = 0
314  center = afwGeom.Point2I(scienceExposure.getWcs().skyToPixel(kernelCandidate.getCoord()))
315  if center[0] < bbox.getMinX() or center[0] > bbox.getMaxX():
316  continue
317  if center[1] < bbox.getMinY() or center[1] > bbox.getMaxY():
318  continue
319 
320  xmin = center[0] - fpGrowPix
321  xmax = center[0] + fpGrowPix
322  ymin = center[1] - fpGrowPix
323  ymax = center[1] + fpGrowPix
324 
325  # Keep object centered
326  if (xmin - bbox.getMinX()) < 0:
327  xmax += (xmin - bbox.getMinX())
328  xmin -= (xmin - bbox.getMinX())
329  if (ymin - bbox.getMinY()) < 0:
330  ymax += (ymin - bbox.getMinY())
331  ymin -= (ymin - bbox.getMinY())
332  if (bbox.getMaxX() - xmax) < 0:
333  xmin -= (bbox.getMaxX() - xmax)
334  xmax += (bbox.getMaxX() - xmax)
335  if (bbox.getMaxY() - ymax) < 0:
336  ymin -= (bbox.getMaxY() - ymax)
337  ymax += (bbox.getMaxY() - ymax)
338  if xmin > xmax or ymin > ymax:
339  continue
340 
341  kbbox = afwGeom.Box2I(afwGeom.Point2I(xmin, ymin), afwGeom.Point2I(xmax, ymax))
342  try:
343  fsb.apply(afwImage.MaskedImageF(templateExposure.getMaskedImage(), kbbox, False).getMask())
344  bm1 = fsb.getBits()
345  fsb.apply(afwImage.MaskedImageF(scienceExposure.getMaskedImage(), kbbox, False).getMask())
346  bm2 = fsb.getBits()
347  except Exception:
348  pass
349  else:
350  if not((bm1 & badBitMask) or (bm2 & badBitMask)):
351  candidateOutList.append({'source':kernelCandidate, 'footprint':afwDetect.Footprint(kbbox)})
352  log.info("Selected %d / %d sources for KernelCandidacy" % (len(candidateOutList), len(candidateInList)))
353  return candidateOutList
An integer coordinate rectangle.
Definition: Box.h:53
A set of pixels in an Image.
Definition: Footprint.h:62
Record class that contains measurements made on a single exposure.
Definition: Source.h:81
def sourceToFootprintList
Converting types.
Definition: diffimTools.py:277
def lsst.ip.diffim.diffimTools.writeKernelCellSet (   kernelCellSet,
  psfMatchingKernel,
  backgroundModel,
  outdir 
)

More coarse debugging.

Definition at line 249 of file diffimTools.py.

250 def writeKernelCellSet(kernelCellSet, psfMatchingKernel, backgroundModel, outdir):
251  if not os.path.isdir(outdir):
252  os.makedirs(outdir)
253 
254  for cell in kernelCellSet.getCellList():
255  for cand in cell.begin(False): # False = include bad candidates
256  cand = diffimLib.cast_KernelCandidateF(cand)
257  if cand.getStatus() == afwMath.SpatialCellCandidate.GOOD:
258  xCand = int(cand.getXCenter())
259  yCand = int(cand.getYCenter())
260  idCand = cand.getId()
261  diffIm = cand.getDifferenceImage(diffimLib.KernelCandidateF.ORIG)
262  kernel = cand.getKernelImage(diffimLib.KernelCandidateF.ORIG)
263  diffIm.writeFits(os.path.join(outdir, 'diffim_c%d_x%d_y%d.fits' % (idCand, xCand, yCand)))
264  kernel.writeFits(os.path.join(outdir, 'kernel_c%d_x%d_y%d.fits' % (idCand, xCand, yCand)))
265 
266  # Diffim from spatial model
267  ski = afwImage.ImageD(kernel.getDimensions())
268  psfMatchingKernel.computeImage(ski, False, xCand, yCand)
269  sk = afwMath.FixedKernel(ski)
270  sbg = backgroundModel(xCand, yCand)
271  sdmi = cand.getDifferenceImage(sk, sbg)
272  sdmi.writeFits(os.path.join(outdir, 'sdiffim_c%d_x%d_y%d.fits' % (idCand, xCand, yCand)))
def writeKernelCellSet
More coarse debugging.
Definition: diffimTools.py:249
A kernel created from an Image.
Definition: Kernel.h:551