Example #1
0
    def plot(self, yrange=None):
        from biggles import FramedPlot, Points, Curve, PlotKey, Table,PlotLabel

        res=self.res

        etrue = res['etrue']
        e1true = res['e1true']
        e2true = res['e2true']

        facvals = res['facvals']
        sigma = res['sigma']
        e1=res['e1']
        e2=res['e2']
        e=res['e']

        T=res['T']
        Ttrue=res['Ttrue']*facvals**2


        e_pdiff = e/etrue-1
        T_pdiff = T/Ttrue-1
        

        compc= Curve(sigma, sigma*0)

        ce = Curve(sigma, e_pdiff, color='red')
        pe = Points(sigma, e_pdiff, type='filled circle')

        e_plt = FramedPlot()
        e_plt.add(ce,pe,compc)
        e_plt.xlabel = r'$\sigma$'
        e_plt.ylabel = r'$e/e_{true}-1$'

        cT = Curve(sigma, T_pdiff, color='red')
        pT = Points(sigma, T_pdiff, type='filled circle')

        T_plt = FramedPlot()
        T_plt.add(cT,pT,compc)
        T_plt.xlabel = r'$\sigma$'
        T_plt.ylabel = r'$T/T_{true}-1$'

        lab=PlotLabel(0.9,0.9,self.model,halign='right')
        T_plt.add(lab)

        if yrange is not None:
            e_plt.yrange=yrange
            T_plt.yrange=yrange
        
        tab=Table(2,1)

        tab[0,0] = T_plt
        tab[1,0] = e_plt


        tab.show()
Example #2
0
def plot_boss_geometry(color=None, colorwheel=None, plt=None, width=1, show=True,
                       region=None):
    """
    Plot the boundaries in the boss_survey.par file
    """
    import esutil as eu
    import biggles
    from biggles import FramedPlot, Curve

    bg = read_boss_geometry()

    if plt is None:
        plt = FramedPlot()
        plt.xlabel=r'$\lambda$'
        plt.ylabel=r'$\eta$'

    if color is not None:
        colors = [color]*len(bg)
    elif colorwheel is not None:
        colors = colorwheel
    else:
        colors = ['red','blue','green','magenta','navyblue','seagreen',
                  'firebrick','cadetblue','green4']
        

    for i in xrange(len(bg)):
        b = bg[i]
        color = colors[i % len(colors)]
        c = eu.plotting.bbox( b['clambdaMin'], b['clambdaMax'], b['cetaMin'], b['cetaMax'],
                             color=color, width=width)
        plt.add(c)

    if region == 'ngc':
        plt.yrange = [-40.,50.]
        plt.xrange = [-80.,80.]
    elif region == 'sgc':
        plt.yrange = [105.,165.]
        plt.xrange = [-60.,60.]
    else:
        plt.yrange = [-40.,165.]
        plt.xrange = [-80.,80.]


    plt.aspect_ratio = (plt.yrange[1]-plt.yrange[0])/(plt.xrange[1]-plt.xrange[0])

    if show:
        plt.show()

    return plt
Example #3
0
    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()
Example #4
0
File: fit.py Project: esheldon/espy
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()
Example #5
0
    def make_plot(self):

        bindata=self.bindata
        bin_field=self.bin_field
        plt=FramedPlot()
        
        plt.uniform_limits=1
        plt.xlog=True
        #plt.xrange=[0.5*bindata[bin_field].min(), 1.5*bindata[bin_field].max()]
        plt.xrange=self.s2n_range
        plt.xlabel=bin_field
        plt.ylabel = r'$\gamma$'
        if self.yrange is not None:
            plt.yrange=self.yrange

        xdata=bindata[bin_field]
        xerr=bindata[bin_field+'_err']

        if self.shnum in sh1exp:
            g1exp=zeros(xdata.size)+sh1exp[self.shnum]
            g2exp=zeros(xdata.size)+sh2exp[self.shnum]
            g1exp_plt=Curve(xdata, g1exp)
            g2exp_plt=Curve(xdata, g2exp)
            plt.add(g1exp_plt)
            plt.add(g2exp_plt)


        xerrpts1 = SymmetricErrorBarsX(xdata, bindata['g1'], xerr)
        xerrpts2 = SymmetricErrorBarsX(xdata, bindata['g2'], xerr)

        type='filled circle'
        g1color='blue'
        g2color='red'
        g1pts = Points(xdata, bindata['g1'], type=type, color=g1color)
        g1errpts = SymmetricErrorBarsY(xdata, bindata['g1'], bindata['g1_err'], color=g1color)
        g2pts = Points(xdata, bindata['g2'], type=type, color=g2color)
        g2errpts = SymmetricErrorBarsY(xdata, bindata['g2'], bindata['g2_err'], color=g2color)

        g1pts.label=r'$\gamma_1$'
        g2pts.label=r'$\gamma_2$'

        key=biggles.PlotKey(0.9,0.5,[g1pts,g2pts],halign='right')

        plt.add( xerrpts1, g1pts, g1errpts )
        plt.add( xerrpts2, g2pts, g2errpts )
        plt.add(key)

        labels=self.get_labels()

        plt.add(*labels)
        plt.aspect_ratio=1

        self.plt=plt
