Last active
May 19, 2022 16:20
-
-
Save shyuep/7177908100b6e963e762e15a89b214f8 to your computer and use it in GitHub Desktop.
Creating a local MP database for fast querying.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymongo | |
from pymatgen.ext.matproj import MPRester | |
# This connects to your local MongoDB without any credentials. | |
client = pymongo.MongoClient() | |
db = client.matproj | |
mpr = MPRester() | |
entries = mpr.get_entries( | |
{}, | |
inc_structure=True, | |
property_data=[ # These are entirely optional, but you can include any useful properties you want | |
"band_gap", | |
"e_above_hull", | |
"icsd_ids" | |
], | |
) | |
docs = [] | |
for e in entries: | |
comp = e.composition | |
elements = sorted(e.composition.keys()) | |
elements_str = sorted([el.symbol for el in elements]) | |
d = e.as_dict() | |
d["pretty_formula"] = comp.reduced_formula | |
d["elements"] = elements_str | |
d["nelements"] = len(comp) | |
d["chemsys"] = "-".join(elements_str) | |
docs.append(d) | |
coll = db.entries | |
coll.insert_many(docs) | |
# These create useful indexes to speed up querying. | |
coll.create_index("entry_id") | |
coll.create_index("pretty_formula") | |
coll.create_index("chemsys") | |
coll.create_index("nelements") | |
coll.create_index("elements") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment