Skip to content

Phase Diagram Serialization Improvement #4414

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
23 changes: 19 additions & 4 deletions src/pymatgen/analysis/phase_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,17 @@ def __init__(

def as_dict(self):
"""Get MSONable dict representation of PhaseDiagram."""

qhull_entry_indices = [self.all_entries.index(e) for e in self.qhull_entries]

return {
"@module": type(self).__module__,
"@class": type(self).__name__,
"all_entries": [e.as_dict() for e in self.all_entries],
"elements": [e.as_dict() for e in self.elements],
"computed_data": self.computed_data,
"computed_data": self.computed_data
| {
"qhull_entries": qhull_entry_indices,
},
}

@classmethod
Expand All @@ -406,9 +411,19 @@ def from_dict(cls, dct: dict[str, Any]) -> Self:
Returns:
PhaseDiagram
"""
entries = [MontyDecoder().process_decoded(entry) for entry in dct["all_entries"]]
elements = [Element.from_dict(elem) for elem in dct["elements"]]
computed_data = dct.get("computed_data")
elements = [Element.from_dict(elem) for elem in dct["elements"]]

# for backwards compatibility, check for old format
if "all_entries" in dct:
entries = [MontyDecoder().process_decoded(entry) for entry in dct["all_entries"]]
else:
entries = [MontyDecoder().process_decoded(entry) for entry in computed_data["all_entries"]]

complete_qhull_entries = [computed_data["all_entries"][i] for i in computed_data["qhull_entries"]]

computed_data = computed_data | {"qhull_entries": complete_qhull_entries}

return cls(entries, elements, computed_data=computed_data)

def _compute(self) -> dict[str, Any]:
Expand Down