Пример #1
0
def make_table():
    import hickory
    tab = hickory.Table(
        nrows=4,
        ncols=4,
        constrained_layout=False,
        sharex=True,
        sharey=True,
        figsize=(16, 14),
        gridspec_kw={
            'wspace': 0.0,
            'hspace': 0.0
        },
    )
    # xlabel=r"$r$ [Mpc]"
    ylabel = r"$\Delta\Sigma ~ [\mathrm{M}_{\odot} \mathrm{pc}^{-2}]$"
    for i, j in ((0, 0), (1, 0), (2, 0), (3, 0)):
        tab[i, j].set(ylabel=ylabel)
    # for i, j in ((3, 0), (3, 1), (3, 2), (3, 3)):
    #     tab[i, j].set(ylabel=ylabel)

    for ax in tab.axes:
        # ax.set(
        #     xlabel=r"$r$ [Mpc]",
        #     ylabel=r"$\Delta\Sigma ~ [\mathrm{M}_{\odot} \mathrm{pc}^{-2}]$",
        #     xlim=(0.25, 185),
        #     ylim=(0.025, 45),
        # )
        ax.set_xscale('log')
        ax.set_yscale('log')

    return tab
def main():
    # xlabel='theta (arcmin)'
    xlabel = r'$R [\mathrm{Mpc}]$'
    ylab = r'$\Delta\Sigma^{\mathrm{Y1}}/\Delta\Sigma^{\mathrm{Y3}} - 1$'  # noqa
    y1, y3, nzs3, nzl1, nzl3 = read_data()

    xlim = 0.4, 200
    ylim = (-1, 3)

    meanlo = []
    meanlo_err = []
    meanhi = []
    meanhi_err = []

    for sbin in [1, 2, 3, 4]:

        # xlim = 1, 400
        tab = hickory.Table(
            figsize=(8, 6),
            nrows=2,
            ncols=2,
        )
        tab.suptitle('Y3 Sources, Different Lenses')
        tab[0, 0].set(
            xlim=xlim,
            ylim=ylim,
            ylabel=ylab,
        )
        tab[0, 1].set(
            xlim=xlim,
            ylim=ylim,
        )

        tab[1, 0].set(
            xlim=xlim,
            ylim=ylim,
            xlabel=xlabel,
            ylabel=ylab,
        )
        tab[1, 1].set(
            xlim=xlim,
            ylim=ylim,
            xlabel=xlabel,
        )

        tab[0, 0].set_xscale('log')
        tab[0, 1].set_xscale('log')
        tab[1, 0].set_xscale('log')
        tab[1, 1].set_xscale('log')

        res1 = plot_bin(
            plt=tab[0, 0],
            y1=y1,
            y3=y3,
            lbin=1,
            sbin=sbin,
            nzs3=nzs3,
            nzl1=nzl1,
            nzl3=nzl3,
            label='sbin %d, lbin 1' % sbin,
        )
        res2 = plot_bin(
            plt=tab[0, 1],
            y1=y1,
            y3=y3,
            lbin=2,
            sbin=sbin,
            nzs3=nzs3,
            nzl1=nzl1,
            nzl3=nzl3,
            label='sbin %d, lbin 2' % sbin,
        )
        res3 = plot_bin(
            plt=tab[1, 0],
            y1=y1,
            y3=y3,
            lbin=3,
            sbin=sbin,
            nzs3=nzs3,
            nzl1=nzl1,
            nzl3=nzl3,
            label='sbin %d, lbin 3' % sbin,
        )
        res4 = plot_bin(
            plt=tab[1, 1],
            y1=y1,
            y3=y3,
            lbin=4,
            sbin=sbin,
            nzs3=nzs3,
            nzl1=nzl1,
            nzl3=nzl3,
            label='sbin %d, lbin 4' % sbin,
        )

        meanlo += [res1[0], res2[0], res3[0], res4[0]]
        meanlo_err += [res1[1], res2[1], res3[1], res4[1]]

        meanhi += [res1[2], res2[2], res3[2], res4[2]]
        meanhi_err += [res1[3], res2[3], res3[3], res4[3]]

        # plt.set_yscale('log')
        # tab[0, 0].legend()
        # tab[0, 1].legend()
        # tab[0, 0].legend()
        # tab[1, 0].legend()
        # tab[1, 1].legend()
        pltname = 'fracdiff-y1-y3-sbin%d-y3sources.png' % sbin
        print('writing:', pltname)
        tab.savefig(pltname, dpi=150)
    # tab.show()

    meanlo = np.array(meanlo)
    meanlo_err = np.array(meanlo_err)
    meanhi = np.array(meanhi)
    meanhi_err = np.array(meanhi_err)

    meanlo, meanlo_err = eu.stat.wmom(meanlo,
                                      1.0 / meanlo_err**2,
                                      calcerr=True)
    meanhi, meanhi_err = eu.stat.wmom(meanhi,
                                      1.0 / meanhi_err**2,
                                      calcerr=True)

    print('overall meanlo: %g +/- %g' % (meanlo, meanlo_err))
    print('overall meanhi: %g +/- %g' % (meanhi, meanhi_err))
