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
pluginRegistry.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # LSST Data Management System
4 # Copyright 2008-2015 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 <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 """Registry for measurement plugins and associated utilities generateAlgorithmName and PluginMap
24 """
25 import collections
26 
27 from builtins import object
28 
29 import lsst.pipe.base
30 import lsst.pex.config
31 from .apCorrRegistry import addApCorrName
32 
33 __all__ = ("generateAlgorithmName", "PluginRegistry", "register", "PluginMap")
34 
35 
36 def generateAlgorithmName(AlgClass):
37  """Generate a string name for an algorithm class that strips away terms that are generally redundant
38  while (hopefully) remaining easy to trace to the code.
39 
40  The returned name will cobmine the package name, with any "lsst" and/or "meas" prefix removed,
41  with the class name, with any "Algorithm" suffix removed. For instance,
42  lsst.meas.base.SdssShapeAlgorithm becomes "base_SdssShape".
43  """
44  name = AlgClass.__name__
45  pkg = AlgClass.__module__
46  name = name.replace("Algorithm", "")
47  terms = pkg.split(".")
48  if terms[-1].endswith("Lib"):
49  terms = terms[:-1]
50  if terms[0] == "lsst":
51  terms = terms[1:]
52  if terms[0] == "meas":
53  terms = terms[1:]
54  if name.lower().startswith(terms[-1].lower()):
55  terms = terms[:-1]
56  return "%s_%s" % ("_".join(terms), name)
57 
58 
59 class PluginRegistry(lsst.pex.config.Registry):
60  """!
61  Base class for plugin registries
62 
63  The Plugin class allowed in the registry is defined in the ctor of the registry.
64 
65  Single-frame and forced plugins have different registries.
66  """
67 
68  class Configurable(object):
69  """!
70  Class used as the actual element in the registry
71 
72  Rather than constructing a Plugin instance, its __call__ method
73  (invoked by RegistryField.apply) returns a tuple
74  of (executionOrder, name, config, PluginClass), which can then
75  be sorted before the plugins are instantiated.
76  """
77 
78  __slots__ = "PluginClass", "name"
79 
80  def __init__(self, name, PluginClass):
81  """!
82  Create a Configurable object for the given PluginClass and name
83  """
84  self.name = name
85  self.PluginClass = PluginClass
86 
87  @property
88  def ConfigClass(self):
89  return self.PluginClass.ConfigClass
90 
91  def __call__(self, config):
92  return (self.PluginClass.getExecutionOrder(), self.name, config, self.PluginClass)
93 
94  def register(self, name, PluginClass, shouldApCorr=False, apCorrList=()):
95  """!
96  Register a Plugin class with the given name.
97 
98  The same Plugin may be registered multiple times with different names; this can
99  be useful if we often want to run it multiple times with different configuration.
100 
101  @param[in] name name of plugin class. This is used as a prefix for all fields produced by the Plugin,
102  and it should generally contain the name of the Plugin or Algorithm class itself
103  as well as enough of the namespace to make it clear where to find the code.
104  For example "base_GaussianFlux" indicates an algorithm in meas_base
105  that measures Gaussian Flux and produces fields such as "base_GaussianFlux_flux",
106  "base_GaussianFlux_fluxSigma" and "base_GaussianFlux_flag".
107  @param[in] shouldApCorr if True then this algorithm measures a flux that should be aperture
108  corrected. This is shorthand for apCorrList=[name] and is ignored if apCorrList is specified.
109  @param[in] apCorrList list of field name prefixes for flux fields that should be aperture corrected.
110  If an algorithm produces a single flux that should be aperture corrected then it is simpler
111  to set shouldApCorr=True. But if an algorithm produces multiple such fields then it must
112  specify apCorrList, instead. For example modelfit_CModel produces 3 such fields:
113  apCorrList=("modelfit_CModel_exp", "modelfit_CModel_exp", "modelfit_CModel_def")
114  If apCorrList is non-empty then shouldApCorr is ignored.
115  """
116  lsst.pex.config.Registry.register(self, name, self.Configurable(name, PluginClass))
117  if shouldApCorr and not apCorrList:
118  apCorrList = [name]
119  for prefix in apCorrList:
120  addApCorrName(prefix)
121 
122  def makeField(self, doc, default=None, optional=False, multi=False):
123  return lsst.pex.config.RegistryField(doc, self, default, optional, multi)
124 
125 
126 def register(name, shouldApCorr=False, apCorrList=()):
127  """!
128  A Python decorator that registers a class, using the given name, in its base class's PluginRegistry.
129  For example,
130  @code
131  @register("base_TransformedCentroid")
132  class ForcedTransformedCentroidPlugin(ForcedPlugin):
133  ...
134  @endcode
135  is equivalent to:
136  @code
137  class ForcedTransformedCentroidPlugin(ForcedPlugin):
138  ...
139  @ForcedPlugin.registry.register("base_TransformedCentroid", ForcedTransformedCentroidPlugin)
140  @endcode
141  """
142  def decorate(PluginClass):
143  PluginClass.registry.register(name, PluginClass, shouldApCorr=shouldApCorr, apCorrList=apCorrList)
144  return PluginClass
145  return decorate
146 
147 
148 class PluginMap(collections.OrderedDict):
149  """!
150  Map of plugins (instances of subclasses of BasePlugin) to be run for a task
151 
152  We assume plugins are added to the PluginMap according to their "Execution Order", so this
153  class doesn't actually do any of the sorting (though it does have to maintain that order,
154  which it does by inheriting from OrderedDict).
155  """
156 
157  def iter(self):
158  """!Return an iterator over plugins for which plugin.config.doMeasure is true
159 
160  @note plugin.config.doMeasure is usually a simple boolean class attribute, not a normal Config field.
161  """
162  for plugin in self.values():
163  if plugin.config.doMeasure:
164  yield plugin
165 
166  def iterN(self):
167  """!Return an iterator over plugins for which plugin.config.doMeasureN is true
168 
169  @note plugin.config.doMeasureN is usually a simple boolean class attribute, not a normal Config field.
170  """
171  for plugin in self.values():
172  if plugin.config.doMeasureN:
173  yield plugin
def __init__
Create a Configurable object for the given PluginClass and name.
Class used as the actual element in the registry.
Base class for plugin registries.
def iter
Return an iterator over plugins for which plugin.config.doMeasure is true.
def register
Register a Plugin class with the given name.
def register
A Python decorator that registers a class, using the given name, in its base class&#39;s PluginRegistry...
def addApCorrName
Add to the set of field name prefixes for fluxes that should be aperture corrected.
Map of plugins (instances of subclasses of BasePlugin) to be run for a task.
def iterN
Return an iterator over plugins for which plugin.config.doMeasureN is true.