Example #6
0
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)
Example #7
0
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
Example #8
0
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
Example #9
0
    def plot_fits(self,st):
        import biggles
        biggles.configure( 'default', 'fontsize_min', 2)
        parnames=['A','a','g0','gmax']

        npars=len(parnames)
        tab=Table(npars,1)

        magmiddle=(st['minmag'] + st['maxmag'])/2
        for i in xrange(npars):

            yval=st['pars'][:,i]
            yerr=st['perr'][:,i]

            ymean=yval.mean()
            ystd=yval.std()
            yrange=[ymean-3.5*ystd, ymean+3.5*ystd]
            
            pts=Points(magmiddle, yval, type='filled circle')
            yerrpts=SymmetricErrorBarsY(magmiddle,yval,yerr)
            xerrpts=ErrorBarsX(yval, st['minmag'], st['maxmag'])

            
            plt=FramedPlot()
            plt.yrange=yrange
            plt.add(pts,xerrpts, yerrpts)
            plt.xlabel='mag'
            plt.ylabel=parnames[i]

            tab[i,0] = plt

        if self.show:
            tab.show()

        d=files.get_prior_dir()
        d=os.path.join(d, 'plots')
        epsfile='pofe-pars-%s.eps' % self.otype
        epsfile=os.path.join(d,epsfile)
        eu.ostools.makedirs_fromfile(epsfile)
        print epsfile
        tab.write_eps(epsfile)
        os.system('converter -d 100 %s' % epsfile)
Example #10
0
    def plot_fits(self, st):
        import biggles

        biggles.configure("default", "fontsize_min", 2)
        parnames = ["A", "a", "g0", "gmax"]

        npars = len(parnames)
        tab = Table(npars, 1)

        magmiddle = (st["minmag"] + st["maxmag"]) / 2
        for i in xrange(npars):

            yval = st["pars"][:, i]
            yerr = st["perr"][:, i]

            ymean = yval.mean()
            ystd = yval.std()
            yrange = [ymean - 3.5 * ystd, ymean + 3.5 * ystd]

            pts = Points(magmiddle, yval, type="filled circle")
            yerrpts = SymmetricErrorBarsY(magmiddle, yval, yerr)
            xerrpts = ErrorBarsX(yval, st["minmag"], st["maxmag"])

            plt = FramedPlot()
            plt.yrange = yrange
            plt.add(pts, xerrpts, yerrpts)
            plt.xlabel = "mag"
            plt.ylabel = parnames[i]

            tab[i, 0] = plt

        if self.show:
            tab.show()

        d = files.get_prior_dir()
        d = os.path.join(d, "plots")
        epsfile = "pofe-pars-%s.eps" % self.otype
        epsfile = os.path.join(d, epsfile)
        eu.ostools.makedirs_fromfile(epsfile)
        print epsfile
        tab.write_eps(epsfile)
        os.system("converter -d 100 %s" % epsfile)
Example #11
0
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
Example #12
0
    def plot_fits(self,st):
        import biggles
        biggles.configure( 'default', 'fontsize_min', 2)
        if self.objtype=='gexp':
            parnames=['A','a','g0','gmax']

        else:
            parnames=['A','b','c']

        npars=len(parnames)
        tab=Table(npars,1)

        magmiddle=(st['minmag'] + st['maxmag'])/2
        for i in xrange(npars):

            yval=st['pars'][:,i]
            yerr=st['perr'][:,i]

            ymean=yval.mean()
            ystd=yval.std()
            yrange=[ymean-3.5*ystd, ymean+3.5*ystd]
            
            pts=Points(magmiddle, yval, type='filled circle')
            yerrpts=SymmetricErrorBarsY(magmiddle,yval,yerr)
            xerrpts=ErrorBarsX(yval, st['minmag'], st['maxmag'])

            
            plt=FramedPlot()
            plt.yrange=yrange
            plt.add(pts,xerrpts, yerrpts)
            plt.xlabel='mag'
            plt.ylabel=parnames[i]

            tab[i,0] = plt

        tab.show()
Example #13
0
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
Example #14
0
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
Example #15
0
    def plot_coverage(self, region='both', show=True, dops=True):
        import biggles
        from biggles import FramedPlot, Points, PlotKey


        l = self.read()
        w,=numpy.where( (l['ra'] >= 0.0) & (l['ra'] <= 360.0) )
        if w.size != l.size:
            print("threw out",l.size-w.size,"with bad ra")
            l=l[w]

        llam,leta = eu.coords.eq2sdss(l['ra'],l['dec'])
        maskflags = l['maskflags']

        lammin,lammax = (-70.,70.)

        if region=='ngc':
            # a bit high to make room for the legend
            emin,emax=(-40,60)

            biggles.configure('screen','width', 1800)
            biggles.configure('screen','height', 1140)

        elif region=='sgc':
            emin,emax=(100,165)
            biggles.configure('screen','width', 1800)
            biggles.configure('screen','height', 1140)
        else:
            emin,emax=(-40,165)
            biggles.configure('screen','width', 1140)
            biggles.configure('screen','height', 1140)

        wl=where1((leta > emin) & (leta < emax))
        llam=llam[wl]
        leta=leta[wl]
        maskflags=maskflags[wl]


        plt=FramedPlot()
        plt.xlabel=r'$\lambda$'
        plt.ylabel=r'$\eta$'

        print("adding all lenses")

        type = 'filled circle'
        symsize=0.2

        allp = Points(llam,leta,type=type,size=symsize)
        allp.label='all'
        plt.add(allp)

        wquad = es_sdsspy.stomp_maps.quad_check(maskflags)
        print("adding quad pass")
        quadp = Points(llam[wquad],leta[wquad],type=type,color='red',size=symsize)
        quadp.label = 'quad good'
        plt.add(quadp)

        fakepoints = eu.plotting.fake_filled_circles(['all','quad good'],['black','red'])
        key=PlotKey(0.95,0.95,fakepoints,halign='right')
        plt.add(key)


        es_sdsspy.stomp_maps.plot_boss_geometry(color='blue',plt=plt,show=False)

        xrng = (lammin, lammax)
        yrng = (emin, emax)
        plt.xrange = xrng
        plt.yrange = yrng
        plt.aspect_ratio = (yrng[1]-yrng[0])/float(xrng[1]-xrng[0])


        if show:
            plt.show()

        if dops:
            d = lensing.files.sample_dir(type='lcat',sample=self['sample'])
            d = os.path.join(d,'plots')
            if not os.path.exists(d):
                os.makedirs(d)
            epsfile = os.path.join(d, 'lcat-%s-%s-coverage.eps' % (self['sample'],region) )
            print("Writing to eps file:",epsfile)
            plt.write_eps(epsfile)
        return plt
