Example #1
0
    def _plot_shear(self, show=False):
        import biggles
        from biggles import plot

        tab=biggles.Table(2,1)

        tab[0,0]=plot(self.s1grid,
                      self.lnp,
                      xlabel='g1',
                      ylabel='log(p)',
                      visible=False)
        p=numpy.exp(self.lnp-self.lnp.max())
        tab[1,0]=plot(self.s1grid,
                      p,
                      xlabel='g1',
                      ylabel='p',
                      visible=False)

        pngfile=files.get_psample_summed_url(self['run'],
                                             self['is2n'], 
                                             itrial=self['itrial'],
                                             ext='png')
        print("writing:",pngfile)
        tab.write_img(800,800,pngfile)

        if show:
            tab.show()
        return tab
Example #2
0
    def add_scinv(self, show=False):
        import fitsio
        import zphot
        from . import sigmacrit
        import biggles

        zsvals = self.get_zsvals()
        cosmo=self['cosmo']
        scalc = sigmacrit.ScinvCalculator(self['zlmin'],
                                          self['zlmax'],
                                          self['nzl'], 
                                          zsvals[0],
                                          zsvals[-1],
                                          H0=cosmo['H0'],
                                          omega_m=cosmo['omega_m'])

        n_zlens=scalc.nzl
        
        data = self.read_original()
        dt=data.dtype.descr
        new_dtype=[]
        for d in dt:
            if d[0] != 'pofz':
                new_dtype.append(d)

        new_dtype += [('scinv','f8',n_zlens)]

        out = numpy.zeros(data.size, dtype=new_dtype)
        eu.numpy_util.ahelp(out)

        print("copying common fields")
        eu.numpy_util.copy_fields(data, out)


        print('adding scinv to each')
        for i in xrange(data.size):
            if ((i+1) % 1000) == 0:
                print("%s/%s  %.1f%%" % (i+1,data.size,float(i+1)/data.size*100.))

            out['scinv'][i,:] = scalc.calc_mean_scinv(zsvals, data['pofz'][i])

            if show:
                biggles.plot(scalc.zlvals, out['scinv'][i], type='solid')
                key=raw_input('hit a key (q to quit): ')
                if key=='q':
                    return

        h=copy.deepcopy(cosmo)
        h['zlvals'] = list(scalc.zlvals)

        outf=self.original_file(with_scinv=True)
        print("writing scinv file:",outf)

        fitsio.write(outf, out, header=h, clobber=True)
Example #3
0
def view_profile(image, **keys): 
    """
    View the image as a radial profile vs radius from the center.

    If show=False just return the plot object.

    Values below zero are clipped, so pre-subtract any background as
    necessary.

    parameters
    ----------
    image or r,g,b: ndarray
        The image(s) as a 2-d array or 
    cen: [c1,c2], optional
        Optional center
    show: bool
        Set False to not show the image in an X window.
    file: string
        Write the image to the intput file name.  .png will be written
        as a png file, else an eps file.
    width, height: integers
        Size for output
    **kw:
        keywords for the FramedPlot and for the output image dimensions
        width
    """
    import biggles

    plt=keys.pop('plt',None)
    show=keys.pop('show',True)
    cen=keys.pop('cen',None)

    if cen is None:
        cen=(numpy.array(image.shape)-1.0)/2.0
    else:
        assert len(cen)==2,"cen must have two elements"

    rows,cols=numpy.mgrid[
        0:image.shape[0],
        0:image.shape[1],
    ]

    rows = rows.astype('f8')-cen[0]
    cols = cols.astype('f8')-cen[1]

    r = numpy.sqrt(rows**2 + cols**2).ravel()
    s = r.argsort()
    r=r[s]
    imravel = image.ravel()[s]

    #pts = biggles.Points(r, imravel, size=size, type=type, **keys)
    keys['visible']=False
    plt = biggles.plot(r, imravel, plt=plt, **keys)

    _writefile_maybe(plt, **keys)
    _show_maybe(plt, show=show,**keys)

    return plt
