/
myclasses.py
476 lines (414 loc) · 18.5 KB
/
myclasses.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
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
469
470
471
472
473
474
475
476
#!/user/bin/env python
# encoding: utf-8
from corrfitter import Corr2, CorrFitter, fastfit, Corr3
from gvar import gvar, log, exp, sqrt, BufferDict, fmt_errorbudget, fmt
from gvar.dataset import Dataset, avg_data, bin_data
from pvalue import conf_int
from datetime import date, time, datetime
from time import strftime
import numpy as np
import yaml
class Ensemble:
def __init__(self, pfile):
params = yaml.load(pfile)
self.datafile = params['datafile']
self.NT = params['NT']
self.NX = params['NX']
self.m_l = params['m_l']
self.m_s = params['m_s']
self.m_c = params['m_c']
self.Text = params['Text']
self.ncon = params['ncon']
self.pi2twist = params['pi2twist']
self.k2twist = params['k2twist']
self.pi0 = params['pi0']
self.pi0_err = params['pi0_err']
self.bin = 1
self.data_full = Dataset(self.datafile.rstrip())
self.data = avg_data(bin_data(self.data_full,self.bin))
class Fit2:
def __init__(self, dic, params):
#print('Creating 2pt fit')
#print(dic)
self.name = dic['name']
self.tmin = dic['tmin']
self.tmax = dic['tmax']
self.nexp = dic['nexp']
self.noxp = dic['noxp']
self.priorfile = dic['priorfile']
self.p0file = dic['p0file']
self.savep0 = dic['savep0']
self.type = dic['type']
self.dofit = dic['dofit']
name = self.name
if name == 'pimom0':
self.a=(name+':a')
self.dE=(name+':dE')
else:
self.a=(name+':a',name+':ao')
self.dE=(name+':dE',name+':dEo')
# build model
self.model = self.build_model(params)
self.fitter = CorrFitter(self.model)
# build prior
self.prior = self.build_prior(params)
def build_model(self,params):
model = Corr2( datatag='2pt'+self.name,tp=params.NT,tdata=range(params.NT),
tfit=range(self.tmin,self.tmax+1),
a=self.a,b=self.a,dE=self.dE,s=(1.,-1.) )
return model
def build_prior(self,params):
"""build prior from file"""
prior = BufferDict()
name = self.name
nexp = self.nexp
noxp = self.noxp
prior_file = open(self.priorfile,'r')
pp = yaml.load(prior_file)
prior['log('+name+':dE)'] = [ gvar(0,0) for i in range(nexp) ]
prior['log('+name+':a)'] = [ gvar(0,0) for i in range(nexp) ]
# non-osc. state priors
prior['log('+name+':dE)'][0] = log(gvar(pp['e0'][0],pp['e0'][1]))
prior['log('+name+':a)'][0] = log(gvar(pp['a0'][0],pp['a0'][1]))
for i in range(1,nexp):
prior['log('+name+':dE)'][i] = log(gvar(pp['e1'][0],pp['e1'][1]))
prior['log('+name+':a)'][i] = log(gvar(pp['a1'][0],pp['a1'][1]))
# osc. state priors
if self.name != 'pimom0':
prior['log('+name+':dEo)'] = [ 0 for i in range(noxp) ]
prior['log('+name+':ao)'] = [ 0 for i in range(noxp) ]
prior['log('+name+':dEo)'][0] = log(gvar(pp['o0'][0],pp['o0'][1]))
prior['log('+name+':ao)'][0] = log(gvar(pp['b0'][0],pp['b0'][1]))
for i in range(1,noxp):
prior['log('+name+':dEo)'][i] = log(gvar(pp['o1'][0],pp['o1'][1]))
prior['log('+name+':ao)'][i] = log(gvar(pp['b1'][0],pp['b1'][1]))
#print(prior)
return prior
def print_fit(self,par,outfile):
"""print the energy and amplitude results from a fit"""
fit = self.results
name = self.name
p = fit.p
self.chi2_cal()
self.chi2_aug_part()
self.Q = conf_int(self.chi2/2, par.ncon, self.dof/2)
dE = exp(p['log('+name+':dE)'])
E = [sum(dE[:i+1]) for i in range(self.nexp)]
a = exp(p['log('+name+':a)'])
if self.name != 'pimom0':
dEo = exp(p['log('+name+':dEo)'])
Eo = [sum(dEo[:i+1]) for i in range(self.nexp)]
ao = exp(p['log('+name+':ao)'])
ofile = open(outfile,'a')
if self.dof > 0:
chi2dof = self.chi2/self.dof
else:
chi2dof = 99.9
if chi2dof > 99:
chi2dof = 99.9
form = "{:s} & {:2d} - {:2d} & {:d}+{:d} & {:4.1f} & {:3d} & {:4.2f} "
ena = "& {:<11s} & {:<11s} "
ofile.write( form.format( self.name,self.tmin,self.tmax,self.nexp,self.noxp,chi2dof,self.dof,self.Q ) )
for i in range(self.nexp):
ofile.write( ena.format(E[i].fmt(ndecimal=4),a[i].fmt(ndecimal=4),Eo[i].fmt(ndecimal=4),ao[i].fmt(ndecimal=4)) )
ofile.write(" \\\\ \n")
def print_prior(self,par,outfile):
"""print the priors and result for comparison"""
fit = self.results
p = self.prior
name = self.name
f = fit.p
dE = exp(f['log('+name+':dE)'])
E = [sum(dE[:i+1]) for i in range(self.nexp)]
a = exp(f['log('+name+':a)'])
if self.name != 'pimom0':
dEo = exp(f['log('+name+':dEo)'])
Eo = [sum(dEo[:i+1]) for i in range(self.nexp)]
ao = exp(f['log('+name+':ao)'])
pdE = exp(p['log('+name+':dE)'])
pa = exp(p['log('+name+':a)'])
if self.name != 'pimom0':
pdEo = exp(p['log('+name+':dEo)'])
pao = exp(p['log('+name+':ao)'])
ofile = open(outfile,'a')
ofile.write("{:s} & Prior & Value & & Prior & Value \\\\ # {:d}-{:d} {:d}+{:d} \n".format(
name,self.tmin,self.tmax,self.nexp,self.nexp))
form = "$a_{:d}$ & {:<11s} & {:<11s} & $dE_{:d}$ & {:<11s} & {:<11s} \\\\ \n"
for state in range(self.nexp):
ofile.write( form.format( state,pa[state].fmt(),a[state].fmt(),
state,pdE[state].fmt(),dE[state].fmt() ) )
if self.name != 'pimom0':
form = "$a'_{:d}$ & {:<11s} & {:<11s} & $dE'_{:d}$ & {:<11s} & {:<11s} \\\\ \n"
for state in range(self.nexp):
ofile.write( form.format( state,pao[state].fmt(),ao[state].fmt(),
state,pdEo[state].fmt(),dEo[state].fmt() ) )
def print_model(self,par,outfile):
"""print the fit data and model and the difference"""
fit = self.results
t,g,dg,gth,dgth = self.fitter.collect_fitresults()['2pt'+self.name]
ofile = open(outfile,'a')
ofile.write( " t | {:12s} value | sigma_m sigma_v \n".format('2pt'+self.name) )
for it in range(0,self.tmax-self.tmin):
data = gvar(g[it],dg[it])
model = gvar(gth[it],dgth[it])
diff1 = (data.mean-model.mean)/data.sdev
diff2 = (data.mean-model.mean)/model.sdev
ofile.write( " {:3d} | {:<20s} {:<20s} | {:+5.3f} {:+5.3f} \n".format(
t[it],data.fmt(),model.fmt(),diff1,diff2) )
def chi2_cal(self):
"""calculate chi2 of fit"""
t,g,dg,gth,dgth = self.fitter.collect_fitresults()['2pt'+self.name]
chi2 = 0.
for it in range(0,self.tmax-self.tmin):
chi2 = ( (g[it]-gth[it])/dg )**2
total = sum(chi2)
#print(chi2,total)
parameters = 0
for key in self.prior.keys():
parameters = parameters + len(self.results.p[key])
self.dof = len(g)-parameters
self.chi2 = total
return total,self.dof
def chi2_aug_part(self):
"""calculate the part of the chi2 coming from the priors"""
pth = self.results.p
p = self.prior
aug = 0
for key in p.keys():
for exp in range(self.nexp):
aug = aug + ( (p[key][exp].mean-pth[key][exp].mean)/p[key][exp].sdev )**2
self.aug = aug
return aug
class Fit3:
def __init__(self, dic, params, childfit, parentfit):
self.name = dic['name']
self.Texts = dic['Text']
self.child_index = dic['child_index']
self.parent_index = dic['parent_index']
self.include2ptdata = dic['include2ptdata']
self.priorfile = dic['priorfile']
self.child = childfit
self.parent = parentfit
self.tmin = childfit.tmin
self.tmax = parentfit.tmin
self.nexp = self.child.nexp
self.dofit = True
if childfit.name == 'pimom0':
self.Von=None
self.Voo=None
else:
self.Von='Von'
self.Voo='Voo'
self.Vnn='Vnn'
self.Vno='Vno'
self.model = self.build_3pt_models(params)
self.fitter = CorrFitter(self.model)
self.prior = self.build_prior(params)
self.prior.update( self.child.prior )
self.prior.update( self.parent.prior )
def build_3pt_models(self,params):
models_3pt = []
for T in self.Texts:
models_3pt.append( Corr3( datatag="3pt"+self.name+"T"+str(T),T=T,tdata=range(T+1),
tfit=range(self.tmin,T-self.tmax+1),
a=self.child.a,dEa=self.child.dE,sa=(1.,-1.),
b=self.parent.a,dEb=self.parent.dE,sb=(1.,-1.),
Vnn=self.Vnn,Vno=self.Vno,Von=self.Von,Voo=self.Voo) )
if self.include2ptdata:
models_3pt.append( self.child.build_model(params) )
models_3pt.append( self.parent.build_model(params) )
return models_3pt
def build_prior(self,params):
"""build interaction matrix prior"""
prior = BufferDict()
nexp = self.child.nexp
noxp = self.child.noxp
prior_file = open(self.priorfile,'r')
pp = yaml.load(prior_file)
prior['Vnn'] = [[ gvar(pp['v22'][0],pp['v22'][1]) for i in range(nexp)] for j in range(nexp)]
prior['Vno'] = [[ gvar(pp['v22'][0],pp['v22'][1]) for i in range(nexp)] for j in range(nexp)]
prior['Vnn'][0][0] = gvar(pp['v00'][0],pp['v00'][1])
prior['Vno'][0][0] = gvar(pp['v11'][0],pp['v11'][1])
for i in range(1,nexp):
for j in range(1,nexp):
if i<1 or j<1:
prior['Vnn'][i][j] = gvar(pp['v11'][0],pp['v11'][1])
prior['Vno'][i][j] = gvar(pp['v11'][0],pp['v11'][1])
if self.name != "DPm0":
prior['Voo'] = [[ gvar(pp['v22'][0],pp['v22'][1]) for i in range(nexp)] for j in range(nexp)]
prior['Von'] = [[ gvar(pp['v22'][0],pp['v22'][1]) for i in range(nexp)] for j in range(nexp)]
prior['Voo'][0][0] = gvar(pp['v11'][0],pp['v11'][1])
prior['Von'][0][0] = gvar(pp['v11'][0],pp['v11'][1])
for i in range(1,nexp):
for j in range(1,nexp):
if i<1 or j<1:
prior['Voo'][i][j] = gvar(pp['v11'][0],pp['v11'][1])
prior['Von'][i][j] = gvar(pp['v11'][0],pp['v11'][1])
return prior
def print_fit(self,par,outfile):
"""print the energy and amplitude results from a fit"""
fit = self.results
name = self.name
p = fit.p
self.chi2_cal()
#self.chi2_aug_part()
self.Q = conf_int(self.chi2/2, par.ncon, self.dof/2)
nexp = self.child.nexp
name = self.child.name
CdE = exp(p['log('+name+':dE)'])
CE = [sum(CdE[:i+1]) for i in range(nexp)]
a = exp(p['log('+name+':a)'])
if self.name != 'pimom0':
CdEo = exp(p['log('+name+':dEo)'])
CEo = [sum(CdEo[:i+1]) for i in range(nexp)]
ao = exp(p['log('+name+':ao)'])
name = self.parent.name
PdE = exp(p['log('+name+':dE)'])
PE = [sum(PdE[:i+1]) for i in range(nexp)]
b = exp(p['log('+name+':a)'])
if self.name != 'pimom0':
PdEo = exp(p['log('+name+':dEo)'])
PEo = [sum(PdEo[:i+1]) for i in range(nexp)]
bo = exp(p['log('+name+':ao)'])
# calculating f_0
# DOESN"T WORK FOR D TO K YET!!!!!!!!!
if self.child.name == 'pimom0':
mpi = CE[0]
else:
mpi = gvar(par.pi0,par.pi0_err)
if self.child.name == 'pimom0' or self.child.name == 'pimom2':
m_q = par.m_l
else:
m_q = par.m_s
Epi = CE[0]
mD = PE[0]
v = p['Vnn'][0][0]
self.f_0 = v*sqrt(Epi*mD)*(par.m_c-m_q)/(mD**2-mpi**2)
self.qsq = mpi**2+mD**2-2*mD*Epi
ofile = open(outfile,'a')
if self.dof != 0:
chi2dof = self.chi2/self.dof
else:
chi2dof = 99.9
if chi2dof > 99:
chi2dof = 99.9
pars = "{:s} & {:2d}-{:2d} & {:2d}-{:2d} & {:d}+{:d} & {:4.1f} & {:3d} & {:4.2f} & "
energies = "{:s} & {:<11s} & {:<11s} & "
form = "{:<12s} & {:<12s} \\\\ \n"
ofile.write( pars.format( self.name,self.tmin,self.child.tmax,self.tmax,self.parent.tmax,
nexp,nexp,chi2dof,self.dof,self.Q )
+energies.format( self.child.name,CE[0].fmt(ndecimal=4),a[0].fmt(ndecimal=4) )
+energies.format( self.parent.name,PE[0].fmt(ndecimal=4),b[0].fmt(ndecimal=4) )
+form.format( self.f_0.fmt(ndecimal=4), self.qsq.fmt(ndecimal=4) ) )
def print_model(self,par,outfile):
"""print the fit data and model and the difference"""
fit = self.results
names = ["2pt"+self.child.name,"2pt"+self.parent.name]
[names.append("3pt"+self.name+"T"+str(T)) for T in self.Texts]
print(names)
ofile = open(outfile,'a')
ofile.write("{:5s} models, {:d}+{:d} fit, {:d} to T-{:d} window "+strftime("%H:%M:%S")+"\n".format(self.name,self.nexp,self.nexp,self.child.tmin,self.child.tmax))
for name in names:
t,g,dg,gth,dgth = self.fitter.collect_fitresults()[name]
ofile.write( " t | {:<11s} theory | sigma_data sigma_th \n".format(name) )
for it in range(0,len(t)):
data = gvar(g[it],dg[it])
model = gvar(gth[it],dgth[it])
diff1 = (data.mean-model.mean)/data.sdev
diff2 = (data.mean-model.mean)/model.sdev
ofile.write( " {:3d} | {:<20s} {:<20s} | {:+6.3f} {:+6.3f} \n".format(
t[it],data.fmt(),model.fmt(),diff1,diff2) )
def print_prior(self,par,outfile):
"""print the priors and result for comparison"""
fit = self.results
p = self.prior
name = self.name
f = fit.p
ofile = open(outfile,'a')
ofile.write("{:5s} priors, {:d}+{:d} fit, {:d} to T-{:d} window "+strftime("%H:%M:%S")+"\n".format(name,self.nexp,self.nexp,self.child.tmin,self.child.tmax))
self.print_2pt_prior(par,ofile,self.child.name)
self.print_2pt_prior(par,ofile,self.parent.name)
ofile.write("V \n")
form = "{:<8s} "
self.V_prior_line(ofile,"Vnn prior",form,self.nexp,p['Vnn'])
self.V_prior_line(ofile,"Vnn fittd",form,self.nexp,f['Vnn'])
self.V_prior_line(ofile,"Vno prior",form,self.nexp,p['Vno'])
self.V_prior_line(ofile,"Vno fittd",form,self.nexp,f['Vno'])
if self.child.name != 'pimom0':
self.V_prior_line(ofile,"Von prior",form,self.nexp,p['Von'])
self.V_prior_line(ofile,"Von fittd",form,self.nexp,f['Von'])
self.V_prior_line(ofile,"Voo prior",form,self.nexp,p['Voo'])
self.V_prior_line(ofile,"Voo fittd",form,self.nexp,f['Voo'])
def print_2pt_prior(self,par,ofile,name):
fit = self.results
p = self.prior
f = fit.p
dE = exp(f['log('+name+':dE)'])
E = [sum(dE[:i+1]) for i in range(self.nexp)]
a = exp(f['log('+name+':a)'])
if self.name != 'pimom0':
dEo = exp(f['log('+name+':dEo)'])
Eo = [sum(dEo[:i+1]) for i in range(self.nexp)]
ao = exp(f['log('+name+':ao)'])
pdE = exp(p['log('+name+':dE)'])
pa = exp(p['log('+name+':a)'])
if self.name != 'pimom0':
pdEo = exp(p['log('+name+':dEo)'])
pao = exp(p['log('+name+':ao)'])
ofile.write("{:5s} \n".format(name))
form = "{:<12s} {:<12s} "
self.prior_line(ofile,"odd prior",form,self.nexp,pa,pdE)
self.prior_line(ofile,"odd fittd",form,self.nexp,a,dE)
if self.name != 'pimom0':
self.prior_line(ofile,"evn prior",form,self.nexp,pao,pdEo)
self.prior_line(ofile,"evn fittd",form,self.nexp,ao,dEo)
def prior_line(self,ofile,name,form,nexp,a,E):
ofile.write(name+": ")
ofile.write(form.format(a[0].fmt(),E[0].fmt()))
for state in range(1,self.nexp):
ofile.write("| "+form.format(a[state].fmt(),E[state].fmt()))
ofile.write(" \n")
def V_prior_line(self,ofile,name,form,nexp,V):
ofile.write(name+": ")
ofile.write(form.format(V[0][0].fmt()))
for i in range(1,self.nexp):
for j in range(1,self.nexp):
ofile.write("| "+form.format(V[i][j].fmt()))
ofile.write(" \n")
def chi2_cal(self):
"""calculate chi2 of fit"""
chi2 = 0.
points = 0
for fit in (self.child,self.parent):
t,g,dg,gth,dgth = self.fitter.collect_fitresults()["2pt"+fit.name]
for it in range(0,fit.tmax-fit.tmin):
chi2 = ( (g[it]-gth[it])/dg )**2
total2pt = sum(chi2)
points = points + len(g)
for T in self.Texts:
t,g,dg,gth,dgth = self.fitter.collect_fitresults()["3pt"+self.name+"T"+str(T)]
for it in range(0,T-fit.tmax-fit.tmin):
chi2 = ( (g[it]-gth[it])/dg )**2
total3pt = sum(chi2)
points = points + len(g)
parameters = 0
for key in self.prior.keys():
parameters = parameters + len(self.results.p[key])
self.dof = points-parameters
self.chi2 = total2pt+total3pt
return self.chi2,self.dof
def chi2_aug_part(self):
"""calculate the part of the chi2 coming from the priors"""
####### NOT WORKING ATM #####
pth = self.results.p
p = self.prior
aug = 0
print(pth.values())
qth = np.fromiter(pth.values(),np.float)
q = np.fromiter(p.values(),np.float)
print(qth)
print(q)
for exp in range(self.nexp):
aug = aug + ( (p[key][exp].mean-pth[key][exp].mean)/p[key][exp].sdev )**2
self.aug = aug
return aug