Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tickets/DM-10848 #1

Merged
merged 5 commits into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 0 additions & 15 deletions python/fakes/FakeSourceLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,8 @@ def addNoise(galImage, detector, rand_gen=None):
variance plane
giving the variance due to the object
"""
ccd = lsst.afw.cameraGeom.cast_Ccd(detector)
gainImage = galImage.Factory(galImage, True)
gainImage.set(0.0)
for ampId in (1, 2, 3, 4):
amp = ccd.findAmp(lsst.afw.cameraGeom.Id(ampId))
ampBBox = amp.getDataSec()
clippedBBox = gainImage.getBBox(lsst.afw.image.PARENT)
clippedBBox.clip(ampBBox)
if clippedBBox.getArea() > 0:
tempImage = gainImage.Factory(gainImage, clippedBBox,
lsst.afw.image.PARENT)
tempImage.set(amp.getElectronicParams().getGain())
del tempImage

# TODO: this is gaussian noise right now, probably good enough
varImage = galImage.Factory(galImage, True)
varImage /= gainImage
if rand_gen is None:
rand_gen = np.random
scale = np.sqrt(np.abs(varImage.getArray())) + 1e-12
Expand Down
58 changes: 29 additions & 29 deletions python/fakes/makeFakeGalaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,11 @@ def galSimFakeSersic(flux, gal, psfImage=None, scaleRad=False, returnObj=True,
else:
serObj = galsim.Exponential(half_light_radius=reff)
if expAll:
print " * Treated as a n=1 Exponential disk : %d" % (gal["ID"])
print(" * Treated as a n=1 Exponential disk : %d" % (gal["ID"]))
elif nSersic == 4.0 or devAll:
serObj = galsim.DeVaucouleurs(half_light_radius=reff, trunc=trunc)
if devAll:
print " * Treated as a n=4 De Vaucouleurs model: %d" % (gal["ID"])
print(" * Treated as a n=4 De Vaucouleurs model: %d" % (gal["ID"]))
elif nSersic <= 0.9:
serObj = galsim.Sersic(nSersic, half_light_radius=reff)
else:
Expand Down Expand Up @@ -616,18 +616,18 @@ def testMakeFake(galList, asciiTab=False, single=True, double=True, real=True):

flux = 10.0 ** ((27.0 - gal['mag']) / 2.5)

print '\n---------------------------------'
print " Input Flux : ", flux
print " Input Parameters : ", gal["sersic_n"], gal["reff"]
print " ", gal["b_a"], gal["theta"]
print('\n---------------------------------')
print(" Input Flux : ", flux)
print(" Input Parameters : ", gal["sersic_n"], gal["reff"])
print(" ", gal["b_a"], gal["theta"])

galArray = galSimFakeSersic(flux, gal, psfImage=psfImage,
plotFake=True, returnObj=False,
trunc=12.0, drawMethod="no_pixel")

print " Output Flux : ", np.sum(galArray)
print " Shape of the Output Array : ", galArray.shape
print '---------------------------------'
print(" Output Flux : ", np.sum(galArray))
print(" Shape of the Output Array : ", galArray.shape)
print('---------------------------------')

# Test DoubleSersic
if double:
Expand All @@ -640,8 +640,8 @@ def testMakeFake(galList, asciiTab=False, single=True, double=True, real=True):

flux = 10.0 ** ((27.0 - gal['mag']) / 2.5)

print '\n---------------------------------'
print " Input Flux : ", flux
print('\n---------------------------------')
print(" Input Flux : ", flux)

(comp1, comp2) = parseDoubleSersic(flux, gal)

Expand All @@ -654,26 +654,26 @@ def testMakeFake(galList, asciiTab=False, single=True, double=True, real=True):
# It seems that b/a = 0.25 is fine, so right now, just change the
# lower limit of b/a to 0.25

print " Flux for Component 1 : ", comp1['mag']
print " Flux for Component 2 : ", comp2['mag']
print " Comp 1 Parameters : %5.2f %8.2f" % (comp1["sersic_n"],
comp1["reff"])
print " %5.2f %8.2f" % (comp1["b_a"],
comp1["theta"])
print " Comp 2 Parameters : %5.2f %8.2f" % (comp2["sersic_n"],
comp2["reff"])
print " %5.2f %8.2f" % (comp2["b_a"],
comp2["theta"])
print(" Flux for Component 1 : ", comp1['mag'])
print(" Flux for Component 2 : ", comp2['mag'])
print(" Comp 1 Parameters : %5.2f %8.2f" % (comp1["sersic_n"],
comp1["reff"]))
print(" %5.2f %8.2f" % (comp1["b_a"],
comp1["theta"]))
print(" Comp 2 Parameters : %5.2f %8.2f" % (comp2["sersic_n"],
comp2["reff"]))
print(" %5.2f %8.2f" % (comp2["b_a"],
comp2["theta"]))

doubleArray = galSimFakeDoubleSersic(comp1, comp2,
psfImage=psfImage,
trunc=12, returnObj=False,
devExp=True, plotFake=True,
drawMethod='no_pixel')

print " Output Flux : ", np.sum(doubleArray)
print " Shape of the Output Array : ", doubleArray.shape
print '---------------------------------'
print(" Output Flux : ", np.sum(doubleArray))
print(" Shape of the Output Array : ", doubleArray.shape)
print('---------------------------------')

# Test RealGalaxy
if real:
Expand Down Expand Up @@ -707,9 +707,9 @@ def testMakeFake(galList, asciiTab=False, single=True, double=True, real=True):
returnObj=False,
drawMethod='no_pixel')

print '\n---------------------------------'
print " Input Flux : ", flux
print('\n---------------------------------')
print(" Input Flux : ", flux)

print " Output Flux : ", np.sum(realArray)
print " Shape of the Output Array : ", realArray.shape
print '---------------------------------'
print(" Output Flux : ", np.sum(realArray))
print(" Shape of the Output Array : ", realArray.shape)
print('---------------------------------')
32 changes: 17 additions & 15 deletions python/fakes/positionGalSimFakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import lsst.afw.coord
import lsst.pex.config as lsstConfig

from lsst.pipe.tasks.fakes import FakeSourcesConfig, FakeSourcesTask
from lsst.pipe.tasks.fakes import BaseFakeSourcesConfig, BaseFakeSourcesTask

import FakeSourceLib as fsl
import makeFakeGalaxy as makeFake


class PositionGalSimFakesConfig(FakeSourcesConfig):
class PositionGalSimFakesConfig(BaseFakeSourcesConfig):
galList = lsstConfig.Field(dtype=str,
doc="catalog of galaxies to add")
maxMargin = lsstConfig.Field(dtype=int, default=600,
Expand Down Expand Up @@ -52,13 +52,13 @@ class PositionGalSimFakesConfig(FakeSourcesConfig):
doc='Exclusion level')


class PositionGalSimFakesTask(FakeSourcesTask):
class PositionGalSimFakesTask(BaseFakeSourcesTask):
ConfigClass = PositionGalSimFakesConfig

def __init__(self, **kwargs):
FakeSourcesTask.__init__(self, **kwargs)
print "RNG seed:", self.config.seed
self.rng = lsst.afw.math.Random(self.config.seed)
BaseFakeSourcesTask.__init__(self, **kwargs)
print("RNG seed:", self.config.seed)
self.rng = lsst.afw.math.Random(seed=self.config.seed)
self.npRand = np.random.RandomState(self.config.seed)
self.galData = fits.open(self.config.galList)[1].data

Expand Down Expand Up @@ -245,11 +245,12 @@ def run(self, exposure, background):
galXY.getY()))

galMaskedImage.getMask().set(self.bitmask)
try:
galMaskedImage.getMask().removeAndClearMaskPlane('FAKE',
True)
except Exception:
pass
if not self.config.addMask:
try:
galMaskedImage.getMask().removeAndClearMaskPlane('FAKE',
True)
except Exception:
pass
try:
galMaskedImage.getMask().removeAndClearMaskPlane('CROSSTALK',
True)
Expand All @@ -272,10 +273,11 @@ def run(self, exposure, background):
True)
except Exception:
pass
try:
maskedImage.getMask().removeAndClearMaskPlane('FAKE', True)
except Exception:
pass
if not self.config.addMask:
try:
maskedImage.getMask().removeAndClearMaskPlane('FAKE', True)
except Exception:
pass

BBox = galMaskedImage.getBBox(PARENT)
subMaskedImage = maskedImage.Factory(exposure.getMaskedImage(),
Expand Down
12 changes: 6 additions & 6 deletions python/fakes/positionStarFakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@
import lsst.afw.image as afwImage
import lsst.pex.config as afwConfig

from lsst.pipe.tasks.fakes import FakeSourcesConfig, FakeSourcesTask
from lsst.pipe.tasks.fakes import BaseFakeSourcesConfig, BaseFakeSourcesTask

import FakeSourceLib as fsl


class PositionStarFakesConfig(FakeSourcesConfig):
class PositionStarFakesConfig(BaseFakeSourcesConfig):
starList = afwConfig.Field(dtype=str,
doc="Catalog of stars with mags ra/dec")
seed = afwConfig.Field(dtype=int, default=1,
doc="Seed for random number generator")


class PositionStarFakesTask(FakeSourcesTask):
class PositionStarFakesTask(BaseFakeSourcesTask):
ConfigClass = PositionStarFakesConfig

def __init__(self, **kwargs):
FakeSourcesTask.__init__(self, **kwargs)
print "RNG seed:", self.config.seed
self.rng = afwMath.Random(self.config.seed)
BaseFakeSourcesTask.__init__(self, **kwargs)
print("RNG seed:", self.config.seed)
self.rng = afwMath.Random(seed=self.config.seed)
self.npRand = np.random.RandomState(self.config.seed)
try:
self.starData = fits.open(self.config.starList)[1].data
Expand Down
27 changes: 13 additions & 14 deletions python/fakes/randomGalSimFakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import lsst.afw.math
import lsst.afw.cameraGeom
import lsst.pex.config
from lsst.pipe.tasks.fakes import FakeSourcesConfig, FakeSourcesTask
from lsst.pipe.tasks.fakes import BaseFakeSourcesConfig, BaseFakeSourcesTask
import pyfits as fits

import makeFakeGalaxy as makeFake
import FakeSourceLib as fsl
import fakes.makeFakeGalaxy as makeFake
import fakes.FakeSourceLib as fsl

class RandomGalSimFakesConfig(FakeSourcesConfig):
class RandomGalSimFakesConfig(BaseFakeSourcesConfig):
galList = lsst.pex.config.Field(dtype=str, doc="catalog of galaxies to add")
margin = lsst.pex.config.Field(dtype=int, default=None, optional=True,
doc="Size of margin at edge that should not be added")
Expand All @@ -22,17 +22,17 @@ class RandomGalSimFakesConfig(FakeSourcesConfig):
'sersic':'single sersic galaxies added',
'real':'real HST galaxy images added'},
doc='type of GalSim galaxies to add')
nGal = lsst.pex.config.Field(dtype=int, doc="""number of galaxies to add, if 0, then everything in catalog,
nGal = lsst.pex.config.Field(dtype=int, doc="""number of galaxies to add, if 0, then everything in catalog,
otherwise a random subset of nGal from the catalog""", default=0)


