Skip to content

Commit

Permalink
Transition wildcard chemsys queries to comp reduced (#909)
Browse files Browse the repository at this point in the history
* Transition wilcard chemsys queries to comp reduced

* Linting

* Update tests

* Fix util test

* Linting

* Fix tests

* More test fixes
  • Loading branch information
munrojm committed Dec 5, 2023
1 parent a1334ad commit 2e2ae4d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions emmet-api/emmet/api/routes/materials/materials/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ def chemsys_to_criteria(chemsys: str) -> Dict:
eles = chemsys_list[0].split("-")

crit["nelements"] = len(eles)
crit["elements"] = {"$all": [ele for ele in eles if ele != "*"]}

if crit["elements"]["$all"] == []:
del crit["elements"]
for el in eles:
if el != "*":
crit[f"composition_reduced.{el}"] = {"$exists": True}

return crit
else:
Expand Down
7 changes: 4 additions & 3 deletions emmet-api/emmet/api/routes/materials/summary/hint_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def generate_hints(self, query):
hints["agg_hint"] = hints["count_hint"]

if list(query.get("criteria").keys()) != ["deprecated", "builder_meta.license"]:

pure_params = []
excluded_elements = False
for param, val in query["criteria"].items():
Expand All @@ -20,14 +19,16 @@ def generate_hints(self, query):
if pure_param == "composition_reduced" and val == {"$exists": False}:
excluded_elements = True


if "has_props" in pure_params:
hints["count_hint"] = {"has_props.$**": 1}
elif "composition_reduced" in pure_params and not excluded_elements:
hints["count_hint"] = {"composition_reduced.$**": 1}
else:
for param in query["criteria"]:
if param not in ["deprecated", "builder_meta.license"] and "composition_reduced" not in param:
if (
param not in ["deprecated", "builder_meta.license"]
and "composition_reduced" not in param
):
hints["count_hint"] = {
"deprecated": 1,
"builder_meta.license": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_chemsys_query():
assert op.query("Si-O") == {"criteria": {"chemsys": "O-Si"}}

assert op.query("Si-*") == {
"criteria": {"nelements": 2, "elements": {"$all": ["Si"]}}
"criteria": {"nelements": 2, "composition_reduced.Si": {"$exists": True}}
}

with ScratchDir("."):
Expand Down
5 changes: 4 additions & 1 deletion emmet-api/tests/materials/materials/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def test_formula_to_criteria():
def test_chemsys_to_criteria():
# Chemsys
assert chemsys_to_criteria("Si-O") == {"chemsys": "O-Si"}
assert chemsys_to_criteria("Si-*") == {"elements": {"$all": ["Si"]}, "nelements": 2}
assert chemsys_to_criteria("Si-*") == {
"composition_reduced.Si": {"$exists": True},
"nelements": 2,
}
assert chemsys_to_criteria("*-*-*") == {"nelements": 3}

assert chemsys_to_criteria("Si-O, P-Li-Fe") == {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_chemsys_query():
assert op.query("O-C") == {"criteria": {"chemsys": "C-O"}}

assert op.query("C-*") == {
"criteria": {"nelements": 2, "elements": {"$all": ["C"]}}
"criteria": {"nelements": 2, "composition_reduced.C": {"$exists": True}}
}

with ScratchDir("."):
Expand Down

0 comments on commit 2e2ae4d

Please sign in to comment.