Loading optimade structures using python-tools

Dear Forum,

I am new to OPTIMADE and I am trying to find the way of querying a specific database and work with the JSON files that are obtained.

The way I am doing it now is by using some external tools

import urllib.request as request
import json

with request.urlopen('https://www.crystallography.net/tcod/optimade/v1/structures?flter=elements+HAS+ANY+"C","Si","Ge","Sn","Pb"') as response:
    source = response.read()
    data = json.loads(source)

From here I get the data object which is a dictionary with 3 keys ‘data’, ‘links’, and ‘meta’. The ‘data’ key is a list with len 10, each element being a dictionary with its ‘attributes’, ect. And the ‘links’ key correctly defines the page limit and offset. The ‘meta’ key is also consistent with the example of the article.

This is the way I found, but I can’t get my head around the optimade python tools themselves. How can I, for example, do this same query using these tools? For instance, to get a set of StructureResource objects, to then obtain the CIF files via the built in adapters? The OptimadeRester from pymatgen also does not use the python-tools, it gets the json files from a specific query and treats the data accordingly to get a pymatgen Structure object.

I am getting something wrong here? I am just searching for a simple way to get the query response from the server and to be able to handle it using the tools provided.

Best regards,

Oier.

Hi @Oier_Arcelus, thanks for the question!

optimade-python-tools is a library primarily designed for people to create their own OPTIMADE APIs (it is used by, for example, NOMAD, Materials Project and Materials Cloud), but also for people implementing their own client code (e.g. the Materials Cloud GUI client, and hopefully pymatgen eventually).

However, you can use it in scripts such as yours too, by serializing your JSON response with the OPTIMADE models (which it sounds like you tried to do). Something like:

import urllib.request as request
import json
from optimade.models import StructureResource

with request.urlopen('https://www.crystallography.net/tcod/optimade/v1/structures?flter=elements+HAS+ANY+"C","Si","Ge","Sn","Pb"') as response:
    source = response.read()
    data = json.loads(source)
    structures = [StructureResource(**entry) for entry in data["data"]]

should “work” (though may raise errors about the data itself, normally caused by the set of default response fields that the database returns). For example, the query you provided above does not return site positions, so it will be unable to create a CIF and the model validation will fail.

It was not our intention with optimade-python-tools to make an OPTIMADE Python API, but there may be space for another package that does this (alongside pymatgen).

If you have any feedback, or want to suggest that this kind of functionality is added to optimade-python-tools itself, feel free to raise an issue on GitHub! You may also be interested in an upcoming (next week) NOMAD tutorial on OPTIMADE, which will cover some of these topics: Tutorial6 | NOMAD Virtual Tutorial Series.

Hi again,

Thanks for the clarifying answer. I was indeed suspecting I was not looking at the python-tools from the right angle.

The OPTIMADE Python API sounds like really nice potential upgrade.

Unfortunately I will not be available those dates. But I would love to assist to future Tutorials, is there anywhere I can sing up for notifications?

Best regards,

Oier.

No problem! Thanks for raising your issue (Suggestion to include an OPTIMADE python API · Issue #932 · Materials-Consortia/optimade-python-tools · GitHub). Now is definitely a good time for us to lay out the scope for any future functionality of the package, and hopefully this will lead to other offshoots!

For future tutorials, we can make sure that we post them on this forum. We also advertised it on the Psi-K mailing list, I believe.

If you are interested in getting involved in OPTIMADE more generally, then check out the general website OPTIMADE | materials-consortia.github.io, which has details about the mailing list and monthly meetings, and contact details if you just want to chat to someone.