class RandomGalSimFakesTask(FakeSourcesTask):
class RandomGalSimFakesTask(BaseFakeSourcesTask):
ConfigClass = RandomGalSimFakesConfig

def __init__(self, **kwargs):
FakeSourcesTask.__init__(self, **kwargs)
print "RNG seed:", self.config.seed
self.rng = lsst.afw.math.Random(self.config.seed)
BaseFakeSourcesTask.__init__(self, **kwargs)
print("RNG seed:", self.config.seed)
self.rng = lsst.afw.math.Random(seed=self.config.seed)
self.npRand = np.random.RandomState(self.config.seed)
self.galData = fits.open(self.config.galList)[1].data

Expand All @@ -42,7 +42,7 @@ def run(self, exposure, background):
self.log.info("Adding fake random galaxies")
psf = exposure.getPsf()
psfBBox = psf.computeImage().getBBox()
minMargin = max(psfBBox.getWidth(), psfBBox.getHeight())/2 + 1
minMargin = int(np.floor(max(psfBBox.getWidth(), psfBBox.getHeight())/2)) + 1
md = exposure.getMetadata()
expBBox = exposure.getBBox()
scalingMatrix = np.array([[0.0,1.0],[1.0,0.0]]) / exposure.getWcs().pixelScale().asArcseconds()
Expand Down Expand Up @@ -88,7 +88,7 @@ def run(self, exposure, background):
'lanczos3')
galBBox = galImage.getBBox(lsst.afw.image.PARENT)