Example #16
0
def doplot(run, st, s2n_field, setname, s2n_range, sratio_range, psfnums, nobj):
    tab=Table(2,1)

    color1='blue'
    color2='red'

    m1pts=Points(st['s2n'],st['m1'],type='filled circle',color=color1)
    m1errpts=SymmetricErrorBarsY(st['s2n'],st['m1'],st['m1_err'],color=color1)
    m2pts=Points(st['s2n'],st['m2'],type='filled circle',color=color2)
    m2errpts=SymmetricErrorBarsY(st['s2n'],st['m2'],st['m2_err'],color=color2)

    c1pts=Points(st['s2n'],st['c1'],type='filled circle',color=color1)
    c1errpts=SymmetricErrorBarsY(st['s2n'],st['c1'],st['c1_err'],color=color1)
    c2pts=Points(st['s2n'],st['c2'],type='filled circle',color=color2)
    c2errpts=SymmetricErrorBarsY(st['s2n'],st['c2'],st['c2_err'],color=color2)

    m1pts.label=r'$\gamma_1$'
    m2pts.label=r'$\gamma_2$'

    key=PlotKey(0.9,0.2,[m1pts,m2pts], halign='right')

    xrng=array( [0.5*s2n_range[0], 1.5*s2n_range[1]])
    cyrng=get_symmetric_range(st['c1'],st['c1_err'],st['c2'],st['c2_err'])
    myrng=get_symmetric_range(st['m1'],st['m1_err'],st['m2'],st['m2_err'])

    mplt=FramedPlot()
    cplt=FramedPlot()
    mplt.xlog=True
    cplt.xlog=True

    mplt.xrange=xrng
    cplt.xrange=xrng
    #mplt.yrange=myrng
    #cplt.yrange=cyrng
    mplt.yrange=[-0.15,0.15]
    cplt.yrange=[-0.01,0.01]

    mplt.xlabel=s2n_field
    mplt.ylabel='m'
    cplt.xlabel=s2n_field
    cplt.ylabel='c'

    zplt=Curve(xrng, [0,0])
    mallow=Curve(xrng, [-0.004, 0.004])
    callow=Curve(xrng, [-0.0004, 0.0004])

    mallow=FillBetween(xrng, [0.004,0.004], 
                       xrng, [-0.004,-0.004],
                       color='grey80')
    callow=FillBetween(xrng, [0.0004,0.0004], 
                       xrng, [-0.0004,-0.0004],
                       color='grey80')



    labels=get_labels(run,
                      psfnums,
                      setname,
                      nobj,
                      sratio_range)

    mplt.add( mallow, zplt, m1pts, m1errpts, m2pts, m2errpts, key )
    mplt.add(*labels)

    cplt.add( callow, zplt, c1pts, c1errpts, c2pts, c2errpts )

    tab[0,0] = mplt
    tab[1,0] = cplt

    tab.show()
