-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathprocessData_dense.py
255 lines (208 loc) · 9.08 KB
/
processData_dense.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
"""
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 DENSE 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_dense.py rawDataFolder processedDataFolder numberOfOutputs
Usage Example:
python processData_dense.py data_dense data_dense_processed 3
Requirements for a single raw data file:
* In ".csv" format;
* The first column is MOLECULE names;
* The second to #(1+numberOfOutputs) column is true activity (if known), with any column name;
* The remaining columns are the input features (compound descriptor);
* Name the file as "training.csv" or "test.csv".
Requirements of organizing a group of data files:
* It is ok to only have "training.csv" but without the corresponding "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
from processData_sparse import smartOpen, packCSR, extractCSR, delete_rows, delete_rows_csr
from DNNSharedFunc import writeMat
def featuresUsed(path,numberOfOutputs):
f = smartOpen(path, 'r')
headerToks = map(lambda t:t.strip(), f.next().split(','))
f.close()
offset = 1 + numberOfOutputs
return set(headerToks[offset:])
def outputUsed(path,numberOfOutputs):
f = smartOpen(path, 'r')
headerToks = map(lambda t:t.strip(), f.next().split(','))
f.close()
offset = 1 + numberOfOutputs
return set(headerToks[1:offset])
def buildGlobalTables(paths,numberOfOutputs):
allFeats = set()
alloutputs = set()
for path in paths:
feats = featuresUsed(path,numberOfOutputs)
allFeats.update(feats)
outs = outputUsed(path,numberOfOutputs)
alloutputs.update(outs)
featTable = dict((feat,j) for j,feat in enumerate(allFeats))
outputTable = dict((out,j) for j,out in enumerate(alloutputs))
return featTable, outputTable
def loadRawDataset(path, featTable, outputTable, numberOfOutputs):
"""
numberOfOutputs = 0, for real test data which have no output columns
"""
print "Loading %s" % (path)
f = smartOpen(path, 'r')
headerToks = map(lambda t:t.strip(), f.next().split(','))
offset = 1 + numberOfOutputs # since the first column is MOLECULE name
header_outputNames = headerToks[1:offset]
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 > 1:
for j in range(1,offset):
targs.append(float(toks[j]))
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).reshape((r,numberOfOutputs))
else:
targs = None
mIds = num.array(mIds, dtype=num.object)
featNames = sorted(featTable, key=featTable.get)
outputNames = sorted(outputTable, key=outputTable.get)
if numberOfOutputs > 0:
targs_ordered = num.zeros(shape=targs.shape)
for i in range(numberOfOutputs):
temp = outputTable.get(header_outputNames[i])
targs_ordered[:,temp] = targs[:,i]
else:
targs_ordered = None
return featNames, outputNames, mIds, inps, targs_ordered
def CSVtoNPZ(rawFilePath, saveFilePath, featTable, outputTable, numberOfOutputs):
featNames, outputNames, molIds, inps, targs = loadRawDataset(rawFilePath, featTable, outputTable, numberOfOutputs)
toSave = {}
toSave['featNames'] = featNames
toSave['outputNames'] = outputNames
toSave['molIds'] = molIds
packCSR('inps', inps, toSave)
toSave['targs'] = targs
print "Save processed data to: %s" % (saveFilePath)
num.savez(saveFilePath, **toSave)
def loadPackedData(path):
fd = open(path, 'rb')
d = num.load(fd)
molIds = d['molIds']
featNames = d['featNames']
outputNames = d['outputNames']
inps = extractCSR(d, 'inps')
targs = d['targs'] # possible to be "None".
fd.close()
return featNames, outputNames, molIds, inps, targs
def preprocess_train(rawDataFolder,numberOfOutputs):
"""
pre-process DENSE raw data inside training function
"""
basePath = rawDataFolder
savePrefix = rawDataFolder
allPaths = glob.glob(basePath+"/*.csv")
trainPath = os.path.join(basePath,"training.csv")
testPath = os.path.join(basePath,"test.csv")
if not os.path.exists(trainPath):
print >> sys.stderr, 'Cannot find training datasets!'
return
if not os.path.exists(testPath):
print "---- Warning: No one-to-one matches between training set and test set. -----"
featTable, outputTable = buildGlobalTables([trainPath],numberOfOutputs)
print "length of all feature table = %d" % len(featTable)
print "length of all output table = %d" % len(outputTable)
tblFile = open(os.path.join(savePrefix,"featTable.pk"),'w')
pk.dump(featTable, tblFile)
tblFile.close()
tblFile = open(os.path.join(savePrefix,"outputTable.pk"),'w')
pk.dump(outputTable, tblFile)
tblFile.close()
savePath = os.path.join(savePrefix,"training.npz")
CSVtoNPZ(trainPath, savePath, featTable, outputTable, numberOfOutputs)
if os.path.exists(testPath):
savePath = os.path.join(savePrefix,"test.npz")
CSVtoNPZ(testPath, savePath, featTable, outputTable, numberOfOutputs)
print "------ Pre-process raw data finished. ------"
def preprocess_test(rawDataFolder,featTable,outputTable,label = 0):
"""
pre-process DENSE 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 feature table load from model = %d" % len(featTable)
if label != 0:
print "length of output table load from model = %d" % len(outputTable)
for p in testPaths:
print "------------"
savePath = os.path.join(savePrefix,os.path.basename(p).split(".")[0]+".npz")
CSVtoNPZ(p, savePath, featTable, outputTable, (label!=0)*len(outputTable))
print "------ Pre-process raw data finished. ------"
def main():
basePath = sys.argv[1]
savePrefix = sys.argv[2]
numberOfOutputs = int(sys.argv[3])
if not os.path.exists(savePrefix):
os.makedirs(savePrefix)
trainPath = os.path.join(basePath,"training.csv")
testPath = os.path.join(basePath,"test.csv")
featTable, outputTable = buildGlobalTables([trainPath],numberOfOutputs)
print "length of all feature table = %d" % len(featTable)
print "length of all output table = %d" % len(outputTable)
tblFile = open(os.path.join(savePrefix,"featTable.pk"),'w')
pk.dump(featTable, tblFile)
tblFile.close()
tblFile = open(os.path.join(savePrefix,"outputTable.pk"),'w')
pk.dump(outputTable, tblFile)
tblFile.close()
savePath = os.path.join(savePrefix,"training.npz")
CSVtoNPZ(trainPath, savePath, featTable, outputTable, numberOfOutputs)
if os.path.exists(testPath):
savePath = os.path.join(savePrefix,"test.npz")
CSVtoNPZ(testPath, savePath, featTable, outputTable, numberOfOutputs)
if __name__ == "__main__":
main()