Example #4
0
    def _get_fracdev_guess(self, fitter):
        tests=self.fracdev_tests
        lnps=zeros(tests.size)
        for i in xrange(tests.size):
            lnps[i] = fitter.calc_lnprob(tests[i:i+1])

        w,=where(isfinite(lnps))
        if w.size == 0:
            return None

        if False:
            from biggles import plot
            plot(tests[w], lnps[w])
            key=raw_input('hit a key: ')

        ibest=lnps[w].argmax()
        guess=tests[w[ibest]]
        return guess
Example #5
0
def view_profile(image, **keys): 
    """
    View the image as a radial profile vs radius from the center.

    If show=False just return the plot object.

    Values below zero are clipped, so pre-subtract any background as
    necessary.

    parameters
    ----------
    image or r,g,b: ndarray
        The image(s) as a 2-d array or 
    cen: [c1,c2], optional
        Optional center
    show: bool
        Set False to not show the image in an X window.
    file: string
        Write the image to the intput file name.  .png will be written
        as a png file, else an eps file.
    width, height: integers
        Size for output
    **kw:
        keywords for the FramedPlot and for the output image dimensions
        width
    """
    import biggles

    plt=keys.pop('plt',None)
    show=keys.pop('show',True)
    cen=keys.pop('cen',None)
    keys['xlabel']=keys.get('xlabel','radius')

    r, pim = get_profile(image, cen=cen)

    keys['visible']=False
    plt = biggles.plot(r, pim, plt=plt, **keys)

    _writefile_maybe(plt, **keys)
    _show_maybe(plt, show=show, **keys)

    return plt
Example #6
0
    def plot_fits(self):

        means=self.means
        if means.size == 1:
            return
        else:
            import biggles
            biggles.configure('default','fontsize_min',1.5)

            fits=self.fits
            args=self.args
            #Q=calc_q(fits)

            if args.yrange is not None:
                yrange=[float(r) for r in args.yrange.split(',')]
            else:
                yrange=[-0.01,0.01]

            xrng=args.xrange
            if xrng is not None:
                xrng=[float(r) for r in args.xrange.split(',')]

            tab=biggles.Table(1,2)
            tab.aspect_ratio=0.5

            diff = means['shear'] - means['shear_true']

            plts=[]
            for i in [0,1]:

                x = means['shear_true'][:,i]
                plt =biggles.plot(
                    x,
                    diff[:,i],
                    xlabel=r'$\gamma_{%d}$ true' % (i+1,),
                    ylabel=r'$\Delta \gamma_{%d}$' % (i+1,),
                    yrange=yrange,
                    xrange=xrng,
                    visible=False,
                )
                yfit=fits['m'][0,i]*x + fits['c'][0,i]

                z=biggles.Curve(x, x*0, color='black')
                c=biggles.Curve(x, yfit, color='red')
                plt.add(z,c)

                '''
                mstr='m%d: %.2g +/- %.2g' % (i+1,fits['m'][0,i],fits['merr'][0,i])
                cstr='c%d: %.2g +/- %.2g' % (i+1,fits['c'][0,i],fits['cerr'][0,i])
                mlab=biggles.PlotLabel(0.1,0.9,
                                       mstr,
                                       halign='left')
                clab=biggles.PlotLabel(0.1,0.85,
                                       cstr,
                                       halign='left')
                plt.add(mlab,clab)
                '''
                if False and i==0:
                    Qstr='Q: %d' % (int(Q),)
                    Qlab=biggles.PlotLabel(0.1,0.8,
                                           Qstr,
                                           halign='left')
                    plt.add(Qlab)


                tab[0,i] = plt

            fname=self._get_fit_plot_file()
            eu.ostools.makedirs_fromfile(fname)
            print("writing:",fname)
            tab.write_eps(fname)

            if args.show:
                tab.show(width=1000, height=1000)