Example #17
0
    def plot_vs_field(self, field, plot_type, 
                      rmag_min=None, 
                      rmag_max=None, 
                      fmin=None, fmax=None, 
                      nbin=20, nperbin=50000,
                      xrng=None, yrng=None, show=True):

        
        allowed=['meane','residual']
        if plot_type not in allowed:
            raise ValueError("plot_type should be in [%s]" % ','.join(allowed))
        if plot_type == 'residual' and self.sweeptype != 'star':
            raise ValueError("residuals only supported for stars")

        if rmag_min is None:
            if self.sweeptype == 'gal':
                rmag_min=18.0
            else:
                rmag_min=15.0

        if rmag_max is None:
            if self.sweeptype == 'gal':
                rmag_max=21.8
            else:
                rmag_max=19.0

        # this will only load the main data once.
        self.load_data(field)

        print("Using rmag range: [%0.2f,%0.2f]" % (rmag_min,rmag_max))
        # notes 
        #  - amflags here is really corrflags_rg for gals
        #  - e1 is really e1_rg for gals
        logic = ((self['amflags'] == 0)
                 & (self['e1'] < 4)
                 & (self['e1'] > -4)
                 & (self['rmag'] > rmag_min)
                 & (self['rmag'] < rmag_max) )
        
        if self.sweeptype == 'gal':
            logic = logic & (self['R'] > 1.0/3.0) & (self['R'] < 1.0)

        w=where1(logic)
        print("Number passing cuts:",w.size)
        minnum=31000
        if w.size < minnum:
            print("want %d good objects, found %d" % (minnum,w.size))
            return

        weights = 1.0/(0.32**2 + self['uncer'][w]**2)

        # we can try to get nice labels for some fields
        if field == 'fwhm_psf':
            field_data = self['fwhm_psf'][w]
            fstr = 'PSF FWHM (arcsec)'
        elif field == 'sigma_psf':
            field_data = self['sigma_psf'][w]
            fstr = r'\sigma_{PSF}'
        elif field == 'sigma':
            field_data = self['sigma'][w]
            fstr = r'\sigma_{obj+PSF}'
        else:
            field_data = self[field][w]
            fstr=field
            fstr = fstr.replace('_','\_')

        print("Plotting for field:",field)

        if plot_type == 'residual':
            print('  doing: residual')
            be1 = eu.stat.Binner(field_data, self['e1'][w]-self['e1_psf'][w], 
                                 weights=weights)
            be2 = eu.stat.Binner(field_data, self['e2'][w]-self['e2_psf'][w], 
                                 weights=weights)
            ylabel = r'$<e_{star}-e_{PSF}>$'
        else:
            print('  doing meane')
            be1 = eu.stat.Binner(field_data, self['e1'][w], weights=weights)
            be2 = eu.stat.Binner(field_data, self['e2'][w], weights=weights)
            ylabel = r'$<e>$'


        # regular hist for display
        print("  regular fixed binsize hist")
        xm,xe,xstd=eu.stat.wmom(field_data, weights, sdev=True)
        #hxmin = xm-4.0*xstd
        #hxmax = xm+4.0*xstd
        bsize = xstd/5.
        hist = eu.stat.histogram(field_data, binsize=bsize, 
                                 weights=weights, more=True)


        print("  hist  e1, nperbin: ",nperbin)
        be1.dohist(nperbin=nperbin, min=fmin, max=fmax)
        #be1.dohist(nbin=nbin, min=fmin, max=fmax)
        print("  stats e1")
        be1.calc_stats()
        print("  hist  e2, nperbin: ",nperbin)
        be2.dohist(nperbin=nperbin, min=fmin, max=fmax)
        #be2.dohist(nbin=nbin, min=fmin, max=fmax)
        print("  stats e2")
        be2.calc_stats()



        plt = FramedPlot()

        if xrng is not None:
            plt.xrange=xrng
        else:
            if field == 'R':
                plt.xrange=[0.29,1.01]

        if yrng is not None:
            plt.yrange=yrng
            ymin = yrng[0]
            ymax = 0.8*yrng[1]
        else:
            ymin = min( be1['wymean'].min(),be2['wymean'].min() )
            ymax = 0.8*max( be1['wymean'].max(),be2['wymean'].max() )

        # this is a histogram-like object
        ph = eu.plotting.make_hist_curve(hist['low'], hist['high'], hist['whist'], 
                                         ymin=ymin, ymax=ymax, color='grey50')
        plt.add(ph)

        p1 = Points( be1['wxmean'], be1['wymean'], 
                    type='filled circle', color='blue')
        p1err = SymErrY( be1['wxmean'], be1['wymean'], be1['wyerr2'], color='blue')
        p1.label = r'$e_1$'

        p2 = Points( be2['wxmean'], be2['wymean'], 
                    type='filled circle', color='red')
        p2.label = r'$e_2$'
        p2err = SymErrY( be2['wxmean'], be2['wymean'], be2['wyerr2'], color='red')

        key = PlotKey(0.1,0.9, [p1,p2])
        plt.add(p1, p1err, p2, p2err, key)

        if self.camcol != 'any' and field == 'R' and plot_type=='meane':
            order=3
            print("  getting poly order",order)
            coeff1 = numpy.polyfit(be1['wxmean'], be1['wymean'], order)
            poly1=numpy.poly1d(coeff1)
            coeff2 = numpy.polyfit(be2['wxmean'], be2['wymean'], order)
            poly2=numpy.poly1d(coeff2)

            ps1 = Curve( be1['wxmean'], poly1(be1['wxmean']), color='blue')
            ps2 = Curve( be2['wxmean'], poly2(be2['wxmean']), color='red')
            plt.add(ps1,ps2)

            polyf = self.R_polyfile(self.camcol, rmag_max)
            out={'coeff_e1':list([float(c) for c in coeff1]),
                 'coeff_e2':list([float(c) for c in coeff2])}
            print("    -- Writing poly coeffs to:",polyf)
            eu.io.write(polyf,out)
        if field != 'rmag':
            rmag_lab = \
                PlotLabel(0.1,0.05,'%0.2f < rmag < %0.2f' % (rmag_min,rmag_max), 
                          halign='left')
            plt.add(rmag_lab)

        procrun_lab = PlotLabel(0.1,0.1,
                            'procrun: %s filter: %s' % (self.procrun, self.band), 
                            halign='left')
        plt.add(procrun_lab)
        cy=0.9
        if self.run != 'any':
            run_lab = PlotLabel(0.9,0.9, 'run: %06i' % self.run, halign='right')
            plt.add(run_lab)
            cy=0.8
        if self.camcol != 'any':
            run_lab = PlotLabel(0.9,cy, 'camcol: %i' % self.camcol, halign='right')
            plt.add(run_lab)



        plt.xlabel = r'$'+fstr+'$'
        plt.ylabel = ylabel


        if show:
            plt.show()
        epsfile = self.plotfile(field, rmag_max, plot_type=plot_type)
        eu.ostools.makedirs_fromfile(epsfile, verbose=True)
        print("  Writing eps file:",epsfile)
        plt.write_eps(epsfile)

        converter.convert(epsfile, verbose=True)
