def plotCls(self,saveFile,keys=None,xlimits=None,ylimits=None,transform=True,showBinnedTheory=False,scaleX='linear',scaleY='linear'): nsigma = 2. binCenters = self.binner.getBinCenters() if transform: ylab = "$\ell C_{\ell}$" mult = binCenters multTh = 1.#binCenters*0.+1. else: ylab = "$C_{\ell}$" mult = binCenters*0.+1. multTh = 0.#binCenters*0. pl = Plotter(labelX="$\ell$",labelY=ylab,scaleX=scaleX,scaleY=scaleY) if keys is None: keys = self.datas.keys() for key in keys: dat = self.datas[key] if dat['covmat'] is None: #This is a theory curve ells = np.array(range(len(dat['unbinned']))) if dat['isFit']: ls="--" lw=1 else: ls="-" lw=2 base_line, = pl.add(ells,(multTh*(ells-1)+1.)*dat['unbinned'],label=dat['label'],lw=lw,ls=ls) if dat['isFit']: pl._ax.fill_between(ells,(multTh*(ells-1)+1.)*dat['unbinned']*(1.-nsigma*dat['amp'][1]/dat['amp'][0]),(multTh*(ells-1)+1.)*dat['unbinned']*(1.+nsigma*dat['amp'][1]/dat['amp'][0]),alpha=0.3, facecolor=base_line.get_color()) if showBinnedTheory: pl.add(binCenters[:len(dat['binned'])],mult[:len(dat['binned'])]*dat['binned'], ls='none',marker='x',mew=2,markersize=10,label=dat['label']+' binned') else: errs = np.sqrt(np.diagonal(dat['covmat'])) print dat['label'] pl.addErr(binCenters[:len(dat['binned'])],mult[:len(dat['binned'])]*dat['binned'],mult[:len(dat['binned'])]*errs,label=dat['label'],marker='o',elinewidth=2,markersize=10,mew=2,) [i.set_linewidth(2.0) for i in pl._ax.spines.itervalues()] pl._ax.tick_params(which='major',width=2) pl._ax.tick_params(which='minor',width=2) pl._ax.axhline(y=0.,ls='--') if not(xlimits is None): pl._ax.set_xlim(*xlimits) else: pl._ax.set_xlim(self.binner.bin_edges[0],self.binner.bin_edges[-1]) if not(ylimits is None): pl._ax.set_ylim(*ylimits) pl.legendOn(loc='lower left',labsize=10) pl.done(saveFile)
def fit(self,keyData,keyTheory,amplitudeRange=np.arange(0.1,2.0,0.01),debug=False,numbins=-1): # evaluate likelihood on a 1d grid and fit to a gaussian # store fit as new theory curve width = amplitudeRange[1]-amplitudeRange[0] Likelihood = lambda x: np.exp(-0.5*self.chisq(keyData,keyTheory,amp=x,numbins=numbins)) Likes = np.array([Likelihood(x) for x in amplitudeRange]) Likes = Likes / (Likes.sum()*width) #normalize ampBest,ampErr = cfit(norm.pdf,amplitudeRange,Likes,p0=[1.0,0.5])[0] if debug: fitVals = np.array([norm.pdf(x,ampBest,ampErr) for x in amplitudeRange]) pl = Plotter() pl.add(amplitudeRange,Likes,label="likes") pl.add(amplitudeRange,fitVals,label="fit") pl.legendOn() pl.done("output/debug_coreFit.png") fitKey = keyData+"_fitTo_"+keyTheory self.datas[fitKey] = {} self.datas[fitKey]['covmat'] = None self.datas[fitKey]['binned'] = self.datas[keyTheory]['binned']*ampBest self.datas[fitKey]['unbinned'] = self.datas[keyTheory]['unbinned']*ampBest self.datas[fitKey]['label'] = keyData+" fit to "+keyTheory+" with amp "+'{0:.2f}'.format(ampBest)+"+-"+'{0:.2f}'.format(ampErr) self.datas[fitKey]['amp']=(ampBest,ampErr) self.datas[fitKey]['isFit'] = True return fitKey
fileList = glob.glob("output/"+derivRoot+"*.csv") farr = np.loadtxt("output/"+derivRoot[:-5]+"fCls.csv",delimiter=',') arrs = {} for fileN in fileList: lab = fileN[len("output/"+derivRoot):-4] arrs[lab] = np.loadtxt(fileN,delimiter=',') specList = ['TT','EE','BB','TE','KK','KT'] for spec in specList: if spec=='BB': continue pls = Plotter(scaleY='log',scaleX='log') ind = specList.index(spec) for lab in arrs: arr = arrs[lab] y = arr[:,ind]**2./farr[:,ind]**2. if lab=='tau': ls = '--' else: ls = "-" pls.add(range(arr.shape[0]),y,label=lab,ls=ls) pls.legendOn(loc='upper right',labsize=8) pls._ax.set_xlim(20.,4000.) pls.done("output/d"+spec+".png")
def fitAuto(self,keyData,keyTheory,amplitudeRange=np.arange(0.1,2.0,0.01),constRange=np.arange(0.1,2.0,0.01),debug=False,store=False): # evaluate likelihood on a 2d grid and fit to a gaussian # store fit as new theory curve width = amplitudeRange[1]-amplitudeRange[0] height = constRange[1]-constRange[0] Likelihood = lambda x,y: np.exp(-0.5*self.chisqAuto(keyData,keyTheory,amp=x,const=y)) #Likelihood = lambda x,y: -0.5*self.chisqAuto(keyData,keyTheory,amp=x,const=y) Likes = np.array([[Likelihood(x,y) for x in amplitudeRange] for y in constRange]) ampLike = np.sum(Likes,axis=0) constLike = np.sum(Likes,axis=1) ampLike = ampLike / (ampLike.sum()*width) #normalize constLike = constLike / (constLike.sum()*height) #normalize ampBest,ampErr = cfit(norm.pdf,amplitudeRange,ampLike,p0=[amplitudeRange.mean(),0.1*amplitudeRange.mean()])[0] constBest,constErr = cfit(norm.pdf,constRange,constLike,p0=[constRange.mean(),0.1*constRange.mean()])[0] if debug: pl = Plotter() pl.plot2d(Likes) pl.done("output/like2d.png") pl = Plotter() fitVals = np.array([norm.pdf(x,ampBest,ampErr) for x in amplitudeRange]) pl.add(amplitudeRange,ampLike,label="amplikes") pl.add(amplitudeRange,fitVals,label="fit") pl.legendOn() pl.done("output/amplike1d.png") pl = Plotter() fitVals = np.array([norm.pdf(x,constBest,constErr) for x in constRange]) pl.add(constRange,constLike,label="constlikes") pl.add(constRange,fitVals,label="fit") pl.legendOn() pl.done("output/constlike1d.png") #sys.exit() if not(store): return constBest,constErr else: self.datas[keyData]['binned'] -= constBest self.datas[keyData]['unbinned'] -= constBest fitKey = keyData+"_fitTo_"+keyTheory self.datas[fitKey] = {} self.datas[fitKey]['covmat'] = None self.datas[fitKey]['binned'] = self.datas[keyTheory]['binned']*ampBest self.datas[fitKey]['unbinned'] = self.datas[keyTheory]['unbinned']*ampBest self.datas[fitKey]['label'] = keyData+" fit to "+keyTheory+" with amp "+'{0:.2f}'.format(ampBest)+"+-"+'{0:.2f}'.format(ampErr) self.datas[fitKey]['amp']=(ampBest,ampErr) self.datas[fitKey]['const']=(constBest,constErr) self.datas[fitKey]['isFit'] = True return fitKey
zsource = 1.0 myInt.addDeltaNz('delta', zsource) for i, zwidth in enumerate(np.arange(0.01, 0.1, 0.01)): myInt.addStepNz('step' + str(i), zsource - zwidth / 2., zsource + zwidth / 2.) print("getting cls..") pl = Plotter(scaleY='log', scaleX='log') ellrange = list(range(2, ellmax, 1)) myInt.generateCls(ellrange) for i, tag in enumerate(sorted(myInt.kernels.keys())): if tag == "cmb": continue retcl = myInt.getCl("cmb", tag) if tag == "delta": compcl = retcl.copy() lw = 2 ls = "--" else: lw = 1 ls = "-" pl.add(ellrange, retcl, label=tag, ls=ls, lw=lw) rat = (retcl / compcl) ratio = (np.abs(rat[np.logical_and(ellrange > 100., ellrange < 2000.)] - 1.)).max() * 100. print((tag, ratio, " %")) pl.legendOn(loc='upper right', labsize=10) pl.done("output/estcls.png")
print xy,beam if files==[]: print "skipping" continue print files[0] noises = [] mss = [] for filen in files: beam,ms = np.loadtxt(filen,unpack=True) noise = float(filen[-7:-4]) noises.append(noise) mss.append(ms) print "last noise ", noises[-1] #print mss[-1] #pl.add(noises,mss,lw=3,label=xy+" "+str(beam) + " arcmin",ls='--',color=col) pl.add(noises,np.array(mss)*0.01,lw=3,label=str(beam) + " arcmin",ls='-',color=col1) pl._ax.set_xlim(1.,5.) #pl._ax.set_ylim(0.,10.) pl._ax.set_ylim(0.,4.*0.01) pl.legendOn() pl.done("beam.png")