I need help trying to submit a query with the Materials Project API for materials that have B, and atleast one of these elements: Mn, Co, Fe, Cr.
I would recommend doing four different queries which pass pairs of elements to the elements
field.
For example, to get the IDs of materials that contain at least B
and Mn
you could do:
from mp_api import MPRester
with MPRester("your_api_key") as mpr:
docs = mpr.materials.search(elements=["B", "Mn"], fields=["material_id"])
mpids = [doc.material_id for doc in docs]
Okay so I tried to do what you suggested and included exclude_elements field to remove oxides, phos any materials containing , and each metal so that I am not getting duplicates in each query. However I am noticing that it’s not really working for example when I ran this:
with MPRester(MY_API_KEY) as mpr:
docsBFe = mpr.summary.search(elements=[“B”,“Fe”], exclude_elements=[“O”,“P”]
,fields=[“material_id”, “formula_pretty”, “cif”, “spacegroup_number”,
“crystal_system”])
print(docsBFe[0:2])
it found 907 documents, but from looking at the first entry in the list, it is mp-1207649 with the formula_pretty being YFe3(BO3)4 which contains O in it.
Also when I excluded Mn, Cr, Co for the Fe & B query above to avoid finding redundant elements in my other queries like this:
with MPRester(JOHAN_API_KEY) as mpr:
docsBFe = mpr.summary.search(elements=[“B”,“Fe”], exclude_elements=[“O”,“P”,“Mn”,“Cr”,“Co”]
,fields=[“material_id”, “formula_pretty”, “cif”, “spacegroup_number”,
“crystal_system”])
print(docsBFe[0:2])
I suprisingly obtained more documents (2933) which intuitively doesn’t make sense to me because I am adding more restrictions.
thanks for all the help btw, I really appreciate it.
Hi @J0chin, thanks for bringing this up. This could be a bug in how exclude_elements
is working. I am investigating now…
– Jason
@J0chin, this is fixed as of mp-api
v0.24.7
. The client tries to multithread requests to our server, and there was an issue with how it was using exclude_elements
to perform that parallelization. I have confirmed your examples work now.
– Jason
Okay ya it’s also working for me too. Thanks again!
Hi, I have a question. How would I submit the query for materials that has only for example “Pb” and “S”?
hi, this example gives me “Pb”, “S” and other elements which has any letter containing “p”,“b”, “s”. However, I only want to grab “Pb” and “S” or at least one of them
@Nadia_Mehjabin here’s a specific example
import os
from mp_api.client import MPRester
from emmet.core.utils import jsanitize
with MPRester("your-api-key-here") as mpr:
docs = mpr.summary.search(
chemsys="Pb-S", fields=["material_id", "formula_pretty", "chemsys"]
)
sanitized_docs = [
{k: v for k, v in doc.items() if k != "fields_not_requested"}
for doc in jsanitize(docs)
]
The resulting sanitized_docs
contains 22 results for this query:
[{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-727323'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1078500'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-2350391'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1078944'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1091375'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1079543'},
{'formula_pretty': 'PbS2', 'chemsys': 'Pb-S', 'material_id': 'mp-1025039'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1078696'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1018115'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-726184'},
{'formula_pretty': 'PbS2', 'chemsys': 'Pb-S', 'material_id': 'mp-1206683'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-21276'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1012435'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1079101'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1057015'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1087486'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-557719'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-561320'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1018019'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-21039'},
{'formula_pretty': 'Pb3S', 'chemsys': 'Pb-S', 'material_id': 'mp-1186425'},
{'formula_pretty': 'PbS', 'chemsys': 'Pb-S', 'material_id': 'mp-1009551'}]
Thanks! I ran just the code and it works. I am just having trouble to use it with the class that I have. It does not have attribute chemsys. So if I can fix that I am sure it will work out. Anyway thnx again.
Thread closed due to inactivity, please open a new thread to address related issues.