Example #18
0
File: fit.py Project: esheldon/espy
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()
Example #19
0
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()
Example #20
0
    def test_scinv_npts(self, nplot, show=False, reload=False, type="png"):
        """

        Test accuracy as a function of the numer of points used in the
        integration.


        """

        dzl = 0.015
        zlmin = 0.02
        zlmax = 0.6

        from biggles import Points, FramedPlot, PlotKey, Table, Histogram, Curve
        from time import time
        import lensing
        import pcolors

        if self.data is None or reload:
            self.load_example_data()

        # this is old ScinvCalculator, need to make work
        # with new one
        scalc1000 = ScinvCalculator(self.zs, dzl, zlmin, zlmax, npts=1000)

        nptsvals = [100, 200, 300, 400, 500, 600, 700, 800, 900]
        numcheck = len(nptsvals)
        # colors=['black','magenta','blue','green','orange','red']
        colors = pcolors.rainbow(len(nptsvals), "hex")
        scalc = []
        for npts in nptsvals:
            scalc.append(ScinvCalculator(self.zs, dzl, zlmin, zlmax, npts=npts))

        times = numpy.zeros(numcheck, dtype="f8")
        time1000 = 0.0

        # we'll fill this in
        scinv_all = numpy.zeros((numcheck, scalc1000.zlvals.size))

        xlim = [0, scalc1000.zsvals.max()]
        for i in xrange(nplot):
            pz = self.data["pofz"][i]

            print("Doing 1000...", end="")

            tm0 = time()
            scinv1000 = scalc1000.calc_mean_scinv(pz)
            time1000 += time() - tm0

            print("done")

            for j in xrange(numcheck):
                npts = nptsvals[j]
                print("%d " % npts, end="")
                tm0 = time()
                scinv_all[j, :] = scalc[j].calc_mean_scinv(pz)
                times[j] += time() - tm0

            print("\nplotting")

            # plot the p(z)
            tab = Table(3, 1)

            binsize = scalc1000.zsvals[1] - scalc1000.zsvals[0]
            pzh = Histogram(pz, x0=scalc1000.zsvals[0], binsize=binsize)
            plt_pzh = FramedPlot()
            plt_pzh.xrange = xlim

            plt_pzh.xtitle = r"$z_s$"
            plt_pzh.ytitle = r"$P(z_s)$"
            plt_pzh.add(pzh)
            tab[0, 0] = plt_pzh

            # plot scinv for each npts value
            plt_scinv = FramedPlot()
            plt_scinv.xrange = xlim

            scinv_plots = []
            for j in xrange(numcheck):
                npts = nptsvals[j]
                p = Curve(scalc[j].zlvals, scinv_all[j, :], type="solid", color=colors[j])
                p.label = "npts: %d" % npts

                plt_scinv.add(p)
                scinv_plots.append(p)

            scinv_key = PlotKey(0.95, 0.9, scinv_plots, halign="right")
            plt_scinv.add(scinv_key)

            plt_scinv.ylabel = r"$\langle \Sigma_{crit}^{-1}(z_{lens}) \rangle$"
            plt_scinv.xlabel = r"$z_{lens}$"
            plt_scinv.yrange = [0, 2.1e-4]

            tab[1, 0] = plt_scinv

            # ratio to 1000 points

            plt_rat = FramedPlot()
            plt_rat.xrange = xlim
            plt_rat.yrange = [1 - 1.0e-2, 1 + 1.0e-2]
            rat_plots = []
            for j in xrange(numcheck):
                npts = nptsvals[j]
                w = where1(scinv1000 > 0)
                ratio = scinv_all[j, w] / scinv1000[w]
                # ratio=scinv_all[j,:]/scinv1000[:]

                p = Curve(scalc[j].zlvals[w], ratio, type="solid", color=colors[j])
                p.label = "npts: %d" % npts

                plt_rat.add(p)
                rat_plots.append(p)

            key = PlotKey(0.95, 0.9, rat_plots, halign="right")
            plt_rat.add(key)

            plt_rat.ylabel = r"$\langle \Sigma_{crit}^{-1} \rangle / \langle \Sigma_{crit}^{-1} \rangle_{1000}$"
            plt_rat.xlabel = r"$z_{lens}$"

            tab[2, 0] = plt_rat

            if show:
                tab.show()

            plotfile = self.npts_plot_file(i, type)
            print("writing to file:", plotfile)
            if type == "png":
                tab.write_img(1000, 1000, plotfile)
            else:
                tab.write_eps(plotfile)

        print("time npts=1000:", time1000)
        for j in xrange(numcheck):
            npts = nptsvals[j]
            print("time npts=%s: %s" % (npts, times[j]))
