Skip to content

Commit

Permalink
save changes to HPF2023 notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Feb 20, 2025
1 parent b3ce9b7 commit dc8a348
Showing 1 changed file with 67 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"# pip install mpcontribs-client mp_api pandas flatten_dict\n",
"import os\n",
"import gzip\n",
"import json\n",
"\n",
"from pathlib import Path\n",
"from mpcontribs.client import Client\n",
Expand All @@ -27,8 +28,28 @@
"metadata": {},
"outputs": [],
"source": [
"client = Client(project=\"HFP2023\")\n",
"mpr = MPRester(api_key=os.environ[\"MPCONTRIBS_API_KEY\"])"
"# mpr = MPRester(api_key=os.environ[\"MPCONTRIBS_API_KEY\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7bf789df-1db1-4ff9-b7ca-0811a2ea840c",
"metadata": {},
"outputs": [],
"source": [
"with open(\"/Users/patrick/Downloads/usf_formates_tasks.json\", \"r\") as f:\n",
" tasks = json.load(f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "824df967-358b-4399-b24f-18b31a42c864",
"metadata": {},
"outputs": [],
"source": [
"lookup = {doc[\"formula_pretty\"] + \"_\" + str(doc[\"symmetry\"][\"number\"]): doc[\"task_id\"] for doc in tasks}"
]
},
{
Expand All @@ -50,11 +71,11 @@
"outputs": [],
"source": [
"# function to search MP via its summary API endpoint\n",
"def search(formula=None, spacegroup_number=None, chemsys=None):\n",
" return mpr.summary.search(\n",
" formula=formula, chemsys=chemsys, spacegroup_number=spacegroup_number,\n",
" fields=[\"material_id\"], sort_fields=\"energy_above_hull\"\n",
" )"
"# def search(formula=None, spacegroup_number=None, chemsys=None):\n",
"# return mpr.summary.search(\n",
"# formula=formula, chemsys=chemsys, spacegroup_number=spacegroup_number,\n",
"# fields=[\"material_id\"], sort_fields=\"energy_above_hull\"\n",
"# )"
]
},
{
Expand Down Expand Up @@ -120,33 +141,37 @@
" # assuming there's only one CIF per directory\n",
" structure = Structure.from_file(cifs[0])\n",
"\n",
" # try to match CIF against MP\n",
" matching_mpids = mpr.find_structure(structure)\n",
" # # try to match CIF against MP\n",
" # matching_mpids = mpr.find_structure(structure)\n",
"\n",
" if matching_mpids:\n",
" identifier = matching_mpids[0]\n",
" else:\n",
" # structure not in MP (yet)\n",
" # get composition, formula, space group, and chemical system\n",
" composition = structure.composition\n",
" formula, _ = composition.get_reduced_formula_and_factor()\n",
" _, spacegroup_number = structure.get_space_group_info()\n",
" chemsys = composition.chemical_system\n",
" # if matching_mpids:\n",
" # identifier = matching_mpids[0]\n",
" # else:\n",
" # # structure not in MP (yet)\n",
" # # get composition, formula, space group, and chemical system\n",
" # composition = structure.composition\n",
" # formula, _ = composition.get_reduced_formula_and_factor()\n",
" # _, spacegroup_number = structure.get_space_group_info()\n",
" # chemsys = composition.chemical_system\n",
"\n",
" # 1) try formula and space group \n",
" docs = search(formula=formula, spacegroup_number=spacegroup_number)\n",
" if not docs:\n",
" # 2) try formula\n",
" docs = search(formula=formula)\n",
" if not docs:\n",
" # 3) try chemical system\n",
" docs = search(chemsys=chemsys)\n",
" # # 1) try formula and space group \n",
" # docs = search(formula=formula, spacegroup_number=spacegroup_number)\n",
" # if not docs:\n",
" # # 2) try formula\n",
" # docs = search(formula=formula)\n",
" # if not docs:\n",
" # # 3) try chemical system\n",
" # docs = search(chemsys=chemsys)\n",
"\n",
" if docs:\n",
" # always use material with lowest energy above hull\n",
" identifier = docs[0].material_id\n",
" \n",
" print(identifier) # \"link to MP\"\n",
" # if docs:\n",
" # # always use material with lowest energy above hull\n",
" # identifier = docs[0].material_id\n",
"\n",
" composition = structure.composition\n",
" formula, _ = composition.get_reduced_formula_and_factor()\n",
" _, spacegroup_number = structure.get_space_group_info()\n",
" identifier = lookup[f\"{formula}_{spacegroup_number}\"]\n",
" print(identifier) # \"link to MP\"\n",
" \n",
" # make sure everything's gzipped\n",
" for p in subdir.glob(\"*.*\"):\n",
Expand All @@ -155,7 +180,7 @@
" \n",
" # init contribution; add all files as attachments; add structure\n",
" contrib = {\n",
" \"identifier\": identifier, \"data\": {},\n",
" \"identifier\": identifier, \"formula\": formula, \"data\": {},\n",
" \"attachments\": list(subdir.glob(\"*.gz\"))\n",
" }\n",
" if identifier.startswith(\"mp-\"):\n",
Expand All @@ -182,6 +207,16 @@
" contributions.append(contrib)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "43104a49-3e15-4f27-a794-db2acfab11e0",
"metadata": {},
"outputs": [],
"source": [
"client = Client(project=\"HFP2023\")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit dc8a348

Please sign in to comment.