-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_data.py
297 lines (261 loc) · 11 KB
/
make_data.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
#!/usr/bin/env python
__author__ = "Pavez J. <[email protected]>"
import ROOT
import numpy as np
import sys
import os.path
import pdb
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from utils import printMultiFrame, printFrame, saveFig, loadData,\
makeROC, makeSigBkg, makePlotName
'''
Functions to make model and data for the decomposed training
method
'''
# Default model parameters
# private coefficients
coeffs_g = [[ 0.28174199,0.46707738,0.25118062],[0.18294893,0.33386682,0.48318425],[ 0.25763285,0.28015834,0.46220881]]
# gaussians parameters
mu_g = []
cov_g = []
mu_g.append([5.,5.,4.,3.,5.,5.,4.5,2.5,4.,3.5])
mu_g.append([2.,4.5,0.6,5.,6.,4.5,4.2,0.2,4.1,3.3])
mu_g.append([1.,0.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0.,0.,0.,0.,0.,0.,11.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.,0.,1.3]])
def makeModelPrivateND(vars_g,c0, c1, n_private=3, coeffs=coeffs_g,cov_l=cov_g, mu_l=mu_g,
workspace='workspace_DecomposingTestOfMixtureModelsClassifiers.root',
dir='/afs/cern.ch/user/j/jpavezse/systematics',model_g='mlp',
c1_g='',verbose_printing=False,load_cov=False):
'''
RooFit statistical model for the data
'''
# Statistical model
w = ROOT.RooWorkspace('w')
print 'Generating initial distributions'
cov_m = []
mu_m = []
mu_str = []
cov_root = []
vec = []
argus = ROOT.RooArgList()
# features
for i,var in enumerate(vars_g):
w.factory('{0}[{1},{2}]'.format(var,-25,30))
argus.add(w.var(var))
n = len(cov_l[0][0])
for glob in range(3):
for priv in range(n_private):
if load_cov == False:
cov_i = np.random.random((n,n))
cov_i = cov_i + cov_i.transpose()
cov_i = cov_i + n*np.eye(n)
np.savetxt('{0}/covariance_{1}_{2}.txt'.format(dir,glob,priv),
cov_i,fmt='%f')
else:
cov_i = np.matrix(np.loadtxt('{0}/data/covariance_{1}_{2}.txt'.format(
dir,glob,priv)))
print cov_i
# generate covriance matrix
cov_m.append(cov_i)
cov_root.append(ROOT.TMatrixDSym(len(vars_g)))
for i,var1 in enumerate(vars_g):
for j,var2 in enumerate(vars_g):
if i <= j:
cov_root[-1][i][j] = cov_m[-1][i,j]
else:
cov_root[-1][i][j] = cov_m[-1][j,i]
getattr(w,'import')(cov_root[-1],'cov{0}'.format(glob*3 + priv))
# generate mu vectors
mu_m.append(np.array(mu_l[glob]) + meansum[glob][priv])
vec.append(ROOT.TVectorD(len(vars_g)))
for i, mu in enumerate(mu_m[-1]):
vec[-1][i] = mu
mu_str.append(','.join([str(mu) for mu in mu_m[-1]]))
# create multivariate gaussian
gaussian = ROOT.RooMultiVarGaussian('f{0}_{1}'.format(glob,priv),
'f{0}_{1}'.format(glob,priv),argus,vec[-1],cov_root[-1])
getattr(w,'import')(gaussian)
# create private mixture model
priv_coeffs = np.array(coeffs[glob])
#print 'priv coef {0} {1}'.format(priv_coeffs, priv_coeffs.sum())
sum_str = ','.join(['c_{0}_{1}[{2}]*f{0}_{1}'.format(glob,j,priv_coeffs[j]) for j in range(n_private)])
w.factory('SUM::f{0}({1})'.format(glob,sum_str))
#mixture model
w.factory("SUM::F0(c00[{0}]*f0,c01[{1}]*f1,f2)".format(c0[0],c0[1]))
w.factory("SUM::F1(c10[{0}]*f0,c11[{1}]*f1,f2)".format(c1[0],c1[1]))
# Check Model
w.Print()
w.writeToFile('{0}/{1}'.format(dir,workspace))
if verbose_printing == True:
printFrame(w,['x0','x1','x2'],[w.pdf('f0'),w.pdf('f1'),w.pdf('f2')],'decomposed_model',['f0','f1','f2']
,dir=dir,model_g=model_g,range=[-15,20],title='Single distributions',x_text='x0',y_text='p(x)')
printFrame(w,['x0','x1','x2'],[w.pdf('F0'),w.pdf('F1')],'full_model',['Bkg','Bkg+Signal'],
dir=dir,model_g=model_g,range=[-15,20],title='Composed model',x_text='x0',y_text='p(x)')
printFrame(w,['x0','x1','x2'],[w.pdf('F1'),'f0'],'full_signal', ['Bkg','Signal'],
dir=dir,model_g=model_g,range=[-15,20],title='Background and signal',x_text='x0',y_text='p(x)')
return w
def makeModelND(vars_g,c0,c1,cov_l=cov_g,mu_l=mu_g,
workspace='workspace_DecomposingTestOfMixtureModelsClassifiers.root',
dir='/afs/cern.ch/user/j/jpavezse/systematics',model_g='mlp',
c1_g='',verbose_printing=False):
'''
RooFit statistical model for the data
'''
# Statistical model
w = ROOT.RooWorkspace('w')
print 'Generating initial distributions'
cov_m = []
mu_m = []
mu_str = []
cov_root = []
vec = []
argus = ROOT.RooArgList()
#features
for i,var in enumerate(vars_g):
w.factory('{0}[{1},{2}]'.format(var,-25,30))
argus.add(w.var(var))
for glob in range(3):
# generate covariance matrix
cov_m.append(np.matrix(cov_l[glob]))
cov_root.append(ROOT.TMatrixDSym(len(vars_g)))
for i,var1 in enumerate(vars_g):
for j,var2 in enumerate(vars_g):
cov_root[-1][i][j] = cov_m[-1][i,j]
getattr(w,'import')(cov_root[-1],'cov{0}'.format(glob))
# generate mu vector
mu_m.append(np.array(mu_l[glob]))
vec.append(ROOT.TVectorD(len(vars_g)))
for i, mu in enumerate(mu_m[-1]):
vec[-1][i] = mu
mu_str.append(','.join([str(mu) for mu in mu_m[-1]]))
# multivariate gaussian
gaussian = ROOT.RooMultiVarGaussian('f{0}'.format(glob),
'f{0}'.format(glob),argus,vec[-1],cov_root[-1])
getattr(w,'import')(gaussian)
# mixture models
w.factory("SUM::F0(c00[{0}]*f0,c01[{1}]*f1,f2)".format(c0[0],c0[1]))
w.factory("SUM::F1(c10[{0}]*f0,c11[{1}]*f1,f2)".format(c1[0],c1[1]))
# Check Model
w.Print()
w.writeToFile('{0}/{1}'.format(dir,workspace))
if verbose_printing == True:
printFrame(w,['x0','x1','x2'],[w.pdf('f0'),w.pdf('f1'),w.pdf('f2')],'decomposed_model',['f0','f1','f2']
,dir=dir,model_g=model_g,range=[-15,20],title='Single distributions',x_text='x0',y_text='p(x)',print_pdf=True)
printFrame(w,['x0','x1','x2'],[w.pdf('F0'),w.pdf('F1')],'full_model',['Bkg','Bkg+Signal'],
dir=dir,model_g=model_g,range=[-15,20],title='Composed model',x_text='x0',y_text='p(x)',print_pdf=True)
printFrame(w,['x0','x1','x2'],[w.pdf('F1'),'f0'],'full_signal', ['Bkg','Signal'],
dir=dir,model_g=model_g,range=[-15,20],title='Background and signal',x_text='x0',y_text='p(x)',print_pdf=True)
return w
def makeModel(c0,c1,cov_l=cov_g,mu_l=mu_g,
workspace='workspace_DecomposingTestOfMixtureModelsClassifiers.root',
dir='/afs/cern.ch/user/j/jpavezse/systematics',model_g='mlp',
c1_g='',verbose_printing=False):
'''
RooFit statistical model for the data
'''
# Statistical model
w = ROOT.RooWorkspace('w')
#w.factory("EXPR::f1('cos(x)**2 + .01',x)")
w.factory("EXPR::f2('exp(x*-1)',x[0,5])")
w.factory("EXPR::f1('0.3 + exp(-(x-5)**2/5.)',x)")
w.factory("EXPR::f0('exp(-(x-2.5)**2/1.)',x)")
#w.factory("EXPR::f2('exp(-(x-2)**2/2)',x)")
w.factory("SUM::F0(c00[{0}]*f0,c01[{1}]*f1,f2)".format(c0[0],c0[1]))
w.factory("SUM::F1(c10[{0}]*f0,c11[{1}]*f1,f2)".format(c1[0],c1[1]))
# Check Model
w.Print()
w.writeToFile('{0}/workspace_DecomposingTestOfMixtureModelsClassifiers.root'.format(dir))
if verbose_printing == True:
printFrame(w,['x'],[w.pdf('f0'),w.pdf('f1'),w.pdf('f2')],'decomposed_model',['f0','f1','f2']
,dir=dir,model_g=model_g,range=[-15,20],title='Single distributions',x_text='x0',y_text='p(x)',
print_pdf=True)
printFrame(w,['x'],[w.pdf('F0'),w.pdf('F1')],'full_model',['Bkg','Bkg+Signal'],
dir=dir,model_g=model_g,range=[-15,20],title='Composed model',x_text='x0',y_text='p(x)',print_pdf=True)
printFrame(w,['x'],[w.pdf('F1'),'f0'],'full_signal', ['Bkg','Signal'],
dir=dir,model_g=model_g,range=[-15,20],title='Background and signal',x_text='x0',y_text='p(x)',
print_pdf=True)
def makeData(vars_g,c0,c1, num_train=500,num_test=100,no_train=False,
workspace='workspace_DecomposingTestOfMixtureModelsClassifiers.root',
dir='/afs/cern.ch/user/j/jpavezse/systematics',
c1_g='',model_g='mlp'):
# Start generating data
'''
Each function will be discriminated pair-wise
so n*n datasets are needed (maybe they can be reused?)
'''
f = ROOT.TFile('{0}/{1}'.format(dir,workspace))
w = f.Get('w')
f.Close()
print 'Making Data'
# Start generating data
'''
Each function will be discriminated pair-wise
so n*n datasets are needed (maybe they can be reused?)
'''
# make data from root pdf
def makeDataFi(x, pdf, num):
traindata = np.zeros((num,len(vars_g)))
data = pdf.generate(x,num)
traindata[:] = [[data.get(i).getRealValue(var) for var in vars_g]
for i in range(num)]
return traindata
# features
vars = ROOT.TList()
for var in vars_g:
vars.Add(w.var(var))
x = ROOT.RooArgSet(vars)
# make data from pdf and save to .dat in folder
# ./data/{model}/{c1}
for k,c in enumerate(c0):
print 'Making {0}'.format(k)
if not no_train:
traindata = makeDataFi(x,w.pdf('f{0}'.format(k)), num_train)
np.savetxt('{0}/data/{1}/{2}/train_{3}.dat'.format(dir,model_g,c1_g,k),
traindata,fmt='%f')
testdata = makeDataFi(x, w.pdf('f{0}'.format(k)), num_test)
np.savetxt('{0}/data/{1}/{2}/test_{3}.dat'.format(dir,model_g,c1_g,k),
testdata,fmt='%f')
if not no_train:
traindata = makeDataFi(x,w.pdf('F0'), num_train)
np.savetxt('{0}/data/{1}/{2}/train_F0.dat'.format(dir,model_g,c1_g),
traindata,fmt='%f')
traindata = makeDataFi(x,w.pdf('F1'), num_train)
np.savetxt('{0}/data/{1}/{2}/train_F1.dat'.format(dir,model_g,c1_g),
traindata,fmt='%f')
testdata = makeDataFi(x, w.pdf('F0'), num_test)
np.savetxt('{0}/data/{1}/{2}/test_F0.dat'.format(dir,model_g,c1_g),
testdata,fmt='%f')
testdata = makeDataFi(x, w.pdf('F1'), num_test)
np.savetxt('{0}/data/{1}/{2}/test_F1.dat'.format(dir,model_g,c1_g),
testdata,fmt='%f')