#check that we're within the larger exposure, otherwise crop
if expBBox.contains(galImage.getBBox(lsst.afw.image.PARENT)) is False:
newBBox = galImage.getBBox(lsst.afw.image.PARENT)
Expand All @@ -97,11 +97,11 @@ def run(self, exposure, background):
galImage = galImage.Factory(galImage, newBBox, lsst.afw.image.PARENT)
galBBox = newBBox


galMaskedImage = fsl.addNoise(galImage, exposure.getDetector(), rand_gen=self.npRand)
mask = galMaskedImage.getMask()
mask.set(self.bitmask)

md.set("FAKE%d" % gal['ID'], "%.3f, %.3f" % (x, y))
self.log.info("Adding fake at: %.1f,%.1f"% (x, y))

Expand All @@ -111,4 +111,3 @@ def run(self, exposure, background):
galMaskedImage.getBBox(lsst.afw.image.PARENT),
lsst.afw.image.PARENT)
subMaskedImage += galMaskedImage

18 changes: 9 additions & 9 deletions python/fakes/randomStarFakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@
import lsst.afw.geom
import lsst.afw.math
import lsst.pex.config
from lsst.pipe.tasks.fakes import FakeSourcesConfig, FakeSourcesTask
from lsst.pipe.tasks.fakes import BaseFakeSourcesConfig, BaseFakeSourcesTask

