Skip to content

Commit

Permalink
DirectMatchTask: add selection of science,ref sources
Browse files Browse the repository at this point in the history
Selecting sources before matching makes the matching process easier.
  • Loading branch information
PaulPrice committed Oct 20, 2017
1 parent 30f6da5 commit 26ee0c4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
28 changes: 23 additions & 5 deletions python/lsst/meas/astrom/directMatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from lsst.pex.config import Config, Field, ConfigurableField
from lsst.pipe.base import Task, Struct
from lsst.meas.algorithms import LoadIndexedReferenceObjectsTask
from lsst.meas.algorithms import (LoadIndexedReferenceObjectsTask, ScienceSourceSelectorTask,
ReferenceSourceSelectorTask)
import lsst.afw.table as afwTable
import lsst.afw.coord as afwCoord
from lsst.afw.geom import arcseconds
Expand All @@ -14,6 +15,10 @@ class DirectMatchConfig(Config):
"""Configuration for DirectMatchTask"""
refObjLoader = ConfigurableField(target=LoadIndexedReferenceObjectsTask, doc="Load reference objects")
matchRadius = Field(dtype=float, default=0.25, doc="Matching radius, arcsec")
sourceSelection = ConfigurableField(target=ScienceSourceSelectorTask,
doc="Selection of science sources")
referenceSelection = ConfigurableField(target=ReferenceSourceSelectorTask,
doc="Selection of reference sources")


class DirectMatchTask(Task):
Expand Down Expand Up @@ -77,6 +82,8 @@ def __init__(self, butler=None, refObjLoader=None, **kwargs):
self.makeSubtask("refObjLoader", butler=butler)
else:
self.refObjLoader = refObjLoader
self.makeSubtask("sourceSelection")
self.makeSubtask("referenceSelection")

def run(self, catalog, filterName=None):
"""!Load reference objects and match to them
Expand All @@ -88,11 +95,22 @@ def run(self, catalog, filterName=None):
"""
circle = self.calculateCircle(catalog)
matchMeta = self.refObjLoader.getMetadataCircle(circle.center, circle.radius, filterName)
sourceSelection = self.sourceSelection.selectSources(catalog)
if len(sourceSelection.sourceCat) == 0:
self.log.warn("No objects selected from %d objects in source catalog", len(catalog))
return Struct(matches=[], matchMeta=matchMeta)
refData = self.refObjLoader.loadSkyCircle(circle.center, circle.radius, filterName)
matches = afwTable.matchRaDec(refData.refCat, catalog, self.config.matchRadius*arcseconds)
self.log.info("Matched %d from %d input and %d reference sources" %
(len(matches), len(catalog), len(refData.refCat)))
return Struct(matches=matches, matchMeta=matchMeta)
refCat = refData.refCat
refSelection = self.referenceSelection.selectSources(refCat)
if len(refSelection.sourceCat) == 0:
self.log.warn("No objects selected from %d objects in reference catalog", len(refCat))
matches = afwTable.matchRaDec(refSelection.sourceCat, sourceSelection.sourceCat,
self.config.matchRadius*arcseconds)
self.log.info("Matched %d from %d/%d input and %d/%d reference sources" %
(len(matches), len(sourceSelection.sourceCat), len(catalog),
len(refSelection.sourceCat), len(refCat)))
return Struct(matches=matches, matchMeta=matchMeta, refCat=refCat, sourceSelection=sourceSelection,
refSelection=refSelection)

def calculateCircle(self, catalog):
"""!Calculate a circle enclosing the catalog
Expand Down
22 changes: 18 additions & 4 deletions python/lsst/meas/astrom/ref_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import lsst.afw.math as afwMath
import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase
from lsst.meas.algorithms import ScienceSourceSelectorTask, ReferenceSourceSelectorTask
from .matchOptimisticB import MatchOptimisticBTask
from .display import displayAstrometry
from . import makeMatchStatistics
Expand All @@ -46,6 +47,10 @@ class RefMatchConfig(pexConfig.Config):
default=2,
min=0,
)
sourceSelection = pexConfig.ConfigurableField(target=ScienceSourceSelectorTask,
doc="Selection of science sources")
referenceSelection = pexConfig.ConfigurableField(target=ReferenceSourceSelectorTask,
doc="Selection of reference sources")

# The following block adds links to this task from the Task Documentation page.
## \addtogroup LSST_task_documentation
Expand Down Expand Up @@ -74,6 +79,8 @@ def __init__(self, refObjLoader, schema=None, **kwargs):
pipeBase.Task.__init__(self, **kwargs)
self.refObjLoader = refObjLoader
self.makeSubtask("matcher")
self.makeSubtask("sourceSelection")
self.makeSubtask("referenceSelection")

@pipeBase.timeMethod
def loadAndMatch(self, exposure, sourceCat):
Expand All @@ -95,12 +102,17 @@ def loadAndMatch(self, exposure, sourceCat):

expMd = self._getExposureMetadata(exposure)

sourceSelection = self.sourceSelection.selectSources(sourceCat)

loadRes = self.refObjLoader.loadPixelBox(
bbox=expMd.bbox,
wcs=expMd.wcs,
filterName=expMd.filterName,
calib=expMd.calib,
)

refSelection = self.referenceSelection.selectSources(loadRes.refCat)

matchMeta = self.refObjLoader.getMetadataBox(
bbox=expMd.bbox,
wcs=expMd.wcs,
Expand All @@ -109,8 +121,8 @@ def loadAndMatch(self, exposure, sourceCat):
)

matchRes = self.matcher.matchObjectsToSources(
refCat=loadRes.refCat,
sourceCat=sourceCat,
refCat=refSelection.sourceCat,
sourceCat=sourceSelection.sourceCat,
wcs=expMd.wcs,
refFluxField=loadRes.fluxField,
match_tolerance=None,
Expand All @@ -125,8 +137,8 @@ def loadAndMatch(self, exposure, sourceCat):
if debug.display:
frame = int(debug.frame)
displayAstrometry(
refCat=loadRes.refCat,
sourceCat=sourceCat,
refCat=refSelection.sourceCat,
sourceCat=sourceSelection.sourceCat,
matches=matchRes.matches,
exposure=exposure,
bbox=expMd.bbox,
Expand All @@ -136,6 +148,8 @@ def loadAndMatch(self, exposure, sourceCat):

return pipeBase.Struct(
refCat=loadRes.refCat,
refSelection=refSelection,
sourceSelection=sourceSelection,
matches=matchRes.matches,
matchMeta=matchMeta,
)
Expand Down
1 change: 1 addition & 0 deletions tests/test_astrometryTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def makeSourceCat(self, distortedWcs):
sourceFluxKey = sourceSchema["slot_ApFlux_flux"].asKey()
sourceFluxSigmaKey = sourceSchema["slot_ApFlux_fluxSigma"].asKey()

sourceCat.reserve(len(refCat))
for refObj in refCat:
src = sourceCat.addNew()
src.set(sourceCentroidKey, refObj.get(refCentroidKey))
Expand Down

0 comments on commit 26ee0c4

Please sign in to comment.