Example #7
0
def plot_results(trials, **keys):
    """
    Plot the points and histograms of trials from an MCMC chain.
    """
    import biggles
    import esutil
    from esutil.numpy_util import where1, between

    npars = trials.shape[1]

    fontsize_min = keys.get("fontsize_min", 1)
    biggles.configure("default", "fontsize_min", fontsize_min)
    weights = keys.get("weights", None)

    nbin = keys.get("nbin", 35)
    names = keys.get("names", None)
    show = keys.get("show", True)

    nsigma = keys.get("nsigma", None)

    means, cov = extract_stats(trials, weights=weights)
    errs = sqrt(diag(cov))

    plt = biggles.Table(npars, 2)

    ind = numpy.arange(trials.shape[0])

    for i in xrange(npars):
        if names is not None:
            name = names[i]
        else:
            name = r"$p_{%d}$" % i

        use_trials = trials[:, i]
        use_ind = ind
        use_wts = weights
        if nsigma is not None:
            w = where1(between(trials[:, i], means[i] - nsigma * errs[i], means[i] + nsigma * errs[i]))
            use_trials = trials[w, i]
            use_ind = ind[w]
            if weights is not None:
                use_wts = weights[w]

        burn_plot_i = biggles.plot(use_ind, use_trials, type="solid", xlabel="step", ylabel=name, visible=False)
        plt[i, 0] = burn_plot_i

        hcurve, bin_edges, harray = biggles.make_histc(use_trials, nbin=nbin, weights=use_wts, get_hdata=True)

        plti = biggles.FramedPlot()

        plti.xlabel = name

        hmax = harray.max()
        plti.yrange = [-0.05 * hmax, 1.2 * hmax]

        plti.add(hcurve)

        lab = r"$<%s> = %0.4g \pm %0.4g$" % (name, means[i], errs[i])
        plab = biggles.PlotLabel(0.1, 0.8, lab, halign="left", color="blue")

        plti.add(plab)

        plt[i, 1] = plti

    plt.title = keys.get("title", None)

    if show:
        plt.show()

    return plt
Example #8
0
    def doplot(self,args):
        import biggles

        means=self.means
        fits=self.fits

        Q=calc_q(fits)

        if args.yrange is not None:
            yrange=[float(r) for r in args.yrange.split(',')]
        else:
            yrange=[-0.005,0.005]

        xrng=args.xrange
        if xrng is not None:
            xrng=[float(r) for r in args.xrange.split(',')]

        tab=biggles.Table(1,2)
        tab.aspect_ratio=0.5

        diff = means['shear'] - means['shear_true']
        differr = means['shear_err']

        plts=[]
        for i in [0,1]:

            x = means['shear_true'][:,i]
            plt =biggles.plot(
                x,
                diff[:,i],
                yerr=differr[:,i],
                xlabel='shear%d true' % (i+1,),
                ylabel='shear%d diff' % (i+1,),
                yrange=yrange,
                xrange=xrng,
                visible=False,
            )
            yfit=fits['m'][0,i]*x + fits['c'][0,i]

            c=biggles.Curve(x, yfit, color='red')
            z=biggles.Curve(x, x*0, color='black')
            plt.add(c,z)

            mstr='m%d: %.2g +/- %.2g' % (i+1,fits['m'][0,i],fits['merr'][0,i])
            cstr='c%d: %.2g +/- %.2g' % (i+1,fits['c'][0,i],fits['cerr'][0,i])
            mlab=biggles.PlotLabel(0.1,0.9,
                                   mstr,
                                   halign='left')
            clab=biggles.PlotLabel(0.1,0.85,
                                   cstr,
                                   halign='left')
            plt.add(mlab,clab)
            if i==0:
                Qstr='Q: %d' % (int(Q),)
                Qlab=biggles.PlotLabel(0.1,0.8,
                                       Qstr,
                                       halign='left')
                plt.add(Qlab)


            tab[0,i] = plt

        fname=files.get_fit_file(self['run'],
                                 extra='fit-m-c',
                                 ext='eps')
        eu.ostools.makedirs_fromfile(fname)
        print("writing:",fname)
        tab.write_eps(fname)

        if args.show:
            tab.show(width=1000, height=1000)
