pymatgen icon indicating copy to clipboard operation
pymatgen copied to clipboard

PatchedPhaseDiagram mimic PhaseDiagram Serialization

Open CompRhys opened this issue 5 months ago • 2 comments

Summary

This avoids the duplication of data when loading a ppd as so allows for serialization to text which is much more robust than using pickle.

Attempts to type hint phase diagrams better hence touching more files to get mypy consistency.

#3940 #4414

Checklist

  • [x] Google format doc strings added. Check with ruff.
  • [x] Type annotations included. Check with mypy.
  • [x] Tests added for new features/fixes.
  • [x] If applicable, new classes/functions/modules have duecredit @due.dcite decorators to reference relevant papers by DOI (example)

Tip: Install pre-commit hooks to auto-check types and linting before every commit:

pip install -U pre-commit
pre-commit install

CompRhys avatar Nov 11 '25 18:11 CompRhys

This does make some breaking changes to #4414 as some of the objects there were left hydrated after as_dict and this also changes that behavior for the PhaseDiagram.

CompRhys avatar Nov 13 '25 01:11 CompRhys

examples of code and new (breaking?) outputs

from pprint import pprint

from pymatgen.analysis.phase_diagram import PatchedPhaseDiagram, PhaseDiagram, PDEntry
from pymatgen.core.composition import Composition

# %%
entries = [
    PDEntry(Composition("Li"), energy=-1.0),
    PDEntry(Composition("O2"), energy=-6.0),
    PDEntry(Composition("Li2O"), energy=-12.0),
    PDEntry(Composition("LiO2"), energy=-9.0),
    PDEntry("He", -1.23)
]

# %%
pd = PhaseDiagram(entries)

ppd = PatchedPhaseDiagram(entries)

# %%
pd_dict = pd.as_dict()
pprint(pd_dict)

# %%
ppd_dict = ppd.as_dict()
pprint(ppd_dict)
{'@class': 'PhaseDiagram',
 '@module': 'pymatgen.analysis.phase_diagram',
 'computed_data': {'all_entries': [{'@class': 'PDEntry',
                                    '@module': 'pymatgen.analysis.phase_diagram',
                                    'attribute': None,
                                    'composition': {'He': 1.0},
                                    'energy': -1.23,
                                    'name': 'He'},
                                   {'@class': 'PDEntry',
                                    '@module': 'pymatgen.analysis.phase_diagram',
                                    'attribute': None,
                                    'composition': {'O': 2.0},
                                    'energy': -6.0,
                                    'name': 'O2'},
                                   {'@class': 'PDEntry',
                                    '@module': 'pymatgen.analysis.phase_diagram',
                                    'attribute': None,
                                    'composition': {'Li': 1.0},
                                    'energy': -1.0,
                                    'name': 'Li'},
                                   {'@class': 'PDEntry',
                                    '@module': 'pymatgen.analysis.phase_diagram',
                                    'attribute': None,
                                    'composition': {'Li': 1.0, 'O': 2.0},
                                    'energy': -9.0,
                                    'name': 'LiO2'},
                                   {'@class': 'PDEntry',
                                    '@module': 'pymatgen.analysis.phase_diagram',
                                    'attribute': None,
                                    'composition': {'Li': 2.0, 'O': 1.0},
                                    'energy': -12.0,
                                    'name': 'Li2O'}],
                   'el_refs': [('He', 0), ('O', 1), ('Li', 2)],
                   'facets': [[1, 3, 2], [4, 1, 2]],
                   'qhull_data': [[0.6666666666666666, 0.0, -3.0],
                                  [0.3333333333333333, 0.0, -4.0],
                                  [0.0, 1.0, -1.23],
                                  [1.0, 0.0, -3.0],
                                  [0.0, 0.0, -1.0],
                                  [0.3333333333333333,
                                   0.3333333333333333,
                                   2.0]],
                   'qhull_entries': [3, 4, 0, 1, 2]},
 'elements': ['Li', 'O', 'He']}
{'@class': 'PatchedPhaseDiagram',
 '@module': 'pymatgen.analysis.phase_diagram',
 'computed_data': {'all_entries': [{'@class': 'PDEntry',
                                    '@module': 'pymatgen.analysis.phase_diagram',
                                    'attribute': None,
                                    'composition': {'He': 1.0},
                                    'energy': -1.23,
                                    'name': 'He'},
                                   {'@class': 'PDEntry',
                                    '@module': 'pymatgen.analysis.phase_diagram',
                                    'attribute': None,
                                    'composition': {'O': 2.0},
                                    'energy': -6.0,
                                    'name': 'O2'},
                                   {'@class': 'PDEntry',
                                    '@module': 'pymatgen.analysis.phase_diagram',
                                    'attribute': None,
                                    'composition': {'Li': 1.0},
                                    'energy': -1.0,
                                    'name': 'Li'},
                                   {'@class': 'PDEntry',
                                    '@module': 'pymatgen.analysis.phase_diagram',
                                    'attribute': None,
                                    'composition': {'Li': 1.0, 'O': 2.0},
                                    'energy': -9.0,
                                    'name': 'LiO2'},
                                   {'@class': 'PDEntry',
                                    '@module': 'pymatgen.analysis.phase_diagram',
                                    'attribute': None,
                                    'composition': {'Li': 2.0, 'O': 1.0},
                                    'energy': -12.0,
                                    'name': 'Li2O'}],
                   'el_refs': [('He', 0), ('O', 1), ('Li', 2)],
                   'pds': {'Li-O': {'all_entries': [1, 2, 3, 4],
                                    'el_refs': [('O', 1), ('Li', 2)],
                                    'elements': ['Li', 'O'],
                                    'facets': [[1, 3], [1, 2]],
                                    'qhull_data': [[0.6666666666666666, -3.0],
                                                   [0.3333333333333333, -4.0],
                                                   [1.0, -3.0],
                                                   [0.0, -1.0],
                                                   [0.5, 2.0]],
                                    'qhull_entries': [3, 4, 1, 2]}},
                   'qhull_entries': [3, 4, 0, 1, 2],
                   'spaces': [('Li', 'O')]},
 'elements': ['Li', 'O', 'He']}

CompRhys avatar Nov 28 '25 20:11 CompRhys