forked from keras-team/keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiter.py
402 lines (382 loc) · 16.6 KB
/
aiter.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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import subprocess
import numpy as np
import datetime
import random
import warnings
import ROOT as rt
import math
from keras.preprocessing.sequence import pad_sequences
from keras.callbacks import Callback
from array import array
from sklearn.metrics import roc_auc_score, auc, roc_curve
class AddVal(Callback):
def __init__(self,valid_sets,savename):
self.valid_sets = valid_sets
self.epoch=[]
self.history={}
self.savename=savename
def on_train_begin(self,logs=None):
self.epoch=[]
self.history={}
def on_epoch_end(self, epoch, logs=None):
logs=logs or {}
self.epoch.append(epoch)
print("validation")
for i,j in logs.items():
self.history.setdefault(i,[]).append(j)
for valid_set in self.valid_sets:
valid,val_name=valid_set
#valid.reset()
#gen=valid.next()
#tar_set=[]
#pre_set=[]
atar_set=[]
apre_set=[]
X,Y=valid
#X=X[0]
"""for j in range(valid.totalnum()):
data,target=next(gen)
#print(target)
#tar_set=np.append(tar_set,target[:,0])
#pre_set=np.append(pre_set,self.model.predict(data,verbose=0)[:,0])
try:atar_set.extend(target[:,0])
except:print(np.array(target).shape)
apre_set.extend(self.model.predict(data,verbose=0)[:,0])
valid.reset()"""
#tar_set=np.array(tar_set)
#pre_set=np.array(pre_set)
atar_set=np.array(Y)[:,0]
apre_set=np.array(self.model.predict(X,verbose=0)[:,0])
#print(valid.totalnum(),valid.batch_size)
#print("############")
#print(tar_set)
#print("AAAAAAAAAAAAAAAAAAAA")
#print(atar_set)
auc_val=roc_auc_score(atar_set,apre_set)
results=self.model.evaluate(X,Y)
print("validation",results,auc_val)
self.history.setdefault(val_name+"_auc",[]).append(auc_val)
for i,result in enumerate(results):
if(i==0):
name=val_name+"_loss"
else:
name=val_name+"_"+self.model.metrics[i-1][:3]
self.history.setdefault(name,[]).append(result)
f=open(self.savename+'/history','w')
f.write(str(self.history))
f.close()
class wkiter(object):
def __init__(self,data_path,data_names=['data'],label_names=['softmax_label'],batch_size=100,begin=0.0,end=1.0,rat=0.7,endcut=1,arnum=16,maxx=0.4,maxy=0.4,istrain=0, varbs=0,rc="rc",onehot=0,channel=64,order=1,eta=0.,etabin=2.4,pt=None,ptmin=0.,ptmax=2.,unscale=0,normb=10):
self.eta=eta
self.pt=pt
self.ptmin=ptmin
self.ptmax=ptmax
self.etabin=etabin
self.channel=channel
self.istrain=istrain
self.unscale=unscale
self.normb=normb*1.
#if(batch_size<100):
self.rand=0.5
# print("batch_size is small it might cause error")
self.count=0
self.rc=rc
self.onehot=onehot
self.order=1
#self.file=rt.TFile(data_path,'read')
dataname1=data_path[0]
dataname2=data_path[1]
self.qfile=rt.TFile(dataname1,'read')
self.gfile=rt.TFile(dataname2,'read')
print(dataname2)
self.gjet=self.gfile.Get("jetAnalyser")
self.gEntries=self.gjet.GetEntriesFast()
if(begin>1):
self.gBegin=int(begin)
else:
self.gBegin=int(begin*self.gEntries)
if(end>1):
self.gEnd=int(end)
else:
self.gEnd=int(self.gEntries*end)
self.a=self.gBegin
self.qjet=self.qfile.Get("jetAnalyser")
self.qEntries=self.qjet.GetEntriesFast()
if(begin>1):
self.qBegin=int(begin)
else:
self.qBegin=int(begin*self.qEntries)
if(end>1):
self.qEnd=int(end)
else:
self.qEnd=int(self.qEntries*end)
self.b=self.qBegin
self.ratt=rat
self.rat=sorted([1-rat,rat])
self.batch_size = batch_size
if(varbs==0):
self._provide_data = zip(data_names, [(self.batch_size, 3, 33, 33)])
else:
data_names=['images','variables']
self._provide_data = zip(data_names, [(self.batch_size, 3, 33, 33),(self.batch_size,5)])
self.varbs=varbs
self._provide_label = zip(label_names, [(self.batch_size,)])
self.arnum=arnum
self.maxx=maxx
self.maxy=maxy
self.endfile=0
self.endcut=endcut
qjetset=[]
gjetset=[]
qptset=[]
gptset=[]
qetaset=[]
getaset=[]
qpidset=[]
gpidset=[]
for i in range(self.gEntries):
if(self.a>=self.gEnd):
self.a=self.gBegin
break
#if((self.a-self.gBegin)%int((self.gEnd-self.gBegin)/self.normb==0):print('.')
self.gjet.GetEntry(self.a)
##label q=1 g=0
self.a+=1
if(self.eta>abs(self.gjet.eta) or self.eta+self.etabin<abs(self.gjet.eta)):
continue
if(self.pt!=None):
if(self.pt*self.ptmin>self.gjet.pt or self.pt*self.ptmax<self.gjet.pt):
continue
gptset.append(self.gjet.pt)
getaset.append(self.gjet.eta)
gpidset.append(self.gjet.parton_id)
if("c" in self.rc):
maxchadpt=1.*max(self.gjet.image_chad_pt_33)/self.normb
maxnhadpt=1.*max(self.gjet.image_nhad_pt_33)/self.normb
maxelecpt=1.*max(self.gjet.image_electron_pt_33)/self.normb
maxmuonpt=1.*max(self.gjet.image_muon_pt_33)/self.normb
maxphotonpt=1.*max(self.gjet.image_photon_pt_33)/self.normb
maxchadmult=1.*max(self.gjet.image_chad_mult_33)/self.normb
maxnhadmult=1.*max(self.gjet.image_nhad_mult_33)/self.normb
maxelecmult=1.*max(self.gjet.image_electron_mult_33)/self.normb
maxmuonmult=1.*max(self.gjet.image_muon_mult_33)/self.normb
maxphotonmult=1.*max(self.gjet.image_photon_mult_33)/self.normb
if(self.unscale==1 or maxchadpt==0):maxchadpt=1.
if(self.unscale==1 or maxnhadpt==0):maxnhadpt=1.
if(self.unscale==1 or maxelecpt==0):maxelecpt=1.
if(self.unscale==1 or maxmuonpt==0):maxmuonpt=1.
if(self.unscale==1 or maxphotonpt==0):maxphotonpt=1.
if(self.unscale==1 or maxchadmult==0):maxchadmult=1.
if(self.unscale==1 or maxnhadmult==0):maxnhadmult=1.
if(self.unscale==1 or maxelecmult==0):maxelecmult=1.
if(self.unscale==1 or maxmuonmult==0):maxmuonmult=1.
if(self.unscale==1 or maxphotonmult==0):maxphotonmult=1.
gjetset.append([(np.array(self.gjet.image_chad_pt_33)/maxchadpt).reshape(2*arnum+1,2*arnum+1),(np.array(self.gjet.image_nhad_pt_33)/maxnhadpt).reshape(2*arnum+1,2*arnum+1),(np.array(self.gjet.image_electron_pt_33)/maxelecpt).reshape(2*arnum+1,2*arnum+1),(np.array(self.gjet.image_muon_pt_33)/maxmuonpt).reshape(2*arnum+1,2*arnum+1),(np.array(self.gjet.image_photon_pt_33)/maxphotonpt).reshape(2*arnum+1,2*arnum+1),(np.array(self.gjet.image_chad_mult_33)/maxchadmult).reshape(2*arnum+1,2*arnum+1),(np.array(self.gjet.image_nhad_mult_33)/maxnhadmult).reshape(2*arnum+1,2*arnum+1),(np.array(self.gjet.image_electron_mult_33)/maxelecmult).reshape(2*arnum+1,2*arnum+1),(np.array(self.gjet.image_muon_mult_33)/maxmuonmult).reshape(2*arnum+1,2*arnum+1),(np.array(self.gjet.image_photon_mult_33)/maxphotonmult).reshape(2*arnum+1,2*arnum+1)])
if("r" in self.rc):
dau_pt=self.gjet.dau_pt
dau_deta=self.gjet.dau_deta
dau_dphi=self.gjet.dau_dphi
dau_charge=self.gjet.dau_charge
dau_pid=self.gjet.dau_pid
dau_is_e=np.zeros(len(dau_pid))
dau_is_mu=np.zeros(len(dau_pid))
dau_is_r=np.zeros(len(dau_pid))
dau_is_chad=np.zeros(len(dau_pid))
dau_is_nhad=np.zeros(len(dau_pid))
for t in range(len(dau_pid)):
if(abs(dau_pid[t])==11):dau_is_e[t]=1.
elif(abs(dau_pid[t])==13):dau_is_mu[t]=1.
elif(abs(dau_pid[t])==22):dau_is_r[t]=1.
elif(dau_charge[t]==0):dau_is_nhad[t]=1.
else:dau_is_chad[t]=1.
dausort=sorted(range(len(dau_pt)),key=lambda k: dau_pt[k],reverse=True)
if(self.order):
maxdaupt=1.*max(dau_pt)/self.normb
maxdaudeta=1.*max(dau_deta)/self.normb
maxdaudphi=1.*max(dau_dphi)/self.normb
maxdaucharge=1.*max(dau_charge)/self.normb
maxdauc=1.*max(dau_is_chad)/self.normb
maxdaun=1.*max(dau_is_nhad)/self.normb
maxdaue=1.*max(dau_is_e)/self.normb
maxdaum=1.*max(dau_is_mu)/self.normb
maxdaup=1.*max(dau_is_r)/self.normb
if(self.unscale==1 or maxdaupt==0):maxdaupt=1.
if(self.unscale==1 or maxdaudeta==0):maxdaudeta=1.
if(self.unscale==1 or maxdaudphi==0):maxdaudphi=1.
if(self.unscale==1 or maxdaucharge==0):maxdaucharge=1.
if(self.unscale==1 or maxdauc==0):maxdauc=1.
if(self.unscale==1 or maxdaun==0):maxdaun=1.
if(self.unscale==1 or maxdaue==0):maxdaue=1.
if(self.unscale==1 or maxdaum==0):maxdaum=1.
if(self.unscale==1 or maxdaup==0):maxdaup=1.
gjetset.append([[dau_pt[dausort[i]]/maxdaupt, dau_deta[dausort[i]]/maxdaudeta, dau_dphi[dausort[i]]/maxdaudphi, dau_charge[dausort[i]]/maxdaucharge, dau_is_e[dausort[i]]/maxdaue, dau_is_mu[dausort[i]]/maxdaum, dau_is_r[dausort[i]]/maxdaup, dau_is_chad[dausort[i]]/maxdauc, dau_is_nhad[dausort[i]]/maxdaun] if len(dau_pt)>i else [0.,0.,0.,0.,0.,0.,0.,0.,0.] for i in range(self.channel)])
self.gjetset=np.array(gjetset)
del gjetset
self.gptset=np.array(gptset)
del gptset
self.getaset=np.array(getaset)
del getaset
self.gpidset=np.array(gpidset)
del gpidset
for i in range(self.qEntries):
if(self.b>=self.qEnd):
self.b=self.qBegin
break
#if((self.b-self.qBegin)%int((self.qEnd-self.qBegin)/self.normb==0):print(',')
self.qjet.GetEntry(self.b)
##label q=1 g=0
self.b+=1
if(self.eta>abs(self.qjet.eta) or self.eta+self.etabin<abs(self.qjet.eta)):
continue
if(self.pt!=None):
if(self.pt*self.ptmin>self.qjet.pt or self.pt*self.ptmax<self.qjet.pt):
continue
qptset.append(self.qjet.pt)
qetaset.append(self.qjet.eta)
qpidset.append(self.qjet.parton_id)
if("c" in self.rc):
maxchadpt=1.*max(self.qjet.image_chad_pt_33)/self.normb
maxnhadpt=1.*max(self.qjet.image_nhad_pt_33)/self.normb
maxelecpt=1.*max(self.qjet.image_electron_pt_33)/self.normb
maxmuonpt=1.*max(self.qjet.image_muon_pt_33)/self.normb
maxphotonpt=1.*max(self.qjet.image_photon_pt_33)/self.normb
maxchadmult=1.*max(self.qjet.image_chad_mult_33)/self.normb
maxnhadmult=1.*max(self.qjet.image_nhad_mult_33)/self.normb
maxelecmult=1.*max(self.qjet.image_electron_mult_33)/self.normb
maxmuonmult=1.*max(self.qjet.image_muon_mult_33)/self.normb
maxphotonmult=1.*max(self.qjet.image_photon_mult_33)/self.normb
if(self.unscale==1 or maxchadpt==0):maxchadpt=1.
if(self.unscale==1 or maxnhadpt==0):maxnhadpt=1.
if(self.unscale==1 or maxelecpt==0):maxelecpt=1.
if(self.unscale==1 or maxmuonpt==0):maxmuonpt=1.
if(self.unscale==1 or maxphotonpt==0):maxphotonpt=1.
if(self.unscale==1 or maxchadmult==0):maxchadmult=1.
if(self.unscale==1 or maxnhadmult==0):maxnhadmult=1.
if(self.unscale==1 or maxelecmult==0):maxelecmult=1.
if(self.unscale==1 or maxmuonmult==0):maxmuonmult=1.
if(self.unscale==1 or maxphotonmult==0):maxphotonmult=1.
qjetset.append([(np.array(self.qjet.image_chad_pt_33)/maxchadpt).reshape(2*arnum+1,2*arnum+1),(np.array(self.qjet.image_nhad_pt_33)/maxnhadpt).reshape(2*arnum+1,2*arnum+1),(np.array(self.qjet.image_electron_pt_33)/maxelecpt).reshape(2*arnum+1,2*arnum+1),(np.array(self.qjet.image_muon_pt_33)/maxmuonpt).reshape(2*arnum+1,2*arnum+1),(np.array(self.qjet.image_photon_pt_33)/maxphotonpt).reshape(2*arnum+1,2*arnum+1),(np.array(self.qjet.image_chad_mult_33)/maxchadmult).reshape(2*arnum+1,2*arnum+1),(np.array(self.qjet.image_nhad_mult_33)/maxnhadmult).reshape(2*arnum+1,2*arnum+1),(np.array(self.qjet.image_electron_mult_33)/maxelecmult).reshape(2*arnum+1,2*arnum+1),(np.array(self.qjet.image_muon_mult_33)/maxmuonmult).reshape(2*arnum+1,2*arnum+1),(np.array(self.qjet.image_photon_mult_33)/maxphotonmult).reshape(2*arnum+1,2*arnum+1)])
if("r" in self.rc):
dau_pt=self.qjet.dau_pt
dau_deta=self.qjet.dau_deta
dau_dphi=self.qjet.dau_dphi
dau_charge=self.qjet.dau_charge
dau_pid=self.qjet.dau_pid
dau_is_e=np.zeros(len(dau_pid))
dau_is_mu=np.zeros(len(dau_pid))
dau_is_r=np.zeros(len(dau_pid))
dau_is_chad=np.zeros(len(dau_pid))
dau_is_nhad=np.zeros(len(dau_pid))
for t in range(len(dau_pid)):
if(abs(dau_pid[t])==11):dau_is_e[t]=1.
elif(abs(dau_pid[t])==13):dau_is_mu[t]=1.
elif(abs(dau_pid[t])==22):dau_is_r[t]=1.
elif(dau_charge[t]==0):dau_is_nhad[t]=1.
else:dau_is_chad[t]=1.
dausort=sorted(range(len(dau_pt)),key=lambda k: dau_pt[k],reverse=True)
#dauset.append([[dau_pt[dausort[i]], dau_deta[dausort[i]], dau_dphi[dausort[i]], dau_charge[dausort[i]]] if len(dau_pt)>i else [0.,0.,0.,0.] for i in range(20)])
if(self.order):
maxdaupt=1.*max(dau_pt)/self.normb
maxdaudeta=1.*max(dau_deta)/self.normb
maxdaudphi=1.*max(dau_dphi)/self.normb
maxdaucharge=1.*max(dau_charge)/self.normb
maxdauc=1.*max(dau_is_chad)/self.normb
maxdaun=1.*max(dau_is_nhad)/self.normb
maxdaue=1.*max(dau_is_e)/self.normb
maxdaum=1.*max(dau_is_mu)/self.normb
maxdaup=1.*max(dau_is_r)/self.normb
if(self.unscale==1 or maxdaupt==0):maxdaupt=1.
if(self.unscale==1 or maxdaudeta==0):maxdaudeta=1.
if(self.unscale==1 or maxdaudphi==0):maxdaudphi=1.
if(self.unscale==1 or maxdaucharge==0):maxdaucharge=1.
if(self.unscale==1 or maxdauc==0):maxdauc=1.
if(self.unscale==1 or maxdaun==0):maxdaun=1.
if(self.unscale==1 or maxdaue==0):maxdaue=1.
if(self.unscale==1 or maxdaum==0):maxdaum=1.
if(self.unscale==1 or maxdaup==0):maxdaup=1.
qjetset.append([[dau_pt[dausort[i]]/maxdaupt, dau_deta[dausort[i]]/maxdaudeta, dau_dphi[dausort[i]]/maxdaudphi, dau_charge[dausort[i]]/maxdaucharge, dau_is_e[dausort[i]]/maxdaue, dau_is_mu[dausort[i]]/maxdaum, dau_is_r[dausort[i]]/maxdaup, dau_is_chad[dausort[i]]/maxdauc, dau_is_nhad[dausort[i]]/maxdaun] if len(dau_pt)>i else [0.,0.,0.,0.,0.,0.,0.,0.,0.] for i in range(self.channel)])
self.qjetset=np.array(qjetset)
del qjetset
self.qptset=np.array(qptset)
del qptset
self.qetaset=np.array(qetaset)
del qetaset
self.qpidset=np.array(qpidset)
del qpidset
"""if("r" in self.rc):
for c in range(channel):
for i in range(3):
#std=np.std(abs(np.append(self.qjetset[:,c,i],self.gjetset[:,c,i])))
#mean=np.mean(abs(np.append(self.qjetset[:,c,i],self.gjetset[:,c,i])))
self.qjetset[:,c,i]=(self.qjetset[:,c,i])#/mean
self.gjetset[:,c,i]=(self.gjetset[:,c,i])#/mean
"""
self.reset()
print("length ",len(self.gjetset),len(self.qjetset))
def __iter__(self):
return self
def reset(self):
self.rand=0.5
self.gjet.GetEntry(self.gBegin)
self.qjet.GetEntry(self.qBegin)
self.a=self.gBegin
self.b=self.qBegin
self.endfile = 0
self.count=0
def __next__(self):
return self.next()
@property
def provide_data(self):
return self._provide_data
@property
def provide_label(self):
return self._provide_label
def close(self):
self.file.Close()
def sampleallnum(self):
return self.Entries
def trainnum(self):
return self.End-self.Begin
def totalnum(self):
return int(math.ceil(1.*(self.gEnd-self.gBegin+self.qEnd-self.qBegin)/(self.batch_size*1.00)))
def next(self):
while self.endfile==0:
self.count+=1
arnum=self.arnum
jetset=[]
variables=[]
labels=[]
for i in range(self.batch_size):
if(random.random()<0.5):
if(self.a-self.gBegin>=len(self.gjetset)):
self.a=self.gBegin
self.endfile=1
break
labels.append([0,1])
jetset.append(self.gjetset[self.a-self.gBegin])
self.a+=1
else:
if(self.b-self.qBegin>=len(self.qjetset)):
self.b=self.qBegin
self.endfile=1
break
labels.append([1,0])
jetset.append(self.qjetset[self.b-self.qBegin])
self.b+=1
data=[]
data.append(np.array(jetset))
label=np.array(labels)
#if(self.totalnum()<=self.count):
# if(self.istrain==1):print "\nreset\n"
# self.reset()
if(self.endfile==1):
#print "\nendd\n"
self.reset()
#print "\n",self.count,self.istrain,"\n"
yield data, label
#else:
#if(self.istrain==1):
# print "\n",datetime.datetime.now()
#raise StopIteration