{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Part 2 - Core Query Builder Functions" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from mdf_forge.forge import Forge" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "mdf = Forge()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Query builders" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### match_field\n", "Forge has many helper functions to make constructing queries easier. The simplest of the helpers is `match_field()`.\n", "\n", "To use `match_field()`, provide it with the field and value to match on." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mdf.match_field(\"material.elements\", \"Al\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use `match_field()` as many times as you like to add more fields and values. (This applies to all of the query builder helpers.)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mdf.match_field(\"mdf.source_name\", \"oqmd*\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once you're done adding fields, use the `search()` method to execute your search. You don't need to specify the `advanced` argument; when using the query builder functions it is always set to `True`.\n", "\n", "After you execute a search, the query is cleared from memory." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{'crystal_structure': {'cross_reference': {'icsd': 43492},\n", " 'number_of_atoms': 1,\n", " 'space_group_number': 225,\n", " 'volume': 16.4826},\n", " 'dft': {'converged': True,\n", " 'cutoff_energy': 520.0,\n", " 'exchange_correlation_functional': 'PBE'},\n", " 'files': [{'data_type': 'ASCII text, with very long lines, with no line terminators',\n", " 'filename': '17.json',\n", " 'globus': 'globus://e38ee745-6d04-11e5-ba46-22000b92c6ec/MDF/mdf_connect/prod/data/oqmd_v13/17.json',\n", " 'length': 11769,\n", " 'mime_type': 'text/plain',\n", " 'sha512': '153783912c0dacf4d21f5d452a0568c39e4d175ca701d7fe49fd7a6fb8208dd986f167ee4b88534fb475c6cd30842c71cf4f00b9594ef94e888f7c79a182dbb7',\n", " 'url': 'https://e38ee745-6d04-11e5-ba46-22000b92c6ec.e.globus.org/MDF/mdf_connect/prod/data/oqmd_v13/17.json'}],\n", " 'material': {'composition': 'Al1', 'elements': ['Al']},\n", " 'mdf': {'ingest_date': '2018-11-09T19:44:43.687681Z',\n", " 'mdf_id': '5be5e6632ef388650e01442b',\n", " 'parent_id': '5be5e3ab2ef388650efd6704',\n", " 'resource_type': 'record',\n", " 'scroll_id': 253222,\n", " 'source_id': 'oqmd_v13',\n", " 'source_name': 'oqmd',\n", " 'version': 13},\n", " 'oqmd': {'band_gap': {'units': 'eV', 'value': 0.0},\n", " 'configuration': 'static',\n", " 'delta_e': {'units': 'eV/atom', 'value': 0.000788779999999711},\n", " 'magnetic_moment': {'units': 'bohr/atom'},\n", " 'stability': {'units': 'eV/atom', 'value': 0.00157755999999942},\n", " 'total_energy': {'units': 'eV/atom', 'value': -3.74495068},\n", " 'volume_pa': {'units': 'angstrom^3/atom', 'value': 16.4826}}}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = mdf.search(limit=10)\n", "res[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### exclude_field\n", "`exclude_field()` is the opposite of `match_field()`; it excludes results with the specified value." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mdf.exclude_field(\"material.elements\", \"Cu\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can chain calls together if you want." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mdf.exclude_field(\"mdf.source_name\", \"sluschi\").match_field(\"material.elements\", \"Al\").exclude_field(\"mdf.source_name\", \"oqmd\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'cip': {'bv': '79.0',\n", " 'energy': '-3.36',\n", " 'forcefield': 'Al99.eam.alloy',\n", " 'gv': '29.4',\n", " 'mpid': 'mp-134',\n", " 'totenergy': '-107.52'},\n", " 'files': [{'data_type': 'ASCII text, with very long lines, with no line terminators',\n", " 'filename': 'classical_interatomic_potentials.json',\n", " 'globus': 'globus://e38ee745-6d04-11e5-ba46-22000b92c6ec/MDF/mdf_connect/prod/data/cip_v1/classical_interatomic_potentials.json',\n", " 'length': 1841203,\n", " 'mime_type': 'text/plain',\n", " 'sha512': '96635ee0c15d1d0187b18805653a02b1a6dfa5648db82153467045de18adcc08c753e2897d2b48a78a2167a442219e9aeff6b1103732c2158facac8fa4911b33',\n", " 'url': 'https://e38ee745-6d04-11e5-ba46-22000b92c6ec.e.globus.org/MDF/mdf_connect/prod/data/cip_v1/classical_interatomic_potentials.json'}],\n", " 'material': {'composition': 'Al32', 'elements': ['Al']},\n", " 'mdf': {'ingest_date': '2018-10-29T17:47:57.468388Z',\n", " 'mdf_id': '5bd747cf2ef3880b0f213904',\n", " 'parent_id': '5bd747cd2ef3880b0f2135d1',\n", " 'resource_type': 'record',\n", " 'scroll_id': 819,\n", " 'source_id': 'cip_v1',\n", " 'source_name': 'cip',\n", " 'version': 1}}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = mdf.search(limit=10)\n", "res[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }