-
Notifications
You must be signed in to change notification settings - Fork 2
/
runShogunOneClassSVMDNASpectrumKernel.py
278 lines (236 loc) · 10.4 KB
/
runShogunOneClassSVMDNASpectrumKernel.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
# Original code from: ../examples/documented/python_modular/regression_libsvr_modular.py
# and: ../examples/documented/python_modular/serialization_string_kernels_modular.py
import numpy as np
from modshogun import StringCharFeatures, StringWordFeatures, SortWordString
from modshogun import DNA, Labels
from modshogun import MSG_DEBUG
from modshogun import SVMLight
from modshogun import CommWordStringKernel
from modshogun import BinaryLabels, LibSVMOneClass,RealFeatures
from modshogun import ROCEvaluation
import argparse
import sys
def parseArgument():
parser = argparse.ArgumentParser(description='Run Shogun One Class SVM DNA Spectrum Kernel')
parser.add_argument('string', metavar='[train data]', nargs=1, help='Training Data File, format: sequence (ATCG) one instance per line' )
parser.add_argument('string', metavar='[train labels]', nargs=1, help='Training Labels File, format: 0/1 where each label of an instance matches the data line number (because this is One-class SVM training set should be all one class therefore should be all "1"' )
parser.add_argument('string', metavar='[validation data]', nargs=1, help='Validation Data File, same format as training data' )
parser.add_argument('string', metavar='[validation label]', nargs=1, help='Validation Lables File, same format as training label (can be either 0 or 1)' )
parser.add_argument('string', metavar='[train classification]', nargs=1, help='Classification result on training set' )
parser.add_argument('string', metavar='[validation classification]', nargs=1, help='Classification result on validation set' )
parser.add_argument('int', metavar='[k]', nargs=1, help='Value for K' )
parser.add_argument('float', metavar='[SVM C]', nargs=1, help='SVM C' )
parser.add_argument('int', metavar='[number of Gaps]', nargs=1, help='Number of Gaps' )
parser.add_argument('float', metavar='[epsilon]', nargs=1, help='Value of epsilon' )
args = parser.parse_args()
global TRAININGDATAFILENAME
global TRAININGLABELSFILENAME
global VALIDATIONDATAFILENAME
global VALIDATIONLABELSFILENAME
global TRAINPREDICTIONSEPSILONFILENAME
global VALIDATIONPREDICTIONSEPSILONFILENAME
global SVMC
global GAP
global K
global EPSILON
TRAININGDATAFILENAME = sys.argv[1]
TRAININGLABELSFILENAME = sys.argv[2]
VALIDATIONDATAFILENAME = sys.argv[3]
VALIDATIONLABELSFILENAME = sys.argv[4]
TRAINPREDICTIONSEPSILONFILENAME = sys.argv[5]
VALIDATIONPREDICTIONSEPSILONFILENAME = sys.argv[6]
K = int(sys.argv[7])
SVMC = float(sys.argv[8]) # Initially 1
GAP = int(sys.argv[9]) # Initially 0
EPSILON = float(sys.argv[10])
def makeStringList(stringFileName):
# Get a string list from a file
stringList = []
stringFile = open(stringFileName)
skippedLines = []
lineCount = 0
for line in stringFile:
# Iterate through the string file and get the string from each line
if "N" in line.strip() or "n" in line.strip():
# The current sequence has an N, so skip it
skippedLines.append(lineCount)
else:
stringList.append(line.strip().upper())
lineCount = lineCount + 1
print len(skippedLines)
stringFile.close()
return [stringList, skippedLines]
def makeIntList(intFileName, skippedLines):
# Get a float list from a file
intList = []
intFile = open(intFileName)
lineCount = 0
for line in intFile:
# Iterate through the float file and get the float from each line
if lineCount in skippedLines:
# Skip the current line
lineCount = lineCount + 1
continue
label = int(line.strip())
if label == 0:
# Labels are 1 and 0 instead of 1 and -1
label = -1
intList.append(label)
lineCount = lineCount + 1
intFile.close()
return np.array(intList)
def runShogunOneClassSVMDNASpectrumKernel(train_xt, train_lt, test_xt):
"""
run svm with spectrum kernel
"""
##################################################
# set up svr
charfeat_train = StringCharFeatures(train_xt, DNA)
feats_train = StringWordFeatures(DNA)
feats_train.obtain_from_char(charfeat_train, K-1, K, GAP, False)
preproc=SortWordString()
preproc.init(feats_train)
feats_train.add_preprocessor(preproc)
feats_train.apply_preprocessor()
charfeat_test = StringCharFeatures(test_xt, DNA)
feats_test=StringWordFeatures(DNA)
feats_test.obtain_from_char(charfeat_test, K-1, K, GAP, False)
feats_test.add_preprocessor(preproc)
feats_test.apply_preprocessor()
kernel=CommWordStringKernel(feats_train, feats_train, False)
kernel.io.set_loglevel(MSG_DEBUG)
# init kernel
labels = BinaryLabels(train_lt)
# run svm model
print "Ready to train!"
svm=LibSVMOneClass(SVMC, kernel)
svm.set_epsilon(EPSILON)
svm.train()
# predictions
print "Making predictions!"
out1DecisionValues = svm.apply(feats_train)
out1=out1DecisionValues.get_labels()
kernel.init(feats_train, feats_test)
out2DecisionValues = svm.apply(feats_test)
out2=out2DecisionValues.get_labels()
# predictions = svm.apply(feats_test)
# return predictions, svm, predictions.get_labels()
return out1,out2,out1DecisionValues,out2DecisionValues
def writeFloatList(floatList, floatListFileName):
# Write a list of floats to a file
floatListFile = open(floatListFileName, 'w+')
for f in floatList:
# Iterate through the floats and record each of them
floatListFile.write(str(f) + "\n")
floatListFile.close()
def outputResultsClassification(out1, out2, out1DecisionValues, out2DecisionValues, train_lt, test_lt):
# Output the results to the appropriate output files
writeFloatList(out1, TRAINPREDICTIONSEPSILONFILENAME)
writeFloatList(out2, VALIDATIONPREDICTIONSEPSILONFILENAME)
numTrainCorrect = 0
for i in range(len(train_lt)):
# Iterate through training labels and count the number that are the same as the predicted labels
if out1[i] == train_lt[i]:
# The current prediction is correct
numTrainCorrect = numTrainCorrect + 1
fracTrainCorrect = float(numTrainCorrect)/float(len(train_lt))
print "Training accuracy:"
print fracTrainCorrect
trainLabels = BinaryLabels(train_lt)
evaluatorTrain = ROCEvaluation()
evaluatorTrain.evaluate(out1DecisionValues, trainLabels)
print "Training AUC:"
print evaluatorTrain.get_auROC()
numValidCorrect = 0
numPosCorrect = 0
numNegCorrect = 0
for i in range(len(test_lt)):
# Iterate through validation labels and count the number that are the same as the predicted labels
if out2[i] == test_lt[i]:
# The current prediction is correct
numValidCorrect = numValidCorrect + 1
if (out2[i] == 1) and (test_lt[i] == 1):
# The prediction is a positive example
numPosCorrect = numPosCorrect + 1
else:
numNegCorrect = numNegCorrect + 1
fracValidCorrect = float(numValidCorrect)/float(len(test_lt))
print "Validation accuracy:"
print fracValidCorrect
print "Number of correct positive examples:"
print numPosCorrect
print "Number of correct negative examples:"
print numNegCorrect
validLabels = BinaryLabels(test_lt)
evaluatorValid = ROCEvaluation()
evaluatorValid.evaluate(out2DecisionValues, validLabels)
print "Validation AUC:"
print evaluatorValid.get_auROC()
def outputResultsClassificationWithMajorityClass(out1, out2, out1DecisionValues, out2DecisionValues, train_lt, test_lt, test_majorityClass):
# Output the results to the appropriate output files
writeFloatList(out1, TRAINPREDICTIONSEPSILONFILENAME)
writeFloatList(out2, VALIDATIONPREDICTIONSEPSILONFILENAME)
numTrainCorrect = 0
for i in range(len(train_lt)):
# Iterate through training labels and count the number that are the same as the predicted labels
if out1[i] == train_lt[i]:
# The current prediction is correct
numTrainCorrect = numTrainCorrect + 1
fracTrainCorrect = float(numTrainCorrect)/float(len(train_lt))
print "Training accuracy:"
print fracTrainCorrect
trainLabels = BinaryLabels(train_lt)
evaluatorTrain = ROCEvaluation()
evaluatorTrain.evaluate(out1DecisionValues, trainLabels)
print "Training AUC:"
print evaluatorTrain.get_auROC()
numValidCorrect = 0
numPosCorrect = 0
numNegCorrect = 0
numMajorityClassCorrect = 0
numMinorityClassCorrect = 0
for i in range(len(test_lt)):
# Iterate through validation labels and count the number that are the same as the predicted labels
if out2[i] == test_lt[i]:
# The current prediction is correct
numValidCorrect = numValidCorrect + 1
if (out2[i] == 1) and (test_lt[i] == 1):
# The prediction is a positive example
numPosCorrect = numPosCorrect + 1
else:
numNegCorrect = numNegCorrect + 1
if test_majorityClass[i] == 1:
numMajorityClassCorrect = numMajorityClassCorrect + 1
else:
numMinorityClassCorrect = numMinorityClassCorrect + 1
fracValidCorrect = float(numValidCorrect)/float(len(test_lt))
print "Validation accuracy:"
print fracValidCorrect
print "Fraction of correct positive examples:"
print float(numPosCorrect)/float(len(np.where(test_lt > 0)[0]))
print "Fraction of correct negative examples:"
print float(numNegCorrect)/float(len(np.where(test_lt <= 0)[0]))
print "Fraction of correct majority class examples:"
print float(numMajorityClassCorrect)/float(len(np.where(test_majorityClass > 0)[0]))
print "Fraction of correct minority class examples:"
print float(numMinorityClassCorrect)/float(len(np.where(test_majorityClass <= 0)[0]))
validLabels = BinaryLabels(test_lt)
evaluatorValid = ROCEvaluation()
evaluatorValid.evaluate(out2DecisionValues, validLabels)
print "Validation AUC:"
print evaluatorValid.get_auROC()
if __name__=='__main__':
parseArgument() #print usage if not correctly used
print('LibSVM')
[train_xt, skippedLinesTrain] = makeStringList(TRAININGDATAFILENAME)
train_lt = makeIntList(TRAININGLABELSFILENAME, skippedLinesTrain)
[test_xt, skippedLinesValid] = makeStringList(VALIDATIONDATAFILENAME)
test_lt = makeIntList(VALIDATIONLABELSFILENAME, skippedLinesValid)
[out1, out2, out1DecisionValues, out2DecisionValues] = runShogunOneClassSVMDNASpectrumKernel(train_xt, train_lt, test_xt)
# if len(sys.argv) > 10:
# # There is majority class information
# validationMajorityClassFileName = sys.argv[10]
# test_majorityClass = makeIntList(validationMajorityClassFileName, skippedLinesValid)
# outputResultsClassificationWithMajorityClass(out1, out2, out1DecisionValues, out2DecisionValues, train_lt, test_lt, test_majorityClass)
# else:
outputResultsClassification(out1, out2, out1DecisionValues, out2DecisionValues, train_lt, test_lt)