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

Make PsfexPsfDeterminer a task #9

Merged
merged 1 commit into from
Jun 7, 2016
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
49 changes: 12 additions & 37 deletions python/lsst/meas/extensions/psfex/psfexPsfDeterminer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import lsst.daf.base as dafBase
import lsst.pex.config as pexConfig
import lsst.pex.exceptions as pexExceptions
import lsst.pex.logging as pexLog
import lsst.afw.cameraGeom as afwCG
import lsst.afw.geom as afwGeom
import lsst.afw.geom.ellipses as afwEll
Expand All @@ -37,10 +36,9 @@
import lsst.afw.math as afwMath
import lsst.meas.algorithms as measAlg
import lsst.meas.algorithms.utils as maUtils
import lsst.meas.algorithms.psfDeterminerRegistry as psfDeterminerRegistry
import lsst.meas.extensions.psfex as psfex

class PsfexPsfDeterminerConfig(pexConfig.Config):
class PsfexPsfDeterminerConfig(measAlg.BasePsfDeterminerConfig):
__nEigenComponents = pexConfig.Field(
doc = "number of eigen components for PSF kernel creation",
dtype = int,
Expand Down Expand Up @@ -71,21 +69,6 @@ class PsfexPsfDeterminerConfig(pexConfig.Config):
dtype = int,
default = 3,
)
kernelSize = pexConfig.Field(
doc = "radius of the kernel to create, relative to the square root of the stellar quadrupole moments",
dtype = float,
default = 81.0,
)
kernelSizeMin = pexConfig.Field(
doc = "Minimum radius of the kernel",
dtype = int,
default = 25,
)
kernelSizeMax = pexConfig.Field(
doc = "Maximum radius of the kernel",
dtype = int,
default = 45,
)
samplingSize = pexConfig.Field(
doc = "Resolution of the internal PSF model relative to the pixel size; e.g. 0.5 is equal to 2x oversampling",
dtype = float,
Expand Down Expand Up @@ -144,19 +127,11 @@ class PsfexPsfDeterminerConfig(pexConfig.Config):
default = False,
)

class PsfexPsfDeterminer(object):
ConfigClass = PsfexPsfDeterminerConfig

def __init__(self, config):
"""Construct a PSFEX PSF Fitter
def setDefaults(self):
self.kernelSize = 81

@param[in] config: instance of PsfexPsfDeterminerConfig
"""
self.config = config
# N.b. name of component is meas.algorithms.psfDeterminer so you can turn on psf debugging
# independent of which determiner is active
self.debugLog = pexLog.Debug("meas.algorithms.psfDeterminer")
self.warnLog = pexLog.Log(pexLog.getDefaultLog(), "meas.algorithms.psfDeterminer")
class PsfexPsfDeterminerTask(measAlg.BasePsfDeterminerTask):
ConfigClass = PsfexPsfDeterminerConfig

def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
"""Determine a PSFEX PSF model for an exposure given a list of PSF candidates
Expand Down Expand Up @@ -209,7 +184,7 @@ def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
if psfCellSet:
psfCellSet.insertCandidate(psfCandidate)
except Exception, e:
self.debugLog.debug(2, "Skipping PSF candidate %d of %d: %s" % (i, len(psfCandidateList), e))
self.log.log(-2, "Skipping PSF candidate %d of %d: %s" % (i, len(psfCandidateList), e))
continue

source = psfCandidate.getSource()
Expand All @@ -218,7 +193,7 @@ def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
sizes[i] = rmsSize

if self.config.kernelSize >= 15:
self.debugLog.debug(1, \
self.log.log(-1, \
"WARNING: NOT scaling kernelSize by stellar quadrupole moment, but using absolute value")
actualKernelSize = int(self.config.kernelSize)
else:
Expand All @@ -236,7 +211,7 @@ def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
if self.config.samplingSize > 0:
pixKernelSize = int(actualKernelSize*self.config.samplingSize)
if pixKernelSize % 2 == 0: pixKernelSize += 1
self.debugLog.debug(3, "Psfex Kernel size=%.2f, Image Kernel Size=%.2f" %
self.log.log(-3, "Psfex Kernel size=%.2f, Image Kernel Size=%.2f" %
(actualKernelSize,pixKernelSize))
psfCandidateList[0].setHeight(pixKernelSize)
psfCandidateList[0].setWidth(pixKernelSize)
Expand Down Expand Up @@ -271,7 +246,7 @@ def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
gain = np.mean(np.array([a.getGain() for a in ccd]))
else:
gain = 1.0
self.warnLog.log(pexLog.Log.WARN, "Setting gain to %g" % gain)
self.log.warn("Setting gain to %g" % (gain,))

contextvalp = []
for i, key in enumerate(context.getName()):
Expand All @@ -289,7 +264,7 @@ def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
contextvalp.append(np.array([psfCandidateList[_].getSource().get(key)
for _ in range(nCand)]))
except KeyError:
raise RuntimeError("*Error*: %s parameter not found" % (key))
raise RuntimeError("*Error*: %s parameter not found" % (key,))
set.setContextname(i, key)

if display:
Expand Down Expand Up @@ -346,7 +321,7 @@ def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
for j in range(set.getNcontext()):
sample.setContext(j, float(contextvalp[j][i]))
except Exception as e:
self.debugLog.debug(2, "Exception when processing sample at (%f,%f): %s" % (xc,yc,e))
self.log.log(-2, "Exception when processing sample at (%f,%f): %s" % (xc, yc, e))
continue
else:
set.finiSample(sample)
Expand Down Expand Up @@ -451,4 +426,4 @@ def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

psfDeterminerRegistry.register("psfex", PsfexPsfDeterminer)
measAlg.psfDeterminerRegistry.register("psfex", PsfexPsfDeterminerTask)
6 changes: 3 additions & 3 deletions tests/testPsfexPsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ def setupDeterminer(self, exposure):

starSelector = measAlg.ObjectSizeStarSelectorTask(schema=self.schema, config=starSelectorConfig)

psfDeterminerFactory = measAlg.psfDeterminerRegistry["psfex"]
psfDeterminerConfig = psfDeterminerFactory.ConfigClass()
psfDeterminerClass = measAlg.psfDeterminerRegistry["psfex"]
psfDeterminerConfig = psfDeterminerClass.ConfigClass()
width, height = exposure.getMaskedImage().getDimensions()
psfDeterminerConfig.sizeCellX = width
psfDeterminerConfig.sizeCellY = height//3
Expand All @@ -232,7 +232,7 @@ def setupDeterminer(self, exposure):
# Include oversampling in test
psfDeterminerConfig.kernelSize = 61
psfDeterminerConfig.samplingSize = 0.5
psfDeterminer = psfDeterminerFactory(psfDeterminerConfig)
psfDeterminer = psfDeterminerClass(psfDeterminerConfig)

return starSelector, psfDeterminer

Expand Down