Пример #3
0
def get_amp(*, rmag, conc, show=False, output=None, get_plot=False):
    """
    get the amplitude of the stellar locus assuming a
    power law distribution A (mag - 13.5)**1.5  The data are binned
    and clipped, and sent to the calculate_amp function.

    Parameters
    ----------
    rmag: array
        psf magnitude, should be r band
    conc: array
        concentration parameters for stars
    show: bool
        If True, show a plot
    output: string, optional
        If sent, write a plot file with that name

    Returns
    -------
    amp, amp_err: float, float
        Amplitude and uncertainty
    """

    w, = np.where(between(rmag, MAGMIN, MAGMAX) & between(conc, CMIN, CMAX))

    hd = eu.stat.histogram(
        rmag[w],
        min=MAGMIN,
        max=MAGMAX,
        nbin=NBIN,
        more=True,
    )
    herr = np.sqrt(hd['hist'])

    amp, amp_err = calculate_amp(rmag=hd['center'], conc=hd['hist'])

    print('amp: %g +/- %g' % (amp, amp_err))

    if show or output is not None or get_plot:
        import hickory
        # alpha = 0.5
        plt = hickory.Table(2, 1, figsize=(8, 7))

        plt[0].plot(
            rmag,
            conc,
            marker='.',
            alpha=0.5,
        )
        plt[0].plot(
            rmag[w],
            conc[w],
            marker='.',
            markeredgecolor='black',
            alpha=0.5,
        )

        plt[0].set(xlim=(16, 20), ylim=(-0.002, 0.005))

        plt[0].set(
            xlabel='psf mag r',
            ylabel='conc',
        )

        predicted = predict(rmag=hd['center'], amp=amp)
        plt[1].errorbar(
            hd['center'],
            hd['hist'],
            yerr=herr,
        )
        plt[1].curve(hd['center'], predicted)
        xvals = np.linspace(13.9, 21)
        plt[1].curve(
            xvals,
            predict(rmag=xvals, amp=amp),
        )
        plt[1].set(
            xlabel='psf mag r',
            ylabel='Number',
            xlim=(13.5, 21),
        )

        if show:
            plt.show()

        if output is not None:
            print('writing:', output)
            plt.savefig(output)

    if get_plot:
        return amp, amp_err, plt
    else:
        return amp, amp_err
Пример #4
0
def plot_all_hist_gausserr(*, comb1, comb2, label1, label2, title):

    import hickory
    tab = hickory.Table(
        figsize=(8, 8),
        nrows=2,
    )
    tab.suptitle(title)

    for i, key in enumerate(['lm200', 'b']):

        errkey = '%s_err' % key
        vals1 = comb1[key]
        err1 = comb1[errkey]
        vals2 = comb2[key]
        err2 = comb2[errkey]

        mn1, mnerr1, msig1 = eu.stat.wmom(
            vals1, 1.0/err1**2,
            sdev=True,
        )
        mn2, mnerr2, msig2 = eu.stat.wmom(
            vals2, 1.0/err2**2,
            sdev=True,
        )
        # nsig = 8
        # minval = min(mn1 - nsig*msig1, mn2 - nsig*msig2)
        # maxval = max(mn1 + nsig*msig1, mn2 + nsig*msig2)

        nsig = 3
        minval = min(
            (vals1 - nsig*err1).min(),
            (vals2 - nsig*err2).min(),
        )
        maxval = max(
            (vals1 + nsig*err1).max(),
            (vals2 + nsig*err2).max(),
        )

        tab[i].set(xlabel=key)

        nbin = 10000
        x1, res1 = plot_hist_gausserr(
            plt=tab[i],
            centers=vals1,
            sigmas=err1,
            min=minval,
            max=maxval,
            nbin=nbin,
            label='%s %s' % (key, label1),
        )
        x2, res2 = plot_hist_gausserr(
            plt=tab[i],
            centers=vals2,
            sigmas=err2,
            min=minval,
            max=maxval,
            nbin=nbin,
            label='%s %s' % (key, label2),
        )

        # w, = np.where(
        #     (res1 > 0.01*res1.max()) |
        #     (res2 > 0.01*res2.max())
        # )
        # xlim = (x1[w].min(), x1[w].max())
        if key == 'lm200':
            xlim = (-13.5, 13.5)
        elif key == 'b':
            xlim = (-5, 15)
        else:
            raise ValueError('bad key')
        tab[i].set(
            xlim=xlim,
            ylim=(0, None),
        )

        tab[i].legend()

    return tab
