-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
469 lines (376 loc) · 18 KB
/
util.py
File metadata and controls
469 lines (376 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import train as tn
from pprint import pprint
import importlib
import os
import numpy as np
from copy import copy
import json
params = {}
# -------------------------------------------------------------------------------------
def defaultParameters(**kwargs):
"""
This function is used to set up parameters such as the directory where the root
files are located, the branches which will be read from the root trees and so
forth.
: params
dataDir : string - specifies the directory of the data. (default =
root://t3dcachedb03.psi.ch//pnfs/psi.ch/cms/trivcat/store
/user/musella/mod_dep_005")
dataFname : string - specifies the file name of the root file inside
the directory dataDir. (default = "output_InsideAcceptance
_125.root")
pfx : string - specifies the prefix going from the root data file
name dataFname down to the branches of the different classes.
The classes are NoTag_0, SigmaMpTTag_0, SigmaMpTTag_1 and
SigmaMpTTag_2. (default = "genDiphotonDumper/trees
/InsideAcceptance_125_13TeV")
inputDir : string - specifis the directory of the classifier in case
one is going to load a classifier. (default = ".")
inputName : string - specifies the name of the classifier fitted.
(default = "effFitter")
outDir : string - specifis the directory where the classifier is
stored. (default = ".")
outName : string - specifies the name of the classifier when saved in
outDir. (default = "effFitter_out")
ncats : int - specifies the number of categories. (default = 3)
genBranches : list - specifies the branches of generated events.
(default = ["genPt","genRapidity",
"genJet2p5Pt0","genJet2p5Rapidity0",
"genJet2p5Pt1","genJet2p5Rapidity1",
"genJet2p5Pt2","genJet2p5Rapidity2",
"genJet2p5Pt3","genJet2p5Rapidity3",
"weight",
"genNjets2p5"
])
recoBranches : list - specifies the branches of reconstructed events.
(default = ['recoPt','recoRapidity',"recoNjets2p5"])
rndseed : int = 9347865 - specifies the starting point of the random
number seed. Needed for shuffling.
rndseed 2 : int = 2315645 - see rndseed for explanantion.
split_frac : float - specifies the amount of data that will be used for
the training. (default = 0.75 i.e. 75% of the data is used
for training)
load : boolean - specifies whether to load an already trained
classifier (True) or generate a new one (False).
(default = True)
forceMake : boolean - specifies whether to force to generate a new
classifier instance.
clean :
classifiers : string - specifies the type of machine learning technique
you want to apply, e.g. class for classification.
(default = [])
defineBins :
: retruns
:
"""
global params
params["dataDir"]="root://t3dcachedb03.psi.ch//pnfs/psi.ch/cms/trivcat/store/user/musella/mod_dep_005"
params["dataFname"] = "output_InsideAcceptance_125.root"
params["pfx"] = "genDiphotonDumper/trees/InsideAcceptance_125_13TeV"
params["inputDir"] = "."
params["inputName"] = "effFitter"
params["outDir"] = "."
params["outName"] = "effFitter_out"
params["ncats"] = 3
params["genBranches"] = ["genPt","genRapidity",
"genJet2p5Pt0","genJet2p5Rapidity0",
"genJet2p5Pt1","genJet2p5Rapidity1",
"genJet2p5Pt2","genJet2p5Rapidity2",
"genJet2p5Pt3","genJet2p5Rapidity3",
"genJet2p5Pt4","genJet2p5Rapidity4",
"weight",
"genNjets2p5",
'genLeadGenIso',
'genSubleadGenIso'
]
params["recoBranches"] = ['recoPt','recoRapidity',"recoNjets2p5"]
params["rndseed"] = 9347865
params["rndseed2"] = 2315645
params["split_frac"] = 0.75
params["load"] = True
params["forceMake"] = False
params["clean"] = []
params["classifiers"] = []
params["defineBins"] = {}
params.update(kwargs)
# -------------------------------------------------------------------------------------
def setParams(default=None,config_files=None):
global params
if default:
print('entered default')
params = copy(default)
if not config_files:
print('entered config files named my_train_config')
config_files = os.environ.get('my_train_config',None)
print('hi')
print(config_files)
if config_files:
print('load some params')
for cfg in config_files.split(','):
print("reading %s" % cfg)
with open(cfg) as fin:
loadparams = json.loads(fin.read())
print(loadparams)
params.update(loadparams)
# -------------------------------------------------------------------------------------
def loadOrMake():
"""
This function loads or makes an object from the class EfficiencyFitter from
train.py and retruns it.
:params
:
retruns
made : train.EfficiencyFitter - instance of the class EfficiencyFitter
"""
global params
name = params["inputName"]
load = params["load"]
forceMake = params["forceMake"]
#need some talking of the code :-)
#++++++++++++++++++++++++++++++++++++++++++++
if forceMake :
print("Forced production of an object with the name "+str(name)
+ " and the following paramters ")
else :
if load :
print("Load object with the name "+str(name)
+ " and the following paramters ")
else :
print("Create object with the name "+str(name)
+ " and the following paramters ")
pprint(params)
#++++++++++++++++++++++++++++++++++++++++++++
make = False
if load:
print('loading')
print(name)
print(params["inputDir"])
onDisk = tn.IO.load(name, path=params["inputDir"], nodata=forceMake)
pprint(onDisk)
if not forceMake:
pprint(onDisk.df.columns)
pprint(onDisk.clfs)
print('onDisk.genBranches', onDisk.genBranches)
print('params["genBranches"]', params["genBranches"])
print('onDisk.recoBranches', onDisk.recoBranches)
print('params["recoBranches"]', params["recoBranches"])
if onDisk.genBranches != params["genBranches"] or onDisk.recoBranches != params["recoBranches"]:
make = True
#make = False
if onDisk.ncats != params["ncats"]:
make = True
load = False
else:
make = True
if make or forceMake:
files = params.get("dataFiles",[])
if len(files) == 0:
files.append( (0,params["dataFname"]) )
if not load:
#Initialize an instance of the class EfficiencyFitter
made = tn.EfficiencyFitter(name)
made.input_files = files
else:
made = onDisk
if hasattr(made,"files"):
files = made.files
else:
print("Warining: efficiency fitter on disk did not store the list of files. Is it and old one?")
inputs = [ (os.path.join(params["dataDir"],ifil[1]),
ifil[0],params["pfx"] if len(ifil)<3 else ifil[2],True,
params.get("genpfx",None) if len(ifil)<4 else ifil[3] )
for ifil in files ]
#generates a pandas data frame in train.py which loads the appropriate
#branchens from the root tree
print ('genpfx', params.get("genpfx",None))
print(params['dataDir'])
print(ifil[1])
print(params['pfx'])
print(inputs)
made.readData(params["ncats"],params["genBranches"],params["recoBranches"],
inputs)
## fileName = os.path.join(params["dataDir"],params["dataFname"])
## made.readData(params["ncats"],params["genBranches"],params["recoBranches"],
## [(fileName,None,params["pfx"])])
#shuffles dataset and orders according this random indices
print('shuffling dataset')
np.random.seed(params['rndseed'])
made.df['random_index'] = np.random.permutation(range(made.df.index.size))
made.df.sort_values(by='random_index',inplace=True)
made.df.set_index('random_index',inplace=True)
made.split_frac = params['split_frac']
print('defining bins')
if 'genRapidity' in made.df.columns and ( not 'absGenRapidity' in made.df.columns ):
made.df['absGenRapidity'] = np.abs(made.df['genRapidity'])
# fold also the rapidity space of the jets, i.e. only care about absolut values of
# jet rapidities
JetRapidityNames = ['genJet2p5Rapidity0','genJet2p5Rapidity1',
'genJet2p5Rapidity2','genJet2p5Rapidity3','genJet2p5Rapidity4','genJet2p5Rapidity5']
for jetRapName in JetRapidityNames :
#replace gen with absGen (mind the capital G)
if jetRapName in made.df.columns and ( not 'abs'+'G'+jetRapName[1:] in made.df.columns ):
made.df['abs'+'G'+jetRapName[1:]] = np.abs(made.df[jetRapName])
#rapidities bellow 20 GeV
JetRapidityNames2 = ['genJet2p5pt20Rapidity0','genJet2p5pt20Rapidity1',
'genJet2p5pt20Rapidity2','genJet2p5pt20Rapidity3','genJet2p5pt20Rapidity4','genJet2p5pt20Rapidity5']
for jetRapName in JetRapidityNames2 :
#replace gen with absGen (mind the capital G)
if jetRapName in made.df.columns and ( not 'abs'+'G'+jetRapName[1:] in made.df.columns ):
made.df['abs'+'G'+jetRapName[1:]] = np.abs(made.df[jetRapName])
runDefineBins(made,params["defineBins"])
else:
made = onDisk
made.cleanClfs(params["clean"])
made.outdir = params["outDir"]
made.name = params["outName"]
return made
# -------------------------------------------------------------------------------------
def loadFromScratch():
"""
This function loads or makes an object from the class EfficiencyFitter from
train.py and retruns it.
:params
:
retruns
made : train.EfficiencyFitter - instance of the class EfficiencyFitter
"""
global params
name = params["inputName"]
load = params["load"]
forceMake = params["forceMake"]
pprint(params)
make = False
if load:
print('loading')
print(name)
print(params["inputDir"])
onDisk = tn.IO.load(name, path=params["inputDir"], nodata=forceMake)
pprint(onDisk)
if not forceMake:
pprint(onDisk.df.columns)
pprint(onDisk.clfs)
print('onDisk.genBranches', onDisk.genBranches)
print('params["genBranches"]', params["genBranches"])
print('onDisk.recoBranches', onDisk.recoBranches)
print('params["recoBranches"]', params["recoBranches"])
if onDisk.genBranches != params["genBranches"] or onDisk.recoBranches != params["recoBranches"]:
make = True
#make = False
if onDisk.ncats != params["ncats"]:
make = True
load = False
else:
make = True
if make or forceMake:
files = params.get("dataFiles",[])
if len(files) == 0:
files.append( (0,params["dataFname"]) )
if not load:
#Initialize an instance of the class EfficiencyFitter
made = tn.EfficiencyFitter(name)
made.input_files = files
else:
made = onDisk
if hasattr(made,"files"):
files = made.files
else:
print("Warining: efficiency fitter on disk did not store the list of files. Is it and old one?")
inputs = [ (os.path.join(params["dataDir"],ifil[1]),
ifil[0],params["pfx"] if len(ifil)<3 else ifil[2],True,
params.get("genpfx",None) if len(ifil)<4 else ifil[3] )
for ifil in files ]
#generates a pandas data frame in train.py which loads the appropriate
#branchens from the root tree
print(params['dataDir'])
print(ifil[1])
print(params['pfx'])
print(inputs)
made.readData(params["ncats"],params["genBranches"],params["recoBranches"],
inputs)
## fileName = os.path.join(params["dataDir"],params["dataFname"])
## made.readData(params["ncats"],params["genBranches"],params["recoBranches"],
## [(fileName,None,params["pfx"])])
#shuffles dataset and orders according this random indices
print('shuffling dataset')
np.random.seed(params['rndseed'])
made.df['random_index'] = np.random.permutation(range(made.df.index.size))
made.df.sort_values(by='random_index',inplace=True)
made.df.set_index('random_index',inplace=True)
made.split_frac = params['split_frac']
print('defining bins')
if 'genRapidity' in made.df.columns and ( not 'absGenRapidity' in made.df.columns ):
made.df['absGenRapidity'] = np.abs(made.df['genRapidity'])
# fold also the rapidity space of the jets, i.e. only care about absolut values of
# jet rapidities
JetRapidityNames = ['genJet2p5Rapidity0','genJet2p5Rapidity1',
'genJet2p5Rapidity2','genJet2p5Rapidity3','genJet2p5Rapidity4','genJet2p5Rapidity5']
for jetRapName in JetRapidityNames :
#replace gen with absGen (mind the capital G)
if jetRapName in made.df.columns and ( not 'abs'+'G'+jetRapName[1:] in made.df.columns ):
made.df['abs'+'G'+jetRapName[1:]] = np.abs(made.df[jetRapName])
print('Which bins are defined',params["defineBins"])
runDefineBins(made,params["defineBins"])
else:
made = onDisk
made.cleanClfs(params["clean"])
made.outdir = params["outDir"]
made.name = params["outName"]
return made
# -------------------------------------------------------------------------------------
def setupJoblib(ipp_profile='default'):
from sklearn.externals.joblib import Parallel, parallel_backend, register_parallel_backend
import ipyparallel as ipp
from ipyparallel.joblib import IPythonParallelBackend
global joblib_rc,joblib_view,joblib_be
joblib_rc = ipp.Client(profile=ipp_profile)
joblib_view = joblib_rc.load_balanced_view()
joblib_be = IPythonParallelBackend(view=joblib_view)
register_parallel_backend('ipyparallel',lambda : joblib_be,make_default=True)
# -------------------------------------------------------------------------------------
def runDefineBins(fitter,binsDef):
for name,params in binsDef.iteritems():
boundaries = fitter.defineBins(name,**params)
#--------------------------------------------------------------------------------------
def target_name(key):
postFix = ""
if key != 'class':
postFix = 'Cat' if (not key in params or not params[key][1].get('factorized',False)) else 'Bin'
return key+postFix
# -------------------------------------------------------------------------------------
def runTraining(effFitter,useAbsWeight=True):
global params
to_train = filter(lambda x: x not in effFitter.clfs.keys(), params["classifiers"])
print("We need to train the following classifiers %s" % " ".join(to_train) )
for key in to_train:
classifier,train_params = params[key]
pack,cls = classifier.rsplit('.',1)
classifier = getattr(importlib.import_module(pack),cls)
print("Fitting %s" % key)
print(classifier)
print(train_params)
if useAbsWeight :
if key == 'class':
effFitter.fitClass(classifier=classifier,**train_params)
else:
effFitter.fitBins(key,classifier=classifier,**train_params)
else :
if key == 'class':
effFitter.fitClass(classifier=classifier,weight_name='weight',ExtendClassBranches=ExtendClassBranches,
**train_params)
else:
effFitter.fitBins(key,classifier=classifier,weight_name='weight',**train_params)
# -------------------------------------------------------------------------------------
def runEvaluation(effFitter):
clf_keys = filter(lambda x: x in effFitter.clfs.keys(),params["classifiers"])
print(clf_keys)
for key in clf_keys:
print(key)
target = target_name(key)
print target
catKey = '%s_prob_0' % (target)
print catKey
print effFitter.df.columns
if not catKey in effFitter.df.columns:
print('running prediction for %s' % key)
effFitter.runPrediction(target,effFitter.clfs[key])
pprint(effFitter.df.columns)