def plot_T(self, k, T, Tc=None, Tb=None): from biggles import FramedPlot, Curve, PlotKey plt=FramedPlot() c=Curve(k, T**2) c.label = '$T^2$' plt.add(c) plist = [c] if Tc is not None: cc=Curve(k,Tc**2,color='blue') cc.label = '$Tc^2$' plt.add(cc) plist.append(cc) if Tb is not None: tmp = where(Tb < 1.e-5, 1.e-5, Tb) cb=Curve(k,tmp**2,color='red') cb.label = '$Tb^2$' plt.add(cb) plist.append(cb) plt.xlog=True plt.ylog=True plt.ylabel = '$T^2' plt.xlabel = 'k' plt.yrange = [1.e-8,1.0] plt.aspect_ratio=1 if Tc is not None or Tb is not None: key=PlotKey(0.1,0.9,plist) plt.add(key) plt.show()
def test_fit_nfw_dsig(rmin=0.01): from biggles import FramedPlot,Points,SymmetricErrorBarsY,Curve omega_m=0.25 z=0.25 n = lensing.nfw.NFW(omega_m, z) r200 = 1.0 c = 5.0 rmax = 5.0 log_rmin = log10(rmin) log_rmax = log10(rmax) npts = 25 logr = numpy.linspace(log_rmin,log_rmax,npts) r = 10.0**logr ds = n.dsig(r, r200, c) # 10% errors dserr = 0.1*ds ds += dserr*numpy.random.standard_normal(ds.size) guess = numpy.array([r200,c],dtype='f8') # add 10% error to the guess guess += 0.1*guess*numpy.random.standard_normal(guess.size) res = fit_nfw_dsig(omega_m, z, r, ds, dserr, guess) r200_fit = res['r200'] r200_err = res['r200_err'] c_fit = res['c'] c_err = res['c_err'] print 'Truth:' print ' r200: %f' % r200 print ' c: %f' % c print 'r200_fit: %f +/- %f' % (r200_fit,r200_err) print ' c_fit: %f +/- %f' % (c_fit,c_err) print 'Cov:' print res['cov'] logr = numpy.linspace(log_rmin,log_rmax,1000) rlots = 10.0**logr yfit = n.dsig(rlots,r200_fit,c_fit) plt=FramedPlot() plt.add(Points(r,ds,type='filled circle')) plt.add(SymmetricErrorBarsY(r,ds,dserr)) plt.add(Curve(rlots,yfit,color='blue')) plt.xlabel = r'$r$ [$h^{-1}$ Mpc]' plt.ylabel = r'$\Delta\Sigma ~ [M_{sun} pc^{-2}]$' plt.xrange = [0.5*rmin, 1.5*rmax] plt.yrange = [0.5*(ds-dserr).min(), 1.5*(ds+dserr).max()] plt.xlog=True plt.ylog=True plt.show()
def plot_nfwfits_byrun(run, name, prompt=False): conf = lensing.files.read_config(run) d = lensing.sample_read(type='fit', sample=run, name=name) omega_m = conf['omega_m'] rvals = numpy.linspace(d['r'].min(), d['r'].max(),1000) for i in xrange(d.size): plt = FramedPlot() lensing.plotting.add_to_log_plot(plt, d['r'][i], d['dsig'][i], d['dsigerr'][i]) z = d['z_mean'][i] n = lensing.nfw.NFW(omega_m, z) yfit = n.dsig(rvals, d['r200_fit'][i],d['c_fit'][i]) plt.add(Curve(rvals,yfit,color='blue')) plt.xlog=True plt.ylog=True plt.xlabel = r'$r$ [$h^{-1}$ Mpc]' plt.ylabel = r'$\Delta\Sigma ~ [M_{sun} pc^{-2}]$' if prompt: plt.show() raw_input('hit a key: ') else: epsfile='/home/esheldon/tmp/plots/desmocks-nfwfit-%02i.eps' % i print 'Writing epsfile:',epsfile plt.write_eps(epsfile)
def plot_sub_pixel(ellip,theta, show=False): import biggles from biggles import PlotLabel,FramedPlot,Table,Curve,PlotKey,Points from pcolors import rainbow f=subpixel_file(ellip,theta,'fits') data = eu.io.read(f) colors = rainbow(data.size,'hex') pltSigma = FramedPlot() pltSigma.ylog=1 pltSigma.xlog=1 curves=[] for j in xrange(data.size): sigest2 = (data['Irr'][j,:] + data['Icc'][j,:])/2 pdiff = sigest2/data['sigma'][j]**2 -1 nsub=numpy.array(data['nsub'][j,:]) #pc = biggles.Curve(nsub, pdiff, color=colors[j]) pp = Points(data['nsub'][j,:], pdiff, type='filled circle',color=colors[j]) pp.label = r'$\sigma: %0.2f$' % data['sigma'][j] curves.append(pp) pltSigma.add(pp) #pltSigma.add(pc) #pltSigma.yrange=[0.8,1.8] #pltSigma.add(pp) c5 = Curve(linspace(1,8, 20), .005+zeros(20)) pltSigma.add(c5) key=PlotKey(0.95,0.95,curves,halign='right',fontsize=1.7) key.key_vsep=1 pltSigma.add(key) pltSigma.xlabel='N_{sub}' pltSigma.ylabel=r'$\sigma_{est}^2 /\sigma_{True}^2 - 1$' lab=PlotLabel(0.05,0.07,r'$\epsilon: %0.2f \theta: %0.2f$' % (ellip,theta),halign='left') pltSigma.add(lab) pltSigma.yrange = [1.e-5,0.1] pltSigma.xrange = [0.8,20] if show: pltSigma.show() epsfile=subpixel_file(ellip,theta,'eps') print("Writing eps file:",epsfile) pltSigma.write_eps(epsfile)
def plot_pk(self,k,pk): from biggles import FramedPlot, Curve plt=FramedPlot() plt.add(Curve(k,pk)) plt.xlog=True plt.ylog=True plt.xlabel = r'$k [h/Mpc]$' plt.ylabel = r'$P_{lin}(k)$' plt.aspect_ratio = 1 plt.show()
def plot_m(self, r200, c): from biggles import FramedPlot, Curve n=1000 r = numpy.linspace(0.01, 20.0,n) m = self.m(r, r200, c) plt=FramedPlot() plt.add( Curve(r,m) ) plt.xlog=True plt.ylog=True plt.show()
def plot_dsig_one(r, dsig, dsigerr, **kw): """ plot delta sigma useful if adding to an existing plot parameters ---------- r: array radius dsig: array delta sigma dsigerr: array error on delta sigma """ from biggles import FramedPlot nbin=1 _set_biggles_defs(nbin) visible=kw.get('visible',True) xlog=kw.get('xlog',True) ylog=kw.get('ylog',True) aspect_ratio=kw.get('aspect_ratio',1) is_ortho=kw.get('is_ortho',False) plt=kw.get('plt',None) if plt is None: plt=FramedPlot() plt.aspect_ratio=aspect_ratio plt.xlog=xlog plt.ylog=ylog plt.xlabel = LABELS['rproj'] if is_ortho: plt.ylabel = LABELS['osig'] else: plt.ylabel = LABELS['dsig'] xrng, yrng = _add_dsig_to_plot(plt, r, dsig, dsigerr, **kw) plt.xrange=xrng plt.yrange=yrng if visible: plt.show() return plt
def plot_xi(self, r, xi): from biggles import FramedPlot, Curve minval = 1.e-4 xi = where(xi < minval, minval, xi) plt=FramedPlot() plt.add(Curve(r,xi)) plt.xlog=True plt.ylog=True plt.xlabel = r'$r [Mpc/h]$' plt.ylabel = r'$\xi_{lin}(r)$' plt.aspect_ratio=1 plt.show()
def test_xi_converge_nplk(epsfile=None): """ Test how xi converges with the number of k points per log10(k) Note we should test other convergence factors too! """ tab=Table(2,1) pltbig = FramedPlot() pltzoom = FramedPlot() pltbig.xlabel='r' pltbig.ylabel='xi(r)' pltbig.xlog=True pltbig.ylog=True pltzoom.xlabel='r' pltzoom.ylabel='xi(r)' l=Linear() r=10.0**linspace(0.0,2.3,1000) nplk_vals = [20,60,100,140,160] color_vals = ['blue','skyblue','green','orange','magenta','red'] plist=[] lw=2.4 for nplk,color in zip(nplk_vals,color_vals): print 'nplk:',nplk xi = l.xi(r, nplk=nplk) limxi = where(xi < 1.e-5, 1.e-5, xi) climxi = Curve(r,limxi,color=color,linewidth=lw) climxi.label = 'nplk: %i' % nplk pltbig.add(climxi) plist.append(climxi) w=where1(r > 50.0) cxi = Curve(r[w], xi[w], color=color, linewidth=lw) pltzoom.add(cxi) key = PlotKey(0.7,0.8, plist) pltzoom.add(key) tab[0,0] = pltbig tab[1,0] = pltzoom if epsfile is not None: tab.write_eps(epsfile) else: tab.show()
def plot_drho(comb=None, r=None, drho=None, drhoerr=None, color='black',type='filled circle', nolabel=False, noshow=False, minval=1.e-5, aspect_ratio=1): """ This one stands alone. """ if comb is not None: r=comb['rdrho'] drho=comb['drho'] drhoerr=comb['drhoerr'] else: if r is None or drho is None or drhoerr is None: raise ValueError("Send a combined struct or r,drho,drhoerr") plt=FramedPlot() plt.aspect_ratio=aspect_ratio plt.xlog=True plt.ylog=True if not nolabel: plt.xlabel = r'$r$ [$h^{-1}$ Mpc]' plt.ylabel = r'$\delta\rho ~ [M_{\odot} pc^{-3}]$' od=add_to_log_plot(plt, r, drho, drhoerr, color=color, type=type, minval=minval) # for drho we need even broader yrange plt.xrange = od['xrange'] yr=od['yrange'] plt.yrange = [0.5*yr[0], 3*yr[1]] if not noshow: plt.show() od['plt'] = plt return od
def plot_mass(comb=None, r=None, mass=None, masserr=None, color='black',type='filled circle', nolabel=False, noshow=False, minval=1.e11, aspect_ratio=1): if comb is not None: r=comb['rmass'] mass=comb['mass'] masserr=comb['masserr'] else: if r is None or mass is None or masserr is None: raise ValueError("Send a combined struct or r,mass,masserr") plt=FramedPlot() plt.aspect_ratio=aspect_ratio plt.xlog=True plt.ylog=True if not nolabel: plt.xlabel = r'$r$ [$h^{-1}$ Mpc]' plt.ylabel = r'$M(<r) ~ [h^{-1} M_{\odot}]$' od=add_to_log_plot(plt, r, mass, masserr, color=color, type=type, minval=minval) plt.xrange = od['xrange'] plt.yrange = od['yrange'] if not noshow: plt.show() od['plt'] = plt return od
def plot_dsig(**keys): """ This one stands alone. """ comb=keys.get('comb',None) r=keys.get('r',None) dsig=keys.get('dsig',None) dsigerr=keys.get('dsigerr',None) color=keys.get('color','black') type=keys.get('type','filled circle') nolabel=keys.get('nolabel',False) show=keys.get('show',True) minval=keys.get('minval',1.e-3) xlog=keys.get('xlog',True) ylog=keys.get('ylog',True) aspect_ratio=keys.get('aspect_ratio',1) plt=keys.get('plt',None) label=keys.get('label',None) if comb is not None: r=comb['r'] dsig=comb['dsig'] dsigerr=comb['dsigerr'] else: if r is None or dsig is None or dsigerr is None: raise ValueError("Send a combined struct or r,dsig,dsigerr") if plt is None: plt=FramedPlot() plt.aspect_ratio=aspect_ratio plt.xlog=xlog plt.ylog=ylog if not nolabel: plt.xlabel = labels['rproj'] plt.ylabel = labels['dsig'] if ylog: od=add_to_log_plot(plt, r, dsig, dsigerr, color=color, type=type, minval=minval) plt.xrange = od['xrange'] plt.yrange = od['yrange'] if label: od['p'].label=label else: zpts=Curve(r, dsig*0) plt.add(zpts) pts=Points(r, dsig, type=type, color=color) if label: pts.label=label plt.add(pts) if dsigerr is not None: epts=SymErrY(r, dsig, dsigerr, color=color) plt.add(epts) yrng=keys.get('yrange',None) xrng=keys.get('xrange',None) if yrng: plt.yrange=yrng if xrng: plt.xrange=xrng else: if xlog: plt.xrange=eu.plotting.get_log_plot_range(r) if show: plt.show() if ylog: od['plt'] = plt return od else: return plt
def plot2dsig_over(r1,dsig1,dsig1err,r2,dsig2, dsig2err, **keys): ptype1=keys.get('ptype1','filled circle') ptype2=keys.get('ptype2','filled circle') size1=keys.get('size1',1) size2=keys.get('size2',1) color1=keys.get('color1','red') color2=keys.get('color2','blue') label = keys.get('label',None) label1 = keys.get('label1',None) label2 = keys.get('label2',None) xrng = keys.get('xrange',None) yrng = keys.get('yrange',None) show = keys.get('show',True) ylog = keys.get('ylog',True) plt=keys.get('plt',None) yall=numpy.concatenate((dsig1, dsig2)) yerrall=numpy.concatenate((dsig1err, dsig2err)) if yrng is None: if ylog: yrng = eu.plotting.get_log_plot_range(yall, err=yerrall, input_range=yrng) rr=numpy.concatenate((r1,r2)) if xrng is None: xrng = eu.plotting.get_log_plot_range(rr) if plt is None: plt=FramedPlot() plt.xlog=True plt.ylog=ylog plt.xrange=xrng plt.yrange=yrng plt.xlabel = labels['rproj'] plt.ylabel = labels['dsig'] dsig1_p = Points(r1, dsig1, color=color1, type=ptype1, size=size1) dsig1err_p = SymErrY(r1, dsig1, dsig1err, color=color2) dsig1_p.label=label1 dsig2_p = Points(r2, dsig2, color=color2, type=ptype2, size=size2) dsig2err_p = SymErrY(r2, dsig2, dsig2err, color=color2) dsig2_p.label=label2 plt.add(dsig1_p, dsig2_p) if ylog: # biggles chokes if you give it negative data for a log plot eu.plotting.add_log_error_bars(plt,'y',r1,dsig1,dsig1err,yrng, color=color1) eu.plotting.add_log_error_bars(plt,'y',r2,dsig2,dsig2err,yrng, color=color2) else: err1 = biggles.SymmetricErrorBarsY(r1,dsig1,dsig1err,color=color1) err2 = biggles.SymmetricErrorBarsY(r2,dsig2,dsig2err,color=color2) plt.add(err1,err2) zerop = biggles.Curve(xrng, [0,0]) plt.add(zerop) if label is not None: plt.add(PlotLabel(0.9,0.9,label,halign='right')) if label1 is not None or label2 is not None: key = PlotKey(0.9,0.15, [dsig1_p,dsig2_p], halign='right') plt.add(key) if show: plt.show() return plt
def compare_idl(ratios=False, epsfile=None): from biggles import FramedPlot,Points, Table omega_m=0.25 omega_b=0.055 sigma_8=1.0 h=1.0 ns=0.98 r=eu.recfile.Open('/home/esheldon/tmp/linear-compare/pkidl.dat', delim=' ',dtype=[('k','f8'),('pk','f8')]) pkidlst = r.read() r.close() k,pkidl = pkidlst['k'],pkidlst['pk'] r=eu.recfile.Open('/home/esheldon/tmp/linear-compare/xiidl.dat', delim=' ',dtype=[('r','f8'),('xi','f8')]) xiidlst = r.read() r.close() r,xiidl = xiidlst['r'],xiidlst['xi'] l = Linear(omega_m=omega_m,omega_b=omega_b,sigma_8=sigma_8,h=h,ns=ns) pk = l.pk(k) xi = l.xi(r, nplk=1000) #xi = l.xi(r) pkrat = pk/pkidl xirat = xi/xiidl tab=Table(2,1) symsize=1 if ratios: pkplt = FramedPlot() pkplt.xlabel='k' pkplt.xlog=True pkplt.ylabel='P(k)/P(k) dave' pkplt.add( Points(k,pkrat,type='filled circle',size=symsize) ) pkplt.add( Curve(k,pkrat,color='blue') ) tab[0,0] = pkplt if ratios: xiplt = FramedPlot() xiplt.xlabel='r' xiplt.xlog=True xiplt.ylabel='xi(r)/xi(r) dave' xiplt.add( Points(r,xirat,type='filled circle',size=symsize) ) xiplt.add( Curve(r,xirat,color='blue') ) tab[1,0] = xiplt else: # big log-log plot limxi = where(xi < 1.e-5, 1.e-5, xi) #pxi = Points(r,limxi,type='filled circle',size=symsize) cxi = Curve(r,limxi,color='blue') limxiidl = where(xiidl < 1.e-5, 1.e-5, xiidl) #pxiidl = Points(r,limxiidl,type='filled circle',size=symsize) cxiidl = Curve(r,limxiidl,color='red') xipltall = FramedPlot() xipltall.xlabel='r' xipltall.xlog=True xipltall.ylog=True xipltall.ylabel='xi(r)' #xipltall.add(pxi,cxi,pxiidl,cxiidl) xipltall.add(cxi,cxiidl) tab[0,0] = xipltall # zoomed in plot xiplt = FramedPlot() xiplt.xlabel='r' xiplt.xlog=True xiplt.ylabel='xi(r)' pxi = Points(r,xi,type='filled circle',size=symsize) cxi = Curve(r,xi,color='blue') cxi.label = 'Mine' pxiidl = Points(r,xiidl,type='filled circle',size=symsize) cxiidl = Curve(r,xiidl,color='red') cxiidl.label = 'Dave' key=PlotKey(0.8,0.9, [cxi,cxiidl]) xiplt.add(pxi,cxi,pxiidl,cxiidl) xiplt.xrange = [50.0,r.max()] xiplt.yrange=[-2.e-3,1.e-2] xiplt.add(key) tab[1,0] = xiplt if epsfile is not None: tab.write_eps(epsfile) else: tab.show()
def test_fit_nfw_lin_dsig(rmin=0.01): from biggles import FramedPlot,Points,SymmetricErrorBarsY,Curve,PlotKey omega_m=0.25 z=0.25 r200 = 1.0 c = 5.0 B=10.0 rmax = 50.0 log_rmin = log10(rmin) log_rmax = log10(rmax) npts = 30 r = 10.0**linspace(log_rmin,log_rmax,npts) fitter = NFWBiasFitter(omega_m,z,r) ds = fitter.dsig(r, r200, c, B) # 10% errors dserr = 0.1*ds ds += dserr*numpy.random.standard_normal(ds.size) guess = numpy.array([r200,c,B],dtype='f8') # add 10% error to the guess guess += 0.1*guess*numpy.random.standard_normal(guess.size) res = fitter.fit(ds,dserr,guess, more=True) r200_fit=res['r200'] r200_err = res['r200_err'] c_fit=res['c'] c_err = res['c_err'] B_fit=res['B'] B_err = res['B_err'] print 'Truth:' print ' r200: %f' % r200 print ' c: %f' % c print ' B: %f' % B print 'r200_fit: %f +/- %f' % (r200_fit,r200_err) print ' c_fit: %f +/- %f' % (c_fit,c_err) print ' B_fit: %f +/- %f' % (B_fit,B_err) print 'Cov:' print res['cov'] rfine = 10.0**linspace(log_rmin,log_rmax,100) fitter2 = NFWBiasFitter(omega_m,z,rfine) yfit = fitter2.dsig(rfine, r200_fit, c_fit, B_fit) yfit_nfw = fitter2.nfw.dsig(rfine, r200_fit, c_fit) yfit_lin = fitter2.lin_dsig(rfine,B_fit) plt=FramedPlot() plt.add(Points(r,ds,type='filled circle')) plt.add(SymmetricErrorBarsY(r,ds,dserr)) cyfit = Curve(rfine,yfit,color='blue') cyfit_nfw = Curve(rfine,yfit_nfw,color='red') cyfit_lin = Curve(rfine,yfit_lin,color='orange') cyfit.label = 'Best Fit' cyfit_nfw.label = 'NFW' cyfit_lin.label = 'linear' key=PlotKey(0.1,0.3,[cyfit,cyfit_nfw,cyfit_lin]) plt.add(cyfit,cyfit_nfw,cyfit_lin,key) plt.xlabel = r'$r$ [$h^{-1}$ Mpc]' plt.ylabel = r'$\Delta\Sigma ~ [M_{sun} pc^{-2}]$' plt.xrange = [0.5*rmin, 1.5*rmax] plt.yrange = [0.5*(ds-dserr).min(), 1.5*(ds+dserr).max()] plt.xlog=True plt.ylog=True plt.show()
def plot_nfw_lin_fits_byrun(run, name, npts=100, prompt=False, withlin=True, ymin=0.01, ymax=2000.0): """ This should be made not specific for the m-z splits we used on the sims """ conf = lensing.files.cascade_config(run) if withlin: ex='lin' nex='lin' else: nex='' ex='' d = lensing.sample_read(type='fit',sample=run, name=name, extra=ex) omega_m = conf['cosmo_config']['omega_m'] rravel = d['r'].ravel() xrange = [0.5*rravel.min(), 1.5*rravel.max()] #for i in xrange(d.size): i=0 for dd in d: zrange = dd['z_range'] mrange = dd['m200_range'] if dd['rrange'][0] > 0: log_rmin = log10(dd['rrange'][0]) log_rmax = log10(dd['rrange'][1]) else: log_rmin = log10(dd['r'][0]) log_rmax = log10(dd['r'][-1]) rvals = 10.0**linspace(log_rmin,log_rmax,npts) plt = FramedPlot() lensing.plotting.add_to_log_plot(plt, dd['r'],dd['dsig'],dd['dsigerr']) z = dd['z_mean'] fitter = lensing.fit.NFWBiasFitter(omega_m,z,rvals,withlin=withlin) if withlin: yfit = fitter.nfw_lin_dsig(rvals, dd['r200_fit'],dd['c_fit'],dd['B_fit']) yfit_nfw = fitter.nfw.dsig(rvals,dd['r200_fit'],dd['c_fit']) yfit_lin = fitter.lin_dsig(rvals,dd['B_fit']) yfit = where(yfit < 1.e-5, 1.e-5, yfit) yfit_lin = where(yfit_lin < 1.e-5, 1.e-5, yfit_lin) cyfit = Curve(rvals,yfit,color='blue') cyfit_nfw = Curve(rvals,yfit_nfw,color='red') cyfit_lin = Curve(rvals,yfit_lin,color='orange') cyfit.label = 'Best Fit' cyfit_nfw.label = 'NFW' cyfit_lin.label = 'linear' key=PlotKey(0.1,0.3,[cyfit,cyfit_nfw,cyfit_lin]) plt.add(cyfit,cyfit_nfw,cyfit_lin,key) else: yfit_nfw = fitter.nfw.dsig(rvals,dd['r200_fit'],dd['c_fit']) cyfit_nfw = Curve(rvals,yfit_nfw,color='blue') plt.add(cyfit_nfw) zlab='%0.2f < z < %0.2f' % (zrange[0],zrange[1]) plt.add(PlotLabel(0.7,0.8,zlab)) ll = (log10(mrange[0]),log10(mrange[1])) mlab = r'$%0.2f < logM_{200} < %0.2f$' % ll plt.add(PlotLabel(0.7,0.9,mlab)) #yrange = [ymin,(dd['dsig']+dd['dsigerr']).max()*1.5] yrange = [ymin,ymax] plt.xrange = xrange plt.yrange = yrange plt.xlog=True plt.ylog=True plt.xlabel = r'$r$ [$h^{-1}$ Mpc]' plt.ylabel = r'$\Delta\Sigma ~ [M_{sun} pc^{-2}]$' plt.aspect_ratio=1 if prompt: plt.show() rinput = raw_input('hit a key: ') if rinput == 'q': return else: d = lensing.files.lensbin_plot_dir(run,name) if not os.path.exists(d): os.makedirs(d) epsfile=path_join(d,'desmocks-nfw%s-fit-%02i.eps' % (nex,i)) print 'Writing epsfile:',epsfile plt.write_eps(epsfile) i += 1