Spaces:
Runtime error
Runtime error
File size: 9,169 Bytes
28dae5c |
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 303 304 |
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import warnings
warnings.filterwarnings("ignore")
from PIL import Image
import base64
import pandas as pd
import streamlit as st
import pickle
from rdkit import Chem
from rdkit.Chem import AllChem
from sklearn.ensemble import RandomForestRegressor
#For KERAS
import random
import numpy as np
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.metrics import mean_squared_error
import time
import numpy
from sklearn.model_selection import GridSearchCV
import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
#from keras.layers import Dense
#from keras.layers import Dropout
# Function to create model, required for KerasClassifier
def create_model(optimizer='RMSprop', learn_rate=0.1, momentum=0.4, activation='sigmoid', dropout_rate=0.0):
keras_model = Sequential()
keras_model.add(Dense(128, input_dim=train_encoded.shape[1], activation=activation))
keras_model.add(Dropout(dropout_rate))
keras_model.add(Dense(32, activation=activation))
keras_model.add(Dropout(dropout_rate))
keras_model.add(Dense(8,activation=activation))
keras_model.add(Dropout(dropout_rate))
keras_model.add(Dense(1,activation='linear'))
keras_model.summary()
# Compile model
keras_model.compile(loss='mean_squared_error', optimizer=optimizer)
return keras_model
######################
# Custom function
######################
## Calculate molecular descriptors
def get_ecfc(smiles_list, radius=2, nBits=2048, useCounts=True):
"""
Calculates the ECFP fingerprint for given SMILES list
:param smiles_list: List of SMILES
:type smiles_list: list
:param radius: The ECPF fingerprints radius.
:type radius: int
:param nBits: The number of bits of the fingerprint vector.
:type nBits: int
:param useCounts: Use count vector or bit vector.
:type useCounts: bool
:returns: The calculated ECPF fingerprints for the given SMILES
:rtype: Dataframe
"""
ecfp_fingerprints=[]
erroneous_smiles=[]
for smiles in smiles_list:
mol=Chem.MolFromSmiles(smiles)
if mol is None:
ecfp_fingerprints.append([None]*nBits)
erroneous_smiles.append(smiles)
else:
mol=Chem.AddHs(mol)
if useCounts:
ecfp_fingerprints.append(list(AllChem.GetHashedMorganFingerprint(mol, radius, nBits)))
else:
ecfp_fingerprints.append(list(AllChem.GetMorganFingerprintAsBitVect(mol, radius, nBits).ToBitString()))
# Create dataframe of fingerprints
df_ecfp_fingerprints = pd.DataFrame(data = ecfp_fingerprints, index = smiles_list)
# Remove erroneous data
if len(erroneous_smiles)>0:
print("The following erroneous SMILES have been found in the data:\n{}.\nThe erroneous SMILES will be removed from the data.".format('\n'.join(map(str, erroneous_smiles))))
df_ecfp_fingerprints = df_ecfp_fingerprints.dropna(how='any')
return df_ecfp_fingerprints
## generate dataset it is diffrent from origin one
import deepchem as dc
from deepchem.models import GraphConvModel
def generate(SMILES, verbose=False):
featurizer = dc.feat.ConvMolFeaturizer()
gcn = featurizer.featurize(SMILES)
properties = [random.randint(-1,1)/100 for i in range(0,len(SMILES))]
dataset = dc.data.NumpyDataset(X=gcn, y=np.array(properties))
return dataset
######################
# Page Title
######################
st.write("""# Accelerated reaction energy prediction for redox batteries 🧪 """)
st.write('By: [Alishba Imran](https://www.linkedin.com/in/alishba-imran-/)')
#%%
# About PART
about_part = st.expander("Learn More About Project", expanded=False)
with about_part:
st.write('''
#### About
Redox flow batteries (RFB) are widely being explored as a class of electrochemical energy storage devices for large-scale energy storage applications. Redox flow batteries convert electrical energy to chemical energy via electrochemical reactions (through reversible oxidation and reduction) of compounds.
To develop next-gen redox flow batteries with high cycle life and energy density, we need to speed up the discovery of electroactive materials with desired properties. This process can currently be very slow and expensive given how large and diverse the chemical space of the candidate compounds is.
Using an attention-based graph convolutional neural network technique, I've developed a model that can take in reactants as SMILEs and predict the reaction energy in the redox reaction.
A lot of this work was inspired and built on top of the paper [here](https://chemrxiv.org/engage/chemrxiv/article-details/60c7575f469df44a40f45465). Feel free to give it a try and reach out for any feedback. Email: alishbai734@gmail.com.
''')
st.write('**Insert your SMILES**')
st.write('Type any SMILES used as a reactant in the redox reaction. This model will output the reaction energy.')
## Read SMILES input
SMILES_input = "Oc1cccc(c12)c(O)c(nn2)O\nc1cccc(c12)cc(nn2)O\nOc1c(O)ccc(c12)cc(nn2)O"
SMILES = st.text_area('press ctrl+enter to run model!', SMILES_input, height=20)
SMILES = SMILES.split('\n')
SMILES = list(filter(None, SMILES))
# st.header('Input SMILES')
# SMILES[1:] # Skips the dummy first item
# Use only top 1000
if len(SMILES)>1000:
SMILES=SMILES[0:1000]
## Calculate molecular descriptors
ecfc_encoder = get_ecfc(SMILES)
#Import pretrained models
#---------------------------------------------------------------------------------
### generate dataset from SMILES and function generate
generated_dataset = generate(SMILES)
### transformer for gcn
filename = 'final_models/transformers.pkl'
infile = open(filename,'rb')
transformers = pickle.load(infile)
infile.close()
## model for gcn
model_dir = 'final_models/tf_chp_initial'
gcne_model = dc.models.GraphConvModel(n_tasks=1, batch_size=100, mode='regression', dropout=0.25,model_dir= model_dir,random_seed=0)
gcne_model.restore('final_models/tf_chp_initial/ckpt-94/ckpt-197')
#print(gcne_model)
## predict energy from gcn model
pred_gcne = gcne_model.predict(generated_dataset, transformers)
#---------------------------------------------------------------------------------
##keras model load
from keras.models import model_from_json
keras_final_model = model_from_json(open('./final_models/keras_final_model_architecture.json').read())
keras_final_model.load_weights('./final_models/keras_final_model_weights.h5')
#keras_final_model = pickle.load(open(r'./final_models/keras_final_model.txt', "rb"))
rf_final_model = pickle.load(open(r'./final_models/rf_final_model.txt', "rb"))
#xgbm_final_model = pickle.load(open(r'.\final_models\xgbm_final_model.txt', "rb"))
#predict test data (Keras,RF, GCN)
pred_keras = keras_final_model.predict(ecfc_encoder)
pred_rf = rf_final_model.predict(ecfc_encoder)
##reshape (n,) ----> (n,1)
pred_rf_r = pred_rf.reshape((len(pred_rf),1))
#pred_xgb = xgbm_final_model.predict(ecfc_encoder)
#calculate consensus
pred_consensus = (pred_keras + pred_gcne + pred_rf)/3
# predefined_models.get_errors(test_logS_list,pred_enseble)
#%% Weighted
#------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------
from sklearn.metrics import mean_absolute_error,mean_squared_error,r2_score
## Test 1 Experiments
test1_mae = []
test1_mae.append(0.00705) # 0 - GCN
test1_mae.append(0.00416) # 1 - Keras
test1_mae.append(0.0035) # 3 - RF
## Test 2 Experiments
test2_mae = []
test2_mae.append(0.00589) # 0 - GCN
test2_mae.append(0.00483) # 1 - Keras
test2_mae.append(0.00799) # 3 - RF
weighted_pred_0_1_3=( np.power(2/(test1_mae[0]+test2_mae[0]),3) * pred_gcne +
np.power(2/(test1_mae[1]+test2_mae[1]),3) * pred_keras +
np.power(2/(test1_mae[2]+test2_mae[2]),3) * pred_rf_r ) / (
np.power(2/(test1_mae[0]+test2_mae[0]),3) + np.power(2/(test1_mae[1]+test2_mae[1]),3) + np.power(2/(test1_mae[2]+test2_mae[2]),3))
#--------
#### ???? array shape not correct and no difference with pred_consensus
pred_weighted = (pred_gcne + pred_keras + pred_rf_r)/3
#%%
# results=np.column_stack([pred_mlp,pred_xgb,pred_rf,pred_consensus])
df_results = pd.DataFrame(SMILES, columns=['SMILES Reactant'])
df_results["Predicted Reaction Energy"]= weighted_pred_0_1_3
#df_results["reaction_energy"]= pred_weighted
df_results=df_results.round(6)
# df_results.to_csv("results/predicted-"+test_data_name+".csv",index=False)
# Results DF
st.header('Prediction of Reaction Energy for RFB')
df_results # Skips the dummy first item
# download=st.button('Download Results File')
# if download:
csv = df_results.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # some strings
|