Skip to content

Commit 06431e5

Browse files
committed
Fix deps, typehints, comments add fields argument
1 parent bb53de1 commit 06431e5

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ phonopy = [
5555
"phonopy==3.3.0",
5656
"spglib==2.7.0",
5757
]
58+
mp-api = ["mp-api==0.37.2"]
5859

5960
[tool.ruff]
6061
exclude = [".ci_support", "tests", "setup.py", "_version.py"]

src/structuretoolkit/build/materialsproject.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from typing import Any
1+
from typing import Any, Iterable
22
from collections.abc import Generator
33
from ase.atoms import Atoms
44
from structuretoolkit.common.pymatgen import pymatgen_to_ase
55

66

77
def search(
8-
chemsys: str | list[str], api_key=None, **kwargs
9-
) -> Generator[dict[str, Any]]:
8+
chemsys: str | list[str], fields: Iterable[str] = (), api_key=None, **kwargs
9+
) -> Generator[dict[str, Any], None, None]:
1010
"""
1111
Search the database for all structures matching the given query.
1212
@@ -19,20 +19,13 @@ def search(
1919
2020
Search for all iron structures:
2121
22-
>>> pr = Project(...)
23-
>>> irons = pr.create.structure.materialsproject.search("Fe")
24-
>>> irons.number_of_structures
22+
>>> irons = structuretoolkit.build.materialsproject.search("Fe")
23+
>>> len(irons)
2524
10
2625
27-
The returned :class:`~.MPQueryResults` object implements :class:`~.HasStructure` and can be accessed with the
28-
material ids as a short-hand
29-
30-
>>> irons.get_structure(1) == irons.get_structure('mp-13')
31-
True
32-
3326
Search for all structures with Al, Li that are on the T=0 convex hull:
3427
35-
>>> alli = pr.create.structure.materialsproject.search(['Al', 'Li', 'Al-Li'], is_stable=True)
28+
>>> alli = structuretoolkit.build.materialsproject.search(['Al', 'Li', 'Al-Li'], is_stable=True)
3629
>>> len(alli)
3730
6
3831
@@ -44,12 +37,17 @@ def search(
4437
4538
Args:
4639
chemsys (str, list of str): confine search to given elements; either an element symbol or multiple element
47-
symbols seperated by dashes; if a list of strings is given return structures matching either of them
40+
symbols seperated by dashes; if a list of strings is given return structures matching either of them
41+
fields (iterable of str): pass as `fields` to :meth:`mp_api.MPRester.summary.search` to request additional
42+
database entries beyond the structure
4843
api_key (str, optional): if your API key is not exported in the environment flag MP_API_KEY, pass it here
4944
**kwargs: passed verbatim to :meth:`mp_api.MPRester.summary.search` to further filter the results
5045
5146
Returns:
52-
:class:`~.MPQueryResults`: resulting structures from the query
47+
list of dict: one dictionary for each search results with at least keys
48+
'material_id': database key of the hit
49+
'structure': ASE atoms object
50+
plus any requested via `fields`.
5351
"""
5452
from mp_api.client import MPRester
5553

@@ -61,12 +59,12 @@ def search(
6159
rest_kwargs["api_key"] = api_key
6260
with MPRester(**rest_kwargs) as mpr:
6361
results = mpr.summary.search(
64-
chemsys=chemsys, **kwargs, fields=["structure", "material_id"]
62+
chemsys=chemsys, **kwargs, fields=list(fields) + ["structure", "material_id"]
6563
)
6664
for r in results:
6765
if "structure" in r:
6866
r["structure"] = pymatgen_to_ase(r["structure"])
69-
yield r
67+
yield r
7068

7169

7270
def by_id(
@@ -80,8 +78,7 @@ def by_id(
8078
8179
This is how you would ask for the iron ground state:
8280
83-
>>> pr = Project(...)
84-
>>> pr.create.structure.materialsproject.by_id('mp-13')
81+
>>> structuretoolkit.build.materialsproject.by_id('mp-13')
8582
Fe: [0. 0. 0.]
8683
tags:
8784
spin: [(0: 2.214)]

0 commit comments

Comments
 (0)