-
Notifications
You must be signed in to change notification settings - Fork 1
Add mp-api interface #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jan-janssen
wants to merge
17
commits into
main
Choose a base branch
from
mp-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
17439d1
Add mp-api interface
jan-janssen 1dbfa98
Drop namespace class
pmrv 98d0a21
Add function imports to init
pmrv bb53de1
Fix typing import
pmrv 06431e5
Fix deps, typehints, comments add `fields` argument
pmrv 588be25
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d175557
fix len
pmrv 6cc4663
fix: apply CodeRabbit auto-fixes
coderabbitai[bot] b8a6f41
Format black
pyiron-runner 742681d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 08c2a0f
Change versioning from 0.1.dev1 to 0.0.1
jan-janssen fe07fb1
Add tests for materialsproject
pmrv a36f061
Add pymatgen to mp-api opt deps
pmrv d3b9125
Skip materialsproject tests when mp-api and pymatgen are not installed
pmrv 211a776
Convert pytest skipif to stdlib unittest in materialsproject tests
pmrv 663154d
Fix materialsproject tests to skip cleanly when dependencies missing
pmrv ed40b63
Fail if either dep is missing
pmrv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,4 @@ dependencies: | |
| - sqsgenerator =0.5.4 | ||
| - hatchling =1.29.0 | ||
| - hatch-vcs =0.5.0 | ||
| - mp-api =0.37.2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| from typing import Any, Iterable | ||
| from collections.abc import Generator | ||
| from ase.atoms import Atoms | ||
| from structuretoolkit.common.pymatgen import pymatgen_to_ase | ||
|
|
||
|
|
||
| def search( | ||
| chemsys: str | list[str], fields: Iterable[str] = (), api_key=None, **kwargs | ||
| ) -> Generator[dict[str, Any], None, None]: | ||
| """ | ||
| Search the database for all structures matching the given query. | ||
|
|
||
| Note that `chemsys` takes distinct values for unaries, binaries and so! A query with `chemsys=["Fe", "O"]` will | ||
| return iron and oxygen structures but not iron oxide. Similarly `chemsys=["Fe-O"]` will | ||
| not return unary structures. | ||
|
|
||
| All keyword arguments for filtering from the original API are supported. See the | ||
| `original docs <https://docs.materialsproject.org/downloading-data/using-the-api>`_ for them. | ||
|
|
||
| Search for all iron structures: | ||
|
|
||
| >>> irons = structuretoolkit.build.materialsproject.search("Fe") | ||
| >>> len(list(irons)) | ||
| 10 | ||
|
|
||
| Search for all structures with Al, Li that are on the T=0 convex hull: | ||
|
|
||
| >>> alli = structuretoolkit.build.materialsproject.search(['Al', 'Li', 'Al-Li'], is_stable=True) | ||
| >>> len(list(alli)) | ||
| 6 | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Usage is only possible with an API key obtained from the Materials Project. To do this, create an account with | ||
| them, login and access `this webpage <https://next-gen.materialsproject.org/api#api-key>`. | ||
|
|
||
| Once you have a key, either pass it as the `api_key` parameter or export an | ||
| environment variable, called `MP_API_KEY`, in your shell setup. | ||
|
|
||
| Args: | ||
| chemsys (str, list of str): confine search to given elements; either an element symbol or multiple element | ||
| symbols separated by dashes; if a list of strings is given return structures matching either of them | ||
| fields (iterable of str): pass as `fields` to :meth:`mp_api.MPRester.summary.search` to request additional | ||
| database entries beyond the structure | ||
| api_key (str, optional): if your API key is not exported in the environment flag MP_API_KEY, pass it here | ||
| **kwargs: passed verbatim to :meth:`mp_api.MPRester.summary.search` to further filter the results | ||
|
|
||
| Returns: | ||
| list of dict: one dictionary for each search results with at least keys | ||
| 'material_id': database key of the hit | ||
| 'structure': ASE atoms object | ||
| plus any requested via `fields`. | ||
| """ | ||
| from mp_api.client import MPRester | ||
|
|
||
| rest_kwargs = { | ||
| "use_document_model": False, # returns results as dictionaries | ||
| "include_user_agent": True, # send some additional software version info to MP | ||
| } | ||
| if api_key is not None: | ||
| rest_kwargs["api_key"] = api_key | ||
| with MPRester(**rest_kwargs) as mpr: | ||
| results = mpr.summary.search( | ||
| chemsys=chemsys, | ||
| **kwargs, | ||
| fields=list(fields) + ["structure", "material_id"], | ||
| ) | ||
|
Comment on lines
+61
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent At Line 63–65, callers can pass Proposed fix- with MPRester(**rest_kwargs) as mpr:
- results = mpr.summary.search(
- chemsys=chemsys, **kwargs, fields=["structure", "material_id"]
- )
+ query_kwargs = dict(kwargs)
+ query_kwargs.pop("fields", None)
+ with MPRester(**rest_kwargs) as mpr:
+ results = mpr.summary.search(
+ chemsys=chemsys, fields=["structure", "material_id"], **query_kwargs
+ )🤖 Prompt for AI Agents |
||
| for r in results: | ||
| if "structure" in r: | ||
| r["structure"] = pymatgen_to_ase(r["structure"]) | ||
| yield r | ||
|
|
||
|
|
||
| def by_id( | ||
| material_id: str | int, | ||
| final: bool = True, | ||
| conventional_unit_cell: bool = False, | ||
| api_key=None, | ||
| ) -> Atoms | list[Atoms]: | ||
| """ | ||
| Retrieve a structure by material id. | ||
|
|
||
| This is how you would ask for the iron ground state: | ||
|
|
||
| >>> structuretoolkit.build.materialsproject.by_id('mp-13') | ||
| Fe: [0. 0. 0.] | ||
| tags: | ||
| spin: [(0: 2.214)] | ||
| pbc: [ True True True] | ||
| cell: | ||
| Cell([[2.318956, 0.000185, -0.819712], [-1.159251, 2.008215, -0.819524], [2.5e-05, 0.000273, 2.459206]]) | ||
|
|
||
| Usage is only possible with an API key obtained from the Materials Project. To do this, create an account with | ||
| them, login and access `this webpage <https://next-gen.materialsproject.org/api#api-key>`. | ||
|
|
||
| Once you have a key, either pass it as the `api_key` parameter or export an | ||
| environment variable, called `MP_API_KEY`, in your shell setup. | ||
|
|
||
| Args: | ||
| material_id (str): the id assigned to a structure by the materials project | ||
| api_key (str, optional): if your API key is not exported in the environment flag MP_API_KEY, pass it here | ||
| final (bool, optional): if set to False, returns the list of initial structures, | ||
| else returns the final structure. (Default is True) | ||
| conventional_unit_cell (bool, optional): if set to True, returns the standard conventional unit cell. | ||
| (Default is False) | ||
|
|
||
| Returns: | ||
| :class:`~.Atoms`: requested final structure if final is True | ||
| list of :class:~.Atoms`: a list of initial (pre-relaxation) structures if final is False | ||
|
|
||
| Raises: | ||
| ValueError: material id does not exist | ||
| """ | ||
| from mp_api.client import MPRester | ||
|
|
||
| rest_kwargs = { | ||
| "include_user_agent": True, # send some additional software version info to MP | ||
| } | ||
| if api_key is not None: | ||
| rest_kwargs["api_key"] = api_key | ||
| with MPRester(**rest_kwargs) as mpr: | ||
| if final: | ||
| return pymatgen_to_ase( | ||
| mpr.get_structure_by_material_id( | ||
| material_id=material_id, | ||
| final=final, | ||
| conventional_unit_cell=conventional_unit_cell, | ||
| ) | ||
| ) | ||
| else: | ||
| return [ | ||
| pymatgen_to_ase(mpr_structure) | ||
| for mpr_structure in ( | ||
| mpr.get_structure_by_material_id( | ||
| material_id=material_id, | ||
| final=final, | ||
| conventional_unit_cell=conventional_unit_cell, | ||
| ) | ||
| ) | ||
| ] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.