Last active
August 29, 2015 14:15
-
-
Save tschaume/21ec06e00e4421ac7f40 to your computer and use it in GitHub Desktop.
Materials Project: get all references for a material
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
# mktmpenv | |
# pip install numpy | |
# pip install scipy | |
# pip install pymongo | |
# pip install monty | |
# pip install -U setuptools | |
# pip install -e git+git@github:materialsproject/mpworks#egg=mpworks | |
import os | |
from pymongo import MongoClient | |
from monty.serialization import loadfn | |
from mpworks.snl_utils.snl_mongo import SNLMongoAdapter | |
sma = SNLMongoAdapter.from_file( | |
os.path.join(os.environ['DB_LOC'], 'snl_db.yaml') | |
) | |
def get_mp_references(mp_id='mp-568345', db_yaml='materials_db.yaml'): | |
"""get all references in MP for specific mp_id""" | |
# load materials database | |
config = loadfn(os.path.join(os.environ['DB_LOC'], db_yaml)) | |
client = MongoClient(config['host'], config['port'], j=False) | |
client[config['db']].authenticate(config['username'], config['password']) | |
materials = client[config['db']].materials | |
# get snlgroup_id_final for mp_id | |
snlgroup_id_final = materials.find_one( | |
{'task_id': mp_id}, {'_id': 0, 'snlgroup_id_final': 1} | |
)['snlgroup_id_final'] | |
# get all_snl_ids for snlgroup_id_final | |
all_snl_ids = sma.database.snlgroups.find_one( | |
{'snlgroup_id': snlgroup_id_final}, {'_id': 0, 'all_snl_ids': 1} | |
)['all_snl_ids'] | |
# get cursor for snls in all_snl_ids | |
cursor = sma.database.snl.find( | |
{'snl_id': {'$in': all_snl_ids}}, {'_id': 0, 'about.references': 1} | |
) | |
# compile references and remove empty strings | |
references = filter(None, [doc['about']['references'] for doc in cursor]) | |
# return as single bibtex string (can be parse by user via pybtex) | |
# TODO: should we fix double entries and bibtex keys? | |
# TODO: should we check validity of bibtex code via pybtex? | |
return '\n'.join(references) | |
if __name__ == '__main__': | |
print get_mp_references() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment