Use DLite strategies from OTEAPI-OPTIMADE¶
This example shows how to use the DLite strategies from the OTEAPI-OPTIMADE plugin.
DLite is a Python library for working with data models and semantics. It is considered to be the default semantic data model backend for use with OTEAPI.
To see more fundamental examples of how to use OTEAPI-OPTIMADE, see the example Use OTEAPI-OPTIMADE with OTElib. OTElib will also be used as a client in the current example. Furthermore, only the HTTP requests-based backend will be used in this example.
Setup¶
Please see the setup instructions in Use OTEAPI-OPTIMADE with OTElib for how to ensure you have a proper environment to run the example in.
Example¶
In this example, we will use the DLite strategies to query the Materials Project OPTIMADE API.
We are interested in finding all structures that include the elements Si
and O
, and that have a maximum of 4 elements in total.
Create a client¶
Let's start by initializing a client:
from otelib import OTEClient
client = OTEClient("python")
Data Resource strategy¶
The general pipeline is the same as it was for Use OTEAPI-OPTIMADE with OTElib:
However, in order to use DLite we need to either reference a specific accessService
or set a flag in the configuration of the data resource strategy.
Note, it is only for the data resource strategy we need to specify the usage of DLite, as the filter strategy is generic and merely a helper for more explicitly setting the query parameters to be used for the underlying OPTIMADE query.
data_resource_strategy = client.create_dataresource(
accessService="OPTIMADE+DLite",
accessUrl="https://optimade.materialsproject.org",
)
# This is equivalent to:
# data_resource_strategy = client.create_dataresource(
# accessService="OPTIMADE",
# accessUrl="https://optimade.materialsproject.org",
# configuration={"use_dlite": True},
# )
Filter strategy¶
The OPTIMADE filter query to fulfill the interests outlined above should look like this:
elements HAS ALL "Si","O" AND nelements<=4
filter_strategy = client.create_filter(
filterType="OPTIMADE",
query='elements HAS ALL "Si","O" AND nelements<=4',
)
Setup, execute and inspect the pipeline¶
import json
pipeline = filter_strategy >> data_resource_strategy
session = pipeline.get()
parsed_session = json.loads(session)
parsed_session.keys()
Setting filter from query. Setting filter from query. Setting filter from query. resource_config: OPTIMADEResourceConfig(user=None, password=None, token=None, client_id=None, client_secret=None, configuration=OPTIMADEConfig(version='v1', endpoint='structures', query_parameters=OPTIMADEQueryParameters(filter='elements HAS ALL "Si","O" AND nelements<=4', response_format='json', email_address='', response_fields='', sort='', page_limit=20, page_offset=0, page_number=None, page_cursor=0, page_above=None, page_below=None, include='references', api_hint=''), datacache_config=DataCacheConfig(cacheDir=PosixPath('oteapi'), accessKey=None, hashType='md5', expireTime=86400, tag='optimade'), return_object=False, use_dlite=False), description='Resource Strategy Data Configuration.\n\n Important:\n Either of the pairs of attributes `downloadUrl`/`mediaType` or\n `accessUrl`/`accessService` MUST be specified.\n\n ', downloadUrl=None, mediaType=None, accessUrl=OPTIMADEUrl('https://optimade.materialsproject.org', base_url='https://optimade.materialsproject.org', scheme='https', tld='org', host_type='domain'), accessService='OPTIMADE+DLite', license=None, accessRights=None, publisher=None) resource_config: OPTIMADEResourceConfig(user=None, password=None, token=None, client_id=None, client_secret=None, configuration=OPTIMADEConfig(version='v1', endpoint='structures', query_parameters=OPTIMADEQueryParameters(filter='elements HAS ALL "Si","O" AND nelements<=4', response_format='json', email_address='', response_fields='', sort='', page_limit=20, page_offset=0, page_number=None, page_cursor=0, page_above=None, page_below=None, include='references', api_hint=''), datacache_config=DataCacheConfig(cacheDir=PosixPath('oteapi'), accessKey=None, hashType='md5', expireTime=86400, tag='optimade'), return_object=False, use_dlite=False), description='Resource Strategy Data Configuration.\n\n Important:\n Either of the pairs of attributes `downloadUrl`/`mediaType` or\n `accessUrl`/`accessService` MUST be specified.\n\n ', downloadUrl=None, mediaType=None, accessUrl=OPTIMADEUrl('https://optimade.materialsproject.org', base_url='https://optimade.materialsproject.org', scheme='https', tld='org', host_type='domain'), accessService='OPTIMADE+DLite', license=None, accessRights=None, publisher=None) resource_config: OPTIMADEResourceConfig(user=None, password=None, token=None, client_id=None, client_secret=None, configuration=OPTIMADEConfig(version='v1', endpoint='structures', query_parameters=OPTIMADEQueryParameters(filter='elements HAS ALL "Si","O" AND nelements<=4', response_format='json', email_address='', response_fields='', sort='', page_limit=20, page_offset=0, page_number=None, page_cursor=0, page_above=None, page_below=None, include='references', api_hint=''), datacache_config=DataCacheConfig(cacheDir=PosixPath('oteapi'), accessKey=None, hashType='md5', expireTime=86400, tag='optimade'), return_object=False, use_dlite=False), description='Resource Strategy Data Configuration.\n\n Important:\n Either of the pairs of attributes `downloadUrl`/`mediaType` or\n `accessUrl`/`accessService` MUST be specified.\n\n ', downloadUrl=None, mediaType=None, accessUrl=OPTIMADEUrl('https://optimade.materialsproject.org', base_url='https://optimade.materialsproject.org', scheme='https', tld='org', host_type='domain'), accessService='OPTIMADE+DLite', license=None, accessRights=None, publisher=None) optimade_query after update: OPTIMADEQueryParameters(filter='elements HAS ALL "Si","O" AND nelements<=4', response_format='json', email_address='', response_fields='', sort='', page_limit=20, page_offset=0, page_number=None, page_cursor=0, page_above=None, page_below=None, include='references', api_hint='') optimade_query after update: OPTIMADEQueryParameters(filter='elements HAS ALL "Si","O" AND nelements<=4', response_format='json', email_address='', response_fields='', sort='', page_limit=20, page_offset=0, page_number=None, page_cursor=0, page_above=None, page_below=None, include='references', api_hint='') optimade_query after update: OPTIMADEQueryParameters(filter='elements HAS ALL "Si","O" AND nelements<=4', response_format='json', email_address='', response_fields='', sort='', page_limit=20, page_offset=0, page_number=None, page_cursor=0, page_above=None, page_below=None, include='references', api_hint='') OPTIMADE URL to be requested: https://optimade.materialsproject.org/v1/structures?filter=elements%20HAS%20ALL%20%22Si%22%2C%22O%22%20AND%20nelements%3C%3D4&response_format=json&page_limit=20&include=references OPTIMADE URL to be requested: https://optimade.materialsproject.org/v1/structures?filter=elements%20HAS%20ALL%20%22Si%22%2C%22O%22%20AND%20nelements%3C%3D4&response_format=json&page_limit=20&include=references OPTIMADE URL to be requested: https://optimade.materialsproject.org/v1/structures?filter=elements%20HAS%20ALL%20%22Si%22%2C%22O%22%20AND%20nelements%3C%3D4&response_format=json&page_limit=20&include=references
dict_keys(['optimade_config', 'optimade_resources', 'optimade_resource_model', 'collection_id'])
from importlib import import_module
import_path, class_name = parsed_session["optimade_resource_model"].split(":", maxsplit=1)
ResourceClass = getattr(import_module(import_path), class_name)
parsed_structures = [ResourceClass(structure) for structure in parsed_session["optimade_resources"]]
print(f"The query resulted in {len(parsed_structures)} structures found (on page 1) of the returned data.")
print(f"Their Materials Project IDs are: {[structure.id for structure in parsed_structures]}")
The query resulted in 20 structures found (on page 1) of the returned data. Their Materials Project IDs are: ['mp-1033911', 'mp-757887', 'mp-1219366', 'mp-733539', 'mp-542090', 'mp-758465', 'mp-560675', 'mp-774171', 'mp-1304778', 'mp-1016821', 'mp-1363556', 'mp-757013', 'mp-683953', 'mp-1020609', 'mp-17612', 'mp-558129', 'mp-1210628', 'mp-1197149', 'mp-21791', 'mp-752892']
from oteapi_dlite.utils import get_collection
collection = get_collection(session=parsed_session)
print(collection)
{ "bfd78bf8-31fe-4487-a4c3-8cc5351ca87d": { "meta": "http://onto-ns.com/meta/0.1/Collection", "dimensions": { "nrelations": 60 }, "properties": { "relations": [["mp-1033911", "_is-a", "Instance"], ["mp-1033911", "_has-uuid", "f0676948-f620-4057-9291-b1851f519d66"], ["mp-1033911", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-757887", "_is-a", "Instance"], ["mp-757887", "_has-uuid", "76add43f-bb1e-4bdb-984e-dc6c16b9205d"], ["mp-757887", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-1219366", "_is-a", "Instance"], ["mp-1219366", "_has-uuid", "d0e7c4f7-1dd7-4206-aa53-2a67b755259d"], ["mp-1219366", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-733539", "_is-a", "Instance"], ["mp-733539", "_has-uuid", "e460fb2d-1639-4b65-9e61-f4c2869ef412"], ["mp-733539", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-542090", "_is-a", "Instance"], ["mp-542090", "_has-uuid", "4625be3d-cad7-42a2-afdd-3c01715c0905"], ["mp-542090", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-758465", "_is-a", "Instance"], ["mp-758465", "_has-uuid", "d041f6c2-4770-41ec-870c-66d07f9aafff"], ["mp-758465", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-560675", "_is-a", "Instance"], ["mp-560675", "_has-uuid", "d6dfd477-cea9-46c1-b56f-487a77fb615e"], ["mp-560675", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-774171", "_is-a", "Instance"], ["mp-774171", "_has-uuid", "ac42160c-9661-4180-812b-4c2b2e44145e"], ["mp-774171", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-1304778", "_is-a", "Instance"], ["mp-1304778", "_has-uuid", "d840939b-9b95-48b9-9be7-4f0d8bdf480d"], ["mp-1304778", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-1016821", "_is-a", "Instance"], ["mp-1016821", "_has-uuid", "d023ba43-f725-4a8d-b38c-ff5021729f9d"], ["mp-1016821", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-1363556", "_is-a", "Instance"], ["mp-1363556", "_has-uuid", "8d011cb4-fa24-44ac-960d-b027588924d3"], ["mp-1363556", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-757013", "_is-a", "Instance"], ["mp-757013", "_has-uuid", "9d7928e0-1209-4251-ab67-92f5cc1cb1a4"], ["mp-757013", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-683953", "_is-a", "Instance"], ["mp-683953", "_has-uuid", "05eae7e9-2e01-4e21-bc86-0e4004221f76"], ["mp-683953", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-1020609", "_is-a", "Instance"], ["mp-1020609", "_has-uuid", "99468a6c-3875-436c-8025-0af9d4b53be5"], ["mp-1020609", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-17612", "_is-a", "Instance"], ["mp-17612", "_has-uuid", "7b266e95-b56a-4fc3-921c-407daa7d1369"], ["mp-17612", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-558129", "_is-a", "Instance"], ["mp-558129", "_has-uuid", "5c4d6757-866a-482b-83ba-cb8aa035b180"], ["mp-558129", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-1210628", "_is-a", "Instance"], ["mp-1210628", "_has-uuid", "a32c7f8c-a4b8-40f8-96e5-92a642c0e644"], ["mp-1210628", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-1197149", "_is-a", "Instance"], ["mp-1197149", "_has-uuid", "68ec190e-7ff5-497d-a6e5-de1bb568205c"], ["mp-1197149", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-21791", "_is-a", "Instance"], ["mp-21791", "_has-uuid", "b9805c87-808a-48b8-be04-58b18ebafb4e"], ["mp-21791", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"], ["mp-752892", "_is-a", "Instance"], ["mp-752892", "_has-uuid", "e2674e42-465f-4991-b7f5-15a65fb07f6d"], ["mp-752892", "_has-meta", "http://onto-ns.com/meta/1.0/OPTIMADEStructure"]] } } }
for inst in collection.get_instances():
print(inst)
{ "f0676948-f620-4057-9291-b1851f519d66": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "46900658-13b0-4072-8fc1-7e20de603765", "id": "mp-1033911" } } } { "76add43f-bb1e-4bdb-984e-dc6c16b9205d": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "62d27df4-ac40-4784-a5fd-ce48cbf12a3a", "id": "mp-757887" } } } { "d0e7c4f7-1dd7-4206-aa53-2a67b755259d": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "c890c277-74b5-42a9-8bed-d0687b34bbd6", "id": "mp-1219366" } } } { "e460fb2d-1639-4b65-9e61-f4c2869ef412": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "4d2dc20a-671f-4c26-90a2-d9f6bc6bbe0c", "id": "mp-733539" } } } { "4625be3d-cad7-42a2-afdd-3c01715c0905": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "c1e0bc0e-bc18-4ebe-b0df-c8ab4cba83ae", "id": "mp-542090" } } } { "d041f6c2-4770-41ec-870c-66d07f9aafff": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "0592aaad-c896-464a-b368-82f62bbe42f9", "id": "mp-758465" } } } { "d6dfd477-cea9-46c1-b56f-487a77fb615e": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "1baa18c6-fec5-4720-8554-5593ca656f06", "id": "mp-560675" } } } { "ac42160c-9661-4180-812b-4c2b2e44145e": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "d9d59dc2-3f83-4f96-ae5c-6d3ad5a241c6", "id": "mp-774171" } } } { "d840939b-9b95-48b9-9be7-4f0d8bdf480d": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "cdbfd5ec-9a84-4a09-94b8-8ec989f09019", "id": "mp-1304778" } } } { "d023ba43-f725-4a8d-b38c-ff5021729f9d": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "0d46f58f-2744-4c4f-882b-57da2e1c9cdf", "id": "mp-1016821" } } } { "8d011cb4-fa24-44ac-960d-b027588924d3": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "c951ff9b-688e-40f3-b15d-b0acfede5e0a", "id": "mp-1363556" } } } { "9d7928e0-1209-4251-ab67-92f5cc1cb1a4": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "5053a07b-f15b-437a-9bde-ce646a669abe", "id": "mp-757013" } } } { "05eae7e9-2e01-4e21-bc86-0e4004221f76": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "c2f151ff-6c3c-45ed-a7b0-3ee5117f0035", "id": "mp-683953" } } } { "99468a6c-3875-436c-8025-0af9d4b53be5": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "22cd1e19-e45f-47b5-a62d-4027f61f9667", "id": "mp-1020609" } } } { "7b266e95-b56a-4fc3-921c-407daa7d1369": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "0aeef165-da7e-4681-8052-e95b484d637f", "id": "mp-17612" } } } { "5c4d6757-866a-482b-83ba-cb8aa035b180": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "ddb31fe8-6885-4c80-bf2e-044dad60f8c3", "id": "mp-558129" } } } { "a32c7f8c-a4b8-40f8-96e5-92a642c0e644": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "62840cd5-2f20-481f-8967-43476ecec964", "id": "mp-1210628" } } } { "68ec190e-7ff5-497d-a6e5-de1bb568205c": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "c0674560-bbde-4e93-aa8a-bf9260ef6975", "id": "mp-1197149" } } } { "b9805c87-808a-48b8-be04-58b18ebafb4e": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "aec646fb-b54f-4526-ba97-5cae2d3a528a", "id": "mp-21791" } } } { "e2674e42-465f-4991-b7f5-15a65fb07f6d": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructure", "dimensions": { }, "properties": { "type": "structures", "attributes": "606e5ca8-0fb1-45a4-941a-45461d19eceb", "id": "mp-752892" } } }
import dlite
structure_instances: list[dlite.Instance] = list(collection.get_instances())
structure_attributes_instance: dlite.Instance = dlite.get_instance(structure_instances[0].attributes)
print(structure_attributes_instance)
{ "46900658-13b0-4072-8fc1-7e20de603765": { "meta": "http://onto-ns.com/meta/1.0/OPTIMADEStructureAttributes", "dimensions": { "nelements": 4, "dimensionality": 3, "nsites": 32, "nspecies": 4, "nstructure_features": 0 }, "properties": { "elements": ["K", "Mg", "O", "Si"], "nelements": 4, "elements_ratios": [0.03125, 0.4375, 0.03125, 0.5], "chemical_formula_descriptive": "KMg14O16Si", "chemical_formula_reduced": "KMg14O16Si", "chemical_formula_hill": "KMg14O16Si", "chemical_formula_anonymous": "A16B14CD", "dimension_types": [1, 1, 1], "nperiodic_dimensions": 3, "lattice_vectors": [[8.59318, 0, 0], [0, 8.59318, 0], [0, 0, 4.41201]], "cartesian_site_positions": [[0, 0, 0], [0, 4.29659, 0], [4.29659, 0, 0], [0, 2.1438, 2.20601], [0, 6.44938, 2.20601], [4.29659, 2.13079, 2.20601], [4.29659, 6.46238, 2.20601], [2.1438, 0, 2.20601], [2.13079, 4.29659, 2.20601], [6.44938, 0, 2.20601], [6.46238, 4.29659, 2.20601], [2.12673, 2.12673, 0], [2.12673, 6.46645, 0], [6.46645, 2.12673, 0], [6.46645, 6.46645, 0], [4.29659, 4.29659, 0], [2.37689, 0, 0], [2.32997, 4.29659, 0], [6.21628, 0, 0], [6.2632, 4.29659, 0], [2.16162, 2.16162, 2.20601], [2.16162, 6.43155, 2.20601], [6.43155, 2.16162, 2.20601], [6.43155, 6.43155, 2.20601], [0, 0, 2.20601], [0, 4.29659, 2.20601], [4.29659, 0, 2.20601], [4.29659, 4.29659, 2.20601], [0, 2.37689, 0], [0, 6.21628, 0], [4.29659, 2.32997, 0], [4.29659, 6.2632, 0]], "nsites": 32, "species": ["b60b7266-2807-4d89-bdd7-776fc7bd84d0", "faa90667-ab90-484d-b0f5-17685b3c9d5b", "880e1de2-1608-4432-986c-8b199cff5018", "9ab25660-3d30-4034-a9f5-ba0e7a7a8adc"], "species_at_sites": ["K", "Mg", "Mg", "Mg", "Mg", "Mg", "Mg", "Mg", "Mg", "Mg", "Mg", "Mg", "Mg", "Mg", "Mg", "Si", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"], "assemblies": null, "structure_features": [], "immutable_id": "645d2b74bcd30f748b4746a3", "last_modified": "2019-10-23 12:27:13+00:00" } } }