Legacy API for downloading mateerial structures from MP Legacy site, not up-to-date MP

I would like to know how we can set up MP API to download materials containing Yb atom from Legacy site.

For our machine learning project, we need material structures and magnetic ordering. I could use mp_api to access the whole material data in the up-to-date MP database. However, I realized there are no longer materials containing Yb, while we could find them in MP Legacy site (e.g. mp-1104517). Does anybody know why we cannot find materials with Yb in the up-to-date MP? Does it mean the Yb-contained material data is no longer reliable?

Also, I would like to know how we can use Legacy API, which I have not used before.
Do I need to install older version of mp_api, and need an API key that is different from the New API? Also hope to know if thereā€™s any example code to download a large number of materials from MP legacy site.

FYI: I saw thereā€™s an archive repo for legacy usage.

I first tried one section, but I had trouble accessing MP legacy.

from pymatgen import MPRester
m = MPRester()
data = m.query(criteria={"task_id": "mp-1234"}, properties=["final_energy"])
print(data)

Then, I got an error:

ImportError: cannot import name ā€˜MPResterā€™ from ā€˜pymatgenā€™ (unknown location)

, suggesting recent pymatgen does not work with the legacy version

Below is another code block I have tried based on the repo:

import json
import requests
api_legacy = 'my_legacy_api'

data = {
    'criteria': {
        'elements': {'$in': ['Li', 'Na', 'K'], '$all': ['O']},
        'nelements': 2,
    },
    'properties': [
        'formula',
        'formation_energy_per_atom',
    ]
}
r = requests.post('https://materialsproject.org/rest/v2/query',
                 headers={'X-API-KEY': api_legacy},
                 data={k: json.dumps(v) for k,v in data.items()})
response_content = r.json() # a dict

print('response_content:', response_content)

ā€¦ then I got:

response_content: {ā€˜errorā€™: ā€œYour user agent is blocked. Please use our mp-api client (mp-api Ā· PyPI) and donā€™t scrape our website.ā€, ā€˜versionā€™: ā€˜blockedā€™}

Thanks!

Best regards,

@Ryotaro_Okabe Iā€™d advise against falling back on the legacy API. The Yb compounds will come back with our imminent next database release. @Aaron_Kaplan and @tsmathis should be able to provide details if needed.

@Ryotaro_Okabe : agree completely with @tschaume that itā€™s best to wait until the Yb docs are updated. Just to build on his answer a bit, if you really do need the Yb compounds now (say to prototype a method or for backwards compatibility with previous models), you can always access deprecated materials using the API:

from mp_api.client import MPRester

with MPRester("your_api_key_goes_here") as mpr:
    summary_docs = mpr.materials.summary.search(elements=["Yb"],deprecated=True)

If you need finer-grained information, like the forces/stresses corresponding to these materials, you can pull them directly from the tasks endpoint:


yb_task_ids = {}
for doc in summary_docs:
    for prop in doc.origins:
        if prop.name == "structure":
            yb_task_ids[doc.material_id] = prop.task_id
            break

with MPRester("your_api_key_goes_here") as mpr:
    yb_tasks = mpr.materials.tasks.search(task_ids = list(yb_task_ids.values())) 
1 Like

@tschaume @Aaron_Kaplan
Thank you for explanation of the API, and the statement about the Yb materials. I could run the API to get the deprecated materials containing Yb, but will consider carefully if I use the data in the end.

Best regards,
Ryotaro