Commit cb45ec5f authored by Xavier Garrido's avatar Xavier Garrido
Browse files

make camel.py script python 3 compatible (without breaking python 2.7 compatibility)

parent 211e11a6
......@@ -73,7 +73,7 @@ def mergeMC( filename, burnin=0.9, num=None, ext="txt", par=None, nelts=None):
if nelts < len(chain):
chain = chain[np.array(np.random.random(nelts)*len(chain),int),:]
dchain = dict( zip(par,np.transpose(chain)))
dchain = dict( list(zip(par,np.transpose(chain))))
return(dchain)
......@@ -123,7 +123,7 @@ def scan(filename):
while not l[index].startswith("SCANNING PAR"):
index = index+1
print(l[index].replace("\n",""))
print((l[index].replace("\n","")))
while not l[index].startswith("it "):
index = index+1
......@@ -163,7 +163,7 @@ def extract_chains( chainame, nchain):
chain = []
for c in range(0,nchain):
print( "%s%d.txt" % (chainame,c+1))
print(( "%s%d.txt" % (chainame,c+1)))
data = []
with open("%s%d.txt" % (chainame,c+1)) as f:
......@@ -222,10 +222,10 @@ def GelmanRubin( chains, gap=10000, length_min=1000, length_max=None, new=False)
print( "Not enough samples...")
exit()
it = range(length_min,length_max,gap)[1:-1]
it = list(range(length_min,length_max,gap))[1:-1]
R = []
for isamp in it:
print( "%d%%" % int(it.index(isamp)*100./len(it)))
print(( "%d%%" % int(it.index(isamp)*100./len(it))))
if new:
#Gelman-Rubin stat on the last "gap" samples for each iteration
n = gap
......@@ -261,10 +261,10 @@ def Geweke( chains, gap=10000, length_max=None):
print( "Not enough samples...")
exit()
it = range(length_min,length_max,gap)[1:]
it = list(range(length_min,length_max,gap))[1:]
T = []
for isamp in it:
print( "%d%%" % int(it.index(isamp)*100./len(it)))
print(( "%d%%" % int(it.index(isamp)*100./len(it))))
n = length_max-isamp
tmp1 = [data[:,isamp:isamp+n*0.1] for data in chains]
tmp2 = [data[:,isamp+n*0.5:length_max] for data in chains]
......@@ -401,7 +401,7 @@ to_cosmomcName = {
'Aps143x217' : 'Aps143x217',
}
from_cosmomcName = dict(zip(to_cosmomcName.values(),to_cosmomcName.keys()))
from_cosmomcName = dict(list(zip(list(to_cosmomcName.values()),list(to_cosmomcName.keys()))))
......@@ -441,7 +441,7 @@ def posterior1d( chains, params, nbin=50, smooth=1,
for c in range(nchain):
chain = chains[c]
if chain.has_key(par):
if par in chain:
hist1d( chain[par], smooth=smooth, color=colors[c], linestyle=linestyles[c], lw=lw[c], bins=nbin)
ax.tick_params(axis='both', which='major', labelsize=16)
......@@ -568,7 +568,7 @@ def hist2d( chain, par1, par2, *args, **kwargs):
def triangle( chains, params, nbin=50, parnames=None, names=[], colors=[], linestyles=[], smooth=1., fontsize=12, rotation=0,
def triangle( chains, params, nbin=50, parnames=None, names=[], colors=[], linestyles=[], smooth=1., fontsize=12,
contour=True, fill=False, cmaps=[], bestfit=None, Norm=True, weights=None):
import matplotlib.ticker as mtick
......@@ -678,7 +678,6 @@ def triangle( chains, params, nbin=50, parnames=None, names=[], colors=[], lines
ax=plt.subplot( npar, npar, (npar-1)*npar+xpar+1)
ax.set_xlabel(parname.get(parnames[xpar],parnames[xpar]))
ax.tick_params(axis='both', which='major', labelsize=fontsize)
plt.xticks(rotation=rotation)
# plt.locator_params(tight=True, nbins=5)
#write legend
......@@ -810,8 +809,8 @@ def plot_correlation( matC, par, **kwargs):
covmat = np.tril( matC, k=-1)
covmat[covmat==0] = np.NaN
plt.imshow( covmat, vmin=vmin, vmax=vmax, **kwargs)
plt.yticks( range(len(covmat)), [parname.get(p,p) for p in par])
plt.xticks( range(len(covmat)), '')
plt.yticks( list(range(len(covmat))), [parname.get(p,p) for p in par])
plt.xticks( list(range(len(covmat))), '')
plt.colorbar()
for i in range( len(par[:-1])):
# text( i, i+0.25, parname.get(par[i+1],par[i+1]),rotation=-90)
......@@ -880,7 +879,7 @@ def read_bestfit( dirname):
newline = f.readline()
if not "nan" in newline and not "inf" in newline and not "Abort" in newline:
value = np.array( newline.split(), dtype=np.double)
bestfit = dict(zip(param, value))
bestfit = dict(list(zip(param, value)))
else :
bestfit = {'chi2':np.inf}
......@@ -934,7 +933,7 @@ def extract_hess( filename, ext="LASymMatrix", reverse=False):
npar = npar + 1
par.append( l.split("\t")[1])
print( "number of parameter : %d/%d" % (npar,len(par)))
print(( "number of parameter : %d/%d" % (npar,len(par))))
if reverse:
lines.reverse()
......@@ -990,11 +989,11 @@ simple_lik_name = {
def writeExtendedBestFit( dbdir, bfwrite=False, outpath=""):
#Extract Bestfit from logfile
path, dirs, files = os.walk("%s" % (dbdir)).next()
path, dirs, files = next(os.walk("%s" % (dbdir)))
for idir in dirs:
p,d,f = os.walk("%s/%s" % (dbdir,idir)).next()
p,d,f = next(os.walk("%s/%s" % (dbdir,idir)))
# this will find the last 'camelZZ.par' file in folder
camelparfile=""
......@@ -1021,7 +1020,7 @@ def writeExtendedBestFit( dbdir, bfwrite=False, outpath=""):
except ValueError:
return()
print CheckProfile
print(CheckProfile)
#build a line with all param names avoiding double counts
try:
parlist = Parnames( camelparfile)
......@@ -1129,7 +1128,7 @@ def writeExtendedBestFit( dbdir, bfwrite=False, outpath=""):
if bfwrite :
bestfitfile = "%s/%s/best_fit%d" % (dbdir,idir,count)
if outpath != "" :
print "outpath gene -> %s/" %(outpath)
print("outpath gene -> %s/" %(outpath))
#print os.path.isdir( "%s/" %(outpath))
if os.path.isdir( "%s/" %(outpath))==False :
......@@ -1137,7 +1136,7 @@ def writeExtendedBestFit( dbdir, bfwrite=False, outpath=""):
#return();
os.mkdir( "%s" %(outpath) )
print "outpath -> %s/%s/" %(outpath,idir)
print("outpath -> %s/%s/" %(outpath,idir))
#print os.path.isdir( "%s/%s/" %(outpath,idir))
if os.path.isdir( "%s/%s/" %(outpath,idir))==False :
......@@ -1161,11 +1160,11 @@ def writeExtendedBestFit( dbdir, bfwrite=False, outpath=""):
def writeDerivedBestFit( dbdir, bfwrite=False, outpath=""):
#Extract Bestfit from logfile
path, dirs, files = os.walk("%s" % (dbdir)).next()
path, dirs, files = next(os.walk("%s" % (dbdir)))
for idir in dirs:
p,d,f = os.walk("%s/%s" % (dbdir,idir)).next()
p,d,f = next(os.walk("%s/%s" % (dbdir,idir)))
# this will find the last 'camelZZ.par' file in folder
camelparfile=""
......@@ -1261,7 +1260,7 @@ def writeDerivedBestFit( dbdir, bfwrite=False, outpath=""):
else :
sf = open( name, 'r')
print name
print(name)
if '.gz' in name :
count = int(name.split(".")[-2])
......@@ -1308,12 +1307,12 @@ def writeDerivedBestFit( dbdir, bfwrite=False, outpath=""):
values += (chval.replace("chi2=","")).replace("\n","")
values += " "
if not gderived:
print "before os.system"
print("before os.system")
os.system("$CAMELROOT/Linux-x86_64/writeSpectra %s 1000 toto.fits > tmp.out" %(camelparfile) )
addin = open("tmp.out","r")
adl = addin.readlines()
print "after run externe"
print("after run externe")
gderived = True
# gets Profile line
......@@ -1338,14 +1337,14 @@ def writeDerivedBestFit( dbdir, bfwrite=False, outpath=""):
vder += " "
addin.close()
print "after get derived %s" %(vder)
print("after get derived %s" %(vder))
if "Profile" in c and not "bestfit" in c and not "nan" in c and not "inf" in c and not "Abort" in c:
values = c
goodline = True
values = values.replace("\n","")
print "values ok"
print("values ok")
sf.close()
# gets extensions
......@@ -1363,17 +1362,17 @@ def writeDerivedBestFit( dbdir, bfwrite=False, outpath=""):
bestfitfile = "%s/%s/best_fit%d" %(outpath,idir,count)
print bestfitfile
print goodline
print(bestfitfile)
print(goodline)
# print(values,bestfitfile)
# if (not os.path.isfile(bestfitfile)) :
if goodline:
newf=open( bestfitfile,'w')
newf.write(param_line)
values= values + vder
print vder
print values
print bestfitfile
print(vder)
print(values)
print(bestfitfile)
newf.write(values.replace("Profile",""))
newf.close()
......@@ -1418,7 +1417,7 @@ class prof:
self._offset = offset
self._CL = 68
path, dirs, files = os.walk("%s" % (self.dbdir)).next()
path, dirs, files = next(os.walk("%s" % (self.dbdir)))
if sum([self.parname+"_" in d for d in dirs])==0:
raise ValueError( "Wrong directory")
......@@ -1428,7 +1427,7 @@ class prof:
if not self.parname in idir:
continue
p,d,f = os.walk("%s/%s" % (self.dbdir,idir)).next()
p,d,f = next(os.walk("%s/%s" % (self.dbdir,idir)))
if writeBestfit:
self._writeBestFit(idir,f)
......@@ -1474,7 +1473,7 @@ class prof:
for val in self._s:
data = self._point_minimum( val)
# print( " ".join([str(data[p]) for p in params]))
print params
print(params)
f.write( " ".join([str(data[p]) for p in params])+"\n")
f.close()
......@@ -1527,7 +1526,7 @@ class prof:
Get all fit chi2 at a given value from multi-Minimize runs
"""
import os
path, dirs, files = os.walk("%s/%s_%s" % (self.dbdir,self.parname,val)).next()
path, dirs, files = next(os.walk("%s/%s_%s" % (self.dbdir,self.parname,val)))
files = [f for f in files if f.startswith("best")]
nmin = len(files)
......@@ -1546,7 +1545,7 @@ class prof:
Get minimum at a given value from multi-Minimize runs
"""
import os
path, dirs, files = os.walk("%s/%s_%s" % (self.dbdir,self.parname,val)).next()
path, dirs, files = next(os.walk("%s/%s_%s" % (self.dbdir,self.parname,val)))
files = [f for f in files if f.startswith("best")]
nmin = len(files)
......@@ -1555,13 +1554,13 @@ class prof:
#read all parameters
fname= "%s/%s_%s/%s" % (self.dbdir,self.parname,val,f)
if verbose:
print fname
print os.stat(fname).st_size
print(fname)
print(os.stat(fname).st_size)
if os.stat(fname).st_size !=0:
bftmp = read_bestfit( "%s/%s_%s/%s" % (self.dbdir,self.parname,val,f))
bftmp = read_bestfit("%s/%s_%s/%s" % (self.dbdir, self.parname, val, f))
if verbose:
prtval = 'chi2' if verbose == True else verbose
print( f, bftmp[prtval])
print(( f, bftmp[prtval]))
if bftmp['chi2'] < bf['chi2']:
bf = bftmp
......@@ -1571,7 +1570,7 @@ class prof:
def _params( self, val):
#Returns list of parameters from the fit file for a given value of the parameter
import os
path, dirs, files = os.walk("%s/%s_%s" % (self.dbdir,self.parname,val)).next()
path, dirs, files = next(os.walk("%s/%s_%s" % (self.dbdir,self.parname,val)))
files = [f for f in files if f.startswith("best")]
#read all parameters
......@@ -1661,12 +1660,12 @@ class prof:
xlow,xhigh = self._deltaChi2( CL=68)
self._res = FeldmanCousins( xmin, xhigh-xmin, self._CL)
self._str_res="%s < %f (%d%% C.L.)" % (parname.get(self._nameforclass,self._nameforclass),self._res, self._CL)
print(self._str_res)
print((self._str_res))
else:
xlow,xhigh = self._deltaChi2( CL=self._CL)
self._res = (xmin,(xlow,xhigh))
self._str_res="$%f_{%+f}^{%+f}$" % (xmin,xlow-xmin,xhigh-xmin)
print(self._str_res)
print((self._str_res))
self._is_fitted = True
return( self._res)
......@@ -1685,7 +1684,7 @@ class prof:
"""
Return the result from the fit
"""
print( self._str_res)
print(( self._str_res))
return(self._res)
......@@ -1709,7 +1708,7 @@ class prof:
if CL:
CL2delta = {68:1,95:3.84,99:6.63}
if CL in CL2delta.keys():
if CL in list(CL2delta.keys()):
delta = CL2delta[CL]
else:
raise ValueError('CL not in the list')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment