forked from DL4Jets/DeepJetCore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing.py
424 lines (343 loc) · 15 KB
/
testing.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
'''
Created on 21 Mar 2017
@author: jkiesele
'''
#
# make training etc..
# create new module:
# in: model, .dc file for testing, identifier
# out: root tree with <ident>_probB, ... , and a descriptor of these new files that links to the old files, too
#
# create a module to read in one or more descriptors (source ntuples all the same)
# passes the descriptors and selected branches to compare (probB, newproB, ..) or something to a C++ module
# C++: creates ROC curves with root
#
#
#
from __future__ import print_function
colormap=['red'
, 'blue'
, 'darkgreen'
, 'purple'
, 'darkred'
, 'darkblue'
, 'green'
, 'darkpurple'
, 'gray']
dashedcolormap=['red','red,dashed'
, 'blue','blue,dashed'
, 'darkgreen','darkgreen,dashed'
, 'purple','purple,dashed'
, 'darkred','darkred,dashed'
, 'darkblue','darkblue,dashed'
, 'green','green,dashed'
, 'darkpurple','darkpurple,dashed'
, 'gray','gray,dashed']
from pdb import set_trace
class testDescriptor(object):
def __init__(self):
self.__sourceroots=[]
self.__predictroots=[]
self.metrics=[]
def makePrediction(self, model, testdatacollection, outputDir,
ident='', store_labels = False, monkey_class=''):
import numpy as np
from root_numpy import array2root
import os
monkey_class_obj = None
if monkey_class:
module, classname = tuple(monkey_class.split(':'))
_temp = __import__(module, globals(), locals(), [classname], -1)
monkey_class_obj = getattr(_temp, classname)
outputDir=os.path.abspath(outputDir)
if len(ident)>0:
ident='_'+ident
self.__sourceroots=[]
self.__predictroots=[]
self.metrics=[]
for i in range(len(testdatacollection.samples)):
sample=testdatacollection.samples[i]
originroot=testdatacollection.originRoots[i]
outrootfilename=os.path.basename(originroot).split('.')[0]+'_predict'+ident+'.root'
fullpath=testdatacollection.getSamplePath(sample)
if monkey_class_obj is not None:
testdatacollection.dataclass = monkey_class_obj()
td=testdatacollection.dataclass
td.readIn(fullpath)
truthclasses=td.getUsedTruth()
formatstring=[]
if len(truthclasses)>0 and len(truthclasses[0])>0:
formatstring = ['prob_%s%s' % (i, ident) for i in truthclasses]
regressionclasses=[]
if hasattr(td, 'regressiontargetclasses'):
regressionclasses=td.regressiontargetclasses
#new implementation. Please check with the store_labels option, Mauro
formatstring.extend(['reg_%s%s' % (i, ident) for i in regressionclasses])
features=td.x
labels=td.y
weights=td.w[0]
prediction = model.predict(features)
if isinstance(prediction, list):
all_write = np.concatenate(prediction, axis=1)
else:
all_write = prediction
all_write = np.concatenate([all_write, weights], axis=1)
formatstring.append('weight')
if not all_write.shape[1] == len(formatstring):
raise ValueError('Prediction output does not match with the provided targets!')
all_write = np.core.records.fromarrays(np.transpose(all_write), names= ','.join(formatstring))
array2root(all_write,outputDir+'/'+outrootfilename,"tree",mode="recreate")
#self.metrics.append(metric)
self.__sourceroots.append(originroot)
self.__predictroots.append(outputDir+'/'+outrootfilename)
print(formatstring)
print('\ncreated predition friend tree '+outputDir+'/'+outrootfilename+ ' for '+originroot)
continue
#print(prediction[1].shape[1])
if isinstance(prediction, list):
formatstring.extend(['reg_%s%s' % (i, ident) for i in regressionclasses])
if prediction[1].shape[1] > len(regressionclasses):
raise ValueError('Regression (2nd prediction output) does not match with the provided targets!')
all_write = np.concatenate(prediction, axis=1)
if store_labels:
all_write = np.concatenate((all_write, labels[0], labels[1]), axis=1)
formatstring.extend(truthclasses)
formatstring.append('truePt')
elif prediction.shape[1] == len(truthclasses):
all_write = prediction
if store_labels:
all_write = np.concatenate((all_write, labels if not isinstance(labels, list) else labels[0]), axis=1)
formatstring.extend(truthclasses)
else:
formatstring.extend(['reg_%s%s' % (i, ident) for i in regressionclasses])
if prediction.shape[1] > 2:
raise ValueError('Regression output does not match with the provided targets!')
all_write = prediction
if store_labels:
all_write = np.concatenate((all_write, labels), axis=1)
formatstring.append('truePt')
all_write = np.concatenate([all_write, weights], axis=1)
formatstring.append('weight')
all_write = np.core.records.fromarrays(np.transpose(all_write), names= ','.join(formatstring))
array2root(all_write,outputDir+'/'+outrootfilename,"tree",mode="recreate")
#self.metrics.append(metric)
self.__sourceroots.append(originroot)
self.__predictroots.append(outputDir+'/'+outrootfilename)
print(formatstring)
print('\ncreated predition friend tree '+outputDir+'/'+outrootfilename+ ' for '+originroot)
def writeToTextFile(self, outfile):
'''
Very simple text file output to use when creating chains with friends.
Format:
source0.root prediction0.root
source1.root prediction1.root
...
'''
listifle=open(outfile,'w')
for i in range(len(self.__predictroots)):
listifle.write(self.__sourceroots[i]+' '+self.__predictroots[i]+'\n')
listifle.close()
def makeASequence(arg,length):
isseq=(not hasattr(arg, "strip") and
hasattr(arg, "__getitem__") or
hasattr(arg, "__iter__"))
out=[]
if isseq:
if len(arg)==length:
return arg
for i in range(length/len(arg)):
out.extend(arg)
else:
for i in range(length):
out.append(arg)
return out
def createColours(colors_list,name_list,nnames=None,extralegend=[]):
extramulti=1
if extralegend==None:
extralegend=[]
if len(extralegend):
extramulti=len(extralegend)
if not nnames:
nnames=len(name_list)
if 'auto' in colors_list:
newcolors=[]
usemap=colormap
if 'dashed' in colors_list and not len(extralegend):
usemap=dashedcolormap
if len(name_list) > len(usemap)*extramulti:
raise Exception('colors_list=auto: too many entries, color map too small: '+str(len(name_list))+'/'+str(len(usemap)*extramulti))
stylecounter=0
colorcounter=0
for i in range(len(name_list)):
if len(extralegend):
newcolors.append(usemap[colorcounter] + ','+extralegend[stylecounter].split('?')[0])
else:
newcolors.append(usemap[colorcounter])
colorcounter=colorcounter+1
if colorcounter == nnames:
colorcounter=0
stylecounter=stylecounter+1
colors_list=newcolors
return colors_list
def makeROCs_async(intextfile, name_list, probabilities_list, truths_list, vetos_list,
colors_list, outpdffile, cuts='',cmsstyle=False, firstcomment='',secondcomment='',
invalidlist='',
extralegend=None,
logY=True,
individual=False,
xaxis=""):#['solid?udsg','hatched?c']):
import copy
namelistcopy= copy.deepcopy(name_list)
extralegcopy=copy.deepcopy(extralegend)
if cmsstyle and extralegcopy==None:
extralegcopy=['solid?udsg','dashed?c']
if extralegcopy==None:
extralegcopy=[]
nnames=len(namelistcopy)
nextra=0
if extralegcopy:
nextra=len(extralegcopy)
if nextra>1 and len(namelistcopy[-1].strip(' ')) >0 :
extranames=['INVISIBLE']*(nnames)*(nextra-1)
namelistcopy.extend(extranames)
colors_list=createColours(colors_list,namelistcopy,nnames,extralegcopy)
#check if multi-input file
files=makeASequence(intextfile,len(namelistcopy))
allcuts=makeASequence(cuts,len(namelistcopy))
probabilities_list=makeASequence(probabilities_list,len(namelistcopy))
truths_list=makeASequence(truths_list,len(namelistcopy))
vetos_list=makeASequence(vetos_list,len(namelistcopy))
invalidlist=makeASequence(invalidlist,len(namelistcopy))
import c_makeROCs
def worker():
try:
c_makeROCs.makeROCs(files,namelistcopy,
probabilities_list,
truths_list,
vetos_list,
colors_list,
outpdffile,allcuts,cmsstyle, firstcomment,secondcomment,invalidlist,extralegcopy,logY,
individual,xaxis)
except Exception as e:
print('error for these inputs:')
print(files)
print(allcuts)
print(probabilities_list)
print(truths_list)
print(vetos_list)
print(invalidlist)
raise e
import multiprocessing
p = multiprocessing.Process(target=worker)
p.start()
return p
# use multiprocessing return thread for waiting option
def makePlots_async(intextfile, name_list, variables, cuts, colours,
outpdffile, xaxis='',yaxis='',
normalized=False,profiles=False,
minimum=-1e100,maximum=1e100,widthprofile=False,
treename="deepntuplizer/tree"):
files_list=makeASequence(intextfile,len(name_list))
variables_list=makeASequence(variables,len(name_list))
cuts_list=makeASequence(cuts,len(name_list))
colours_list=createColours(colours, name_list)
import c_makePlots
def worker():
if profiles:
c_makePlots.makeProfiles(files_list,name_list,
variables_list,cuts_list,colours_list,
outpdffile,xaxis,yaxis,normalized,minimum, maximum,treename)
else:
c_makePlots.makePlots(files_list,name_list,
variables_list,cuts_list,colours_list,
outpdffile,xaxis,yaxis,normalized,profiles,widthprofile,minimum,maximum,treename)
# return worker()
import multiprocessing
p = multiprocessing.Process(target=worker)
p.start()
return p
def makeEffPlots_async(intextfile, name_list, variables, cutsnum,cutsden, colours,
outpdffile, xaxis='',yaxis='',
minimum=-1e100,maximum=1e100,
rebinfactor=1, SetLogY = False, Xmin = -1., Xmax = 1000. ,
treename="deepntuplizer/tree"):
files_list=makeASequence(intextfile,len(name_list))
variables_list=makeASequence(variables,len(name_list))
cutsnum_list=makeASequence(cutsnum,len(name_list))
cutsden_list=makeASequence(cutsden,len(name_list))
colours_list=createColours(colours, name_list)
import c_makePlots
def worker():
try:
c_makePlots.makeEffPlots(files_list,name_list,
variables_list,cutsnum_list,cutsden_list,colours_list,
outpdffile,xaxis,yaxis,rebinfactor,SetLogY, Xmin, Xmax,minimum,maximum,treename)
except Exception as e:
print('error for these inputs:')
print(files_list)
print(name_list)
print(variables_list)
print(cutsnum_list)
print(cutsden_list)
print(colours_list)
raise e
# return worker()
import multiprocessing
p = multiprocessing.Process(target=worker)
p.start()
return p
def make_association(txtfiles, input_branches=None, output_branches=None, limit=None):
from root_numpy import root2array
from pandas import DataFrame
#parse associations
def association(fname):
return dict(tuple(i.strip().split()) for i in open(fname))
associations = [association(i) for i in txtfiles]
#check that the input files are the same
keys = set(associations[0].keys())
for i in associations:
if set(i.keys()) != keys:
raise ValueError('Association files with different inputs')
#make input lists
file_lists = [[] for _ in range(len(associations))]
input_files = []
for idx, infile in enumerate(associations[0]):
if limit and idx >= limit: break
input_files.append(infile)
for i, association in enumerate(associations):
file_lists[i].append(association[infile])
truth = DataFrame(root2array(input_files, branches=input_branches, treename='deepntuplizer/tree'))
models = [
DataFrame(root2array(i, branches=output_branches)) for i in file_lists
]
return truth, models
######### old part - keep for reference, might be useful some day
#just a collection of what will be helpful
#import numpy as np
#from numpy.lib.recfunctions import merge_arrays
#dt1 = [('foo', int), ('bar', float)]
#dt2 = [('foobar', int), ('barfoo', float)]
#aa = np.empty(6, dtype=dt1).view(np.recarray)
#bb = np.empty(6, dtype=dt2).view(np.recarray)
#
#cc = merge_arrays((aa, bb), asrecarray=True, flatten=True)
#type(cc)
#print (cc)
#
#
## this can be used to add new 'branches' to the input array for a root output tuple
##in traindata
#
##only save them for test data (not for val or train)
#passthrough=['jet_pt','jet_eta','pfCombinedInclusiveSecondaryVertexV2BJetTags'] #etc
#savedpassarrays=[]
#for i in range (len(passthrough)):
# savedpassarrays[i]
#
#
##here come the truth and predicted parts again, weights for convenience
#all_write = np.core.records.fromarrays( np.hstack(((predict_test,labels),savedpassarrays)).transpose(),
# names='probB, probC, probUDSG, isB, isC, isUDSG, [passthrough]')
# #can probably go formats = 'float32,float32,float32,float32,float32,float32,[npt * float32]')
#
#