lsst.meas.algorithms  13.0-18-gc4ad4228+1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
testUtils.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 #
4 # Copyright 2008-2017 AURA/LSST.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <https://www.lsstcorp.org/LegalNotices/>.
22 #
23 #!/usr/bin/env python
24 import numpy
25 
26 import lsst.afw.image as afwImage
27 import lsst.afw.geom as afwGeom
28 from . import SingleGaussianPsf
29 
30 
31 def plantSources(bbox, kwid, sky, coordList, addPoissonNoise=True):
32  """Make an exposure with stars (modelled as Gaussians)
33 
34  @param bbox: parent bbox of exposure
35  @param kwid: kernel width (and height; kernel is square)
36  @param sky: amount of sky background (counts)
37  @param coordList: a list of [x, y, counts, sigma], where:
38  * x,y are relative to exposure origin
39  * counts is the integrated counts for the star
40  * sigma is the Gaussian sigma in pixels
41  @param addPoissonNoise: add Poisson noise to the exposure?
42  """
43  # make an image with sources
44  img = afwImage.ImageD(bbox)
45  meanSigma = 0.0
46  for coord in coordList:
47  x, y, counts, sigma = coord
48  meanSigma += sigma
49 
50  # make a single gaussian psf
51  psf = SingleGaussianPsf(kwid, kwid, sigma)
52 
53  # make an image of it and scale to the desired number of counts
54  thisPsfImg = psf.computeImage(afwGeom.PointD(int(x), int(y)))
55  thisPsfImg *= counts
56 
57  # bbox a window in our image and add the fake star image
58  imgSeg = img.Factory(img, thisPsfImg.getBBox())
59  imgSeg += thisPsfImg
60  meanSigma /= len(coordList)
61 
62  img += sky
63 
64  # add Poisson noise
65  if (addPoissonNoise):
66  numpy.random.seed(seed=1) # make results reproducible
67  imgArr = img.getArray()
68  imgArr[:] = numpy.random.poisson(imgArr)
69 
70  # bundle into a maskedimage and an exposure
71  mask = afwImage.Mask(bbox)
72  var = img.convertFloat()
73  img -= sky
74  mimg = afwImage.MaskedImageF(img.convertFloat(), mask, var)
75  exposure = afwImage.makeExposure(mimg)
76 
77  # insert an approximate psf
78  psf = SingleGaussianPsf(kwid, kwid, meanSigma)
79  exposure.setPsf(psf)
80 
81  return exposure