Skip to content

PatchedPhaseDiagram mimic PhaseDiagram Serialization#4541

Merged
shyuep merged 14 commits intomaterialsproject:masterfrom
CompRhys:ppd-to-json
Mar 10, 2026
Merged

PatchedPhaseDiagram mimic PhaseDiagram Serialization#4541
shyuep merged 14 commits intomaterialsproject:masterfrom
CompRhys:ppd-to-json

Conversation

@CompRhys
Copy link
Copy Markdown
Contributor

@CompRhys CompRhys commented Nov 11, 2025

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

  • Google format doc strings added. Check with ruff.
  • Type annotations included. Check with mypy.
  • Tests added for new features/fixes.
  • 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
Copy link
Copy Markdown
Contributor Author

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
Copy link
Copy Markdown
Contributor Author

CompRhys commented Nov 28, 2025

examples of code and new 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']}

@shyuep
Copy link
Copy Markdown
Member

shyuep commented Mar 10, 2026

Thanks. We have refactored pymatgen to move core packages to a separate pymatgen-core repo. If you don't mind, can you fix the merge conflicts and if necessary, put some of the changes in pymatgen-core pls? Thanks.

@CompRhys
Copy link
Copy Markdown
Contributor Author

Okay great! this should be able to be merged now. The changes to core were mostly unrelated typing consistency things and so have just discarded them from this PR.

@shyuep shyuep merged commit 05e5b5e into materialsproject:master Mar 10, 2026
6 checks passed
@shyuep
Copy link
Copy Markdown
Member

shyuep commented Mar 10, 2026

Thanks!

@CompRhys CompRhys deleted the ppd-to-json branch March 11, 2026 15:33
Yb: 31
Zn: 70
Coefficient of linear thermal expansion:
reference: "https://en.wikipedia.org/wiki/Thermal_expansivities_of_the_elements" # WEL
Copy link
Copy Markdown
Contributor

@DanielYang59 DanielYang59 Mar 14, 2026

Choose a reason for hiding this comment

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

I think here we should add this file to codespell exclude list instead:

exclude: src/pymatgen/analysis/aflow_prototypes.json

This comment was added because that wiki page might have multiple sources for certain entries (without use field), so it was added to make sure people know the actual source used, for example:

Image

Maybe I should have added a description field to explain this better...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants