Sorry for not logging into the forum for so long, this is the code I used
from mp_api.client import MPRester
import csv
class MP(object):
def __init__(self):
self.API_KEY = ""
def get_data(self):
with MPRester(self.API_KEY) as mpr:
doc1 = mpr.materials.summary.search(
band_gap=(3, None), is_stable=True,
fields=["material_id", "formula_pretty", "band_gap", "bulk_modulus", "shear_modulus"]
)
doc2 = mpr.materials.summary.search(
band_gap=(3, None), theoretical=False,
fields=["material_id", "formula_pretty", "band_gap", "bulk_modulus", "shear_modulus"]
)
# Merge and deduplicate documents
unique_docs = {doc.material_id: doc for doc in doc1 + doc2}.values()
mpid = [doc.material_id for doc in unique_docs]
formula = [doc.formula_pretty for doc in unique_docs]
bandgap = [doc.band_gap for doc in unique_docs]
# Check for existence of bulk_modulus and shear_modulus fields
k_vrh = [doc.bulk_modulus.get("vrh") if doc.bulk_modulus else None for doc in unique_docs]
g_vrh = [doc.shear_modulus.get("vrh") if doc.shear_modulus else None for doc in unique_docs]
# Identify if the material is stable and if it's theoretical
is_stable = [1 if doc.material_id in [d.material_id for d in doc1] else 0 for doc in unique_docs]
exp = [1 if doc.material_id in [d.material_id for d in doc2] else 0 for doc in unique_docs]
return mpid, formula, bandgap, k_vrh, g_vrh, is_stable, exp
def write_to_file(self):
mpid, formula, bandgap, k_vrh, g_vrh, is_stable, exp = self.get_data()
headers = [
"material_id",
"formula_pretty",
"band_gap",
"k_vrh",
"g_vrh",
"is_stable",
"exp",
]
with open('Hv_2_3.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=headers)
writer.writeheader()
writer.writerows(
{
"material_id": material_id,
"formula_pretty": formula_pretty,
"band_gap": gap,
"k_vrh": bulk_modulus,
"g_vrh": shear_modulus,
"is_stable": stable,
"exp": exp,
} for material_id, formula_pretty, gap, bulk_modulus, shear_modulus, stable, exp in zip(mpid, formula, bandgap, k_vrh, g_vrh, is_stable, exp)
)
s = MP()
s.write_to_file()