def get_parameters(formula): """ make parameters from chemical formula Parameters ---------- formula : string chemical formula Returns ------- array-like, shage = [2 * numbers of atom] atomic number Z, numbers of atom """ material = Composition(formula) features = [] atomicNo = [] natom = [] for element in material: natom.append( material.get_atomic_fraction(element) * material.num_atoms) atomicNo.append(float(element.Z)) """ ex) formula : 'H3S' material = 'H3' , 'S' 1回目. material.get_atomic_fraction(H3) : 0.75, material.num_atoms : 4 2回目. material.get_atomic_fraction(S) : 0.25, material.num_atoms : 4 rslt) natom : [3.0, 1.0] atomicNo : [1.0, 16.0] """ features.extend(atomicNo) features.extend(natom) return features
def get_parameters(formula): """ make parameters from chemical formula Parameters ---------- formula : string chemical formula Returns ------- array-like, shape = [2*numbers of atom] atomic number Z, numbers of atom """ material = Composition(formula) features = [] atomicNo = [] natom = [] for element in material: natom.append( material.get_atomic_fraction(element) * material.num_atoms) atomicNo.append(float(element.Z)) features.extend(atomicNo) features.extend(natom) return features
def get_parameters(formula): material = Composition(formula) features = [] atomicNo = [] natom = [] for element in material: natom.append(material.get_atomic_fraction(element)*material.num_atoms) atomicNo.append(float(element.Z)) features.extend(atomicNo) features.extend(natom) return features
def naive_vectorize(comp): """naive_vectorize Converts a chemical composition into numbers based on atomic fractions. :param comp: Composition :return: A vector based on the composition's stoichiometry. """ comp = Composition(comp) vector = np.zeros(MAX_VECTOR) for el in comp: frac = comp.get_atomic_fraction(el) vector[el.Z - 1] = frac # Python indexing starts at 0, science doesn't return vector
def get_element_density(mt): """Return a vector in which the elements are corresponding to its atomic fraction. """ fraction_matrix = zeros(100) composition = Composition(mt['pretty_formula']) for element in composition: fraction = composition.get_atomic_fraction( element) # get the atomic fraction. fraction_matrix[element.Z] = fraction return fraction_matrix
def get_row_group_density(mt, rg_limits): """Return the atomic fraction corresponding to row and group position.""" fraction_matrix = zeros(rg_limits) composition = Composition(mt['pretty_formula']) for element in composition: fraction = composition.get_atomic_fraction(element) elem = Element(element) row = elem.row group = elem.group fraction_matrix[row - 1][group - 1] = fraction return fraction_matrix
def get_parameters(formula): from mendeleev import element atomn = [] natom = [] amass = [] eion1 = [] aradi = [] dense = [] elaff = [] meltp = [] therm = [] nvale = [] material = Composition(formula) for x in material: atomn.append(float(x.Z)) natom.append(material.get_atomic_fraction(x) * material.num_atoms) x = element(str(x)) # if(x.electron_affinity==None): # x.electron_affinity = 0.1 # else: # x.electron_affinity = abs(x.electron_affinity)+2.5 if (x.thermal_conductivity == None): x.thermal_conductivity = 0.1 if (x.density == None): x.density = 0.1 amass.append(x.mass) # AMU eion1.append( x.ionenergies[1]) # a dictionary with ionization energies in eV aradi.append(x.atomic_radius_rahm) # Atomic radius by Rahm et al. pm dense.append(x.density) # g/cm3 elaff.append(x.electron_affinity) # eV meltp.append(x.melting_point) # Kelvin therm.append(x.thermal_conductivity) # W /(m K) nvale.append(x.nvalence()) # no unit features = [] pplist = [amass, eion1, aradi, dense, elaff, meltp, therm, nvale] pplist = [amass, eion1, aradi, meltp, nvale] for pp in pplist: params = calc_parameters(*natom, *pp) features += list(params) return features
def get_stoichiometry_score(shelx_file, cache): """ Calculate a score for the given shelx file based on how well the composition agrees with the nominal formula Bounds for this score are 0 to 2.0 (maximum difference between two vectors of length 1). :param shelx_file: SHELX file :param cache: OptimizerCache object :return: stoichiometry agreement score """ analytic_formula = Composition(shelx_file.get_analytic_formula()) score = 0.0 for el in cache.element_list: el_name = el.get_name(True) diff = abs( cache.nominal_formula.get_atomic_fraction(el_name) - analytic_formula.get_atomic_fraction(el_name)) if diff > 0.05: score += diff return score
def get_simplified_energy_for_others(ps_dict, atomtable): #initialize _ = [] #Coulomb Matrix output for multi-dimensional representation matrix_1d = [] import collections derv_properties = collections.OrderedDict() cif_properties = collections.OrderedDict() ########################################## #ACTION ########################################## ## derived properties from derv function derv_properties_others = getProperties_from_dict(ps_dict, atomtable) #derv_volume = {"volume": derv.getUnitCellVolume(ps.lattice)} #derv_density = {"density": derv.getUnitCellDensity(derv_properties_others['sum_mass'], derv_volume['volume'])} from pymatgen import Composition comp = Composition(ps_dict) derv_nelements = {"nelements": comp.to_data_dict["nelements"]} derv_nsites = {"nsites": sum(ps_dict.values())} derv_properties.update(derv_properties_others) #derv_properties.update(derv_volume) #derv_properties.update(derv_density) derv_properties.update(derv_nelements) derv_properties.update(derv_nsites) #add electronegativity derv_avg_electroneg = {"electronegativity": comp.average_electroneg} derv_properties.update(derv_avg_electroneg) ########################################## ## derived properties from cif function (CM) ps_frac = {} ## 20170904 added by JClee ################ for el in ps_dict.keys(): ps_frac[el] = comp.get_atomic_fraction(el) derv_properties.update(ps_frac) return derv_properties
def parse_composition(structure_type, s, ctype): toks = s.strip().split() if len(toks) == 1: c = Composition({toks[0].split(":")[0]: 1}) else: c = Composition( {t.split(":")[0]: float(t.split(":")[1]) for t in toks}) c = Composition({k2: v2 / sum(c.values()) for k2, v2 in c.items()}) if len(c) != 2: raise ValueError("Bad composition on %s." % ctype) frac = [c.get_atomic_fraction(k) for k in c.keys()] if structure_type == 'garnet': if ctype == "A": if abs(frac[0] - 0.5) > 0.01: raise ValueError("Bad composition on %s. " "Only 1:1 mixing allowed!" % ctype) elif ctype in ["C", "D"]: if not (abs(frac[0] - 1.0 / 3) < 0.01 or abs(frac[1] - 1.0 / 3) < 0.01): raise ValueError("Bad composition on %s. " "Only 2:1 mixing allowed!" % ctype) elif structure_type == 'perovskite': if abs(frac[0] - 0.5) > 0.01: raise ValueError("Bad composition on %s. " "Only 1:1 mixing allowed!" % ctype) try: for k in c.keys(): k.oxi_state if k not in ELS[structure_type][ctype]: raise ValueError("%s is not a valid species for %s site." % (k, ctype)) except AttributeError: raise ValueError("Oxidation states must be specified for all species!") return c
for i in range(MAX_Z): element = Element.from_Z(i + 1) print(element.symbol + ': ' + str(linear.coef_[i])) ####To be continued # more physically-motivated physicalFeatures = [] for material in materials: theseFeatures = [] fraction = [] atomicNo = [] eneg = [] group = [] for element in material: fraction.append(material.get_atomic_fraction(element)) atomicNo.append(float(element.Z)) eneg.append(element.X) group.append(float(element.group)) # We want to sort this feature set # according to which element in the binary compound is more abundant mustReverse = False if fraction[1] > fraction[0]: mustReverse = True for features in [fraction, atomicNo, eneg, group]: if mustReverse: features.reverse() theseFeatures.append(fraction[0] / fraction[1])
# {{{ trainFile = open("tc.csv","r").readlines() tc = [] pf = [] for line in trainFile: split = str.split(line, ',') material = Composition(split[0]) pressure = float(split[4]) tc.append(float(split[8])) features = [] atomicNo = [] natom = [] for element in material: natom.append(material.get_atomic_fraction(element)*material.num_atoms) atomicNo.append(float(element.Z)) features.extend(atomicNo) features.extend(natom) features.append(pressure) pf.append(features[:]) # }}} # # set X_train, y_train, X_test # {{{ X = pf[:] y = tc[:] X_train = X[:] y_train = y[:] materials = []
x = self.fc1(x) x = self.relu(x) x = self.fc2(x) return x if __name__ == '__main__': API_KEY = 'tvLpmn5hMTXsVy8G' # You have to register with Materials Project to receive an API csv_file_path = 'storage/bandgapDFT.csv' df = load_csv(csv_file_path) print(df) composition = Composition(df[0][0]) for element in composition: print(composition.get_atomic_fraction(element)) materials = df.loc[:, 0].values.tolist() materials = list(map(lambda m: Composition(m), materials)) target_band_gap = df.loc[:, 1].values y_data = np.array(list(map(lambda t: [t], target_band_gap)), np.float32) print(y_data) x_data = get_feature_vec(materials) pca = PCA(n_components=2) pca.fit(x_data) Xd = pca.transform(x_data) print(Xd) #plt.scatter(Xd[:, 0], Xd[:, 1]) #plt.show()
############################################################################################################## # Create alternative feature set that is more physically-motivated physicalFeatures = [] for material in materials: theseFeatures = [] fraction = [] atomicNo = [] eneg = [] group = [] for element in material: fraction.append(material.get_atomic_fraction(element)) atomicNo.append(float(element.Z)) eneg.append(element.X) group.append(float(element.group)) # We want to sort this feature set # according to which element in the binary compound is more abundant mustReverse = False if fraction[1] > fraction[0]: mustReverse = True for features in [fraction, atomicNo, eneg, group]: if mustReverse: features.reverse() theseFeatures.append(fraction[0] / fraction[1])
materials = [] tc = [] pf= [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]] npf=23 npf+=1 for line in trainFile: split = str.split(line, ',') material = Composition(split[0]) pressure = float(split[4]) tc.append(float(split[8])) features = [] feature = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]] for element in material: feature[ 1].append(float(element.Z)) feature[ 2].append(material.get_atomic_fraction(element)*material.num_atoms) feature[ 3].append(float(element.group)) feature[ 4].append(float(element.row)) feature[ 5].append(element.X) feature[ 6].append(float(element.max_oxidation_state)) feature[ 7].append(float(element.min_oxidation_state)) feature[ 8].append(float(str(element.atomic_mass).split("a")[0])) feature[ 9].append(float(element.mendeleev_no)) feature[10].append(float(str(element.melting_point).split("K")[0])) feature[11].append(float(str(element.molar_volume).split("c")[0])) feature[12].append(float(str(element.thermal_conductivity).split("W")[0])) feature[13].append(element.is_noble_gas) feature[14].append(element.is_transition_metal) feature[15].append(element.is_rare_earth_metal) feature[16].append(element.is_metalloid) feature[17].append(element.is_alkali)
def atomic_fractions(self, composition: Composition): return [ composition.get_atomic_fraction(e) for e in self.cpd.vertex_elements ]
#!/usr/bin/env python # -*- coding: utf-8 -*- import math from pymatgen import Element from pymatgen import Composition comp = Composition("LiFePO4") print(comp.num_atoms) print(comp.formula) print(comp.get_atomic_fraction(Element("Li")))
trainFile = open("tc.csv", "r").readlines() tc = [] pf = [] for line in trainFile: split = str.split(line, ',') material = Composition(split[0]) pressure = float(split[4]) tc.append(float(split[8])) features = [] atomicNo = [] natom = [] for element in material: natom.append( material.get_atomic_fraction(element) * material.num_atoms) atomicNo.append(float(element.Z)) features.extend(atomicNo) features.extend(natom) features.append(pressure) pf.append(features[:]) X = pf[:] y = tc[:] import numpy as np import pandas as pd Z = np.array(X) def read_file(name): data = np.array(pd.read_csv(filepath_or_buffer=name, header=None,
from time import time start = time() for i in range(len(X)): material = Composition(X[i]) natom = [] amass = [] eion1 = [] aradi = [] dense = [] elaff = [] meltp = [] therm = [] nvale = [] lcalc = True for x in material: natom.append(material.get_atomic_fraction(x)*material.num_atoms) x = element(str(x)) if(x.electron_affinity==None): lcalc = False else: x.electron_affinity += 2.5 if(x.thermal_conductivity==None): lcalc = False if(x.density==None): lcalc = False if(lcalc): amass.append(x.mass) # AMU eion1.append(x.ionenergies[1]) # a dictionary with ionization energies in eV aradi.append(x.atomic_radius_rahm) # Atomic radius by Rahm et al. pm dense.append(x.density) # g/cm3 elaff.append(x.electron_affinity) # eV