LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
pluginRegistry.py
Go to the documentation of this file.
1 # This file is part of meas_base.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (https://www.lsst.org).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
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 GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 
22 """Registry for measurement plugins and utilities for plugin management.
23 """
24 
25 import collections
26 
27 import lsst.pipe.base
28 import lsst.pex.config
29 from .apCorrRegistry import addApCorrName
30 
31 __all__ = ("generateAlgorithmName", "PluginRegistry", "register", "PluginMap")
32 
33 
34 def generateAlgorithmName(AlgClass):
35  """Generate a name for an algorithm.
36 
37  This generates a short name for an algorithmic class that strips away
38  terms that are generally redundant while remaining easy to trace to the
39  code.
40 
41  Parameters
42  ----------
43  AlgClass : subclass of `BaseAlgorithm`
44  The class to generate a name for.
45 
46  Returns
47  -------
48  name : `str`
49  A short name for the algorithm.
50 
51  Notes
52  -----
53  The returned name will cobmine the package name, with any ``lsst`` and/or
54  ``meas`` prefix removed, with the class name, with any ``Algorithm``
55  suffix removed. For instance, ``lsst.meas.base.SdssShapeAlgorithm``
56  becomes ``base_SdssShape``.
57  """
58  name = AlgClass.__name__
59  pkg = AlgClass.__module__
60  name = name.replace("Algorithm", "")
61  terms = pkg.split(".")
62  # Hide private module name only if it's part of a public package
63  if len(terms) > 1 and terms[-1].startswith("_"):
64  terms = terms[:-1]
65  if len(terms) > 1 and terms[-1].endswith("Lib"):
66  terms = terms[:-1]
67  if terms[0] == "lsst":
68  terms = terms[1:]
69  if terms[0] == "meas":
70  terms = terms[1:]
71  if name.lower().startswith(terms[-1].lower()):
72  terms = terms[:-1]
73  return "%s_%s" % ("_".join(terms), name)
74 
75 
77  """Base class for plugin registries.
78 
79  Notes
80  -----
81  The class of plugins allowed in the registry is defined in the constructor
82  of the registry.
83 
84  Single-frame and forced plugins have different registries.
85  """
86 
87  class Configurable:
88  """Class used as the element in the plugin registry.
89 
90  Parameters
91  ----------
92  name : `str`
93  Name under which the plugin is registerd.
94  PluginClass : subclass of `BasePlugin`
95  The class of plugin which can be stored in the registry.
96 
97  Notes
98  -----
99  Rather than constructing a Plugin instance, its __call__ method
100  (invoked by RegistryField.apply) returns a tuple
101  of ``(executionOrder, name, config, PluginClass)``, which can then
102  be sorted before the plugins are instantiated.
103  """
104 
105  __slots__ = "PluginClass", "name"
106 
107  def __init__(self, name, PluginClass):
108  self.name = name
109  self.PluginClass = PluginClass
110 
111  @property
112  def ConfigClass(self):
113  return self.PluginClass.ConfigClass
114 
115  def __call__(self, config):
116  return (self.PluginClass.getExecutionOrder(), self.name, config, self.PluginClass)
117 
118  def register(self, name, PluginClass, shouldApCorr=False, apCorrList=()):
119  """Register a plugin class with the given name.
120 
121  Parameters
122  ----------
123  name : `str`
124  The name of the plugin. This is used as a prefix for all fields
125  produced by the plugin, and it should generally contain the name
126  of the plugin or algorithm class itself as well as enough of the
127  namespace to make it clear where to find the code. For example
128  ``base_GaussianFlux`` indicates an algorithm in `lsst.meas.base`
129  that measures Gaussian Flux and produces fields such as
130  ``base_GaussianFlux_instFlux``, ``base_GaussianFlux_instFluxErr``
131  and ``base_GaussianFlux_flag``.
132  shouldApCorr : `bool`
133  If `True`, then this algorithm measures an instFlux that should
134  be aperture corrected. This is shorthand for ``apCorrList=[name]``
135  and is ignored if ``apCorrList`` is specified.
136  apCorrList : `list` of `str`
137  List of field name prefixes for instFlux fields to be aperture
138  corrected. If an algorithm produces a single instFlux that should
139  be aperture corrected then it is simpler to set
140  ``shouldApCorr=True``. But if an algorithm produces multiple such
141  fields then it must specify ``apCorrList`` instead. For example,
142  ``modelfit_CModel`` produces three such fields:
143  ``apCorrList=("modelfit_CModel_exp", "modelfit_CModel_exp",
144  "modelfit_CModel_def")``. If ``apCorrList`` is not empty then
145  shouldApCorr is ignored.
146 
147  Notes
148  -----
149  The same plugin may be registered multiple times with different names;
150  this can be useful if we often want to run it multiple times with
151  different configuration.
152  """
153  lsst.pex.config.Registry.register(self, name, self.Configurable(name, PluginClass))
154  if shouldApCorr and not apCorrList:
155  apCorrList = [name]
156  for prefix in apCorrList:
157  addApCorrName(prefix)
158 
159  def makeField(self, doc, default=None, optional=False, multi=False):
160  return lsst.pex.config.RegistryField(doc, self, default, optional, multi)
161 
162 
163 def register(name, shouldApCorr=False, apCorrList=()):
164  """A decorator to register a plugin class in its base class's registry.
165 
166  Parameters
167  ----------
168  shouldApCorr : `bool`
169  If `True`, then this algorithm measures an instFlux that should be
170  aperture corrected. This is shorthand for ``apCorrList=[name]`` and is
171  ignored if ``apCorrList`` is specified.
172  apCorrList : `list` of `str`
173  List of field name prefixes for instFlux fields to be aperture
174  corrected. If an algorithm produces a single instFlux that should be
175  aperture corrected then it is simpler to set ``shouldApCorr=True``.
176  But if an algorithm produces multiple such fields then it must specify
177  ``apCorrList`` instead. For example, ``modelfit_CModel`` produces
178  three such fields: ``apCorrList=("modelfit_CModel_exp",
179  "modelfit_CModel_exp", "modelfit_CModel_def")``. If ``apCorrList`` is
180  not empty then shouldApCorr is ignored.
181 
182  """
183 
184  def decorate(PluginClass):
185  PluginClass.registry.register(name, PluginClass, shouldApCorr=shouldApCorr, apCorrList=apCorrList)
186  return PluginClass
187  return decorate
188 
189 
190 class PluginMap(collections.OrderedDict):
191  """Map of plugins to be run for a given task.
192 
193  Notes
194  -----
195  Plugins are classes derived from `BasePlugin`.
196 
197  We assume plugins are added to the plugin map according to their
198  "Execution Order", so this class doesn't actually do any of the sorting
199  (though it does have to maintain that order, which it does by inheriting
200  from `collections.OrderedDict`).
201  """
202 
203  def iter(self):
204  """Return an iterator over plugins for use in single-object mode.
205 
206  Notes
207  -----
208  Plugins which should be used in single-object mode are identified by
209  having the `doMeasure` config attribute evaluate to `True`. This is
210  usually a simple boolean class attribute.
211  """
212  for plugin in self.values():
213  if plugin.config.doMeasure:
214  yield plugin
215 
216  def iterN(self):
217  """Return an iterator over plugins for use in multi-object mode.
218 
219  Notes
220  -----
221  Plugins which should be used in multi-object mode are identified by
222  having the `doMeasureN` config attribute evaluate to `True`.
223  This is usually a simple boolean class attribute.
224  """
225  for plugin in self.values():
226  if plugin.config.doMeasureN:
227  yield plugin
lsst::meas::base.pluginRegistry.generateAlgorithmName
def generateAlgorithmName(AlgClass)
Definition: pluginRegistry.py:34
lsst::meas::base.pluginRegistry.PluginMap.iterN
def iterN(self)
Definition: pluginRegistry.py:216
lsst::meas::base.pluginRegistry.register
def register(name, shouldApCorr=False, apCorrList=())
Definition: pluginRegistry.py:163
lsst::meas::base.pluginRegistry.PluginRegistry.Configurable.__call__
def __call__(self, config)
Definition: pluginRegistry.py:115
lsst::meas::base.pluginRegistry.PluginRegistry.makeField
def makeField(self, doc, default=None, optional=False, multi=False)
Definition: pluginRegistry.py:159
lsst.pex.config.registry.RegistryField
Definition: registry.py:283
lsst::meas::base.apCorrRegistry.addApCorrName
def addApCorrName(name)
Definition: apCorrRegistry.py:31
lsst::meas::base.pluginRegistry.PluginMap.iter
def iter(self)
Definition: pluginRegistry.py:203
lsst::meas::base.pluginRegistry.PluginRegistry.Configurable.PluginClass
PluginClass
Definition: pluginRegistry.py:109
lsst::meas::base.pluginRegistry.PluginMap
Definition: pluginRegistry.py:190
lsst::meas::base.pluginRegistry.PluginRegistry
Definition: pluginRegistry.py:76
lsst.pex.config.registry.Registry
Definition: registry.py:52
lsst.pex.config
Definition: __init__.py:1
lsst::meas::base.pluginRegistry.PluginRegistry.Configurable
Definition: pluginRegistry.py:87
lsst::meas::base.pluginRegistry.PluginRegistry.Configurable.name
name
Definition: pluginRegistry.py:108
lsst::meas::base.pluginRegistry.PluginRegistry.Configurable.ConfigClass
def ConfigClass(self)
Definition: pluginRegistry.py:112
lsst::meas::base.pluginRegistry.PluginRegistry.register
def register(self, name, PluginClass, shouldApCorr=False, apCorrList=())
Definition: pluginRegistry.py:118
lsst.pipe.base
Definition: __init__.py:1
lsst::meas::base.pluginRegistry.PluginRegistry.Configurable.__init__
def __init__(self, name, PluginClass)
Definition: pluginRegistry.py:107