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
Public Member Functions | List of all members
lsst.pipe.base.configOverrides.ConfigOverrides Class Reference

Public Member Functions

def __init__ (self)
 
def addFileOverride (self, filename)
 
def addValueOverride (self, field, value)
 
def addPythonOverride (self, str python_snippet)
 
def addInstrumentOverride (self, str instrument, str task_name)
 
def applyTo (self, config)
 

Detailed Description

Defines a set of overrides to be applied to a task config.

Overrides for task configuration need to be applied by activator when
creating task instances. This class represents an ordered set of such
overrides which activator receives from some source (e.g. command line
or some other configuration).

Methods
----------
addFileOverride(filename)
    Add overrides from a specified file.
addValueOverride(field, value)
    Add override for a specific field.
applyTo(config)
    Apply all overrides to a `config` instance.

Notes
-----
Serialization support for this class may be needed, will add later if
necessary.

Definition at line 121 of file configOverrides.py.

Constructor & Destructor Documentation

◆ __init__()

def lsst.pipe.base.configOverrides.ConfigOverrides.__init__ (   self)

Definition at line 144 of file configOverrides.py.

144  def __init__(self):
145  self._overrides = []
146 

Member Function Documentation

◆ addFileOverride()

def lsst.pipe.base.configOverrides.ConfigOverrides.addFileOverride (   self,
  filename 
)
Add overrides from a specified file.

Parameters
----------
filename : str
    Path to the override file.

Definition at line 147 of file configOverrides.py.

147  def addFileOverride(self, filename):
148  """Add overrides from a specified file.
149 
150  Parameters
151  ----------
152  filename : str
153  Path to the override file.
154  """
155  self._overrides.append((OverrideTypes.File, filename))
156 
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
Definition: functional.cc:33

◆ addInstrumentOverride()

def lsst.pipe.base.configOverrides.ConfigOverrides.addInstrumentOverride (   self,
str  instrument,
str  task_name 
)
Apply any overrides that an instrument has for a task

Parameters
----------
instrument: str
    A string containing the fully qualified name of an instrument from
    which configs should be loaded and applied
task_name: str
    The _DefaultName of a task associated with a config, used to look
    up overrides from the instrument.

Definition at line 186 of file configOverrides.py.

186  def addInstrumentOverride(self, instrument: str, task_name: str):
187  """Apply any overrides that an instrument has for a task
188 
189  Parameters
190  ----------
191  instrument: str
192  A string containing the fully qualified name of an instrument from
193  which configs should be loaded and applied
194  task_name: str
195  The _DefaultName of a task associated with a config, used to look
196  up overrides from the instrument.
197  """
198  instrument_lib = doImport(instrument)()
199  self._overrides.append((OverrideTypes.Instrument, (instrument_lib, task_name)))
200 

◆ addPythonOverride()

def lsst.pipe.base.configOverrides.ConfigOverrides.addPythonOverride (   self,
str  python_snippet 
)
Add Overrides by running a snippit of python code against a config.

Parameters
----------
python_snippet: str
    A string which is valid python code to be executed. This is done
    with config as the only local accessible value.

Definition at line 175 of file configOverrides.py.

175  def addPythonOverride(self, python_snippet: str):
176  """Add Overrides by running a snippit of python code against a config.
177 
178  Parameters
179  ----------
180  python_snippet: str
181  A string which is valid python code to be executed. This is done
182  with config as the only local accessible value.
183  """
184  self._overrides.append((OverrideTypes.Python, python_snippet))
185 

◆ addValueOverride()

def lsst.pipe.base.configOverrides.ConfigOverrides.addValueOverride (   self,
  field,
  value 
)
Add override for a specific field.

This method is not very type-safe as it is designed to support
use cases where input is given as string, e.g. command line
activators. If `value` has a string type and setting of the field
fails with `TypeError` the we'll attempt `eval()` the value and
set the field with that value instead.

