LSST Applications  22.0.1,22.0.1+01bcf6a671,22.0.1+046ee49490,22.0.1+05c7de27da,22.0.1+0c6914dbf6,22.0.1+1220d50b50,22.0.1+12fd109e95,22.0.1+1a1dd69893,22.0.1+1c910dc348,22.0.1+1ef34551f5,22.0.1+30170c3d08,22.0.1+39153823fd,22.0.1+611137eacc,22.0.1+771eb1e3e8,22.0.1+94e66cc9ed,22.0.1+9a075d06e2,22.0.1+a5ff6e246e,22.0.1+a7db719c1a,22.0.1+ba0d97e778,22.0.1+bfe1ee9056,22.0.1+c4e1e0358a,22.0.1+cc34b8281e,22.0.1+d640e2c0fa,22.0.1+d72a2e677a,22.0.1+d9a6b571bd,22.0.1+e485e9761b,22.0.1+ebe8d3385e
LSST Data Management Base Package
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 __all__ = ["ResourceConfig", "PipelineTaskConfig"]
25 
26 # -------------------------------
27 # Imports of standard modules --
28 # -------------------------------
29 from numbers import Number
30 
31 # -----------------------------
32 # Imports for other modules --
33 # -----------------------------
34 import lsst.pex.config as pexConfig
35 from .connections import PipelineTaskConnections
36 
37 # ----------------------------------
38 # Local non-exported definitions --
39 # ----------------------------------
40 
41 # ------------------------
42 # Exported definitions --
43 # ------------------------
44 
45 
46 class TemplateField(pexConfig.Field):
47  """This Field is specialized for use with connection templates.
48  Specifically it treats strings or numbers as valid input, as occasionally
49  numbers are used as a cycle counter in templates.
50 
51  The reason for the specialized field, is that when numbers are involved
52  with the config override system through pipelines or from the command line,
53  sometimes the quoting to get appropriate values as strings gets
54  complicated. This will simplify the process greatly.
55  """
56  def _validateValue(self, value):
57  if value is None:
58  return
59 
60  if not (isinstance(value, str) or isinstance(value, Number)):
61  raise TypeError(f"Value {value} is of incorrect type {pexConfig.config._typeStr(value)}."
62  f" Expected type str or a number")
63  if self.check is not None and not self.check(value):
64  ValueError("Value {value} is not a valid value")
65 
66  def __set__(self, instance, value, at=None, label='assignment'):
67  # validate first, even though validate will be called in super
68  self._validateValue_validateValue(value)
69  # now, explicitly make it into a string
70  value = str(value)
71  super().__set__(instance, value, at, label)
72 
73 
74 class PipelineTaskConfigMeta(pexConfig.ConfigMeta):
75  """Metaclass used in the creation of PipelineTaskConfig classes
76 
77  This metaclass ensures a `PipelineTaskConnections` class is specified in
78  the class construction parameters with a parameter name of
79  pipelineConnections. Using the supplied connection class, this metaclass
80  constructs a `lsst.pex.config.Config` instance which can be used to
81  configure the connections class. This config is added to the config class
82  under declaration with the name "connections" used as an identifier. The
83  connections config also has a reference to the connections class used in
84  its construction associated with an atttribute named `ConnectionsClass`.
85  Finally the newly constructed config class (not an instance of it) is
86  assigned to the Config class under construction with the attribute name
87  `ConnectionsConfigClass`.
88  """
89  def __new__(cls, name, bases, dct, **kwargs):
90  if name != "PipelineTaskConfig":
91  # Verify that a connection class was specified and the argument is
92  # an instance of PipelineTaskConfig
93  if 'pipelineConnections' not in kwargs:
94  for base in bases:
95  if hasattr(base, "connections"):
96  kwargs['pipelineConnections'] = base.connections.dtype.ConnectionsClass
97  break
98  if 'pipelineConnections' not in kwargs:
99  raise NameError("PipelineTaskConfig or a base class must be defined with connections class")
100  connectionsClass = kwargs['pipelineConnections']
101  if not issubclass(connectionsClass, PipelineTaskConnections):
102  raise ValueError("Can only assign a PipelineTaskConnections Class to pipelineConnections")
103 
104  # Create all the fields that will be used in the newly created sub
105  # config (under the attribute name "connections")
106  configConnectionsNamespace = {}
107  for fieldName, obj in connectionsClass.allConnections.items():
108  configConnectionsNamespace[fieldName] = pexConfig.Field(dtype=str,
109  doc=f"name for "
110  f"connection {fieldName}",
111  default=obj.name)
112  # If there are default templates also add them as fields to
113  # configure the template values
114  if hasattr(connectionsClass, 'defaultTemplates'):
115  docString = "Template parameter used to format corresponding field template parameter"
116  for templateName, default in connectionsClass.defaultTemplates.items():
117  configConnectionsNamespace[templateName] = TemplateField(dtype=str,
118  doc=docString,
119  default=default)
120  # add a reference to the connection class used to create this sub
121  # config
122  configConnectionsNamespace['ConnectionsClass'] = connectionsClass
123 
124  # Create a new config class with the fields defined above
125  Connections = type("Connections", (pexConfig.Config,), configConnectionsNamespace)
126  # add it to the Config class that is currently being declared
127  dct['connections'] = pexConfig.ConfigField(dtype=Connections,
128  doc='Configurations describing the'
129  ' connections of the PipelineTask to datatypes')
130  dct['ConnectionsConfigClass'] = Connections
131  dct['ConnectionsClass'] = connectionsClass
132  inst = super().__new__(cls, name, bases, dct)
133  return inst
134 
135  def __init__(self, name, bases, dct, **kwargs):
136  # This overrides the default init to drop the kwargs argument. Python
137  # metaclasses will have this argument set if any kwargs are passes at
138  # class construction time, but should be consumed before calling
139  # __init__ on the type metaclass. This is in accordance with python
140  # documentation on metaclasses
141  super().__init__(name, bases, dct)
142 
143 
144 class PipelineTaskConfig(pexConfig.Config, metaclass=PipelineTaskConfigMeta):
145  """Configuration class for `PipelineTask`
146 
147  This Configuration class functions in largely the same manner as any other
148  derived from `lsst.pex.config.Config`. The only difference is in how it is
149  declared. `PipelineTaskConfig` children need to be declared with a
150  pipelineConnections argument. This argument should specify a child class of
151  `PipelineTaskConnections`. During the declaration of a `PipelineTaskConfig`
152  a config class is created with information from the supplied connections
153  class to allow configuration of the connections class. This dynamically
154  created config class is then attached to the `PipelineTaskConfig` via a
155  `~lsst.pex.config.ConfigField` with the attribute name `connections`.
156  """
157  saveMetadata = pexConfig.Field(
158  dtype=bool, default=True, optional=False,
159  doc="Flag to enable/disable metadata saving for a task, enabled by default.")
160 
161 
162 class ResourceConfig(pexConfig.Config):
163  """Configuration for resource requirements.
164 
165  This configuration class will be used by some activators to estimate
166  resource use by pipeline. Additionally some tasks could use it to adjust
167  their resource use (e.g. reduce the number of threads).
168 
169  For some resources their limit can be estimated by corresponding task,
170  in that case task could set the field value. For many fields defined in
171  this class their associated resource used by a task will depend on the
172  size of the data and is not known in advance. For these resources their
173  value will be configured through overrides based on some external
174  estimates.
175  """
176  minMemoryMB = pexConfig.Field(dtype=int, default=None, optional=True,
177  doc="Minimal memory needed by task, can be None if estimate is unknown.")
178  minNumCores = pexConfig.Field(dtype=int, default=1,
179  doc="Minimal number of cores needed by task.")
table::Key< int > type
Definition: Detector.cc:163
def __init__(self, name, bases, dct, **kwargs)
Definition: config.py:135
def __new__(cls, name, bases, dct, **kwargs)
Definition: config.py:89
def __set__(self, instance, value, at=None, label='assignment')
Definition: config.py:66
def _validateValue(self, value)
Definition: config.py:56