Example #9
0
    def plot_fits(self):

        means = self.means
        if means.size == 1:
            return
        else:
            import biggles
            biggles.configure('default', 'fontsize_min', 1.5)

            fits = self.fits
            args = self.args
            #Q=calc_q(fits)

            if args.yrange is not None:
                yrange = [float(r) for r in args.yrange.split(',')]
            else:
                yrange = [-0.01, 0.01]

            xrng = args.xrange
            if xrng is not None:
                xrng = [float(r) for r in args.xrange.split(',')]

            tab = biggles.Table(1, 2)
            tab.aspect_ratio = 0.5

            diff = means['shear'] - means['shear_true']

            plts = []
            for i in [0, 1]:

                x = means['shear_true'][:, i]
                plt = biggles.plot(
                    x,
                    diff[:, i],
                    xlabel=r'$\gamma_{%d}$ true' % (i + 1, ),
                    ylabel=r'$\Delta \gamma_{%d}$' % (i + 1, ),
                    yrange=yrange,
                    xrange=xrng,
                    visible=False,
                )
                yfit = fits['m'][0, i] * x + fits['c'][0, i]

                z = biggles.Curve(x, x * 0, color='black')
                c = biggles.Curve(x, yfit, color='red')
                plt.add(z, c)
                '''
                mstr='m%d: %.2g +/- %.2g' % (i+1,fits['m'][0,i],fits['merr'][0,i])
                cstr='c%d: %.2g +/- %.2g' % (i+1,fits['c'][0,i],fits['cerr'][0,i])
                mlab=biggles.PlotLabel(0.1,0.9,
                                       mstr,
                                       halign='left')
                clab=biggles.PlotLabel(0.1,0.85,
                                       cstr,
                                       halign='left')
                plt.add(mlab,clab)
                '''
                if False and i == 0:
                    Qstr = 'Q: %d' % (int(Q), )
                    Qlab = biggles.PlotLabel(0.1, 0.8, Qstr, halign='left')
                    plt.add(Qlab)

                tab[0, i] = plt

            fname = self._get_fit_plot_file()
            eu.ostools.makedirs_fromfile(fname)
            print("writing:", fname)
            tab.write_eps(fname)

            if args.show:
                tab.show(width=1000, height=1000)
Example #10
0
def make_summed_pofz(scat_vers, tilenames=None):
    """
    match the dg scat to scinv using a Matcher

    parameters
    ----------
    scat_vers: string
        name of a source catalog version e.g. scat-006
    tilenames: list, optional
        subset to process, for testing
    """
    import fitsio

    pz_sum_colname = get_pz_sum_colname(scat_vers)
    print("will write to column: '%s'" % pz_sum_colname)

    xi = XShearInput(scat_vers)
    cols = pz.open_pz_columns(xi["pz_vers"], xi["pz_type"])

    zvals = cols["zgrid"][:]
    pzsum = zeros(zvals.size)

    if tilenames is None:
        tilenames = get_tilenames(xi["scat_table"])

    ntile = len(tilenames)
    ntile_use = 0
    nuse = 0
    first = True

    for i, tilename in enumerate(tilenames):
        print("-" * 70)
        print("processing tile %d/%d: %s" % (i + 1, ntile, tilename))

        data = xi.read_input_data(tilename)
        if data is None:
            # not all tiles are present
            continue

        w = xi.select(data)

        indices = data["coadd_objects_id"][w]

        matches = cols["index"].match(indices)
        nmatches = matches.size
        print("    matches: %d/%d" % (nmatches, indices.size))

        pofz = cols["pofz"][matches]

        good = zeros(nmatches, dtype="i8")
        for mi in xrange(nmatches):
            wnan, = where(isnan(pofz[mi, :]))
            if wnan.size == 0:
                good[mi] = 1

        wgood, = where(good)
        if wgood.size > 0:
            print("    not NaN: %d/%d" % (wgood.size, nmatches))
            pofz = pofz[wgood, :]

            pzsum += pofz.sum(axis=0)

        ntile_use += 1
        nuse += wgood.size

        if False and i > 5:
            from biggles import plot

            plot(zvals, pzsum)
            break

    print("finally used: %d/%d tiles" % (ntile_use, ntile))
    print("summed %d p(z)" % nuse)

    print("writing to column: '%s'" % pz_sum_colname)
    cols.write_column(pz_sum_colname, pzsum, create=True)