1
|
|
|
#! /usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# Copyright (C) 2016 Rich Lewis <[email protected]> |
4
|
|
|
# License: 3-clause BSD |
5
|
|
|
|
6
|
1 |
|
""" |
7
|
|
|
# skchem.data.converters.base |
8
|
|
|
|
9
|
|
|
Defines the base converter class. |
10
|
|
|
""" |
11
|
|
|
|
12
|
1 |
|
import warnings |
13
|
1 |
|
import logging |
14
|
1 |
|
import os |
15
|
1 |
|
from collections import namedtuple |
16
|
|
|
|
17
|
1 |
|
import numpy as np |
|
|
|
|
18
|
1 |
|
import pandas as pd |
|
|
|
|
19
|
1 |
|
import h5py |
|
|
|
|
20
|
1 |
|
from fuel.datasets import H5PYDataset |
|
|
|
|
21
|
|
|
|
22
|
1 |
|
from ... import forcefields |
23
|
1 |
|
from ... import filters |
24
|
1 |
|
from ... import features |
25
|
1 |
|
from ... import standardizers |
26
|
1 |
|
from ... import pipeline |
27
|
|
|
|
28
|
1 |
|
logger = logging.getLogger(__name__) |
|
|
|
|
29
|
|
|
|
30
|
|
|
|
31
|
1 |
|
def default_pipeline(): |
32
|
|
|
""" Return a default pipeline to be used for general datasets. """ |
33
|
|
|
return pipeline.Pipeline([ |
34
|
|
|
standardizers.ChemAxonStandardizer(keep_failed=True, warn_on_fail=False), |
35
|
|
|
forcefields.UFF(add_hs=True, warn_on_fail=False), |
36
|
|
|
filters.OrganicFilter(), |
37
|
|
|
filters.AtomNumberFilter(above=5, below=100, include_hydrogens=True), |
38
|
|
|
filters.MassFilter(below=1000) |
39
|
|
|
]) |
40
|
|
|
|
41
|
1 |
|
DEFAULT_PYTABLES_KW = { |
42
|
|
|
'complib': 'bzip2', |
43
|
|
|
'complevel': 9 |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
def contiguous_order(to_order, splits): |
47
|
|
|
""" Determine a contiguous order from non-overlapping splits, and put data in that order. |
48
|
|
|
|
49
|
|
|
Args: |
50
|
|
|
to_order (iterable<pd.Series, pd.DataFrame, pd.Panel>): |
51
|
|
|
The pandas objects to put in contiguous order. |
52
|
|
|
splits (iterable<pd.Series>): |
53
|
|
|
The non-overlapping splits, as boolean masks. |
54
|
|
|
|
55
|
|
|
Returns: |
56
|
|
|
iterable<pd.Series, pd.DataFrame, pd.Panel>: The data in contiguous order. |
57
|
|
|
""" |
58
|
|
|
|
59
|
|
|
member = pd.Series(0, index=splits[0].index) |
60
|
|
|
for i, split in enumerate(splits): |
61
|
|
|
member[split] = i |
62
|
|
|
idx = member.sort_values().index |
63
|
|
|
return (order.reindex(idx) for order in to_order) |
64
|
|
|
|
65
|
1 |
|
Feature = namedtuple('Feature', ['fper', 'key', 'axis_names']) |
66
|
|
|
|
67
|
|
|
|
68
|
1 |
|
def default_features(): |
|
|
|
|
69
|
|
|
return ( |
70
|
|
|
Feature(fper=features.MorganFeaturizer(), |
71
|
|
|
key='X_morg', |
72
|
|
|
axis_names=['batch', 'features']), |
73
|
|
|
Feature(fper=features.PhysicochemicalFeaturizer(), |
74
|
|
|
key='X_pc', |
75
|
|
|
axis_names=['batch', 'features']), |
76
|
|
|
Feature(fper=features.AtomFeaturizer(max_atoms=100), |
|
|
|
|
77
|
|
|
key='A', |
78
|
|
|
axis_names=['batch', 'atom_idx', 'features']), |
79
|
|
|
Feature(fper=features.GraphDistanceTransformer(max_atoms=100), |
|
|
|
|
80
|
|
|
key='G', |
81
|
|
|
axis_names=['batch', 'atom_idx', 'atom_idx']), |
82
|
|
|
Feature(fper=features.SpacialDistanceTransformer(max_atoms=100), |
|
|
|
|
83
|
|
|
key='G_d', |
84
|
|
|
axis_names=['batch', 'atom_idx', 'atom_idx']), |
85
|
|
|
Feature(fper=features.ChemAxonFeaturizer(features='all'), |
86
|
|
|
key='X_cx', |
87
|
|
|
axis_names=['batch', 'features']), |
88
|
|
|
Feature(fper=features.ChemAxonAtomFeaturizer(features='all', max_atoms=100), |
|
|
|
|
89
|
|
|
key='A_cx', |
90
|
|
|
axis_names=['batch', 'atom_idx', 'features']) |
91
|
|
|
) |
92
|
|
|
|
93
|
|
|
|
94
|
1 |
|
class Split(object): |
|
|
|
|
95
|
|
|
|
96
|
1 |
|
def __init__(self, mask, name, converter): |
97
|
|
|
self.mask = mask |
98
|
|
|
self.name = name |
99
|
|
|
self.converter = converter |
100
|
|
|
|
101
|
1 |
|
@property |
102
|
|
|
def contiguous(self): |
|
|
|
|
103
|
|
|
diff = np.ediff1d(self.mask.astype(int)) |
104
|
|
|
if self.mask.iloc[0] != 0: |
105
|
|
|
diff[0] = 1 |
106
|
|
|
if self.mask.iloc[-1] != 0: |
107
|
|
|
diff[-1] = -1 |
108
|
|
|
return sum(diff == -1) == 1 or sum(diff == 1) == 1 |
109
|
|
|
|
110
|
1 |
|
@property |
111
|
|
|
def indices(self): |
|
|
|
|
112
|
|
|
return np.nonzero(self.mask)[0] |
113
|
|
|
|
114
|
1 |
|
def save(self): |
|
|
|
|
115
|
|
|
self.converter.data_file[self.name + '_indices'] = self.indices |
116
|
|
|
with warnings.catch_warnings(): |
117
|
|
|
warnings.simplefilter('ignore') |
118
|
|
|
self.mask.to_hdf(self.converter.data_file.filename, '/indices/' + self.name) |
119
|
|
|
|
120
|
1 |
|
@property |
121
|
|
|
def ref(self): |
|
|
|
|
122
|
|
|
return self.converter.data_file[self.name + '_indices'].ref |
123
|
|
|
|
124
|
1 |
|
def to_dict(self): |
|
|
|
|
125
|
|
|
idx = self.indices |
126
|
|
|
if self.contiguous: |
127
|
|
|
low, high = min(idx), max(idx) |
128
|
|
|
return {source: (low, high) for source in self.converter.source_names} |
129
|
|
|
else: |
130
|
|
|
return {source: (-1, -1, self.ref) for source in self.converter.source_names} |
131
|
|
|
|
132
|
|
|
|
133
|
1 |
|
class Converter(object): |
134
|
|
|
""" Create a fuel dataset from molecules and targets. """ |
135
|
|
|
|
136
|
1 |
|
def __init__(self, directory, output_directory, output_filename='default.h5'): |
|
|
|
|
137
|
|
|
raise NotImplemented |
|
|
|
|
138
|
|
|
|
139
|
1 |
|
def run(self, ms, y, output_path, splits=None, features=None, pytables_kws=DEFAULT_PYTABLES_KW): |
|
|
|
|
140
|
|
|
|
141
|
|
|
""" |
142
|
|
|
Args: |
143
|
|
|
ms (pd.Series): |
144
|
|
|
The molecules of the dataset. |
145
|
|
|
ys (pd.Series or pd.DataFrame): |
146
|
|
|
The target labels of the dataset. |
147
|
|
|
output_path (str): |
148
|
|
|
The path to which the dataset should be saved. |
149
|
|
|
features (list[Feature]): |
150
|
|
|
The features to calculate. Defaults are used if `None`. |
151
|
|
|
splits (iterable<(name, split)>): |
152
|
|
|
An iterable of name, split tuples. Splits are provided as boolean arrays of the whole data. |
|
|
|
|
153
|
|
|
""" |
154
|
|
|
|
155
|
|
|
self.output_path = output_path |
|
|
|
|
156
|
|
|
self.pytables_kws = pytables_kws |
|
|
|
|
157
|
|
|
self.features = features if features is not None else default_features() |
|
|
|
|
158
|
|
|
self.feature_names = [feat.key for feat in self.features] |
|
|
|
|
159
|
|
|
self.task_names = ['y'] |
|
|
|
|
160
|
|
|
self.splits = [Split(split, name, self) for name, split in splits] |
|
|
|
|
161
|
|
|
|
162
|
|
|
self.create_file(output_path) |
163
|
|
|
|
164
|
|
|
self.save_splits() |
165
|
|
|
self.save_molecules(ms) |
166
|
|
|
self.save_targets(y) |
167
|
|
|
self.save_features(ms) |
168
|
|
|
|
169
|
1 |
|
@property |
170
|
|
|
def source_names(self): |
|
|
|
|
171
|
|
|
return self.feature_names + self.task_names |
172
|
|
|
|
173
|
1 |
|
@property |
174
|
|
|
def split_names(self): |
|
|
|
|
175
|
|
|
return self.splits |
176
|
|
|
|
177
|
1 |
|
def create_file(self, path): |
|
|
|
|
178
|
|
|
logger.info('Creating h5 file at %s...', self.output_path) |
179
|
|
|
self.data_file = h5py.File(path, 'w') |
|
|
|
|
180
|
|
|
return self.data_file |
181
|
|
|
|
182
|
1 |
|
def save_molecules(self, mols): |
183
|
|
|
|
184
|
|
|
""" Save the molecules to the data file. """ |
185
|
|
|
|
186
|
|
|
logger.info('Writing molecules to file...') |
187
|
|
|
logger.debug('Writing %s molecules to %s', len(mols), self.data_file.filename) |
188
|
|
|
with warnings.catch_warnings(): |
189
|
|
|
warnings.simplefilter('ignore') |
190
|
|
|
mols.to_hdf(self.data_file.filename, 'structure', **self.pytables_kws) |
191
|
|
|
mols.apply(lambda m: m.to_smiles().encode('utf-8')).to_hdf(self.data_file.filename, 'smiles') |
|
|
|
|
192
|
|
|
|
193
|
1 |
|
def save_frame(self, data, name, prefix='targets'): |
194
|
|
|
|
195
|
|
|
""" Save the a frame to the data file. """ |
196
|
|
|
|
197
|
|
|
logger.info('Writing %s', name) |
198
|
|
|
logger.debug('Writing data of shape %s to %s', data.shape, self.data_file.filename) |
199
|
|
|
|
200
|
|
|
with warnings.catch_warnings(): |
201
|
|
|
warnings.simplefilter('ignore') |
202
|
|
|
if len(data.shape) > 2: |
203
|
|
|
data = data.transpose(2, 1, 0) # panel serializes backwards for some reason... |
204
|
|
|
data.to_hdf(self.data_file.filename, |
205
|
|
|
key='/{prefix}/{name}'.format(prefix=prefix, name=name), |
206
|
|
|
**self.pytables_kws) |
207
|
|
|
|
208
|
|
|
if isinstance(data, pd.Series): |
209
|
|
|
self.data_file[name] = h5py.SoftLink('/{prefix}/{name}/values'.format(prefix=prefix, name=name)) |
|
|
|
|
210
|
|
|
self.data_file[name].dims[0].label = data.index.name |
211
|
|
|
|
212
|
|
|
elif isinstance(data, pd.DataFrame): |
213
|
|
|
self.data_file[name] = h5py.SoftLink('/{prefix}/{name}/block0_values'.format(prefix=prefix, name=name)) |
|
|
|
|
214
|
|
|
self.data_file[name].dims[0].label = data.index.name |
215
|
|
|
self.data_file[name].dims[1].label = data.columns.name |
216
|
|
|
|
217
|
|
|
elif isinstance(data, pd.Panel): |
218
|
|
|
self.data_file[name] = h5py.SoftLink('/{prefix}/{name}/block0_values'.format(prefix=prefix, name=name)) |
|
|
|
|
219
|
|
|
self.data_file[name].dims[0].label = data.minor_axis.name # as panel serializes backwards |
|
|
|
|
220
|
|
|
self.data_file[name].dims[1].label = data.major_axis.name |
221
|
|
|
self.data_file[name].dims[2].label = data.items.name |
222
|
|
|
|
223
|
1 |
|
def save_targets(self, y): |
|
|
|
|
224
|
|
|
|
225
|
|
|
self.save_frame(y, name='y', prefix='targets') |
226
|
|
|
|
227
|
1 |
|
def save_features(self, ms): |
|
|
|
|
228
|
|
|
|
229
|
|
|
""" Save all features for the dataset. """ |
230
|
|
|
logger.debug('Saving features') |
231
|
|
|
for feat in self.features: |
232
|
|
|
self._save_feature(ms, feat) |
233
|
|
|
|
234
|
1 |
|
def _save_feature(self, ms, feat): |
|
|
|
|
235
|
|
|
|
236
|
|
|
""" Calculate and save a feature to the data file. """ |
237
|
|
|
logger.info('Calculating %s', feat.key) |
238
|
|
|
|
239
|
|
|
fps = feat.fper.transform(ms) |
240
|
|
|
self.save_frame(fps, name=feat.key, prefix='feats') |
241
|
|
|
|
242
|
1 |
|
def save_splits(self): |
243
|
|
|
|
244
|
|
|
""" Save the splits to the data file. """ |
245
|
|
|
|
246
|
|
|
logger.info('Producing dataset splits...') |
247
|
|
|
for split in self.splits: |
248
|
|
|
split.save() |
249
|
|
|
split_dict = {split.name: split.to_dict() for split in self.splits} |
250
|
|
|
splits = H5PYDataset.create_split_array(split_dict) |
251
|
|
|
logger.debug('split: %s', splits) |
252
|
|
|
logger.info('Saving splits...') |
253
|
|
|
with warnings.catch_warnings(): |
254
|
|
|
warnings.simplefilter('ignore') |
255
|
|
|
self.data_file.attrs['split'] = splits |
256
|
|
|
|
257
|
1 |
|
@classmethod |
258
|
|
|
def convert(cls, **kwargs): |
|
|
|
|
259
|
|
|
kwargs.setdefault('directory', os.getcwd()) |
260
|
|
|
kwargs.setdefault('output_directory', os.getcwd()) |
261
|
|
|
|
262
|
|
|
return cls(**kwargs).output_path, |
263
|
|
|
|
264
|
1 |
|
@classmethod |
265
|
|
|
def fill_subparser(cls, subparser): |
|
|
|
|
266
|
|
|
return cls.convert |
267
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.