File size: 11,879 Bytes
59926cd 21c847b 2225ea4 9645cdc fab7c1f e81ff0f 7e00f8a 2225ea4 ed5bf3a 2225ea4 9bde84d 2225ea4 1648a83 2225ea4 ed5bf3a 2225ea4 ede9665 af0a77a 2225ea4 ede9665 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# from pip._internal import main
# main(['install', 'matminer'])
import subprocess
import sys
def install_package(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
install_package('matminer')
import os
import gradio as gr
from pandas import Series, DataFrame
import pandas as pd
import sys
import numpy as np
import os
from Interface.empirical_parameter_calculator import EmpiricalParams
from pymatgen.core.periodic_table import Element
# main(['install', 'joblib'])
# main(['install', 'scikit-learn==1.2.2'])
install_package('scikit-learn==1.2.2')
import joblib
'''
Get the path of all of the saved models.
'''
gmm_ssl_compressive_strength = "./Model/saved_model/gmm_ssl/compressive_strength.pkl"
gmm_ssl_elongation = "./Model/saved_model/gmm_ssl/elongation.pkl"
gmm_ssl_hardness ="./Model/saved_model/gmm_ssl/hardness.pkl"
gmm_ssl_plasticity = "./Model/saved_model/gmm_ssl/plasticity.pkl"
gmm_ssl_tensile_strength = "./Model/saved_model/gmm_ssl/tensile_strength.pkl"
gmm_ssl_yield_strength = "./Model/saved_model/gmm_ssl/yield_strength.pkl"
'''
Get the composition of input alloy.
'''
def normalize_molar_ratios(ratios):
normalized_ratios = list()
ele_sum = sum(ratios)
for ele in ratios:
ele = float(ele / ele_sum)
normalized_ratios.append(ele)
return normalized_ratios
'''
Load the saved ML models.
'''
def predict_input(path):
with open(path, 'rb') as p:
loaded_model = joblib.load(p)
return loaded_model
'''
Predict the six properties of the input alloy,
with Linear Regression Models, K-Means Semi-supervised Model and GMM Semi-supervised Model.
'''
def pred(Al, B, C, Co, Cr, Cu, Fe, Ga, Ge, Hf, Li, Mg, Mn, Mo, N, Nb, Ni, Sc, Si, Sn, Ta, Ti, V, W, Y, Zn, Zr, operation):
# Get the model acceptable composition format.
# print(B)
comp = {"Al": Al, "B": B, "C": C, "Co": Co, "Cr": Cr, "Cu": Cu, "Fe": Fe, "Ga": Ga, "Ge": Ge, "Hf": Hf, "Li": Li,
"Mg": Mg, "Mn": Mn,
"Mo": Mo, "N": N, "Nb": Nb, "Ni": Ni, "Sc": Sc, "Si": Si, "Sn": Sn, "Ta": Ta, "Ti": Ti, "V": V, "W": W,
"Y": Y, "Zn": Zn, "Zr": Zr}
df_values = normalize_molar_ratios(comp.values())
df = pd.DataFrame(data=[df_values],
columns=["Al", "B", "C", "Co", "Cr", "Cu", "Fe", "Ga", "Ge", "Hf", "Li", "Mg", "Mn", "Mo", "N",
"Nb", "Ni",
"Sc", "Si", "Sn", "Ta", "Ti", "V", "W", "Y", "Zn", "Zr"])
# print(df)
Composition = ""
index = 0
for k, v in comp.items():
if v != 0:
Composition = Composition + k + str(round(df_values[index], 2))
index += 1
# print(Composition)
# print(comp.values())
# Using semi_supervisor Label Propagation to predict properties.
Hardness = predict_input(gmm_ssl_hardness).predict(df)
YieldStrength = predict_input(gmm_ssl_yield_strength).predict(df)
TensileStrength = predict_input(gmm_ssl_tensile_strength).predict(df)
Elongation = predict_input(gmm_ssl_elongation).predict(df)
CompressiveStrength = predict_input(gmm_ssl_compressive_strength).predict(df)
Plasticity = predict_input(gmm_ssl_plasticity).predict(df)
Hardness = round(float(Hardness),2)
YieldStrength = round(float(YieldStrength),2)
TensileStrength = round(float(TensileStrength),2)
Elongation = round(float(Elongation),2)
CompressiveStrength = round(float(CompressiveStrength),2)
Plasticity = round(float(Plasticity),2)
return Composition, Hardness, YieldStrength, TensileStrength, Elongation, CompressiveStrength, Plasticity
'''
Import the function to Calculate the empirical parameters.
'''
def empirical_parameter(Al, B, C, Co, Cr, Cu, Fe, Ga, Ge, Hf, Li, Mg, Mn, Mo, N, Nb, Ni, Sc, Si, Sn, Ta, Ti, V, W, Y, Zn, Zr):
# Get the model acceptable composition format.
# print(B)
comp = {"Al": Al, "B": B, "C": C, "Co": Co, "Cr": Cr, "Cu": Cu, "Fe": Fe, "Ga": Ga, "Ge": Ge, "Hf": Hf, "Li": Li,
"Mg": Mg, "Mn": Mn,
"Mo": Mo, "N": N, "Nb": Nb, "Ni": Ni, "Sc": Sc, "Si": Si, "Sn": Sn, "Ta": Ta, "Ti": Ti, "V": V, "W": W,
"Y": Y, "Zn": Zn, "Zr": Zr}
df_values = normalize_molar_ratios(comp.values())
print(df_values)
df_element=[]
df_ratio=[]
index = 0
for key, value in comp.items():
if int(value) != 0:
df_element.append(key)
df_ratio.append(df_values[index])
index += 1
print(df_element)
print(df_ratio)
df_elements = []
for i in df_element:
df_elements.append(Element[i])
print(df_elements)
input_ele = EmpiricalParams(element_list=df_elements,mol_ratio=df_ratio)
# 1. Calculate the entropy mixing.
para1 = round(float(input_ele.entropy_mixing()),2)
#2. Calculate the average atomic radius.
para2 = round(float(input_ele.mean_atomic_radius()),2)
#3. Calculate the atomic size difference.
para3 = round(float(input_ele.atomic_size_difference()),2)
#4. Calculate the enthalpy of mixing.
para4= round(float(input_ele.enthalpy_mixing()),2)
#5. Calculate the standard deviation of enthalpy.
para5= round(float(input_ele.std_enthalpy_mixing()),2)
#6. Calculate the average melting point.
para6= round(float(input_ele.average_melting_point()),2)
#7. Calculate the standard melting point.
para7= round(float(input_ele.std_melting_point()),2)
#8. Calculate the average electronegativity.
para8= round(float(input_ele.mean_electronegativity()),2)
#9. Calculate the standard deviation of electronegativity.
para9= round(float(input_ele.std_electronegativity()),2)
#10. Calculate the valence electron concentration.
para10= round(float(input_ele.average_vec()),2)
#11. Calculate the standard deviation of valence electron concentration.
para11= round(float(input_ele.std_vec()),2)
#12. Calculate the omega.
para12= round(float(input_ele.calc_omega()),2)
#13. Calculate the density.
para13= round(float(input_ele.calc_density()),2)
#14. Calculate the price.
para14= round(float(input_ele.calc_price()),2)
return para1, para2, para3, para4,para5, para6, para7, para8, para9, para10, para11, para12, para13, para14
'''
The function of Clear button.
'''
def clear_input():
return 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "",\
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
'''
Interface Details
'''
with gr.Blocks() as demo:
# The title and description of interface.
gr.Markdown("# Multi Principal Element Alloy Property Predictor")
gr.Markdown("Input alloy composition to obtain output of alloy properties (sum of composition should be equal to 100)")
# The section to input the element ratio.
with gr.Row():
Al = gr.Number(label="Al")
B = gr.Number(label="B")
C = gr.Number(label="C")
Co = gr.Number(label="Co")
Cr = gr.Number(label="Cr")
Cu = gr.Number(label="Cu")
Fe = gr.Number(label="Fe")
Ga = gr.Number(label="Ga")
Ge = gr.Number(label="Ge")
Hf = gr.Number(label="Hf")
Li = gr.Number(label="Li")
Mg = gr.Number(label="Mg")
Mn = gr.Number(label="Mn")
Mo = gr.Number(label="Mo")
N = gr.Number(label="N")
Nb = gr.Number(label="Nb")
Ni = gr.Number(label="Ni")
Sc = gr.Number(label="Sc")
Si = gr.Number(label="Si")
Sn = gr.Number(label="Sn")
Ta = gr.Number(label="Ta")
Ti = gr.Number(label="Ti")
V = gr.Number(label="V")
W = gr.Number(label="W")
Y = gr.Number(label="Y")
Zn = gr.Number(label="Zn")
Zr = gr.Number(label="Zr")
Composition = gr.Text(label="Alloy Composition")
#Action buttons.("Clear" and "Prediction")
with gr.Row():
clear = gr.Button("Clear")
submit = gr.Button("Predict")
#The prediction result(Six mechanical properties) of ML models.
with gr.Row():
Hardness = gr.Number(label="Hardness (VHN)")
YieldStrength = gr.Number(label="Yield Strength (MPa)")
TensileStrength = gr.Number(label="Tensile Strength (MPa)")
Elongation = gr.Number(label="Elongation (%)")
CompressiveStrength = gr.Number(label="Compressive Strength (MPa)")
Plasticity = gr.Number(label="Plasticity (from compression)")
#Calculer resutl of empirical parameters.
with gr.Row():
entropy_mixing = gr.Number(label="Entropy of Mixing (J/K*mol)")
average_atomic_radius = gr.Number(label="Average Atomic Radius (Angstroms)")
atomic_size_dif = gr.Number(label="Atomic Size Difference")
enthalpy_mixing = gr.Number(label="Enthalpy of Mixing (kJ/mol)")
std_deviation_enthalpy = gr.Number(label="Standard Deviation of Enthalpy")
average_melting_point = gr.Number(label="Average Melting Point (Tm, in Celcius)")
std_deviation_melting_point = gr.Number(label="Standard Deviation of Melting Point")
average_electronegativity = gr.Number(label="Average Electronegativity (X)")
std_deviation_electronegativity= gr.Number(label="Standard Deviation of Electronegativity")
valence_electron_concentration = gr.Number(label="Valence Electron Concentration (VEC)")
std_deviation_valence_electron_concentration = gr.Number(label="Standard Deviation of Valence Electron Concentration (VEC)")
omega = gr.Number(label="The Unitless Parameter Omega")
density = gr.Number(label="Density (g/cm^3)")
price = gr.Number(label="Price (USD/kg)")
# Define the action of "Clear" button.
clear.click(fn=clear_input, inputs=[],
outputs=[Al, B, C, Co, Cr, Cu, Fe, Ga, Ge, Hf, Li, Mg, Mn, Mo, N, Nb, Ni, Sc, Si, Sn, Ta, Ti, V, W, Y,
Zn, Zr, Composition,
Hardness, YieldStrength, TensileStrength, Elongation, CompressiveStrength, Plasticity, entropy_mixing, average_atomic_radius, atomic_size_dif, enthalpy_mixing,
std_deviation_enthalpy, average_melting_point, std_deviation_melting_point,
average_electronegativity, std_deviation_electronegativity, valence_electron_concentration,
std_deviation_valence_electron_concentration, omega, density, price])
# Define the action of "Predict" button.
# 1.Predict the result of "Composition", "Hardness", "YieldStrength", "TensileStrength", "Elongation", "CompressiveStrength", "Plasticity".
submit.click(fn=pred,
inputs=[Al, B, C, Co, Cr, Cu, Fe, Ga, Ge, Hf, Li, Mg, Mn, Mo, N, Nb, Ni, Sc, Si, Sn, Ta, Ti, V, W, Y,
Zn, Zr],
outputs=[Composition, Hardness, YieldStrength, TensileStrength, Elongation, CompressiveStrength,
Plasticity])
# 2.Activate the empirical parameter calculator.
submit.click(fn=empirical_parameter,
inputs=[Al, B, C, Co, Cr, Cu, Fe, Ga, Ge, Hf, Li, Mg, Mn, Mo, N, Nb, Ni, Sc, Si, Sn, Ta, Ti, V, W, Y,
Zn, Zr],
outputs=[entropy_mixing, average_atomic_radius, atomic_size_dif, enthalpy_mixing,
std_deviation_enthalpy, average_melting_point, std_deviation_melting_point,
average_electronegativity, std_deviation_electronegativity, valence_electron_concentration,
std_deviation_valence_electron_concentration, omega, density, price])
'''
Launch the interface.
'''
if __name__ == "__main__":
# Run the Alloy Property Predictor Interface, without public URL.
demo.launch()
# Run the Alloy Property Predictor Interface, with a public URL.
# demo.launch(share="True")
|