forked from keras-team/keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eiter.py
270 lines (251 loc) · 11.3 KB
/
eiter.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
import os
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)
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=[]
for j in range(valid.totalnum()):
data,target=next(gen)
tar_set=np.append(tar_set,target[:,0])
pre_set=np.append(pre_set,self.model.predict(data,verbose=0)[:,0])
valid.reset()
auc_val=roc_auc_score(tar_set,pre_set)
results=self.model.evaluate_generator(valid.next(),valid.totalnum())
print(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=30,order=1,eta=0.,etabin=2.4):
self.eta=eta
self.etabin=etabin
self.channel=channel
self.istrain=istrain
#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=order
#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
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((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=[]
dauset=[]
variables=[]
labels=[]
rand=random.choice(self.rat)
for i in range(self.batch_size):
if(random.random()<0.5):
self.gjet.GetEntry(self.a)
self.a+=1
if(abs(self.gjet.eta)<self.eta or abs(self.gjet.eta)>self.eta+self.etabin):
while(abs(self.gjet.eta)<self.eta or abs(self.gjet.eta)>self.eta+self.etabin):
self.gjet.GetEntry(self.a)
self.a+=1
if(self.a>=self.gEnd):break
##label q=1 g=0
labels.append([0,1])#q=0 q=1
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_pid[t]==0):dau_is_nhad[t]=1.
else:dau_is_chad[t]=1.
if("c" in self.rc):
#jetset.append(np.array(self.gim).reshape((3,2*arnum+1,2*arnum+1)))
#jetset.append([np.array(self.gjet.image_chad_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_nhad_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_chad_mult_33).reshape(2*arnum+1,2*arnum+1)])
jetset.append([np.array(self.gjet.image_chad_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_nhad_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_electron_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_muon_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_photon_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_chad_mult_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_nhad_mult_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_electron_mult_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_muon_mult_33).reshape(2*arnum+1,2*arnum+1),np.array(self.gjet.image_photon_mult_33).reshape(2*arnum+1,2*arnum+1)])
if("r" in self.rc):
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.onehot):dauset.append([[dau_pt[dausort[i]], dau_deta[dausort[i]], dau_dphi[dausort[i]], dau_charge[dausort[i]], dau_pid[dausort[i]]] if len(dau_pt)>i else [0.,0.,0.,0.,0.] for i in range(self.channel)])
else:
if(self.order):
dauset.append([[dau_pt[dausort[i]], dau_deta[dausort[i]], dau_dphi[dausort[i]], dau_charge[dausort[i]], dau_is_e[dausort[i]], dau_is_mu[dausort[i]], dau_is_r[dausort[i]], dau_is_chad[dausort[i]], dau_is_nhad[dausort[i]]] if len(dau_pt)>i else [0.,0.,0.,0.,0.,0.,0.,0.,0.] for i in range(self.channel)])
else:
dauset.append([[dau_pt[i], dau_deta[i], dau_dphi[i], dau_charge[i], dau_is_e[i], dau_is_mu[i], dau_is_r[i], dau_is_chad[i], dau_is_nhad[i]] if len(dau_pt)>i else [0.,0.,0.,0.,0.,0.,0.,0.,0.] for i in range(self.channel)])
if(len(dau_pt)==0):
print self.a
print "@@@@@@@@@@@@@@"
print self.a
break
if(self.a>=self.gEnd):
self.a=self.gBegin
self.endfile=1
else:
self.qjet.GetEntry(self.b)
self.b+=1
if(abs(self.qjet.eta)<self.eta or abs(self.qjet.eta)>self.eta+self.etabin):
while(abs(self.qjet.eta)<self.eta or abs(self.qjet.eta)>self.eta+self.etabin):
self.qjet.GetEntry(self.b)
self.b+=1
if(self.b>=self.qEnd):break
##label q=1 g=0
labels.append([1,0])#q=0 q=1
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_pid[t]==0):dau_is_nhad[t]=1.
else:dau_is_chad[t]=1.
if("c" in self.rc):
jetset.append([np.array(self.qjet.image_chad_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.qjet.image_nhad_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.qjet.image_electron_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.qjet.image_muon_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.qjet.image_photon_pt_33).reshape(2*arnum+1,2*arnum+1),np.array(self.qjet.image_chad_mult_33).reshape(2*arnum+1,2*arnum+1),np.array(self.qjet.image_nhad_mult_33).reshape(2*arnum+1,2*arnum+1),np.array(self.qjet.image_electron_mult_33).reshape(2*arnum+1,2*arnum+1),np.array(self.qjet.image_muon_mult_33).reshape(2*arnum+1,2*arnum+1),np.array(self.qjet.image_photon_mult_33).reshape(2*arnum+1,2*arnum+1)])
if("r" in self.rc):
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.onehot):dauset.append([[dau_pt[dausort[i]], dau_deta[dausort[i]], dau_dphi[dausort[i]], dau_charge[dausort[i]], dau_pid[dausort[i]]] if len(dau_pt)>i else [0.,0.,0.,0.,0.] for i in range(self.channel)])
else:
if(self.order):
dauset.append([[dau_pt[dausort[i]], dau_deta[dausort[i]], dau_dphi[dausort[i]], dau_charge[dausort[i]], dau_is_e[dausort[i]], dau_is_mu[dausort[i]], dau_is_r[dausort[i]], dau_is_chad[dausort[i]], dau_is_nhad[dausort[i]]] if len(dau_pt)>i else [0.,0.,0.,0.,0.,0.,0.,0.,0.] for i in range(self.channel)])
else:
dauset.append([[dau_pt[i], dau_deta[i], dau_dphi[i], dau_charge[i], dau_is_e[i], dau_is_mu[i], dau_is_r[i], dau_is_chad[i], dau_is_nhad[i]] if len(dau_pt)>i else [0.,0.,0.,0.,0.,0.,0.,0.,0.] for i in range(self.channel)])
if(len(dau_pt)==0):
print self.a
print "@@@@@@@@@@@@@@"
print self.a
break
if(self.b>=self.qEnd):
self.b=self.qBegin
self.endfile=1
data=[]
for rc in self.rc:
if(rc=="r"):data.append(np.array(dauset))
if(rc=="c"):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