forked from bradenkatzman/CellClassificationMachineLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
421 lines (331 loc) · 18.1 KB
/
main.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import sys
import time
import numpy as np
from RNASeqData import RNASeqData
import preprocess
import rbfSVC_RNASeq
# import neuralNetwork_RNASeq
import knn_RNASeq
import randomForest_RNASeq
import analysis
# File: main.py
#
# This file processes the command line arguments supplied and runs the program using the selected classifier. Based on user input,
# the program decides whether to down sample, cross validate, and which classifier to use. This file defines the four classifiers
# supplied to the user for classification: Support Vector Machine using Radial Basis Function Kernel, Mutli-Layer Perceptron (Neural Network),
# K-Nearest Neighbor, and Random Forest. After calling the preproccess code and running the classification, this class sends the results
# to an analysis file that performs evaluations and writes the results to file
def rbfSVC(trainingData, testingData, trainingDataTargets, testingDataTargets):
# fit training data to rbf svc
rbfSVC_RNASeq.fitTrainingData(trainingData, trainingDataTargets)
# predict values using rbf support vector machine
rbfSVC_predictionResults = rbfSVC_RNASeq.predictTestData(testingData)
return rbfSVC_predictionResults
def mlp(trainingData, testingData, trainingDataTargets, testingDataTargets):
# fit training data to multi layer perceptron
neuralNetwork_RNASeq.fitTrainingData(trainingData, trainingDataTargets)
# predict values using neural network
neuralNetwork_predictionResults = neuralNetwork_RNASeq.predictTestData(testingData)
return neuralNetwork_predictionResults
def knn(trainingData, testingData, trainingDataTargets, testingDataTargets):
# fit training data to knn
knn_RNASeq.fitTrainingData(trainingData, trainingDataTargets)
# predict the values using knn classifier
knn_predictionResults = knn_RNASeq.predictTestData(testingData)
return knn_predictionResults
def rf(trainingData, testingData, trainingDataTargets, testingDataTargets):
# fit training data to rf
randomForest_RNASeq.fitTrainingData(trainingData, trainingDataTargets)
# predict the values using random forest classifier
rf_predictionResults = randomForest_RNASeq.predictTestData(testingData)
return rf_predictionResults
if __name__ == '__main__':
t0 = time.clock()
print "start"
# check for correct number of args
if int(sys.argv[3]) == 3 and len(sys.argv) != 7:
print "Usage: python main.py <raw_data_file> <annotations_file> <classifier [1,2,3,4] - svm, nn, knn, rf> <down sample? --> 0,1> <cross validate? --> 0,1> <n_neighbors (only if knn)>"
sys.exit(0)
if int(sys.argv[3]) != 3 and len(sys.argv) != 6:
print "Usage: python main.py <raw_data_file> <annotations_file> <classifier [1,2,3,4] - svm, nn, knn, rf> <down sample? --> 0,1> <cross validate? --> 0,1> <n_neighbors (only if knn)>"
sys.exit(0)
raw_data_file = sys.argv[1]
annotations_file = sys.argv[2]
classifier = int(sys.argv[3])
downSampleFlag = False
crossValidateFlag = False
if sys.argv[4] == "1":
downSampleFlag = True
if sys.argv[5] == "1":
crossValidateFlag = True
n_neighbors = -1
if classifier == 3: # using knn, need to supply param for number of neighbors
n_neighbors = int(sys.argv[6])
knn_RNASeq.initializeKnn(n_neighbors) # initialize the classifier with n_neighbors
print "Using:"
print " - raw data: {raw}".format(raw=raw_data_file)
print " - annotations: {ann}".format(ann=annotations_file)
if classifier == 1:
print " - Using Radial Basis Function Kernel Support Vector Machine"
elif classifier == 2:
print " - Using Multi-Layer Perceptron (Neural Network)"
elif classifier == 3:
print " - Using K Nearest Neighbor Classifier with k = {k}".format(k=n_neighbors)
elif classifier == 4:
print " - Using Random Forest Classifier"
else:
print "** ERROR: invalid classifier selection"
sys.exit(0)
if downSampleFlag:
print "** Down sampling enabled **"
else:
print "** Down sampling disabled **"
if crossValidateFlag:
print "** Cross validation enabled **"
else:
print "** Cross validation disabled **"
# initialize the data set class
data = RNASeqData(raw_data_file, annotations_file)
# read raw RNA seq data into memory
data.setRawData(preprocess.loadRawData(data.getRawDataFileName()))
# read cell identifier annotations into memory
data.setCellIdentifierAnnotations(preprocess.loadCellIdentifierAnnotations(data.getAnnotationsFileName(),
data.getNumCellsRaw()))
# read molecule count annotations into memory
data.setMoleculeCountAnnotations(preprocess.loadMoleculeCountAnnotations(data.getAnnotationsFileName(),
data.getNumCellsRaw()))
if downSampleFlag:
# down sample the data by cluster size --> MAKE DOWN SAMPLING A CLA OPTION
# i.e. scale all cluster size to the smallest cluster (by number of cells)
# save down sampled data and random indices for accessing corresponding annotations
downSampleClusterData, randIndices = preprocess.downSampleByClusterSize(data.getRawData(),
data.getCellIdentifierAnnotations())
# add the data and random indices reference to the data class
data.setDSClusterData(downSampleClusterData)
data.setRandIndicesFromDS(randIndices)
# down sample the data by the cell with the least number of molecules
data.setDSCluster_MoleculeData(preprocess.downSampleByMoleculeCount(data.getDSClusterData(),
data.getMoleculeCountAnnotations(), data.getRandIndices()))
if crossValidateFlag:
# make 10-fold cross validation data
data.makeCrossValidationTrainingAndTestingData(downSampleFlag)
folds = data.getFolds()
foldsKey = data.getFoldsKey()
# make sure the data is parallel
if len(folds) != len(foldsKey) or len(folds[0]) != len(foldsKey[0]):
print "error: folds and folds key are not parallel data sets"
sys.exit(0)
iterator = 0 # we'll use this to iterate through folds and use each as the training data
foldsEvaluations = []
while iterator < 10:
testingData = folds[iterator]
testingDataKey = foldsKey[iterator]
# make 2D arrays of training cells and keys
trainingFolds = []
trainingKeys = []
i = 0
while i < 10:
if i != iterator:
for cell in folds[i]:
trainingFolds.append(cell)
for key in foldsKey[i]:
trainingKeys.append(key)
i += 1
if classifier == 1:
# ***************** RBF SVC *****************
# fit and make predictions
rbfSVC_predictionResults = rbfSVC(trainingFolds, testingData, trainingKeys, testingDataKey)
# add the accuracies for this fold to accuracies list
foldsEvaluations.append(analysis.calculateEvaluations(rbfSVC_predictionResults, testingDataKey))
# ***************** END RBF SVC *****************
elif classifier == 2:
# ***************** MLP *****************
# fit and make predictions
neuralNetwork_predictionResults = mlp(trainingFolds, testingData, trainingKeys, testingDataKey)
# add the accuracies for this fold to accuracies list
foldsEvaluations.append(analysis.calculateEvaluations(neuralNetwork_predictionResults, testingDataKey))
# ***************** END MLP *****************
elif classifier == 3:
# ***************** KNN *****************
# fit and make predictions
knn_predictionResults = knn(trainingFolds, testingData, trainingKeys, testingDataKey)
# add the accuracies for this fold to the accuracies list
foldsEvaluations.append(analysis.calculateEvaluations(knn_predictionResults, testingDataKey))
# ***************** END KNN *****************
elif classifier == 4:
# ***************** RF *****************
# fit and make predictions
rf_predictionResults = rf(trainingFolds, testingData, trainingKeys, testingDataKey)
# add the accuracies for this fold to the accuracies list
foldsEvaluations.append(analysis.calculateEvaluations(rf_predictionResults, testingDataKey))
# ***************** END RF *****************
# increment iterator to process the next fold as testing data
iterator += 1
print "finished fold #{num}".format(num=iterator)
if classifier == 1:
# ***************** RBF SVC *****************
analysis.analyzeAndWriteToFile("Radial Basis Function Support Vector Machine", rbfSVC_predictionResults, testingDataKey, foldsEvaluations, 10, 0)
# ***************** END RBF SVC *****************
elif classifier == 2:
# ***************** MLP *****************
analysis.analyzeAndWriteToFile("Multi-Layer Perceptron (Neural Network)", neuralNetwork_predictionResults, testingDataKey, foldsEvaluations, 10, 0)
# ***************** END MLP *****************
elif classifier == 3:
# ***************** KNN *****************
analysis.analyzeAndWriteToFile("KNearestNeighbor Classifier_{k}".format(k=n_neighbors), knn_predictionResults, testingDataKey, foldsEvaluations, 10, 0)
# ***************** END KNN *****************
elif classifier == 4:
# ***************** RF *****************
analysis.analyzeAndWriteToFile("Random Forest Classifier", rf_predictionResults, testingDataKey, foldsEvaluations, 10, 0)
# ***************** END RF *****************
else:
# partition the down sampled data set into 70% training and 30% testing
data.makeDSTrainingAndTestingData()
if classifier == 1:
# ***************** RBF SVC *****************
rbfSVC_predictionResults = rbfSVC(data.getDSTrainingData(), data.getDSTestingData(), data.getDSTargetValues(),
data.getDSTestingDataTargetValues())
# analyze results using robust evaluations
foldsEvaluations = [] # single fold list but we still need to use a 3D list
foldsEvaluations.append(analysis.calculateEvaluations(rbfSVC_predictionResults, data.getDSTestingDataTargetValues()))
analysis.analyzeAndWriteToFile("Radial Basis Function Support Vector Machine", rbfSVC_predictionResults, data.getDSTestingDataTargetValues(), foldsEvaluations, 1, 1)
# ***************** END RBF SVC *****************
elif classifier == 2:
# ***************** MLP *****************
neuralNetwork_predictionResults = mlp(data.getDSTrainingData(), data.getDSTestingData(), data.getDSTargetValues(),
data.getDSTestingDataTargetValues())
# analyze results using robust evaluations
foldsEvaluations = [] # single fold list but we still need to use a 3D list
foldsEvaluations.append(analysis.calculateEvaluations(neuralNetwork_predictionResults, data.getDSTestingDataTargetValues()))
analysis.analyzeAndWriteToFile("Multi-Layer Perceptron (Neural Network)", neuralNetwork_predictionResults, data.getDSTestingDataTargetValues(), foldsEvaluations, 1, 1)
# ***************** END MLP *****************
elif classifier == 3:
# ***************** KNN *****************
knn_predictionResults = knn(data.getDSTrainingData(), data.getDSTestingData(), data.getDSTargetValues(),
data.getDSTestingDataTargetValues())
foldsEvaluations = [] # single fold list but we still need to use a 3D list
foldsEvaluations.append(analysis.calculateEvaluations(knn_predictionResults, data.getDSTestingDataTargetValues()))
analysis.analyzeAndWriteToFile("KNearestNeighbor Classifier_{k}".format(k=n_neighbors), knn_predictionResults, data.getDSTestingDataTargetValues(), foldsEvaluations, 1, 1)
# ***************** END KNN *****************
elif classifier == 4:
# ***************** RF *****************
rf_predictionResults = rf(data.getDSTrainingData(), data.getDSTestingData(), data.getDSTargetValues(),
data.getDSTestingDataTargetValues())
foldsEvaluations = [] # single fold list but we still need to use a 3D list
foldsEvaluations.append(analysis.calculateEvaluations(rf_predictionResults, data.getDSTestingDataTargetValues()))
analysis.analyzeAndWriteToFile("Random Forest Classifier", rf_predictionResults, data.getDSTestingDataTargetValues(), foldsEvaluations, 1, 1)
# ***************** END RF *****************
else:
if crossValidateFlag:
# make 10-fold cross validation data
data.makeCrossValidationTrainingAndTestingData(downSampleFlag)
folds = data.getFolds()
foldsKey = data.getFoldsKey()
# make sure the data is parallel
if len(folds) != len(foldsKey) or len(folds[0]) != len(foldsKey[0]):
print "error: folds and folds key are not parallel data sets"
sys.exit(0)
iterator = 0 # we'll use this to iterate through folds and use each as the training data
foldsEvaluations = []
while iterator < 10:
testingData = folds[iterator]
testingDataKey = foldsKey[iterator]
# make 2D arrays of training cells and keys
trainingFolds = []
trainingKeys = []
i = 0
while i < 10:
if i != iterator:
for cell in folds[i]:
trainingFolds.append(cell)
for key in foldsKey[i]:
trainingKeys.append(key)
i += 1
if classifier == 1:
# ***************** RBF SVC *****************
# fit and make predictions
rbfSVC_predictionResults = rbfSVC(trainingFolds, testingData, trainingKeys, testingDataKey)
# add the accuracies for this fold to accuracies list
foldsEvaluations.append(analysis.calculateEvaluations(rbfSVC_predictionResults, testingDataKey))
# ***************** END RBF SVC *****************
elif classifier == 2:
# ***************** MLP *****************
# fit and make predictions
neuralNetwork_predictionResults = mlp(trainingFolds, testingData, trainingKeys, testingDataKey)
# add the accuracies for this fold to accuracies list
foldsEvaluations.append(analysis.calculateEvaluations(neuralNetwork_predictionResults, testingDataKey))
# ***************** END MLP *****************
elif classifier == 3:
# ***************** KNN *****************
# fit and make predictions
knn_predictionResults = knn(trainingFolds, testingData, trainingKeys, testingDataKey)
# add the accuracies for this fold to accuracies list
foldsEvaluations.append(analysis.calculateEvaluations(knn_predictionResults, testingDataKey))
# ***************** END KNN *****************
elif classifier == 4:
# ***************** RF *****************
# fit and make predictions
rf_predictionResults = rf(trainingFolds, testingData, trainingKeys, testingDataKey)
# add the accuracies for this fold to accuracies list
foldsEvaluations.append(analysis.calculateEvaluations(rf_predictionResults, testingDataKey))
# ***************** END RF *****************
# increment iterator to process the next fold as testing data
iterator += 1
print "finished fold #{num}".format(num=iterator)
if classifier == 1:
# ***************** RBF SVC *****************
analysis.analyzeAndWriteToFile("Radial Basis Function Support Vector Machine", rbfSVC_predictionResults, testingDataKey, foldsEvaluations, 10, 2)
# ***************** END RBF SVC *****************
elif classifier == 2:
# ***************** MLP *****************
analysis.analyzeAndWriteToFile("Multi-Layer Perceptron (Neural Network)", neuralNetwork_predictionResults, testingDataKey, foldsEvaluations, 10, 2)
# ***************** END MLP *****************
elif classifier == 3:
# ***************** KNN *****************
analysis.analyzeAndWriteToFile("KNearestNeighbor Classifier_{k}".format(k=n_neighbors), knn_predictionResults, testingDataKey, foldsEvaluations, 10, 2)
# ***************** END KNN *****************
elif classifier == 4:
# ***************** RF *****************
analysis.analyzeAndWriteToFile("Random Forest Classifier", rf_predictionResults, testingDataKey, foldsEvaluations, 10, 2)
# ***************** END RF *****************
else:
# partition the data set into 70% training and 30% testing
data.makeTrainingAndTestingData()
if classifier == 1:
# ***************** RBF SVC *****************
rbfSVC_predictionResults = rbfSVC(data.getTrainingData(), data.getTestingData(), data.getTrainingDataTargetValues(),
data.getTestingDataTargetValues())
# analyze results using robust evaluations
foldsEvaluations = [] # single fold list but we still need to use a 3D list
foldsEvaluations.append(analysis.calculateEvaluations(rbfSVC_predictionResults, data.getTestingDataTargetValues()))
analysis.analyzeAndWriteToFile("Radial Basis Function Support Vector Machine", rbfSVC_predictionResults, data.getTestingDataTargetValues(), foldsEvaluations, 1, 3)
# ***************** END RBF SVC *****************
elif classifier == 2:
# ***************** MLP *****************
neuralNetwork_predictionResults = mlp(data.getTrainingData(), data.getTestingData(), data.getTrainingDataTargetValues(),
data.getTestingDataTargetValues())
# analyze results using robust evaluations
foldsEvaluations = [] # single fold list but we still need to use a 3D list
foldsEvaluations.append(analysis.calculateEvaluations(neuralNetwork_predictionResults, data.getTestingDataTargetValues()))
analysis.analyzeAndWriteToFile("Multi-Layer Perceptron (Neural Network)", neuralNetwork_predictionResults, data.getTestingDataTargetValues(), foldsEvaluations, 1, 3)
# ***************** END MLP *****************
elif classifier == 3:
# ***************** KNN *****************
knn_predictionResults = knn(data.getTrainingData(), data.getTestingData(), data.getTrainingDataTargetValues(),
data.getTestingDataTargetValues())
# analyze results using robust evaluations
foldsEvaluations = []
foldsEvaluations.append(analysis.calculateEvaluations(knn_predictionResults, data.getTestingDataTargetValues()))
analysis.analyzeAndWriteToFile("KNearestNeighbor Classifier_{k}".format(k=n_neighbors), knn_predictionResults, data.getTestingDataTargetValues(), foldsEvaluations, 1, 3)
# ***************** END KNN *****************
elif classifier == 4:
# ***************** RF *****************
rf_predictionResults = rf(data.getTrainingData(), data.getTestingData(), data.getTrainingDataTargetValues(),
data.getTestingDataTargetValues())
# analyze results using robust evaluations
foldsEvaluations = []
foldsEvaluations.append(analysis.calculateEvaluations(rf_predictionResults, data.getTestingDataTargetValues()))
analysis.analyzeAndWriteToFile("Random Forest Classifier", rf_predictionResults, data.getTestingDataTargetValues(), foldsEvaluations, 1, 3)
# ***************** END RF *****************
print "\nprogram execution: {t} seconds".format(t=time.clock()-t0)
print "exiting"