Skip to content
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

KeyError of elemental references in pre-computed phase diagram #2809

Merged
merged 8 commits into from
Jan 21, 2023
Merged

KeyError of elemental references in pre-computed phase diagram #2809

merged 8 commits into from
Jan 21, 2023

Conversation

peikai
Copy link
Contributor

@peikai peikai commented Jan 20, 2023

When I try to plot a pre-computed phase diagrams that are retrieved via get_phase_diagram_from_chemsys() method,

phaseDiagram = mpr.get_phase_diagram_from_chemsys('Li-Fe-O')
plotter = PDPlotter(phaseDiagram)

a KeyError raises as shown below, which arises from the inconsistence on object stored in el_refs dictionary, i.e., the keys stored in phaseDiagram.el_refs are str object (dict_keys(['O', 'Fe', 'Li'])), instead of the Element object (dict_keys([Element O, Element Fe, Element Li])).

The pre-computed phase diagrams in database might not be updated conveniently. So, I add some codes in pymatgen.analysis.phase_diagram.PhaseDiagram class, to update keys in elemental reference dictionary to Element object, in case it loads a pre-computed phase diagram. A corresponding test unit is also added to check the keys in el_refs dictionary,

KeyError message:

Traceback (most recent call last):
File "D:\Seafile\peikai\My Libraries\Repos\Volume-Planning-for-Anodes\phase_diagram_mixscheme.py", line 37, in
plotter = PDPlotter(phase_diagram, show_unstable=0, backend='matplotlib')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\pymatgen2023\Lib\site-packages\pymatgen\analysis\phase_diagram.py", line 2111, in init
self._min_energy = min(self._pd.get_form_energy_per_atom(e) for e in self._pd.stable_entries)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\pymatgen2023\Lib\site-packages\pymatgen\analysis\phase_diagram.py", line 2111, in
self._min_energy = min(self._pd.get_form_energy_per_atom(e) for e in self._pd.stable_entries)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\pymatgen2023\Lib\site-packages\pymatgen\analysis\phase_diagram.py", line 577, in get_form_energy_per_atom
return self.get_form_energy(entry) / entry.composition.num_atoms
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\pymatgen2023\Lib\site-packages\pymatgen\analysis\phase_diagram.py", line 564, in get_form_energy
return entry.energy - sum(comp[el] * self.el_refs[el].energy_per_atom for el in comp.elements)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Anaconda\envs\pymatgen2023\Lib\site-packages\pymatgen\analysis\phase_diagram.py", line 564, in
return entry.energy - sum(comp[el] * self.el_refs[el].energy_per_atom for el in comp.elements)
~~~~~~~~~~~~^^^^
KeyError: Element Li

@coveralls
Copy link

coveralls commented Jan 20, 2023

Coverage Status

Coverage: 78.129% (-0.7%) from 78.807% when pulling cd146e3 on peikai:master into d9b8697 on materialsproject:master.

@munrojm
Copy link
Member

munrojm commented Jan 20, 2023

@peikai, I appreciate you making this PR. I just patched the API client to temporarily resolve this issue yesterday as we were having plotting issues on the front-end with the pre-computed data. I also found an additional change that needs to be made to for some ternary compounds. Namely, qhull_data needs to be a numpy array, and not a list. Although, it looks like you don't have that issue. In any case, to fix your local problems before this is merged, you can update mp-api as well.

@janosh, this relates to the emmet issue I tagged you in regarding PhaseDiagram de-serialization problems with monty.

@janosh
Copy link
Member

janosh commented Jan 21, 2023

@peikai Thanks for this PR. Do we really need pymatgen/analysis/tests/pre_computed_phase_diagram_Li-Fe-O.json? Maybe you can re-use an existing test file in the test_files/ dir? The repo size is already approaching 1GB which we really need to get down.

@peikai peikai requested a review from janosh January 21, 2023 16:51
@peikai
Copy link
Contributor Author

peikai commented Jan 21, 2023

@munrojm Thanks for the information. The updated mp-api works. And this PR would fix the issue in pymatgen.

@janosh janosh enabled auto-merge (squash) January 21, 2023 19:28
@janosh janosh merged commit dcbbe00 into materialsproject:master Jan 21, 2023
@peikai peikai changed the title KeyError of Elemental references in pre-computed phase diagram KeyError of elemental references in pre-computed phase diagram Jan 23, 2023
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.

4 participants