ATLAS Offline Software
ConfigText.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 #
3 # @author Joseph Lambert
4 
5 import yaml
6 import json
7 import os
8 import sys
9 import importlib
10 import pathlib
11 
12 from AnalysisAlgorithmsConfig.ConfigSequence import ConfigSequence
13 from AnalysisAlgorithmsConfig.ConfigFactory import ConfigFactory
14 
15 from AnaAlgorithm.Logging import logging
16 logCPAlgTextCfg = logging.getLogger('CPAlgTextCfg')
17 
18 
19 def readYaml(yamlPath):
20  """Loads YAML file into a dictionary"""
21  if not os.path.isfile(yamlPath):
22  raise ValueError(f"{yamlPath} is not a file.")
23  with open(yamlPath, 'r') as f:
24  textConfig = yaml.safe_load(f)
25  return textConfig
26 
27 
28 def printYaml(d, sort=False, jsonFormat=False):
29  """Prints a dictionary as YAML"""
30  print(yaml.dump(d, default_flow_style=jsonFormat, sort_keys=sort))
31 
32 
33 class TextConfig(ConfigFactory):
34  def __init__(self, yamlPath=None, *, addDefaultBlocks=True):
35  super().__init__(addDefaultBlocks=False)
36 
37  # Block to add new blocks to this object
38  self.addAlgConfigBlock(algName="AddConfigBlocks", alg=self._addNewConfigBlocks,
39  defaults={'self': self})
40  # add default blocks
41  if addDefaultBlocks:
42  self.addDefaultAlgs()
43  # load yaml
44  self._config = {}
45  # do not allow for loading multiple yaml files
46  self.__loadedYaml = False
47  if yamlPath is not None:
48  self.loadConfig(yamlPath)
49  # last is used for setOptionValue when using addBlock
50  self._last = None
51 
52 
53  def setConfig(self, config):
54  """Print YAML configuration file."""
55  if self._config:
56  raise ValueError("Configuration has already been loaded.")
57  self._config = config
58  return
59 
60 
61  def loadConfig(self, yamlPath):
62  """
63  read a YAML file. Will combine with any config blocks added using python
64  """
65  if self.__loadedYaml or isinstance(yamlPath, list):
66  raise NotImplementedError("Mering multiple yaml files is not implemented.")
67  self.__loadedYaml = True
68 
69  def merge(config, algs, path=''):
70  """Add to config block-by-block"""
71  if not isinstance(config, list):
72  config = [config]
73  # loop over list of blocks with same block name
74  for blocks in config:
75  # deal with case where empty dict is config
76  if blocks == {} and path:
77  self.addBlock(path)
78  return
79  # remove any subBlocks from block config
80  subBlocks = {}
81  for blockName in algs:
82  if blockName in blocks:
83  subBlocks[blockName] = blocks.pop(blockName)
84  # anything left should be a block and it's configuration
85  if blocks:
86  self.addBlock(path, **blocks)
87  # add in any subBlocks
88  for subName, subBlock in subBlocks.items():
89  newPath = f'{path}.{subName}' if path else subName
90  merge(subBlock, algs[subName].subAlgs, newPath)
91  return
92 
93  logCPAlgTextCfg.info(f'loading {yamlPath}')
94  config = readYaml(yamlPath)
95  # check if blocks are defined in yaml file
96  if "AddConfigBlocks" in config:
97  self._configureAlg(self._algs["AddConfigBlocks"], config["AddConfigBlocks"])
98  merge(config, self._algs)
99  return
100 
101 
102  def printConfig(self, sort=False, jsonFormat=False):
103  """Print YAML configuration file."""
104  if self._config is None:
105  raise ValueError("No configuration has been loaded.")
106  printYaml(self._config, sort, jsonFormat)
107  return
108 
109 
110  def saveYaml(self, filePath='config.yaml', default_flow_style=False,
111  **kwargs):
112  """
113  Convert dictionary representation to yaml and save
114  """
115  logCPAlgTextCfg.info(f"Saving configuration to {filePath}")
116  config = self._config
117  with open(filePath, 'w') as outfile:
118  yaml.dump(config, outfile, default_flow_style=False, **kwargs)
119  return
120 
121 
122  def addBlock(self, name, **kwargs):
123  """
124  Create entry into dictionary representing the text configuration
125  """
126  def setEntry(name, config, opts):
127  if '.' not in name:
128  if name not in config:
129  config[name] = opts
130  elif isinstance(config[name], list):
131  config[name].append(opts)
132  else:
133  config[name] = [config[name], opts]
134  # set last added block for setOptionValue
135  self._last = opts
136  else:
137  name, rest = name[:name.index('.')], name[name.index('.') + 1:]
138  config = config[name]
139  if isinstance(config, list):
140  config = config[-1]
141  setEntry(rest, config, opts)
142  return
143  setEntry(name, self._config, dict(kwargs))
144  return
145 
146 
147  def setOptions(self, **kwargs):
148  """
149  Set option(s) for the lsat block that was added. If an option
150  was added previously, will update value
151  """
152  if self._last is None:
153  raise TypeError("Cannot set options before adding a block")
154  # points to dict with opts for last added block
155  self._last.update(**kwargs)
156 
157 
158  def configure(self):
159  """Process YAML configuration file and confgure added algorithms."""
160  # make sure all blocks in yaml file are added (otherwise they would be ignored)
161  for blockName in self._config:
162  if blockName not in self._order[self.ROOTNAME]:
163  if not blockName:
164  blockName = list(self._config[blockName].keys())[0]
165  raise ValueError(f"Unkown block {blockName} in yaml file")
166 
167  # configure blocks
168  configSeq = ConfigSequence()
169  for blockName in self._order[self.ROOTNAME]:
170  if blockName == "AddConfigBlocks":
171  continue
172 
173  assert blockName in self._algs
174 
175  # order only applies to root blocks
176  if blockName in self._config:
177  blockConfig = self._config[blockName]
178  alg = self._algs[blockName]
179  self._configureAlg(alg, blockConfig, configSeq)
180  else:
181  continue
182  return configSeq
183 
184 
185  def _addNewConfigBlocks(self, modulePath, functionName,
186  algName, defaults=None, pos=None, superBlocks=None):
187  """
188  Load <functionName> from <modulePath>
189  """
190  try:
191  module = importlib.import_module(modulePath)
192  fxn = getattr(module, functionName)
193  except ModuleNotFoundError as e:
194  raise ModuleNotFoundError(f"{e}\nFailed to load {functionName} from {modulePath}")
195  else:
196  sys.modules[functionName] = fxn
197  # add new algorithm to available algorithms
198  self.addAlgConfigBlock(algName=algName, alg=fxn,
199  defaults=defaults,
200  superBlocks=superBlocks,
201  pos=pos)
202  return
203 
204 
205  def _configureAlg(self, block, blockConfig, configSeq=None, containerName=None):
206  if not isinstance(blockConfig, list):
207  blockConfig = [blockConfig]
208 
209  for options in blockConfig:
210  # Special case: propogate containerName down to subAlgs
211  if 'containerName' in options:
212  containerName = options['containerName']
213  elif containerName is not None and 'containerName' not in options:
214  options['containerName'] = containerName
215  # will check which options are associated alg and not options
216  logCPAlgTextCfg.info(f"Configuring {block.algName}")
217  seq, funcOpts = block.makeConfig(options)
218  if not seq._blocks:
219  continue
220  algOpts = seq.setOptions(options)
221  if configSeq is not None:
222  configSeq += seq
223 
224  # check to see if there are unused parameters
225  algOpts = [i['name'] for i in algOpts]
226  expectedOptions = set(funcOpts)
227  expectedOptions |= set(algOpts)
228  expectedOptions |= set(block.subAlgs)
229 
230  difference = set(options.keys()) - expectedOptions
231  if difference:
232  difference = "\n".join(difference)
233  raise ValueError(f"There are options set that are not used for "
234  f"{block.algName}:\n{difference}\n"
235  "Please check your configuration.")
236 
237  # check for sub-blocks and call this function recursively
238  for alg in self._order.get(block.algName, []):
239  if alg in options:
240  subAlg = block.subAlgs[alg]
241  self._configureAlg(subAlg, options[alg], configSeq, containerName)
242  return configSeq
243 
244 
245 def makeSequence(configPath, dataType, algSeq, geometry=None, autoconfigFromFlags=None,
246  isPhyslite=False, noPhysliteBroken=False, noSystematics=None):
247  """
248  """
249 
250  from AnalysisAlgorithmsConfig.ConfigAccumulator import ConfigAccumulator
251 
252  config = TextConfig(configPath)
253 
254  logCPAlgTextCfg.info("Configuration file read in:")
255  config.printConfig()
256 
257  logCPAlgTextCfg.info("Default algorithms:")
258  config.printAlgs(printOpts=True)
259 
260  logCPAlgTextCfg.info("Configuring algorithms based on YAML file:")
261  configSeq = config.configure()
262 
263  # defaults are added to config as algs are configured
264  logCPAlgTextCfg.info("Configuration used:")
265  config.printConfig()
266 
267  # compile
268  configAccumulator = ConfigAccumulator(algSeq, dataType, isPhyslite=isPhyslite, geometry=geometry, autoconfigFromFlags=autoconfigFromFlags, noSystematics=noSystematics)
269  configSeq.fullConfigure(configAccumulator)
270 
271  # blocks can be reordered during configSeq.fullConfigure
272  logCPAlgTextCfg.info("ConfigBlocks and their configuration:")
273  configSeq.printOptions()
274 
275  from AnaAlgorithm.DualUseConfig import isAthena, useComponentAccumulator
276  if isAthena and useComponentAccumulator:
277  return configAccumulator.CA
278  else:
279  return None
280 
281 
282 # Combine configuration files
283 #
284 # See the README for more info on how this works
285 #
286 def combineConfigFiles(local, config_path, fragment_key="include"):
287 
288  # if this isn't an iterable there's nothing to combine
289  if isinstance(local, dict):
290  to_combine = local.values()
291  elif isinstance(local, list):
292  to_combine = local
293  else:
294  return
295 
296  # otherwise descend into all the entries here
297  for sub in to_combine:
298  combineConfigFiles(sub, config_path, fragment_key=fragment_key)
299 
300  # if there are no fragments to include we're done
301  if fragment_key not in local:
302  return
303 
304  fragment_path = _find_fragment(
305  pathlib.Path(local[fragment_key]),
306  config_path)
307 
308  with open(fragment_path) as fragment_file:
309  # once https://github.com/yaml/pyyaml/issues/173 is resolved
310  # pyyaml will support the yaml 1.2 spec, which is compatable
311  # with json. Until then yaml and json behave differently, so
312  # we have this override.
313  if fragment_path.suffix == '.json':
314  fragment = json.load(fragment_file)
315  else:
316  fragment = yaml.safe_load(fragment_file)
317 
318  # fill out any sub-fragments, looking in the parent path of the
319  # fragment for local sub-fragments.
321  fragment,
322  fragment_path.parent,
323  fragment_key=fragment_key
324  )
325 
326  # merge the fragment with this one
327  _merge_dicts(local, fragment)
328 
329  # delete the fragment so we don't stumble over it again
330  del local[fragment_key]
331 
332 
333 def _find_fragment(fragment_path, config_path):
334  paths_to_check = [
335  fragment_path,
336  config_path / fragment_path,
337  *[x / fragment_path for x in os.environ["DATAPATH"].split(":")]
338  ]
339  for path in paths_to_check:
340  if path.exists():
341  return path
342 
343  raise FileNotFoundError(fragment_path)
344 
345 
346 def _merge_dicts(local, fragment):
347  # in the list case append the fragment to the local list
348  if isinstance(local, list):
349  local += fragment
350  return
351  # In the dict case, append only missing values to local: the local
352  # values take precidence over the fragment ones.
353  if isinstance(local, dict):
354  for key, value in fragment.items():
355  if key in local:
356  _merge_dicts(local[key], value)
357  else:
358  local[key] = value
359  return
python.ConfigText.TextConfig
Definition: ConfigText.py:33
python.ConfigText.combineConfigFiles
def combineConfigFiles(local, config_path, fragment_key="include")
Definition: ConfigText.py:286
python.ConfigText.TextConfig.saveYaml
def saveYaml(self, filePath='config.yaml', default_flow_style=False, **kwargs)
Definition: ConfigText.py:110
python.ConfigText.TextConfig.__init__
def __init__(self, yamlPath=None, *addDefaultBlocks=True)
Definition: ConfigText.py:34
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.ConfigText.makeSequence
def makeSequence(configPath, dataType, algSeq, geometry=None, autoconfigFromFlags=None, isPhyslite=False, noPhysliteBroken=False, noSystematics=None)
Definition: ConfigText.py:245
python.ConfigText.readYaml
def readYaml(yamlPath)
Definition: ConfigText.py:19
python.ConfigText.TextConfig.configure
def configure(self)
Definition: ConfigText.py:158
python.ConfigText.TextConfig._config
_config
Definition: ConfigText.py:44
python.ConfigText.TextConfig.setOptions
def setOptions(self, **kwargs)
Definition: ConfigText.py:147
python.ConfigText._find_fragment
def _find_fragment(fragment_path, config_path)
Definition: ConfigText.py:333
python.ConfigText.printYaml
def printYaml(d, sort=False, jsonFormat=False)
Definition: ConfigText.py:28
python.ConfigText.TextConfig.__loadedYaml
__loadedYaml
Definition: ConfigText.py:46
python.ConfigText.TextConfig._configureAlg
def _configureAlg(self, block, blockConfig, configSeq=None, containerName=None)
Definition: ConfigText.py:205
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.ConfigText.TextConfig.loadConfig
def loadConfig(self, yamlPath)
Definition: ConfigText.py:61
Trk::open
@ open
Definition: BinningType.h:40
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
python.utility.LHE.merge
def merge(input_file_pattern, output_file)
Merge many input LHE files into a single output file.
Definition: LHE.py:29
python.ConfigText._merge_dicts
def _merge_dicts(local, fragment)
Definition: ConfigText.py:346
python.ConfigText.TextConfig.setConfig
def setConfig(self, config)
Definition: ConfigText.py:53
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
python.ConfigText.TextConfig.printConfig
def printConfig(self, sort=False, jsonFormat=False)
Definition: ConfigText.py:102
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
python.ConfigText.TextConfig.addBlock
def addBlock(self, name, **kwargs)
Definition: ConfigText.py:122
python.ConfigText.TextConfig._addNewConfigBlocks
def _addNewConfigBlocks(self, modulePath, functionName, algName, defaults=None, pos=None, superBlocks=None)
Definition: ConfigText.py:185
WriteBchToCool.update
update
Definition: WriteBchToCool.py:67
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
merge
Definition: merge.py:1
python.ConfigText.TextConfig._last
_last
Definition: ConfigText.py:50