Skip to content

Commit 3d47747

Browse files
committed
deepcopy atoms.info/structure.properties during ase/pymatgen conversion
this fixes bug where atoms/structure objects were themselves modified when converted
1 parent c76c7a6 commit 3d47747

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/pymatgen/io/ase.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import warnings
99
from importlib.metadata import PackageNotFoundError
1010
from typing import TYPE_CHECKING
11+
import copy
1112

1213
import numpy as np
1314
from monty.json import MontyDecoder, MSONable, jsanitize
@@ -204,7 +205,7 @@ def get_atoms(structure: SiteCollection, msonable: bool = True, **kwargs) -> MSO
204205

205206
# Atoms.info <---> Structure.properties
206207
if properties := structure.properties:
207-
atoms.info = properties
208+
atoms.info = copy.deepcopy(properties)
208209

209210
# Regenerate Spacegroup object from `.todict()` representation
210211
if isinstance(atoms.info.get("spacegroup"), dict):
@@ -298,10 +299,11 @@ def get_structure(atoms: Atoms, cls: type[Structure] = Structure, **cls_kwargs)
298299
sel_dyn = None
299300

300301
# Atoms.info <---> Structure.properties
301-
# But first make sure `spacegroup` is JSON serializable
302-
if atoms.info.get("spacegroup") and isinstance(atoms.info["spacegroup"], Spacegroup):
303-
atoms.info["spacegroup"] = atoms.info["spacegroup"].todict()
304-
properties = getattr(atoms, "info", {})
302+
properties = copy.deepcopy(getattr(atoms, "info", {}))
303+
# If present, convert Spacegroup object to JSON-serializable dict
304+
if properties.get("spacegroup") and isinstance(properties["spacegroup"], Spacegroup):
305+
properties["spacegroup"] = properties["spacegroup"].todict()
306+
305307

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

0 commit comments

Comments
 (0)