Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: Update AseAtomsAdaptor to handle Structure.properties/Molecule.properties #3270

Merged
merged 15 commits into from
Aug 31, 2023
Merged
Prev Previous commit
Next Next commit
jsanitize the atoms info
Andrew-S-Rosen committed Aug 31, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 8d73db2f89f52ca184107beb64be94bfaf5bb27f
3 changes: 2 additions & 1 deletion pymatgen/io/ase.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
from typing import TYPE_CHECKING

import numpy as np
from monty.json import jsanitize

from pymatgen.core.structure import Molecule, Structure

@@ -217,7 +218,7 @@ def get_structure(atoms: Atoms, cls: type[Structure] = Structure, **cls_kwargs)
sel_dyn = None

# Atoms.info <---> Structure.properties (excluding properties["calc"])
properties = getattr(atoms, "info", {})
properties = jsanitize(getattr(atoms, "info", {}))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janosh: Just wanted to point out one subtlety to you. Currently, we have the mapping: Atoms.info <--> Structure.properties, since they serve the same purpose between ASE and Pymtatgen, respectively. However, I jsanitize-d the atoms.info before assigning it to .properties to ensure the Structure/Molecule object can be (de)serialized since this is often an implicit assumption of Structure/Molecule objects but is not necessarily one for ASE Atoms objects. In practice, this means that there could be some scenarios where a user interconverts between ASE Atoms and Pymatgen Structure/Molecule with some loss of information; namely, if the atoms.info is modified by jsanitize() then it can't be restored in its original form. There might be a better solution than this, but I implemented it because oftentimes ASE will dump un-serializable class objects (namely, a Spacegroup class) into atoms.info, such as when reading in a CIF with symmetry flags.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for highlighting! Seems like a good way to go about this for now. I'll keep this in mind if we run into any issues.


# Return a Molecule object if that was specifically requested;
# otherwise return a Structure object as expected