13 from AnalysisAlgorithmsConfig.ConfigSequence
import ConfigSequence
14 from AnalysisAlgorithmsConfig.ConfigFactory
import ConfigFactory
15 from AnalysisAlgorithmsConfig.ConfigAccumulator
import deprecationWarningCategory
17 from AnaAlgorithm.Logging
import logging
18 logCPAlgTextCfg = logging.getLogger(
'CPAlgTextCfg')
22 """Loads YAML file into a dictionary"""
23 if not os.path.isfile(yamlPath):
24 raise ValueError(f
"{yamlPath} is not a file.")
25 with open(yamlPath,
'r')
as f:
26 textConfig = yaml.safe_load(f)
31 """Prints a dictionary as YAML"""
32 print(yaml.dump(d, default_flow_style=jsonFormat, sort_keys=sort))
38 class TextConfig(ConfigFactory):
39 def __init__(self, yamlPath=None, *, config=None, addDefaultBlocks=True):
40 super().
__init__(addDefaultBlocks=
False)
42 if yamlPath
and config:
43 raise ValueError(
"Cannot specify both yamlPath and config. Use one or the other.")
47 defaults={
'self': self})
55 if yamlPath
is not None or config
is not None:
62 """Print YAML configuration file."""
64 raise ValueError(
"Configuration has already been loaded.")
71 Preprocess the configuration dictionary.
72 Ensure blocks with only sub-blocks are initialized with an empty dictionary.
74 def processNode(node, algs):
75 if not isinstance(node, dict):
77 for blockName, blockContent
in list(node.items()):
81 if isinstance(blockContent, dict)
and not any(
82 key
in algs[blockName].options
for key
in blockContent
85 node[blockName] = {
'__placeholder__':
True, **blockContent}
87 processNode(node[blockName], algs[blockName].subAlgs)
90 processNode(config, algs)
95 Remove placeholder markers after initialization.
97 if not isinstance(config, dict):
99 if "__placeholder__" in config:
100 del config[
"__placeholder__"]
101 for key, value
in config.items():
106 read a YAML file. Will combine with any config blocks added using python
109 raise NotImplementedError(
"Mering multiple yaml files is not implemented.")
112 def merge(config, algs, path=''):
113 """Add to config block-by-block"""
114 if not isinstance(config, list):
117 for blocks
in config:
119 if blocks == {}
and path:
124 for blockName
in algs:
125 if blockName
in blocks:
126 subBlocks[blockName] = blocks.pop(blockName)
131 for subName, subBlock
in subBlocks.items():
132 newPath = f
'{path}.{subName}' if path
else subName
133 merge(subBlock, algs[subName].subAlgs, newPath)
136 logCPAlgTextCfg.info(f
'loading {yamlPath}')
137 if configDict
is not None:
143 if "AddConfigBlocks" in config:
144 self.
_configureAlg(self._algs[
"AddConfigBlocks"], config[
"AddConfigBlocks"])
149 merge(config, self._algs)
158 """Print YAML configuration file."""
160 raise ValueError(
"No configuration has been loaded.")
165 def saveYaml(self, filePath='config.yaml', default_flow_style=False,
168 Convert dictionary representation to yaml and save
170 logCPAlgTextCfg.info(f
"Saving configuration to {filePath}")
172 with open(filePath,
'w')
as outfile:
173 yaml.dump(config, outfile, default_flow_style=
False, **kwargs)
179 Create entry into dictionary representing the text configuration
181 def setEntry(name, config, opts):
183 if name
not in config:
185 elif isinstance(config[name], list):
188 config[name] = [config[name], opts]
192 name, rest = name[:name.index(
'.')], name[name.index(
'.') + 1:]
193 config = config[name]
194 if isinstance(config, list):
196 setEntry(rest, config, opts)
198 setEntry(name, self.
_config, dict(kwargs))
204 Set option(s) for the lsat block that was added. If an option
205 was added previously, will update value
207 if self.
_last is None:
208 raise TypeError(
"Cannot set options before adding a block")
210 self.
_last.update(**kwargs)
214 """Process YAML configuration file and confgure added algorithms."""
217 if blockName
not in self._order[self.ROOTNAME]:
220 raise ValueError(f
"Unkown block {blockName} in yaml file")
223 configSeq = ConfigSequence()
224 for blockName
in self._order[self.ROOTNAME]:
225 if blockName ==
"AddConfigBlocks":
228 assert blockName
in self._algs
232 blockConfig = self.
_config[blockName]
233 alg = self._algs[blockName]
241 algName, defaults=None, pos=None, superBlocks=None):
243 Load <functionName> from <modulePath>
246 module = importlib.import_module(modulePath)
247 fxn = getattr(module, functionName)
248 except ModuleNotFoundError
as e:
249 raise ModuleNotFoundError(f
"{e}\nFailed to load {functionName} from {modulePath}")
251 sys.modules[functionName] = fxn
253 self.addAlgConfigBlock(algName=algName, alg=fxn,
255 superBlocks=superBlocks,
260 def _configureAlg(self, block, blockConfig, configSeq=None, containerName=None,
262 if not isinstance(blockConfig, list):
263 blockConfig = [blockConfig]
265 for options
in blockConfig:
267 if 'containerName' in options:
268 containerName = options[
'containerName']
269 elif containerName
is not None and 'containerName' not in options:
270 options[
'containerName'] = containerName
272 logCPAlgTextCfg.info(f
"Configuring {block.algName}")
273 seq, funcOpts = block.makeConfig(options)
276 algOpts = seq.setOptions(options)
279 if containerName
is None:
281 if 'name' in opt
and opt[
'name'] ==
'containerName':
282 containerName = opt.get(
'value',
None)
285 if configSeq
is not None:
289 if extraOptions
is None:
290 extraOptionsList = [
"skipOnData",
"skipOnMC",
"onlyForDSIDs"]
292 if i[
'name']
in extraOptionsList
and i[
'defaultValue'] != i[
'value']:
293 if extraOptions
is None:
295 extraOptions[i[
'name']] = i[
'value']
297 algOpts = seq.setOptions(extraOptions.copy())
300 algOpts = [i[
'name']
for i
in algOpts]
301 expectedOptions =
set(funcOpts)
302 expectedOptions |=
set(algOpts)
303 expectedOptions |=
set(block.subAlgs)
305 difference =
set(options.keys()) - expectedOptions
306 difference.discard(
'__placeholder__')
308 difference =
"\n".
join(difference)
309 raise ValueError(f
"There are options set that are not used for "
310 f
"{block.algName}:\n{difference}\n"
311 "Please check your configuration.")
314 for alg
in self._order.
get(block.algName, []):
316 subAlg = block.subAlgs[alg]
317 self.
_configureAlg(subAlg, options[alg], configSeq, containerName, extraOptions)
321 def makeSequence(configPath, *, flags=None, algSeq=None, noSystematics=None, dataType=None, geometry=None, autoconfigFromFlags=None, isPhyslite=None, noPhysliteBroken=False):
329 if autoconfigFromFlags
is not None:
330 if flags
is not None:
331 raise ValueError(
"Cannot pass both flags and autoconfigFromFlags arguments")
332 flags = autoconfigFromFlags
333 warnings.warn (
'Using autoconfigFromFlags parameter is deprecated, use flags instead', category=deprecationWarningCategory, stacklevel=2)
335 warnings.warn (
'it is deprecated to configure meta-data for analysis configuration manually, please read the configuration flags via the meta-data reader', category=deprecationWarningCategory, stacklevel=2)
337 from AnalysisAlgorithmsConfig.ConfigAccumulator
import ConfigAccumulator
341 logCPAlgTextCfg.info(
"Configuration file read in:")
344 logCPAlgTextCfg.info(
"Default algorithms:")
345 config.printAlgs(printOpts=
True)
347 logCPAlgTextCfg.info(
"Configuring algorithms based on YAML file:")
348 configSeq = config.configure()
351 logCPAlgTextCfg.info(
"Configuration used:")
355 configAccumulator = ConfigAccumulator(algSeq=algSeq, dataType=dataType, isPhyslite=isPhyslite, geometry=geometry, autoconfigFromFlags=autoconfigFromFlags, flags=flags, noSystematics=noSystematics)
356 configSeq.fullConfigure(configAccumulator)
359 logCPAlgTextCfg.info(
"ConfigBlocks and their configuration:")
360 configSeq.printOptions()
362 from AnaAlgorithm.DualUseConfig
import isAthena, useComponentAccumulator
363 if isAthena
and useComponentAccumulator:
364 return configAccumulator.CA
375 Recursively combine configuration fragments into `local`.
377 - Looks for `fragment_key` at any dict node.
378 - If value is a string/path: merge that fragment.
379 - If value is a list: merge all fragments in order.
380 For conflicts between fragments, the **earlier** file in the list wins.
381 Local keys still override the merged fragments.
383 Returns True if any merging happened below this node.
388 if isinstance(local, dict):
389 to_combine = local.values()
390 elif isinstance(local, list):
396 for sub
in to_combine:
397 combined =
combineConfigFiles(sub, config_path, fragment_key=fragment_key)
or combined
400 if fragment_key
not in local:
404 if not isinstance(local, dict):
408 value = local[fragment_key]
409 if isinstance(value, (str, pathlib.Path)):
411 f
"{fragment_key} should be followed with a list of files",
416 elif isinstance(value, list):
419 raise TypeError(f
"'{fragment_key}' must be a string path or a list of paths, got {type(value).__name__}")
434 del local[fragment_key]
443 """Load a YAML or JSON fragment
445 This function is superfluous as of the yaml 1.2 spec (which
446 has not been implemented in ATLAS Yaml dependencies).
447 Once https://github.com/yaml/pyyaml/issues/173 is resolved
448 pyyaml will support yaml 1.2, which is compatable with json.
449 Until then yaml and json behave differently in some scientific
453 with open(fragment_path,
'r')
as fragment_file:
454 if fragment_path.suffix.lower() ==
'.json':
455 return json.load(fragment_file)
457 return yaml.safe_load(fragment_file)
462 config_path / fragment_path,
463 *[x / fragment_path
for x
in os.environ[
"DATAPATH"].
split(
":")]
465 for path
in paths_to_check:
469 raise FileNotFoundError(fragment_path)
474 if isinstance(local, list):
479 if isinstance(local, dict):
480 for key, value
in fragment.items():