-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeat.py
171 lines (144 loc) · 4.88 KB
/
feat.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
import numpy as np
import math
def pad_array(x, shape, fill=0, both=False):
x = np.asarray(x)
if not isinstance(shape, tuple):
shape = tuple(shape for _ in range(x.ndim))
pad = []
for i in range(x.ndim):
diff = shape[i] - x.shape[i]
assert diff >= 0
if both:
a, b = divmod(diff, 2)
b += a
pad.append((a, b))
else:
pad.append((0, diff))
pad = tuple(pad)
x = np.pad(x, pad, mode='constant', constant_values=fill)
return x
def coulomb_matrix(molecule):
numAtoms = len(molecule["atoms"])
matrix = np.zeros((numAtoms, numAtoms))
for i in range(numAtoms):
for j in range(numAtoms):
atomNumI = molecule["atoms"][i]["num"]
atomNumJ = molecule["atoms"][j]["num"]
if i == j:
matrix[i, j] = 0.5 * atomNumI**2.4
elif i < j:
d = calculate_atom_distance(molecule["atoms"][i]["position"], molecule["atoms"][j]["position"])
# Convert AtomPositions from Angstrom to bohr (atomic units)
d = d / (0.52917721092)
matrix[i, j] = (atomNumI * atomNumJ) / d
matrix[j, i] = matrix[i, j]
else:
continue
return matrix
def calculate_atom_distance(p1, p2):
return calculateDistance(p1, p2)
def calculateDistance(p1, p2):
distance = None
squared_deltas = []
for i in range(len(p1)):
delta = math.fabs(np.subtract(p1[i], p2[i]))
delta_squared = delta * delta
squared_deltas.append(delta_squared)
delta_sums = 0
for delta in squared_deltas:
delta_sums += delta
return math.sqrt(delta_sums)
"""
coulomb maxtrix eigin value based
"""
def calculate_eigenvalues(molecule, max_num_atoms):
cmat = coulomb_matrix(molecule)
w, v = np.linalg.eig(cmat)
w_abs = np.abs(w)
sortidx = np.argsort(w_abs)
sortidx = sortidx[::-1]
w = w[sortidx]
f = pad_array(w, max_num_atoms)
return f
"""
Randomize a Coulomb matrix
"""
def randomize_coulomb_matrix(m, numSamples=1):
"""
Randomize a Coulomb matrix as decribed in Montavon et al., _New Journal
of Physics_ __15__ (2013) 095003:
1. Compute row norms for M in a vector row_norms.
2. Sample a zero-mean unit-variance noise vector e with dimension
equal to row_norms.
3. Permute the rows and columns of M with the permutation that
sorts row_norms + e.
Parameters
----------
m : ndarray
Coulomb matrix.
n_samples : int, optional (default 1)
Number of random matrices to generate.
seed : int, optional
Random seed. """
rval = []
row_norms = np.asarray([np.linalg.norm(row) for row in m], dtype=float)
rng = np.random.RandomState(0)
for i in range(numSamples):
e = rng.normal(size=row_norms.size)
p = np.argsort(row_norms + e)
#p = np.flip(p, 0) # reverse the order
new = m[p][:, p] # permute rows first, then columns
rval.append(new)
return rval
"""
directly use coulomb maxtrix as featurization
"""
def feat_coulombMatrix(molecule, max_atoms):
m = coulomb_matrix(molecule)
row_norms = np.asarray([np.linalg.norm(row) for row in m], dtype=float)
p = np.argsort(row_norms)
m = m[p][:, p] # permute rows first, then columns
m = pad_array(m, max_atoms)
rval = m[np.triu_indices_from(m)]
# flatten into one list
rval = np.asarray(np.ravel(rval))
return rval
"""
use randomnized coulomb maxtrix as featurization
"""
def feat_random_coulombMatrix(molecule, max_atoms, numSamples=1):
origMatrix = coulomb_matrix(molecule)
rval = []
for m in randomize_coulomb_matrix(origMatrix, numSamples):
m = pad_array(m, max_atoms)
rval.append(m[np.triu_indices_from(m)])
# flatten into one list
rval = np.asarray(np.ravel(rval))
return rval
def featurize(molecules, coulomb_eigen=True, coulomb_random_samples=0):
""" coulomb matrix based featurization
Parameters
-----------
molecules: dictionary based molecules data
coulomb_eigen: True - use coulomb eigen. False - use coulomb directly
"""
max_atoms = -1
for mol in molecules:
if (len(mol["atoms"]) > max_atoms):
max_atoms = len(mol["atoms"])
features = []
for mol in molecules:
if coulomb_eigen == True:
f = calculate_eigenvalues(mol, max_atoms)
elif coulomb_random_samples > 0:
f = feat_random_coulombMatrix(mol, max_atoms, coulomb_random_samples)
else:
f = feat_coulombMatrix(mol, max_atoms)
features.append(f)
return features
def extractTarget(molecules):
energies = []
for mol in molecules:
e = mol["energy"]
energies.append(e)
return energies