Exemplo n.º 1
0
def interpolate_y1_onto_y3(r3, r1, ds1):
    """
    interpolate the Y1 delta sigma measurements at the Y3 mean radii

    r3: array
        Array of Y3 radii
    r1: array
        Array of Y1 radii
    ds1: array
        Array of Y1 DeltaSigma

    Returns
    -------
    Delta Sigma interpolated to Y3 radii
    """
    ds1_interp = np.interp(
        r3, r1, ds1,
    )

    if False:
        import hickory
        plt = hickory.Plot()
        plt.plot(r1, ds1, label='y1 binning')
        plt.plot(r3, ds1_interp, label='interp')
        plt.set(
            xlabel=r'$R [\mathrm{Mpc}]$',
            ylabel=r'$\Delta\Sigma$',
        )
        plt.set_xscale('log')
        plt.set_yscale('log')
        plt.legend()
        plt.show()

    return ds1_interp
Exemplo n.º 2
0
    def plot_dsig(self, rmin=0.01, rmax=20.0, npts=1000):
        import hickory

        r = np.logspace(np.log10(rmin), np.log10(rmax), npts)
        dsig = self.get_dsig(r)

        plt = hickory.Plot()
        plt.curve(r, dsig)
        plt.set_xscale('log')
        plt.set_yscale('log')

        plt.show()
Exemplo n.º 3
0
    def plot_xi(self, r, xi):
        import hickory

        minval = 1.0e-4

        xi = np.where(xi < minval, minval, xi)

        plt = hickory.Plot()
        plt.set(
            xlabel=r"$r [Mpc/h]$",
            ylabel=r"$\xi_{lin}(r)$",
        )

        plt.set_xscale('log')
        plt.set_yscale('log')
        plt.curve(r, xi)

        plt.show()
Exemplo n.º 4
0
def jackknife_ratio(*, data1, weights1, data2, weights2, doplot=False):

    assert data1.size == data2.size
    nchunks = data1.size

    wsum1 = weights1.sum()
    wsum2 = weights2.sum()

    sum1 = (data1 * weights1).sum()
    sum2 = (data2 * weights2).sum()

    mn1 = sum1/wsum1
    mn2 = sum2/wsum2

    rat = mn1/mn2

    rats = np.zeros(data1.size)

    for i in range(nchunks):

        tsum1 = sum1 - data1[i] * weights1[i]
        tsum2 = sum2 - data2[i] * weights2[i]

        twsum1 = wsum1 - weights1[i]
        twsum2 = wsum2 - weights2[i]

        tmn1 = tsum1/twsum1
        tmn2 = tsum2/twsum2

        rats[i] = tmn1/tmn2

    fac = (nchunks-1)/float(nchunks)
    rat_var = fac*(((rat - rats)**2).sum())

    if doplot:
        import hickory
        plt = hickory.Plot()
        plt.hist((rats - rats.mean())*fac + rats.mean(), bins=20)
        plt.show()

    rat_err = np.sqrt(rat_var)
    return rat, rat_err
def interpolate_y1_onto_y3(r3, r1, ds1):
    ds1_interp = np.interp(
        r3,
        r1,
        ds1,
    )

    if False:
        plt = hickory.Plot()
        plt.plot(r1, ds1, label='y1 binning')
        plt.plot(r3, ds1_interp, label='interp')
        plt.set(
            xlabel=r'$R [\mathrm{Mpc}]$',
            ylabel=r'$\Delta\Sigma$',
        )
        plt.set_xscale('log')
        plt.set_yscale('log')
        plt.legend()
        plt.show()

    return ds1_interp
