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”}’