-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutlier_detection.py
More file actions
1132 lines (788 loc) · 27.1 KB
/
Copy pathoutlier_detection.py
File metadata and controls
1132 lines (788 loc) · 27.1 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##
## part of AutoComp
## play with MFI
##
##
## TODO : function to evaluate a single fcs file
##
def create_outlier_detection_training_datafile(data_folder):
"""
IN PROGRESS
Create a training data file from MFI informations
files present in the data_folder
Each line correspond to a file.
TODO : flag if the file is analysable
"""
## importation
import glob
## initialise training data file
training_data = open("MFI_training_data.csv", "w")
mfi_files_list = glob.glob(data_folder+"/*.txt")
## create the header
cmpt = 0
horizontal_label = []
vertical_label = []
header = "analysable,compensated,"
mfi_file = open(mfi_files_list[0], "r")
for line in mfi_file:
if(cmpt == 0):
line = line.rstrip()
line = line.replace("\"", "")
line_in_array = line.split(",")
line_in_array = line_in_array[1:]
for elt in line_in_array:
horizontal_label.append(elt)
else:
line = line.rstrip()
line = line.replace("\"", "")
line_in_array = line.split(",")
vertical_label.append(line_in_array[0])
cmpt += 1
for elt_2 in vertical_label:
for elt_1 in horizontal_label:
header += str(elt_1)+"-"+str(elt_2) +","
mfi_file.close()
training_data.write(header+"\n")
## loop over the files in the data_folder
cmpt = 0
for mfi_file in mfi_files_list:
mfi_file_name = mfi_file.split("/")
mfi_file_name = mfi_file_name[-1].split("_")
mfi_data = open(mfi_file, "r")
## TODO
## determine if the file is an outlier
analysable = "NA"
if("bad" in mfi_file_name):
analysable = 0
else:
analysable = 1
## determine if the file is compensated
compensated = "NA"
if("compensated.txt" == mfi_file_name[-1]):
compensated = 1
else:
compensated = 0
line_to_write = str(analysable)+","+str(compensated)+","
mfi_cmpt = 0
for line in mfi_data:
if(mfi_cmpt != 0):
line = line.rstrip()
line = line.replace("\"", "")
line_in_array = line.split(",")
line_in_array = line_in_array[1:]
for elt in line_in_array:
line_to_write += str(elt) +","
mfi_cmpt += 1
line_to_write = line_to_write[:-1]
if(cmpt == len(mfi_files_list)):
training_data.write(line_to_write)
else:
training_data.write(line_to_write+"\n")
mfi_data.close()
cmpt += 1
## close training data file
training_data.close()
def extract_MFI_informations(data_folder):
"""
Extract MFI information for all the fcs files
in the data folder.
raise a warnings if multiple centers or/and panels
are found in the data folder
Extraction is perfromed by a R script.
"""
## importation
import glob
import os
## parameters
panel_list = []
center_list = []
MFIEXTRACTION_SCRIPT = "ExtractMFIinformation.R"
for fcs_file in glob.glob(data_folder+"/*.fcs"):
fcs_file_name = fcs_file.split("/")
fcs_file_name = fcs_file_name[-1].split("_")
panel = fcs_file_name[1]
center = fcs_file_name[3]
## control the number of panel
## and centers in data file
if(panel not in panel_list):
panel_list.append(panel)
if(center not in center_list):
center_list.append(center)
## Perfrom Extraction
os.system("Rscript "+MFIEXTRACTION_SCRIPT+" "+str(fcs_file))
## raise warnings if needed
if(len(panel_list) > 1):
print "[WARNINGS] => Mix of panels used for MFI training file generation"
if(len(center_list) > 1):
print "[WARNINGS] => Mix of centers used for MFI training file generation"
def flag_bad_files(data_folder):
"""
flag non analysable files with a "bad" tag
"""
## importation
import glob
import os
import shutil
## parameters
bad_id_list = ["32150099", "32140242", "32140244", "32150093", "32152159", "32140206", "32152297", "32151645", '32150107', '32151097', '32151655', '32151659', '32152270', '32160045', '32160388', '32160509', '32160388', '32160509'] # FPS
bad_id_list += ["32160554","32161017","32161023","32161025","32161027","32161028","32170216"] # DRFZ
bad_id_list += ["32151963"] # IRCCS
bad_id_list += ["32150945","32160263","32161168","32161181","32161202"] # KUL
bad_id_list += ["32150936"] # UNIGE
for mfi_info_file in glob.glob(data_folder+"/*.txt"):
mfi_info_file_name = mfi_info_file.split("/")
mfi_info_file_name = mfi_info_file_name[-1].split("_")
panel = mfi_info_file_name[1]
center = mfi_info_file_name[2]
patient_id = mfi_info_file_name[3]
finalPart = mfi_info_file_name[4]
if(patient_id in bad_id_list):
print "[+] Flag file "+str(mfi_info_file)
mfi_file_new_name = "Panel_"+str(panel)+"_"+str(center)+"_"+str(patient_id)+"_bad_"+str(finalPart)
os.system("cp "+str(mfi_info_file)+" "+str(data_folder)+"/"+str(mfi_file_new_name))
os.system("rm "+str(mfi_info_file))
def split_dataset(data_filename, train_proportion):
"""
-> Split data_filename to train and test data file,
according to train_proportion (belong to 0 - 1)
"""
## importation
import random
train_data_filename = data_filename.split(".")
train_data_filename = train_data_filename[0]+"_train.csv"
train_data = open(train_data_filename, "w")
train_data.close()
test_data_filename = data_filename.split(".")
test_data_filename = test_data_filename[0]+"_test.csv"
test_data = open(test_data_filename, "w")
test_data.close()
## Get the number of entries, assume the file has no header
data_file = open(data_filename, "r")
number_of_lines = 0
for line in data_file:
number_of_lines += 1
data_file.close()
## compute the number of line to keep in train data
number_of_lines_to_keep = train_proportion * number_of_lines
## split the data
number_of_lines_in_train_dataset = 0
selected_lines = []
while(number_of_lines_in_train_dataset < number_of_lines_to_keep):
line_selected = random.randint(0,number_of_lines)
if(line_selected not in selected_lines):
## Find the corresponding line
data_file = open(data_filename, "r")
cmpt = 0
for line in data_file:
line = line.split("\n")
line = line[0]
if(cmpt == line_selected and cmpt != 0):
if(number_of_lines_in_train_dataset == number_of_lines_to_keep - 1):
## write line in train data file
train_data = open(train_data_filename, "a")
train_data.write(line)
train_data.close()
else:
## write line in train data file
train_data = open(train_data_filename, "a")
train_data.write(line+"\n")
train_data.close()
selected_lines.append(line_selected)
number_of_lines_in_train_dataset += 1
cmpt += 1
data_file.close()
## store the rest of lines in test data file
data_file = open(data_filename, "r")
number_of_lines_in_test_dataset = 0
cmpt = 0
for line in data_file:
line = line.split("\n")
line = line[0]
if(cmpt not in selected_lines and cmpt != 0):
if(number_of_lines_in_test_dataset == number_of_lines - number_of_lines_in_train_dataset - 1):
test_data = open(test_data_filename, "a")
test_data.write(line)
test_data.close()
else:
test_data = open(test_data_filename, "a")
test_data.write(line+"\n")
test_data.close()
cmpt += 1
data_file.close()
def run_xgboost():
"""
##-----------------------------##
## playing with xgboosted tree ##
##-----------------------------##
"""
## importation
import matplotlib
from matplotlib import pyplot
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import time
from numpy import loadtxt
import xgboost
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
training_data_file = "MFI_FPS_panel_5_training_reduced.csv"
train_proportion = 0.5
split_dataset(training_data_file, train_proportion)
# load data
train_dataset = loadtxt('MFI_FPS_panel_5_training_reduced_train.csv', delimiter=",")
test_dataset = loadtxt('MFI_FPS_panel_5_training_reduced_test.csv', delimiter=",")
# split data into X and y
train_X = train_dataset[:,1:-1]
train_Y = train_dataset[:,0]
test_X = test_dataset[:,1:-1]
test_Y = test_dataset[:,0]
model = XGBClassifier()
model.fit(train_X, train_Y)
# make predictions for test data
y_pred = model.predict(test_X)
predictions = [round(value) for value in y_pred]
# evaluate predictions
accuracy = accuracy_score(test_Y, predictions)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# feature importance
print(model.feature_importances_)
f_importance = model.feature_importances_
pyplot.bar(range(len(model.feature_importances_)), model.feature_importances_)
pyplot.show()
def egalize_learning_dataset(data_folder):
"""
IN PROGRESS
"""
## importation
import glob
import random
import os
bad_files_list = []
compensated_file_list = []
uncompensated_file_list = []
for file in glob.glob(data_folder+"/*.txt"):
file_name = file.split("/")
file_name = file_name[-1].split("_")
satus = file_name[-1].replace(".txt", "")
if("bad" in file_name):
bad_files_list.append(file)
elif(satus == "compensated"):
compensated_file_list.append(file)
else:
uncompensated_file_list.append(file)
bad_files_cmpt = len(bad_files_list)
file_to_save = []
while(len(file_to_save) <= bad_files_cmpt):
saved_compensated_file = compensated_file_list[random.randint(0,len(compensated_file_list)-1)]
saved_uncompensated_file = uncompensated_file_list[random.randint(0,len(uncompensated_file_list)-1)]
file_to_save.append(saved_uncompensated_file)
file_to_save.append(saved_compensated_file)
file_to_save += bad_files_list
for file in glob.glob(data_folder+"/*.txt"):
if(file not in file_to_save):
os.system("rm "+str(file))
def run_lda():
"""
Need more than 2 classes to plot something
"""
## importation
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from numpy import loadtxt
import numpy
from sklearn.externals import joblib
data_file = "MFI_FPS_panel_5_plot.csv"
## load real data
data = loadtxt(data_file, delimiter=",", skiprows=1)
X = data[:,1:-1]
y = data[:,0]
target_names = ["comp", "uncomp", "decouple"]
X = numpy.asarray(X)
y = numpy.asarray(y)
## perform PCA
pca = PCA(n_components=2)
X_r = pca.fit(X).transform(X)
## perform lda
lda = LinearDiscriminantAnalysis(n_components=2)
X_r2 = lda.fit(X, y).transform(X)
# Percentage of variance explained for each components
print('explained variance ratio (first two components): %s'% str(pca.explained_variance_ratio_))
## lda paramaters
print lda.coef_
## save lda model
joblib.dump(lda, 'lda_outlierDetector_model.pkl')
## plot PCA
plt.figure()
colors = ["navy", "green", "red"]
lw = 2
for color, i, target_name in zip(colors, [0, 1, 2], target_names):
plt.scatter(X_r[y == i, 0], X_r[y == i, 1], color=color, alpha=.8, lw=lw,
label=target_name)
plt.legend(loc='best', shadow=False, scatterpoints=1)
plt.title('PCA of MFI dataset Mean')
## plot LDA, need more than 2 classes
plt.figure()
for color, i, target_name in zip(colors, [0, 1, 2], target_names):
plt.scatter(X_r2[y == i, 0], X_r2[y == i, 1], alpha=.8, color=color,
label=target_name)
plt.legend(loc='best', shadow=False, scatterpoints=1)
plt.title('LDA of MFI dataset Mean')
plt.show()
def generate_pca_file():
"""
IN PROGRESS
kind of trash run ...
group 0 : analysable et compense
group 1 : analysable et pas compense
group 2 : non analysable
"""
dataset = open("MFI_training_data.csv", "r")
output_file = open("MFI_FPS_panel_5_plot.csv", "w")
cmpt = 0
for line in dataset:
if(cmpt == 0):
header = "group,"
line = line.rstrip()
line_in_array = line.split(",")
index = 0
for elt in line_in_array:
if(index > 1):
header += str(elt)+","
index += 1
header = header[:-1]
output_file.write(header+"\n")
else:
line = line.rstrip()
line_in_array = line.split(",")
index = 0
group = "NA"
if(line_in_array[0] == "1" and line_in_array[1] == "1"):
group = 0
elif(line_in_array[0] == "0"):
group = 2
else:
group = 1
line_to_write = str(group)+","
for scalar in line_in_array:
if(index > 1):
line_to_write += str(scalar)+","
index += 1
line_to_write = line_to_write[:-1]
output_file.write(line_to_write+"\n")
cmpt += 1
output_file.close()
dataset.close()
def generate_lda_file(pca_file):
"""
IN PROGRESS
Generate a lda file from the pca file where we select only
the uncompensated and the decouple files (i.e group 1 and 2)
to perfrom a 2 group LDA
"""
data_file = open(pca_file, "r")
output_file = open("lda_input_data.csv", "w")
cmpt = 0
for line in data_file:
if(cmpt == 0):
output_file.write(line)
else:
line = line.rstrip()
line_in_array = line.split(",")
if(line_in_array[0] != str(0)):
output_file.write(line+"\n")
cmpt += 1
output_file.close()
data_file.close()
def generate_lda_model():
"""
Scavange from test space
"""
## importation
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from numpy import loadtxt
import numpy
from sklearn.externals import joblib
import os
## generate training data file
os.system("rm trash/MFI_FPS_all/*")
extract_MFI_informations("trash/MFI_input_FPS")
os.system("cp data/MFI/compensated/* trash/MFI_FPS_all/")
os.system("cp data/MFI/uncompensated/* trash/MFI_FPS_all/")
flag_bad_files("trash/MFI_FPS_all")
create_outlier_detection_training_datafile("trash/MFI_FPS_all")
generate_pca_file()
generate_lda_file("MFI_FPS_panel_5_plot.csv")
## load real data
data_file = "lda_input_data.csv"
data = loadtxt(data_file, delimiter=",", skiprows=1)
X = data[:,1:-1]
y = data[:,0]
X = numpy.asarray(X)
y = numpy.asarray(y)
lda = LinearDiscriminantAnalysis(n_components=2)
X_r2 = lda.fit(X, y).transform(X)
## save lda model
joblib.dump(lda, 'lda_outlierDetector_model.pkl')
## model validation
data_file = "MFI_FPS_5_validation.csv"
data = loadtxt(data_file, delimiter=",", skiprows=1)
X = data[:,1:-1]
y = data[:,0]
target_names = ["comp", "uncomp", "decouple"]
X = numpy.asarray(X)
y = numpy.asarray(y)
predictions = lda.predict(X)
cmpt = 0
for prediction in predictions:
print str(prediction) +" || "+str(y[cmpt])
def detect_decouplage(fcs_file, model):
"""
Try to predict if ther is a decoupling
in the fcs file. Use MFI information
return 1 if file looks okay
return 2 if file is suspect
model is lda model file to load, -> trying stuff
"""
## importation
import os
from sklearn.externals import joblib
from numpy import loadtxt
import numpy
## parameters
MFIEXTRACTION_SCRIPT = "ExtractMFIinformation.R"
## clean data MFI folders
os.system("rm data/MFI/compensated/*")
os.system("rm data/MFI/uncompensated/*")
## get information from file name
file_name_in_array = fcs_file.split("/")
if("_comp" not in file_name_in_array[-1]):
status = "uncompensated"
else:
status = "compensated"
file_name_in_array = file_name_in_array[-1].split("_")
fcs_panel = file_name_in_array[1]
fcs_id = file_name_in_array[2]
fcs_center = file_name_in_array[3]
mfi_information_file_name = "data/MFI/"+str(status)+"/Panel_"+str(fcs_panel)+"_"+str(fcs_center)+"_"+str(fcs_id)+"_"+str(status)+".txt"
mfi_evaluation_file = "outlier_evaluation_data_"+str(fcs_center)+"_"+str(fcs_panel)+"_"+str(fcs_id)+".csv"
## Extract MFI information from fcs file
os.system("Rscript "+MFIEXTRACTION_SCRIPT+" "+str(fcs_file))
## generate a data file from MFI information file
flag_bad_files("data/MFI/"+str(status))
create_outlier_detection_training_datafile("data/MFI/"+str(status))
generate_pca_file()
os.system("mv MFI_FPS_panel_5_plot.csv "+str(mfi_evaluation_file))
generate_lda_file(mfi_evaluation_file)
## load model
#clf = joblib.load('lda_outlierDetector_model.pkl')
clf = joblib.load(model)
## load data to evaluate
data = loadtxt(mfi_evaluation_file, delimiter=",", skiprows=1)
X = data[1:-1]
y = data[0]
X = numpy.asarray(X)
y = numpy.asarray(y)
## compute prediction
prediction = clf.predict([X])
## return prediction
return str(prediction[0])
def select_random_bad_files(number_of_bad_files):
"""
"""
## importation
import random
import glob
import os
## parameters
folder_list = ["panel_5_DRFZ", "panel_5_FPS", "panel_5_IDIBELL", "panel_5_IRCCS", "panel_5_KUL", "PANEL_5_SAS", "PANEL_5_UBO", "panel_5_UCL"]
#folder_list = ["panel_5_FPS", "panel_5_IDIBELL", "panel_5_IRCCS", "PANEL_5_SAS", "PANEL_5_UBO", "panel_5_UCL"]
forbidden_id = []
data = open("/home/elwin/decouplage_pannel_5.txt", "r")
for line in data:
line = line.rstrip()
line_in_array = line.split(";")
index = 0
for elt in line_in_array:
if(index != 0 and elt not in forbidden_id):
forbidden_id.append(elt)
index +=1
data.close()
for x in xrange(0,number_of_bad_files):
valid_choice = False
while(not valid_choice):
center = folder_list[random.randint(0,len(folder_list)-1)]
fcs_files = glob.glob("/home/elwin/"+str(center)+"/*.fcs")
candidate_file = fcs_files[random.randint(0,len(fcs_files)-1)]
candidate_file_name = candidate_file.split("/")
candidate_file_name = candidate_file_name[-1].split("_")
if(candidate_file_name[2] in forbidden_id):
os.system("cp "+str(candidate_file)+" trash/MFI_input_FPS/")
valid_choice = True
def select_random_good_files(number_of_good_files):
"""
"""
## importation
import random
import glob
import os
## parameters
folder_list = ["panel_5_DRFZ", "panel_5_FPS", "panel_5_IDIBELL", "panel_5_IRCCS", "panel_5_KUL", "PANEL_5_SAS", "PANEL_5_UBO", "panel_5_UCL"]
#folder_list = ["panel_5_FPS", "panel_5_IDIBELL", "panel_5_IRCCS", "PANEL_5_SAS", "PANEL_5_UBO", "panel_5_UCL"]
forbidden_id = []
data = open("/home/elwin/decouplage_pannel_5.txt", "r")
for line in data:
line = line.rstrip()
line_in_array = line.split(";")
index = 0
for elt in line_in_array:
if(index != 0 and elt not in forbidden_id):
forbidden_id.append(elt)
index +=1
data.close()
data = open("/home/elwin/pannel_5_fichier_de_merde_analyse_qd_mm_.txt", "r")
for line in data:
line = line.rstrip()
line_in_array = line.split(";")
index = 0
for elt in line_in_array:
if(index != 0 and elt not in forbidden_id):
forbidden_id.append(elt)
index +=1
data.close()
for x in xrange(0,number_of_good_files):
valid_choice = False
while(not valid_choice):
center = folder_list[random.randint(0,len(folder_list)-1)]
fcs_files = glob.glob("/home/elwin/"+str(center)+"/*.fcs")
candidate_file = fcs_files[random.randint(0,len(fcs_files)-1)]
candidate_file_name = candidate_file.split("/")
candidate_file_name = candidate_file_name[-1].split("_")
if(candidate_file_name[2] not in forbidden_id):
os.system("cp "+str(candidate_file)+" trash/MFI_input_FPS/")
valid_choice = True
select_random_bad_files(10)
import glob
import random
import os
import numpy as np
## generate validation dataset
## get the list of forbidden id
forbidden_id = []
number_of_files_to_evaluate = 25
log_file = open("learn_lda.log", "w")
good_enough = False
generation = 1
while(not good_enough):
log_file.write("Generation,"+str(generation)+"\n")
score_E1 = 0
score_E2 = 0
score_E3 = 0
## Build and save model 1
## new training data for the next model
## Look for a wise elector, if none is avaialable build
## the first model
if(os.path.isfile("models/elector_wise.pkl")):
print "[+] Find Wise elector"
os.system("cp models/elector_wise.pkl models/elector_1.pkl")
else:
os.system("rm trash/MFI_input_FPS/*")
select_random_bad_files(12)
select_random_good_files(15)
## Model Construction
print "[+] Model 1 Construction"
generate_lda_model()
## Save model 1
os.system("cp lda_outlierDetector_model.pkl models/elector_1.pkl")
## Build and save model 2
## new training data for the next model
os.system("rm trash/MFI_input_FPS/*")
select_random_bad_files(12)
select_random_good_files(15)
## Model Construction
print "[+] Model 2 Construction"
generate_lda_model()
## Save model 1
os.system("cp lda_outlierDetector_model.pkl models/elector_2.pkl")
## Build and save model 3
## new training data for the next model
os.system("rm trash/MFI_input_FPS/*")
select_random_bad_files(12)
select_random_good_files(15)
## Model Construction
print "[+] Model 3 Construction"
generate_lda_model()
## Save model 1
os.system("cp lda_outlierDetector_model.pkl models/elector_3.pkl")
##------------------##
## Model Evaluation ##
##------------------##
## clean validation folders and create validation dataset
os.system("rm trash/FCS_multicenter_validation_2/*")
data = open("/home/elwin/decouplage_pannel_5.txt", "r")
for line in data:
line = line.rstrip()
line_in_array = line.split(";")
index = 0
for elt in line_in_array:
if(index != 0 and elt not in forbidden_id):
forbidden_id.append(elt)
index +=1
data.close()
data = open("/home/elwin/pannel_5_fichier_de_merde_analyse_qd_mm_.txt", "r")
for line in data:
line = line.rstrip()
line_in_array = line.split(";")
index = 0
for elt in line_in_array:
if(index != 0 and elt not in forbidden_id):
forbidden_id.append(elt)
index +=1
data.close()
folder_list = ["panel_5_FPS", "panel_5_IDIBELL", "panel_5_IRCCS", "PANEL_5_SAS", "PANEL_5_UBO", "panel_5_UCL"]
for x in xrange(0,number_of_files_to_evaluate):
valid_choice = False
while(not valid_choice):
center = folder_list[random.randint(0,len(folder_list)-1)]
fcs_files = glob.glob("/home/elwin/"+str(center)+"/*.fcs")
candidate_file = fcs_files[random.randint(0,len(fcs_files)-1)]
candidate_file_name = candidate_file.split("/")
candidate_file_name = candidate_file_name[-1].split("_")
if(candidate_file_name[2] not in forbidden_id):
os.system("cp "+str(candidate_file)+" trash/FCS_multicenter_validation_2/")
valid_choice = True
## perform validation -> false positive Score
print "[+] False positive Evaluation"
false_positive_count = 0
for test_file in glob.glob("trash/FCS_multicenter_validation_2/*.fcs"):
test_file_name = test_file.split("/")
test_file_name = test_file_name[-1].split("_")
center = test_file_name[3]
prediction_1 = detect_decouplage(test_file, "models/elector_1.pkl")
prediction_2 = detect_decouplage(test_file, "models/elector_2.pkl")
prediction_3 = detect_decouplage(test_file, "models/elector_3.pkl")
if(prediction_1[0] == "1"):
score_E1 += 1
if(prediction_2[0] == "1"):
score_E2 += 1
if(prediction_3[0] == "1"):
score_E3 += 1
prediction_list = [prediction_1,prediction_2,prediction_3]
prediction_final = str(max(set(prediction_list), key=prediction_list.count))
print "[+] "+str(center) +" : "+ str(prediction_1[0]) +" || "+str(prediction_2[0]) +" || "+str(prediction_3[0]) +" => "+prediction_final
log_file.write("FP,"+"[+] "+str(center) +" : "+ str(prediction_1[0]) +" || "+str(prediction_2[0]) +" || "+str(prediction_3[0]) +" => "+prediction_final+"\n")
if(str(prediction_final[0]) == "2" ):
false_positive_count += 1
false_positive_count = float(false_positive_count) / float(number_of_files_to_evaluate) * 100
print '[+] False Positive : '+str(false_positive_count)
log_file.write("FP,"+str(false_positive_count)+"\n")
## perform validation -> false negative score
print "[+] False Negative Evaluation"
false_negative_count = 0
for test_file in glob.glob("trash/FCS_milticenter_validation/*.fcs"):
test_file_name = test_file.split("/")
test_file_name = test_file_name[-1].split("_")
center = test_file_name[3]
prediction_1 = detect_decouplage(test_file, "models/elector_1.pkl")
prediction_2 = detect_decouplage(test_file, "models/elector_2.pkl")
prediction_3 = detect_decouplage(test_file, "models/elector_3.pkl")
if(prediction_1[0] == "2"):
score_E1 += 1
if(prediction_2[0] == "2"):
score_E2 += 1
if(prediction_3[0] == "2"):
score_E3 += 1
prediction_list = [prediction_1,prediction_2,prediction_3]
prediction_final = str(max(set(prediction_list), key=prediction_list.count))
print "[+] "+str(center) +" : "+ str(prediction_1[0]) +" || "+str(prediction_2[0]) +" || "+str(prediction_3[0]) +" => "+prediction_final
log_file.write("FN,"+"[+] "+str(center) +" : "+ str(prediction_1[0]) +" || "+str(prediction_2[0]) +" || "+str(prediction_3[0]) +" => "+prediction_final+"\n")