Skip to content

Commit 8ea2b0f

Browse files
committed
fixed bson serialization problem by moving the name from key to value
1 parent 391cfd1 commit 8ea2b0f

File tree

6 files changed

+80
-52
lines changed

6 files changed

+80
-52
lines changed

atomate/common/powerups.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -224,31 +224,27 @@ def set_queue_adapter(
224224

225225
def powerup_by_kwargs(
226226
original_wf: Workflow,
227-
add_powerup_module: List[str] = None,
228-
**powerup_kwargs,
227+
powerup_dicts: List[dict],
229228
):
230229
"""
231-
apply powerups in the form using a kwargs dictionary of the form:
232-
{
233-
powerup_function_name1 : {parameter1 : value1, parameter2: value2},
234-
powerup_function_name2 : {parameter1 : value1, parameter2: value2},
235-
}
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+
]
236235
237236
As an example:
238-
power_up_by_kwargs( "add_additional_fields_to_taskdocs" : {
239-
"update_dict" : {"foo" : "bar"}
240-
}
237+
power_up_by_kwargs([
238+
{"powerup_name" : "add_additional_fields_to_taskdocs",
239+
"kwargs: {"update_dict" : {"foo" : "bar"}}}
240+
]
241241
)
242242
243243
Args:
244244
original_wf: workflow that will be changed
245-
add_powerup_module: user_made modules that contain powerups
246-
powerup_kwargs: KWARGS used to apply any power, EXAMPLE:
247-
{"add_modify_incar": {
248-
"modify_incar_params": {
249-
"incar_update": {"KPAR": 8}}
250-
}
251-
}
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
252248
253249
"""
254250
# a list of possible powerups in atomate (most specific first)
@@ -257,17 +253,27 @@ def powerup_by_kwargs(
257253
"atomate.qchem.powerups",
258254
"atomate.common.powerups",
259255
]
260-
# user can add new modules containing custom powerups
261-
if add_powerup_module is not None:
262-
powerup_modules = add_powerup_module + powerup_modules
263-
264-
for k, v in powerup_kwargs.items():
265-
for module_name in powerup_modules:
266-
try:
267-
module = import_module(module_name)
268-
powerup = getattr(module, k)
269-
original_wf = powerup(original_wf, **v)
270-
break
271-
except Exception:
272-
pass
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))
273279
return original_wf

atomate/common/tests/test_powerups.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,16 @@ def test_add_tags(self):
8383

8484
def test_powerup_by_kwargs(self):
8585
my_wf = copy_wf(self.bs_wf)
86-
my_wf = powerup_by_kwargs(my_wf, add_tags={"tags_list": ["foo", "bar"]})
86+
my_wf = powerup_by_kwargs(
87+
my_wf,
88+
[
89+
{"powerup_name": "add_tags", "kwargs": {"tags_list": ["foo", "bar"]}},
90+
{
91+
"powerup_name": "atomate.common.powerups.add_priority",
92+
"kwargs": {"root_priority": 123},
93+
},
94+
],
95+
)
8796
self.assertEqual(my_wf.metadata["tags"], ["foo", "bar"])
8897

8998
def test_set_queue_adapter(self):

atomate/vasp/firetasks/electrode_tasks.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import math
2-
from collections import defaultdict
32

43
from fireworks import FiretaskBase, explicit_serialize, FWAction, Firework, Workflow
54
from pymatgen.core import Structure
@@ -316,10 +315,13 @@ def get_powerup_wf(wf, fw_spec, additional_fields=None):
316315
Returns:
317316
Updated workflow
318317
"""
319-
d_pu = defaultdict(dict)
320-
d_pu.update(fw_spec.get("vasp_powerups", {}))
318+
powerup_list = []
319+
powerup_list.extend(fw_spec.get("vasp_powerups", []))
321320
if additional_fields is not None:
322-
d_pu["add_additional_fields_to_taskdocs"].update(
323-
{"update_dict": additional_fields}
321+
powerup_list.append(
322+
{
323+
"powerup_name": "add_additional_fields_to_taskdocs",
324+
"kwargs": {"update_dict": additional_fields},
325+
}
324326
)
325-
return powerup_by_kwargs(wf, **d_pu)
327+
return powerup_by_kwargs(wf, powerup_list)

atomate/vasp/tests/test_vasp_powerups.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,13 @@ def test_use_potcar_spec(self):
247247

248248
def test_powerup_by_kwargs(self):
249249
my_wf = copy_wf(self.bs_wf)
250-
my_wf = powerup_by_kwargs(my_wf, add_trackers={})
251-
my_wf = powerup_by_kwargs(my_wf, add_tags={"tags_list": ["foo", "bar"]})
250+
my_wf = powerup_by_kwargs(
251+
my_wf, [{"powerup_name": "add_trackers", "kwargs": {}}]
252+
)
253+
my_wf = powerup_by_kwargs(
254+
my_wf,
255+
[{"powerup_name": "add_tags", "kwargs": {"tags_list": ["foo", "bar"]}}],
256+
)
252257
for fw in my_wf.fws:
253258
self.assertEqual(len(fw.spec["_trackers"]), 2)
254259
self.assertEqual(my_wf.metadata["tags"], ["foo", "bar"])

atomate/vasp/workflows/base/electrode.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import List
2+
13
from fireworks import Workflow
24
from pymatgen.core import Structure
35
from pymatgen.analysis.structure_matcher import StructureMatcher
@@ -23,7 +25,7 @@ def get_ion_insertion_wf(
2325
db_file: str = DB_FILE,
2426
vasptodb_kwargs: dict = None,
2527
volumetric_data_type: str = "CHGCAR",
26-
vasp_powerups: dict = None,
28+
vasp_powerups: List[dict] = None,
2729
max_insertions: int = 4,
2830
allow_fizzled_parents: bool = True,
2931
optimizefw_kwargs: dict = None,
@@ -106,7 +108,7 @@ def get_ion_insertion_wf(
106108

107109
# Apply the vasp powerup if present
108110
if vasp_powerups is not None:
109-
wf = powerup_by_kwargs(wf, **vasp_powerups)
111+
wf = powerup_by_kwargs(wf, vasp_powerups)
110112
for fw in wf.fws:
111113
fw.spec["vasp_powerups"] = vasp_powerups
112114

atomate/vasp/workflows/tests/test_insertion_workflow.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,23 @@ def setUp(self):
4141
working_ion="Mg",
4242
volumetric_data_type="AECCAR",
4343
db_file=db_dir / "db.json",
44-
vasp_powerups={
45-
"add_modify_incar": {
46-
"modify_incar_params": {"incar_update": {"KPAR": 8}}
44+
vasp_powerups=[
45+
{
46+
"powerup_name": "add_modify_incar",
47+
"kwargs": {"modify_incar_params": {"incar_update": {"KPAR": 8}}},
4748
},
48-
"use_fake_vasp": {
49-
"ref_dirs": calc_dirs,
50-
"check_incar": False,
51-
"check_kpoints": False,
52-
"check_poscar": False,
53-
"check_potcar": False,
49+
{
50+
"powerup_name": "use_fake_vasp",
51+
"kwargs": {
52+
"ref_dirs": calc_dirs,
53+
"check_incar": False,
54+
"check_kpoints": False,
55+
"check_poscar": False,
56+
"check_potcar": False,
57+
},
5458
},
55-
"use_potcar_spec": {},
56-
},
59+
{"powerup_name": "use_potcar_spec", "kwargs": {}},
60+
],
5761
optimizefw_kwargs={"ediffg": -0.05},
5862
)
5963
wf = use_fake_vasp(

0 commit comments

Comments
 (0)