LSSTApplications  19.0.0-14-gb0260a2+599893a4c6,20.0.0+126303c00d,20.0.0+2f3d0e5c40,20.0.0+36ef800059,20.0.0+5ac7adcc0c,20.0.0+693a64958a,20.0.0+bebc1f60e8,20.0.0+cad136aba6,20.0.0+e2e26847c2,20.0.0+e69b5d60e7,20.0.0-1-g10df615+11b215b765,20.0.0-1-g253301a+36ef800059,20.0.0-1-g2b7511a+bebc1f60e8,20.0.0-1-g4d801e7+aeeb640673,20.0.0-1-g5b95a8c+f111d5f02f,20.0.0-1-g660595b+f45b7d88f4,20.0.0-1-gc96f8cb+e3b38461e6,20.0.0-1-gd1c87d7+85c46248f3,20.0.0-1-gedffbd8+d0b27f8bcb,20.0.0-16-g111fe95+e3b38461e6,20.0.0-16-g18096c8+d1a4df0137,20.0.0-16-g233ea98+a4df35922d,20.0.0-17-ga9337b4+41f27cfd54,20.0.0-2-g4dae9ad+e3b38461e6,20.0.0-2-g7818986+85c46248f3,20.0.0-2-gec03fae+ff10c6d78d,20.0.0-28-g282f9e7e+feda6aebd8,20.0.0-3-g4cc78c6+63636aeed8,20.0.0-3-g6a8623c+d1a4df0137,20.0.0-3-g750bffe+f5427621ce,20.0.0-4-gfea843c+f45b7d88f4,20.0.0-5-g357b56b+f45b7d88f4,20.0.0-5-gfcebe35+e2b15ed341,20.0.0-52-g73d9071+9bf1eb8e0a,20.0.0-7-gcda7bf1+773ba852cb,20.0.0-8-g4540fe2a+952f6d3c43,20.0.0-9-g61a2a9a3d+14f89e4eca,w.2020.40
LSSTDataManagementBasePackage
randomGalSimFakes.py
Go to the documentation of this file.
1 import numpy as np
2 from astropy.io import fits
3 
4 import lsst.afw.image
5 import lsst.afw.geom
6 import lsst.afw.math
8 import lsst.pex.config
9 from lsst.pipe.tasks.fakes import BaseFakeSourcesConfig, BaseFakeSourcesTask
10 import lsst.synpipe.makeFakeGalaxy as makeFake
11 
12 
14  galList = lsst.pex.config.Field(dtype=str, doc="catalog of galaxies to add")
15  margin = lsst.pex.config.Field(dtype=int, default=None, optional=True,
16  doc="Size of margin at edge that should not be added")
17  seed = lsst.pex.config.Field(dtype=int, default=1,
18  doc="Seed for random number generator")
19  galType = lsst.pex.config.ChoiceField(dtype=str, default='sersic',
20  allowed={'dsersic': 'double sersic galaxies added',
21  'sersic': 'single sersic galaxies added',
22  'real': 'real HST galaxy images added'},
23  doc='type of GalSim galaxies to add')
24  nGal = lsst.pex.config.Field(dtype=int, doc="""number of galaxies to add, if 0, then everything in catalog,
25  otherwise a random subset of nGal from the catalog""", default=0)
26 
27 
29  ConfigClass = RandomGalSimFakesConfig
30 
31  def __init__(self, **kwargs):
32  BaseFakeSourcesTask.__init__(self, **kwargs)
33  print("RNG seed:", self.config.seed)
34  self.rng = lsst.afw.math.Random(seed=self.config.seed)
35  self.npRand = np.random.RandomState(self.config.seed)
36  self.galData = fits.open(self.config.galList)[1].data
37 
38  def run(self, exposure, background):
39 
40  self.log.info("Adding fake random galaxies")
41  psf = exposure.getPsf()
42  psfBBox = psf.computeImage().getBBox()
43  minMargin = int(np.floor(max(psfBBox.getWidth(), psfBBox.getHeight())/2)) + 1
44  md = exposure.getMetadata()
45  expBBox = exposure.getBBox()
46  scalingMatrix = np.array([[0.0, 1.0], [1.0, 0.0]]) / exposure.getWcs().pixelScale().asArcseconds()
47 
48  if self.config.nGal == 0:
49  doGal = enumerate(self.galData)
50  else:
51  inds = self.npRand.choice(list(range(len(self.galData))), size=self.config.nGal, replace=False)
52  doGal = list(zip(inds, self.galData[inds]))
53 
54  for igal, gal in doGal:
55  try:
56  galident = gal["ID"]
57  except KeyError:
58  galident = igal + 1
59 
60  try:
61  flux = exposure.getCalib().getFlux(float(gal['mag']))
62  except KeyError:
63  raise KeyError("No mag column in %s table"%self.config.galList)
64 
65  # don't put the galaxy within one PSF box of the edge
66  # or within the given pixel margin
67  if self.config.margin is not None:
68  margin = self.config.margin
69  else:
70  margin = minMargin
71  bboxI = (exposure.getBBox(lsst.afw.image.PARENT))
72  bboxI.grow(-margin)
73  bboxD = lsst.afw.geom.BoxD(bboxI)
74  x = self.rng.flat(bboxD.getMinX(), bboxD.getMaxX())
75  y = self.rng.flat(bboxD.getMinY(), bboxD.getMaxY())
76  # TODO: check for overlaps here and regenerate x,y if necessary
77 
78  psfImage = psf.computeKernelImage(lsst.afw.geom.Point2D(x, y))
79  galArray = makeFake.makeGalaxy(flux, gal, psfImage.getArray(), self.config.galType,
80  transform=scalingMatrix)
81  galImage = lsst.afw.image.ImageF(galArray.astype(np.float32))
82  galBBox = galImage.getBBox(lsst.afw.image.PARENT)
83  galImage = lsst.afw.math.offsetImage(galImage,
84  x - galBBox.getWidth()/2.0 + 0.5,
85  y - galBBox.getHeight()/2.0 + 0.5,
86  'lanczos3')
87  galBBox = galImage.getBBox(lsst.afw.image.PARENT)
88 
89  # check that we're within the larger exposure, otherwise crop
90  if expBBox.contains(galImage.getBBox(lsst.afw.image.PARENT)) is False:
91  newBBox = galImage.getBBox(lsst.afw.image.PARENT)
92  newBBox.clip(expBBox)
93  self.log.info("Cropping FAKE%d from %s to %s"%(galident, str(galBBox), str(newBBox)))
94  galImage = galImage.Factory(galImage, newBBox, lsst.afw.image.PARENT)
95  galBBox = newBBox
96 
97  galMaskedImage = lsst.afw.image.MaskedImageF(galImage)
98 
99  mask = galMaskedImage.getMask()
100  mask.set(self.bitmask)
101 
102  md.set("FAKE%d" % gal['ID'], "%.3f, %.3f" % (x, y))
103  self.log.info("Adding fake at: %.1f,%.1f" % (x, y))
104 
105  # TODO: set the mask
106  galMaskedImage.getMask().set(self.bitmask)
107  subMaskedImage = exposure.getMaskedImage().Factory(exposure.getMaskedImage(),
108  galMaskedImage.getBBox(lsst.afw.image.PARENT),
109  lsst.afw.image.PARENT)
110  subMaskedImage += galMaskedImage
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::log.log.logContinued.info
def info(fmt, *args)
Definition: logContinued.py:201
lsst.synpipe.randomGalSimFakes.RandomGalSimFakesTask.__init__
def __init__(self, **kwargs)
Definition: randomGalSimFakes.py:31
lsst.pipe.tasks.fakes.BaseFakeSourcesTask.bitmask
bitmask
Definition: fakes.py:61
lsst.pipe.tasks.fakes.BaseFakeSourcesConfig
Definition: fakes.py:29
lsst.pipe.tasks.fakes.BaseFakeSourcesTask
Definition: fakes.py:36
lsst.synpipe.randomGalSimFakes.RandomGalSimFakesTask.run
def run(self, exposure, background)
Definition: randomGalSimFakes.py:38
lsst.synpipe.makeFakeGalaxy
Definition: makeFakeGalaxy.py:1
lsst.pex.config
Definition: __init__.py:1
lsst.pipe.base.task.Task.config
config
Definition: task.py:149
lsst.pipe.base.task.Task.log
log
Definition: task.py:148
max
int max
Definition: BoundedField.cc:104
lsst.pex.config.choiceField.ChoiceField
Definition: choiceField.py:34
lsst.synpipe.randomGalSimFakes.RandomGalSimFakesTask.npRand
npRand
Definition: randomGalSimFakes.py:35
lsst.pipe.tasks.fakes
Definition: fakes.py:1
lsst::afw::cameraGeom
Definition: Amplifier.h:33
lsst::afw::math::Random
A class that can be used to generate sequences of random numbers according to a number of different a...
Definition: Random.h:57
lsst::afw::image.slicing.Factory
Factory
Definition: slicing.py:252
list
daf::base::PropertyList * list
Definition: fits.cc:913
lsst::afw::math
Definition: statistics.dox:6
lsst.synpipe.randomGalSimFakes.RandomGalSimFakesConfig
Definition: randomGalSimFakes.py:13
lsst::afw::math::offsetImage
std::shared_ptr< ImageT > offsetImage(ImageT const &image, float dx, float dy, std::string const &algorithmName="lanczos5", unsigned int buffer=0)
Return an image offset by (dx, dy) using the specified algorithm.
Definition: offsetImage.cc:41
lsst.synpipe.randomGalSimFakes.RandomGalSimFakesTask.galData
galData
Definition: randomGalSimFakes.py:36
lsst.synpipe.randomGalSimFakes.RandomGalSimFakesTask
Definition: randomGalSimFakes.py:28
lsst.pex.config.config.Field
Definition: config.py:247
lsst.synpipe.randomGalSimFakes.RandomGalSimFakesTask.rng
rng
Definition: randomGalSimFakes.py:34
set
daf::base::PropertySet * set
Definition: fits.cc:912
lsst::afw::geom
Definition: frameSetUtils.h:40