Exemplo n.º 6
0
def fit_conc_hists(*, data, prior_file, rmag_index, seed):

    rng = np.random.RandomState(seed)

    data = data[data['flags'] == 0]

    prior_data = fitsio.read(prior_file)

    edges = [
        (16.5, 17.0),
        (17.0, 17.5),
        (17.5, 18.0),
        (18.0, 18.5),

        # (16, 18.5),
        (18.5, 19.0),
        (19, 19.5),
        (19.5, 20.0),
        (20, 20.5),
        (20.5, 21.0),
        (21, 21.5),
        (21.5, 22.0),
        (22, 22.5),
        (22.5, 23),
        (23, 23.5),
        (23.5, 24.0),
        (24, 24.5),
    ]
    # edges = [
    #     (23, 23.5),
    #     (23.5, 24.0),
    #     (24, 24.5),
    # ]
    #

    rmag_centers = np.array(
        [0.5 * (rmagmin + rmagmax) for rmagmin, rmagmax in edges])

    # initial estimate of N(mag) for stars
    initial_amp, initial_amp_err = staramp.get_amp(
        rmag=data['psf_mag'][:, rmag_index],
        conc=data['conc'],
        show=True,
    )
    gal_num_pars = galamp.fit_exp(
        rmag=data['psf_mag'][:, rmag_index],
        conc=data['conc'],
        show=True,
    )

    init_nstar, init_nstar_err = staramp.predict(
        rmag=rmag_centers,
        amp=initial_amp,
        amp_err=initial_amp_err,
    )
    init_ngal = galamp.exp_func(gal_num_pars, rmag_centers)
    # init_ngal[0] = galamp.exp_func(gal_num_pars, 18.5)

    init_ngal_err = np.sqrt(init_ngal)

    # init_ngal_err *= 10
    # init_nstar_err *= 10

    ngal_list = np.zeros(len(edges))
    for i in range(len(edges)):
        rmagmin, rmagmax = edges[i]
        rmag = 0.5 * (rmagmin + rmagmax)
        label = 'rmag: [%.2f, %.2f]' % (rmagmin, rmagmax)

        binsize = 0.00005
        binsize_coarse = 0.0004
        binsize_coarser = 0.0004 * 2

        # off = 0.007
        # power = 0.5

        minconc, maxconc = -0.01, 0.025
        # minconc, maxconc = -0.0005, 0.01
        w, = np.where(
            between(data['psf_mag'][:, rmag_index], rmagmin, rmagmax)
            & between(data['conc'], minconc, maxconc))
        print('number in bin:', w.size)

        nstar_predicted = init_nstar[i]
        star_priors = get_star_priors(
            rng=rng,
            data=prior_data,
            rmag=rmag,
            nstar=nstar_predicted,
            nstar_err=init_nstar_err[i],
        )

        if rmag < 18.0:
            gal_ngauss = 1
        else:
            gal_ngauss = 2

            # print_pars(ngal_list[0:i], front='ngal_list: ')
            # gal_num_res = galamp.fit_exp_binned(
            #     rmag_centers[0:i],
            #     ngal_list[0:i],
            #     np.sqrt(ngal_list[0:i]),
            #     gal_num_pars,
            # )
            # print_pars(gal_num_pars, front='orig: ')
            # gal_num_pars = gal_num_res['pars']
            # print_pars(gal_num_pars, front='new: ')
            # init_ngal = galamp.exp_func(gal_num_pars, rmag_centers)
            # # init_ngal[0] = galamp.exp_func(gal_num_pars, 18.5)
            #
            # init_ngal_err = np.sqrt(init_ngal)

        # gal_ngauss = 2
        ngal_predicted = w.size - nstar_predicted
        if ngal_predicted < 3:
            ngal_predicted = init_ngal[i]
            if ngal_predicted < 3:
                ngal_predicted = 3

        gal_priors = get_gal_priors(
            rng=rng,
            data=prior_data,
            rmag=rmag,
            ngal=ngal_predicted,
            ngal_err=init_ngal_err[i],
            ngauss=gal_ngauss,
        )

        hd = eu.stat.histogram(
            data['conc'][w],
            min=minconc,
            max=maxconc,
            binsize=binsize,
            more=True,
        )
        plt = hickory.Plot(title=label, xlabel='concentration')
        plt.bar(hd['center'], hd['hist'], width=binsize)
        plt.show()
        hd_coarse = eu.stat.histogram(
            data['conc'][w],
            min=minconc,
            max=maxconc,
            binsize=binsize_coarse,
            more=True,
        )
        hd_coarser = eu.stat.histogram(
            data['conc'][w],
            min=minconc,
            max=maxconc,
            binsize=binsize_coarser,
            more=True,
        )

        x = hd['center']
        y = hd['hist']
        yerr = np.sqrt(hd['hist'])
        fitter = Fitter(
            x=x,
            y=y,
            yerr=yerr,
            star_priors=star_priors,
            gal_priors=gal_priors,
            rng=rng,
        )

        fitter.go()

        res = fitter.result
        print_pars(res['pars'], front='pars: ')
        print_pars(res['pars_err'], front='perr: ')
        pars = res['pars']

        nstar_meas = pars[2] + pars[5]
        if gal_ngauss == 1:
            nobj_meas = pars[2] + pars[5] + pars[8]
            ngal_meas = res['pars'][-1]
        else:
            nobj_meas = pars[2] + pars[5] + pars[8] + pars[11]
            ngal_meas = res['pars'][-1] + res['pars'][-4]

        ngal_list[i] = ngal_meas
        print('true num: %g meas num: %g' % (w.size, nobj_meas))
        print('nstar pred: %g nstar meas: %g' % (nstar_predicted, nstar_meas))
        print('ngal pred: %g ngal meas: %g' % (ngal_predicted, ngal_meas))

        if res['flags'] == 0:
            fitter.plot(
                figsize=(10, 7.5),
                x=hd_coarse['center'],
                y=hd_coarse['hist'],
                xlabel='concentration',
                legend=True,
                show=True,
                title=label,
                xlim=(-0.005, 0.025),
            )

            splt = fitter.plot(
                figsize=(10, 7.5),
                legend=True,
                show=False,
                title=label,
                xlim=(-0.003, 0.003),
            )
            splt.show()

            cmin, cmax = 0.003, 0.025
            w, = np.where(between(hd_coarser['center'], cmin, cmax))
            ylim = [0, 1.1 * hd_coarser['hist'][w].max()]

            fitter.plot(
                figsize=(10, 7.5),
                x=hd_coarser['center'],
                y=hd_coarser['hist'],
                xlabel='concentration',
                legend=True,
                show=True,
                title=label,
                xlim=(0, cmax),
                ylim=ylim,
            )
