lsst.meas.algorithms  13.0-24-g22030a45+2
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 import numpy
24 
25 import lsst.afw.image as afwImage
26 import lsst.afw.geom as afwGeom
27 from . import SingleGaussianPsf
28 
29 
30 def plantSources(bbox, kwid, sky, coordList, addPoissonNoise=True):
31  """Make an exposure with stars (modelled as Gaussians)
32 
33  @param bbox: parent bbox of exposure
34  @param kwid: kernel width (and height; kernel is square)
35  @param sky: amount of sky background (counts)
36  @param coordList: a list of [x, y, counts, sigma], where:
37  * x,y are relative to exposure origin
38  * counts is the integrated counts for the star
39  * sigma is the Gaussian sigma in pixels
40  @param addPoissonNoise: add Poisson noise to the exposure?
41  """
42  # make an image with sources
43  img = afwImage.ImageD(bbox)
44  meanSigma = 0.0
45  for coord in coordList:
46  x, y, counts, sigma = coord
47  meanSigma += sigma
48 
49  # make a single gaussian psf
50  psf = SingleGaussianPsf(kwid, kwid, sigma)
51 
52  # make an image of it and scale to the desired number of counts
53  thisPsfImg = psf.computeImage(afwGeom.PointD(int(x), int(y)))
54  thisPsfImg *= counts
55 
56  # bbox a window in our image and add the fake star image
57  imgSeg = img.Factory(img, thisPsfImg.getBBox())
58  imgSeg += thisPsfImg
59  meanSigma /= len(coordList)
60 
61  img += sky
62 
63  # add Poisson noise
64  if (addPoissonNoise):
65  numpy.random.seed(seed=1) # make results reproducible
66  imgArr = img.getArray()
67  imgArr[:] = numpy.random.poisson(imgArr)
68 
69  # bundle into a maskedimage and an exposure
70  mask = afwImage.Mask(bbox)
71  var = img.convertFloat()
72  img -= sky
73  mimg = afwImage.MaskedImageF(img.convertFloat(), mask, var)
74  exposure = afwImage.makeExposure(mimg)
75 
76  # insert an approximate psf
77  psf = SingleGaussianPsf(kwid, kwid, meanSigma)
78  exposure.setPsf(psf)
79 
80  return exposure
def plantSources(bbox, kwid, sky, coordList, addPoissonNoise=True)
Definition: testUtils.py:30