import FakeSourceLib as fsl
import fakes.FakeSourceLib as fsl
import numpy as np

class RandomStarFakeSourcesConfig(FakeSourcesConfig):
class RandomStarFakeSourcesConfig(BaseFakeSourcesConfig):
nStars = lsst.pex.config.Field(dtype=int, default=1,
doc="Number of stars to add")
magnitude = lsst.pex.config.Field(dtype=float, default=20.0,
doc="Magnitude of all stars to be added")
margin = lsst.pex.config.Field(dtype=int, default=None, optional=True,
doc="Size of margin at edge that should not be added")
seed = lsst.pex.config.Field(dtype=int, default=0,
seed = lsst.pex.config.Field(dtype=int, default=1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the default seed changed to 1?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply because the code fails if the seed is zero with an InvalidParameterError with the current stack. As it was just a seed, I figured the value didnt matter

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Sometimes I've seed=0 mean use the current time stamp, but I don't know if that is the case with our software. If seed=0 is not allowed, than it probably doesn't matter.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is produced:

lsst.afw.math.Random(seed=0)
---------------------------------------------------------------------------
InvalidParameterError                     Traceback (most recent call last)
<ipython-input-41-e4860cd80795> in <module>()
----> 1 lsst.afw.math.Random(seed=0)

InvalidParameterError: 
  File "src/math/Random.cc", line 66, in void lsst::afw::math::Random::initialize()
    Invalid RNG seed: 0 {0}
lsst::pex::exceptions::InvalidParameterError: 'Invalid RNG seed: 0'

doc="Seed for random number generator")


class RandomStarFakeSourcesTask(FakeSourcesTask):
class RandomStarFakeSourcesTask(BaseFakeSourcesTask):
ConfigClass = RandomStarFakeSourcesConfig

def __init__(self, **kwargs):
FakeSourcesTask.__init__(self, **kwargs)
print "RNG seed:", self.config.seed
self.rng = lsst.afw.math.Random(self.config.seed)
BaseFakeSourcesTask.__init__(self, **kwargs)
print("RNG seed:", self.config.seed)
self.rng = lsst.afw.math.Random(seed=self.config.seed)
self.npRand = np.random.RandomState(self.config.seed)

def run(self, exposure, background):

self.log.info("Adding fake random stars")
psf = exposure.getPsf()
psfBBox = psf.computeImage().getBBox()
margin = max(psfBBox.getWidth(), psfBBox.getHeight())/2 + 1
margin = int(np.floor(max(psfBBox.getWidth(), psfBBox.getHeight())/2)) + 1
if self.config.margin is not None:
if self.config.margin < margin:
raise ValueError("margin is not large enough for PSF")
Expand Down