Skip to content

Commit

Permalink
Accept list[str] for thermo_types in ThermoRester.search() (#729)
Browse files Browse the repository at this point in the history
* accept list[str] for thermo_types in ThermoRester.search()

* fix valid_types (was using keys, not values of ThermoType enum)

* only print invalid thermo types in ValueError msg

* Add thermo types to docstrings

Co-authored-by: Jason Munro <jmunro@lbl.gov>
  • Loading branch information
janosh and munrojm committed Jan 26, 2023
1 parent 1e83e2b commit 0713f8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
10 changes: 9 additions & 1 deletion mp_api/client/mprester.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ def get_entries(
Get a list of ComputedEntries or ComputedStructureEntries corresponding
to a chemical system or formula.
Note that by default this returns mixed GGA/GGA+U entries. For others,
pass GGA/GGA+U/R2SCAN, or R2SCAN as thermo_types in additional_criteria.
Args:
chemsys_formula_mpids (str, List[str]): A chemical system, list of chemical systems
(e.g., Li-Fe-O, Si-*, [Si-O, Li-Fe-P]), formula, list of formulas
Expand Down Expand Up @@ -889,6 +892,10 @@ def get_entries_in_chemsys(
entries in the Li-Fe-O chemical system, i.e., all LixOy,
FexOy, LixFey, LixFeyOz, Li, Fe and O phases. Extremely useful for
creating phase diagrams of entire chemical systems.
Note that by default this returns mixed GGA/GGA+U entries. For others,
pass GGA/GGA+U/R2SCAN, or R2SCAN as thermo_types in additional_criteria.
Args:
elements (str or [str]): Chemical system string comprising element
symbols separated by dashes, e.g., "Li-Fe-O" or List of element
Expand Down Expand Up @@ -918,7 +925,8 @@ def get_entries_in_chemsys(
additional_criteria (dict): Any additional criteria to pass. The keys and values should
correspond to proper function inputs to `MPRester.thermo.search`. For instance,
if you are only interested in entries on the convex hull, you could pass
{"energy_above_hull": (0.0, 0.0)} or {"is_stable": True}.
{"energy_above_hull": (0.0, 0.0)} or {"is_stable": True}, or if you are only interested
in entry data
Returns:
List of ComputedStructureEntries.
"""
Expand Down
20 changes: 13 additions & 7 deletions mp_api/client/routes/thermo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import warnings
import numpy as np
from collections import defaultdict
from typing import Optional, List, Tuple, Union
from mp_api.client.core import BaseRester
from mp_api.client.core.utils import validate_ids
from typing import List, Optional, Tuple, Union

from emmet.core.thermo import ThermoDoc, ThermoType
from pymatgen.analysis.phase_diagram import PhaseDiagram
from pymatgen.core import Element

from mp_api.client.core import BaseRester
from mp_api.client.core.utils import validate_ids


class ThermoRester(BaseRester[ThermoDoc]):

Expand Down Expand Up @@ -41,7 +43,7 @@ def search(
material_ids: Optional[List[str]] = None,
num_elements: Optional[Tuple[int, int]] = None,
thermo_ids: Optional[List[str]] = None,
thermo_types: Optional[List[ThermoType]] = [ThermoType.GGA_GGA_U],
thermo_types: Optional[List[Union[ThermoType, str]]] = [ThermoType.GGA_GGA_U],
total_energy: Optional[Tuple[float, float]] = None,
uncorrected_energy: Optional[Tuple[float, float]] = None,
sort_fields: Optional[List[str]] = None,
Expand Down Expand Up @@ -104,9 +106,13 @@ def search(
query_params.update({"thermo_ids": ",".join(validate_ids(thermo_ids))})

if thermo_types:
query_params.update(
{"thermo_types": ",".join([t.value for t in thermo_types])}
)
t_types = {t if isinstance(t, str) else t.value for t in thermo_types}
valid_types = {*map(str, ThermoType.__members__.values())}
if invalid_types := t_types - valid_types:
raise ValueError(
f"Invalid thermo type(s) passed: {invalid_types}, valid types are: {valid_types}"
)
query_params.update({"thermo_types": ",".join(t_types)})

if num_elements:
if isinstance(num_elements, int):
Expand Down

0 comments on commit 0713f8c

Please sign in to comment.