-
Notifications
You must be signed in to change notification settings - Fork 27
/
processData_sparse.py
306 lines (257 loc) · 10.3 KB
/
processData_sparse.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
Copyright (c) 2011,2012,2016,2017 Merck Sharp & Dohme Corp. a subsidiary of Merck & Co., Inc., Kenilworth, NJ, USA.
This file is part of the Deep Neural Network QSAR program.
Deep Neural Network QSAR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
"""
A group of functions for processing a group of SPARSE QSAR data sets used by DeepNeuralNet_QSAR Programs
Main function:
Pre-processing the raw data to facilitate later use.
Given data folder path, convert all *.csv file to numpy *.npz file and save in the given save path .
Usage Illustration:
python processData_sparse.py rawDataFolder processedDataFolder
Usage Example:
python processData_sparse.py data_sparse data_sparse_processed
Requirements for a single raw data file:
* In ".csv" format;
* The first column is MOLECULE names;
* The second column is true activity (if known), with column name "Act";
* The remaining columns are the input features (compound descriptor);
* Name the file as "datName_training.csv" or "datName_test.csv", where "datName" is the QSAR task name;
* The "datName" cannot contain "_". For example, you need to re-name the "RAT_F" task as "RAT-F".
Requirements of organizing a group of data files:
* It is ok to only have "datName_training.csv" but without the corresponding "datName_test.csv".
* Put all the raw data sets under one folder.
Last modified by Yuting Xu on Feb.8, 2017
"""
import gzip
import numpy as num
import glob
import scipy.sparse as sp
import cPickle as pk
import os
import sys
def smartOpen(path, mode='r'):
if path.endswith(".gz"):
return gzip.open(path, mode)
return open(path, mode)
def featuresUsed(path):
f = smartOpen(path, 'r')
headerToks = map(lambda t:t.strip(), f.next().split(','))
f.close()
if headerToks[1] == "Act":
return set(headerToks[2:])
return set(headerToks[1:])
def buildGlobalFeatureTable(paths):
allFeats = set()
for path in paths:
feats = featuresUsed(path)
allFeats.update(feats)
table = dict((feat,j) for j,feat in enumerate(allFeats))
return table
def dsName(path):
return str(os.path.basename(path).split("_")[0])
def loadRawDataset(path, featTable = None):
if featTable is None:
# for loading single data set to train single DNN
featTable = buildGlobalFeatureTable([path])
print "Loading %s" % (path)
f = smartOpen(path, 'r')
headerToks = map(lambda t:t.strip(), f.next().split(','))
offset = 1 # since the first column is MOLECULE name
if headerToks[1] == "Act":
offset = 2
mIds = []
data, row, col = [], [], []
targs = []
r = 0
for line in f:
toks = map(lambda t:t.strip(), line.split(','))
mIds.append(toks[0])
if offset == 2:
targs.append(float(toks[1]))
for j in range(len(headerToks) - offset):
x = float(toks[offset+j])
key = headerToks[offset+j]
if x != 0 and (key in featTable):
c = featTable[headerToks[offset+j]]
row.append(r)
col.append(c)
data.append(x)
r += 1
f.close()
data = num.array(data, dtype=num.int32)
row = num.array(row, dtype=num.int)
col = num.array(col, dtype=num.int)
inps = sp.coo_matrix((data, (row, col)),shape=(r, len(featTable)))
print "Converting inputs to csr ..."
inps = inps.tocsr()
if len(targs) > 0:
targs = num.array(targs)
else:
targs = None
mIds = num.array(mIds, dtype=num.object)
featNames = sorted(featTable, key=featTable.get)
return featNames, mIds, inps, targs
def packCSR(baseName, mat, d):
"""
pack the csr matrix mat into dictionary d
Used by function: CSVtoNPZ
"""
d[baseName + "_data"] = mat.data
d[baseName + "_indices"] = mat.indices
d[baseName + "_indptr"] = mat.indptr
d[baseName + "_shape"] = num.array(mat.shape)
return d
def extractCSR(d, baseName):
"""
Used by function: loadPackedData
"""
data = d[baseName + "_data"]
indices = d[baseName + "_indices"]
indptr = d[baseName + "_indptr"]
shape = tuple(d[baseName + "_shape"])
return sp.csr_matrix((data, indices, indptr), shape=shape)
def loadPackedData(path):
fd = open(path, 'rb')
d = num.load(fd)
molIds = d['molIds']
featNames = d['featNames']
inps = extractCSR(d, 'inps')
targs = d['targs'] # possible to be "None".
fd.close()
return featNames, molIds, inps, targs
def removeAllZeroFeatures(X,featNames):
assert(X.shape[1] == len(featNames))
total = num.abs(num.array(X.sum(axis=0))).flatten()
nonzero = num.arange(X.shape[1])[total>0]
xx = X.tocsc()[:, nonzero]
newfeatNames = map(lambda t:featNames[t], nonzero)
return xx.tocsr(), newfeatNames
def prepareTestInpsColumn(testX, testFeatNames, trainFeatNames):
assert(testX.shape[1] == len(testFeatNames))
# build the feature table of training features.
featTable = dict((feat,j) for j,feat in enumerate(trainFeatNames))
# allocate an empty matrix with the right size.
testXX = sp.lil_matrix((testX.shape[0], len(trainFeatNames)), dtype=testX.dtype)
for i in range(len(testFeatNames)):
testFeatName = testFeatNames[i]
if testFeatName in featTable.keys():
testXX[:,featTable[testFeatName]]=testX[:,i]
return testXX.tocsr()
def delete_rows(mat, indices):
"""
Remove the rows denoted by "indices" from the numpy ndarray or CSR sparse matrix "mat"
"""
if isinstance(mat, num.ndarray):
mat = num.delete(mat,indices,0)
elif isinstance(mat, sp.csr_matrix):
mat = delete_rows_csr(mat, indices)
else:
raise ValueError("Cannot handle input data type!")
return mat
def delete_rows_csr(mat, indices):
"""
Remove the rows denoted by "indices" from the CSR sparse matrix "mat"
"""
if not isinstance(mat, sp.csr_matrix):
raise ValueError("works only for CSR format -- use .tocsr() first")
indices = list(indices)
mask = num.ones(mat.shape[0],dtype=bool)
mask[indices] = False
return mat[mask]
def CSVtoNPZ(featTable, rawFilePath, saveFilePath):
featNames, molIds, inps, targs = loadRawDataset(rawFilePath, featTable)
toSave = {}
toSave['featNames'] = featNames
toSave['molIds'] = molIds
packCSR('inps', inps, toSave)
toSave['targs'] = targs
print "Save processed data to: %s" % (saveFilePath)
num.savez(saveFilePath, **toSave)
def preprocess_train(rawDataFolder):
"""
pre-process raw data inside training function
"""
basePath = rawDataFolder
savePrefix = rawDataFolder
allPaths = glob.glob(basePath+"/*.csv")
trainPaths = glob.glob(basePath+"/*training.csv")
trainPaths.sort()
if len(trainPaths)==0:
print >> sys.stderr, 'Cannot find training datasets!'
return
testPaths = glob.glob(basePath+"/*test.csv")
testPaths.sort()
if len(trainPaths) != len(testPaths):
print "---- Warning: No one-to-one matches between training set and test set. -----"
featTable = buildGlobalFeatureTable(trainPaths)
print "length of all feature table build from training sets = %d" % len(featTable)
tblFile = open(os.path.join(savePrefix,"featTable.pk"),'w')
pk.dump(featTable, tblFile)
tblFile.close()
for p in allPaths:
print "------------"
savePath = os.path.join(savePrefix,os.path.basename(p).split(".")[0]+".npz")
CSVtoNPZ(featTable, p, savePath)
print "------ Pre-process raw data finished. ------"
def preprocess_test(rawDataFolder,featTable):
"""
pre-process raw data inside prediction function
"""
basePath = rawDataFolder
savePrefix = rawDataFolder
testPaths = glob.glob(basePath+"/*test.csv")
testPaths.sort()
if len(testPaths)==0:
print >> sys.stderr, 'Cannot find any raw datasets!'
return
print "length of all feature table load from model = %d" % len(featTable)
for p in testPaths:
print "------------"
savePath = os.path.join(savePrefix,os.path.basename(p).split(".")[0]+".npz")
CSVtoNPZ(featTable, p, savePath)
print "------ Pre-process raw data finished. ------"
def main():
basePath = sys.argv[1]
savePrefix = sys.argv[2]
if not os.path.exists(savePrefix):
os.makedirs(savePrefix)
allPaths = glob.glob(basePath+"/*.csv")
trainPaths = glob.glob(basePath+"/*training.csv")
testPaths = glob.glob(basePath+"/*test.csv")
trainPaths.sort()
testPaths.sort()
featTable = buildGlobalFeatureTable(trainPaths)
#featTable = buildGlobalFeatureTable(allPaths)
print "length of all feature table = %d" % len(featTable)
tblFile = open(os.path.join(savePrefix,"featTable.pk"),'w')
pk.dump(featTable, tblFile)
tblFile.close()
if len(trainPaths) == len(testPaths):
for trainPath, testPath in zip(trainPaths, testPaths):
ds = dsName(trainPath)
print "======================"
print "dataset name: %s" % ds
assert(ds==dsName(testPath))
for p in [trainPath, testPath]:
savePath = os.path.join(savePrefix,os.path.basename(p).split(".")[0]+".npz")
CSVtoNPZ(featTable, p, savePath)
else:
# in case that training sets and test sets are unmatched
print "---- Warning: No one-to-one matches between training set and test set. -----"
for p in allPaths:
print "======================"
savePath = os.path.join(savePrefix,os.path.basename(p).split(".")[0]+".npz")
CSVtoNPZ(featTable, p, savePath)
if __name__ == "__main__":
main()