Пример #5
0
def fit_exp(*, rmag, conc, show=False, output=None):
    """
    get the amplitude of the stellar locus assuming a
    power law distribution A (mag - 13.5)**1.5  The data are binned
    and clipped, and sent to the calculate_amp function.

    Parameters
    ----------
    rmag: array
        psf magnitude, should be r band
    conc: array
        concentration parameters for stars
    show: bool
        If True, show a plot
    output: string, optional
        If sent, write a plot file with that name

    Returns
    -------
    amp, amp_err: float, float
        Amplitude and uncertainty
    """

    w, = np.where(between(rmag, MAGMIN, MAGMAX) & between(conc, CMIN, CMAX))

    hd = eu.stat.histogram(
        rmag[w],
        min=MAGMIN,
        max=MAGMAX,
        nbin=NBIN,
        more=True,
    )

    num = hd['hist']
    num_err = np.sqrt(num)
    guess = [1000, 18, 4, num[:4].mean()]
    res = fit_exp_binned(hd['center'], num, num_err, guess)
    print_pars(res['pars'], front='pars: ')
    print_pars(res['pars_err'], front='pars: ')

    if res['flags'] != 0:
        raise RuntimeError('fit failed')

    if show or output is not None:
        import hickory
        # alpha = 0.5
        plt = hickory.Table(2, 1, figsize=(8, 7))

        plt[0].plot(
            rmag,
            conc,
            marker='.',
            alpha=0.5,
        )
        plt[0].plot(
            rmag[w],
            conc[w],
            marker='.',
            markeredgecolor='black',
            alpha=0.5,
        )

        plt[0].set(
            xlim=(MAGMIN - 0.5, MAGMAX + 0.5),
            ylim=(-0.0005, CMAX),
        )

        plt[0].set(
            xlabel='psf mag r',
            ylabel='conc',
        )

        plt[1].errorbar(
            hd['center'],
            num,
            yerr=num_err,
        )

        predicted = exp_func(res['pars'], hd['center'])

        eu.misc.colprint(num,
                         predicted,
                         num_err,
                         format='%20s',
                         names=['num', 'pred', 'num_err'])

        plt[1].curve(hd['center'], predicted)
        xvals = np.linspace(13.9, MAGMAX + 0.5)
        plt[1].curve(
            xvals,
            exp_func(res['pars'], xvals),
        )
        plt[1].set(
            xlabel='psf mag r',
            ylabel='Number',
            xlim=(MAGMIN - 0.5, MAGMAX + 0.5),
            yscale='log',
            # xlim=(13.5, MAGMAX+0.5),
        )

        if show:
            plt.show()

        if output is not None:
            print('writing:', output)
            plt.savefig(output)

    return res['pars']
