lsst.meas.algorithms  13.0-24-g22030a45+4
sourceSelector.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 #
4 # Copyright 2008-2017 AURA/LSST.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <https://www.lsstcorp.org/LegalNotices/>.
22 #
23 from __future__ import absolute_import, division, print_function
24 
25 __all__ = ["BaseSourceSelectorConfig", "BaseSourceSelectorTask", "sourceSelectorRegistry"]
26 
27 import abc
28 
29 import lsst.afw.table as afwTable
30 import lsst.pex.config as pexConfig
31 import lsst.pipe.base as pipeBase
32 from future.utils import with_metaclass
33 
34 
35 class BaseSourceSelectorConfig(pexConfig.Config):
36  badFlags = pexConfig.ListField(
37  doc="List of flags which cause a source to be rejected as bad",
38  dtype=str,
39  default=[
40  "base_PixelFlags_flag_edge",
41  "base_PixelFlags_flag_interpolatedCenter",
42  "base_PixelFlags_flag_saturatedCenter",
43  "base_PixelFlags_flag_crCenter",
44  "base_PixelFlags_flag_bad",
45  "base_PixelFlags_flag_interpolated",
46  ],
47  )
48 
49 
50 class BaseSourceSelectorTask(with_metaclass(abc.ABCMeta, pipeBase.Task)):
51  """!Base class for source selectors
52 
53  Register all source selectors with the sourceSelectorRegistry using:
54  sourceSelectorRegistry.register(name, class)
55  """
56 
57  ConfigClass = BaseSourceSelectorConfig
58  _DefaultName = "sourceSelector"
59 
60  def __init__(self, **kwargs):
61  """!Initialize a source selector."""
62  pipeBase.Task.__init__(self, **kwargs)
63 
64  def run(self, sourceCat, maskedImage=None, **kwargs):
65  """!Select sources and return them.
66 
67  @param[in] sourceCat catalog of sources that may be sources (an lsst.afw.table.SourceCatalog)
68  @param[in] maskedImage the maskedImage containing the sources, for plotting.
69 
70  @return an lsst.pipe.base.Struct containing:
71  - sourceCat catalog of sources that were selected
72  """
73  return self.selectSources(maskedImage=maskedImage, sourceCat=sourceCat, **kwargs)
74 
75  @abc.abstractmethod
76  def selectSources(self, sourceCat, matches=None):
77  """!Return a catalog of sources: a subset of sourceCat.
78 
79  @param[in] sourceCat catalog of sources that may be sources (an lsst.afw.table.SourceCatalog)
80 
81  @return a pipeBase.Struct containing:
82  - sourceCat a catalog of sources
83  """
84 
85  # NOTE: example implementation, returning all sources that have no bad flags set.
86  result = afwTable.SourceCatalog(sourceCat.table)
87  for source in sourceCat:
88  if not self._isBad(source):
89  result.append(source)
90  return pipeBase.Struct(sourceCat=result)
91 
92  def _isBad(self, source):
93  """Return True if any of config.badFlags are set for this source."""
94  return any(source.get(flag) for flag in self.config.badFlags)
95 
96 
97 sourceSelectorRegistry = pexConfig.makeRegistry(
98  doc="A registry of source selectors (subclasses of BaseSourceSelectorTask)",
99 )
def selectSources(self, sourceCat, matches=None)
Return a catalog of sources: a subset of sourceCat.
def __init__(self, kwargs)
Initialize a source selector.
def run(self, sourceCat, maskedImage=None, kwargs)
Select sources and return them.