예제 #1
0
파일: sigmacrit.py 프로젝트: esheldon/espy
    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]))
예제 #2
0
파일: sigmacrit.py 프로젝트: esheldon/espy
    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]))