Parameters
----------
field : str
    Fully-qualified field name.
value :
    Value to be given to a filed.

Definition at line 157 of file configOverrides.py.

157  def addValueOverride(self, field, value):
158  """Add override for a specific field.
159 
160  This method is not very type-safe as it is designed to support
161  use cases where input is given as string, e.g. command line
162  activators. If `value` has a string type and setting of the field
163  fails with `TypeError` the we'll attempt `eval()` the value and
164  set the field with that value instead.
165 
166  Parameters
167  ----------
168  field : str
169  Fully-qualified field name.
170  value :
171  Value to be given to a filed.
172  """
173  self._overrides.append((OverrideTypes.Value, (field, value)))
174 

◆ applyTo()

def lsst.pipe.base.configOverrides.ConfigOverrides.applyTo (   self,
  config 
)
Apply all overrides to a task configuration object.

Parameters
----------
config : `pex.Config`

Raises
------
`Exception` is raised if operations on configuration object fail.

Definition at line 213 of file configOverrides.py.

213  def applyTo(self, config):
214  """Apply all overrides to a task configuration object.
215 
216  Parameters
217  ----------
218  config : `pex.Config`
219 
220  Raises
221  ------
222  `Exception` is raised if operations on configuration object fail.
223  """
224  # Look up a stack of variables people may be using when setting
225  # configs. Create a dictionary that will be used akin to a namespace
226  # for the duration of this function.
227  vars = {}
228  # pull in the variables that are declared in module scope of the config
229  mod = inspect.getmodule(config)
230  vars.update({k: v for k, v in mod.__dict__.items() if not k.startswith("__")})
231  # put the supplied config in the variables dictionary
232  vars['config'] = config
233 
234  # Create a parser for config expressions that may be strings
235  configParser = ConfigExpressionParser(namespace=vars)
236 
237  for otype, override in self._overrides:
238  if otype is OverrideTypes.File:
239  config.load(override)
240  elif otype is OverrideTypes.Value:
241  field, value = override
242  if isinstance(value, str):
243  value = self._parser(value, configParser)
244  # checking for dicts and lists here is needed because {} and []
245  # are valid yaml syntax so they get converted before hitting
246  # this method, so we must parse the elements.
247  #
248  # The same override would remain a string if specified on the
249  # command line, and is handled above.
250  if isinstance(value, dict):
251  new = {}
252  for k, v in value.items():
253  if isinstance(v, str):
254  new[k] = self._parser(v, configParser)
255  else:
256  new[k] = v
257  value = new
258  elif isinstance(value, list):
259  new = []
260  for v in value:
261  if isinstance(v, str):
262  new.append(self._parser(v, configParser))
263  else:
264  new.append(v)
265  value = new
266  # The field might be a string corresponding to a attribute
267  # hierarchy, attempt to split off the last field which
268  # will then be set.
269  parent, *child = field.rsplit(".", maxsplit=1)
270  if child:
271  # This branch means there was a hierarchy, get the
272  # field to set, and look up the sub config for which
273  # it is to be set
274  finalField = child[0]
275  tmpConfig = attrgetter(parent)(config)
276  else:
277  # There is no hierarchy, the config is the base config
278  # and the field is exactly what was passed in
279  finalField = parent
280  tmpConfig = config
281  # set the specified config
282  setattr(tmpConfig, finalField, value)
283 
284  elif otype is OverrideTypes.Python:
285  # exec python string with the context of all vars known. This
286  # both lets people use a var they know about (maybe a bit
287  # dangerous, but not really more so than arbitrary python exec,
288  # and there are so many other places to worry about security
289  # before we start changing this) and any imports that are done
290  # in a python block will be put into this scope. This means
291  # other config setting branches can make use of these
292  # variables.
293  exec(override, None, vars)
294  elif otype is OverrideTypes.Instrument:
295  instrument, name = override
296  instrument.applyConfigOverrides(name, config)

The documentation for this class was generated from the following file: