-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dielectric, Piezoelectric, Magnetism, Bonds, and Phase Diagrams (#420)
* Emmet bump * Update dielectric, piezoelectric, magnetism * task_id to material_id in clients * Fix collection names * Remove sub doc fields from client tests * Ignore some endpoints in tests temp * Temp xfail on endpoints * Bump emmet * Add bonds endpoint * Bump emmet * Phase diagram endpoint added to thermo * Temp xfail on thermo pd client method * Linting * Alter materials and thermo collection names * Fix tests * Linting * Fix robocrys test * Update robocrys test * Update database names * Fix missing fstrings * Remove stray print * Temp xfail on find structure test
- Loading branch information
Jason Munro
authored
Nov 10, 2021
1 parent
fcb94a4
commit 113557a
Showing
34 changed files
with
559 additions
and
254 deletions.
There are no files selected for viewing
This file contains 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 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
Empty file.
This file contains 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,105 @@ | ||
from typing import List, Optional, Tuple | ||
from collections import defaultdict | ||
|
||
from mp_api.core.client import BaseRester | ||
from emmet.core.bonds import BondingDoc | ||
|
||
|
||
class BondsRester(BaseRester[BondingDoc]): | ||
|
||
suffix = "bonds" | ||
document_model = BondingDoc # type: ignore | ||
primary_key = "material_id" | ||
|
||
def search_bonds_docs( | ||
self, | ||
max_bond_length: Optional[Tuple[float, float]] = None, | ||
min_bond_length: Optional[Tuple[float, float]] = None, | ||
mean_bond_length: Optional[Tuple[float, float]] = None, | ||
coordination_envs: Optional[List[str]] = None, | ||
coordination_envs_anonymous: Optional[List[str]] = None, | ||
sort_field: Optional[str] = None, | ||
ascending: Optional[bool] = None, | ||
num_chunks: Optional[int] = None, | ||
chunk_size: int = 1000, | ||
all_fields: bool = True, | ||
fields: Optional[List[str]] = None, | ||
): | ||
""" | ||
Query dielectric docs using a variety of search criteria. | ||
Arguments: | ||
max_bond_length (Tuple[float,float]): Minimum and maximum value for the maximum bond length | ||
in the structure to consider. | ||
min_bond_length (Tuple[float,float]): Minimum and maximum value for the minimum bond length | ||
in the structure to consider. | ||
mean_bond_length (Tuple[float,float]): Minimum and maximum value for the mean bond length | ||
in the structure to consider. | ||
coordination_envs (List[str]): List of coordination environments to consider (e.g. ['Mo-S(6)', 'S-Mo(3)']). | ||
coordination_envs_anonymous (List[str]): List of anonymous coordination environments to consider | ||
(e.g. ['A-B(6)', 'A-B(3)']). | ||
sort_field (str): Field used to sort results. | ||
ascending (bool): Whether sorting should be in ascending order. | ||
num_chunks (int): Maximum number of chunks of data to yield. None will yield all possible. | ||
chunk_size (int): Number of data entries per chunk. | ||
all_fields (bool): Whether to return all fields in the document. Defaults to True. | ||
fields (List[str]): List of fields in DielectricDoc to return data for. | ||
Default is material_id and last_updated if all_fields is False. | ||
Returns: | ||
([BondingDoc]) List of bonding documents. | ||
""" | ||
|
||
query_params = defaultdict(dict) # type: dict | ||
|
||
if max_bond_length: | ||
query_params.update( | ||
{ | ||
"max_bond_length_min": max_bond_length[0], | ||
"max_bond_length_max": max_bond_length[1], | ||
} | ||
) | ||
|
||
if min_bond_length: | ||
query_params.update( | ||
{ | ||
"min_bond_length_min": min_bond_length[0], | ||
"min_bond_length_max": min_bond_length[1], | ||
} | ||
) | ||
|
||
if mean_bond_length: | ||
query_params.update( | ||
{ | ||
"mean_bond_length_min": mean_bond_length[0], | ||
"mean_bond_length_max": mean_bond_length[1], | ||
} | ||
) | ||
|
||
if coordination_envs is not None: | ||
query_params.update({"coordination_envs": ",".join(coordination_envs)}) | ||
|
||
if coordination_envs_anonymous is not None: | ||
query_params.update( | ||
{"coordination_envs_anonymous": ",".join(coordination_envs_anonymous)} | ||
) | ||
|
||
if sort_field: | ||
query_params.update({"sort_field": sort_field}) | ||
|
||
if ascending is not None: | ||
query_params.update({"ascending": ascending}) | ||
|
||
query_params = { | ||
entry: query_params[entry] | ||
for entry in query_params | ||
if query_params[entry] is not None | ||
} | ||
|
||
return super().search( | ||
num_chunks=num_chunks, | ||
chunk_size=chunk_size, | ||
all_fields=all_fields, | ||
fields=fields, | ||
**query_params | ||
) |
This file contains 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,102 @@ | ||
from typing import Optional | ||
from fastapi import Query | ||
from maggma.api.query_operator import QueryOperator | ||
from maggma.api.utils import STORE_PARAMS | ||
|
||
from collections import defaultdict | ||
|
||
|
||
class BondLengthQuery(QueryOperator): | ||
""" | ||
Method to generate a query on bond length data. | ||
""" | ||
|
||
def query( | ||
self, | ||
max_bond_length_max: Optional[float] = Query( | ||
None, | ||
description="Maximum value for the maximum bond length in the structure.", | ||
), | ||
max_bond_length_min: Optional[float] = Query( | ||
None, | ||
description="Minimum value for the maximum bond length in the structure.", | ||
), | ||
min_bond_length_max: Optional[float] = Query( | ||
None, | ||
description="Maximum value for the minimum bond length in the structure.", | ||
), | ||
min_bond_length_min: Optional[float] = Query( | ||
None, | ||
description="Minimum value for the minimum bond length in the structure.", | ||
), | ||
mean_bond_length_max: Optional[float] = Query( | ||
None, | ||
description="Maximum value for the mean bond length in the structure.", | ||
), | ||
mean_bond_length_min: Optional[float] = Query( | ||
None, | ||
description="Minimum value for the mean bond length in the structure.", | ||
), | ||
) -> STORE_PARAMS: | ||
|
||
crit = defaultdict(dict) # type: dict | ||
|
||
d = { | ||
"bond_length_stats.max": [max_bond_length_min, max_bond_length_max], | ||
"bond_length_stats.min": [min_bond_length_min, min_bond_length_max], | ||
"bond_length_stats.mean": [mean_bond_length_min, mean_bond_length_max], | ||
} | ||
|
||
for entry in d: | ||
if d[entry][0] is not None: | ||
crit[entry]["$gte"] = d[entry][0] | ||
|
||
if d[entry][1] is not None: | ||
crit[entry]["$lte"] = d[entry][1] | ||
|
||
return {"criteria": crit} | ||
|
||
def ensure_indexes(self): # pragma: no cover | ||
keys = [ | ||
"bond_length_stats.max", | ||
"bond_length_stats.min", | ||
"bond_length_stats.mean", | ||
] | ||
return [(key, False) for key in keys] | ||
|
||
|
||
class CoordinationEnvsQuery(QueryOperator): | ||
""" | ||
Method to generate a query on coordination environment data. | ||
""" | ||
|
||
def query( | ||
self, | ||
coordination_envs: Optional[str] = Query( | ||
None, | ||
description="Query by coordination environments in the material composition as a comma-separated list\ | ||
(e.g. 'Mo-S(6),S-Mo(3)')", | ||
), | ||
coordination_envs_anonymous: Optional[str] = Query( | ||
None, | ||
description="Query by anonymous coordination environments in the material composition as a comma-separated\ | ||
list (e.g. 'A-B(6),A-B(3)')", | ||
), | ||
) -> STORE_PARAMS: | ||
|
||
crit = {} # type: dict | ||
|
||
if coordination_envs: | ||
env_list = [env.strip() for env in coordination_envs.split(",")] | ||
crit["coordination_envs"] = {"$all": [str(env) for env in env_list]} | ||
|
||
if coordination_envs_anonymous: | ||
env_list = [env.strip() for env in coordination_envs_anonymous.split(",")] | ||
crit["coordination_envs_anonymous"] = { | ||
"$all": [str(env) for env in env_list] | ||
} | ||
|
||
return {"criteria": crit} | ||
|
||
def ensure_indexes(self): # pragma: no cover | ||
return [("coordination_envs", False), ("coordination_envs_anonymous", False)] |
This file contains 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,26 @@ | ||
from maggma.api.resource import ReadOnlyResource | ||
from emmet.core.bonds import BondingDoc | ||
|
||
from maggma.api.query_operator import PaginationQuery, SortQuery, SparseFieldsQuery | ||
|
||
from mp_api.routes.bonds.query_operators import BondLengthQuery, CoordinationEnvsQuery | ||
|
||
|
||
def bonds_resource(bonds_store): | ||
resource = ReadOnlyResource( | ||
bonds_store, | ||
BondingDoc, | ||
query_operators=[ | ||
BondLengthQuery(), | ||
CoordinationEnvsQuery(), | ||
SortQuery(), | ||
PaginationQuery(), | ||
SparseFieldsQuery( | ||
BondingDoc, default_fields=["material_id", "last_updated"], | ||
), | ||
], | ||
tags=["Bonds"], | ||
disable_validation=True, | ||
) | ||
|
||
return resource |
Oops, something went wrong.