-
Notifications
You must be signed in to change notification settings - Fork 0
/
gp_metrics.py
195 lines (135 loc) · 4.72 KB
/
gp_metrics.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
"""
GP METRICS
====================
This module allows the use of Gaussian Processes for property
prediction. It is basically a wrapper of GPmol within the
ChemORGAN architecture. It has been entirely programmed by
Carlos Outeiral.
The author whishes to thank Rodolfo Ferro for sharing his
library in the alpha phase.
"""
import GPmol
import tensorflow as tf
import numpy as np
import pandas as pd
import rdkit.Chem.AllChem as Chem
class GaussianProcess(object):
"""
Class for handling gaussian processes.
"""
def __init__(self, label, nBits=4096):
"""Initializes the model.
Arguments
-----------
- label. Identifies the property predicted
by the gaussian process.
- nBits. Refers to the number of bits in which
the Morgan fingerprints are encoded. By
default, 4096.
Note
-----------
Using the same label for different Machine Learning
models in the same run might lead to crashes.
"""
self.name = label
self.nBits = nBits
self.graph = tf.Graph()
with self.graph.as_default():
self.model = GPmol.model.GPMol()
def predict(self, smiles):
"""
Computes the predictions for a batch of molecules.
Arguments
-----------
- smiles. Array or list containing the
SMILES representation of the molecules.
Returns
-----------
A list containing the predictions.
"""
with self.graph.as_default():
input_x = self.computeFingerprints(smiles)
input_x = np.reshape(input_x, (-1, self.nBits))
return self.model.predict(input_x)[0]
def evaluate(self, train_x, train_y):
"""
Evaluates the accuracy of the method.
Arguments
-----------
- train_x. Array or list containing the
SMILES representation of the molecules.
- train_y. The real values of the desired
properties.
Returns
-----------
Mean squared error.
"""
with self.graph.as_default():
input_x = self.computeFingerprints(train_x)
predicted = self.model.predict(input_x)[0]
error = (predicted - train_y)**2
return np.mean(error)
def train(self, train_x, train_y):
"""
Trains the model
Arguments
-----------
- train_x. Array or list containing the
SMILES representation of the molecules.
- train_y. The real values of the desired
properties.
Arguments
-----------
A .json file will be created in the main dir,
named according to the model's label.
"""
with self.graph.as_default():
input_x = pd.DataFrame({'smiles': pd.Series(train_x),
'property': pd.Series(train_y)})
preproc = GPmol.preprocessor.Preprocessor(input_x)
preproc.addFp(duplicates=True, args={'nBits': self.nBits,
'radius': 12})
preproc.addTarget(target='property')
kernel = GPmol.kernels.Tanimoto(input_dim=self.nBits)
self.model = GPmol.model.GPMol(kernel=kernel, preprocessor=preproc)
self.model.train()
self.model.save('../data/gps/{}.json'.format(self.name))
def load(self, file):
"""
Loads a previously trained model.
Arguments
-----------
- file. A string pointing to the .json file.
"""
with self.graph.as_default():
self.model.loadJSON(file)
def computeFingerprints(self, smiles):
"""
Computes Morgan fingerprints
Arguments
-----------
- smiles. An array or list of molecules in
the SMILES codification.
Returns
-----------
A numpy array containing Morgan fingerprints
bitvectors.
"""
if isinstance(smiles, str):
smiles = [smiles]
mols = [Chem.MolFromSmiles(smile) for smile in smiles]
fps = [Chem.GetMorganFingerprintAsBitVect(
mol, 12, nBits=self.nBits) for mol in mols]
bitvectors = [self.fingerprintToBitVect(fp) for fp in fps]
return np.asarray(bitvectors)
def fingerprintToBitVect(self, fp):
"""
Transforms a Morgan fingerprint to a bit vector.
Arguments
-----------
- fp. Morgan fingerprint
Returns
-----------
A bit vector.
"""
return np.asarray([float(i) for i in fp])