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