-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunctions.py
1525 lines (1323 loc) · 68.8 KB
/
functions.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 numpy as np
from numpy import inf
import itertools
import keras
import matplotlib
import matplotlib.pyplot as plt
from sklearn import preprocessing, svm
from sklearn.metrics import roc_auc_score, roc_curve, auc, confusion_matrix
from sklearn.utils.extmath import stable_cumsum
from sklearn.utils import check_consistent_length, assert_all_finite, column_or_1d, check_array
import scipy.optimize as opt
from scipy.optimize import fsolve
from sklearn.utils.multiclass import type_of_target
from sklearn.model_selection import train_test_split
from IPython.display import FileLink, FileLinks
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, BatchNormalization
from keras.utils import to_categorical, plot_model
from keras.callbacks import History, ModelCheckpoint, ReduceLROnPlateau
from keras.optimizers import Adam
from copy import deepcopy
from ROOT import TCanvas, TFile, TH1F, TH2F, gROOT, kRed, kBlue, kGreen, kMagenta, kCyan, kOrange, gStyle
from ROOT import gErrorIgnoreLevel
from ROOT import kInfo, kWarning, kError
from constants import *
import math
import pickle
import sys
import os
def dict_to_str(parameters):
layers_str = [str(parameters['layers'][i]) for i in range(len(parameters['layers']))]
tag = 'layers_'
for i in range(len(layers_str)):
tag = tag + layers_str[i]
if i < len(layers_str)-1: tag = tag + '_'
tag = tag + '__batchsize_' + str(parameters['batchsize'])
tag = tag + '__classes_' + str(len(parameters['classes'])) + '_'
for i in range(len(parameters['classes'])):
for j in range(len(parameters['classes'][i])):
tag = tag + parameters['classes'][i][j]
if j < len(parameters['classes'][i]) - 1:
tag = tag + '+'
if i < len(parameters['classes']) - 1:
tag = tag + '_'
tag = tag + '__regmethod_' + parameters['regmethod']
tag = tag + '__regrate_' + '{num:06d}'.format(num=int(parameters['regrate']*100000.))
tag = tag + '__batchnorm_' + str(parameters['batchnorm'])
tag = tag + '__epochs_' + str(parameters['epochs'])
tag = tag + '__learningrate_' + '{num:06d}'.format(num=int(parameters['learningrate']*100000.))
tag = tag + '__runonfraction_' + '{num:03d}'.format(num=int(parameters['runonfraction']*100.))
tag = tag + '__eqweight_' + str(parameters['eqweight'])
if len(tag.split('__')) != len(parameters): raise ValueError('in dict_to_str: Number of parameters given in the dictionary does no longer match the prescription how to build the tag out of it.')
return tag
def get_classes_tag(parameters):
tag = 'classes_' + str(len(parameters['classes'])) + '_'
for i in range(len(parameters['classes'])):
for j in range(len(parameters['classes'][i])):
tag = tag + parameters['classes'][i][j]
if j < len(parameters['classes'][i]) - 1:
tag = tag + '+'
if i < len(parameters['classes']) - 1:
tag = tag + '_'
return tag
def get_classtitles(parameters):
classes = parameters['classes']
classtitles = {}
for key in classes.keys():
list = classes[key]
title = ''
for i in range(len(list)):
title = title + list[i]
if i < len(list)-1:
title = title + '+'
classtitles[key] = title
return classtitles
def get_fraction(parameters):
runonfraction = parameters['runonfraction']
string = str('{num:03d}'.format(num=int(parameters['runonfraction']*100.)))
return string
def load_data(parameters, inputfolder, filepostfix):
print 'Loading data...'
tag = dict_to_str(parameters)
classtag = get_classes_tag(parameters)
fraction = get_fraction(parameters)
input_train = np.load(inputfolder+'/input_'+fraction+'_train'+filepostfix+'.npy')
input_test = np.load(inputfolder+'/input_'+fraction+'_test'+filepostfix+'.npy')
input_val = np.load(inputfolder+'/input_'+fraction+'_val'+filepostfix+'.npy')
labels_train = np.load(inputfolder+'/labels_'+fraction+'_train'+filepostfix+'.npy')
labels_test = np.load(inputfolder+'/labels_'+fraction+'_test'+filepostfix+'.npy')
labels_val = np.load(inputfolder+'/labels_'+fraction+'_val'+filepostfix+'.npy')
sample_weights_train = np.load(inputfolder+'/sample_weights_'+fraction+'_train'+filepostfix+'.npy')
eventweights_train = np.load(inputfolder+'/eventweights_'+fraction+'_train'+filepostfix+'.npy')
sample_weights_test = np.load(inputfolder+'/sample_weights_'+fraction+'_test'+filepostfix+'.npy')
eventweights_test = np.load(inputfolder+'/eventweights_'+fraction+'_test'+filepostfix+'.npy')
sample_weights_val = np.load(inputfolder+'/sample_weights_'+fraction+'_val'+filepostfix+'.npy')
eventweights_val = np.load(inputfolder+'/eventweights_'+fraction+'_val'+filepostfix+'.npy')
signal_identifiers = ['RSGluon_All', 'RSGluon_M1000', 'RSGluon_M2000', 'RSGluon_M3000', 'RSGluon_M4000', 'RSGluon_M5000', 'RSGluon_M6000']
signals = {}
signal_eventweights = {}
signal_normweights = {}
for i in range(len(signal_identifiers)):
signals[i] = np.load(inputfolder+'/' + signal_identifiers[i] + filepostfix+'.npy')
signal_eventweights[i] = np.load(inputfolder+'/' + signal_identifiers[i] + '_eventweight'+filepostfix+'.npy')
sum_signal_eventweights = signal_eventweights[i].sum()
signal_normweights[i] = np.array([1./sum_signal_eventweights for j in range(signal_eventweights[i].shape[0])])
return input_train, input_test, input_val, labels_train, labels_test, labels_val, sample_weights_train, sample_weights_test, sample_weights_val, eventweights_train, eventweights_test, eventweights_val, signals, signal_eventweights, signal_normweights
def load_predictions(outputfolder, filepostfix):
print 'Loading predictions...'
signal_identifiers = ['RSGluon_All', 'RSGluon_M1000', 'RSGluon_M2000', 'RSGluon_M3000', 'RSGluon_M4000', 'RSGluon_M5000', 'RSGluon_M6000']
# Load model prediction
pred_signals = {}
pred_train = np.load(outputfolder+'/prediction_train'+filepostfix+'.npy')
pred_val = np.load(outputfolder+'/prediction_val'+filepostfix+'.npy')
pred_test = np.load(outputfolder+'/prediction_test'+filepostfix+'.npy')
for i in range(len(signal_identifiers)):
pred_signals[i] = np.load(outputfolder+'/prediction_'+signal_identifiers[i]+''+filepostfix+'.npy')
return pred_train, pred_test, pred_val, pred_signals
def binary_clf_curve(y_true, y_score, pos_label=None, sample_weight=None):
# Check to make sure y_true is valid
y_type = type_of_target(y_true)
if not (y_type == "binary" or
(y_type == "multiclass" and pos_label is not None)):
raise ValueError("{0} format is not supported".format(y_type))
check_consistent_length(y_true, y_score, sample_weight)
y_true = column_or_1d(y_true)
y_score = column_or_1d(y_score)
assert_all_finite(y_true)
assert_all_finite(y_score)
if sample_weight is not None:
sample_weight = column_or_1d(sample_weight)
# ensure binary classification if pos_label is not specified
classes = np.unique(y_true)
if (pos_label is None and
not (np.array_equal(classes, [0, 1]) or
np.array_equal(classes, [-1, 1]) or
np.array_equal(classes, [0]) or
np.array_equal(classes, [-1]) or
np.array_equal(classes, [1]))):
raise ValueError("Data is not binary and pos_label is not specified")
elif pos_label is None:
pos_label = 1.
# make y_true a boolean vector
y_true = (y_true == pos_label)
# sort scores and corresponding truth values
desc_score_indices = np.argsort(y_score, kind="mergesort")[::-1]
y_score = y_score[desc_score_indices]
y_true = y_true[desc_score_indices]
if sample_weight is not None:
weight = sample_weight[desc_score_indices]
else:
weight = 1.
# y_score typically has many tied values. Here we extract
# the indices associated with the distinct values. We also
# concatenate a value for the end of the curve.
distinct_value_indices = np.where(np.diff(y_score))[0]
threshold_idxs = np.r_[distinct_value_indices, y_true.size - 1]
# accumulate the true positives with decreasing threshold
tps = stable_cumsum(y_true * weight)[threshold_idxs]
if sample_weight is not None:
# express fps as a cumsum to ensure fps is increasing even in
# the presence of floating point errors
fps = stable_cumsum((1 - y_true) * weight)[threshold_idxs]
else:
fps = 1 + threshold_idxs - tps
return fps, tps, y_score[threshold_idxs]
def roc_curve_own(y_true, y_score, pos_label=None, sample_weight=None, drop_intermediate=True):
# Copied from https://github.com/scikit-learn/scikit-learn/blob/7389dba/sklearn/metrics/ranking.py#L535
# Extended by purity-part
fps, tps, thresholds = binary_clf_curve(y_true, y_score, pos_label=pos_label, sample_weight=sample_weight)
if drop_intermediate and len(fps) > 2:
optimal_idxs = np.where(np.r_[True, np.logical_or(np.diff(fps, 2), np.diff(tps, 2)), True])[0]
fps = fps[optimal_idxs]
tps = tps[optimal_idxs]
thresholds = thresholds[optimal_idxs]
if tps.size == 0 or fps[0] != 0 or tps[0] != 0:
tps = np.r_[0, tps]
fps = np.r_[0, fps]
thresholds = np.r_[thresholds[0] + 1, thresholds]
if fps[-1] <= 0:
warnings.warn("No negative samples in y_true, "
"false positive value should be meaningless",
UndefinedMetricWarning)
fpr = np.repeat(np.nan, fps.shape)
else:
fpr = fps / fps[-1]
if tps[-1] <= 0:
warnings.warn("No positive samples in y_true, "
"true positive value should be meaningless",
UndefinedMetricWarning)
tpr = np.repeat(np.nan, tps.shape)
else:
tpr = tps / tps[-1]
# purity!
prt = tps/(tps+fps)
return fpr, tpr, thresholds, prt
def get_fpr_tpr_thr_auc(parameters, pred_val, labels_val, weights_val):
eqweight = parameters['eqweight']
FalsePositiveRates = {}
TruePositiveRates = {}
Thresholds = {}
SignalPuritys = {}
aucs = {}
for i in range(labels_val.shape[1]):
FalsePositiveRates[i], TruePositiveRates[i], Thresholds[i], SignalPuritys[i] = roc_curve_own(labels_val[:,i], pred_val[:,i], sample_weight=weights_val)
aucs[i] = np.trapz(TruePositiveRates[i], FalsePositiveRates[i])
return (FalsePositiveRates, TruePositiveRates, Thresholds, aucs, SignalPuritys)
def get_cut_efficiencies(parameters, predictions, thresholds, weights):
effs_list = []
indices = []
length = thresholds.shape[0]
stepsize = length/10000
for i in range(thresholds.shape[0]):
if i%(stepsize)==0:
effs_list.append(weights[predictions > thresholds[i]].sum())
indices.append(i)
sumweights = weights.sum()
effs = np.array(effs_list)
effs /= sumweights
return np.array(effs), indices
def get_data_dictionaries(parameters, eventweights_train, sample_weights_train, pred_train, labels_train, eventweights_val, sample_weights_val, pred_val, labels_val, eventweights_test, sample_weights_test, pred_test, labels_test):
classes = parameters['classes']
eqweight = parameters['eqweight']
pred_trains = {}
pred_vals = {}
pred_tests = {}
weights_trains = {}
weights_vals = {}
weights_tests = {}
normweights_trains = {}
normweights_vals = {}
normweights_tests = {}
lumiweights_trains = {}
lumiweights_vals = {}
lumiweights_tests = {}
for cl in classes.keys():
pred_trains_thistrueclass = {}
pred_vals_thistrueclass = {}
pred_tests_thistrueclass = {}
weights_trains_thistrueclass = {}
weights_vals_thistrueclass = {}
weights_tests_thistrueclass = {}
normweights_trains_thistrueclass = {}
normweights_vals_thistrueclass = {}
normweights_tests_thistrueclass = {}
lumiweights_trains_thistrueclass = {}
lumiweights_vals_thistrueclass = {}
lumiweights_tests_thistrueclass = {}
for node in classes.keys():
if not eqweight:
weights_trains_thistrueclass[node] = eventweights_train[labels_train[:,cl] == 1]
weights_vals_thistrueclass[node] = eventweights_val[labels_val[:,cl] == 1]
weights_tests_thistrueclass[node] = eventweights_test[labels_test[:,cl] == 1]
else:
weights_trains_thistrueclass[node] = sample_weights_train[labels_train[:,cl] == 1]
weights_vals_thistrueclass[node] = sample_weights_val[labels_val[:,cl] == 1]
weights_tests_thistrueclass[node] = sample_weights_test[labels_test[:,cl] == 1]
pred_trains_thistrueclass[node] = pred_train[:,node][labels_train[:,cl] == 1]
pred_vals_thistrueclass[node] = pred_val[:,node][labels_val[:,cl] == 1]
pred_tests_thistrueclass[node] = pred_test[:,node][labels_test[:,cl] == 1]
lumiweights_trains_thistrueclass[node] = eventweights_train[labels_train[:,cl] == 1]
lumiweights_vals_thistrueclass[node] = eventweights_val[labels_val[:,cl] == 1]
lumiweights_tests_thistrueclass[node] = eventweights_test[labels_test[:,cl] == 1]
sum_train = weights_trains_thistrueclass[node].sum()
sum_val = weights_vals_thistrueclass[node].sum()
sum_test = weights_tests_thistrueclass[node].sum()
normweights_trains_thistrueclass[node] = np.array([1./sum_train for j in range(weights_trains_thistrueclass[node].shape[0])])
normweights_vals_thistrueclass[node] = np.array([1./sum_val for j in range(weights_vals_thistrueclass[node].shape[0])])
normweights_tests_thistrueclass[node] = np.array([1./sum_test for j in range(weights_tests_thistrueclass[node].shape[0])])
pred_trains[cl] = pred_trains_thistrueclass
pred_vals[cl] = pred_vals_thistrueclass
pred_tests[cl] = pred_tests_thistrueclass
weights_trains[cl] = weights_trains_thistrueclass
weights_vals[cl] = weights_vals_thistrueclass
weights_tests[cl] = weights_tests_thistrueclass
normweights_trains[cl] = normweights_trains_thistrueclass
normweights_vals[cl] = normweights_vals_thistrueclass
normweights_tests[cl] = normweights_tests_thistrueclass
lumiweights_trains[cl] = lumiweights_trains_thistrueclass
lumiweights_vals[cl] = lumiweights_vals_thistrueclass
lumiweights_tests[cl] = lumiweights_tests_thistrueclass
return pred_trains, weights_trains, normweights_trains, lumiweights_trains, pred_vals, weights_vals, normweights_vals, lumiweights_vals, pred_tests, weights_tests, normweights_tests, lumiweights_tests
def get_indices_wrong_predictions(labels, preds):
mask = []
for i in range(labels.shape[0]):
label = -1
predclass = -1
maxpred = -1
for j in range(labels.shape[1]):
if labels[i,j] == 1: label = j
if maxpred < preds[i,j]:
maxpred = preds[i,j]
predclass = j
if label != predclass:
mask.append(i)
return mask
def hinton(matrix, max_weight=None, ax=None):
"""Draw Hinton diagram for visualizing a weight matrix."""
ax = ax if ax is not None else plt.gca()
if not max_weight:
max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))
ax.patch.set_facecolor('gray')
ax.set_aspect('equal', 'box')
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())
for (x, y), w in np.ndenumerate(matrix):
color = 'white' if w > 0 else 'black'
size = np.sqrt(np.abs(w) / max_weight)
rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
facecolor=color, edgecolor=color)
ax.add_patch(rect)
ax.autoscale_view()
ax.invert_yaxis()
def conf_matrix(labels, predictions, weights):
# will return a list of lists, shape=NxN
# each list is for one true class
if labels.shape != predictions.shape:
raise ValueError('Labels and predictions do not have the same shape.')
if labels.shape[0] != weights.shape[0]:
raise ValueError('Labels and weights do not have the same length (.shape[0]).')
# initialize confusion matrix
matrix = np.zeros((labels.shape[1], labels.shape[1]))
# format inputs
for i in range(labels.shape[0]):
label = -1
predclass = -1
maxpred = -1
for j in range(labels.shape[1]):
if labels[i,j] == 1: label = j
if maxpred < predictions[i,j]:
maxpred = predictions[i,j]
predclass = j
if label == -1: raise ValueError('For this event, the labels of all classes are 0, so the event doesn\'t have a class?')
matrix[label,predclass] = matrix[label,predclass] + weights[i]
return matrix
def plot_confusion_matrix(cm, classes,
normalize=False, axis='',
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize and axis == 'x':
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix along x")
elif normalize and axis == 'y':
cm = cm.astype('float') / cm.sum(axis=0)[np.newaxis, :]
print("Normalized confusion matrix along y")
else:
print('Confusion matrix, without normalization')
if not axis == '' and not normalize: raise ValueError('a normalization axis was given but normalization was switched off!')
if axis == '' and normalize: raise ValueError('no normalization axis was given but normalization was switched on!')
plt.imshow(cm, interpolation='none', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
listofclasses = []
for key in classes.keys():
listofclasses.append(classes[key])
plt.xticks(tick_marks, listofclasses, rotation=45)
plt.yticks(tick_marks, listofclasses)
# fmt = '.2f' if normalize else 'd'
fmt = '.2f'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
def log_model_performance(parameters, model_history, outputfolder):
loss_train = model_history['loss']
loss_val = model_history['val_loss']
#Fit Losses
x, fitx, fitfunc, pars_train = fit_loss(model_history['loss'])
x, fitx, fitfunc, pars_val = fit_loss(model_history['val_loss'])
#build difference
pars = [pars_train, pars_val]
# def func_diff(fitx, mypars):
# return fitfunc(fitx, *mypars[0]) - fitfunc(fitx, *mypars[1])
def func_diff(x, fitfunc, pars):
return fitfunc(x, *(pars[0])) - fitfunc(x, *(pars[1]))
#Find intersection between losses - aka roots of (f_train - f_val)
# sol = opt.root_scalar(func_diff, (pars))
roots = fsolve(func_diff, [100.], (fitfunc, pars))
print roots
# Get closest integer numbers
for root in roots:
x_low = int(math.floor(root))
x_high = int(math.ceil(root))
# compare losses at these values, pick the one where difference of losses is smallest
diff_xlow = math.fabs(fitfunc(x_low, *pars_train) - fitfunc(x_low, *pars_val))
diff_xhigh = math.fabs(fitfunc(x_high, *pars_train) - fitfunc(x_high, *pars_val))
bestloss_val = fitfunc(x_low, *pars_val)
bestx = x_low
if diff_xhigh < diff_xlow:
bestloss_val = fitfunc(x_high, *pars_val)
bestx = x_high
if root < 10:
bestloss_val = 999999
bestx = 999999
print "Validation loss in point of closest approach: %f, reached after %i epochs" % (bestloss_val, bestx)
acc_train = model_history['categorical_accuracy']
acc_val = model_history['val_categorical_accuracy']
tag = dict_to_str(parameters)
with open(outputfolder+'/ModelPerformance.txt', 'w') as f:
f.write('\n\n====================\n')
f.write('Tag: %s\n\n' % (tag))
f.write('Minimum validation loss reached after %i epochs\n' % (loss_val.index(min(loss_val))))
f.write('Validation loss in point of closest approach: %2.3f, reached after %i epochs\n' % (bestloss_val, bestx))
f.write('Performance: training loss (min, final) -- validation loss (min, final) -- training acc (min, final) -- validation acc (min, final)\n')
f.write(' ({0:2.3f}, {1:2.3f}) -- ({2:2.3f}, {3:2.3f}) -- ({4:1.3f}, {5:1.3f}) -- ({6:1.3f}, {7:1.3f})\n'.format(min(loss_train), loss_train[len(loss_train)-1], min(loss_val), loss_val[len(loss_val)-1], min(acc_train), acc_train[len(acc_train)-1], min(acc_val), acc_val[len(acc_val)-1]))
def plot_rocs(parameters, plotfolder, pred_val, labels_val, sample_weights_val, eventweights_val, pred_signals=None, eventweight_signals=None, usesignals=[0], use_best_model=False):
tag = dict_to_str(parameters)
classtitles = get_classtitles(parameters)
classes = parameters['classes']
do_sig = (pred_signals is not None) and (eventweight_signals is not None)
print 'plotting ROC curves'
# Inclusive backgrounds
# equallyweighted
FalsePositiveRates, TruePositiveRates, Thresholds, aucs, SignalPuritys = get_fpr_tpr_thr_auc(parameters=parameters, pred_val=pred_val, labels_val=labels_val, weights_val=sample_weights_val)
# print 'eqweight: ', sample_weights_val[10:15]
# print 'fpr: ', FalsePositiveRates[1][10:15]
plt.clf()
fig = plt.figure()
plt.xticks(np.arange(0.1,1.1,0.1))
plt.grid(True, which='both')
for i in range(len(FalsePositiveRates)):
plt.semilogy(TruePositiveRates[i], FalsePositiveRates[i], label=classtitles[i] + ', AUC: '+str(round(aucs[i],3)), color=colorstr[i])
plt.legend(loc='upper left')
plt.ylim([0.0001, 1.05])
plt.xlabel('Selection efficiency')
plt.ylabel('Background efficiency')
title = 'ROC_val_eqweight'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
plt.clf()
fig = plt.figure()
for i in range(len(TruePositiveRates)):
plt.plot(TruePositiveRates[i], SignalPuritys[i], label=classtitles[i], color=colorstr[i])
plt.legend(loc='best')
plt.ylim([0., 1.05])
plt.xlim([0., 1.])
plt.xticks(np.arange(0.,1.1,0.1))
plt.yticks(np.arange(0.,1.1,0.1))
plt.grid(True, which='both')
plt.xlabel('Selection efficiency')
plt.ylabel('Purity')
title = 'EffVsPur_val_eqweight'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
# lumiweighted
FalsePositiveRates, TruePositiveRates, Thresholds, aucs, SignalPuritys = get_fpr_tpr_thr_auc(parameters=parameters, pred_val=pred_val, labels_val=labels_val, weights_val=eventweights_val)
# print 'lumiweight: ', eventweights_val[10:15]
# print 'fpr: ', FalsePositiveRates[1][10:15]
plt.clf()
fig = plt.figure()
plt.xticks(np.arange(0.1,1.1,0.1))
plt.grid(True, which='both')
for i in range(len(FalsePositiveRates)):
plt.semilogy(TruePositiveRates[i], FalsePositiveRates[i], label=classtitles[i] + ', AUC: '+str(round(aucs[i],3)), color=colorstr[i])
plt.legend(loc='upper left')
plt.ylim([0.0001, 1.05])
plt.xlabel('Class selection efficiency')
plt.ylabel('Class background efficiency')
title = 'ROC_val_lumiweighted'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
plt.clf()
fig = plt.figure()
for i in range(len(TruePositiveRates)):
plt.plot(TruePositiveRates[i], SignalPuritys[i], label=classtitles[i], color=colorstr[i])
plt.legend(loc='best')
plt.ylim([0., 1.05])
plt.xlim([0., 1.])
plt.xticks(np.arange(0.,1.1,0.1))
plt.yticks(np.arange(0.,1.1,0.1))
plt.grid(True, which='both')
plt.xlabel('Selection efficiency')
plt.ylabel('Purity')
title = 'EffVsPur_val_lumiweighted'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
# now plot signal efficiency versus signal purity --> calculate things ourselves
# Individual backgrounds: 1 plot per class, the other classes are individual curves. Therefore we only need to look at outputnode no. 'cl'
for cl in classes.keys():
#cl is the true class
# Dictionaries to store the rocs against each individual background
fprs_eq = {}
tprs_eq = {}
thrs_eq = {}
aucss_eq = {}
prts_eq = {}
fprs_lum = {}
tprs_lum = {}
thrs_lum = {}
aucss_lum = {}
prts_lum = {}
eff_signals_lum = {}
auc_signals_lum = {}
# Loop over all remaining classes, always keep predictions, labels, and weights for class 'cl' and this one background
for i in classes.keys():
# i is the index of the one background class
if i == cl: continue
mask = np.logical_or(labels_val[:,cl] == 1, labels_val[:,i] == 1)
pred_this = pred_val[mask]
labels_this = labels_val[mask]
weights_sample = sample_weights_val[mask]
weights_lum = eventweights_val[mask]
pred_this = pred_this[:,[cl,i]]
labels_this = labels_this[:,[cl,i]]
fprs_eq[i], tprs_eq[i], thrs_eq[i], aucss_eq[i], prts_eq[i] = get_fpr_tpr_thr_auc(parameters=parameters, pred_val=pred_this, labels_val=labels_this, weights_val=weights_sample)
fprs_lum[i], tprs_lum[i], thrs_lum[i], aucss_lum[i], prts_lum[i] = get_fpr_tpr_thr_auc(parameters=parameters, pred_val=pred_this, labels_val=labels_this, weights_val=weights_lum)
# don't care, which tpr and thr we choose for class 'cl', we are calculating the singal efficiency for those values anyway ;)
if do_sig:
for key in pred_signals.keys():
eff_signals_lum[key], indices = get_cut_efficiencies(parameters=parameters, predictions=pred_signals[key][:,cl], thresholds=thrs_lum[0 if cl > 0 else 1][0], weights=eventweight_signals[key])
# print thrs_lum[0 if cl > 0 else 1][0]
# print eff_signals_lum
auc_signals_lum[key] = np.trapz(tprs_lum[0 if cl > 0 else 1][0][indices], eff_signals_lum[key])
# Now just plot all 4 curves (equallyweighted)
plt.clf()
fig = plt.figure()
plt.xticks(np.arange(0.1,1.1,0.1))
plt.grid(True, which='both')
for i in fprs_eq.keys():
plt.semilogy(tprs_eq[i][0], fprs_eq[i][0], label='Bkg: ' + classtitles[i] + ', AUC: '+str(round(aucss_eq[i][0],3)), color=colorstr[i])
plt.legend(loc='upper left')
plt.ylim([0.0001, 1.05])
plt.xlabel(classtitles[cl]+' selection efficiency')
plt.ylabel('Class background efficiency')
title = 'ROC_val_class'+str(cl)+'_eqweight'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
plt.clf()
fig = plt.figure()
for i in fprs_eq.keys():
plt.plot(tprs_eq[i][0], prts_eq[i][0], label='Bkg: ' + classtitles[i], color=colorstr[i])
plt.legend(loc='best')
plt.ylim([0., 1.05])
plt.xlim([0., 1.])
plt.xticks(np.arange(0.,1.1,0.1))
plt.yticks(np.arange(0.,1.1,0.1))
plt.grid(True, which='both')
plt.xlabel(classtitles[cl]+' selection efficiency')
plt.ylabel('Purity wrt. given background')
title = 'EffVsPur_val_class'+str(cl)+'_eqweight'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
# Now just plot all 4 curves (lumiweighted)
plt.clf()
fig = plt.figure()
plt.xticks(np.arange(0.1,1.1,0.1))
plt.grid(True, which='both')
for i in fprs_lum.keys():
plt.semilogy(tprs_lum[i][0], fprs_lum[i][0], label='Bkg: '+classtitles[i] + ', AUC: '+str(round(aucss_lum[i][0],3)), color=colorstr[i])
if do_sig:
for sigidx in range(len(usesignals)):
plt.semilogy(tprs_lum[0 if cl > 0 else 1][0][indices], eff_signals_lum[usesignals[sigidx]], label='Signal (%s), AUC: %s' % (signalmasses[usesignals[sigidx]], str(round(auc_signals_lum[usesignals[sigidx]],3))), color='k', linestyle=signal_linestyles[sigidx])
plt.legend(loc='upper left')
plt.ylim([0.0001, 1.05])
plt.xlabel(classtitles[cl]+' selection efficiency')
plt.ylabel('Class background efficiency')
title = 'ROC_val_class'+str(cl)+'_lumiweighted'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
plt.clf()
fig = plt.figure()
for i in fprs_eq.keys():
plt.plot(tprs_lum[i][0], prts_lum[i][0], label='Bkg: ' + classtitles[i], color=colorstr[i])
plt.legend(loc='best')
plt.ylim([0., 1.05])
plt.xlim([0., 1.])
plt.xticks(np.arange(0.,1.1,0.1))
plt.yticks(np.arange(0.,1.1,0.1))
plt.grid(True, which='both')
plt.xlabel(classtitles[cl]+' selection efficiency')
plt.ylabel('Purity wrt. given background')
title = 'EffVsPur_val_class'+str(cl)+'_lumiweighted'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
def plot_loss(parameters, plotfolder, model_history):
print 'Starting to plot Loss'
eqweight = parameters['eqweight']
# def fitfunc(x, a, b, c, d, e):
# return a + b/x + c*x + d*x*x + e/x/x
tag = dict_to_str(parameters)
plt.clf()
fig = plt.figure()
plt.grid()
x, fitx, fitfunc, postfitpars_train = fit_loss(model_history['loss'])
x, fitx, fitfunc, postfitpars_val = fit_loss(model_history['val_loss'])
plt.plot(x, model_history['loss'], label = 'Training set')
plt.plot(x, model_history['val_loss'], label = 'Validation set')
plt.plot(fitx, fitfunc(fitx, *postfitpars_train), label="Fit (training set)")
plt.plot(fitx, fitfunc(fitx, *postfitpars_val), label="Fit (validation set)")
plt.legend(loc='upper right')
plt.ylim([0.1, 0.25])
if eqweight:
plt.ylim([0.01, 0.06])
plt.ylabel('Loss')
plt.xlabel('Number of training epochs')
fig.savefig(plotfolder+'/Loss.pdf')
plt.close()
def fit_loss(losslist, maxfev=50000):
def fitfunc(x, a, b, c, d, e):
return a + b/x + c*x + d*x*x + e/x/x
x = range(len(losslist)+1)
x = x[1:]
x = np.array(x)
fitx = x[9:]
fity = losslist[9:]
postfitpars, cov = opt.curve_fit(fitfunc, fitx, fity, maxfev=maxfev)
return x, fitx, fitfunc, postfitpars
def plot_accuracy(parameters, plotfolder, model_history):
print 'Starting to plot accuracy'
tag = dict_to_str(parameters)
plt.clf()
fig = plt.figure()
plt.grid()
x = range(len(model_history['categorical_accuracy'])+1)
x = x[1:]
plt.plot(x, model_history['categorical_accuracy'], label = 'Training set')
plt.plot(x, model_history['val_categorical_accuracy'], label = 'Validation set')
plt.legend(loc='lower right')
plt.ylim([0., 1.05])
plt.ylabel('Prediction accuracy')
plt.xlabel('Number of training epochs')
fig.savefig(plotfolder+'/Accuracy.pdf')
def plot_weight_updates(parameters, model, input_val):
print 'Starting to plot weights'
tag = dict_to_str(parameters)
epochs = parameters['epochs']
tmp = os.listdir('output/'+tag)
weightfiles = []
weights = []
rel_updates = []
updates = []
activations = []
for t in tmp:
if 'model_epoch' in t and '.h5' in t:
weightfiles.append(t)
idx=0
for weightfile in weightfiles:
model = keras.models.load_model('output/'+tag+'/'+weightfile)
weights_thismodel = []
updates_thismodel = []
activations_thismodel = []
for i in range(len(model.layers)):
try:
W = model.layers[i].kernel.get_value(borrow=True)
except AttributeError:
continue
W = model.layers[i].kernel.get_value(borrow=True)
W = np.squeeze(W)
W = W.ravel()
weights_thismodel.append(W)
weights.append(weights_thismodel)
if idx > 0:
updates_thismodel = [weights[idx][i] - weights[idx-1][i] for i in range(len(weights[idx]))]
updates_thismodel = [updates_thismodel[i] / weights[idx-1][i] for i in range(len(weights[idx-1]))]
rel_updates.append(updates_thismodel)
idx += 1
# print weights_norm
# print rel_updates
for i in range(len(weights)):
allweights = []
allupdates = []
for j in range(len(weights[i])):
# print i, j
weights[i][j][weights[i][j]==inf] = 0.
weights[i][j][weights[i][j]==-inf] = 0.
allweights += weights[i][j].tolist()
if i > 0:
rel_updates[i][j][rel_updates[i][j]==inf] = 0.
rel_updates[i][j][rel_updates[i][j]==-inf] = 0.
allupdates += rel_updates[i][j].tolist()
nbins = 50
current_epoch = epochs/5 * i
plt.clf()
fig = plt.figure()
plt.hist(allweights, bins=nbins, histtype='step', label='Weights after '+str(current_epoch)+' training epochs')
plt.yscale('log')
plt.xlabel('Weight')
plt.ylabel('Number of nodes')
fig.savefig(plotfolder+'/Weights_epoch'+str(current_epoch)+'.pdf')
plt.close()
if i>0:
plt.clf()
fig = plt.figure()
plt.hist(allupdates, bins=nbins, histtype='step', label='Relative updates after '+str(current_epoch)+' training epochs')
plt.yscale('log')
plt.xlabel('Relative weight update')
plt.ylabel('Number of nodes')
fig.savefig(plotfolder+'/Updates_epoch'+str(current_epoch)+'.pdf')
plt.close()
def plot_confusion_matrices(parameters, plotfolder, pred_train, labels_train, sample_weights_train, eventweights_train, pred_val, labels_val, sample_weights_val, eventweights_val, use_best_model=False):
print 'Starting to plot confusion matrix'
tag = dict_to_str(parameters)
eqweight = parameters['eqweight']
classtitles = get_classtitles(parameters)
labels_1d = np.empty(labels_val.shape[0])
pred_1d = np.empty(pred_val.shape[0])
for i in range(len(labels_1d)):
label = -1
predclass = -1
maxpred = -1
for j in range(labels_val.shape[1]):
if labels_val[i,j] == 1: label = j
if maxpred < pred_val[i,j]:
maxpred = pred_val[i,j]
predclass = j
if label == -1: raise ValueError('For this event, the labels of all classes are 0, so the event doesn\'t have a class?')
labels_1d[i] = label
pred_1d[i] = predclass
if eqweight:
conf_matrix_train = conf_matrix(labels_train, pred_train, sample_weights_train)
conf_matrix_val = conf_matrix(labels_val, pred_val, sample_weights_val)
else:
conf_matrix_train = conf_matrix(labels_train, pred_train, eventweights_train)
conf_matrix_val = conf_matrix(labels_val, pred_val, eventweights_val)
plt.clf()
fig = plt.figure()
plot_confusion_matrix(conf_matrix_val, classes=classtitles, title='Confusion matrix for validation set, without normalization')
title = 'Confusion_matrix_val'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
plt.clf()
fig = plt.figure()
plot_confusion_matrix(conf_matrix_val, classes=classtitles, normalize=True, axis='x', title='Confusion matrix for validation set, rows normalized')
title = 'Confusion_matrix_val_normx'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
plt.clf()
fig = plt.figure()
plot_confusion_matrix(conf_matrix_val, classes=classtitles, normalize=True, axis='y', title='Confusion matrix for validation set, columns normalized')
title = 'Confusion_matrix_val_normy'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
plt.clf()
fig = plt.figure()
plot_confusion_matrix(conf_matrix_train, classes=classtitles, title='Confusion matrix for training set, without normalization')
title = 'Confusion_matrix_train'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
plt.clf()
fig = plt.figure()
plot_confusion_matrix(conf_matrix_train, classes=classtitles, normalize=True, axis='x', title='Confusion matrix for training set, rows normalized')
title = 'Confusion_matrix_train_normx'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
plt.clf()
fig = plt.figure()
plot_confusion_matrix(conf_matrix_train, classes=classtitles, normalize=True, axis='y', title='Confusion matrix for training set, columns normalized')
title = 'Confusion_matrix_train_normy'
if use_best_model: title += '_best'
title += '.pdf'
fig.savefig(plotfolder+'/'+title)
plt.close()
def plot_outputs_2d(parameters, plotfolder, pred_vals, lumiweights_vals, use_best_model=False):
print 'Starting to plot 2d plots of output variables'
tag = dict_to_str(parameters)
eqweight = parameters['eqweight']
classes = parameters['classes']
classtitles = get_classtitles(parameters)
gErrorIgnoreLevel = kWarning
gROOT.SetBatch(True);
for i in classes.keys():
for j in classes.keys():
if j <= i: continue
idx = 0
hists = []
for trueclass in classes.keys():
c1 = TCanvas('c1', 'c1', 600, 600)
hist = TH2F( classtitles[trueclass], classtitles[trueclass], 100, 0, 1.0001, 100, 0, 1.0001)
for k in range(pred_vals[trueclass][i].shape[0]):
hist.Fill(pred_vals[trueclass][i][k], pred_vals[trueclass][j][k], lumiweights_vals[trueclass][i][k])
hist.SetMarkerColor(rootcolors[colorstr[trueclass]])
hist.GetXaxis().SetTitle('Classifier output, node ' + classtitles[i])
hist.GetYaxis().SetTitle('Classifier output, node ' + classtitles[j])
hist.Scale(1./hist.Integral())
hist.GetZaxis().SetRangeUser(1.E-7, 1)
hist.Draw("COLZ")
idx += 1
gStyle.SetOptStat(0)
c1.SetLogz(True)
title = 'Outputs_trueclass'+classtitles[trueclass]+'_node'+str(i)+'_node'+str(j)
if use_best_model: title += '_best'
title += '.pdf'
c1.SaveAs(plotfolder+'/'+title)
del c1
def plot_outputs_1d_nodes(parameters, plotfolder, pred_trains, labels_train, weights_trains, lumiweights_trains, normweights_trains, pred_vals, labels_val, weights_vals, lumiweights_vals, normweights_vals, pred_signals=None, eventweight_signals=None, normweight_signals=None, usesignals=[0], use_best_model=False):
print 'Starting to plot the classifier output distribution'
tag = dict_to_str(parameters)
classtitles = get_classtitles(parameters)
do_sig = (pred_signals is not None) and (eventweight_signals is not None) and (normweight_signals is not None)
for cl in range(labels_train.shape[1]):
# 'cl' is the output node number
nbins = 100
binwidth = 1./float(nbins)
y_trains = {}
y_vals = {}
y_trains_norm = {}
y_vals_norm = {}
bin_edges_trains = {}
bin_edges_vals = {}
bin_centers = {}
yerrs = {}
yerrs_norm = {}
y_signals = {}
yerrs_signals = {}
for i in range(labels_train.shape[1]):
# 'i' is the true class (always the first index)
y_trains[i], dummy = np.histogram(pred_trains[i][cl], bins=nbins, weights=lumiweights_trains[i][cl])
y_trains_norm[i], bin_edges_trains[i] = np.histogram(pred_trains[i][cl], bins=nbins, weights=weights_trains[i][cl])
y_vals[i], dummy = np.histogram(pred_vals[i][cl], bins=nbins, weights=lumiweights_vals[i][cl])
y_vals_norm[i], bin_edges_vals[i] = np.histogram(pred_vals[i][cl], bins=nbins, weights=weights_vals[i][cl])
bin_centers[i] = 0.5*(bin_edges_trains[i][1:] + bin_edges_trains[i][:-1])
yerrs_norm[i] = y_vals_norm[i]**0.5
yerrs[i] = y_vals[i]**0.5
y_vals_norm[i] = y_vals_norm[i] * normweights_vals[i][cl][0]
yerrs_norm[i] = yerrs_norm[i] * normweights_vals[i][cl][0]
if do_sig:
for key in pred_signals.keys():
y_signals[key], dummy = np.histogram(pred_signals[key][:,cl], bins=nbins, weights=eventweight_signals[key])
yerrs_signals[key] = y_signals[key]**0.5