Skip to content

Commit

Permalink
Only jsanitize before Store update and fix maggma 0.64.0 (#1835)
Browse files Browse the repository at this point in the history
## Summary of Changes

Only call `jsanitize()` once: before `Store.update`. Closes #1992.

Requires:
- pymatgen > 2024.3.1

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent da90eac commit 404fb0c
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ dependencies = [
"cclib>=1.8", # for I/O parsing of molecular DFT codes
"custodian>=2024.3.12", # for automated error corrections
"emmet-core>=0.80.0", # for pre-made schemas
"maggma<=0.63.4", # for database handling
"maggma>=0.64.0", # for database handling
"monty>=2024.2.26", # miscellaneous Python utilities
"numpy>=1.25.0", # for array handling
"psutil", # for getting compute architecture details
"pydantic>=2.0.1", # for settings management
"pydantic-settings>=2.2.0", # for settings management
"pymatgen>=2024.2.20", # for structure manipulation
"pymatgen>=2024.4.12", # for structure manipulation
"typer>=0.12.1", # for the CLI
]

Expand Down
6 changes: 2 additions & 4 deletions src/quacc/schemas/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from __future__ import annotations

from copy import deepcopy
from typing import TYPE_CHECKING

from emmet.core.structure import MoleculeMetadata, StructureMetadata
from monty.json import jsanitize
from pymatgen.io.ase import AseAtomsAdaptor

from quacc.atoms.core import (
Expand Down Expand Up @@ -83,9 +83,7 @@ def atoms_to_metadata(
metadata = {}

# Copy the info flags as a separate entry in the DB for easy querying
results["atoms_info"] = jsanitize(
atoms.info, enum_values=True, recursive_msonable=True
)
results["atoms_info"] = deepcopy(atoms.info)

# Store Atoms object
results["atoms"] = atoms
Expand Down
3 changes: 1 addition & 2 deletions src/quacc/schemas/cclib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import cclib
from ase.atoms import Atoms
from cclib.io import ccread
from monty.json import jsanitize

from quacc import SETTINGS
from quacc.atoms.core import get_final_atoms_from_dynamics
Expand Down Expand Up @@ -328,7 +327,7 @@ def _make_cclib_schema(
raise RuntimeError(msg)

# Fetch all the attributes (i.e. all input/outputs from cclib)
attributes = jsanitize(cclib_obj.getattributes())
attributes = cclib_obj.getattributes()

# monty datetime bug workaround:
# github.com/materialsvirtuallab/monty/issues/275
Expand Down
11 changes: 9 additions & 2 deletions src/quacc/wflow_tools/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import uuid
from typing import TYPE_CHECKING

from monty.json import jsanitize

if TYPE_CHECKING:
from typing import Any

Expand All @@ -31,8 +33,13 @@ def results_to_db(store: Store, results: dict[str, Any] | list[dict]) -> None:
if not isinstance(results, list):
results = [results]

for result in results:
sanitized_results = [
jsanitize(result, enum_values=True, recursive_msonable=True)
for result in results
]

for result in sanitized_results:
result["uuid"] = str(uuid.uuid4())

with store:
store.update(results, key="uuid")
store.update(sanitized_results, key="uuid")
19 changes: 7 additions & 12 deletions tests/core/schemas/test_ase.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ def test_summarize_run(tmpdir, monkeypatch):

with gzip.open(Path(results["dir_name"], "quacc_results.pkl.gz"), "rb") as f:
pickle_results = pickle.load(f)
output = results.copy()
output.pop("uuid")
assert pickle_results.keys() == output.keys()
assert pickle_results.keys() == results.keys()

assert pickle_results["nsites"] == output["nsites"]
assert pickle_results["results"]["energy"] == output["results"]["energy"]
assert pickle_results["atoms"].info == output["atoms"].info
assert pickle_results["nsites"] == results["nsites"]
assert pickle_results["results"]["energy"] == results["results"]["energy"]
assert pickle_results["atoms"].info == results["atoms"].info


def test_summarize_run2(tmp_path, monkeypatch):
Expand Down Expand Up @@ -143,16 +141,13 @@ def test_summarize_opt_run(tmp_path, monkeypatch):
with gzip.open(Path(results["dir_name"], "quacc_results.pkl.gz"), "rb") as f:
pickle_results = pickle.load(f)

output = results.copy()
output.pop("uuid")

assert pickle_results.keys() == output.keys()
assert pickle_results.keys() == results.keys()

# assert things on the trajectory are the same
assert pickle_results["trajectory"] == output["trajectory"]
assert pickle_results["trajectory"] == results["trajectory"]
assert (
pickle_results["trajectory_results"][-1]["energy"]
== output["trajectory_results"][-1]["energy"]
== results["trajectory_results"][-1]["energy"]
)

# Test DB
Expand Down
1 change: 1 addition & 0 deletions tests/core/wflow/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ def test_results_to_db():
results_to_db(store, {"atoms": atoms})
with store:
assert store.count() == 1
assert store.query_one().get("uuid")
4 changes: 2 additions & 2 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ ase @ https://gitlab.com/ase/ase/-/archive/master/ase-master.zip
cclib==1.8.1
custodian==2024.3.12
emmet-core==0.82.1
maggma==0.63.4
maggma==0.64.0
monty==2024.3.31
numpy==1.26.4
psutil==5.9.8
pydantic==2.6.4
pydantic-settings==2.2.1
pymatgen==2024.3.1
pymatgen==2024.4.12
typer==0.12.2

0 comments on commit 404fb0c

Please sign in to comment.