Skip to content

Commit 6d2e77e

Browse files
authored
Fix MPRester tests and access phonon properties from the new API without having mp-api installed. (#3950)
1 parent 1bdca30 commit 6d2e77e

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

src/pymatgen/ext/matproj.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def get_summary_by_material_id(self, material_id: str, fields: list | None = Non
178178
Dict
179179
"""
180180
get = "_all_fields=True" if fields is None else "_fields=" + ",".join(fields)
181-
return self.request(f"materials/summary/{material_id}/?{get}")[0]
181+
return self.request(f"materials/summary/?{get}", payload={"material_ids": material_id})[0]
182182

183183
get_doc = get_summary_by_material_id
184184

@@ -349,6 +349,32 @@ def get_entries_in_chemsys(self, elements, *args, **kwargs):
349349

350350
return self.get_entries(criteria, *args, **kwargs)
351351

352+
def get_phonon_bandstructure_by_material_id(self, material_id: str):
353+
"""Get phonon bandstructure by material_id.
354+
355+
Args:
356+
material_id (str): Materials Project material_id
357+
358+
Returns:
359+
PhononBandStructureSymmLine: A phonon band structure.
360+
"""
361+
prop = "ph_bs"
362+
response = self.request(f"materials/phonon/?material_ids={material_id}&_fields={prop}")
363+
return response[0][prop]
364+
365+
def get_phonon_dos_by_material_id(self, material_id: str):
366+
"""Get phonon density of states by material_id.
367+
368+
Args:
369+
material_id (str): Materials Project material_id
370+
371+
Returns:
372+
CompletePhononDos: A phonon DOS object.
373+
"""
374+
prop = "ph_dos"
375+
response = self.request(f"materials/phonon/?material_ids={material_id}&_fields={prop}")
376+
return response[0][prop]
377+
352378

353379
class MPRester:
354380
"""A class to conveniently interface with the new and legacy Materials Project REST interface.

tests/ext/test_matproj.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,28 @@
1616
from pymatgen.electronic_structure.dos import CompleteDos
1717
from pymatgen.entries.compatibility import MaterialsProject2020Compatibility
1818
from pymatgen.entries.computed_entries import ComputedEntry
19-
from pymatgen.ext.matproj import MP_LOG_FILE, MPRestError, _MPResterBasic
20-
from pymatgen.ext.matproj_legacy import TaskType, _MPResterLegacy
19+
from pymatgen.ext.matproj import MP_LOG_FILE, _MPResterBasic
20+
from pymatgen.ext.matproj_legacy import MPRestError, TaskType, _MPResterLegacy
2121
from pymatgen.phonon.bandstructure import PhononBandStructureSymmLine
2222
from pymatgen.phonon.dos import CompletePhononDos
2323
from pymatgen.util.testing import TEST_FILES_DIR, PymatgenTest
2424
from pytest import approx
2525
from ruamel.yaml import YAML
2626

27+
PMG_MAPI_KEY = SETTINGS.get("PMG_MAPI_KEY", "")
28+
if (10 < len(PMG_MAPI_KEY) <= 20) and "PMG_MAPI_KEY" in SETTINGS:
29+
MP_URL = "https://legacy.materialsproject.org"
30+
elif len(PMG_MAPI_KEY) > 20:
31+
MP_URL = "https://api.materialsproject.org"
32+
else:
33+
MP_URL = "https://materialsproject.org"
2734
try:
28-
skip_mprester_tests = requests.get("https://materialsproject.org", timeout=600).status_code != 200
35+
skip_mprester_tests = requests.get(MP_URL, timeout=600).status_code != 200
2936

3037
except (ModuleNotFoundError, ImportError, requests.exceptions.ConnectionError):
3138
# Skip all MPRester tests if some downstream problem on the website, mp-api or whatever.
3239
skip_mprester_tests = True
3340

34-
PMG_MAPI_KEY = SETTINGS.get("PMG_MAPI_KEY", "")
35-
3641

3742
@pytest.mark.skipif(
3843
skip_mprester_tests or (not 10 < len(PMG_MAPI_KEY) <= 20),
@@ -513,8 +518,8 @@ def test_api_key_is_none(self):
513518
skip_mprester_tests or (not len(PMG_MAPI_KEY) > 20),
514519
reason="PMG_MAPI_KEY environment variable not set or MP API is down.",
515520
)
516-
class TestMPResterNewBasic:
517-
def setup(self):
521+
class TestMPResterNewBasic(PymatgenTest):
522+
def setUp(self):
518523
self.rester = _MPResterBasic()
519524

520525
def test_get_summary(self):
@@ -679,11 +684,12 @@ def test_get_entry_by_material_id(self):
679684
# assert isinstance(bs_unif, BandStructure)
680685
# assert not isinstance(bs_unif, BandStructureSymmLine)
681686
#
682-
# def test_get_phonon_data_by_material_id(self):
683-
# bs = self.rester.get_phonon_bandstructure_by_material_id("mp-661")
684-
# assert isinstance(bs, PhononBandStructureSymmLine)
685-
# dos = self.rester.get_phonon_dos_by_material_id("mp-661")
686-
# assert isinstance(dos, CompletePhononDos)
687+
def test_get_phonon_data_by_material_id(self):
688+
bs = self.rester.get_phonon_bandstructure_by_material_id("mp-661")
689+
assert isinstance(bs, PhononBandStructureSymmLine)
690+
dos = self.rester.get_phonon_dos_by_material_id("mp-661")
691+
assert isinstance(dos, CompletePhononDos)
692+
687693
# ddb_str = self.rester.get_phonon_ddb_by_material_id("mp-661")
688694
# assert isinstance(ddb_str, str)
689695

0 commit comments

Comments
 (0)