Skip to content

Commit 499fed1

Browse files
more housekeeping
1 parent 0303331 commit 499fed1

9 files changed

Lines changed: 60 additions & 32 deletions

File tree

mp_api/client/core/client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,6 @@ def _query_resource(
461461
url = validate_endpoint(self.endpoint, suffix=suburl)
462462

463463
if query_s3:
464-
db_version = self.db_version.replace(".", "-")
465464
if "/" not in self.suffix:
466465
suffix = self.suffix
467466
elif self.suffix == "molecules/summary":
@@ -477,7 +476,7 @@ def _query_resource(
477476
bucket_suffix, prefix = "parsed", "tasks_atomate2"
478477
else:
479478
bucket_suffix = "build"
480-
prefix = f"collections/{db_version}/{suffix}"
479+
prefix = f"collections/{self.db_version.replace('.', '-')}/{suffix}"
481480

482481
bucket = f"materialsproject-{bucket_suffix}"
483482
paginator = self.s3_client.get_paginator("list_objects_v2")

mp_api/client/mprester.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from emmet.core.electronic_structure import BSPathType
1010
from emmet.core.mpid import MPID, AlphaID
11-
from emmet.core.tasks import TaskDoc
1211
from emmet.core.types.enums import ThermoType
1312
from emmet.core.vasp.calc_types import CalcType
1413
from packaging import version
@@ -38,30 +37,23 @@
3837
if TYPE_CHECKING:
3938
from typing import Any, Literal
4039

40+
from emmet.core.tasks import CoreTaskDoc
4141
from pymatgen.analysis.phase_diagram import PDEntry
4242
from pymatgen.entries.computed_entries import ComputedEntry
4343

44+
4445
DEFAULT_THERMOTYPE_CRITERIA = {"thermo_types": ["GGA_GGA+U"]}
4546

4647
RESTER_LAYOUT = {
4748
"molecules/core": LazyImport(
4849
"mp_api.client.routes.molecules.molecules.MoleculeRester"
4950
),
50-
"materials/core": MATERIALS_RESTERS["materials"],
51-
**{
52-
f"materials/{k}": v
53-
for k, v in MATERIALS_RESTERS.items()
54-
if k not in {"materials", "doi"}
55-
},
51+
"materials/core": LazyImport(
52+
"mp_api.client.routes.materials.materials.MaterialsRester"
53+
),
54+
**{f"materials/{k}": v for k, v in MATERIALS_RESTERS.items() if k not in {"doi"}},
5655
"doi": MATERIALS_RESTERS["doi"],
57-
**{
58-
f"molecules/{k}": v
59-
for k, v in MOLECULES_RESTERS.items()
60-
if k
61-
not in {
62-
"molecules",
63-
}
64-
},
56+
**{f"molecules/{k}": v for k, v in MOLECULES_RESTERS.items()},
6557
**GENERIC_RESTERS,
6658
}
6759

@@ -1134,22 +1126,21 @@ def get_wulff_shape(self, material_id: str):
11341126

11351127
def get_charge_density_from_task_id(
11361128
self, task_id: str, inc_task_doc: bool = False
1137-
) -> Chgcar | tuple[Chgcar, TaskDoc | dict] | None:
1129+
) -> Chgcar | tuple[Chgcar, CoreTaskDoc | dict] | None:
11381130
"""Get charge density data for a given task_id.
11391131
11401132
Arguments:
11411133
task_id (str): A task id
11421134
inc_task_doc (bool): Whether to include the task document in the returned data.
11431135
11441136
Returns:
1145-
(Chgcar, (Chgcar, TaskDoc | dict), None): Pymatgen Chgcar object, or tuple with object and TaskDoc
1137+
(Chgcar, (Chgcar, CoreTaskDoc | dict), None): Pymatgen Chgcar object, or tuple with object and CoreTaskDoc
11461138
"""
1147-
kwargs = dict(
1139+
chgcar = self.materials.tasks._query_open_data(
11481140
bucket="materialsproject-parsed",
11491141
key=f"chgcars/{validate_ids([task_id])[0]}.json.gz",
11501142
decoder=lambda x: load_json(x, deser=True),
1151-
)
1152-
chgcar = self.materials.tasks._query_open_data(**kwargs)[0][0]["data"]
1143+
)[0][0]["data"]
11531144

11541145
if inc_task_doc:
11551146
task_doc = self.materials.tasks.search(task_ids=task_id)[0]
@@ -1159,15 +1150,16 @@ def get_charge_density_from_task_id(
11591150

11601151
def get_charge_density_from_material_id(
11611152
self, material_id: str, inc_task_doc: bool = False
1162-
) -> Chgcar | tuple[Chgcar, TaskDoc | dict] | None:
1153+
) -> Chgcar | tuple[Chgcar, CoreTaskDoc | dict] | None:
11631154
"""Get charge density data for a given Materials Project ID.
11641155
11651156
Arguments:
11661157
material_id (str): Material Project ID
11671158
inc_task_doc (bool): Whether to include the task document in the returned data.
11681159
11691160
Returns:
1170-
(Chgcar, (Chgcar, TaskDoc | dict), None): Pymatgen Chgcar object, or tuple with object and TaskDoc
1161+
(Chgcar, (Chgcar, CoreTaskDoc | dict), None): Pymatgen Chgcar object,
1162+
or tuple with object and CoreTaskDoc
11711163
"""
11721164
# TODO: really we want a recommended task_id for charge densities here
11731165
# this could potentially introduce an ambiguity
@@ -1177,7 +1169,7 @@ def get_charge_density_from_material_id(
11771169
if not task_ids:
11781170
return None
11791171

1180-
results: list[TaskDoc] = self.materials.tasks.search(
1172+
results: list[CoreTaskDoc] = self.materials.tasks.search(
11811173
task_ids=task_ids, fields=["last_updated", "task_id"]
11821174
) # type: ignore
11831175

mp_api/client/routes/materials/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Define routes and imports to materials resters."""
1+
"""Define routes and imports to non-core materials resters."""
22
from __future__ import annotations
33

44
from mp_api.client.core.utils import LazyImport
@@ -29,7 +29,6 @@
2929
("grain_boundaries", "grain_boundaries", "GrainBoundaryRester"),
3030
("insertion_electrodes", "electrodes", "ElectrodeRester"),
3131
("magnetism", "magnetism", "MagnetismRester"),
32-
("materials", "materials", "MaterialsRester"),
3332
("oxidation_states", "oxidation_states", "OxidationStatesRester"),
3433
("phonon", "phonon", "PhononRester"),
3534
("piezoelectric", "piezo", "PiezoRester"),

mp_api/client/routes/molecules/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
"""Define routes to non-core molecules resters."""
12
from __future__ import annotations
23

34
from mp_api.client.core.utils import LazyImport
45

5-
MOLECULES_RESTERS = {
6+
MOLECULES_RESTERS: dict[str, LazyImport] = {
67
k: LazyImport(f"mp_api.client.routes.molecules.{k}.{v}")
78
for k, v in (
8-
("molecules", "MoleculeRester"),
99
("jcesr", "JcesrMoleculesRester"),
1010
("summary", "MoleculesSummaryRester"),
1111
)

mp_api/client/routes/molecules/molecules.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""Define core molecules functionality.
2+
3+
Note that these classes are not currently working.
4+
The `MoleculeRester` methods: `search`, `find_molecule`, `get_molecule_by_mpculeid`,
5+
all return 404s when attempting to use them.
6+
"""
7+
18
from __future__ import annotations
29

310
from emmet.core.mpid import MPculeID
@@ -190,6 +197,3 @@ class AssociatedMoleculeRester(BaseMoleculeRester):
190197
class MoleculeRester(BaseMoleculeRester):
191198
suffix = "molecules/core"
192199
_sub_resters = MOLECULES_RESTERS
193-
194-
def __dir__(self):
195-
return dir(MoleculeRester) + self._sub_resters

tests/core/test_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
from mp_api.client.core.utils import LazyImport
88

99

10+
def test_lazy_import_module():
11+
import_str = "typing"
12+
lazy_mod = LazyImport(import_str)
13+
assert lazy_mod._module_name == import_str
14+
assert lazy_mod._class_name is None
15+
assert lazy_mod.__repr__() == str(lazy_mod)
16+
lazy_any = lazy_mod.Any
17+
18+
from typing import Any
19+
20+
assert lazy_any == Any
21+
22+
# Ensure failure to load raises error
23+
with pytest.raises(ImportError, match="Failed to import"):
24+
_ = LazyImport("tieping")._load()
25+
26+
1027
def test_lazy_import_function():
1128
import_str = "json.dumps"
1229
lazy_func = LazyImport(import_str)

tests/materials/test_materials.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from emmet.core.symmetry import CrystalSystem
66

77
from mp_api.client.routes.materials.materials import MaterialsRester
8+
from mp_api.client.routes.materials import MATERIALS_RESTERS
89

910

1011
@pytest.fixture
@@ -55,6 +56,10 @@ def rester():
5556
def test_client(rester):
5657
search_method = rester.search
5758

59+
assert sorted(dir(rester)) == sorted(
60+
dir(rester.__class__) + list(MATERIALS_RESTERS)
61+
)
62+
5863
client_search_testing(
5964
search_method=search_method,
6065
excluded_params=excluded_params,

tests/molecules/test_molecules.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Test basic molecules functionality."""
2+
3+
from mp_api.client.routes.molecules.molecules import BaseMoleculeRester, MoleculeRester
4+
from mp_api.client.routes.molecules import MOLECULES_RESTERS
5+
6+
7+
def test_molecule_rester():
8+
assert set(dir(MoleculeRester())) == set(
9+
dir(BaseMoleculeRester) + list(MOLECULES_RESTERS)
10+
)

tests/test_mprester.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ def test_get_charge_density_from_material_id(self, mpr):
365365
assert isinstance(chgcar, Chgcar)
366366
assert isinstance(TaskDoc.model_validate(task_doc.model_dump()), TaskDoc)
367367

368+
assert mpr.get_charge_density_from_material_id("mp-0") is None
369+
368370
def test_get_charge_density_from_task_id(self, mpr):
369371
chgcar = mpr.get_charge_density_from_task_id("mp-2246557")
370372
assert isinstance(chgcar, Chgcar)

0 commit comments

Comments
 (0)