-
Notifications
You must be signed in to change notification settings - Fork 3
/
predict.py
231 lines (163 loc) · 6.86 KB
/
predict.py
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
import tensorflow as tf
from tensorflow.python.client import device_lib
from tensorflow.keras import backend as K
from tensorflow.keras.models import Model
from tensorflow.keras import optimizers
from pymatgen import Composition
import pandas as pd
from utils import *
import featurizer
from featurizer import *
import numpy as np
import joblib
import argparse
import sys
import warnings
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser(description='SC_model'
'')
parser.add_argument('--crystal', default=0)
parser.add_argument('--type', choices=['formula', 'cif'],
default='formula')
def main():
args = parser.parse_args(sys.argv[1:])
if args.type == 'cif':
print('---------Loading Model and Predicting---------------')
model = tf.keras.models.load_model('./trained_models/SC_realspace')
predict_list = [args.crystal]
predict_crystals = crystal_represent_3(predict_list)
a = np.stack(predict_crystals, axis=0)
X = a
X = pad(X, 2)
y_result = model.predict(X)
print('')
print('---------Printing Result---------------')
crystal_ = Structure.from_file(args.crystal)
formula = crystal_.composition.reduced_formula
print('The SC for CIF[{}] is {}'.format(formula,y_result[0]))
print('')
elif args.type == 'formula':
print('---------Loading Model and Predicting---------------')
model = tf.keras.models.load_model('./trained_models/SC_atomic')
predict_list = [args.crystal]
predict_crystals = atomic_represent2(predict_list)
a = np.stack(predict_crystals, axis=0)
X = a
X = pad(X, 2)
y_result = model.predict(X)
print('')
print('---------Printing Result---------------')
print('The atomic SC for [{}] is {}'.format(predict_list[0],y_result[0]))
print('')
def atomic_represent2(crystal_list, num_ele=3, num_sites=20):
Element = joblib.load('./files/element.pkl')
df1 = pd.read_csv('files/atomic_features.csv')
E_v = np_utils.to_categorical(np.arange(0, len(Element), 1))
elem_embedding_file = './files/atom_init.json'
with open(elem_embedding_file) as f:
elem_embedding = json.load(f)
elem_embedding = {int(key): value for key, value
in elem_embedding.items()}
feat_cgcnn = []
for key, value in elem_embedding.items():
feat_cgcnn.append(value)
feat_cgcnn = np.array(feat_cgcnn)
# start featurization
atomic = []
for x in range(len(crystal_list)):
crystal = Composition(crystal_list[x])
size = len(crystal.elements)
z_u = np.array([crystal.elements[i].number for i in range(size)])
onehot = np.zeros((num_ele, len(E_v)))
onehot[:len(z_u), :] = E_v[z_u - 1, :]
coeffs_crsytal = np.zeros((num_ele, feat_cgcnn.shape[1]))
for i in range(len(z_u)):
coeffs_crsytal[i, :] = feat_cgcnn[z_u[i] - 1, :]
dic = crystal.get_el_amt_dict()
ratio_ = np.array([dic[crystal.elements[i].symbol] for i in range(size)])
ratio_ /= crystal._natoms
ratio = np.zeros((num_ele, 1))
ratio[:len(z_u), 0] = ratio_
ratio = ratio.reshape(1, num_ele)
ratio1 = ratio * crystal._natoms
# real space represeatnion
atom_list = np.concatenate(
((onehot.T * ratio).T, ratio1.T, ratio.T, np.zeros((num_ele, 1)), (coeffs_crsytal.T * ratio).T), axis=1)
atom_list = atom_list.T
atomic.append(atom_list)
return atomic
def crystal_represent_3(cif_list, num_ele=3, num_sites=20):
Element = joblib.load('./files/element.pkl')
E_v = np_utils.to_categorical(np.arange(0, len(Element), 1))
df1 = pd.read_csv('files/atomic_features.csv')
elem_embedding_file = './files/atom_init.json'
with open(elem_embedding_file) as f:
elem_embedding = json.load(f)
elem_embedding = {int(key): value for key, value
in elem_embedding.items()}
feat_cgcnn = []
for key, value in elem_embedding.items():
feat_cgcnn.append(value)
feat_cgcnn = np.array(feat_cgcnn)
# start featurization
ftcp = []
for idx in range(len(cif_list)): # 46382
crystal = Structure.from_file(cif_list[idx])
latt = crystal.lattice
ui, ux, uy = np.unique(crystal.atomic_numbers, return_index=True, return_inverse=True)
z_sorted = np.array(crystal.atomic_numbers)
z_u = z_sorted[np.sort(ux)]
onehot = np.zeros((num_ele, len(E_v)))
onehot[:len(z_u), :] = E_v[z_u - 1, :]
fc1 = np.zeros((num_sites, 3))
fc1_ind = np.zeros((num_sites, num_ele))
# Fourier space, 1.2 is used at the maximum distance
recip_latt = latt.reciprocal_lattice_crystallographic
recip_pts = recip_latt.get_points_in_sphere([[0, 0, 0]], [0, 0, 0], 1.2)
zs = []
coeffs = []
fcoords = []
coords = []
occus = []
for site in crystal:
for sp, occu in site.species.items():
zs.append(sp.Z)
c = feat_cgcnn[sp.Z - 1, :]
coeffs.append(c)
fcoords.append(site.frac_coords)
occus.append(occu)
coords.append(site.coords)
zs = np.array(zs)
coeffs = np.array(coeffs)
coeffs_crsytal = np.zeros((num_ele, feat_cgcnn.shape[1]))
coeffs_crsytal[:len(z_u), :] = coeffs[np.sort(ux), :]
coords = np.array(coords)
fcoords = np.array(fcoords)
fc1[:fcoords.shape[0], :] = fcoords
occus = np.array(occus).reshape(-1, 1)
abc1 = np.asarray(latt.abc)
ang1 = np.asarray(latt.angles)
for i in range(len(z_u)):
fc1_ind[np.where(z_sorted == z_u[i]), i] = 1
crys_list = np.concatenate((abc1.reshape(-1, 1),
ang1.reshape(-1, 1), fc1.T), axis=1)
crys_list1 = np.zeros((num_ele, crys_list.shape[1]))
crys_list1[:crys_list.shape[0], :] = crys_list
# calculate the ratio of each element in a compound
ratio = np.sum(fc1_ind, axis=0)
ratio = ratio / np.sum(ratio)
ratio = ratio.reshape(1, 3)
# added elemental features (Raymond)
ele_feature = np.zeros((num_ele, 56))
for i, x in enumerate(z_u):
array = df1[df1['Number'] == x].to_numpy()[:, 1:]
if array.shape == (1, 56):
ele_feature[i, :] = array
# real space represeatnion
atom_list = np.concatenate((onehot, crys_list1, fc1_ind.T, np.zeros((num_ele, 1)), coeffs_crsytal), axis=1)
# atom_list = np.concatenate((onehot,ratio.T,np.zeros((num_ele,1)),coeffs_crsytal),axis=1)
atom_list = atom_list.T
ftcp.append(atom_list)
return ftcp
if __name__ == '__main__':
main()