LSSTApplications  16.0-10-g1758552+4,16.0-10-g4f78f78+4,16.0-10-gc1446dd+30,16.0-11-g39ac3c7+2,16.0-13-g066a532+3,16.0-14-g6c7ed55+4,16.0-14-gd373004+3,16.0-15-g072d20e+4,16.0-15-gb461e1a+2,16.0-16-g48c959a+3,16.0-16-g89065d4+2,16.0-16-gd8e3590+4,16.0-19-gb830ed4e+4,16.0-2-g0febb12+20,16.0-2-g9d5294e+53,16.0-2-ga8830df+3,16.0-20-g17d57d5+3,16.0-22-gf7a7fdf+3,16.0-28-g5e946dc7,16.0-3-g324faa9+3,16.0-3-gcfd6c53+51,16.0-3-ge00e371+9,16.0-4-g03cf288+42,16.0-4-g5f3a788+19,16.0-4-ga3eb747+9,16.0-4-gabf74b7+4,16.0-4-gb13d127+3,16.0-5-g6a53317+9,16.0-5-gb3f8a4b+62,16.0-5-gef99c9f+4,16.0-57-g90e7ba260+2,16.0-6-g0838257+3,16.0-6-g9321be7+3,16.0-6-gcbc7b31+4,16.0-6-gf49912c+4,16.0-7-gd2eeba5+13,16.0-8-g21fd5fe+4,16.0-8-g3a9f023+4,16.0-9-g85d1a16+4,master-g7b902255af+4,w.2018.43
LSSTDataManagementBasePackage
config.py
Go to the documentation of this file.
1 # This file is part of pipe_base.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (http://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 <http://www.gnu.org/licenses/>.
21 
22 """Module defining config classes for PipelineTask.
23 """
24 
25 __all__ = ["InputDatasetConfig", "InputDatasetField",
26  "OutputDatasetConfig", "OutputDatasetField",
27  "InitInputDatasetConfig", "InitInputDatasetField",
28  "InitOutputDatasetConfig", "InitOutputDatasetField",
29  "ResourceConfig", "QuantumConfig", "PipelineTaskConfig"]
30 
31 # -------------------------------
32 # Imports of standard modules --
33 # -------------------------------
34 from textwrap import dedent, indent
35 
36 # -----------------------------
37 # Imports for other modules --
38 # -----------------------------
39 import lsst.pex.config as pexConfig
40 
41 # ----------------------------------
42 # Local non-exported definitions --
43 # ----------------------------------
44 
45 # ------------------------
46 # Exported definitions --
47 # ------------------------
48 
49 
50 def _makeDatasetField(name, dtype):
51  """ Function to make callables which produce ConfigField objects
52 
53  This is factory function which produces factory functions. The factories
54  returned by this function are used to simplify the process of creating
55  pex config ConfigFields which have dtypes derived from either
56  _DatasetTypeConfig, or _GlobalDatasetTypeConfig. These functions can
57  then be used in a mannor similar to other ConfigField constructors.
58 
59  Below is a flow diagram to explain the use of this function visually,
60  where arrows indicate processing flow.
61 
62  Make a ConfigField factory:
63  _makeDatasetField() -> return wrappedFunc -> assign to variable corresponding
64  to name
65 
66  Use a ConfigField factory:
67  name() -> factory() -> return pexConfig instance
68 
69  Example
70  -------
71  FooField = _makeDatasetField("FooField", FooConfig)
72  fooFieldInstance = FooField("An example Foo ConfigField",
73  "fooConfigurable",
74  ("tract", "patch"),
75  "Exposure")
76 
77  Parameters
78  ----------
79  name : `str`
80  The name to use as the final output Field constructor
81  dtype : Configuration Object
82  This is the python type to set as the dtype in the ConfigField
83  construction
84 
85  Returns
86  -------
87  func : function
88  Python callable function which can be used to produce instances of
89  ConfigFields of type dtype.
90 
91  Raises
92  ------
93  TypeError
94  Possibly raises a TypeError if attempting to create a factory function
95  from an incompatible type
96  """
97 
98  def factory(**kwargs):
99  """ This is the innermost function in the closure, and does the work
100  of actually producing the ConfigField
101  """
102  # kwargs contain all the variables needed to construct the ConfigField
103  # The doc and check variables are used to construct the ConfigField,
104  # while the rest are used in the construction of the dtype object,
105  # which is why doc and check are filted out before unpacking the
106  # dictionary to the dtype constructor.
107  return pexConfig.ConfigField(doc=kwargs['doc'],
108  dtype=dtype,
109  default=dtype(
110  **{k: v for k, v in kwargs.items()
111  if k not in ('doc', 'check')}),
112  check=kwargs['check'])
113 
114  # here the dtype is checked against its baseclass type. This is due to the fact
115  # the code nesseary to make ConfigField factories is shared, but the arguments
116  # the factories take differ between base class types
117  if issubclass(dtype, _GlobalDatasetTypeConfig):
118  # Handle global dataset types like InitInputDatasetConfig, these types have
119  # a function signature with no units variable
120  def wrappedFunc(doc, name, storageClass, check=None):
121  return factory(**{k: v for k, v in locals().items() if k != 'factory'})
122  # This factory does not take a units argument, so set the variables for the
123  # units documentation to empty python strings
124  extraDoc = ""
125  extraFields = ""
126  elif issubclass(dtype, _DatasetTypeConfig):
127  # Handle dataset types like InputDatasetConfig, note these take a units argument
128  def wrappedFunc(doc, name, units, storageClass, scalar=False, check=None):
129  return factory(**{k: v for k, v in locals().items() if k != 'factory'})
130  # Set the string corresponding to the units parameter documentation
131  # formatting is to support final output of the docstring variable
132  extraDoc = """
133  units : iterable of `str`
134  Iterable of DataUnits for this `~lsst.daf.butler.DatasetType`
135  scalar : `bool`, optional
136  If set to True then only a single dataset is expected on input or
137  produced on output. In that case list of objects/DataIds will be
138  unpacked before calling task methods, returned data is expected
139  to contain single objects as well."""
140  # Set a string to add the units argument to the list of arguments in the
141  # docstring explanation section formatting is to support final output
142  # of the docstring variable
143  extraFields = ", units, scalar,"
144  else:
145  # if someone tries to create a config factory for a type that is not
146  # handled raise and exception
147  raise TypeError(f"Cannot create a factory for dtype {dtype}")
148 
149  # Programatically create a docstring to use in the factory function
150  docstring = f""" Factory function to create `~lsst.pex.config.Config` class instances
151  of `{dtype.__name__}`
152 
153  This function servers as syntactic sugar for creating Configurable fields
154  which are `{dtype.__name__}`. The naming of this function violates the
155  normal convention of a lowercase first letter in the function name, as
156  this function is intended to sit in the same place as
157  `~lsst.pex.config.ConfigField` classes, and consistency in declaration
158  syntax is important.
159 
160  The input arguments for this class are a combination of the arguments for
161  `~lsst.pex.config.ConfigField` and `{dtype.__name__}`. The arguments
162  doc and check come from `~lsst.pex.config.ConfigField`, while name{extraFields}
163  and storageClass come from `{dtype.__name__}`.
164 
165  Parameters
166  ----------
167  doc : `str`
168  Documentation string for the `{dtype.__name__}`
169  name : `str`
170  Name of the `~lsst.daf.butler.DatasetType` in the returned
171  `{dtype.__name__}`{indent(dedent(extraDoc), " " * 4)}
172  storageClass : `str`
173  Name of the `~lsst.daf.butler.StorageClass` in the `{dtype.__name__}`
174  check : callable
175  A callable to be called with the field value that returns
176  False if the value is invalid.
177 
178  Returns
179  -------
180  result : `~lsst.pex.config.ConfigField`
181  Instance of a `~lsst.pex.config.ConfigField` with `InputDatasetConfig` as a dtype
182  """
183  # Set the name to be used for the returned ConfigField factory function
184  wrappedFunc.__name__ = name
185  # Set the name to be used for the returned ConfigField factory function, and unindent
186  # the docstring as it was indednted to corrispond to this factory functions indention
187  wrappedFunc.__doc__ = dedent(docstring)
188  return wrappedFunc
189 
190 
191 class QuantumConfig(pexConfig.Config):
192  """Configuration class which defines PipelineTask quanta units.
193 
194  In addition to a list of dataUnit names this also includes optional list of
195  SQL statements to be executed against Registry database. Exact meaning and
196  format of SQL will be determined at later point.
197  """
198  units = pexConfig.ListField(dtype=str,
199  doc="list of DataUnits which define quantum")
200  sql = pexConfig.ListField(dtype=str,
201  doc="sequence of SQL statements",
202  optional=True)
203 
204 
205 class _BaseDatasetTypeConfig(pexConfig.Config):
206  """Intermediate base class for dataset type configuration in PipelineTask.
207  """
208  name = pexConfig.Field(dtype=str,
209  doc="name of the DatasetType")
210  storageClass = pexConfig.Field(dtype=str,
211  doc="name of the StorageClass")
212 
213 
215  """Configuration class which defines dataset type used by PipelineTask.
216 
217  Consists of DatasetType name, list of DataUnit names and StorageCass name.
218  PipelineTasks typically define one or more input and output datasets. This
219  class should not be used directly, instead one of `InputDatasetConfig` or
220  `OutputDatasetConfig` should be used in PipelineTask config.
221  """
222  units = pexConfig.ListField(dtype=str,
223  doc="list of DataUnits for this DatasetType")
224  scalar = pexConfig.Field(dtype=bool,
225  default=False,
226  optional=True,
227  doc=("If set to True then only a single dataset is expected "
228  "on input or produced on output. In that case list of "
229  "objects/DataIds will be unpacked before calling task "
230  "methods, returned data is expected to contain single "
231  "objects as well."))
232 
233 
235  pass
236 
237 
238 class OutputDatasetConfig(_DatasetTypeConfig):
239  pass
240 
241 
243  """Configuration class which defines dataset types used in PipelineTask
244  initialization.
245 
246  Consists of DatasetType name and StorageCass name, with a read-only
247  ``units`` property that returns an empty tuple, enforcing the constraint
248  that datasets used in initialization are not associated with any
249  DataUnits. This class should not be used directly, instead one of
250  `InitInputDatasetConfig` or `InitOutputDatasetConfig` should be used in
251  PipelineTask config.
252  """
253  @property
254  def units(self):
255  """DataUnits associated with this DatasetType (always empty)."""
256  return ()
257 
258 
260  pass
261 
262 
263 class InitOutputDatasetConfig(_GlobalDatasetTypeConfig):
264  pass
265 
266 
267 class ResourceConfig(pexConfig.Config):
268  """Configuration for resource requirements.
269 
270  This configuration class will be used by some activators to estimate
271  resource use by pipeline. Additionally some tasks could use it to adjust
272  their resource use (e.g. reduce the number of threads).
273 
274  For some resources their limit can be estimated by corresponding task,
275  in that case task could set the field value. For many fields defined in
276  this class their associated resource used by a task will depend on the
277  size of the data and is not known in advance. For these resources their
278  value will be configured through overrides based on some external
279  estimates.
280  """
281  minMemoryMB = pexConfig.Field(dtype=int, default=None, optional=True,
282  doc="Minimal memory needed by task, can be None if estimate is unknown.")
283  minNumCores = pexConfig.Field(dtype=int, default=1,
284  doc="Minimal number of cores needed by task.")
285 
286 
287 class PipelineTaskConfig(pexConfig.Config):
288  """Base class for all PipelineTask configurations.
289 
290  This class defines fields that must be defined for every PipelineTask.
291  It will be used as a base class for all PipelineTask configurations instead
292  of `pex.config.Config`.
293  """
294  quantum = pexConfig.ConfigField(dtype=QuantumConfig,
295  doc="configuration for PipelineTask quantum")
296 
297 
298 InputDatasetField = _makeDatasetField("InputDatasetField", InputDatasetConfig)
299 OutputDatasetField = _makeDatasetField("OutputDatasetField", OutputDatasetConfig)
300 InitInputDatasetField = _makeDatasetField("InitInputDatasetField", InitInputDatasetConfig)
301 InitOutputDatasetField = _makeDatasetField("InitOutputDatasetField", InitOutputDatasetConfig)
std::vector< SchemaItem< Flag > > * items