Skip to content

Commit 2ec98bd

Browse files
author
Jason Munro
authored
Allow thermo_type string passing for phase diagram method (#742)
* Allow string inputs for thermo type in pd method * Fix type hint * Linting * Fix exclude element testts * Update ele tests * Exclude nsites * Revert test change * Fix set
1 parent 9dc27dc commit 2ec98bd

4 files changed

Lines changed: 22 additions & 38 deletions

File tree

mp_api/client/mprester.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def __init__(
169169
session=self.session,
170170
monty_decode=monty_decode,
171171
use_document_model=use_document_model,
172-
headers=self.headers
172+
headers=self.headers,
173173
) # type: BaseRester
174174

175175
self._all_resters.append(rester)
@@ -710,9 +710,7 @@ def get_ion_reference_data(self) -> List[Dict]:
710710
compounds and aqueous species, Wiley, New York (1978)'}}
711711
"""
712712
return self.contribs.query_contributions(
713-
query={"project": "ion_ref_data"},
714-
fields=["identifier", "formula", "data"],
715-
paginate=True
713+
query={"project": "ion_ref_data"}, fields=["identifier", "formula", "data"], paginate=True
716714
).get("data")
717715

718716
def get_ion_reference_data_for_chemsys(self, chemsys: Union[str, List]) -> List[Dict]:
@@ -926,7 +924,7 @@ def get_entries_in_chemsys(
926924
correspond to proper function inputs to `MPRester.thermo.search`. For instance,
927925
if you are only interested in entries on the convex hull, you could pass
928926
{"energy_above_hull": (0.0, 0.0)} or {"is_stable": True}, or if you are only interested
929-
in entry data
927+
in entry data
930928
Returns:
931929
List of ComputedStructureEntries.
932930
"""

mp_api/client/routes/thermo.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ def search_thermo_docs(self, *args, **kwargs): # pragma: no cover
2424
"""
2525

2626
warnings.warn(
27-
"MPRester.thermo.search_thermo_docs is deprecated. "
28-
"Please use MPRester.thermo.search instead.",
27+
"MPRester.thermo.search_thermo_docs is deprecated. " "Please use MPRester.thermo.search instead.",
2928
DeprecationWarning,
3029
stacklevel=2,
3130
)
@@ -109,25 +108,19 @@ def search(
109108
t_types = {t if isinstance(t, str) else t.value for t in thermo_types}
110109
valid_types = {*map(str, ThermoType.__members__.values())}
111110
if invalid_types := t_types - valid_types:
112-
raise ValueError(
113-
f"Invalid thermo type(s) passed: {invalid_types}, valid types are: {valid_types}"
114-
)
111+
raise ValueError(f"Invalid thermo type(s) passed: {invalid_types}, valid types are: {valid_types}")
115112
query_params.update({"thermo_types": ",".join(t_types)})
116113

117114
if num_elements:
118115
if isinstance(num_elements, int):
119116
num_elements = (num_elements, num_elements)
120-
query_params.update(
121-
{"nelements_min": num_elements[0], "nelements_max": num_elements[1]}
122-
)
117+
query_params.update({"nelements_min": num_elements[0], "nelements_max": num_elements[1]})
123118

124119
if is_stable is not None:
125120
query_params.update({"is_stable": is_stable})
126121

127122
if sort_fields:
128-
query_params.update(
129-
{"_sort_fields": ",".join([s.strip() for s in sort_fields])}
130-
)
123+
query_params.update({"_sort_fields": ",".join([s.strip() for s in sort_fields])})
131124

132125
name_dict = {
133126
"total_energy": "energy_per_atom",
@@ -146,11 +139,7 @@ def search(
146139
}
147140
)
148141

149-
query_params = {
150-
entry: query_params[entry]
151-
for entry in query_params
152-
if query_params[entry] is not None
153-
}
142+
query_params = {entry: query_params[entry] for entry in query_params if query_params[entry] is not None}
154143

155144
return super()._search(
156145
num_chunks=num_chunks,
@@ -161,7 +150,7 @@ def search(
161150
)
162151

163152
def get_phase_diagram_from_chemsys(
164-
self, chemsys: str, thermo_type: ThermoType = ThermoType.GGA_GGA_U
153+
self, chemsys: str, thermo_type: Union[ThermoType, str] = ThermoType.GGA_GGA_U
165154
) -> PhaseDiagram:
166155
"""
167156
Get a pre-computed phase diagram for a given chemsys.
@@ -173,8 +162,14 @@ def get_phase_diagram_from_chemsys(
173162
Returns:
174163
phase_diagram (PhaseDiagram): Pymatgen phase diagram object.
175164
"""
165+
166+
t_type = thermo_type if isinstance(thermo_type, str) else thermo_type.value
167+
valid_types = {*map(str, ThermoType.__members__.values())}
168+
if invalid_types := {t_type} - valid_types:
169+
raise ValueError(f"Invalid thermo type(s) passed: {invalid_types}, valid types are: {valid_types}")
170+
176171
sorted_chemsys = "-".join(sorted(chemsys.split("-")))
177-
phase_diagram_id = f"{sorted_chemsys}_{thermo_type.value}"
172+
phase_diagram_id = f"{sorted_chemsys}_{t_type}"
178173
response = self._query_resource(
179174
fields=["phase_diagram"],
180175
suburl=f"phase_diagram/{phase_diagram_id}",

tests/test_materials.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def rester():
1919
"num_chunks",
2020
"all_fields",
2121
"fields",
22+
"exclude_elements", # temp until timeout update
2223
]
2324

2425
sub_doc_fields = [] # type: list
@@ -39,17 +40,14 @@ def rester():
3940
"formula": "Si",
4041
"chemsys": "Si-O",
4142
"elements": ["Si", "O"],
42-
"exclude_elements": ["Si"],
4343
"task_ids": ["mp-149"],
4444
"crystal_system": CrystalSystem.cubic,
4545
"spacegroup_number": 38,
4646
"spacegroup_symbol": "Amm2",
4747
} # type: dict
4848

4949

50-
@pytest.mark.skipif(
51-
os.environ.get("MP_API_KEY", None) is None, reason="No API key found."
52-
)
50+
@pytest.mark.skipif(os.environ.get("MP_API_KEY", None) is None, reason="No API key found.")
5351
def test_client(rester):
5452
search_method = rester.search
5553

@@ -98,7 +96,4 @@ def test_client(rester):
9896
if sub_field in doc:
9997
doc = doc[sub_field]
10098

101-
assert (
102-
doc[project_field if project_field is not None else param]
103-
is not None
104-
)
99+
assert doc[project_field if project_field is not None else param] is not None

tests/test_summary.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"all_fields",
1515
"fields",
1616
"equilibrium_reaction_energy", # temp until data update
17+
"exclude_elements", # temp until data update
1718
]
1819

1920
alt_name_dict = {
@@ -42,7 +43,6 @@
4243
"formula": "SiO2",
4344
"chemsys": "Si-O",
4445
"elements": ["Si", "O"],
45-
"exclude_elements": ["Si"],
4646
"possible_species": ["O2-"],
4747
"crystal_system": CrystalSystem.cubic,
4848
"spacegroup_number": 38,
@@ -54,9 +54,7 @@
5454
} # type: dict
5555

5656

57-
@pytest.mark.skipif(
58-
os.environ.get("MP_API_KEY", None) is None, reason="No API key found."
59-
)
57+
@pytest.mark.skipif(os.environ.get("MP_API_KEY", None) is None, reason="No API key found.")
6058
def test_client():
6159

6260
search_method = SummaryRester().search
@@ -107,6 +105,4 @@ def test_client():
107105
else:
108106
raise ValueError("No documents returned")
109107

110-
assert (
111-
doc[project_field if project_field is not None else param] is not None
112-
)
108+
assert doc[project_field if project_field is not None else param] is not None

0 commit comments

Comments
 (0)