Skip to content

Commit 20b68ff

Browse files
felix-adamspre-commit-ci[bot]shyuepmkhorton
authored
Phase Diagram Serialization Improvement (#4414)
* Removed extra copy of entry data from phase diagram serialization. * Serialized qhull_entries as list of indices instead of list of entries. * pre-commit auto-fixes * Added phase diagram serialization backwards compatibilty. * Removed accidental side effects of PhaseDiagram.from_dict(). --------- Co-authored-by: Felix Adams <98037892+fadams-umd@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Shyue Ping Ong <shyuep@users.noreply.github.com> Co-authored-by: Matt Horton <mkhorton@users.noreply.github.com>
1 parent 94c78f2 commit 20b68ff

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/pymatgen/analysis/phase_diagram.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,17 @@ def __init__(
389389

390390
def as_dict(self):
391391
"""Get MSONable dict representation of PhaseDiagram."""
392+
393+
qhull_entry_indices = [self.all_entries.index(e) for e in self.qhull_entries]
394+
392395
return {
393396
"@module": type(self).__module__,
394397
"@class": type(self).__name__,
395-
"all_entries": [e.as_dict() for e in self.all_entries],
396398
"elements": [e.as_dict() for e in self.elements],
397-
"computed_data": self.computed_data,
399+
"computed_data": self.computed_data
400+
| {
401+
"qhull_entries": qhull_entry_indices,
402+
},
398403
}
399404

400405
@classmethod
@@ -406,9 +411,19 @@ def from_dict(cls, dct: dict[str, Any]) -> Self:
406411
Returns:
407412
PhaseDiagram
408413
"""
409-
entries = [MontyDecoder().process_decoded(entry) for entry in dct["all_entries"]]
410-
elements = [Element.from_dict(elem) for elem in dct["elements"]]
411414
computed_data = dct.get("computed_data")
415+
elements = [Element.from_dict(elem) for elem in dct["elements"]]
416+
417+
# for backwards compatibility, check for old format
418+
if "all_entries" in dct:
419+
entries = [MontyDecoder().process_decoded(entry) for entry in dct["all_entries"]]
420+
else:
421+
entries = [MontyDecoder().process_decoded(entry) for entry in computed_data["all_entries"]]
422+
423+
complete_qhull_entries = [computed_data["all_entries"][i] for i in computed_data["qhull_entries"]]
424+
425+
computed_data = computed_data | {"qhull_entries": complete_qhull_entries}
426+
412427
return cls(entries, elements, computed_data=computed_data)
413428

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

0 commit comments

Comments
 (0)