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