-
Notifications
You must be signed in to change notification settings - Fork 0
/
alt_zfactor.py
executable file
·159 lines (136 loc) · 7.22 KB
/
alt_zfactor.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
#!/usr/bin/env python
import numpy as np
import logging
import argparse
import plot_files
import pandas as pd
import zfactor
def build_cor_mat_error(corwild, ops, to):
N = len(ops)
cormat = np.matrix(np.zeros((N, N)), dtype=np.complex128)
errmat = np.matrix(np.zeros((N, N)), dtype=np.complex128)
for col, src in enumerate(ops):
for row, snk in enumerate(ops):
logging.debug("Reading snk:{}, src:{}".format(snk, src))
raw_c = plot_files.read_file(corwild.format(snk, src))
df = raw_c
Cij = df.ix[df['time'] == to, 'correlator']
cormat[row, col] = np.array(Cij)[0]
Eij = df.ix[df['time'] == to, 'error']
errmat[row, col] = np.array(Eij)[0]
return cormat, errmat
def read_zrots(filename):
txt = plot_files.lines_without_comments(filename)
df = pd.read_csv(txt, delimiter=',', names=["level", "amp", "error"], index_col=0)
return df
def read_level_fits(filename):
"""
Read in fit values to the levels. Fits should be single exp
Should be in the format
# Level, Amp, Error(Amp), Mass, Error(Mass)
0, 1.74082463996023, 0.0154835440133309, 0.153284504901198, 0.000705009866455281
final column chi^2 optional
"""
txt = plot_files.lines_without_comments(filename)
df = pd.read_csv(txt, delimiter=',', names=["level", "amp", "amp_error", "mass", "mass_error", "chisqr"], index_col=0)
return df
def alt_zfactor(corwild, zrotfile, rotfile, ops, t0, outputstub,
maxlevels=None, normalize=False, reconstruct_stub=None, inputemass=None):
# zrots = read_zrots(zrotfile)
fit_values = read_level_fits(zrotfile)
N = len(fit_values)
N = len(ops)
levels_to_make = range(min(N, maxlevels, len(fit_values)))
raw_v = zfactor.read_coeffs_file(rotfile)
v = np.matrix(raw_v.identities.values.reshape((N, N))).T
roterror = np.matrix(raw_v.error.values.reshape((N, N))).T
cormat, errmat = build_cor_mat_error(corwild, ops, t0)
Zs = {}
err = {}
ABS = np.abs
def nothing(a):
return a
if args.complex:
ABS = nothing
for level in levels_to_make:
zr = fit_values.amp[level]
#err[level] = zrots.error[level]*np.ones(N)
for op in range(N):
v_n = (v[:, level])
ev_n = np.ravel(roterror[:, level])
Zs[level] = [ABS((cormat[j]*(v_n)).flat[0])*np.sqrt(zr) for j in range(len(ops))]
err[level] = [ABS((cormat[j]*(v_n)).flat[0])*np.sqrt(fit_values.amp_error[level]) +
ABS((errmat[j]*(v_n)).flat[0])*np.sqrt(zr)
for j in range(len(ops))]
#err[level] = np.sqrt(fit_values.amp_error[level])*ABS(v_n)+np.sqrt(zr)*ABS(ev_n)
# normalized_Zs = zfactor.normalize_Zs(Zs, normalize)
A = np.array(Zs.values())
if normalize:
maximums = np.array([max(np.abs(A[:, i])) for i in range(len(Zs[0]))])
normalized_Zs = {k: np.abs(values)/maximums for k, values in Zs.iteritems()}
normalized_err = {k: np.abs(values)/maximums for k, values in err.iteritems()}
else:
normalized_Zs = Zs
normalized_err = err
# print err
# print normalized_err
if(outputstub):
logging.info("Writing alt_zfactors to {}".format(outputstub+".out"))
with open(outputstub+".out", 'w') as outfile:
outfile.write("# normalized Zfactors\n")
for level in levels_to_make:
for j in range(N):
if args.complex:
outfile.write("{:d}{:03d} ({},{}) ({},{})\n".format(j+1, level+1,
np.real(normalized_Zs[level][j]), np.imag(normalized_Zs[level][j]),
np.real(normalized_err[level][j]), np.imag(normalized_err[level][j])))
else:
outfile.write("{:d}{:03d} {} {}\n".format(j+1, level+1,
normalized_Zs[level][j], normalized_err[level][j]))
if(reconstruct_stub):
reconstructed_correaltors(Zs, err, fit_values, ops, reconstruct_stub)
def reconstructed_correaltors(Zs, error, fit_values, ops, stub):
emasses = fit_values.mass
emasses_err = fit_values.mass_error
for i in range(len(Zs[0])):
for j in range(len(Zs[0])):
with open("{}.{}.{}.cor".format(stub, ops[i], ops[j]), "w") as outfile:
for t in range(40):
C = sum((Zs[level][i]*np.conj(Zs[level][j]))*np.exp(-1.0*emasses[level]*t) for level in Zs.keys())
Cerr = sum((error[level][i]*np.conj(Zs[level][j]))*np.exp(-1.0 * emasses[level] * t)+
(Zs[level][i]*np.conj(error[level][j]))*np.exp(-1.0 * emasses[level] * t)+
(Zs[level][i]*np.conj(Zs[level][j]))*np.exp(-1.0*emasses[level]*t)*(-1.0*emasses_err[level]*t)
for level in Zs.keys())
outfile.write("{} ({},{}) ({},{})\n".format(t, np.real(C), np.imag(C), np.real(Cerr), np.imag(Cerr)))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Compute the Zfactors from the correlator and diagonalized coeffs")
parser.add_argument("-zr", "--z-rot", type=str, required=True,
help="file to read the zrot from")
parser.add_argument("-ic", "--inputcorrelatorformat", type=str, required=True,
help="Correlator file to read from")
parser.add_argument("-ops", "--operators", type=str, nargs="+", required=True,
help="operator strings, order matters!")
parser.add_argument("-ir", "--inputrotationcoeffs", type=str, required=True,
help="rotationcoeffs file to read from")
parser.add_argument("-t0", "--tnaught", type=int, required=True,
help="t naught, reference time")
parser.add_argument("-o", "--output_stub", type=str, required=False,
help="stub of name to write output to")
parser.add_argument("-r", "--reconstruct_stub", type=str, required=False,
help="stub for reconstrcuting the correlators")
parser.add_argument("-n", "--number", type=int, required=False,
help="restrict to a number of levels", default=1000)
parser.add_argument("-norm", "--normalize", action="store_true", required=False,
help="normalized the zfactors")
parser.add_argument("-c", "--complex", action="store_true", required=False,
help="output in complex format")
parser.add_argument("-v", "--verbose", action="store_true",
help="increase output verbosity")
args = parser.parse_args()
if args.verbose:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
logging.debug("Verbose debuging mode activated")
else:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
alt_zfactor(args.inputcorrelatorformat, args.z_rot, args.inputrotationcoeffs, args.operators,
args.tnaught, args.output_stub, args.number, args.normalize, args.reconstruct_stub)