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

DM-5532 #9

Merged
merged 3 commits into from
Mar 30, 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
1 change: 1 addition & 0 deletions python/lsst/ip/diffim/__init__.py
Expand Up @@ -37,6 +37,7 @@
from diffimTools import *
from kernelCandidateQa import *
from getTemplate import *
from .diaCatalogSourceSelector import *
from lsst.meas.base import wrapSimpleAlgorithm

# automatically register ip_diffim Algorithms
Expand Down
121 changes: 89 additions & 32 deletions python/lsst/ip/diffim/diaCatalogSourceSelector.py
Expand Up @@ -20,12 +20,16 @@
# see <http://www.lsstcorp.org/LegalNotices/>.
#
import numpy as np

from lsst.afw.table import SourceCatalog
from lsst.pipe.base import Struct
import lsst.pex.config as pexConfig
import lsst.afw.display.ds9 as ds9
import lsst.meas.algorithms as measAlg
import lsst.pex.logging as pexLog

class DiaCatalogSourceSelectorConfig(pexConfig.Config):
__all__ = ["DiaCatalogSourceSelectorConfig", "DiaCatalogSourceSelectorTask"]

class DiaCatalogSourceSelectorConfig(measAlg.StarSelectorConfig):
# Selection cuts on the input source catalog
fluxLim = pexConfig.Field(
doc = "specify the minimum psfFlux for good Kernel Candidates",
Expand All @@ -39,12 +43,6 @@ class DiaCatalogSourceSelectorConfig(pexConfig.Config):
default = 0.0,
check = lambda x: x >= 0.0,
)
badPixelFlags = pexConfig.ListField(
doc = "Kernel candidate objects may not have any of these bits set",
dtype = str,
default = ["base_PixelFlags_flag_edge", "base_PixelFlags_flag_interpolatedCenter",
"base_PixelFlags_flag_saturatedCenter", "slot_Centroid_flag"],
)
# Selection cuts on the reference catalog
selectStar = pexConfig.Field(
doc = "Select objects that are flagged as stars",
Expand All @@ -71,12 +69,21 @@ class DiaCatalogSourceSelectorConfig(pexConfig.Config):
dtype = float,
default = 3.0
)
def setDefaults(self):
measAlg.StarSelectorConfig.setDefaults(self)
self.badFlags = [
"base_PixelFlags_flag_edge",
"base_PixelFlags_flag_interpolatedCenter",
"base_PixelFlags_flag_saturatedCenter",
"slot_Centroid_flag",
]


class CheckSource(object):
"""A functor to check whether a source has any flags set that should cause it to be labeled bad."""

def __init__(self, table, fluxLim, fluxMax, badPixelFlags):
self.keys = [table.getSchema().find(name).key for name in badPixelFlags]
def __init__(self, table, fluxLim, fluxMax, badFlags):
self.keys = [table.getSchema().find(name).key for name in badFlags]
self.fluxLim = fluxLim
self.fluxMax = fluxMax

Expand All @@ -90,30 +97,81 @@ def __call__(self, source):
return False
return True

class DiaCatalogSourceSelector(object):
## \addtogroup LSST_task_documentation
## \{
## \page DiaCatalogSourceSelectorTask
## \ref DiaCatalogSourceSelectorTask_ "DiaCatalogSourceSelectorTask"
## \copybrief DiaCatalogSourceSelectorTask
## \}

class DiaCatalogSourceSelectorTask(measAlg.StarSelectorTask):
"""!Select sources for Kernel candidates

@anchor DiaCatalogSourceSelectorTask_

@section ip_diffim_diaCatalogSourceSelector_Contents Contents

- @ref ip_diffim_diaCatalogSourceSelector_Purpose
- @ref ip_diffim_diaCatalogSourceSelector_Initialize
- @ref ip_diffim_diaCatalogSourceSelector_IO
- @ref ip_diffim_diaCatalogSourceSelector_Config
- @ref ip_diffim_diaCatalogSourceSelector_Debug

@section ip_diffim_diaCatalogSourceSelector_Purpose Description

A naive star selector based on second moments. Use with caution.

@section ip_diffim_diaCatalogSourceSelector_Initialize Task initialisation

@copydoc \_\_init\_\_

@section ip_diffim_diaCatalogSourceSelector_IO Invoking the Task

Like all star selectors, the main method is `run`.

@section ip_diffim_diaCatalogSourceSelector_Config Configuration parameters

See @ref DiaCatalogSourceSelectorConfig

@section ip_diffim_diaCatalogSourceSelector_Debug Debug variables

DiaCatalogSourceSelectorTask has a debug dictionary with the following keys:
<dl>
<dt>display
<dd>bool; if True display debug information
<dt>displayExposure
<dd>bool; if True display exposure
<dt>pauseAtEnd
<dd>bool; if True wait after displaying everything and wait for user input
</dl>

For example, put something like:
@code{.py}
import lsstDebug
def DebugInfo(name):
di = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively
if name.endswith("catalogStarSelector"):
di.display = True

return di

lsstDebug.Info = DebugInfo
@endcode
into your `debug.py` file and run your task with the `--debug` flag.
"""
ConfigClass = DiaCatalogSourceSelectorConfig
usesMatches = True # selectStars uses (requires) its matches argument

def __init__(self, config=None):
"""Construct a source selector that uses a reference catalog

@param[in] config: An instance of ConfigClass
"""
if not config:
config = DiaCatalogSourceSelector.ConfigClass()
self.config = config
self.log = pexLog.Log(pexLog.Log.getDefaultLog(),
'lsst.ip.diffim.DiaCatalogSourceSelector', pexLog.Log.INFO)

