Hi all,
I am teaching an Integrated Computational Materials Science summer school where we are doing a Jupyter notebook on Pymatgen. This is being hosted at the University of Michigan.
Last Thursday, we ran our code as a class and it worked well. Today we are running the same code, without changing anything, and about 70% of the class cannot run the MP Query. We are getting a 403 error. This is using the Legacy MP API. Can someone help us investigate?
Here is the example code:
from pymatgen.ext.matproj import MPRester
MPR = MPRester(your_API_key)
criteria = {âelementsâ:{â$inâ:[âLiâ, âNaâ, âKâ], â$allâ: [âOâ]}, # All compounds contain O, and must have Li or Na or K
ânelementsâ:3, # Each compound should be ternary (3 elements in the composition)
âanonymous_formulaâ: {âAâ: 1, âBâ: 1, âCâ: 3}, # Each compound should have stoichiometry matching ABC3 stoichiometry (Perovskite li)
âicsd_idsâ: {â$gteâ: 0}, # Must have a non-zero ICSD ID (no hypothetical compounds)
âe_above_hullâ: {â$lteâ: 0.01}, # Energy of metastability below 10 meV/atom.
âband_gapâ: {â$gtâ: 1} # DFT-calculated Band Gap is greater than 1
}# The properties and the criteria use MaterialsProject features # You can see what is queryable from the MP API documentation: # https://github.com/materialsproject/mapidoc/tree/master/materials # The criteria uses mongodb query language. See here # for more details: https://docs.mongodb.com/manual/reference/operator/query/
props = [âmaterial_idâ,âpretty_formulaâ,âe_above_hullâ,âband_gapâ,âstructureâ, âband_structureâ]
entries = MPR.query(criteria=criteria, properties=props)
print(len(entries))
for e in entries:
print(e[âpretty_formulaâ],e[âband_gapâ])
#print(e)
Here is our error output:
MPRestError Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\pymatgen\ext\matproj.py:253, in MPRester._make_request(self, sub_url, payload, method, mp_decode)
251 raise MPRestError(data[âerrorâ])
â 253 raise MPRestError(f"REST query returned with error status code {response.status_code}")
255 except Exception as ex:MPRestError: REST query returned with error status code 403
During handling of the above exception, another exception occurred:
MPRestError Traceback (most recent call last)
Input In [3], in <cell line: 44>()
40 json.dump(new_entries, f)
41 return entries
â> 44 entries=get_bs_entries()
46 print(len(entries))Input In [3], in get_bs_entries()
24 props = [âstructureâ, âmaterial_idâ,âpretty_formulaâ,âe_above_hullâ,âwarningsâ,âband_gapâ,âband_structureâ]
25 #The properties and the criteria use MaterialsProject features
26 #You can see what is queryable from the MP API documentation: mapidoc/materials at master ¡ materialsproject/mapidoc ¡ GitHub
â> 28 entries = MPR.query(criteria=criteria, properties=props)
29 print(len(entries))
31 #Save files are prepared in a âJSONâ file.
32 #Some MP objects are not JSONable, and so they must be turned into a dictionary before they can be saved.File ~\anaconda3\lib\site-packages\pymatgen\ext\matproj.py:994, in MPRester.query(self, criteria, properties, chunk_size, max_tries_per_chunk, mp_decode)
992 count_payload = payload.copy()
993 count_payload[âoptionsâ] = json.dumps({âcount_onlyâ: True})
â 994 num_results = self._make_request(â/queryâ, payload=count_payload, method=âPOSTâ)
995 if num_results <= chunk_size:
996 return self._make_request(â/queryâ, payload=payload, method=âPOSTâ, mp_decode=mp_decode)File ~\anaconda3\lib\site-packages\pymatgen\ext\matproj.py:257, in MPRester._make_request(self, sub_url, payload, method, mp_decode)
255 except Exception as ex:
256 msg = f"{str(ex)}. Content: {response.content}" if hasattr(response, âcontentâ) else str(ex)
â 257 raise MPRestError(msg)MPRestError: REST query returned with error status code 403. Content: bâ{âvalid_responseâ: false, âerrorâ: âInvalid request method.â, âversionâ: {âdbâ: â2020_09_08â, âpymatgenâ: â2022.0.8â, ârestâ: â2.0â}, âcreated_atâ: â2023-06-19T11:59:37.467667â}â