Example #21
0
    def test_scinv_dz(self, beg, end, yrange=[0, 2.1e-4], show=False, reload=False, type="png"):
        """

        Test accuracy of interpolating scinv as a function of dzl, the
        lens redshift spacing.

        """
        import biggles
        from biggles import Points, FramedPlot, PlotKey, Table, Histogram, Curve
        from time import time
        import lensing
        import pcolors

        biggles.configure("default", "fontface", "HersheySans")
        biggles.configure("default", "fontsize_min", 1.3)

        zsmin = self.zs[0]
        zsmax = self.zs[-1]

        zlmin = 0.00
        zlmax = 1.0

        # dzl_vals = numpy.linspace(0.001,0.015,10)
        dzl_vals = numpy.linspace(0.001, 0.015, 4)
        nzl_vals = ((zlmax - zlmin) / dzl_vals).astype("i8")

        numcheck = len(dzl_vals)
        colors = pcolors.rainbow(numcheck, "hex")
        scalc = []
        for nzl in nzl_vals:
            s = ScinvCalculator(zlmin, zlmax, nzl, zsmin, zsmax, npts=100)
            scalc.append(s)

        times = numpy.zeros(numcheck, dtype="f8")

        # we'll fill this in
        # scinv_all = numpy.zeros( (numcheck, scalc[0].zlvals.size) )

        xlim = [0, zsmax]
        for i in xrange(beg, end):
            scinv_all = []
            pz = self.data["pofz"][i]

            # print(pz)

            for j in xrange(numcheck):
                dzl = dzl_vals[j]
                nzl = nzl_vals[j]
                print("    nzl: %s dzl: %g" % (nzl, dzl))
                tm0 = time()
                # scinv_all[j,:] = scalc[j].calc_mean_scinv(pz)
                sc = scalc[j].calc_mean_scinv(self.zs, pz)
                # sc=sc.clip(min=0.0)
                # print("sc",j,sc)
                scinv_all.append(sc)
                times[j] += time() - tm0

            print("\nplotting")

            # plot the p(z)
            tab = Table(3, 1)

            binsize = self.zs[1] - self.zs[0]
            pzh = Histogram(pz, x0=self.zs[0], binsize=binsize)
            plt_pzh = FramedPlot()
            plt_pzh.xrange = xlim

            plt_pzh.xtitle = r"$z_s$"
            plt_pzh.ytitle = r"$P(z_s)$"
            plt_pzh.add(pzh)
            tab[0, 0] = plt_pzh
            # plt_pzh.show()

            # plot scinv for each dzl
            plt_scinv = FramedPlot()
            plt_scinv.xrange = xlim

            scinv_plots = []
            for j in xrange(numcheck):
                dzl = dzl_vals[j]
                nzl = nzl_vals[j]
                p = Curve(scalc[j].zlvals, scinv_all[j], type="solid", color=colors[j])
                p.label = r"$nz_{lens}: %s dz_{lens}: %0.3f$" % (nzl, dzl)

                plt_scinv.add(p)
                scinv_plots.append(p)

            scinv_key = PlotKey(0.95, 0.9, scinv_plots, halign="right")
            plt_scinv.add(scinv_key)

            plt_scinv.ylabel = r"$\Sigma_{crit}^{-1}(z_{lens})$"
            plt_scinv.xlabel = r"$z_{lens}$"
            plt_scinv.yrange = yrange

            # plt_scinv.show()
            tab[1, 0] = plt_scinv

            # %diff to best dz

            plt_pdiff = FramedPlot()
            plt_pdiff.xrange = xlim
            plt_pdiff.yrange = [-0.05, 0.05]
            pdiff_plots = []

            zl_interp = numpy.linspace(zlmin, zlmax, 1000)
            scinv_interp_best = esutil.stat.interplin(scinv_all[0], scalc[0].zlvals, zl_interp)

            w = where1(scinv_interp_best > 0)
            for j in xrange(numcheck):
                dzl = dzl_vals[j]
                nzl = nzl_vals[j]

                scinv_interp = esutil.stat.interplin(scinv_all[j], scalc[j].zlvals, zl_interp)

                if w.size > 0:
                    pdiff = scinv_interp[w] / scinv_interp_best[w] - 1.0
                    p = Curve(zl_interp[w], pdiff, type="solid", color=colors[j])
                else:
                    pdiff = numpy.ones(scinv_interp.size)
                    p = Curve(zl_interp, pdiff, type="solid", color=colors[j])

                p.label = r"$nz_{lens}: %s dz_{lens}: %0.3f$" % (nzl, dzl)

                plt_pdiff.add(p)
                pdiff_plots.append(p)

            key = PlotKey(0.95, 0.9, pdiff_plots, halign="right")
            plt_pdiff.add(key)

            plt_pdiff.ylabel = r"$\Sigma_{crit}^{-1} /  \Sigma_{crit}^{-1}_{best} - 1$"
            plt_pdiff.xlabel = r"$z_{lens}$"

            tab[2, 0] = plt_pdiff

            if show:
                tab.show()

            plotfile = self.dzl_plot_file(i, type)
            print("writing to file:", plotfile)
            if type == "png":
                tab.write_img(1000, 1000, plotfile)
            else:
                tab.write_eps(plotfile)

        for j in xrange(numcheck):
            dzl = dzl_vals[j]
            print("time dzl=%s: %s" % (dzl, times[j]))
Example #22
0
    def plot_coverage(self, region='both', show=True, dops=True):
        """

        Plot a random subset of the randoms along with the
        boss survey geometry area as bounding boxes

        """
        import biggles
        from biggles import FramedPlot, Points, PlotKey

        symsize=0.5

        l = self.read()
        llam,leta = eu.coords.eq2sdss(l['ra'],l['dec'])

        lammin,lammax = (-70.,70.)

        if region=='ngc':
            # a bit high to make room for the legend
            emin,emax=(-40,60)

            biggles.configure('screen','width', 1800)
            biggles.configure('screen','height', 1140)
            width=1.5

        elif region=='sgc':
            emin,emax=(125,165)
            biggles.configure('screen','width', 1800)
            biggles.configure('screen','height', 1140)
            width=2
        else:
            emin,emax=(-40,165)
            biggles.configure('screen','width', 1140)
            biggles.configure('screen','height', 1140)
            width=2

        wl=where1((leta > emin) & (leta < emax))
        llam=llam[wl]
        leta=leta[wl]


        plt=FramedPlot()
        plt.xlabel=r'$\lambda$'
        plt.ylabel=r'$\eta$'
        xrng = (lammin, lammax)
        yrng = (emin, emax)
        plt.xrange = xrng
        plt.yrange = yrng


        print("adding random subset of randoms")

        ii = eu.numpy_util.random_subset(llam.size, 500000)
        allp = Points(llam[ii],leta[ii],type='dot',size=symsize)
        plt.add(allp)

        plt.aspect_ratio = (yrng[1]-yrng[0])/float(xrng[1]-xrng[0])

        #es_sdsspy.stomp_maps.plot_boss_geometry(color='blue',plt=plt,show=False)
        es_sdsspy.stomp_maps.plot_boss_geometry(plt=plt,show=False,width=width)

        if show:
            plt.show()

        if dops:
            d = lensing.files.sample_dir(type='lcat',sample=self['sample'])
            d = os.path.join(d,'plots')
            if not os.path.exists(d):
                os.makedirs(d)
            epsfile = os.path.join(d, '%s-%s-coverage.eps' % ('lcat',self['sample']))
            print("Writing to eps file:",epsfile)
            plt.write_eps(epsfile)
        return plt
