class mp_query(): def __init__(self, api_key): self.mpr = MPRester(api_key) def find_access_strings(self, search): data = self.mpr.get_data(search) material_ids = [datum['material_id'] for datum in data] return material_ids def mp_structure(self, material_id): struct = self.mpr.get_structure_by_material_id(material_id) struct = SpacegroupAnalyzer( struct).get_conventional_standard_structure() return struct def make_structures(self, search): material_ids = self.find_access_strings(search) structures = [] for ID in material_ids: struct = self.mp_structure(ID) structures.append(struct) return structures
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36' df_2d = pd.read_csv('C:/Users/Subrat.Subedi/Downloads/2D.csv') for index, row in df_2d.iterrows(): baseurl = 'https://www.materialsproject.org/rest/v2/materials/%s' % ( row['mpid']) output = {} try: response = requests.get(baseurl + '/doc', headers={ 'x-api-key': API_KEY, 'User-Agent': USER_AGENT }).json() mp_json = response['response'] except KeyError: m = MPRester(API_KEY) data = m.get_data(row['mpid']) mp_json = to_dict(data) try: output['name'] = mp_json["exp"]["tags"][0] except: try: output['name'] = mp_json["pretty_formula"] except: continue with open( 'C:/Users/Subrat.Subedi/Downloads/Data Collections/rex_stanford_xls/' + row['mpid'] + '.json', 'w', encoding='utf-8') as rex: json.dump(mp_json, rex) output['authors'] = ["Materials Project"]
class CompoundEntries(object): def __init__(self, materialsprojectKey, compoundFomulaList): self.rester = MPRester(materialsprojectKey) self.materialFormulaList = compoundFomulaList # self.features = ['element1','element2', 'formation_energy_per_atom' ] #self.featureSize=len(self.features) self.grepped_entry_list = [None] * len(self.materialFormulaList) self.compoundFeatureMatrix = np.array([]) self.compoundFeatures = [ 'nsites', 'density', 'energy_per_atom', 'point_group' ] def dataPretreatment(self, data_stable_phase): data_stable_phase.update( data_stable_phase['spacegroup'] ) # Add sub-dictionary to the main-dictionary. del data_stable_phase['spacegroup'] # Remove sub-dictionary. # remove 'cif' and 'icsd_ids' from dictionaries for they are too long. del data_stable_phase['cif'] del data_stable_phase['icsd_ids'] return data_stable_phase def grep(self, formula): # Give me one formula at a time. return one directory. try: data = self.rester.get_data( formula) # data is list of directories. except pymatgen.ext.matproj.MPRestError: data = [] j = 0 if data == []: # No data on MaterialsProject for this compound return [] stable_phase_found = False while j < len(data): if data[j]['e_above_hull'] == 0: stable_phase_found = True data_stable_phase = data[j] break elif j == len(data) - 1 and stable_phase_found == False: return [] print("No 'e_above_hull'==0 for " + materialFormula) j += 1 treated_data_stable_phase = self.dataPretreatment(data_stable_phase) treated_data_stable_phase = entryToFloat.entryToFloat( treated_data_stable_phase) return treated_data_stable_phase def grepCompoundFeatureMatrix(self): index = 0 for formula in self.materialFormulaList: print(formula) stablePhaseEntry = self.grep(formula) singleCompoundFeature = [] if stablePhaseEntry != []: for feature in self.compoundFeatures: try: singleCompoundFeature = np.append( singleCompoundFeature, stablePhaseEntry[feature]) # a list except KeyError: singleCompoundFeature = np.append( singleCompoundFeature, 0) if index == 0: self.compoundFeatureMatrix = singleCompoundFeature else: self.compoundFeatureMatrix = np.vstack( [self.compoundFeatureMatrix, singleCompoundFeature]) elif stablePhaseEntry == []: # retrived data is []. Add zeros. if index == 0: self.compoundFeatureMatrix = np.zeros( len(self.compoundFeatures)) else: self.compoundFeatureMatrix = np.vstack([ self.compoundFeatureMatrix, np.zeros(len(self.compoundFeatures)) ]) index += 1 return
class CompositionToStructureFromMP(ConversionFeaturizer): """ Featurizer to get a Structure object from Materials Project using the composition alone. The most stable entry from Materials Project is selected, or NaN if no entry is found in the Materials Project. Args: target_col_id (str or None): The column in which the converted data will be written. If the column already exists then an error will be thrown unless `overwrite_data` is set to `True`. If `target_col_id` begins with an underscore the data will be written to the column: `"{}_{}".format(col_id, target_col_id[1:])`, where `col_id` is the column being featurized. If `target_col_id` is set to None then the data will be written "in place" to the `col_id` column (this will only work if `overwrite_data=True`). overwrite_data (bool): Overwrite any data in `target_column` if it exists. map_key (str): Materials API key """ def __init__(self, target_col_id='structure', overwrite_data=False, mapi_key=None): super().__init__(target_col_id, overwrite_data) self.mpr = MPRester(mapi_key) self.set_n_jobs(1) def featurize(self, comp): """ Get the most stable structure from Materials Project Args: comp (`pymatgen.core.composition.Composition`): A composition. Returns: (`pymatgen.core.structure.Structure`): A Structure object. """ entries = self.mpr.get_data(comp.reduced_formula, prop="energy_per_atom") if len(entries) > 0: most_stable_entry = \ sorted(entries, key=lambda e: e['energy_per_atom'])[0] s = self.mpr.get_structure_by_material_id( most_stable_entry["material_id"]) return [s] return [float("nan")] def citations(self): return [ "@article{doi:10.1063/1.4812323, author = {Jain,Anubhav and Ong," "Shyue Ping and Hautier,Geoffroy and Chen,Wei and Richards, " "William Davidson and Dacek,Stephen and Cholia,Shreyas " "and Gunter,Dan and Skinner,David and Ceder,Gerbrand " "and Persson,Kristin A. }, title = {Commentary: The Materials " "Project: A materials genome approach to accelerating materials " "innovation}, journal = {APL Materials}, volume = {1}, number = " "{1}, pages = {011002}, year = {2013}, doi = {10.1063/1.4812323}, " "URL = {https://doi.org/10.1063/1.4812323}, " "eprint = {https://doi.org/10.1063/1.4812323}}", "@article{Ong2015, author = {Ong, Shyue Ping and Cholia, " "Shreyas and Jain, Anubhav and Brafman, Miriam and Gunter, Dan " "and Ceder, Gerbrand and Persson, Kristin a.}, doi = " "{10.1016/j.commatsci.2014.10.037}, issn = {09270256}, " "journal = {Computational Materials Science}, month = {feb}, " "pages = {209--215}, publisher = {Elsevier B.V.}, title = " "{{The Materials Application Programming Interface (API): A simple, " "flexible and efficient API for materials data based on " "REpresentational State Transfer (REST) principles}}, " "url = {http://linkinghub.elsevier.com/retrieve/pii/S0927025614007113}, " "volume = {97}, year = {2015} } " ] def implementors(self): return ["Anubhav Jain"]
class CompositionToStructureFromMP(ConversionFeaturizer): """ Featurizer to get a Structure object from Materials Project using the composition alone. The most stable entry from Materials Project is selected, or NaN if no entry is found in the Materials Project. Args: target_col_id (str or None): The column in which the converted data will be written. If the column already exists then an error will be thrown unless `overwrite_data` is set to `True`. If `target_col_id` begins with an underscore the data will be written to the column: `"{}_{}".format(col_id, target_col_id[1:])`, where `col_id` is the column being featurized. If `target_col_id` is set to None then the data will be written "in place" to the `col_id` column (this will only work if `overwrite_data=True`). overwrite_data (bool): Overwrite any data in `target_column` if it exists. map_key (str): Materials API key """ def __init__(self, target_col_id='structure', overwrite_data=False, mapi_key=None): super().__init__(target_col_id, overwrite_data) self.mpr = MPRester(mapi_key) def featurize(self, comp): """ Get the most stable structure from Materials Project Args: comp (`pymatgen.core.composition.Composition`): A composition. Returns: (`pymatgen.core.structure.Structure`): A Structure object. """ entries = self.mpr.get_data(comp.reduced_formula, prop="energy_per_atom") if len(entries) > 0: most_stable_entry = \ sorted(entries, key=lambda e: e['energy_per_atom'])[0] s = self.mpr.get_structure_by_material_id( most_stable_entry["material_id"]) return[s] return [float("nan")] def citations(self): return [ "@article{doi:10.1063/1.4812323, author = {Jain,Anubhav and Ong," "Shyue Ping and Hautier,Geoffroy and Chen,Wei and Richards, " "William Davidson and Dacek,Stephen and Cholia,Shreyas " "and Gunter,Dan and Skinner,David and Ceder,Gerbrand " "and Persson,Kristin A. }, title = {Commentary: The Materials " "Project: A materials genome approach to accelerating materials " "innovation}, journal = {APL Materials}, volume = {1}, number = " "{1}, pages = {011002}, year = {2013}, doi = {10.1063/1.4812323}, " "URL = {https://doi.org/10.1063/1.4812323}, " "eprint = {https://doi.org/10.1063/1.4812323}}", "@article{Ong2015, author = {Ong, Shyue Ping and Cholia, " "Shreyas and Jain, Anubhav and Brafman, Miriam and Gunter, Dan " "and Ceder, Gerbrand and Persson, Kristin a.}, doi = " "{10.1016/j.commatsci.2014.10.037}, issn = {09270256}, " "journal = {Computational Materials Science}, month = {feb}, " "pages = {209--215}, publisher = {Elsevier B.V.}, title = " "{{The Materials Application Programming Interface (API): A simple, " "flexible and efficient API for materials data based on " "REpresentational State Transfer (REST) principles}}, " "url = {http://linkinghub.elsevier.com/retrieve/pii/S0927025614007113}, " "volume = {97}, year = {2015} } "] def implementors(self): return ["Anubhav Jain"]
import pandas as pd import matplotlib.pyplot as plt import itertools df = pd.DataFrame() allBinaries = itertools.combinations(periodic_table.all_symbols(), 2) # Create list of all binary systems API_KEY = None # Enter your key received from Materials Project if API_KEY is None: m = MPRester() else: m = MPRester(API_KEY) for system in allBinaries: results = m.get_data(system[0] + '-' + system[1], data_type='vasp') # Download DFT data for each binary system for material in results: # We will receive many compounds within each binary system if material['e_above_hull'] < 1e-6: # Check if this compound is thermodynamically stable dat = [material['pretty_formula'], material['band_gap'], material['formation_energy_per_atom'], material['density']] df = df.append(pd.Series(dat), ignore_index=True) df.columns = ['materials', 'bandgaps', 'formenergies', 'densities'] print df[0:2] print df.columns MAX_Z = 100 # maximum length of vector to hold naive feature set def naive_vectorize(composition): """
from pymatgen.io.cif import CifWriter import csv import os import json import math if __name__ == "__main__": MAPI_KEY = "h9GBsMfA1JvXbC7n" # You must change this to your Materials API key! (or set MAPI_KEY env variable) QUERY = "mp-1180346" # change this to the mp-id of your compound of interest # QUERY = "TiO" # change this to a formula of interest # QUERY = "Ti-O" # change this to a chemical system of interest mpr = MPRester(MAPI_KEY) # object for connecting to MP Rest interface # all information 2+2 warning label result = mpr.get_data("mp-9272") print(result) print() result = mpr.get_data("mp-571420") print(result) print() result = mpr.get_data("mp-1094115") print(result) print() result = mpr.get_data("mp-1180226") print(result) print() # structures = mpr.get_structures(QUERY) # for s in structures: # print(s)