Exemplo n.º 7
0
    def plot(self,
             *,
             x=None,
             y=None,
             components=None,
             min=None,
             max=None,
             npts=None,
             data=None,
             nbin=None,
             binsize=None,
             file=None,
             dpi=100,
             show=False,
             **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.
        npts: int, optional
            Number of points to use for the plot.  If data are sent you
            can leave this off and a suitable value will be chosen based
            on the data binsize
        data: array, optional
            Optional data to plot as a histogram
        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

        pars = self.result['pars']

        plt = hickory.Plot(**plot_kws)

        dx_orig = self.x[1] - self.x[0]

        if x is not None and y is not None:
            dx_data = x[1] - x[0]
            fac = dx_data / dx_orig
        elif x is not None or y is not None:
            raise ValueError('send both x and y')
        else:
            x = self.x
            y = self.y
            dx_data = dx_orig
            fac = 1

        plt.bar(
            x,
            y,
            label='data',
            width=dx_data,
            alpha=0.5,
            color='#a6a6a6',
        )

        model = fac * self.eval(
            pars,
            components=components,
            x=x,
        )

        plt.curve(x, model, label='model')

        if components is None:
            components = range(self.ngauss)

        npars_per = 3
        for i in components:
            start = i * npars_per
            end = (i + 1) * npars_per

            tpars = pars[start:end]
            tmodel = fac * self.eval_one(tpars, x=x)

            label = 'component %d' % i
            plt.curve(x, tmodel, label=label)

        if show:
            plt.show()

        if file is not None:
            plt.savefig(file, dpi=dpi)

        return plt
Exemplo n.º 8
0
def plot_gmix(
    *,
    gmix,
    min=None,
    max=None,
    npts=None,
    data=None,
    nbin=None,
    binsize=None,
    file=None,
    dpi=100,
    show=False,
    plt=None,
    **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.
    npts: int, optional
        Number of points to use for the plot.  If data are sent you
        can leave this off and a suitable value will be chosen based
        on the data binsize
    data: array, optional
        Optional data to plot as a histogram
    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 esutil as eu
    import hickory

    if plt is None:
        plt = hickory.Plot(**plot_kws)

    if data is not None:

        dx_data = binsize

        if min is None:
            min = data.min()
        if max is None:
            max = data.max()

        if npts is None:
            dx_model = dx_data / 10
            npts = int((max - min) / dx_model)

        xvals = np.linspace(
            min,
            max,
            npts,
        )
        dx_model = xvals[1] - xvals[0]

        wdata, = np.where((data > min) & (data < max))

        if wdata.size == 0:
            dsum = 0.0
        else:
            hd = eu.stat.histogram(
                data,
                min=min,
                max=max,
                nbin=nbin,
                binsize=binsize,
                more=True,
            )
            dsum = hd['hist'].sum()

            plt.bar(hd['center'],
                    hd['hist'],
                    label='data',
                    width=dx_data,
                    alpha=0.5,
                    color='#a6a6a6')

    else:
        if npts is None:
            raise ValueError('send npts if not sending data')
        if min is None:
            raise ValueError('send min if not sending data')
        if max is None:
            raise ValueError('send max if not sending data')

        xvals = np.linspace(min, max, npts)

    predicted = gmix_eval(gmix, xvals)

    if data is not None:
        psum = predicted.sum()
        if dsum > 0:
            fac = dsum / psum * dx_data / dx_model
        else:
            fac = dx_data / dx_model
    else:
        fac = 1.0

    plt.curve(xvals, fac * predicted, label='model')
    for i in range(gmix.size):
        predicted = gauss_eval(gmix[i], xvals)

        label = 'component %d' % i
        plt.curve(xvals, fac * predicted, label=label)

    if show:
        plt.show()

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

    return plt
Exemplo n.º 9
0
def plot_residuals(
    *,
    r,
    dsig,
    dsigcov,
    z,
    m200=None,
    lm200=None,
    b=None,
    xlim=None,
    ylim=None,
    resid_axis_kw={},
    no_resid_xticklabels=False,
    no_resid_yticklabels=False,
    plt=None,
    show=False,
):

    import hickory

    if b is None:
        withlin = False
    else:
        withlin = True

    fitter = NFWBiasFitter(z=z, r=r, withlin=withlin)
    yfit = fitter.get_dsig(r=r, m200=m200, lm200=lm200, b=b)

    dsigerr = np.sqrt(np.diag(dsigcov))

    ymin, ymax = [
        0.5 * (dsig - dsigerr).min(),
        1.5 * (dsig + dsigerr).max(),
    ]
    if ymin < 0:
        ymin = 0.1

    if ylim is None:
        ylim = [0.5 * ymin, 1.5 * ymax]

    if xlim is None:
        rmin, rmax = r.min(), r.max()
        xlim = [0.5 * rmin, 1.5 * rmax]

    if plt is None:
        dolegend = True
        plt = hickory.Plot(
            xlabel=r"$r$ [Mpc]",
            ylabel=r"$\Delta\Sigma ~ [\mathrm{M}_{\odot} \mathrm{pc}^{-2}]$",
            xlim=xlim,
            ylim=ylim,
        )
        if 'ylabel' not in resid_axis_kw:
            resid_axis_kw['ylabel'] = r'$\Delta$'

        plt.set_xscale('log')
        plt.set_yscale('log')
    else:
        plt.set(xlim=xlim, ylim=ylim)
        dolegend = False

    alpha = 0.5
    _residuals_plots(
        plt=plt,
        x=r,
        y=dsig,
        yerr=dsigerr,
        model=yfit,
        resid_axis_kw=resid_axis_kw,
        data_kw={'alpha': alpha},
        model_kw={'label': 'model'},
        no_resid_xticklabels=no_resid_xticklabels,
        no_resid_yticklabels=no_resid_yticklabels,
    )

    if withlin:
        yfit_lin = fitter.get_lin_dsig(r=r, b=b)
        yfit_nfw = fitter.get_nfw_dsig(r=r, m200=m200, lm200=lm200)
        plt.curve(r, yfit_nfw, label='nfw')
        plt.curve(r, yfit_lin, label='linear')

    if dolegend:
        plt.legend()

    if show:
        plt.show()

    return plt
Exemplo n.º 10
0
def plot(
    *,
    r,
    z,
    m200=None,
    lm200=None,
    b=None,
    dsig=None,
    dsigcov=None,
    xlim=None,
    ylim=None,
    plt=None,
    show=False,
):

    import hickory

    if b is None:
        withlin = False
    else:
        withlin = True

    fitter = NFWBiasFitter(z=z, r=r, withlin=withlin)
    yfit = fitter.get_dsig(r=r, m200=m200, lm200=lm200, b=b)

    if dsig is not None or dsigcov is not None:
        assert dsigcov is not None, "send both dsig and dsigcov"
        assert dsig is not None, "send both dsig and dsigcov"
        dsigerr = np.sqrt(np.diag(dsigcov))

        ymin, ymax = [
            0.5 * (dsig - dsigerr).min(),
            1.5 * (dsig + dsigerr).max(),
        ]
        if ymin < 0:
            ymin = 0.1
    else:
        ymin, ymax = [0.5 * yfit.min(), 1.5 * yfit.max()]

    if ylim is None:
        ylim = [0.5 * ymin, 1.5 * ymax]

    if xlim is None:
        rmin, rmax = r.min(), r.max()
        xlim = [0.5 * rmin, 1.5 * rmax]

    if plt is None:
        dolegend = True
        plt = hickory.Plot(
            xlabel=r"$r$ [$h^{-1}$ Mpc]",
            ylabel=r"$\Delta\Sigma ~ [h \mathrm{M}_{\odot} \mathrm{pc}^{-2}]$",
            xlim=xlim,
            ylim=ylim,
        )
        plt.set_xscale('log')
        plt.set_yscale('log')
    else:
        dolegend = False

    alpha = 0.5
    if dsig is not None:
        plt.errorbar(r, dsig, dsigerr, alpha=alpha)

    plt.curve(r, yfit, label='model')
    if withlin:
        yfit_lin = fitter.get_lin_dsig(r=r, b=b)
        yfit_nfw = fitter.get_nfw_dsig(r=r, m200=m200, lm200=lm200)
        plt.curve(r, yfit_nfw, label='nfw')
        plt.curve(r, yfit_lin, label='linear')

    if dolegend:
        plt.legend()

    if show:
        plt.show()

    return plt