-
Notifications
You must be signed in to change notification settings - Fork 0
/
noisescript2.py
89 lines (77 loc) · 2.56 KB
/
noisescript2.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
#!/usr/bin/env python
import sys
import os
import math
import numarray.random_array as rnd
def main(argv=None):
if argv is None:
argv = sys.argv
if len(argv)!=6:
print "Usage: %s <method> <frequency> <vmin> <vmax> <bestfitdatafile>"%argv[0]
return 1
hybrid = "%s_hybrid"%argv[1]
simplex = "%s_simplex"%argv[1]
dfile = argv[5]
hybrid_out = "%s.hybrid_output"%dfile
simplex_out = "%s.simplex_output"%dfile
temp_in = "%s.temp_input"%dfile
frequency = argv[2]
vmin = argv[3]
vmax = argv[4]
fin = open(dfile,"r")
params = []
paramnames = []
v = []
tin = []
tfit = []
for l in fin:
tokens = l.split()
if tokens[0]=='#':
if tokens[1]!="Attained":
paramnames.append(tokens[1])
params.append(float(tokens[2]))
else:
v.append(float(tokens[0]))
tin.append(float(tokens[1]))
tfit.append(float(tokens[2]))
fin.close()
tres = [tfit[i]-tin[i] for i in xrange(len(tin))]
rms = reduce(lambda x,y: x+y*y, tres,0.0)
rms = math.sqrt(rms/len(tres))
prmstr = ' '.join([str(i) for i in params])
rnd.seed()
meanparams = [0.0]*len(params)
rmsparams = [0.0]*len(params)
niter = 100
for i in xrange(niter):
ret = 1
while(ret!=0):
rmsarr = rnd.normal(0.0,rms,len(tfit))
trand = [tfit[j]+rmsarr[j] for j in xrange(len(tfit))]
fout = open(temp_in,"w")
for j in range(len(v)):
print >>fout,"%f\t%g"%(v[j],trand[j])
fout.close()
ret=os.system("%s %s %s %s %s %s %s"%(simplex,temp_in,frequency,vmin,vmax,prmstr,simplex_out))
simplex_params = []
fin = open(simplex_out,"r")
for l in fin:
tokens = l.split()
if tokens[0]=='#':
if tokens[1]!="Attained":
simplex_params.append(float(tokens[2]))
fin.close()
for j in xrange(len(params)):
meanparams[j]+=simplex_params[j]
rmsparams[j]+=simplex_params[j]*simplex_params[j]
rmsparams=map(lambda x,y: math.sqrt((x-(y**2)/niter)/niter),rmsparams,meanparams)
meanparams=[x/niter for x in meanparams]
print "Residual RMS: ",rms
print "Parameter\tMean\t\tSigma\t\tBest"
for i in range(len(paramnames)):
print "%s\t\t%f\t%f\t%f"%(paramnames[i],meanparams[i],rmsparams[i],params[i])
os.remove(simplex_out)
os.remove(temp_in)
return 0
if __name__ == "__main__":
sys.exit(main())