Example #23
0
File: fit.py Project: esheldon/espy
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
Example #24
0
    def plot_coverage_bybin(self, binner, region='both', 
                            show=True, dops=True, rand=None):
        import pcolors
        import biggles
        import converter
        from biggles import FramedPlot, Points, PlotKey


        orig = self.read_original()
        lcat = self.read()

        all_clam,all_ceta = eu.coords.eq2sdss(orig['ra'],orig['dec'])

        l = orig[lcat['zindex']]
        clam,ceta = eu.coords.eq2sdss(lcat['ra'],lcat['dec'])

        clammin,clammax = (-70.,120.)

        if region=='ngc':
            # a bit high to make room for the legend
            emin,emax=(-40,60)

            biggles.configure('screen','width', 1800)
            biggles.configure('screen','height', 1140)

            clammin,clammax = (-70.,120.)

        elif region=='sgc':
            emin,emax=(105,165)
            biggles.configure('screen','width', 1800)
            biggles.configure('screen','height', 1140)
            clammin,clammax = (-50.,90.)
        else:
            emin,emax=(-40,165)
            biggles.configure('screen','width', 1140)
            biggles.configure('screen','height', 1140)
            clammin,clammax = (-70.,120.)

        wl=where1((all_ceta > emin) & (all_ceta < emax))
        all_clam=all_clam[wl]
        all_ceta=all_ceta[wl]

        wl=where1((ceta > emin) & (ceta < emax))
        clam=clam[wl]
        ceta=ceta[wl]
        l=l[wl]


        plt=FramedPlot()
        plt.xlabel=r'$\lambda$'
        plt.ylabel=r'$\eta$'
        xrng = (clammin, clammax)
        yrng = (emin, emax)
        plt.xrange = xrng
        plt.yrange = yrng


        print("adding all lenses")

        type = 'filled circle'
        symsize=0.2
        colors = pcolors.rainbow(binner['nbin'],'hex')


        if rand is not None:
            clam_r,ceta_r = eu.coords.eq2sdss(rand['ra'],rand['dec'])
            wl=where1((ceta_r > emin) & (ceta_r < emax))
            clam_r=clam_r[wl]
            ceta_r=ceta_r[wl]
            rp = Points(clam_r, ceta_r, type='dot', size=0.2)
            plt.add(rp)
        
        size_min=0.2
        size_max=4

        sizes=[]
        minlambda = l['lambda_zred'].min()
        maxlambda = l['lambda_zred'].max()
        for i in xrange(binner['nbin']):
            w=binner.select_bin(l, i)
            mlam=l['lambda_zred'][w].mean()
            # scale 0 to 1
            sz=(mlam-minlambda)/maxlambda
            # now scale size
            sz = size_min + sz*(size_max-size_min)
            sizes.append(sz)

        all_plots=[]
        labels=[]
        #for i in xrange(binner['nbin']):
        for i in reversed(xrange(binner['nbin'])):
            w=binner.select_bin(l, i)

            #points = Points(clam[w], ceta[w],type=type,size=symsize, color=colors[i])
            points = Points(clam[w], ceta[w],type=type,size=sizes[i], color=colors[i])
            labels.append(binner.bin_label(i))

            plt.add(points)


        labels.reverse()
        fakepoints = eu.plotting.fake_filled_circles(labels, colors)
        key=PlotKey(0.95,0.95,fakepoints,halign='right',size=1.5)
        plt.add(key)

        plt.aspect_ratio = (yrng[1]-yrng[0])/float(xrng[1]-xrng[0])

        es_sdsspy.stomp_maps.plot_boss_geometry(color='blue',plt=plt,show=False)

        if show:
            plt.show()

        if dops:
            d = lensing.files.sample_dir(type='lcat',sample=self['sample'])
            d = os.path.join(d,'plots')
            if not os.path.exists(d):
                os.makedirs(d)
            epsfile = os.path.join(d, 'lcat-%s-coverage-bybin.eps' % self['sample'])
            if rand is not None:
                epsfile=epsfile.replace('.eps','-withrand.eps')
            if region in ['sgc','ngc']:
                epsfile=epsfile.replace('.eps','-%s.eps' % region)

            print("Writing to eps file:",epsfile)
            plt.write_eps(epsfile)
            print("converting to png")
            converter.convert(epsfile, dpi=300)
        return plt
