-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackend_ana.py
More file actions
350 lines (259 loc) · 12.2 KB
/
backend_ana.py
File metadata and controls
350 lines (259 loc) · 12.2 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
#!/usr/bin/env python3
import sys
sys.path.append('./cpp/')
import cppana as bind
from util_stat import errorBootstrap, effMass
import json
from scipy.linalg import eigh
import numpy as np
# dev
import os
# GEVP routines:
#
# theDat -- array with indices (config, tSep, iOp, jOp)
#
# fixed t0, tstar; on the mean
def fixedGevp(theDat, tP):
# use only subset of operators
subDat = np.array(theDat)[np.ix_(range(theDat.shape[0]), range(theDat.shape[1]), tP['opIndList'], tP['opIndList'])]
eigVals, eigVecs = eigh(subDat[0,tP['tstar'],:,:], subDat[0,tP['t0'],:,:])
# reverse level order
#eigVecs = np.flip(eigVecs, 1)
eigVecs = np.fliplr(eigVecs)
rotDat = np.einsum('ctij,in,jm->ctnm', subDat, np.conj(eigVecs), eigVecs)
return {'eigVecs' : eigVecs, 'rotDat' : rotDat}
# fit routines:
#
# theDat -- array with indices (config, tSep) (must be real)
#
def bootSingleExpFit(iLev, theDat, tP, oP, tMin):
# try to get decent initial guesses
ratioCorr = theDat[0,tMin]/theDat[0,tMin+3]
initM = 1./3. * np.log(ratioCorr) if ratioCorr > 0. else 0.1
initA = theDat[0, tMin] * np.exp(initM*tMin)
initGuess = [initA, initM]
cppRet = np.array(bind.bootSingleExp(np.array(theDat[:,tMin:tP['tMax']+1]),tMin,tP['tMax'],initGuess))
chiSq = cppRet[0,0]
mVal = cppRet[0,2]
mErr = errorBootstrap(cppRet[:,2])
return {'chiSq': chiSq, 'mVal' : (mVal, mErr[0], mErr[1]), 'mSmpls' : cppRet[:,2], 'ovSmpls' : cppRet[:,1]}
def bootTwoExpFit(iLev, theDat, tP, oP, tMin):
# try to get decent initial guesses
ratioCorr = theDat[0,tMin]/theDat[0,tMin+3]
initM = 1./3. * np.log(ratioCorr) if ratioCorr > 0. else 0.1
initA = theDat[0, tMin] * np.exp(initM*tMin)
initGuess = [initA, initM, 0.1, 0.05]
cppRet = np.array(bind.bootTwoExp(np.array(theDat[:,tMin:tP['tMax']+1]),tMin,tP['tMax'],initGuess))
chiSq = cppRet[0,0]
mVal = cppRet[0,2]
mErr = errorBootstrap(cppRet[:,2])
return {'chiSq': chiSq, 'mVal' : (mVal, mErr[0], mErr[1]), 'mSmpls' : cppRet[:,2], 'ovSmpls' : cppRet[:,1]}
#def bootRatioFit(iLev, theDat, tP, oP, tMin):
# # compute ratio
# ratioDat = np.array(theDat)
# for aHad in tP['lvlPriors'][str(iLev)]:
# shDat = np.array(oP[aHad]['corr']).real if aHad[0]!='0' else 1.
# ratioDat = ratioDat / shDat
#
# # try to get decent initial guesses
# ratioCorr = theDat[0,tMin]/theDat[0,tMin+3]
# initM = 1./3. * np.log(ratioCorr) if ratioCorr > 0. else 0.
# initA = ratioDat[0, tMin] * np.exp(initM*tMin)
# initGuess = [initA, initM]
#
# cppRet = np.array(bind.bootSingleExp(np.array(ratioDat[:,tMin:tP['tMax']+1]),tMin,tP['tMax'],initGuess))
# chiSq = cppRet[0,0]
#
# # put energies back in
# mSmpls = cppRet[:,2]
#
# for aHad in tP['lvlPriors'][str(iLev)]:
# shM = np.array(oP[aHad]['mSmpls']) if aHad[0]!='0' else 0.
# mSmpls = mSmpls + shM
#
# mVal = mSmpls[0]
# mErr = errorBootstrap(mSmpls)
# return {'chiSq': chiSq, 'mVal' : (mVal, mErr[0], mErr[1]), 'mSmpls' : mSmpls, 'aSmpls' : cppRet[:,1]}
def bootRatioFit(iLev, theDat, tP, oP, tMin):
# compute ratio
ratioDat = np.array(theDat[:,tMin:tP['tMax']+1])
for aHad in tP['lvlPriors'][str(iLev)]:
shDat = np.array(oP[aHad]['corr']).real[:,tMin:tP['tMax']+1] if aHad[0]!='0' else 1.
ratioDat = ratioDat / shDat
# try to get decent initial guesses
ratioCorr = theDat[0,0]/theDat[0,3]
initM = 1./3. * np.log(ratioCorr) if ratioCorr > 0. else 0.
initA = ratioDat[0, 0] * np.exp(initM*tMin)
initGuess = [initA, initM]
cppRet = np.array(bind.bootSingleExp(np.array(ratioDat),tMin,tP['tMax'],initGuess))
chiSq = cppRet[0,0]
# put energies and overlaps back in
mSmpls = np.array(cppRet[:,2], copy=True)
ovSmpls = np.array(cppRet[:,1], copy=True)
for aHad in tP['lvlPriors'][str(iLev)]:
shM = np.array(oP[aHad]['mSmpls']) if aHad[0]!='0' else 0.
mSmpls = mSmpls + shM
shOv = np.array(oP[aHad]['ovSmpls']) if aHad[0]!='0' else 1.
ovSmpls = ovSmpls * shOv
mVal = mSmpls[0]
mErr = errorBootstrap(mSmpls)
return {'chiSq': chiSq, 'mVal' : (mVal, mErr[0], mErr[1]), 'mSmpls' : mSmpls, 'aSmpls' : cppRet[:,1], 'ovSmpls' : ovSmpls, 'delESmpls' : cppRet[:,2]}
def doTmin(iLev, theDat, aFitRoutine, tP, oP, tminLow, tminUp):
if aFitRoutine == bootTwoExpFit: tminUp = 11
retDat = np.zeros((tminUp-tminLow+1, 5))
for iT, tMin in enumerate(range(tminLow, tminUp+1)):
retDic = aFitRoutine(iLev, theDat, tP, oP, tMin)
# mVal, mErr[0], mErr[1], chiSq
retDat[iT,:] = np.array([tMin,]+[aX for aX in retDic['mVal']]+[retDic['chiSq']])
return retDat
def bootRatioCorrEn(iLev, theDat, parDic, oP, tMin):
if oP['mSmpls'][iLev] is None: return None, None
tList = range(theDat.shape[1])[tMin:]
reconstr = np.exp(-0.5*np.outer(oP['mSmpls'][iLev], tList))
return tList, theDat[:,tMin:]/(oP['rotDat'][iLev][:,tMin:]**0.5 * reconstr)
def bootRatioCorrOv(iLev, theDat, parDic, oP, tMin):
if oP['ovSmpls'][iLev] is None: return None, None
tList = range(theDat.shape[1])[tMin:]
return tList, theDat[:,tMin:] * np.outer(oP['ovSmpls'][iLev]**0.5, np.ones_like(tList))/oP['rotDat'][iLev][:,tMin:]
def bootRatioOvEn(iLev, theDat, parDic, oP, tMin):
if oP['mSmpls'][iLev] is None or oP['ovSmpls'][iLev] is None: return None, None
tList = range(theDat.shape[1])[tMin:]
#reconstr = np.exp(-0.5*np.outer(oP['mSmpls'][iLev], tList))
#return tList, theDat[:,tMin:]/(oP['ovSmpls'][iLev] * reconstr)
reconstr = np.einsum('c,ct->ct', oP['ovSmpls'][iLev]**0.5, np.exp(-1.0*np.outer(oP['mSmpls'][iLev], tList)))
return tList, theDat[:,tMin:]/reconstr
def bootRatio4(iLev, theDat, parDic, oP, tMin):
if oP['aSmpls'][iLev] is None: return None, None
if oP['delESmpls'][iLev] is None: return None, None
if oP['mSmpls'][iLev] is None: return None, None
if oP['ovSmpls'][iLev] is None: return None, None
if 'lvlPriors' not in parDic: return None, None
tList = range(theDat.shape[1])[tMin:]
# compute ratio with SH corrs
ratioDat = np.array(theDat[:,tMin:])
for aHad in parDic['lvlPriors'][str(iLev)]:
shDat = np.array(oP[aHad]['corr']).real[:,tMin:] if aHad[0]!='0' else 1.
ratioDat = ratioDat / shDat
# put fitted ratio back in
ratOv = np.outer(oP['aSmpls'][iLev], np.ones_like(tList))
ratExp = np.exp(-1.0*np.outer(oP['delESmpls'][iLev], tList))
ratCorr = ratOv * ratExp
extraOv = np.outer(oP['ovSmpls'][iLev], np.ones_like(tList))
ratioDat = ratioDat / ratCorr * extraOv**0.5
return tList, ratioDat
gevpRoutines = {'fixedGevp' : fixedGevp}
fitRoutines = {'singleExpFit' : bootSingleExpFit, 'ratioFit' : bootRatioFit}
currRoutines = {'corren' : bootRatioCorrEn, 'corrov' : bootRatioCorrOv, 'oven' : bootRatioOvEn, 'ratio4' : bootRatio4}
# given a correlator matrix, get tmin plot
def taskTMinPlot(theDat, parDic, oP):
# perform GEVP
# theDat needs to be in format as described for GEVP routines above
if 'gevpRout' in parDic:
rotDat = gevpRoutines[parDic['gevpRout']](theDat, parDic)['rotDat']
nLev = rotDat.shape[2]
lvlList = [rotDat[:,:,iLev,iLev] for iLev in range(nLev)]
else:
lvlList = theDat
retList = []
# perform fits
for iLev, aCorr in enumerate(lvlList):
retList.append({'tMinPlot' : doTmin(iLev, aCorr.real, fitRoutines[parDic['fitRout']], parDic, oP, tminLow = 4, tminUp = parDic['tMax'] - 12)})
return retList
def taskGetSamples(theDat, parDic, othP):
# perform GEVP
# theDat needs to be in format as described for GEVP routines above
if 'gevpRout' in parDic:
rotDat = gevpRoutines[parDic['gevpRout']](theDat, parDic)['rotDat']
nLev = rotDat.shape[2]
lvlList = [rotDat[:,:,iLev,iLev] for iLev in range(nLev)]
else:
lvlList = theDat
retList = []
# perform fits
for iLev, (aCorr, aTMin) in enumerate(zip(lvlList, othP['tMinList'])):
if aTMin == None:
retList.append(None)
else:
fitRes = fitRoutines[parDic['fitRout']](iLev, aCorr.real, parDic, othP, aTMin)
retDict = {'mSmpls' : fitRes['mSmpls'], 'ovSmpls' : fitRes['ovSmpls']}
# aSmpls is the amplitude returned from ratio fits, so might not be there
if 'aSmpls' in fitRes:
retDict['aSmpls'] = fitRes['aSmpls']
# delESmpls is the delta E returned from ratio fits, so might not be there
if 'delESmpls' in fitRes:
retDict['delESmpls'] = fitRes['delESmpls']
retList.append(retDict)
return retList
# given a correlator matrix and current correlators, construct ratios to obtain
# current matrix elements
#
# theDat -- array with indices (config, tSep, iOp)
def taskCurrentRatioPlot(theDat, parDic, oP):
# perform GEVP
# theDat needs to be in format as described for GEVP routines above
gevpRet = gevpRoutines[parDic['gevpRout']](oP['corrDat'], parDic)
rotDat = gevpRet['rotDat']
eigVecs = gevpRet['eigVecs']
subDat = np.array(theDat)[np.ix_(range(theDat.shape[0]), range(theDat.shape[1]), parDic['opIndList'])]
nLev = rotDat.shape[2]
# rotated correlators
oP['rotDat'] = [rotDat[:,:,iLev,iLev].real for iLev in range(nLev)]
# rotated current correlators
dLvlList = np.einsum('cti,in->ctn', np.conj(subDat), eigVecs).real
retList = []
# perform fits
for iLev in range(dLvlList.shape[2]):
#retList.append({'tPlot' : doTmin(iLev, aCorr.real, fitRoutines[parDic['fitRout']], parDic, oP, tminLow = 4, tminUp = parDic['tMax'] - 12)})
tList, ratSmpls = currRoutines[parDic['currRout']](iLev, dLvlList[:,:,iLev], parDic, oP, tMin=4)
if ratSmpls is None: retList.append({})
else:
ratVal = ratSmpls[0]
ratErr = errorBootstrap(ratSmpls)
retList.append({'tMinPlot' : np.stack([tList,ratVal,ratErr[0],ratErr[1]], axis=-1)})
return retList
def taskCurrentGetSamples(theDat, parDic, oP):
# perform GEVP
# theDat needs to be in format as described for GEVP routines above
gevpRet = gevpRoutines[parDic['gevpRout']](oP['corrDat'], parDic)
rotDat = gevpRet['rotDat']
eigVecs = gevpRet['eigVecs']
subDat = np.array(theDat)[np.ix_(range(theDat.shape[0]), range(theDat.shape[1]), parDic['opIndList'])]
nLev = rotDat.shape[2]
# rotated correlators
oP['rotDat'] = [rotDat[:,:,iLev,iLev].real for iLev in range(nLev)]
# rotated current correlators
dLvlList = np.einsum('cti,in->ctn', np.conj(subDat), eigVecs).real
retList = []
# perform fits
for iLev, aTMin in enumerate(oP['tMinList']):
if aTMin is None: retList.append(None)
else:
tList, ratSmpls = currRoutines[parDic['currRout']](iLev, dLvlList[:,:,iLev], parDic, oP, tMin=aTMin[0])
if ratSmpls is None:
retList.append(None)
else:
# perform weighted average here
weightArr = 1./np.mean(np.array(errorBootstrap(ratSmpls)), axis=0)**2
nAvg = aTMin[1]-aTMin[0]+1
retList.append({
'ovSmpls' : np.average(ratSmpls[:,:nAvg], weights=weightArr[:nAvg], axis=1)
})
return retList
# wait for new task; a task is a tuple (chanKey, theDat, parDict) where
#
# task -- string specifying the task to be performed
# chanKey -- will be returned to queue as identifier
# parDict -- analysis dictionary
# theDat -- correlation matrix of the form of the GEVP routine input
#
def worker_main(inQueue, outQueue):
taskList = {
'tMinPlot' : taskTMinPlot,
'getSamples' : taskGetSamples,
'currentRatioPlot' : taskCurrentRatioPlot,
'currentGetSamples' : taskCurrentGetSamples,
}
while True:
task, chanKey, parDic, theDat, otherPars = inQueue.get(True)
retList = taskList[task](theDat, parDic, otherPars)
outQueue.put((task, chanKey, parDic, retList))