Пример #6
0
def plot_fits_vs_rmag(data, type, dofits=False, show=False):
    """
    make a plot of gaussian mixture parameters vs the central
    magnitude of the bin
    """
    import hickory
    if type == 'star':
        start = 0
        end = 2
        label = 'stars'
    else:
        label = 'galaxies'
        start = 2
        end = 4

    tab = hickory.Table(
        nrows=2,
        ncols=2,
    )

    xlim = (15, 25)
    tab.suptitle(label, fontsize=15)
    tab[0, 0].set(xlabel='r mag', ylabel='weight', xlim=xlim)
    tab[0, 1].set(
        xlabel='r mag',
        ylabel='relweight',
        xlim=xlim,
    )
    tab[1, 0].set(
        xlabel='r mag',
        ylabel='mean',
        xlim=xlim,
    )

    tab[1, 1].set(
        xlabel='r mag',
        ylabel=r'$\sigma$',
        xlim=xlim,
    )

    centers = data['rmag']
    gmixes = data['gmix']

    wsums = gmixes['weight'][:, start:end].sum(axis=1)

    colors = ['#1f77b4', '#ff7f0e']
    for igauss in range(start, end):
        color = colors[igauss - start]

        weights = gmixes['weight'][:, igauss]
        relweights = weights / wsums

        means = gmixes['mean'][:, igauss]
        sigmas = gmixes['sigma'][:, igauss]

        tab[0, 0].plot(centers, weights, marker='o', markersize=2, color=color)
        tab[0, 1].plot(centers,
                       relweights,
                       marker='o',
                       markersize=2,
                       color=color)

        if dofits and type == 'gal':
            from .fitting import fit_erf, erf_func

            if igauss == 2:
                ftype = 'falling'
                guess = [0.6, 0.95, 20, 1]
            else:
                ftype = 'rising'
                guess = [0.05, 0.4, 20, 1]

            res = fit_erf(centers, relweights, guess, ftype)
            print(igauss, 'erf pars:', res['pars'])

            tab[0, 1].curve(
                centers,
                erf_func(res['pars'], centers, ftype),
                color='red',
            )

        tab[1, 0].plot(centers, means, marker='o', markersize=2, color=color)
        tab[1, 1].plot(centers, sigmas, marker='o', markersize=2, color=color)

        if not dofits:

            xinterp = np.linspace(centers[0], centers[-1], 1000)

            ystd = (smooth_data_hann3(weights) - weights).std()
            yinterp, ysigma = interp_gp(centers, weights, ystd, xinterp)
            tab[0, 0].curve(xinterp, yinterp, linestyle='solid', color=color)
            if True:
                ystd = (smooth_data_hann3(means) - means).std()
                yinterp, ysigma = interp_gp(centers, means, ystd, xinterp)
                tab[1, 0].curve(xinterp,
                                yinterp,
                                linestyle='solid',
                                color=color)
            ystd = (smooth_data_hann3(sigmas) - sigmas).std()
            if type == 'gal':
                ysend = smooth_data_hann3(sigmas)
            else:
                ysend = sigmas

            yinterp, ysigma = interp_gp(centers, ysend, ystd, xinterp)
            tab[1, 1].curve(xinterp, yinterp, linestyle='solid', color=color)

    if show:
        tab.show()

    return tab
Пример #7
0
def plot_purity(data, type, rng):
    """
    plot the cumulative contamination, e.g.

    intergral(nstar)/(integral(nstar) + integral(ngal))
    intergral(ngal)/(integral(nstar) + integral(ngal))
    """
    import scipy.stats
    import hickory

    tab = hickory.Table(
        figsize=(10, 7.5),
        nrows=4,
        ncols=4,
    )

    gmixes = data['gmix']
    ngauss = gmixes['mean'].shape[1]
    npts = 1000

    for rmagbin in range(data.size):

        label = r'$%.2f < r < %.2f$' % (
            data['rmagmin'][rmagbin],
            data['rmagmax'][rmagbin],
        )

        all_cdf = np.zeros(npts)
        this_cdf = np.zeros(npts)
        purity = np.zeros(npts)

        if type == 'star':
            minconc, maxconc = 0, 0.005
        else:
            minconc, maxconc = -0.001, 0.005

        num = 1000
        conc = np.linspace(minconc, maxconc, num)

        for igauss in range(ngauss):
            mean = gmixes['mean'][rmagbin, igauss]
            sigma = gmixes['sigma'][rmagbin, igauss]
            norm = gmixes['num'][rmagbin, igauss]

            n = scipy.stats.norm(loc=mean, scale=sigma)

            if type == 'star':
                vals = norm * n.cdf(conc)
            else:
                vals = norm * n.sf(conc)

            all_cdf += vals

            if type == 'star' and igauss < 2:
                this_cdf += vals
            elif type == 'gal' and igauss >= 2:
                this_cdf += vals

        w, = np.where(all_cdf > 0)
        purity[w] = this_cdf[w] / all_cdf[w]

        ylim = (
            0.9 * purity[w].min(),
            1.1,
        )
        ax = tab.axes[rmagbin]
        ax.set(
            title=label,
            xlabel='concentration',
            ylabel='%s purity' % type,
            ylim=ylim,
        )
        ax.axhline(1, color='black')
        ax.curve(conc[w], purity[w])

    tab.show()
