-
Notifications
You must be signed in to change notification settings - Fork 0
/
Galaxy_raw_Data_Array_MTDDB.py
1705 lines (1375 loc) · 62.9 KB
/
Galaxy_raw_Data_Array_MTDDB.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
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
import pandas as pd
import os, argparse
import math as mt
import numpy as np
import csv
import json
from datetime import datetime
from prettytable import PrettyTable
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
import timeit
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# HOW TO RUN OVER ONE SINGLE FILE
# python3 Galaxy_raw_Data_Array_MTDDB.py --data ArrayData/812* --array 812
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
start = timeit.default_timer()
#+++++++++++++++++++++++++++++++++++++++++++
# Parsing - Date - Producers definifition
#+++++++++++++++++++++++++++++++++++++++++++
parser = argparse.ArgumentParser()
parser.add_argument('--data',dest='data', help='data file *.TXT', type=str,required=True,default=False)
parser.add_argument('--array',dest='array',required=False,default=False)
args = parser.parse_args()
#+++++++++++++++++++++++++++++++++++++
# filename: 415_2022-05-04-12-52.txt
# Date and time as 2022-05-04-12-52
# Run001_416_2023-01-19-12-40_OLD.TXT
#+++++++++++++++++++++++++++++++++++++
data = args.data
data = data[:-2]
info = data.split('_')
run = info[0]
args.array = barcode = info[1]
date = info[2]
tag = info[3]
date = date.replace('.T','')
data = data.replace('.T','')
tag = tag.replace('.T','')
print('Filename :', data)
print('Barcode :', barcode)
print('Tag :', tag)
print('RunNumber :', run)
#print('Barcode :', barcode.split('/')[2]) # this when we run it using runAll script otherwise we have to replace with print('Barcode :', barcode)
print('Date in filename :', date)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# convert datetime string into date,month,day and
# hours:minutes:and seconds format using strptime
#++++++++++++++++++++++++++++++++++++++++++++++++++
time = datetime.strptime(date, '%Y-%m-%d-%H-%M') #in use time format
timestamp = time.strftime('%Y-%m-%d %H:%M:%S') #new time format
print('Time after conversion :',timestamp)
print('Info LYSO Array: {}'.format(str(args)))
#+++++++++++++++++++++++++++++++++++++++++++
# JUST FOR REFERENCE: NOMINAL DIMENSIONS
#+++++++++++++++++++++++++++++++++++++++++++
#----------------------------------------------------------------------------
# Array type | w | t | L
#----------------------------------------------------------------------------
# 1 | 51.50+-0.10 | 4.05+-0.10 | 55(56.30)+-0.020
# 2 | 51.50+-0.10 | 3.30+-0.10 | 55(56.30)+-0.020
# 3 | 51.50+-0.10 | 2.70+-0.10 | 55(56.30)+-0.020
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
# Bar type | w | t | L
#----------------------------------------------------------------------------
# 1 | 3.12+-0.10 | 3.75+-0.10 | 55(56.30)+-0.020
# 2 | 3.12+-0.10 | 3.00 (3.30)+-0.10 | 55(56.30)+-0.020 MS3 w and w/o ESR
# 3 | 3.12+-0.10 | 2.40 +-0.10 | 55(56.30)+-0.020 MS3 w/o ESR
#----------------------------------------------------------------------------
#+++++++++++++++++++++++++++++++++++++++++++
# Tolerances and ranges definition
#+++++++++++++++++++++++++++++++++++++++++++
#length_m = 56.30 #mm #old MS3/OPT
#length_min = 56.2 #for plot only #old MS3/OPT
#length_max = 56.4 #for plot only #old MS3/OPT
length_m = 55.0 #mm
e_low_length = 0.02 #mm
e_high_length = 0.02 #mm
length_min = 54.9 #for plot only
length_max = 55.1 #for plot only
length_bin = 40 #for plot only
width_m = 51.50 #mm
e_low_width = 0.10 #mm
e_high_width = 0.10 #mm
width_min = 51.10 #for plot only
width_max = 51.90 #for plot only
width_bin = 80 #for plot only
thickness_m = 0. #mm
e_low_thickness = 0. #mm
e_high_thickness = 0. #mm
thickness_min = 0. #for plot only
thickness_max = 0. #for plot only
thickness_bin = 0. #for plot only
if(type==1):
thickness_m = 4.05 #mm
e_low_thickness = 0.10 #mm
e_high_thickness = 0.10 #mm
thickness_min = 3.65 #for plot only
thickness_max = 4.45 #for plot only
thickness_bin = 80 #for plot only
if(type==2):
thickness_m = 3.30 #mm
e_low_thickness = 0.10 #mm
e_high_thickness = 0.10 #mm
thickness_min = 2.90 #for plot only
thickness_max = 3.70 #for plot only
thickness_bin = 80 #for plot only
if(type==3):
thickness_m = 2.70 #mm
e_low_thickness = 0.10 #mm
e_high_thickness = 0.10 #mm
thickness_min = 2.30 #for plot only
thickness_max = 3.10 #for plot only
thickness_bin = 80 #for plot only
#+++++++++++++++++++++++++
# Reading data
#+++++++++++++++++++++++++
df_LS = pd.DataFrame(columns=['X', 'Y', 'Z'])
counter_LS = 0
df_LN = pd.DataFrame(columns=['X', 'Y', 'Z'])
counter_LN = 0
df_FS = pd.DataFrame(columns=['X', 'Y', 'Z'])
counter_FS = 0
df_LO = pd.DataFrame(columns=['X', 'Y', 'Z'])
counter_LO = 0
df_LE = pd.DataFrame(columns=['X', 'Y', 'Z'])
counter_LE = 0
for line in open(args.data,errors='ignore'):
line = line.rstrip()
splitline = line.split()
n_elements = len(splitline)
if(n_elements<5):
continue
n = x = y = z = 0
n = splitline[0]
x = splitline[1]
y = splitline[2]
z = splitline[3]
side = splitline[0]
if( ('_LS' in side) or ('_LN' in side) or ('_FS' in side) or ('_LO' in side) or ('_LE' in side) ):
n = splitline[1]
x = float(splitline[2])
y = float(splitline[3])
z = float(splitline[4])
if('_LS' in side):
counter_LS = 1
if('_LN' in side):
counter_LN = 1
if('_FS' in side):
counter_FS = 1
if('_LO' in side):
counter_LO = 1
if('_LE' in side):
counter_LE = 1
#++++++++++++++++++++++++++++++++++++++++++++++
# HERE IS WHERE WE SELECT GALAXY POINTS
#++++++++++++++++++++++++++++++++++++++++++++++
if(counter_LS>0 and counter_LS<40):
values_to_add = {'X': x, 'Y': y, 'Z': z}
row_to_add = pd.Series(values_to_add, name=n)
df_LS = df_LS.append(row_to_add)
counter_LS = counter_LS + 1
if(counter_LN>0 and counter_LN<40):
values_to_add = {'X': x, 'Y': y, 'Z': z}
row_to_add = pd.Series(values_to_add, name=n)
df_LN = df_LN.append(row_to_add)
counter_LN = counter_LN + 1
if(counter_FS>0 and counter_FS<104):
values_to_add = {'X': x, 'Y': y, 'Z': z}
row_to_add = pd.Series(values_to_add, name=n)
df_FS = df_FS.append(row_to_add)
counter_FS = counter_FS + 1
if(counter_LO>0 and counter_LO<12):
values_to_add = {'X': x, 'Y': y, 'Z': z}
row_to_add = pd.Series(values_to_add, name=n)
df_LO = df_LO.append(row_to_add)
counter_LO = counter_LO + 1
if(counter_LE>0 and counter_LE<12):
values_to_add = {'X': x, 'Y': y, 'Z': z}
row_to_add = pd.Series(values_to_add, name=n)
df_LE = df_LE.append(row_to_add)
counter_LE = counter_LE + 1
#++++++++++++++++++++++++++++++++++++++++++++++++++
# FINAL DATASETS TO ANALYZE
#++++++++++++++++++++++++++++++++++++++++++++++++++
df_LS = df_LS.astype({'X': float, 'Y': float, 'Z': float})
df_LN = df_LN.astype({'X': float, 'Y': float, 'Z': float})
df_FS = df_FS.astype({'X': float, 'Y': float, 'Z': float})
df_LO = df_LO.astype({'X': float, 'Y': float, 'Z': float})
df_LE = df_LE.astype({'X': float, 'Y': float, 'Z': float})
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# REORDERED DATASETS TO BE CONSISTENT WITH THE OLD CODE
# LS -> ascending X LN -> descending X
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
df_LS = df_LS.reindex(['37','38','39','34','35','36','31','32','33','28','29','30','25','26','27','22','23','24',
'19','20','21','16','17','18','13','14','15','10','11','12','7','8','9','4','5','6',
'1','2','3'])
df_LS.index = pd.RangeIndex(1,1 + len(df_LS))
df_LN = df_LN.reindex(['37','38','39','34','35','36','31','32','33','28','29','30','25','26','27','22','23','24',
'19','20','21','16','17','18','13','14','15','10','11','12','7','8','9','4','5','6',
'1','2','3'])
df_LN.index = pd.RangeIndex(1,1 + len(df_LN))
#+++++++++++++++++++++++++++++++
# VERSION WITH WRAPPING
#+++++++++++++++++++++++++++++++
l_length = [] #l1_length+l2_length
l1_length = [] #length 1st row of measurements
l2_length = [] #length 2nd row of measurements
wrap_length =[] #length wrapping included
#+++++++++++++++++++++++++++++++++++++++++++++++++++++
# South and North definition for plots LS-LN length
#+++++++++++++++++++++++++++++++++++++++++++++++++++++
y_sud1 = []
y_nord1 = []
y_sud2 = []
y_nord2 = []
y_sud = []
y_nord = []
south_side = []
north_side = []
#++++++++++++++++++++++++++++++++++
# WRAPPING EXCESS MEASUREMENT
#++++++++++++++++++++++++++++++++++
for point in range(3,31,3):
y_wrap_LS = float(df_LS.loc[point]['Y'])
y_wrap_LN = float(df_LN.loc[33-point]['Y'])
wrapping = y_wrap_LN - y_wrap_LS
wrap_length.append(wrapping)
# print('++++++++++++++++++++++++++')
# print(' WRAPPING SUMMARY TABLE ')
# print('++++++++++++++++++++++++++')
# p = PrettyTable(['Point LS','Point LN','y_wrap_LS', 'y_wrap_LN','length w/wrapping','Z LS','Z LN','X LS','X LN'])
# p.add_row([point, (33-point),y_wrap_LS,y_wrap_LN,wrapping,df_LS.loc[(point)]['Z'],df_LN.loc[(33-point)]['Z'],df_LS.loc[(point)]['X'],df_LN.loc[(33-point)]['X']])
# print(p)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# POINTS ASSOCIATION FOR SINGLE BAR length MEASUREMENT
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# print('++++++++++++++++++++++++++')
# print(' length1 SUMMARY TABLE ')
# print('++++++++++++++++++++++++++')
# p = PrettyTable(['Point LS','Point LN','y_LS', 'y_LN','length1','Z LS','Z LN','X LS','X LN'])
for point in range(1,31,3):
y_LS = float(df_LS.loc[(point)]['Y'])
y_LN = float(df_LN.loc[(29-point)]['Y'])
length1 = y_LN - y_LS
l1_length.append(length1)
# This is just for length histo/plot LS vs LN
sud1 = y_LS
nord1 = y_LN
y_sud1.append(sud1)
y_nord1.append(nord1)
# p.add_row([(point), (29-point),y_LS,y_LN,length1,df_LS.loc[(point)]['Z'],df_LN.loc[(29-point)]['Z'],df_LS.loc[(point)]['X'],df_LN.loc[(29-point)]['X']])
# print(p)
# print('++++++++++++++++++++++++++')
# print(' length2 SUMMARY TABLE ')
# print('++++++++++++++++++++++++++')
# p = PrettyTable(['Point LS','Point LN','y_LS', 'y_LN','length2','Z LS','Z LN','X LS','X LN'])
for point1 in range(2,31,3):
y_LS = float(df_LS.loc[(point1)]['Y'])
y_LN = float(df_LN.loc[(31-point1)]['Y'])
length2 = y_LN - y_LS
l2_length.append(length2)
# This is just for length histo/plot LS vs LN
sud2 = y_LS
nord2 = y_LN
y_sud2.append(sud2)
y_nord2.append(nord2)
# p.add_row([(point1), (31-point1),y_LS,y_LN,length2,df_LS.loc[(point1)]['Z'],df_LN.loc[(31-point1)]['Z'],df_LS.loc[(point1)]['X'],df_LN.loc[(31-point1)]['X']])
# print(p)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Combine length in two different ranges
#++++++++++++++++++++++++++++++++++++++++++++++++++
l_length = l1_length + l2_length
#++++++++++++++++++++++++++++++++++++
# This is for LS-LN Plots
#++++++++++++++++++++++++++++++++++++
south_side1 = np.array(y_sud1)
south_side2 = np.array(y_sud2)
north_side1 = np.array(y_nord1)
north_side2 = np.array(y_nord2)
south_side = []
north_side = []
south_side = np.concatenate((south_side1, south_side2))
north_side = np.concatenate((north_side1, north_side2))
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# REMINDER: points used to compute the single bars length
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# south_side1 = np.array(y_sud1) # 1st set of points on LS
# south_side2 = np.array(y_sud2) # 2nd set of points on LS
# north_side1 = np.array(y_nord1) # 1st set of points on LN
# north_side2 = np.array(y_nord2) # 2nd set of points on LN
# south_side = []
# north_side = []
# south_side = np.concatenate((south_side1, south_side2)) #combine the 2 set of measurements on LS
# north_side = np.concatenate((north_side1, north_side2)) #combine the 2 set of measurements on LN
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sud = south_side
nord = north_side
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# REMINDER:
# l_length = [] #l1_length+l2_length
# l1_length = [] #length 1st row of measurements
# l2_length = [] #length 2nd row of measurements
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
np_length_all = np.asarray(l_length)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# SINGLE BARS MEAN
#++++++++++++++++++++++++++++++++++++++++++++++++++
np_length = np.mean(np_length_all.reshape(2, 10), axis=0) #.reshape(2, 10) --> 10 columns and 2 rows
np_length = np_length.round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# SINGLE BARS STD
#++++++++++++++++++++++++++++++++++++++++++++++++++
np_length_std = np.std(np_length_all.reshape(-1, 2), axis=1) #.reshape(-1, 2) --> 2 columns and n rows
np_length_std = np_length_std.round(3)
np_wrap_length_all = np.asarray(wrap_length)
np_wrap_length = np.mean(np_wrap_length_all.reshape(-1, 2), axis=1)
np_wrap_length = np_wrap_length.round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# This is just for length histo/plot LS vs LN
#++++++++++++++++++++++++++++++++++++++++++++++++++
np_lato_sud = np.asarray(y_sud)
np_lato_nord = np.asarray(y_nord)
np_yLS = np_lato_sud.round(3)
np_yLN = np_lato_nord.round(3)
np_lato_sud = np_lato_sud.reshape(-1, 1)
np_lato_nord = np_lato_nord.reshape(-1, 1)
#++++++++++++++++++++++++++++++++++++++++++
# Bar length (Mean +/- std)
#++++++++++++++++++++++++++++++++++++++++++
length_mean = np_length.mean().round(3)
length_std = np_length.std().round(3)
#++++++++++++++++++++++++++++++++++++++++++
# With Wrap length
#++++++++++++++++++++++++++++++++++++++++++
wrap_length_mean = np_wrap_length.mean().round(3)
wrap_length_std = np_wrap_length.std().round(3)
#+++++++++++++++++++++++++++++++++++++++++
# Only wrap length
#+++++++++++++++++++++++++++++++++++++++++
wrap = (wrap_length_mean - length_mean).round(3)
print('------------------------------------')
print(' Wrapping Excess:',str(wrap) +' mm',)
print('------------------------------------')
print('')
#-------------------------------------------------------------------------------------
# ADDITIONAL OUTPUT: OUT OF RANGE/TOLERANCE: uncomment this if you want the print out
#-------------------------------------------------------------------------------------
n_length_outOfPlotRange = np.count_nonzero( (np_length < length_min) | (np_length > length_max) )
n_length_outOfTolerance = np.count_nonzero( (np_length < length_m-e_low_length) | (np_length > length_m+e_high_length))
#print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
#print ('Bars out plot range:',n_length_outOfPlotRange)
#print ('Bars out tolerance:', n_length_outOfTolerance)
#print ('Number of points:',np_length_all.size)
#print ('Number of points after mean:',np_length.size)
#print ('All length:',np_length_all)
##print (np_length_all.reshape(-1, 2))
#print ('Single bar Mean length : ',np_length)
#print('Bars Mean length : ',length_mean)
#print ('Single bar Mean length Std : ',np_length_std)
#print('Bars std length : ',length_std)
#print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
p = PrettyTable(['Array','Bar length out Plot range','Bar length out of Tolerance'])
p.add_row([barcode+str('_')+tag, str(n_length_outOfPlotRange),str(n_length_outOfTolerance)])
print(p)
print('')
#+++++++++++++++++++++++++++++++++++++++++++++++++++++
# PLOT 2: BarOutRange - single bar out of tolerance
#+++++++++++++++++++++++++++++++++++++++++++++++++++++
fig, ax = plt.subplots()
ax.plot(np_length,linestyle = 'dashed', marker = 'o', label='Bar length')
# Define bbox style
box_style=dict(boxstyle='round', facecolor='green', alpha=0.3)
box_style1=dict(boxstyle='round', facecolor='yellow', alpha=0.5)
plt.text(0.3, 55.06, '# Bars out plot range = ' + str(n_length_outOfPlotRange),fontsize = 10,bbox=box_style)
plt.text(0.3, 55.08, '# Bars out of tolerance = ' + str(n_length_outOfTolerance), fontsize = 10,bbox=box_style1)
plt.xlim([-3.5,12.5]) #to see all bars
plt.ylim(length_min,length_max)
plt.grid()
plt.legend()
plt.ylabel('length [mm]')
plt.xlabel('# Bars')
bars = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
ax.set_xticks(np.arange(-3,len(np_length)+3,1))
ax.set_xticklabels(bars)
color = 'tab:red'
#-----> For Production
x1, y1 = [-3.5, 12.5], [54.67, 54.67]
x2, y2 = [-3.5, 12.5], [54.65, 54.65]
x3, y3 = [-3.5, 12.5], [54.73, 54.73]
#------> For MS3 and OPT
#x1, y1 = [-3.5, 12.5], [55., 55.]
#x2, y2 = [-3.5, 12.5], [54.98, 54.98]
#x3, y3 = [-3.5, 12.5], [55.02, 55.02]
plt.plot(x1, y1,color=color,linestyle='dashed')
plt.plot(x2, y2,color='k',linestyle='dashed')
plt.plot(x3, y3,color='k',linestyle='dashed')
plt.title('CMS MTD' + str(args.array) + ' - Bar length')
fig.savefig(run+str('_ARRAY')+barcode+str('_')+tag+'_BarOutRange.pdf',bbox_inches='tight')
#fig.savefig(str(args.array)+str('_')+tag+'_BarOutRange.pdf',bbox_inches='tight')
# Set axes limit
plt.ylim(54.9,55.1)
plt.ylim(length_min,length_max)
#plt.grid()
plt.legend()
#plt.show() #uncomment this if you want display plots while running code
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# LENGTH MEASUREMENTS WITH WRAPPING AND ALL BARS
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
max_y_LS = np.amax(df_LS['Y'].to_numpy()) #considering wrapping
min_y_LS = np.amin(df_LS['Y'].to_numpy()) #considering wrapping
max_y_LN = np.amax(df_LN['Y'].to_numpy()) #considering wrapping
min_y_LN = np.amin(df_LN['Y'].to_numpy()) #considering wrapping
mean_y_LN = (df_LN['Y'].to_numpy()).mean().round(3)
mean_y_LS = (df_LS['Y'].to_numpy()).mean().round(3)
# Spread on Y on north/sud side (length)
std_y_LS = (df_LS['Y'].to_numpy()).std().round(3)
std_y_LN = (df_LN['Y'].to_numpy()).std().round(3)
std_deltay = round(mt.sqrt(std_y_LS**2+std_y_LN**2),3)
# MAX LENGTH
array_length_max = (max_y_LN - min_y_LS).round(3)
# MEAN LENGTH
array_length_mean = (mean_y_LN - mean_y_LS).round(3)
array_length_mean_std = round(mt.sqrt( (std_y_LS/ mt.sqrt((df_LS['Y'].to_numpy()).size) )**2
+ (std_y_LN/mt.sqrt((df_LN['Y'].to_numpy()).size) )**2 ) , 3)
# LENGTH MAX VAR
delta_y_LN = (max_y_LN - min_y_LN).round(3)
delta_y_LS = (max_y_LS - min_y_LS).round(3)
# print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
# print('------ L with wrapping and all points (39 points on each side) ------')
# print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
# p = PrettyTable(['Array','MaxVar Y LS','MaxVar Y LN','Mean Y LS','Mean Y LN','Spread Y LS/LN', 'Max array length size (Y)' ])
# p.add_row([barcode.split('/')[1], str(delta_y_LS)+' mm', str(delta_y_LN)+' mm', str(mean_y_LS)+' mm',str(mean_y_LN)+' mm',str(std_deltay)+' mm',str(array_length_max)+' mm'])
# print(p)
# print('')
# print('MAX_LN:',max_y_LN)
# print('MIN_LN:',min_y_LN)
# print('MAXVAR_LN:',delta_y_LN)
# print('')
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# LENGTH MEASUREMENTS ON ALL BARS W/O WRAPPING
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sud = df_LS['Y'].to_numpy()
sud = np.delete(sud,(2,5,8,11,14,17,20,23,26,29,32,35,38),axis=0) #not considering wrapping
nord = df_LN['Y'].to_numpy()
nord = np.delete(nord,(2,5,8,11,14,17,20,23,26,29,32,35,38),axis=0) #not considering wrapping
max_y_LS_nowr = np.amax(sud)
min_y_LS_nowr = np.amin(sud)
max_y_LN_nowr = np.amax(nord)
min_y_LN_nowr = np.amin(nord)
# Spread on Y on north/sud side (length)
std_y_LS_nowr = (sud.std().round(3))
std_y_LN_nowr = (nord.std().round(3))
std_deltay_nowr = round(mt.sqrt(std_y_LS_nowr**2+std_y_LN_nowr**2),3)
# MAX LENGTH W/O WRAPPING
array_length_max_nowr = (max_y_LN_nowr - min_y_LS_nowr).round(3)
# MEAN LENGTH W/O WRAPPING
mean_y_LS_nowr = (sud.mean().round(3))
mean_y_LN_nowr = (nord.mean().round(3))
array_length_mean_nowr = (mean_y_LN_nowr - mean_y_LS_nowr).round(3)
array_length_mean_std_nowr = round(mt.sqrt( (std_y_LS_nowr/ mt.sqrt(sud.size) )**2
+ (std_y_LN_nowr/mt.sqrt(nord.size) )**2 ) , 3)
# LMAXVAR along Y
delta_y_LS_nowr = (max_y_LS_nowr - min_y_LS_nowr).round(3)
delta_y_LN_nowr= (max_y_LN_nowr - min_y_LN_nowr).round(3)
#----------------------------------------------------------------------
# LENGTH MEASUREMENT W/O WRAPPING AND EXTERNAL BARS
#----------------------------------------------------------------------
# Remove Bar 0 (LS) and Bar 15 (LN) because of instable measurements
sud = np.delete(sud ,(-1,-2),axis=0)
nord = np.delete(nord,(-1,-2),axis=0)
max_y_LS_noExtBars = np.amax(sud)
min_y_LS_noExtBars = np.amin(sud)
max_y_LN_noExtBars = np.amax(nord)
min_y_LN_noExtBars = np.amin(nord)
# Spread on Y on north/sud side (length)
std_y_LS_noExtBars = (sud.std().round(3))
std_y_LN_noExtBars = (nord.std().round(3))
std_deltay_noExtBars = round(mt.sqrt(std_y_LS_noExtBars**2+std_y_LN_noExtBars**2),3)
# MAX LENGTH W/O EXTERNAL BARS
array_length_max_noExtBars = (max_y_LN_noExtBars - min_y_LS_noExtBars).round(3)
# MEAN LENGTH W/O EXTERNAL BARS
mean_y_LS_noExtBars = (sud.mean().round(3))
mean_y_LN_noExtBars = (nord.mean().round(3))
array_length_mean_noExtBars = (mean_y_LN_noExtBars - mean_y_LS_noExtBars).round(3)
array_length_mean_std_noExtBars = round(mt.sqrt( (std_y_LS_noExtBars/ mt.sqrt(sud.size) )**2
+ (std_y_LN_noExtBars/mt.sqrt(nord.size) )**2 ) , 3)
# LENGTH MAX VARIATION W/O EXTERNAL BARS
delta_y_LS_noExtBars = (max_y_LS_noExtBars - min_y_LS_noExtBars).round(3)
delta_y_LN_noExtBars= (max_y_LN_noExtBars - min_y_LN_noExtBars).round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Create some mock data for all point collected along LS and LN - 39 points on each side -
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sud = df_LS['Y'].to_numpy() #considering wrapping
nord = df_LN['Y'].to_numpy() #considering wrapping
# Removing points used to compute wrapping measurements
df1_LS = df_LS.drop([3,6,9,12,15,18,21,23,27,30,33,36,39])
df1_LN = df_LN.drop([3,6,9,12,15,18,21,23,27,30,33,36,39])
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# New dataset after removing points used to compute wrapping measurements
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sud = np.array(df1_LS['Y'])
nord = np.array(df1_LN['Y'])
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Compute Mean Value for the two points/measurements on each side (LS-LN)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
n=2
sud = np.average(sud.reshape(-1, n), axis=1)
nord = np.average(nord.reshape(-1, n), axis=1)
sud_min = np.amin(sud)
sud_max = np.amax(sud)
nord_min = np.amin(nord)
nord_max= np.amax(nord)
nord = nord[::-1]
sud_misalign = sud - sud.mean()
nord_misalign = nord - nord.mean()
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# PLOT 3 LN-LS_MaxVar_13_measured_points: New plot upon Paolo's request - FOR VENDORS - (mean of 26 points w/o wrapping)
# VERSION WITH FIXED Y AXIS RANGE
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fig, ax1 = plt.subplots()
#++++++++++++++++++++++++
# South Side
#++++++++++++++++++++++++
sud_misalign = np.append([np.nan,np.nan,np.nan], sud_misalign)
sud_x_index = np.arange(0,16,1)
color = 'tab:red'
ax1.plot(sud_x_index, sud_misalign, color=color,linestyle='dashed', marker='o',label='Barcode Side')
#++++++++++++++++++++++++
# North Side
#++++++++++++++++++++++++
nord_x_index = np.arange(0,13,1)
color = 'tab:blue'
ax1.plot(nord_x_index,nord_misalign,color=color,linestyle='dashed', marker='o',label='Opposite to Barcode Side')
# Labels and appearance
ax1.grid()
ax1.set_xlabel('# Bar')
loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax1.xaxis.set_major_locator(loc)
ax1.set_ylabel('Mis-alignment [mm]')
plt.ylim([-0.09,0.09]) # fix y range
ax1.text(0.1,-0.038,'MTD tolerance (0.060 mm)',color='r')
plt.axhline(y = 0.03, color = 'r', linestyle = '--') # MTD acceptance
plt.axhline(y =-0.03, color = 'r', linestyle = '--') # MTD acceptance
# Adding legend
fig.legend(loc='upper left', bbox_to_anchor=(0.03,0.15), bbox_transform=ax1.transAxes)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.suptitle('CMS MTD' + str(args.array) + ' - North-South Side Misalignment', y=1.02,x=0.5)
# Plot show/saving
plt.savefig(run+"_"+date+"_"+str(args.array)+str('_')+tag+'_LN-LS_MaxVar_13_measured_points.png',bbox_inches='tight')
#plt.show() #uncomment this if you want display plots while running code
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# PLOT 4 LN-LS_MaxVar_central: Same as plot 3 but only 10 central bars
# VERSION WITH FIXED Y AXIS RANGE
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fig, ax1 = plt.subplots()
#++++++++++++++++++++++++
# South Side
#++++++++++++++++++++++++
sud_misalign = np.delete(sud_misalign, [0,1,2,13,14,15], None)
sud_x_index = np.arange(3,13,1)
color = 'tab:blue'
ax1.plot(sud_x_index, sud_misalign, color=color,linestyle='dashed', marker='o',label='Barcode Side')
#++++++++++++++++++++++++
# North Side
#++++++++++++++++++++++++
nord_misalign = np.delete(nord_misalign, [10,11,12], None)
nord_x_index = np.arange(3,13,1)
color = 'tab:red'
ax1.plot(nord_x_index,nord_misalign, color=color,linestyle='dashed', marker='o',label='Opposite to Barcode Side')
# Labels and appearance
ax1.grid()
ax1.set_xlabel('# Bar')
loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax1.xaxis.set_major_locator(loc)
ax1.set_ylabel('Mis-alignment [mm]')
plt.ylim([-0.09,0.09]) # fix y range
ax1.text(3.1,-0.038,'MTD tolerance (0.060 mm)',color='r')
plt.axhline(y = 0.03, color = 'r', linestyle = '--') # MTD acceptance
plt.axhline(y =-0.03, color = 'r', linestyle = '--') # MTD acceptance
# Adding legend
fig.legend(loc='upper left', bbox_to_anchor=(0.03,0.15), bbox_transform=ax1.transAxes)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.suptitle('CMS MTD' + str(args.array) + ' - North-South Side Misalignment', y=1.02,x=0.5)
# Plot show/saving
plt.savefig(run+"_"+date+"_"+str(args.array)+str('_')+tag+'_LN-LS_MaxVar_central.png',bbox_inches='tight')
#plt.show() #uncomment this if you want display plots while running code
# print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
# print(' New mean dataset after removing points used to compute wrapping measurements: USE ONLY to match points in Paolo plot ')
# print(' Mean computed over 26 points --> plotted 13 points for each side corresponding to 13 single bars we can measure per array ')
# print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
# p2 = PrettyTable(['Array','Min LS','Max LS', 'MaxVar LS','Min LN','Max LN', 'MaxVar LN','MaxVar mean','Max length'])
# p2.add_row([barcode.split('/')[1],str(sud_min.round(3))+' mm',str(sud_max.round(3))+' mm',str((sud_max-sud_min).round(3))+' mm',str(nord_min.round(3))+' mm',str(nord_max.round(3))+' mm',str((nord_max-nord_min).round(3))+' mm',str((((sud_max-sud_min)+(nord_max-nord_min))/2).round(3))+' mm',str(array_length_max_noExtBars.round(3))+' mm'])
# print(p2)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Simulating Mitutoyo (length) with wrapping
#++++++++++++++++++++++++++++++++++++++++++++++++++
np_mitutoyo_length_LS = max_y_LN - df_LS['Y'].to_numpy()
np_mitutoyo_length_LN = df_LN['Y'].to_numpy() - min_y_LS
np_mitutoyo_length = np_mitutoyo_length_LS
np_mitutoyo_length = np.concatenate([np_mitutoyo_length,np_mitutoyo_length_LN])
mitutoyo_array_length_mean = (np_mitutoyo_length.mean()).round(3)
mitutoyo_array_length_std = (np_mitutoyo_length.std()).round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Simulating Mitutoyo (length) w/o wrapping
#++++++++++++++++++++++++++++++++++++++++++++++++++
np_mitutoyo_length_LS_nowr = max_y_LN - sud
np_mitutoyo_length_LN_nowr = nord - min_y_LS
np_mitutoyo_length_nowr = np_mitutoyo_length_LS_nowr
np_mitutoyo_length_nowr = np.concatenate([np_mitutoyo_length_nowr,np_mitutoyo_length_LN_nowr])
mitutoyo_array_length_nowr_mean = (np_mitutoyo_length_nowr.mean()).round(3)
mitutoyo_array_length_nowr_std = (np_mitutoyo_length_nowr.std()).round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# WIDTH: X -> MaxVar - Mean - Spread - Max array size - average array size
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Max. X variation on 'ovest'/east side (width)
#++++++++++++++++++++++++++++++++++++++++++++++++++
max_x_LO = np.amax(df_LO['X'].to_numpy())
min_x_LO = np.amin(df_LO['X'].to_numpy())
delta_x_LO = (max_x_LO - min_x_LO).round(3)
max_x_LE = np.amax(df_LE['X'].to_numpy())
min_x_LE = np.amin(df_LE['X'].to_numpy())
delta_x_LE = (max_x_LE - min_x_LE).round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Mean on X on 'ovest'/east side (width)
#++++++++++++++++++++++++++++++++++++++++++++++++++
mean_x_LO = (df_LO['X'].to_numpy()).mean().round(3)
mean_x_LE = (df_LE['X'].to_numpy()).mean().round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Std. dev. on X on 'ovest'/east side (width)
#++++++++++++++++++++++++++++++++++++++++++++++++++
std_x_LO = (df_LO['X'].to_numpy()).std().round(3)
std_x_LE = (df_LE['X'].to_numpy()).std().round(3)
std_deltax = round(mt.sqrt(std_x_LO**2+std_x_LE**2),3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Max. array size along X (width)
#++++++++++++++++++++++++++++++++++++++++++++++++++
array_width_max = (max_x_LE - min_x_LO).round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Average array size along X (width)
#++++++++++++++++++++++++++++++++++++++++++++++++++
array_width_mean = (mean_x_LE - mean_x_LO).round(3)
array_width_mean_std = round(mt.sqrt( (std_x_LO/mt.sqrt((df_LO['X'].to_numpy()).size))**2
+ (std_x_LE/mt.sqrt((df_LE['X'].to_numpy()).size))**2 ) , 3)
#++++++++++++++++++++++++++++++++++
# WIDTH: Mitutoyo simulation
#++++++++++++++++++++++++++++++++++
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Simulating Mitutoyo (width)
#++++++++++++++++++++++++++++++++++++++++++++++++++
np_mitutoyo_width_LO = max_x_LE - df_LO['X'].to_numpy()
np_mitutoyo_width_LE = df_LE['X'].to_numpy() - min_x_LO
np_mitutoyo_width = np_mitutoyo_width_LO
np_mitutoyo_width = np.concatenate([np_mitutoyo_width,np_mitutoyo_width_LE])
mitutoyo_array_width_mean = (np_mitutoyo_width.mean()).round(3)
mitutoyo_array_width_std = (np_mitutoyo_width.std()).round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# THICKNESS: Z -> MaxVar - Mean - Spread - Max array size - average array size
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# =================
# === Thickness ===
# =================
np_thickness = df_FS['Z'].to_numpy()
np_thickness = np_thickness.round(3)
###################################
### TEMPORARY FIX FOR FIRST PRE-PROD BATCH (subtract 75um from label thickness)
print ("before fix: ", np_thickness.mean().round(3),np_thickness.std().round(3))
#np_thickness = np_thickness - 0.075
print ("after fix: ", np_thickness.mean().round(3),np_thickness.std().round(3))
###################################
thickness_mean = np_thickness.mean().round(3)
thickness_std = np_thickness.std().round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Max. Z variation on front side (thickness)
#++++++++++++++++++++++++++++++++++++++++++++++++++
max_z_FS = np.amax(np_thickness)
min_z_FS = np.amin(np_thickness)
delta_z_FS = (max_z_FS - min_z_FS).round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Mean on Z on front side (thickness)
#++++++++++++++++++++++++++++++++++++++++++++++++++
mean_z_FS = (np_thickness).mean().round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Std. dev. on Z on front side (thickness)
#++++++++++++++++++++++++++++++++++++++++++++++++++
std_z_FS = (np_thickness).std().round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Max. array size along Z (thickness)
#++++++++++++++++++++++++++++++++++++++++++++++++++
array_thickness_max = (max_z_FS - 0).round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Average array size along Z (thickness)
#++++++++++++++++++++++++++++++++++++++++++++++++++
array_thickness_mean = (mean_z_FS - 0).round(3)
array_thickness_mean_std = round( std_z_FS / mt.sqrt( (np_thickness).size ), 3 )
#+++++++++++++++++++++++++++++++++++++++++++
# ******** GEOMETRY DEFINITION ********
#+++++++++++++++++++++++++++++++++++++++++++
geo=['geo1','geo2','geo3']
if (array_thickness_mean > 3.95):
geo=geo[0]
elif (array_thickness_mean < 2.9):
geo=geo[2]
else:
geo=geo[1]
print('')
print('++++++++++++++++++++++++++')
print('Array Geometry :',str(geo) )
print('++++++++++++++++++++++++++')
print('')
#++++++++++++++++++++++++++++++++++++++++++++++++++
# THICKNESS: Mitutoyo simulation
#++++++++++++++++++++++++++++++++++++++++++++++++++
#++++++++++++++++++++++++++++++++++++++++++++++++++
# Simulating Mitutoyo (thickness)
#++++++++++++++++++++++++++++++++++++++++++++++++++
np_mitutoyo_thickness = np_thickness
mitutoyo_array_thickness_mean = (np_mitutoyo_thickness.mean()).round(3)
mitutoyo_array_thickness_std = (np_mitutoyo_thickness.std()).round(3)
#++++++++++++++++++++++++++++++++++++++++++++++++++
# === WRITING OUTPUT IN CSV FILE: Output .csv & .json files
#++++++++++++++++++++++++++++++++++++++++++++++++++
json_array = [{
'runName': run.split('/')[-1]+'_ARRAY'+barcode+str('_')+tag,
'id': barcode,
#'producer': str(prod),
#'geometry': str(geo),
'time': date,
'L_bar_mu': length_mean,
'L_bar_std': length_std,
'L_maxVar_LS': delta_y_LS_noExtBars,
'L_maxVar_LN': delta_y_LN_noExtBars,
'L_std_LS': std_y_LS_noExtBars,
'L_std_LN': std_y_LN_noExtBars,
'L_std_tot': std_deltay_noExtBars,
'L_max': array_length_max_noExtBars,
'L_mean': array_length_mean_noExtBars,
'L_mean_std': array_length_mean_std_nowr,
'L_maxVar_LS_allBars': delta_y_LS_nowr,
'L_maxVar_LN_allBars': delta_y_LN_nowr,
'L_std_LS_allBars': std_y_LS_nowr,
'L_std_LN_allBars': std_y_LN_nowr,
'L_std_tot_allBars': std_deltay_nowr,
'L_max_allBars': array_length_max_nowr,
'L_mean_allBars': array_length_mean_nowr,
'L_mean_std_allBars': array_length_mean_std_nowr,
'L_mean_mitu': '',
'L_std_mitu': '',
'W_maxVar_LO':delta_x_LO,
'W_maxVar_LE':delta_x_LE,
'W_std_LO': std_x_LO,
'W_std_LE': std_x_LE,
'W_std_tot': std_deltax,
'W_max': array_width_max,
'W_mean': array_width_mean,
'W_mean_std': array_width_mean_std,