def selectSources(self, exposure, sourceCat, matches=None):
"""Return a list of Sources for Kernel candidates
def selectStars(self, exposure, sourceCat, matches=None):
"""Select sources for Kernel candidates

@param[in] exposure the exposure containing the sources
@param[in] sourceCat catalog of sources that may be stars (an lsst.afw.table.SourceCatalog)
@param[in] matches a match vector as produced by meas_astrom; required
(defaults to None to match the StarSelector API and improve error handling)

@return kernelCandidateSourceList: a list of sources to be used as kernel candidates
@return an lsst.pipe.base.Struct containing:
- starCat a list of sources to be used as kernel candidates
"""
import lsstDebug
display = lsstDebug.Info(__name__).display
Expand All @@ -131,12 +189,12 @@ def selectSources(self, exposure, sourceCat, matches=None):
#
# Look for flags in each Source
#
isGoodSource = CheckSource(sourceCat, self.config.fluxLim, self.config.fluxMax, self.config.badPixelFlags)
isGoodSource = CheckSource(sourceCat, self.config.fluxLim, self.config.fluxMax, self.config.badFlags)

#
# Go through and find all the acceptable candidates in the catalogue
#
kernelCandidateSourceList = []
starCat = SourceCatalog(sourceCat.schema)

doColorCut = True
with ds9.Buffering():
Expand Down Expand Up @@ -165,7 +223,7 @@ def selectSources(self, exposure, sourceCat, matches=None):
isRightType = (self.config.selectStar and isStar) or (self.config.selectGalaxy and not isStar)
isRightVar = (self.config.includeVariable) or (self.config.includeVariable is isVar)
if isRightType and isRightVar and isRightColor:
kernelCandidateSourceList.append(source)
starCat.append(source)
symb, ctype = "+", ds9.GREEN
else:
symb, ctype = "o", ds9.BLUE
Expand All @@ -179,7 +237,6 @@ def selectSources(self, exposure, sourceCat, matches=None):
if pauseAtEnd:
raw_input("Continue? y[es] p[db] ")

return kernelCandidateSourceList

measAlg.starSelectorRegistry.register("diacatalog", DiaCatalogSourceSelector)

return Struct(
starCat = starCat,
)
18 changes: 9 additions & 9 deletions tests/diaCatalogSourceSelector.py
Expand Up @@ -36,7 +36,7 @@
class DiaCatalogSourceSelectorTest(unittest.TestCase):

def setUp(self):
self.sourceSelector = ipDiffim.DiaCatalogSourceSelector()
self.sourceSelector = ipDiffim.DiaCatalogSourceSelectorTask()
self.exposure = afwImage.ExposureF()

def tearDown(self):
Expand All @@ -53,7 +53,7 @@ def makeSrcCatalog(self):
schema = afwTable.SourceTable.makeMinimalSchema()
schema.addField("test_flux", type=float)
schema.addField("test_fluxSigma", type=float)
for flag in self.sourceSelector.config.badPixelFlags:
for flag in self.sourceSelector.config.badFlags:
schema.addField(flag, type="Flag")
table = afwTable.SourceTable.make(schema)
table.definePsfFlux("test")
Expand All @@ -77,7 +77,7 @@ def makeMatches(self, refCat, srcCat, nSrc):
srcSrc.setCoord(coord)
srcSrc.set(srcSrc.getTable().getPsfFluxKey(), 10.)
srcSrc.set(srcSrc.getTable().getPsfFluxErrKey(), 1.)
for flag in self.sourceSelector.config.badPixelFlags:
for flag in self.sourceSelector.config.badFlags:
srcSrc.set(flag, False)

mat = afwTable.matchRaDec(refCat, srcCat, 1.0 * afwGeom.arcseconds, False)
Expand All @@ -91,17 +91,17 @@ def testCuts(self):
srcCat = self.makeSrcCatalog()

matches = self.makeMatches(refCat, srcCat, nSrc)
sources = self.sourceSelector.selectSources(self.exposure, srcCat, matches)
sources = self.sourceSelector.selectStars(self.exposure, srcCat, matches).starCat
self.assertEqual(len(sources), nSrc)

# Set one of the source flags to be bad
matches[0].second.set(self.sourceSelector.config.badPixelFlags[0], True)
sources = self.sourceSelector.selectSources(self.exposure, srcCat, matches)
matches[0].second.set(self.sourceSelector.config.badFlags[0], True)
sources = self.sourceSelector.selectStars(self.exposure, srcCat, matches).starCat
self.assertEqual(len(sources), nSrc-1)

# Set one of the ref flags to be bad
matches[1].first.set("photometric", False)
sources = self.sourceSelector.selectSources(self.exposure, srcCat, matches)
sources = self.sourceSelector.selectStars(self.exposure, srcCat, matches).starCat
self.assertEqual(len(sources), nSrc-2)

# Set one of the colors to be bad
Expand All @@ -110,13 +110,13 @@ def testCuts(self):
gFluxField = getRefFluxField(refCat.schema, "g")
gFlux = 10**(-0.4 * (grMin - 0.1)) * matches[2].first.get(rFluxField)
matches[2].first.set(gFluxField, gFlux)
sources = self.sourceSelector.selectSources(self.exposure, srcCat, matches)
sources = self.sourceSelector.selectStars(self.exposure, srcCat, matches).starCat
self.assertEqual(len(sources), nSrc-3)

# Set one of the types to be bad
if self.sourceSelector.config.selectStar and not self.sourceSelector.config.selectGalaxy:
matches[3].first.set("resolved", True)
sources = self.sourceSelector.selectSources(self.exposure, srcCat, matches)
sources = self.sourceSelector.selectStars(self.exposure, srcCat, matches).starCat
self.assertEqual(len(sources), nSrc-4)

def suite():
Expand Down