Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fomightez/8ef2f588965fbc10f19db79ee0035094 to your computer and use it in GitHub Desktop.
Save fomightez/8ef2f588965fbc10f19db79ee0035094 to your computer and use it in GitHub Desktop.
demo use mp_api with Materials Project to answer stackoverflow question https://stackoverflow.com/q/74330082/8508004
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "358a6ffa-38c2-470e-8117-41cb968b0fc8",
"metadata": {},
"source": [
"## for StackOverflow reply\n",
"see https://stackoverflow.com/q/74330082/8508004"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "681d5935-cc89-4383-b4da-5fe76304452f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/srv/conda/envs/notebook/lib/python3.9/site-packages/pymatgen/analysis/phase_diagram.py:26: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
" from tqdm.autonotebook import tqdm\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c0b22a79afc54412a701a203259f099a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Retrieving MaterialsDoc documents: 0%| | 0/1 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from mp_api.client import MPRester\n",
"from pymatgen.analysis.diffraction.xrd import XRDCalculator\n",
"from pymatgen.symmetry.analyzer import SpacegroupAnalyzer\n",
"\n",
"with MPRester(api_key='8dI6UZHs3Nc9lxTp75RrJcPdwvPn6jZb') as mpr:\n",
" # first retrieve the relevant structure\n",
" structure = mpr.get_structure_by_material_id('mp-980949')\n",
"\n",
"# important to use the conventional structure to ensure\n",
"# that peaks are labelled with the conventional Miller indices\n",
"sga = SpacegroupAnalyzer(structure)\n",
"conventional_structure = sga.get_conventional_standard_structure()\n",
"\n",
"# this example shows how to obtain an XRD diffraction pattern\n",
"# these patterns are calculated on-the-fly from the structure\n",
"calculator = XRDCalculator(wavelength='CuKa')\n",
"pattern = calculator.get_pattern(conventional_structure)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "adab8ded-fa08-42c7-85a4-0e543b9ad5fc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"api.ipynb README.md requirements.txt runtime.txt Untitled.ipynb\n"
]
}
],
"source": [
"ls"
]
},
{
"cell_type": "markdown",
"id": "ae90369f-f74f-4539-ba13-ecea259d4228",
"metadata": {},
"source": [
"Nothing new is obtained."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "460d6125-b803-4018-ae72-fda64168bafc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DiffractionPattern\n",
"$2\\Theta$: [13.74822981 14.16506955 16.47949297 17.13272665 18.64152876 20.87640653\n",
" 25.18795085 25.65634291 26.48806184 26.93538664 27.35851843 27.69950382\n",
" 28.5525764 29.75239703 30.35464405 30.73976355 30.93589019 31.35108606\n",
" 32.66860353 33.22319047 33.31273534 34.47963127 34.66419554 37.47577623\n",
" 37.80066486 38.45632109 39.00158581 39.32660821 39.95041874 39.95694778\n",
" 40.85859817 41.3175173 41.47176292 42.07541679 42.0855657 42.48915702\n",
" 42.67436067 42.95227101 42.98619873 43.11737627 43.4185239 43.55685532\n",
" 44.15315007 45.58616147 46.14647582 46.21446653 47.06147739 47.28935232\n",
" 47.31841848 47.33552627 47.83066761 48.10138257 48.69397646 49.35976897\n",
" 49.89853581 50.92825362 50.92833524 51.27717495 51.31755262 51.70823063\n",
" 52.08434149 52.72589929 52.84687535 52.99185135 53.08538713 54.03662927\n",
" 54.48144598 54.48808055 54.54177307 55.09670042 55.25623761 55.26922639\n",
" 55.52325345 55.82429163 55.98712644 56.39768463 56.45489122 56.75440193\n",
" 57.11892702 57.208016 57.35341843 57.48253355 58.1408592 58.29278042\n",
" 58.40290356 58.52086357 58.76428539 58.79892208 59.10171636 59.25378431\n",
" 59.96716687 59.99137873 60.74339917 60.86004239 60.99971539 61.43250994\n",
" 61.56688411 61.78950314 62.02314243 62.02321418 62.59085805 62.7100552\n",
" 63.15002403 64.02423208 64.47072472 64.5490064 64.80470289 65.11670225\n",
" 65.13764034 65.41888329 65.4487317 65.84781839 66.31776307 66.34051415\n",
" 66.46028409 66.47836875 66.77143979 67.09925373 67.65120799 67.77620529\n",
" 68.07781299 68.10157376 68.31968705 68.41060267 68.45531378 68.65385733\n",
" 68.68072812 68.72658442 68.84694536 69.07356568 69.69257524 69.71794241\n",
" 69.74673772 69.75606886 69.95602712 70.13505131 70.24314404 70.34425015\n",
" 70.4411832 70.46797674 70.76976023 72.70389436 72.85816992 72.8820489\n",
" 73.14213343 73.27303169 73.36377266 73.47014591 73.49426666 73.51673257\n",
" 73.80250369 73.80270165 73.93111483 73.97843365 74.33134398 74.63195268\n",
" 75.20418726 75.25661643 75.43764542 75.67837647 75.84450745 75.86652255\n",
" 76.12124771 76.39626229 76.44852779 76.47871555 76.67250509 76.68543109\n",
" 76.81814051 77.06077993 77.11924565 77.26700652 77.26947858 77.7208998\n",
" 78.23485592 78.97752134 79.11554528 79.46213597 79.73196674 79.83135138\n",
" 79.89853324 79.95377882 80.1026433 80.33771894 80.75876352 81.18656891\n",
" 81.39372031 81.62078483 81.70619817 81.93490322 82.00332896 82.32464651\n",
" 82.39574561 82.64501876 82.66559217 82.83157724 82.92114367 82.96355637\n",
" 83.32767833 83.34528001 83.53105237 83.57096637 83.6483477 83.73092897\n",
" 83.74345522 83.77032881 83.91414797 83.9347092 84.0826022 84.48042614\n",
" 84.52045453 84.59564788 84.75953613 84.8277624 85.35939193 85.38694132\n",
" 85.43297803 85.4492303 85.5164895 85.58515552 85.91976152 86.19266801\n",
" 86.20947591 86.2906871 86.34034241 86.37390375 86.59822553 86.83303599\n",
" 87.00230257 87.2063007 87.58114022 87.85621973 88.35390188 88.41097583\n",
" 88.4537168 88.54994425 88.64417692 88.7561762 89.3647528 89.65763343\n",
" 89.75653504]\n",
"Intensity: [1.00000000e+02 9.20572253e+01 7.98092202e+00 7.46147550e+00\n",
" 5.02619910e+01 3.05186962e+01 1.60707192e+00 2.03243529e+00\n",
" 1.75652208e+00 1.22495770e+00 1.12961529e+01 3.12796313e+00\n",
" 7.74670898e-01 2.31900460e+00 7.81037896e+00 6.87528211e+00\n",
" 1.36068585e+01 2.99728421e+00 2.26048588e+01 1.07901797e+01\n",
" 4.37614539e+01 1.43272910e+00 1.36553005e-01 1.01054903e+00\n",
" 1.24957507e+01 1.50863598e+00 7.16180965e-01 5.87807990e+00\n",
" 7.72660931e-01 7.59490439e+00 1.13120546e+01 1.69138474e+01\n",
" 1.82742589e+00 2.10314611e+00 4.13281402e+00 1.34844390e+01\n",
" 7.69820914e+00 5.39012032e+00 1.96341515e+00 5.15204562e+00\n",
" 2.71043035e+00 8.46533422e+00 2.59369586e+00 7.20528175e-01\n",
" 5.00036445e-01 5.79157619e+00 4.54677829e+00 9.63127996e+00\n",
" 3.76911066e+00 4.90291803e+00 3.43879371e+00 3.60008865e+00\n",
" 3.51629996e+00 7.30721880e+00 8.54322189e-01 1.21937188e+01\n",
" 3.70680703e+00 4.41710563e+00 1.05660090e+00 2.64107517e+00\n",
" 5.95471023e+00 8.54402206e-01 9.29763033e-01 4.16991551e-01\n",
" 3.29688730e+00 6.82676090e-01 2.92639666e+00 8.73698060e-01\n",
" 6.59006631e+00 1.20246569e+00 2.13108889e+00 3.21413542e-01\n",
" 1.06896704e+01 2.78039136e-01 1.56339202e+00 3.26199490e+00\n",
" 2.68002521e+00 4.68841905e-01 2.62739897e+00 2.13700497e+00\n",
" 2.25016540e+00 4.74812640e+00 4.11520206e+00 1.26395756e+00\n",
" 1.05863265e+00 1.00188662e+00 1.60773447e+00 1.71378562e-01\n",
" 4.53134755e+00 2.63791274e+00 2.74893052e-01 8.87798526e-01\n",
" 5.98133227e-02 6.46088672e-01 2.74041708e-01 5.06867053e-02\n",
" 2.35233576e+00 1.92420043e+00 2.98020317e-01 2.62006502e+00\n",
" 2.21616686e+00 1.09260237e+00 1.06142602e+00 2.21806776e-03\n",
" 1.28477510e-01 4.28067144e+00 2.49383889e+00 1.07192024e-02\n",
" 1.65013815e+00 2.24732530e+00 1.61203988e-01 1.21896094e+00\n",
" 2.68585418e+00 1.33703644e+00 5.38301982e-01 4.84792989e-01\n",
" 1.82907053e+00 5.50764047e-01 9.26971549e-01 1.74181387e+00\n",
" 1.70227372e-01 1.90717324e+00 2.93119742e-01 1.40154834e-01\n",
" 8.97397842e-01 1.61159774e-01 8.82323918e-01 9.91308362e-02\n",
" 4.46171622e-01 1.27747929e+00 4.62583396e-01 1.89667114e+00\n",
" 1.56099603e-01 1.05785843e+00 1.16339379e+00 4.65929923e-01\n",
" 1.21042331e+00 8.05220528e-03 1.40864016e+00 3.50032466e-01\n",
" 1.71633275e-02 3.04172011e-01 2.01245710e-01 1.13412700e+00\n",
" 1.98960276e+00 2.42452899e+00 1.93471343e+00 1.91595782e+00\n",
" 1.99860230e-02 6.53468871e-01 1.04237057e-01 7.85588465e-02\n",
" 1.05286610e+00 4.35182507e-03 9.04250637e-02 1.28292794e+00\n",
" 6.90473573e-01 1.82688549e+00 1.41438513e-02 2.78554374e-01\n",
" 6.70661702e-01 1.69041219e+00 1.52672871e+00 7.03449137e-01\n",
" 1.33441450e+00 1.41956686e+00 3.04230958e-01 1.68249899e+00\n",
" 1.59762002e+00 1.26214807e+00 2.69754474e+00 1.15437412e+00\n",
" 3.54628021e-01 3.49311224e-01 1.31634365e+00 3.17802170e-01\n",
" 2.17745912e+00 2.20870163e+00 6.74320155e-01 8.05449723e-01\n",
" 7.04949861e-01 7.12361658e-01 2.50032986e-01 1.17521142e-01\n",
" 6.95855033e-01 1.65568043e+00 7.60951172e-01 5.41753691e-01\n",
" 1.24437653e+00 1.20471176e+00 8.34743502e-01 4.12076485e-01\n",
" 1.93295018e+00 5.77488452e-01 1.08060880e-01 3.33491583e-02\n",
" 7.98939714e-02 1.17890776e+00 1.98765393e+00 8.77768835e-02\n",
" 1.25525109e+00 1.27578220e+00 5.96414023e-01 1.90473790e-01\n",
" 3.45102022e-01 1.41254960e+00 1.48916367e+00 1.48614199e+00\n",
" 1.56688091e-01 8.05514051e-01 1.35601507e+00 2.45215408e-01\n",
" 7.11381182e-03 1.14969733e+00 2.29610748e-01 9.54939440e-01\n",
" 2.44154753e-01 9.93646454e-01 1.51602223e+00 8.36122611e-01\n",
" 1.95325913e+00 9.43872887e-02 1.18042988e+00 2.21620144e+00\n",
" 4.90732681e-01 5.54876777e-03 8.48452935e-01 7.73133089e-01\n",
" 1.20511884e+00 2.01469484e+00 4.73533841e-02 3.41182229e-02\n",
" 1.56417968e+00 9.77706123e-01 2.64398696e-02 3.23698564e-01\n",
" 3.82077126e-01 1.47052542e+00 7.95500648e-02 5.65737335e-02\n",
" 8.45419407e-02]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pattern"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a764a5c4-9c03-4e31-9640-26d3879d3119",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Structure Summary\n",
"Lattice\n",
" abc : 12.583618 6.519782 8.514034991951231\n",
" angles : 90.0 130.841378352691 90.0\n",
" volume : 528.440564205655\n",
" A : 0.0 12.583618 0.0\n",
" B : 6.519782 0.0 0.0\n",
" C : 0.0 -5.567898999999999 -6.441063000000001\n",
" pbc : True True True\n",
"PeriodicSite: W (1.3346, 0.0000, 0.0000) [0.0000, 0.2047, 0.0000]\n",
"PeriodicSite: W (5.1852, 0.0000, 0.0000) [0.0000, 0.7953, 0.0000]\n",
"PeriodicSite: W (4.5945, 6.2918, 0.0000) [0.5000, 0.7047, 0.0000]\n",
"PeriodicSite: W (1.9253, 6.2918, 0.0000) [0.5000, 0.2953, 0.0000]\n",
"PeriodicSite: Cl (3.2599, 1.3315, -0.9603) [0.1718, 0.5000, 0.1491]\n",
"PeriodicSite: Cl (0.0000, 1.5956, -1.1503) [0.2058, 0.0000, 0.1786]\n",
"PeriodicSite: Cl (4.8049, 4.9483, -1.8672) [0.5215, 0.7370, 0.2899]\n",
"PeriodicSite: Cl (1.7149, 4.9483, -1.8672) [0.5215, 0.2630, 0.2899]\n",
"PeriodicSite: Cl (4.8049, 2.0674, -4.5739) [0.4785, 0.7370, 0.7101]\n",
"PeriodicSite: Cl (1.7149, 2.0674, -4.5739) [0.4785, 0.2630, 0.7101]\n",
"PeriodicSite: Cl (0.0000, 5.4201, -5.2908) [0.7942, 0.0000, 0.8214]\n",
"PeriodicSite: Cl (3.2599, 5.6842, -5.4807) [0.8282, 0.5000, 0.8509]\n",
"PeriodicSite: Cl (0.0000, 7.6233, -0.9603) [0.6718, 0.0000, 0.1491]\n",
"PeriodicSite: Cl (3.2599, 7.8874, -1.1503) [0.7058, 0.5000, 0.1786]\n",
"PeriodicSite: Cl (1.5450, -1.3435, -1.8672) [0.0215, 0.2370, 0.2899]\n",
"PeriodicSite: Cl (4.9748, -1.3435, -1.8672) [0.0215, 0.7630, 0.2899]\n",
"PeriodicSite: Cl (1.5450, 8.3592, -4.5739) [0.9785, 0.2370, 0.7101]\n",
"PeriodicSite: Cl (4.9748, 8.3592, -4.5739) [0.9785, 0.7630, 0.7101]\n",
"PeriodicSite: Cl (3.2599, -0.8717, -5.2908) [0.2942, 0.5000, 0.8214]\n",
"PeriodicSite: Cl (0.0000, -0.6076, -5.4807) [0.3282, 0.0000, 0.8509]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conventional_structure"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "db7e5ca9-382a-406c-a9d8-b711216a5e29",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pymatgen.symmetry.analyzer.SpacegroupAnalyzer at 0x7fa42b2dc100>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sga"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1edfa5b2-f630-4032-a462-96903cf7c79b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing 'pattern' (DiffractionPattern) to file 'pattern.txt'.\n"
]
}
],
"source": [
"%store pattern >pattern.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "083f7180-bdb2-4c0e-a006-4fe7441e9fc1",
"metadata": {},
"outputs": [],
"source": [
"%store convential_structure >conventional_structure.txt"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b19607d1-2cbc-466f-8502-69d170316c63",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Structure Summary\n",
"Lattice\n",
" abc : 7.086166087833533 7.086166087833533 8.514034991951231\n",
" angles : 54.50319138768853 54.50319138768853 54.77885193228324\n",
" volume : 264.22028210282747\n",
" A : 3.259891 6.291809 0.0\n",
" B : -3.259891 6.291809 0.0\n",
" C : 0.0 5.567899 6.441063\n",
" pbc : True True True\n",
"PeriodicSite: W (1.9253, 6.2918, 0.0000) [0.7953, 0.2047, 0.0000]\n",
"PeriodicSite: W (-1.9253, 6.2918, 0.0000) [0.2047, 0.7953, 0.0000]\n",
"PeriodicSite: Cl (0.0000, 13.1912, 5.4807) [0.6718, 0.6718, 0.8509]\n",
"PeriodicSite: Cl (0.0000, 7.1635, 5.2908) [0.2058, 0.2058, 0.8214]\n",
"PeriodicSite: Cl (1.7149, 10.5162, 4.5739) [0.7845, 0.2585, 0.7101]\n",
"PeriodicSite: Cl (-1.7149, 10.5162, 4.5739) [0.2585, 0.7845, 0.7101]\n",
"PeriodicSite: Cl (1.7149, 7.6353, 1.8672) [0.7415, 0.2155, 0.2899]\n",
"PeriodicSite: Cl (-1.7149, 7.6353, 1.8672) [0.2155, 0.7415, 0.2899]\n",
"PeriodicSite: Cl (0.0000, 10.9880, 1.1503) [0.7942, 0.7942, 0.1786]\n",
"PeriodicSite: Cl (0.0000, 4.9603, 0.9603) [0.3282, 0.3282, 0.1491]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"structure"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "581c3bf9-2fb5-480f-a6a1-80a324fcbe77",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing 'structure' (Structure) to file 'structure.txt'.\n"
]
}
],
"source": [
"%store structure >structure.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b3a4ff5-10fe-494a-b8ed-365657cb9298",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@luizdisconzi
Copy link

awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment