test_featurizers.py 11.8 KB
Newer Older
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
import torch

from dgllife.utils.featurizers import *
from rdkit import Chem

def test_one_hot_encoding():
    x = 1.
    allowable_set = [0., 1., 2.]
    assert one_hot_encoding(x, allowable_set) == [0, 1, 0]
    assert one_hot_encoding(x, allowable_set, encode_unknown=True) == [0, 1, 0, 0]

    assert one_hot_encoding(x, allowable_set) == [0, 1, 0, 0]
    assert one_hot_encoding(x, allowable_set, encode_unknown=True) == [0, 1, 0, 0]
    assert one_hot_encoding(-1, allowable_set, encode_unknown=True) == [0, 0, 0, 1]

def test_mol1():
    return Chem.MolFromSmiles('CCO')

def test_mol2():
    return Chem.MolFromSmiles('C1=CC2=CC=CC=CC2=C1')

def test_atom_type_one_hot():
    mol = test_mol1()
    assert atom_type_one_hot(mol.GetAtomWithIdx(0), ['C', 'O']) == [1, 0]
    assert atom_type_one_hot(mol.GetAtomWithIdx(2), ['C', 'O']) == [0, 1]

def test_atomic_number_one_hot():
    mol = test_mol1()
    assert atomic_number_one_hot(mol.GetAtomWithIdx(0), [6, 8]) == [1, 0]
    assert atomic_number_one_hot(mol.GetAtomWithIdx(2), [6, 8]) == [0, 1]

def test_atomic_number():
    mol = test_mol1()
    assert atomic_number(mol.GetAtomWithIdx(0)) == [6]
    assert atomic_number(mol.GetAtomWithIdx(2)) == [8]

def test_atom_degree_one_hot():
    mol = test_mol1()
    assert atom_degree_one_hot(mol.GetAtomWithIdx(0), [0, 1, 2]) == [0, 1, 0]
    assert atom_degree_one_hot(mol.GetAtomWithIdx(1), [0, 1, 2]) == [0, 0, 1]

def test_atom_degree():
    mol = test_mol1()
    assert atom_degree(mol.GetAtomWithIdx(0)) == [1]
    assert atom_degree(mol.GetAtomWithIdx(1)) == [2]

def test_atom_total_degree_one_hot():
    mol = test_mol1()
    assert atom_total_degree_one_hot(mol.GetAtomWithIdx(0), [0, 2, 4]) == [0, 0, 1]
    assert atom_total_degree_one_hot(mol.GetAtomWithIdx(2), [0, 2, 4]) == [0, 1, 0]

def test_atom_total_degree():
    mol = test_mol1()
    assert atom_total_degree(mol.GetAtomWithIdx(0)) == [4]
    assert atom_total_degree(mol.GetAtomWithIdx(2)) == [2]

57
58
59
60
61
62
63
64
65
66
def test_atom_explicit_valence_one_hot():
    mol = test_mol1()
    assert atom_implicit_valence_one_hot(mol.GetAtomWithIdx(0), [1, 2, 3]) == [1, 0, 0]
    assert atom_implicit_valence_one_hot(mol.GetAtomWithIdx(1), [1, 2, 3]) == [0, 1, 0]

def test_atom_explicit_valence():
    mol = test_mol1()
    assert atom_explicit_valence(mol.GetAtomWithIdx(0)) == [1]
    assert atom_explicit_valence(mol.GetAtomWithIdx(1)) == [2]

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
def test_atom_implicit_valence_one_hot():
    mol = test_mol1()
    assert atom_implicit_valence_one_hot(mol.GetAtomWithIdx(0), [1, 2, 3]) == [0, 0, 1]
    assert atom_implicit_valence_one_hot(mol.GetAtomWithIdx(1), [1, 2, 3]) == [0, 1, 0]

def test_atom_implicit_valence():
    mol = test_mol1()
    assert atom_implicit_valence(mol.GetAtomWithIdx(0)) == [3]
    assert atom_implicit_valence(mol.GetAtomWithIdx(1)) == [2]

def test_atom_hybridization_one_hot():
    mol = test_mol1()
    assert atom_hybridization_one_hot(mol.GetAtomWithIdx(0)) == [0, 0, 1, 0, 0]

def test_atom_total_num_H_one_hot():
    mol = test_mol1()
    assert atom_total_num_H_one_hot(mol.GetAtomWithIdx(0)) == [0, 0, 0, 1, 0]
    assert atom_total_num_H_one_hot(mol.GetAtomWithIdx(1)) == [0, 0, 1, 0, 0]

def test_atom_total_num_H():
    mol = test_mol1()
    assert atom_total_num_H(mol.GetAtomWithIdx(0)) == [3]
    assert atom_total_num_H(mol.GetAtomWithIdx(1)) == [2]

def test_atom_formal_charge_one_hot():
    mol = test_mol1()
    assert atom_formal_charge_one_hot(mol.GetAtomWithIdx(0)) == [0, 0, 1, 0, 0]

def test_atom_formal_charge():
    mol = test_mol1()
    assert atom_formal_charge(mol.GetAtomWithIdx(0)) == [0]

def test_atom_num_radical_electrons_one_hot():
    mol = test_mol1()
    assert atom_num_radical_electrons_one_hot(mol.GetAtomWithIdx(0)) == [1, 0, 0, 0, 0]

def test_atom_num_radical_electrons():
    mol = test_mol1()
    assert atom_num_radical_electrons(mol.GetAtomWithIdx(0)) == [0]

def test_atom_is_aromatic_one_hot():
    mol = test_mol1()
    assert atom_is_aromatic_one_hot(mol.GetAtomWithIdx(0)) == [1, 0]
    mol = test_mol2()
    assert atom_is_aromatic_one_hot(mol.GetAtomWithIdx(0)) == [0, 1]

def test_atom_is_aromatic():
    mol = test_mol1()
    assert atom_is_aromatic(mol.GetAtomWithIdx(0)) == [0]
    mol = test_mol2()
    assert atom_is_aromatic(mol.GetAtomWithIdx(0)) == [1]

119
120
121
122
123
124
125
126
127
128
129
130
def test_atom_is_in_ring_one_hot():
    mol = test_mol1()
    assert atom_is_in_ring_one_hot(mol.GetAtomWithIdx(0)) == [1, 0]
    mol = test_mol2()
    assert atom_is_in_ring_one_hot(mol.GetAtomWithIdx(0)) == [0, 1]

def test_atom_is_in_ring():
    mol = test_mol1()
    assert atom_is_in_ring(mol.GetAtomWithIdx(0)) == [0]
    mol = test_mol2()
    assert atom_is_in_ring(mol.GetAtomWithIdx(0)) == [1]

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
def test_atom_chiral_tag_one_hot():
    mol = test_mol1()
    assert atom_chiral_tag_one_hot(mol.GetAtomWithIdx(0)) == [1, 0, 0, 0]

def test_atom_mass():
    mol = test_mol1()
    atom = mol.GetAtomWithIdx(0)
    assert atom_mass(atom) == [atom.GetMass() * 0.01]
    atom = mol.GetAtomWithIdx(1)
    assert atom_mass(atom) == [atom.GetMass() * 0.01]

def test_concat_featurizer():
    test_featurizer = ConcatFeaturizer(
        [atom_is_aromatic_one_hot, atom_chiral_tag_one_hot]
    )
    mol = test_mol1()
    assert test_featurizer(mol.GetAtomWithIdx(0)) == [1, 0, 1, 0, 0, 0]
    mol = test_mol2()
    assert test_featurizer(mol.GetAtomWithIdx(0)) == [0, 1, 1, 0, 0, 0]

class TestAtomFeaturizer(BaseAtomFeaturizer):
    def __init__(self):
        super(TestAtomFeaturizer, self).__init__(
            featurizer_funcs={
                'h1': ConcatFeaturizer([atom_total_degree_one_hot,
                                        atom_formal_charge_one_hot]),
                'h2': ConcatFeaturizer([atom_num_radical_electrons_one_hot])
            }
        )

def test_base_atom_featurizer():
    test_featurizer = TestAtomFeaturizer()
    mol = test_mol1()
    feats = test_featurizer(mol)
    torch.allclose(feats['h1'], torch.tensor([[0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0.],
                                              [0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0.],
                                              [0., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0.]]))
    torch.allclose(feats['h2'], torch.tensor([[1., 0., 0., 0., 0.],
                                              [1., 0., 0., 0., 0.],
                                              [1., 0., 0., 0., 0.]]))

def test_canonical_atom_featurizer():
    test_featurizer = CanonicalAtomFeaturizer()
    mol = test_mol1()
    feats = test_featurizer(mol)
    assert list(feats.keys()) == ['h']
    torch.allclose(feats['h'], torch.tensor([[1., 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., 1., 0., 0., 0.,
                                              0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.,
                                              0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.,
                                              1., 0.],
                                             [1., 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., 1., 0., 0.,
                                              0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.,
                                              0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1.,
                                              0., 0.],
                                             [0., 0., 1., 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., 1., 0., 0., 0.,
                                              0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.,
                                              0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0.,
                                              0., 0.]]))

def test_bond_type_one_hot():
    mol = test_mol1()
    assert bond_type_one_hot(mol.GetBondWithIdx(0)) == [1, 0, 0, 0]
    mol = test_mol2()
    assert bond_type_one_hot(mol.GetBondWithIdx(0)) == [0, 0, 0, 1]

def test_bond_is_conjugated_one_hot():
    mol = test_mol1()
    assert bond_is_conjugated_one_hot(mol.GetBondWithIdx(0)) == [1, 0]
    mol = test_mol2()
    assert bond_is_conjugated_one_hot(mol.GetBondWithIdx(0)) == [0, 1]

def test_bond_is_conjugated():
    mol = test_mol1()
    assert bond_is_conjugated(mol.GetBondWithIdx(0)) == [0]
    mol = test_mol2()
    assert bond_is_conjugated(mol.GetBondWithIdx(0)) == [1]

def test_bond_is_in_ring_one_hot():
    mol = test_mol1()
    assert bond_is_in_ring_one_hot(mol.GetBondWithIdx(0)) == [1, 0]
    mol = test_mol2()
    assert bond_is_in_ring_one_hot(mol.GetBondWithIdx(0)) == [0, 1]

def test_bond_is_in_ring():
    mol = test_mol1()
    assert bond_is_in_ring(mol.GetBondWithIdx(0)) == [0]
    mol = test_mol2()
    assert bond_is_in_ring(mol.GetBondWithIdx(0)) == [1]

def test_bond_stereo_one_hot():
    mol = test_mol1()
    assert bond_stereo_one_hot(mol.GetBondWithIdx(0)) == [1, 0, 0, 0, 0, 0]

class TestBondFeaturizer(BaseBondFeaturizer):
    def __init__(self):
        super(TestBondFeaturizer, self).__init__(
            featurizer_funcs={
                'h1': ConcatFeaturizer([bond_is_in_ring, bond_is_conjugated]),
                'h2': ConcatFeaturizer([bond_stereo_one_hot])
            }
        )

def test_base_bond_featurizer():
    test_featurizer = TestBondFeaturizer()
    mol = test_mol1()
    feats = test_featurizer(mol)
    assert torch.allclose(feats['h1'], torch.tensor([[0., 0.], [0., 0.], [0., 0.], [0., 0.]]))
    assert torch.allclose(feats['h2'], torch.tensor([[1., 0., 0., 0., 0., 0.],
                                                     [1., 0., 0., 0., 0., 0.],
                                                     [1., 0., 0., 0., 0., 0.],
                                                     [1., 0., 0., 0., 0., 0.]]))

def test_canonical_bond_featurizer():
    test_featurizer = CanonicalBondFeaturizer()
    mol = test_mol1()
    feats = test_featurizer(mol)
    assert torch.allclose(feats['e'], torch.tensor(
        [[1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
         [1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
         [1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
         [1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]]))

if __name__ == '__main__':
    test_one_hot_encoding()
    test_atom_type_one_hot()
    test_atomic_number_one_hot()
    test_atomic_number()
    test_atom_degree_one_hot()
    test_atom_degree()
    test_atom_total_degree_one_hot()
    test_atom_total_degree()
271
    test_atom_explicit_valence()
272
273
274
275
276
277
278
279
280
281
282
    test_atom_implicit_valence_one_hot()
    test_atom_implicit_valence()
    test_atom_hybridization_one_hot()
    test_atom_total_num_H_one_hot()
    test_atom_total_num_H()
    test_atom_formal_charge_one_hot()
    test_atom_formal_charge()
    test_atom_num_radical_electrons_one_hot()
    test_atom_num_radical_electrons()
    test_atom_is_aromatic_one_hot()
    test_atom_is_aromatic()
283
284
    test_atom_is_in_ring_one_hot()
    test_atom_is_in_ring()
285
286
287
288
289
290
291
292
293
294
295
296
297
    test_atom_chiral_tag_one_hot()
    test_atom_mass()
    test_concat_featurizer()
    test_base_atom_featurizer()
    test_canonical_atom_featurizer()
    test_bond_type_one_hot()
    test_bond_is_conjugated_one_hot()
    test_bond_is_conjugated()
    test_bond_is_in_ring_one_hot()
    test_bond_is_in_ring()
    test_bond_stereo_one_hot()
    test_base_bond_featurizer()
    test_canonical_bond_featurizer()