Skip to content

Commit e0193f5

Browse files
committed
Fixes issue #438 in web
1 parent 56af63e commit e0193f5

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

crystal_toolkit/components/search.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pymatgen.core.composition import CompositionError
1111
from pymatgen.util.string import unicodeify
1212
from mp_api import MPRester
13+
from mp_api.client import MPRestError
1314

1415

1516
from pymatgen.util.string import unicodeify_spacegroup
@@ -127,44 +128,42 @@ def get_human_readable_results_from_search_term(search_term):
127128

128129
with MPRester() as mpr:
129130
try:
130-
entries = mpr.search.search(
131-
search_term,
131+
entries = mpr.summary.search_summary_docs(
132+
formula=search_term,
132133
fields=[
133-
"task_id",
134+
"material_id",
134135
"formula_pretty",
135-
"e_above_hull",
136+
"energy_above_hull",
136137
"symmetry",
137138
],
138139
)
139-
except CompositionError:
140+
except MPRestError:
140141
entries = []
141142

142143
if len(entries) == 0:
143144
self.logger.info(f"Search: no results for {search_term}")
144145
return {"error": f"No results found for {search_term}."}
145146

146-
# sort by e_above_hull if a normal query, or by Levenshtein distance
147-
# if fuzzy matching (order of mpids list if present matches Levenshtein distance)
148-
if not mpids:
149-
entries = sorted(entries, key=lambda x: x["e_above_hull"])
150-
else:
151-
entries = sorted(entries, key=lambda x: mpids.index(x["task_id"]))
147+
entries = sorted(entries, key=lambda x: x.energy_above_hull)
152148

149+
human_readable_hull_labels = []
153150
for entry in entries:
154-
e_hull = entry["e_above_hull"]
151+
e_hull = entry.energy_above_hull
155152
if e_hull == 0:
156-
entry["e_above_hull_human"] = "predicted stable phase"
153+
human_readable_hull_labels.append("predicted stable phase")
157154
elif e_hull >= 0.01:
158-
entry["e_above_hull_human"] = f"+{e_hull:.2f} eV/atom"
155+
human_readable_hull_labels.append(f"+{e_hull:.2f} eV/atom")
159156
else:
160157
e_hull_str = np.format_float_scientific(e_hull, precision=2)
161-
entry["e_above_hull_human"] = f"+{e_hull_str} eV/atom"
158+
human_readable_hull_labels.append(f"+{e_hull_str} eV/atom")
162159

163160
human_readable_results = {
164-
entry["task_id"]: f"{unicodeify(entry['pretty_formula'])} "
165-
f"({unicodeify_spacegroup(entry['spacegroup.symbol'])}) "
166-
f"{entry['e_above_hull_human']}"
167-
for entry in entries
161+
entry.material_id: f"{unicodeify(entry.formula_pretty)} "
162+
f"({unicodeify_spacegroup(entry.symmetry.symbol)}) "
163+
f"{human_readable_hull_label}"
164+
for entry, human_readable_hull_label in zip(
165+
entries, human_readable_hull_labels
166+
)
168167
}
169168

170169
return human_readable_results

0 commit comments

Comments
 (0)