Пример #8
0
    def plot3(self, *, label=None, show=False, file=None, dpi=100, **plot_kws):
        """
        plot the model and each component.  Optionally plot a set of
        data as well.  Currently only works for 1d

        Parameters
        ----------
        min: float
            Min value to plot, if data is sent then this can be left
            out and the min value will be gotten from that data.
        max: float
            Max value to plot, if data is sent then this can be left
            out and the max value will be gotten from that data.
        nbin: int, optional
            Optional number of bins for histogramming data
        binsize: float, optional
            Optional binsize for histogramming data
        file: str, optional
            Optionally write out a plot file
        dpi: int, optional
            Optional dpi for graphics like png, default 100
        show: bool, optional
            If True, show the plot on the screen

        Returns
        -------
        plot object
        """

        import hickory
        tab = hickory.Table(
            figsize=(10, 7.5),
            nrows=2,
            ncols=2,
        )
        tab[1, 1].axis('off')
        tab[0, 0].set(xlabel='concentration')
        tab[0, 1].set(xlabel='concentration')
        tab[1, 0].set(xlabel='concentration')

        tab[1, 1].ntext(0.5,
                        0.5,
                        label,
                        horizontalalignment='center',
                        verticalalignment='center',
                        fontsize=16)
        binsize = 0.0004
        binsize_coarse = 0.0004 * 2
        maxconc = 0.025

        self.plot(
            binsize=binsize,
            xlabel='concentration',
            legend=True,
            plt=tab[0, 0],
            show=False,
        )
        tab[0, 0].legend()

        star_samples = cem.gmix_sample(
            self.gmix,
            self.rng,
            size=1000,
            components=[0, 1],
        )

        smn, ssig = eu.stat.sigma_clip(star_samples)
        xlim = (smn - 4 * ssig, smn + 4 * ssig)
        star_binsize = ssig / 5

        tab[0, 1].set(xlim=xlim)
        self.plot(
            binsize=star_binsize,
            legend=True,
            xlim=xlim,
            plt=tab[0, 1],
            show=False,
        )

        cmin, cmax = 0.0005, maxconc
        self.plot(
            binsize=binsize_coarse,
            min=cmin,
            max=cmax,
            xlabel='concentration',
            legend=True,
            plt=tab[1, 0],
            show=False,
            xlim=(cmin, cmax),
        )

        if show:
            tab.show()

        if file is not None:
            print('writing:', file)
            tab.savefig(file, dpi=dpi)

        return tab
Пример #9
0
def plot_all(*, struct, type, show=False, output=None):

    if type == 'star':
        label = 'stars'
    else:
        label = 'galaxies'

    tab = hickory.Table(
        nrows=2,
        ncols=2,
    )

    tab[1, 1].axis('off')

    tab[0, 0].ntext(0.1, 0.5, label, verticalalignment='center')
    tab[0, 0].set(
        xlabel='r mag',
        ylabel='weight',
    )
    tab[0, 1].set(
        xlabel='r mag',
        ylabel='mean',
    )
    tab[1, 0].set(
        xlabel='r mag',
        ylabel=r'$\sigma$',
    )

    centers = struct['rmag_centers']
    ngauss = struct['%s_weights' % type].shape[1]

    for igauss in range(ngauss):

        weights = struct['%s_weights' % type][:, igauss]
        means = struct['%s_means' % type][:, igauss]
        sigmas = struct['%s_sigmas' % type][:, igauss]

        wmean = weights.mean()
        print(type, igauss, wmean)
        tab[0, 0].axhline(wmean, color='black')
        tab[0, 0].curve(centers, weights, marker='o', markersize=2)

        if type == 'gal':
            poly = np.poly1d(np.polyfit(centers, means, 2))
            print(poly)
            tab[0, 1].curve(centers, poly(centers), color='black')
        else:
            from .fitting import fit_exp, exp_func

            if igauss == 0:
                guess = [-1.0e-6, 16, 1]
            else:
                guess = [+1.0e-6, 16, 1]
            res = fit_exp(centers, means, guess)
            assert res['flags'] == 0
            print_pars(
                res['pars'], front='star mean exp pars: ',
                fmt='%.6g',
            )
            tab[0, 1].curve(centers, exp_func(res['pars'], centers),
                            color='black')

        tab[0, 1].curve(centers, means, marker='o', markersize=1.5)
        tab[1, 0].curve(centers, sigmas, marker='o', markersize=1.5)

    if show:
        tab.show()

    if output is not None:
        print('writing:', output)
        tab.savefig(output, dpi=100)