|
3 | 3 | """
|
4 | 4 | This module defines general powerups that can be used for all workflows
|
5 | 5 | """
|
| 6 | +from importlib import import_module |
| 7 | +from typing import List |
6 | 8 |
|
7 | 9 | from atomate.utils.utils import get_fws_and_tasks
|
8 | 10 | from fireworks import Workflow, FileWriteTask
|
9 | 11 | from fireworks.utilities.fw_utilities import get_slug
|
10 | 12 |
|
11 |
| - |
12 | 13 | __author__ = "Janine George, Guido Petretto, Ryan Kingsbury"
|
13 | 14 | __email__ = (
|
14 | 15 |
|
@@ -219,3 +220,60 @@ def set_queue_adapter(
|
219 | 220 | original_wf.fws[idx_fw].spec["_queueadapter"] = q
|
220 | 221 |
|
221 | 222 | return original_wf
|
| 223 | + |
| 224 | + |
| 225 | +def powerup_by_kwargs( |
| 226 | + original_wf: Workflow, |
| 227 | + powerup_dicts: List[dict], |
| 228 | +): |
| 229 | + """ |
| 230 | + apply powerups in the form using a list of dictionaries |
| 231 | + [ |
| 232 | + {"powerup_name" : powerup_function1, "kwargs": {parameter1 : value1, parameter2: value2}}, |
| 233 | + {"powerup_name" : powerup_function2, "kwargs": {parameter1 : value1, parameter2: value2}}, |
| 234 | + ] |
| 235 | +
|
| 236 | + As an example: |
| 237 | + power_up_by_kwargs([ |
| 238 | + {"powerup_name" : "add_additional_fields_to_taskdocs", |
| 239 | + "kwargs: {"update_dict" : {"foo" : "bar"}}} |
| 240 | + ] |
| 241 | + ) |
| 242 | +
|
| 243 | + Args: |
| 244 | + original_wf: workflow that will be changed |
| 245 | + powerup_dicts: dictionary containing the powerup_name and kwarg. |
| 246 | + if "." is present in the name it will be imported as a full path |
| 247 | + if not we will use standard atomate modules where the powerups are kept |
| 248 | +
|
| 249 | + """ |
| 250 | + # a list of possible powerups in atomate (most specific first) |
| 251 | + powerup_modules = [ |
| 252 | + "atomate.vasp.powerups", |
| 253 | + "atomate.qchem.powerups", |
| 254 | + "atomate.common.powerups", |
| 255 | + ] |
| 256 | + |
| 257 | + for pd in powerup_dicts: |
| 258 | + name = pd["powerup_name"] |
| 259 | + kwargs = pd["kwargs"] |
| 260 | + found = False |
| 261 | + if "." in name: |
| 262 | + module_name, method_name = name.rsplit(".", 1) |
| 263 | + module = import_module(module_name) |
| 264 | + powerup = getattr(module, method_name) |
| 265 | + original_wf = powerup(original_wf, **kwargs) |
| 266 | + found = True |
| 267 | + else: |
| 268 | + for module_name in powerup_modules: |
| 269 | + try: |
| 270 | + module = import_module(module_name) |
| 271 | + powerup = getattr(module, name) |
| 272 | + original_wf = powerup(original_wf, **kwargs) |
| 273 | + found = True |
| 274 | + break |
| 275 | + except Exception: |
| 276 | + pass |
| 277 | + if not found: |
| 278 | + raise RuntimeError("Could not find powerup {}.".format(name)) |
| 279 | + return original_wf |
0 commit comments