LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
catalogCalculation.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 from collections import namedtuple
23 
24 import lsst.pipe.base
25 import lsst.pex.config
26 import lsst.daf.base
27 from lsst.utils.timer import timeMethod
28 
29 from .pluginsBase import BasePlugin, BasePluginConfig
30 from .pluginRegistry import PluginRegistry, PluginMap
31 from . import FatalAlgorithmError, MeasurementError
32 
33 # Exceptions that the measurement tasks should always propagate up to their
34 # callers
35 FATAL_EXCEPTIONS = (MemoryError, FatalAlgorithmError)
36 
37 __all__ = ("CatalogCalculationPluginConfig", "CatalogCalculationPlugin", "CatalogCalculationConfig",
38  "CatalogCalculationTask")
39 
40 
42  """Default configuration class for catalog calcuation plugins.
43  """
44  pass
45 
46 
48  """Base class for catalog calculation plugins.
49 
50  Parameters
51  ----------
52  config : `CatalogCalculationPlugin.ConfigClass`
53  Plugin configuration.
54  name : `str`
55  The string the plugin was registered with.
56  schema : `lsst.afw.table.Schema`
57  The source schema, New fields should be added here to
58  hold output produced by this plugin.
59  metadata : `lsst.daf.base.PropertySet`
60  Plugin metadata that will be attached to the output catalog
61  """
62 
63  ConfigClass = CatalogCalculationPluginConfig # documentation inherited
64 
65  registry = PluginRegistry(CatalogCalculationPluginConfig)
66  """List of available plugins (`lsst.meas.base.PluginRegistry`).
67  """
68 
69  plugType = 'single'
70  """Does the plugin operate on a single source or the whole catalog (`str`)?
71 
72  If the plugin operates on a single source at a time, this should be set to
73  ``"single"``; if it expects the whoe catalog, to ``"multi"``. If the
74  plugin is of type ``"multi"``, the `fail` method must be implemented to
75  accept the whole catalog. If the plugin is of type ``"single"``, `fail`
76  should accept a single source record.
77  """
78 
79  def __init__(self, config, name, schema, metadata):
80  BasePlugin.__init__(self, config, name)
81 
82  @classmethod
84  r"""Used to set the relative order of plugin execution.
85 
86  The values returned by `getExecutionOrder` are compared across all
87  plugins, and smaller numbers run first.
88 
89  Notes
90  -----
91  `CatalogCalculationPlugin`\s must run with
92  `BasePlugin.DEFAULT_CATALOGCALCULATION` or higher.
93 
94  All plugins must implement this method with an appropriate run level
95  """
96  raise NotImplementedError()
97 
98  def calculate(self, cat, **kwargs):
99  """Perform the calculation specified by this plugin.
100 
101  This method can either be used to operate on a single catalog record
102  or a whole catalog, populating it with the output defined by this
103  plugin.
104 
105  Note that results may be added to catalog records as new columns, or
106  may result in changes to existing values.
107 
108  Parameters
109  ----------
110  cat : `lsst.afw.table.SourceCatalog` or `lsst.afw.table.SourceRecord`
111  May either be a `~lsst.afw.table.SourceCatalog` or a single
112  `~lsst.afw.table.SourceRecord`, depending on the plugin type. Will
113  be updated in place to contain the results of plugin execution.
114  **kwargs
115  Any additional keyword arguments that may be passed to the plugin.
116  """
117  raise NotImplementedError()
118 
119 
120 class CCContext:
121  """Handle errors that are thrown by catalog calculation plugins.
122 
123  This is a context manager.
124 
125  Parameters
126  ----------
127  plugin : `CatalogCalculationPlugin`
128  The plugin that is to be run.
129  cat : `lsst.afw.table.SourceCatalog` or `lsst.afw.table.SourceRecord`
130  May either be a `~lsst.afw.table.SourceCatalog` or a single
131  `~lsst.afw.table.SourceRecord`, depending on the plugin type.
132  log : `lsst.log.Log` or `logging.Logger`
133  A logger. Generally, this should be the logger of the object in which
134  the context manager is being used.
135  """
136  def __init__(self, plugin, cat, log):
137  self.pluginplugin = plugin
138  self.catcat = cat
139  self.loglog = log
140 
141  def __enter__(self):
142  return
143 
144  def __exit__(self, exc_type, exc_value, traceback):
145  if exc_type is None:
146  return True
147  if exc_type in FATAL_EXCEPTIONS:
148  raise exc_value
149  elif exc_type is MeasurementError:
150  self.plugin.fail(self.cat, exc_value)
151  else:
152  self.log.warning("Error in %s.calculate: %s", self.plugin.name, exc_value)
153  return True
154 
155 
157  """Config class for the catalog calculation driver task.
158 
159  Specifies which plugins will execute when the `CatalogCalculationTask`
160  associated with this configuration is run.
161  """
162 
163  plugins = CatalogCalculationPlugin.registry.makeField(
164  multi=True,
165  default=["base_ClassificationExtendedness",
166  "base_FootprintArea"],
167  doc="Plugins to be run and their configuration")
168 
169 
170 class CatalogCalculationTask(lsst.pipe.base.Task):
171  """Run plugins which operate on a catalog of sources.
172 
173  This task facilitates running plugins which will operate on a source
174  catalog. These plugins may do things such as classifying an object based
175  on source record entries inserted during a measurement task.
176 
177  Parameters
178  ----------
179  plugMetaData : `lsst.daf.base.PropertyList` or `None`
180  Will be modified in-place to contain metadata about the plugins being
181  run. If `None`, an empty `~lsst.daf.base.PropertyList` will be
182  created.
183  **kwargs
184  Additional arguments passed to the superclass constructor.
185 
186  Notes
187  -----
188  Plugins may either take an entire catalog to work on at a time, or work on
189  individual records.
190  """
191  ConfigClass = CatalogCalculationConfig
192  _DefaultName = "catalogCalculation"
193 
194  def __init__(self, schema, plugMetadata=None, **kwargs):
195  lsst.pipe.base.Task.__init__(self, **kwargs)
196  self.schemaschema = schema
197  if plugMetadata is None:
198  plugMetadata = lsst.daf.base.PropertyList()
199  self.plugMetadataplugMetadata = plugMetadata
200  self.pluginsplugins = PluginMap()
201 
202  self.initializePluginsinitializePlugins()
203 
204  def initializePlugins(self):
205  """Initialize the plugins according to the configuration.
206  """
207 
208  pluginType = namedtuple('pluginType', 'single multi')
209  self.executionDictexecutionDict = {}
210  # Read the properties for each plugin. Allocate a dictionary entry for each run level. Verify that
211  # the plugins are above the minimum run level for an catalogCalculation plugin. For each run level,
212  # the plugins are sorted into either single record, or multi record groups to later be run
213  # appropriately
214  for executionOrder, name, config, PluginClass in sorted(self.config.plugins.apply()):
215  if executionOrder not in self.executionDictexecutionDict:
216  self.executionDictexecutionDict[executionOrder] = pluginType(single=[], multi=[])
217  if PluginClass.getExecutionOrder() >= BasePlugin.DEFAULT_CATALOGCALCULATION:
218  plug = PluginClass(config, name, self.schemaschema, metadata=self.plugMetadataplugMetadata)
219  self.pluginsplugins[name] = plug
220  if plug.plugType == 'single':
221  self.executionDictexecutionDict[executionOrder].single.append(plug)
222  elif plug.plugType == 'multi':
223  self.executionDictexecutionDict[executionOrder].multi.append(plug)
224  else:
225  errorTuple = (PluginClass, PluginClass.getExecutionOrder(),
226  BasePlugin.DEFAULT_CATALOGCALCULATION)
227  raise ValueError("{} has an execution order less than the minimum for an catalogCalculation "
228  "plugin. Value {} : Minimum {}".format(*errorTuple))
229 
230  @timeMethod
231  def run(self, measCat):
232  """The entry point for the catalog calculation task.
233 
234  Parameters
235  ----------
236  meascat : `lsst.afw.table.SourceCatalog`
237  Catalog for measurement.
238  """
239  self.callComputecallCompute(measCat)
240 
241  def callCompute(self, catalog):
242  """Run each of the plugins on the catalog.
243 
244  Parameters
245  ----------
246  catalog : `lsst.afw.table.SourceCatalog`
247  The catalog on which the plugins will operate.
248  """
249  for runlevel in sorted(self.executionDictexecutionDict):
250  # Run all of the plugins which take a whole catalog first
251  for plug in self.executionDictexecutionDict[runlevel].multi:
252  with CCContext(plug, catalog, self.log):
253  plug.calculate(catalog)
254  # Run all the plugins which take single catalog entries
255  for measRecord in catalog:
256  for plug in self.executionDictexecutionDict[runlevel].single:
257  with CCContext(plug, measRecord, self.log):
258  plug.calculate(measRecord)
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
def __exit__(self, exc_type, exc_value, traceback)
def __init__(self, config, name, schema, metadata)
def __init__(self, schema, plugMetadata=None, **kwargs)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174