Example #25
0
    def plot_ellip_vs_field(self, field, rmag_max=21.8, fmin=None, fmax=None, nbin=20, nperbin=50000,
                            yrange=None, show=True):
        self.load_data()

        w=where1((self['cmodelmag_dered_r'] > 18.0) & (self['cmodelmag_dered_r'] < rmag_max) )

        if w.size == 0:
            print("no good objects")
            return

        weights = 1.0/(0.32**2 + self['uncer_rg'][w]**2)

        if field == 'psf_fwhm':
            field_data = self['psf_fwhm'][w]
            fstr = 'PSF FWHM (arcsec)'
        elif field == 'psf_sigma':
            field_data = self['psf_sigma'][w]
            fstr = r'$\sigma_{PSF}$'
        elif field == 'R_rg':
            field_data = self['r_rg'][w]
            fstr = 'R_rg'
        else:
            field_data = self[field][w]
            fstr=field

        print("Plotting mean e for field:",field)

        fstr = fstr.replace('_','\_')

        be1 = eu.stat.Binner(field_data, self['e1_rg'][w], weights=weights)
        be2 = eu.stat.Binner(field_data, self['e2_rg'][w], weights=weights)

        print("  hist  e1")
        be1.dohist(nperbin=nperbin, min=fmin, max=fmax)
        #be1.dohist(nbin=nbin, min=fmin, max=fmax)
        print("  stats e1")
        be1.calc_stats()
        print("  hist  e2")
        be2.dohist(nperbin=nperbin, min=fmin, max=fmax)
        #be2.dohist(nbin=nbin, min=fmin, max=fmax)
        print("  stats e2")
        be2.calc_stats()

        plt = FramedPlot()
        p1 = Points( be1['wxmean'], be1['wymean'], type='filled circle', color='blue')
        p1err = SymErrY( be1['wxmean'], be1['wymean'], be1['wyerr2'], color='blue')
        p1.label = r'$e_1$'

        p2 = Points( be2['wxmean'], be2['wymean'], type='filled circle', color='red')
        p2.label = r'$e_2$'
        p2err = SymErrY( be2['wxmean'], be2['wymean'], be2['wyerr2'], color='red')

        key = PlotKey(0.8, 0.9, [p1,p2])
        plt.add(p1, p1err, p2, p2err, key)

        if field != 'cmodelmag_dered_r':
            rmag_lab = PlotLabel(0.1,0.05,'rmag < %0.2f' % rmag_max, halign='left')
            plt.add(rmag_lab)

        plab = PlotLabel(0.1,0.1, 'CH+RM', halign='left')
        plt.add(plab)

        plt.xlabel = r'$'+fstr+'$'
        plt.ylabel = r'$<e>$'

        if yrange is not None:
            plt.yrange=yrange

        if show:
            plt.show()
        epsfile = self.plotfile(field, rmag_max)
        print("  Writing eps file:",epsfile)
        plt.write_eps(epsfile)
Example #26
0
def main():
    options,args = parser.parse_args(sys.argv[1:])

    if len(args) < 3:
        parser.print_help()
        sys.exit(1)

    run1 = args[0]
    run2 = args[1]
    bintype = args[2]

    rrange=options.rrange
    if rrange is not None:
        rrange=[float(r) for r in rrange.split(',')]

    biggles.configure('screen','width', 1400)
    biggles.configure('default','fontsize_min',1.0)

    b = lensing.binning.instantiate_binner(bintype)

    name=b.get_name()
    data1 = lensing.files.sample_read(type=options.type, sample=run1, name=name)
    data2 = lensing.files.sample_read(type=options.type, sample=run2, name=name)

    nbin=data1.size

    tab=Table(1,2)
    for i in xrange(nbin):
        tab[0,0] = plot2dsig_over(
            data1['r'][i],data1['dsig'][i],data1['dsigerr'][i],
            data2['r'][i],data2['dsig'][i],data2['dsigerr'][i],
            label1=run1,label2=run2,label=b.bin_label(i),
            show=False)
        tab[0,0].aspect_ratio=1

        pbot = FramedPlot()

        rat=data1['dsig'][i]/data2['dsig'][i]
        raterr = rat*numpy.sqrt( (data1['dsigerr'][i]/data1['dsig'][i])**2
                                +(data2['dsigerr'][i]/data2['dsig'][i])**2 )

        rpts=Points(data1['r'][i], rat, type='filled circle',color='blue')
        rerr=SymErrY(data1['r'][i], rat, raterr,color='blue')

        if rrange is None:
            rat_use,raterr_use=rat,raterr
        else:
            w,=numpy.where( (data1['r'][i] > rrange[0]) & (data1['r'][i] < rrange[1]))
            rat_use,raterr_use=rat[w],raterr[w]
        mn,err = eu.stat.wmom(rat_use, 1/raterr_use**2, calcerr=True)

        plot_rrange=[0.5*data1['r'][i].min(),data1['r'][i].max()*2]
        z=Curve(plot_rrange, [1,1])
        pbot.add(rpts,rerr,z)
        pbot.xlog=True
        pbot.xlabel = lensing.plotting.labels['rproj']
        pbot.ylabel = '%s/%s' % (run1,run2)
        pbot.yrange=[0,2]
        pbot.xrange = plot_rrange
        pbot.aspect_ratio=1

        lab='<ratio>'
        if rrange is not None:
            lab = r'$%s r \in [%.2f,%.2f]$' % (lab,rrange[0],rrange[1])

        lab='%s: %0.2f +/- %0.2f' % (lab,mn,err)
        pbot.add(PlotLabel(0.9,0.9,lab,halign='right'))

        tab[0,1] = pbot
        tab.show()

        epsfile=lensing.files.sample_file(type='binned-plots',
                                          sample=run1,
                                          name=name,
                                          extra='compare-%s-%02d' % (run2,i),
                                          ext='eps')
        print("writing:",epsfile)
        tab.write_eps(epsfile)
        if nbin > 1:
            key=raw_input('hit a key: ')
            if key.lower() == 'q':
                return