-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocomp_dev.py
More file actions
2395 lines (1748 loc) · 65.7 KB
/
autocomp_dev.py
File metadata and controls
2395 lines (1748 loc) · 65.7 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
##
## Try to find a smart solution to the compensation problem
## by smart i mean "pseudo linear" solution, without invoking
## big monsters like CNN
##
def generate_image_from_fcs(fcs_file):
"""
Generate image for all channel
combination in the fcs file
"""
## importation
import FlowCal
import matplotlib.pyplot as plt
## open fcs file
s = FlowCal.io.FCSData(fcs_file)
s = FlowCal.transform.to_rfi(s)
## exctract list of channels
channel_list = s.channels
## compute and save image
for channel_1 in channel_list:
for channel_2 in channel_list:
if(channel_1 != channel_2):
try:
png_file_name = str(fcs_file)+"_"+str(channel_1)+"_"+str(channel_2)+".png"
FlowCal.plot.density2d(s, channels=[str(channel_1), str(channel_2)], mode='scatter', yscale='logicle', xscale='logicle', ylim=-1000, xlim=(0,500000))
#FlowCal.plot.density2d(s, channels=[str(channel_1), str(channel_2)], ylim=-1000, xlim=(0,500000))
plt.savefig(png_file_name)
plt.close()
except:
pass
def randomy_split_dataset(input_data_file, proportion):
"""
Randomly split the input data file into 2 data file
according to proportion
"""
## importation
import random
## parameters
output_data_1_file = input_data_file.split(".")
output_data_1_file = output_data_1_file[0]+"_splited_1.csv"
output_data_2_file = output_data_1_file.replace("1", "2")
## get the number of lines in data file
case_cmpt = 0
input_data = open(input_data_file, "r")
for line in input_data:
case_cmpt +=1
case_cmpt = case_cmpt - 1 # drop the header
number_of_line_to_keep_in_data_1 = float(proportion)*case_cmpt
## write header
input_data = open(input_data_file, "r")
output_data_1 = open(output_data_1_file, "w")
output_data_2 = open(output_data_2_file, "w")
cmpt = 0
for line in input_data:
if(cmpt == 0):
output_data_1.write(line)
output_data_2.write(line)
cmpt += 1
input_data.close()
output_data_2.close()
output_data_1.close()
selected_lines = []
number_of_line_in_data_1 = 0
while(number_of_line_in_data_1 < number_of_line_to_keep_in_data_1):
input_data = open(input_data_file, "r")
output_data_1 = open(output_data_1_file, "a")
cmpt = 0
for line in input_data:
if(cmpt != 0):
dice = random.randint(0,100)
if(dice > 50 and number_of_line_in_data_1 < number_of_line_to_keep_in_data_1 and cmpt not in selected_lines):
output_data_1.write(line)
selected_lines.append(cmpt)
number_of_line_in_data_1 += 1
cmpt += 1
input_data.close()
output_data_1.close()
## write data 2
input_data = open(input_data_file, "r")
output_data_2 = open(output_data_2_file, "a")
cmpt = 0
for line in input_data:
if(cmpt != 0 and cmpt not in selected_lines):
output_data_2.write(line)
cmpt += 1
input_data.close()
output_data_2.close()
def plot_compensation_matrix(compensation_matrix):
"""
Generate an heatmap for the compensation matrix file
and save it under the results/image folder
Work on predicted, mannually compensated and uncompensated matrix
"""
## importation
import numpy
import matplotlib
import matplotlib.pyplot as plt
## Detect matrix type
## - uncompensated
## - predicted
## - Manually compensated
matrix_file_name = compensation_matrix.split("/")
matrix_file_name = matrix_file_name[-1]
matrix_file_name_in_array = matrix_file_name.split("_")
matrix_tag = matrix_file_name_in_array[-1]
if(matrix_tag not in ["uncompensated.txt", "predicted.txt"]):
matrix_tag = "compensated"
output_file_name = "results/images/compensation_matrix_heatmap_"+str(matrix_tag)+".png"
output_file_name = output_file_name.replace(".txt", "")
## Parse Matrix file
## init grid to display structure
matrix_grid = []
scalar = "NA"
for x in xrange(0,8):
matrix_grid.append([])
for y in xrange(0,8):
matrix_grid[x].append(scalar)
## Deal with uncompensated matrix
if(matrix_tag == "uncompensated.txt"):
matrix_data = open(compensation_matrix, "r")
cmpt = 0
pos_y = 0
for line in matrix_data:
line = line.rstrip()
if(cmpt > 0):
line_in_array = line.split(",")
index = 0
pos_x = 0
for scalar in line_in_array:
if(index > 0):
matrix_grid[pos_y][pos_x] = float(scalar)
pos_x += 1
index += 1
pos_y += 1
cmpt += 1
matrix_data.close()
## Deal with predicted matrix and
## mannualy compensated matrix
else:
matrix_data = open(compensation_matrix, "r")
cmpt = 0
pos_y = 0
for line in matrix_data:
line = line.rstrip()
if(cmpt > 0):
line_in_array = line.split("\t")
index = 0
pos_x = 0
for scalar in line_in_array:
if(index > 1):
matrix_grid[pos_y][pos_x] = float(scalar)/100.0
pos_x += 1
index += 1
pos_y += 1
cmpt += 1
matrix_data.close()
## Plot matrix
## get the label
x_label = ["FITC.A","PE.A","PC5.5.A","PC7.A","APC.A","APC.AF750.A","PB.A","KO.A"]
y_label = x_label
## get the grid
grid_to_display = numpy.asarray(matrix_grid)
## plot the stuff
fig, ax = plt.subplots()
im = ax.imshow(grid_to_display)
# We want to show all ticks...
ax.set_xticks(numpy.arange(len(x_label)))
ax.set_yticks(numpy.arange(len(y_label)))
# ... and label them with the respective list entries
ax.set_xticklabels(x_label)
ax.set_yticklabels(y_label)
# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(im, ticks=[numpy.amin(grid_to_display), numpy.amax(grid_to_display)])
cbar.ax.set_yticklabels([numpy.amin(grid_to_display), numpy.amax(grid_to_display)]) # vertically oriented colorbar
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
ax.set_title("Matrix compensation")
fig.tight_layout()
plt.savefig(output_file_name)
#plt.show()
def transpose_matrix_file(matrix_file):
"""
Transpose the matrix in matrix file,
work for predicted matrix, overwite the
matrix file
"""
# importation
import numpy
import shutil
## parameters
channel_list = ["FITC.A","PE.A","PC5.5.A","PC7.A","APC.A","APC.AF750.A","PB.A","KO.A"]
output_file_name = matrix_file.split("/")
output_file_name = output_file_name[-1].split(".")
output_file_name = output_file_name[0]+"_transposed."+output_file_name[1]
## init grid
matrix_grid = []
scalar = "NA"
for x in xrange(0,8):
grid_vector = []
for x in xrange(0,8):
grid_vector.append(scalar)
matrix_grid.append(grid_vector)
## Parse data
matrix_data = open(matrix_file, "r")
cmpt = 0
pos_y = 0
header = ""
prefix = ""
for line in matrix_data:
if(cmpt > 0):
line = line.rstrip()
line_in_array = line.split("\t")
index = 0
pos_x = 0
prefix = line_in_array[0]
for scalar in line_in_array:
if(index > 1):
matrix_grid[pos_y][pos_x] = float(scalar)
pos_x += 1
index += 1
pos_y += 1
else:
header = line
cmpt += 1
matrix_data.close()
## transpose matrix
matrix_grid = numpy.asarray(matrix_grid)
matrix_grid = matrix_grid.transpose()
## write new matrix file
transposed_matrix = open(output_file_name, "w")
transposed_matrix.write(header)
cmpt = 0
for vector in matrix_grid:
line_to_write = str(prefix)+"\t"+channel_list[cmpt]+"\t"
for scalar in vector:
line_to_write += str(scalar)+"\t"
line_to_write = line_to_write[:-1]
if(cmpt == 7):
transposed_matrix.write(line_to_write)
else:
transposed_matrix.write(line_to_write+"\n")
cmpt += 1
transposed_matrix.close()
## replace old matrix file transpose matrix file
shutil.copy(output_file_name, matrix_file)
def transpose_matrix_file_dev(matrix_file):
"""
Transpose the matrix in matrix file,
work for predicted matrix, overwite the
matrix file
"""
# importation
import numpy
import shutil
## parameters
channel_list = ["FITC.A","PE.A","PC5.5.A","PC7.A","APC.A","APC.AF750.A","PB.A","KO.A"]
output_file_name = matrix_file.split("/")
output_file_name = output_file_name[-1].split(".")
output_file_name = output_file_name[0]+"_transposed."+output_file_name[1]
## init grid
matrix_grid = []
scalar = "NA"
for x in xrange(0,8):
grid_vector = []
for x in xrange(0,8):
grid_vector.append(scalar)
matrix_grid.append(grid_vector)
## Parse data for predicted matrix file
matrix_data = open(matrix_file, "r")
cmpt = 0
pos_y = 0
header = ""
prefix = ""
for line in matrix_data:
line_in_array = line.split("\t")
## uncompensated matrix
if(len(line_in_array) == 1):
if(cmpt > 0):
line = line.rstrip()
line_in_array = line.split(",")
index = 0
pos_x = 0
prefix = "0"
for scalar in line_in_array:
if(index > 0):
matrix_grid[pos_y][pos_x] = float(scalar)
pos_x += 1
index += 1
pos_y += 1
else:
header = line
channel_list = header.rstrip()
channel_list = channel_list.split(",")
channel_list = channel_list[1:]
## predcted matrix
else:
if(cmpt > 0):
line = line.rstrip()
line_in_array = line.split("\t")
index = 0
pos_x = 0
prefix = line_in_array[0]
for scalar in line_in_array:
if(index > 1):
matrix_grid[pos_y][pos_x] = float(scalar)
pos_x += 1
index += 1
pos_y += 1
else:
header = line
cmpt += 1
matrix_data.close()
## transpose matrix
matrix_grid = numpy.asarray(matrix_grid)
matrix_grid = matrix_grid.transpose()
## write new matrix file
transposed_matrix = open(output_file_name, "w")
transposed_matrix.write(header)
cmpt = 0
for vector in matrix_grid:
line_to_write = str(prefix)+"\t"+channel_list[cmpt]+"\t"
for scalar in vector:
line_to_write += str(scalar)+"\t"
line_to_write = line_to_write[:-1]
if(cmpt == 7):
transposed_matrix.write(line_to_write)
else:
transposed_matrix.write(line_to_write+"\n")
cmpt += 1
transposed_matrix.close()
## replace old matrix file transpose matrix file
shutil.copy(output_file_name, matrix_file)
def load_data_for_training(uncompensated_fcs_folder, compensated_fcs_folder, matrix_compensated_folder):
"""
IN PROGRESS
"""
## importation
import glob
import os
## parameters
uncompensated_output = "data/fcs/raw/"
compensated_output = "data/fcs/compensated/"
matrix_output = "data/matrix/compensated/"
EXTRACTMATRIX_SCRIPT = "extractMatrix.R"
## copy all files in the uncompensated_folder
uncompensated_file_list = glob.glob(uncompensated_fcs_folder+"/*.fcs")
for uncompensated_file in uncompensated_file_list:
os.system("cp "+uncompensated_file+" "+uncompensated_output)
## copy all files in the compensated folder
compensated_file_list = glob.glob(compensated_fcs_folder+"/*.fcs")
for compensated_file in compensated_file_list:
os.system("cp "+compensated_file+" "+compensated_output)
## Extract matrix for uncompensated files
for fcs_file in glob.glob(uncompensated_output+"/*.fcs"):
os.system("Rscript "+EXTRACTMATRIX_SCRIPT+" "+str(fcs_file))
## copy all mannual compensated matrix files
matrix_file_list = glob.glob(matrix_compensated_folder+"/*.txt")
for matrix_file in matrix_file_list:
os.system("cp "+str(matrix_file)+" "+matrix_output)
def select_compensation_matrix():
"""
Find the good compensation files
and store them in the data folder,
by good I mean with an equivalent
uncompensated file
Start small
"""
## importation
import glob
import shutil
## get files list
uncompensated_matrix_files = glob.glob("data/matrix/uncompensated/*.txt")
compensated_matrix_files = glob.glob("/home/elrohir/COMPENSATION/*.txt")
output_directory = "data/matrix/compensated/"
found_cmpt = 0
for compensated_matrix in compensated_matrix_files:
clear_to_copy = False
## parse compensated matrix file name
compensated_matrix_name = compensated_matrix.split("/")
compensated_matrix_name_in_array = compensated_matrix_name[-1].split("_")
compensated_matrix_panel = compensated_matrix_name_in_array[1]
compensated_matrix_center = compensated_matrix_name_in_array[2]
compensated_matrix_ID = compensated_matrix_name_in_array[3]
compensated_matrix_ID = compensated_matrix_ID.split(".")
compensated_matrix_ID = compensated_matrix_ID[0]
for uncompensated_matrix in uncompensated_matrix_files:
## parse uncompensated matrix file name
uncompensated_matrix_name = uncompensated_matrix.split("/")
uncompensated_matrix_name_in_array = uncompensated_matrix_name[-1].split("_")
uncompensated_matrix_panel = uncompensated_matrix_name_in_array[1]
uncompensated_matrix_center = uncompensated_matrix_name_in_array[2]
uncompensated_matrix_ID = uncompensated_matrix_name_in_array[3]
## test the files, get the "goods one"
if(uncompensated_matrix_center == compensated_matrix_center and uncompensated_matrix_panel == compensated_matrix_panel and uncompensated_matrix_ID == compensated_matrix_ID):
clear_to_copy = True
if(clear_to_copy):
shutil.copy(compensated_matrix, output_directory+str(compensated_matrix_name[-1]))
found_cmpt += 1
print "[*] Retrieve "+str((float(found_cmpt)/float(len(uncompensated_matrix_files))*100)) +"%"
def compute_delta_matrix(matrix_1, matrix_2, output_matrix):
"""
compute and write a new matrix from matrix 1 and 2
which is the "delta matrix", represent the variation
at each position
write the result in an output file given ny the
output_matrix parameters
matrix_1 correspond to the uncompensated matrix
matrix_2 correspond to the compensated matrix
"""
## deal with the uncompensated matrix
matrix_1_data = open(matrix_1, "r")
matrix_1_value = []
cmpt = 0
for line in matrix_1_data:
if(cmpt != 0):
vector = []
line = line.rstrip()
line_in_array = line.split(",")
index = 0
for scalar in line_in_array:
if(index != 0):
vector.append(scalar)
index += 1
matrix_1_value.append(vector)
cmpt += 1
matrix_1_data.close()
## deal with the compensated matrix
matrix_2_data = open(matrix_2, "r")
matrix_2_value = []
cmpt = 0
for line in matrix_2_data:
line = line.rstrip()
if(cmpt != 0):
vector = []
line_in_array = line.split("\t")
index = 0
for scalar in line_in_array:
if(index >= 2):
vector.append(float(scalar)/100.0)
index += 1
matrix_2_value.append(vector)
cmpt += 1
matrix_2_data.close()
## compute the delta matrix
delta_matrix = []
pos_y = 0
for vector in matrix_1_value:
delta_vector = []
pos_x = 0
for scalar in vector:
compensated_scalar = matrix_2_value[pos_y][pos_x]
delta_scalar = float(compensated_scalar) - float(scalar)
delta_vector.append(delta_scalar)
pos_x += 1
delta_matrix.append(delta_vector)
pos_y += 1
## write the delta matrix in a csv file
delta_matrix_file = open(output_matrix, "w")
cmpt = 0
for vector in delta_matrix:
line_to_write = ""
for scalar in vector:
line_to_write += str(scalar)+","
line_to_write = line_to_write[:-1]
if(cmpt < len(delta_matrix)):
delta_matrix_file.write(line_to_write+"\n")
else:
delta_matrix_file.write(line_to_write)
cmpt +=1
delta_matrix_file.close()
def generate_all_delta_matrix():
"""
Generate all delta matrix from matrix
found in the data folder
"""
## importation
import glob
## get files list
uncompensated_matrix_files = glob.glob("data/matrix/uncompensated/*.txt")
compensated_matrix_files = glob.glob("data/matrix/compensated/*.txt")
output_directory = "data/matrix/delta/"
found_cmpt = 0
for compensated_matrix in compensated_matrix_files:
## parse compensated matrix file name
compensated_matrix_name = compensated_matrix.split("/")
compensated_matrix_name_in_array = compensated_matrix_name[-1].split("_")
compensated_matrix_panel = compensated_matrix_name_in_array[1]
compensated_matrix_center = compensated_matrix_name_in_array[2]
compensated_matrix_ID = compensated_matrix_name_in_array[3]
compensated_matrix_ID = compensated_matrix_ID.split(".")
compensated_matrix_ID = compensated_matrix_ID[0]
for uncompensated_matrix in uncompensated_matrix_files:
## parse uncompensated matrix file name
uncompensated_matrix_name = uncompensated_matrix.split("/")
uncompensated_matrix_name_in_array = uncompensated_matrix_name[-1].split("_")
uncompensated_matrix_panel = uncompensated_matrix_name_in_array[1]
uncompensated_matrix_center = uncompensated_matrix_name_in_array[2]
uncompensated_matrix_ID = uncompensated_matrix_name_in_array[3]
## test the files, get the "goods one"
if(uncompensated_matrix_center == compensated_matrix_center and uncompensated_matrix_panel == compensated_matrix_panel and uncompensated_matrix_ID == compensated_matrix_ID):
delta_matrix = output_directory+str(compensated_matrix_name[-1])
delta_matrix = delta_matrix.split(".")
delta_matrix = str(delta_matrix[0])+"_delta."+str(delta_matrix[1])
compute_delta_matrix(uncompensated_matrix, compensated_matrix, delta_matrix)
def get_untouched_position():
"""
Loop over the delta matrix file and find the
position that are left untouched by the compensation
(i.e delta = 0)
display the grid in the console
return list of position
"""
## importation
import glob
## init display matrix
display_matrix = []
untouched_char = "#"
touched_char = "-"
for x in xrange(0,8):
display_matrix.append([])
for y in xrange(0,8):
display_matrix[x].append(untouched_char)
## loop over all delta matrix
delta_matrix_files = glob.glob("data/matrix/delta/*.txt")
for delta_matrix in delta_matrix_files:
current_delta_matrix = []
data_file = open(delta_matrix, "r")
pos_y = 0
for line in data_file:
line = line.rstrip()
line_in_array = line.split(",")
pos_x = 0
for scalar in line_in_array:
if(float(scalar) != 0.0):
display_matrix[pos_y][pos_x] = touched_char
pos_x += 1
pos_y += 1
data_file.close()
## Display grid
untouched_position = []
pos_y = 0
for vector in display_matrix:
line_to_display = ""
pos_x = 0
for scalar in vector:
line_to_display += " " + str(scalar)+" "
if(scalar == untouched_char):
untouched_position.append((pos_y,pos_x))
pos_x += 1
print line_to_display
pos_y += 1
print "="*23
## return untouched position
return untouched_position
def draw_variance_matrix(data_folder):
"""
Get the distribution of delta values for each slot of
the compensation matrix from all delta matrix, then compute
variance of each distribution and plot an heatmap to highlight
the spot with high variance
"""
## importation
import glob
import numpy
import matplotlib
import matplotlib.pyplot as plt
## init distribution matrix
position_to_distribution = {}
for x in xrange(0,8):
for y in xrange(0,8):
key = str(y)+"_"+str(x)
position_to_distribution[key] = []
## init variance matrix
variance_matrix = []
variance = "NA"
for x in xrange(0,8):
variance_matrix.append([])
for y in xrange(0,8):
variance_matrix[x].append(variance)
## loop over all delta matrix
delta_matrix_files = glob.glob(data_folder+"/*.txt")
for delta_matrix in delta_matrix_files:
data_file = open(delta_matrix, "r")
pos_y = 0
for line in data_file:
line = line.rstrip()
line_in_array = line.split(",")
pos_x = 0
for scalar in line_in_array:
key = str(pos_y)+"_"+str(pos_x)
position_to_distribution[key].append(float(scalar))
pos_x += 1
pos_y += 1
data_file.close()
## compute variance matrix
for y in xrange(0,8):
for x in xrange(0,8):
key = str(x)+"_"+str(y)
distribution = position_to_distribution[key]
variance = numpy.var(distribution)
variance_matrix[y][x] = variance
## plot heatmap
## get the label
x_label = ["FITC.A","PE.A","PC5.5.A","PC7.A","APC.A","APC.AF750.A","PB.A","KO.A"]
y_label = x_label
## get the grid
grid_to_display = numpy.asarray(variance_matrix)
## plot the stuff
fig, ax = plt.subplots()
im = ax.imshow(grid_to_display)
# We want to show all ticks...
ax.set_xticks(numpy.arange(len(x_label)))
ax.set_yticks(numpy.arange(len(y_label)))
# ... and label them with the respective list entries
ax.set_xticklabels(x_label)
ax.set_yticklabels(y_label)
# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(im, ticks=[numpy.amin(grid_to_display), numpy.amax(grid_to_display)])
cbar.ax.set_yticklabels([numpy.amin(grid_to_display), numpy.amax(grid_to_display)]) # vertically oriented colorbar
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
ax.set_title("Matrix compensation variance")
fig.tight_layout()
plt.show()
def perform_linear_regression(channel_a, channel_b):
"""
Perform linear regression on specific slot for
the compensation matrix
=> not the best solution, uncompensated matrix are identical
"""
## impotation
import glob
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
channel_list = ["FITC.A","PE.A","PC5.5.A","PC7.A","APC.A","APC.AF750.A","PB.A","KO.A"]
## check the target slot
target_x = "NA"
target_y = "NA"
index = 0
for channel in channel_list:
if(channel_a == channel):
target_y = index
if(channel_b == channel):
target_x = index
index += 1
## get uncompensated coordinates
uncompensated_vector = []
uncompensated_files = glob.glob("data/matrix/uncompensated/*.txt")
for uncompensated_matrix in uncompensated_files:
cmpt = 0
pos_y = 0
data_file = open(uncompensated_matrix, "r")
for line in data_file:
if(cmpt != 0):
line = line.rstrip()
line_in_array = line.split(",")
pos_x = 0
index = 0
for elt in line_in_array:
if(index > 0):
if(pos_x == target_x and pos_y == target_y):
uncompensated_vector.append(elt)
pos_x += 1
index += 1
pos_y += 1
cmpt += 1
data_file.close()
## get compensated coordinates
compensated_vector = []
compensated_files = glob.glob("data/matrix/compensated/*.txt")
for compensated_matrix in compensated_files:
cmpt = 0
pos_y = 0
data_file = open(compensated_matrix, "r")
for line in data_file:
if(cmpt != 0):
line = line.rstrip()
line_in_array = line.split("\t")
pos_x = 0
index = 0
for elt in line_in_array:
if(index > 1):
if(pos_x == target_x and pos_y == target_y):
compensated_vector.append(float(elt)/100.0)
pos_x += 1
index += 1
pos_y += 1
cmpt += 1
data_file.close()
## Perform linear regression
## split data into training an testing
X_train = uncompensated_vector[:-50]
X_test = uncompensated_vector[-50:]
X_train_formated = []
for scalar in X_train:
X_train_formated.append([float(scalar)])
X_test_formated = []
for scalar in X_test:
X_test_formated.append([float(scalar)])
Y_train = compensated_vector[:-50]
Y_test = compensated_vector[-50:]
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit(X_train_formated, Y_train)
# Make predictions using the testing set
Y_pred = regr.predict(X_test_formated)
# The coefficients
print "Coefficients: " +str(regr.coef_)
# The mean squared error
print("Mean squared error: %.2f"
% mean_squared_error(Y_test, Y_pred))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(Y_test, Y_pred))
"""
# Plot outputs
plt.scatter(X_test_formated, Y_test, color='black')
plt.plot(X_test_formated, Y_pred, color='blue', linewidth=3)
plt.xticks(())
plt.yticks(())
plt.show()
"""
def get_custum_coeff(channel_a, channel_b):
"""
get custum score from the delta matrix
"""
## impotation
import glob
import numpy
channel_list = ["FITC.A","PE.A","PC5.5.A","PC7.A","APC.A","APC.AF750.A","PB.A","KO.A"]
## check the target slot
target_x = "NA"
target_y = "NA"
index = 0
for channel in channel_list:
if(channel_a == channel):
target_x = index
if(channel_b == channel):
target_y = index
index += 1
## get uncompensated coordinates
delta_vector = []
delta_files = glob.glob("data/matrix/delta/*.txt")
for delta_matrix in delta_files:
pos_y = 0
data_file = open(delta_matrix, "r")
for line in data_file:
line = line.rstrip()
line_in_array = line.split(",")
pos_x = 0
for elt in line_in_array:
if(pos_x == target_x and pos_y == target_y):
delta_vector.append(float(elt))
pos_x += 1
pos_y += 1
data_file.close()
## Youhou ...
score = numpy.mean(delta_vector)
return score
def create_correction_values():
"""
Create custom correction values for all
slot in compensation matrix
return a dictionnary