示例#1
0
def create_plot_binary(dist=100.0, num_sys=100, bins=25):
    """ Create a set of random binaries and plot the distribution
    of resulting theta vs pm

    Parameters
    ----------
    dist : float
        Distance to the population for angular (rather than physical) units (pc)
    num_sys : float
        Number of random binaries to generate
    bins : int
        Number of bins for contours

    Returns
    -------
    None
    """

    global binary_set

    if binary_set is None or len(binary_set) != num_sys:
        generate_binary_set(num_sys=num_sys, dist=dist)


    fig, ax1 = plt.subplots(1,1, figsize=(6,4))

    # Plot limits
    xmin, xmax = 0.0, 5000.0
    ymin, ymax = 0.0, 3.0
    ax1.set_xlim(xmin, xmax)
    ax1.set_ylim(ymin, ymax)

    # Plot labels
    ax1.set_xlabel("Projected separation (AU)")
    ax1.set_ylabel("Proper motion difference (km/s)")

    # Plot distribution
    contourf_kwargs = {'bins':bins}
    corner.hist2d(binary_set['proj_sep']*c.Rsun_to_cm/c.AU_to_cm, binary_set['pm'], nbins=bins,
                    range=([xmin,xmax],[ymin,ymax]), **contourf_kwargs)

    # Add angular separation at dist axis
    ax2 = ax1.twiny()
    xticks = np.linspace(xmin,xmax,6)
    angles = (xticks * c.AU_to_cm)/(dist * c.pc_to_cm) * (180.0 * 3600.0 / np.pi)
    ax2.set_xticks(angles)
    ax2.set_xlabel('Angular separation at distance of ' + str(dist) + ' pc (arcsec)')

    # Add proper motion at dist axis
    ax3 = ax1.twinx()
    yticks = np.linspace(ymin, ymax, 7)
    def pm_at_dist(pm, dist=100.0):
        return (pm * 1.0e5)/(dist * c.pc_to_cm) * (1.0e3 * 180.0 * 3600.0 / np.pi) * c.day_to_sec*365.25

    ax3.set_ylim(0.0, pm_at_dist(ax1.get_ylim()[1], dist=dist))
    ax3.set_ylabel('Proper motion at distance of ' + str(dist) + ' pc (mas/yr)')

    plt.tight_layout()
    plt.show()
示例#2
0
def _run_hist2d(nm, N=50000, seed=1234, **kwargs):
    # Generate some fake data.
    np.random.seed(seed)
    x = np.random.randn(N)
    y = np.random.randn(N)

    fig, ax = pl.subplots(1, 1, figsize=(8, 8))
    corner.hist2d(x, y, ax=ax, **kwargs)
示例#3
0
def test_Mh_SFR(run, theta, nsnap0=15, sigma_smhm=0.2, downsampled='14'):
    ''' Take a look at the M_h SFR relationship 
    '''
    subcat_sim = abcee.model(run, theta, 
            nsnap0=nsnap0, sigma_smhm=sigma_smhm, downsampled=downsampled) 

    isSF = np.where(subcat_sim['gclass'] == 'sf') 

    fig = plt.figure()
    sub = fig.add_subplot(111)
    DFM.hist2d(subcat_sim['halo.m'][isSF], subcat_sim['sfr'][isSF], weights=subcat_sim['weights'][isSF], 
            levels=[0.68, 0.95], range=[[10., 15.], [-4., 2.]], color='#1F77B4', 
            plot_datapoints=True, fill_contours=False, plot_density=False, ax=sub) 
    plt.show() 
示例#4
0
def _run_hist2d(nm, N=50000, seed=1234, **kwargs):
    print(" .. {0}".format(nm))

    if not os.path.exists(FIGURE_PATH):
        os.makedirs(FIGURE_PATH)

    # Generate some fake data.
    np.random.seed(seed)
    x = np.random.randn(N)
    y = np.random.randn(N)

    fig, ax = pl.subplots(1, 1, figsize=(8, 8))
    corner.hist2d(x, y, ax=ax, **kwargs)
    fig.savefig(os.path.join(FIGURE_PATH, "hist2d_{0}.png".format(nm)))
    pl.close(fig)
示例#5
0
def _run_hist2d(nm, N=50000, seed=1234, **kwargs):
    print(" .. {0}".format(nm))

    if not os.path.exists(FIGURE_PATH):
        os.makedirs(FIGURE_PATH)

    # Generate some fake data.
    np.random.seed(seed)
    x = np.random.randn(N)
    y = np.random.randn(N)

    fig, ax = pl.subplots(1, 1, figsize=(8, 8))
    corner.hist2d(x, y, ax=ax, **kwargs)
    fig.savefig(os.path.join(FIGURE_PATH, "hist2d_{0}.png".format(nm)))
    pl.close(fig)
示例#6
0
def whatsThatFeature(survey='2MASS',
                     quantile=0.05,
                     dataFilename='All.npz',
                     iter='10th',
                     ngauss=128):
    if survey == '2MASS':
        mag1 = 'J'
        mag2 = 'K'
        absmag = 'J'
        xlabel = r'$(J-K_s)^C$'
        ylabel = r'$M_J^C$'
        xlim = [-0.25, 1.25]
        ylim = [6, -6]
    dustEBV = 0.0
    tgas, twoMass, Apass, bandDictionary, indices = testXD.dataArrays()
    color = testXD.colorArray(mag1, mag2, dustEBV, bandDictionary)
    file = 'posteriorParallax.' + str(ngauss) + 'gauss.dQ' + str(
        quantile) + '.' + iter + '.' + survey + '.' + dataFilename
    data = np.load(file)
    parallax = data['mean']
    notnans = ~np.isnan(parallax)
    parallax = parallax[notnans]
    absMagKinda, apparentMagnitude = testXD.absMagKindaArray(
        absmag, dustEBV, bandDictionary, tgas['parallax'])
    apparentMagnitudeGood = apparentMagnitude[notnans]
    absMagKinda = parallax * 10.**(0.2 * apparentMagnitudeGood)
    figFeature, axFeature = plt.subplots()
    x = color[notnans]
    y = testXD.absMagKinda2absMag(absMagKinda)
    im = corner.hist2d(x,
                       y,
                       ax=axFeature,
                       levels=None,
                       bins=200,
                       no_fill_contours=True,
                       plot_density=False,
                       color=contourColor,
                       rasterized=True,
                       plot_contours=False)
    axFeature.set_xlim(xlim)
    axFeature.set_ylim(ylim)
    axFeature.set_xlabel(xlabel)
    axFeature.set_ylabel(ylabel)
    lowerMainSequence = (0.45, 5.5)
    upperMainSequence = (-0.225, 2)
    binarySequence = (0.75, 4)
    redClump = (0.35, -2)
    redGiantBranch = (1.0, -2)
    turnOff = (0.0, 3.5)
    features = [
        lowerMainSequence, upperMainSequence, binarySequence, redClump,
        redGiantBranch, turnOff
    ]
    labels = [
        'lower MS', 'upper MS', 'binary sequence', 'red clump', 'RGB',
        'MS turn off', 'subgiant branch'
    ]
    for l, f in zip(labels, features):
        axFeature.text(f[0], f[1], l, fontsize=15)
    figFeature.savefig('whatsThatFeature.pdf', format='pdf')
示例#7
0
def plot_beam_pos(postdata, frb_data, radius, bins=50, color="blue"):
    xposmin = -2; xposmax =  2;
    yposmin = -2; yposmax =  2;
    beamx     = frb_data.xpos_new
    beamy     = frb_data.ypos_new
    beam_name = frb_data.beam

    fig     = plt.figure(figsize=(8, 8))
    main_ax = plt.axes((0.1, 0.1, 0.6, 0.6))                 # [left, bottom, width, height]

    corner.hist2d(postdata[:,0], postdata[:,1], bins=bins, color=color, plot_datapoints=False, \
                  smooth=True, plot_density=True, plot_contours=False, ax=main_ax)

    for ibeam in range(len(beamx)):
        circ = plt.Circle((beamx[ibeam], beamy[ibeam]), radius=radius[ibeam], fill=False, \
                           linestyle="--", ec="k")
        main_ax.annotate("beam " + beam_name[ibeam], xy=(beamx[ibeam], beamy[ibeam]), \
                          verticalalignment='center', horizontalalignment='center')
        main_ax.add_patch(circ)

    main_ax.set_xlim(xposmin, xposmax)
    main_ax.set_ylim(yposmin, yposmax)
    plt.xlabel(r'$\Delta \alpha \, Cos (\delta)$ (deg)', size="large")
    plt.ylabel(r'$\Delta \delta $ (deg)', size="large")

    postx_ax = plt.axes((0.1, 0.7, 0.6, 0.2), sharex=main_ax)
    postx_ax.hist(postdata[:,0], bins=bins, density=True, edgecolor='blue', lw=0.5)
    postx_ax.set_xlim(xposmin, xposmax)
    plt.ylabel(r'$\rho $ (arb.)', size="large")
    postx_ax.get_yaxis().set_ticks([])
    plt.setp(postx_ax.xaxis.get_ticklabels(), visible=False)

    posty_ax = plt.axes((0.7, 0.1, 0.2, 0.6), sharey=main_ax)
    posty_ax.hist(postdata[:,1], bins=bins, orientation="horizontal", normed=True, \
                  edgecolor='blue', lw=0.5)
    posty_ax.set_ylim(yposmin, yposmax)
    plt.xlabel(r'$\rho $ (arb.)', size="large")
    posty_ax.get_xaxis().set_ticks([])
    plt.setp(posty_ax.yaxis.get_ticklabels(), visible=False)

    return fig
示例#8
0
def obs_tauV(): 
    ''' examine optical depth tauV(Mstar) for SDSS data assuming face on (theta=0) slab dust model
    tau_V = A_V / 1.086
    '''
    # read data 
    dat_dir = os.environ['GALPOPFM_DIR']
    fsdss = os.path.join(dat_dir, 'obs', 'tinker_SDSS_centrals_M9.7.valueadd.hdf5') 
    sdss = h5py.File(fsdss, 'r') 
    # get attenuation A_V 
    AV_sdss     = sdss['AV'][...] 
    mstar_sdss  = sdss['MASS'][...] * 0.7**2 

    # there should be some cut here 
    cut = AV_sdss > 0. 
    AV_sdss     = AV_sdss[cut]
    mstar_sdss  = mstar_sdss[cut]
    
    # get tauV 
    tauV = AV_sdss/1.086     
    sensible_tau = (tauV > 0.) & (tauV < 10.) 

    import corner as DFM 
    fig = plt.figure()
    sub = fig.add_subplot(111) 
    DFM.hist2d(np.log10(mstar_sdss)[sensible_tau], tauV[sensible_tau], color='k', 
            levels=[0.68, 0.95], range=[[8.5, 11.], [0., 4.]], 
            plot_datapoints=True, fill_contours=False, plot_density=True, 
            ax=sub) 
    # overplot tauV(msta) within our prior 
    marr = np.linspace(8, 12, 20) 
    _tauv = (marr-10.) + 1.5
    sub.plot(marr, _tauv, c='C1', ls='--') 

    sub.set_xlabel(r'$\log\,M_*$', fontsize=20) 
    sub.set_ylabel(r'$\tau_V = A_V/1.086$', fontsize=20) 
    fig.savefig(os.path.join(dat_dir, 'obs', 'sdss.tauV_mstar.png'), bbox_inches='tight') 
    return None 
示例#9
0
def CMplot(table,r,g,ext='mag_forced_cmodel', axis=None,plotpoints = True,**kwargs):
    """ plot color-magnitude diagram for a sample of galaxies given in table. """
    
    y = table[g+ext] - table[r+ext]
    x = table[r+ext]
    if axis is None:
        axis = [[np.nanmin(x), np.nanmax(x)], [np.nanmin(y), np.nanmax(y)]]    
    if plotpoints:
        obj = plt.scatter(x, y,s=1,**kwargs)
        plt.axis(axis.flatten())
    else:
        obj = corner.hist2d(x, y,range=axis,bins=100,plot_datapoints=False,smooth=0.5,levels=[0.683,0.955])
    plt.xlabel(r + ' [mag]')
    plt.ylabel(g + ' - ' + r + ' [mag]')
    return obj
示例#10
0
def walkers(nwalkers,
            x,
            y,
            model,
            theta,
            conditions=None,
            var2=0.01,
            mov=100,
            d=1,
            tol=1. * 10**-3,
            mode=False):
    result = []
    error = []
    par = []
    for i in range(nwalkers):
        theta, nwalk, y0 = walk(x, y, model, theta, conditions, var2, mov, d,
                                tol, mode)
        par.append(theta)
        nwalk = np.array(nwalk).reshape((len(nwalk), len(nwalk[i])))
        error.append(y0)
        for k in range(nwalk.shape[1]):
            sub = '$\\theta _{' + str(k) + '}$'
            plt.plot(range(len(nwalk[:, k])), nwalk[:, k], '-', label=sub)
            plt.ylabel('$\\theta$')
            plt.xlabel('iterations')
            #plt.legend()
    plt.show()
    if (nwalk.shape[1] == 2):
        fig = corner.hist2d(nwalk[:, 0],
                            nwalk[:, 1],
                            range=None,
                            bins=18,
                            smooth=True,
                            plot_datapoints=True,
                            plot_density=True)
        plt.ylabel('$\\theta_{1}$')
        plt.xlabel('$\\theta_{0}$')
        plt.savefig("1401.png")
    return (par, error)
示例#11
0
    mpl.rcParams['ytick.minor.width'] = 1.2
    mpl.rcParams['ytick.major.pad'] = 8
    mpl.rcParams['ytick.labelsize'] = 'large'
    mpl.rcParams['font.family'] = 'sans-serif'
    mpl.rcParams['font.weight'] = 500
    mpl.rcParams['font.size'] = 16
    # if we want to plot gamma-Rc and p-Rout in two panels
    # from matplotlib import gridspec
    # fig = plt.figure(figsize=(10, 5))
    # gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1])
    # ax = plt.subplot(gs[0]), plt.subplot(gs[1])

    fig, ax = plt.subplots(1, 1, figsize=(5, 5))
    levels = 1.-np.exp(-0.5*np.arange(1., 3.1, 1)**2.)  # correspond to 1, 2 and 3 sigma for a 2D hystogram

    hist2d(flatchain[:, 0], flatchain[:, 2], ax=ax, bins=30, levels=levels, plot_datapoints=False,
            smooth=1.5, plot_contours=True, fill_contours=True, plot_density=False, color='#2D89E5')

    # add point for best-fit model
    ax.plot(bestfit_pars[0], bestfit_pars[2], marker='*',
     markerfacecolor='#FFFF66', markeredgecolor='#FFFF66', markersize=16)

    if headers['fit']['sigma_prescription'] == 'g':
        ax.set_xlabel(r'$\gamma$', fontsize=20)
        ax.set_ylabel(r'$R_c$', fontsize=20)
    elif headers['fit']['sigma_prescription'] == 'pl':
        ax.set_xlabel(r'$p$', fontsize=20)
        ax.set_ylabel(r'$R_{\mathrm{out}}$', fontsize=20)

    # set axes limits
    ax.set_ylim(0, 300.)
    yticks = np.arange(0, 301, 100, dtype='int')
示例#12
0
def plot_dterms(modelinfo,
                station,
                burnin=0,
                print_dterms=True,
                levels=None,
                smooth=1.0):
    """ Plot right and left D-terms for a single station

       Args:
           modelinfo (dict): dmc modelinfo dictionary
           station (str): the station whose D-terms to plot
           burnin (int): length of burn-in
           print_dterms (bool): flag to print the D-terms and uncertainties on the plot
           levels (list): a list of contour levels to plot
           smooth (float): amount by which to KDE smooth the histograms
           
       Returns:
           dtermplot: figure containing the D-term plot

    """

    # contour levels
    if levels is None:
        levels = [
            1.0 - np.exp(-0.5 * (1.1775**2.0)),
            1.0 - np.exp(-0.5 * (2.146**2.0)),
            1.0 - np.exp(-0.5 * (3.035**2.0))
        ]

    ###################################################
    # organize chain info

    trace = modelinfo['trace']
    stations = modelinfo['stations']

    # get index for requested station
    istat = (stations == station)

    # read D-terms
    Dterm_reals_R = trace['right_Dterm_reals'][burnin:]
    Dterm_reals_L = trace['left_Dterm_reals'][burnin:]
    Dterm_imags_R = trace['right_Dterm_imags'][burnin:]
    Dterm_imags_L = trace['left_Dterm_imags'][burnin:]

    # remove divergences
    div_mask = np.invert(trace[burnin:].diverging)
    Dterm_reals_R = Dterm_reals_R[div_mask]
    Dterm_reals_L = Dterm_reals_L[div_mask]
    Dterm_imags_R = Dterm_imags_R[div_mask]
    Dterm_imags_L = Dterm_imags_L[div_mask]

    ###################################################
    # make figure

    dtermplot = plt.figure(figsize=(6, 6))
    ax = dtermplot.add_axes([0.18, 0.18, 0.8, 0.8])

    # add axis lines
    ax.plot([-10, 10], [0, 0], 'k--', alpha=0.5, zorder=-10)
    ax.plot([0, 0], [-10, 10], 'k--', alpha=0.5, zorder=-10)

    # plot contours
    corner.hist2d(Dterm_reals_R[:, istat],
                  Dterm_imags_R[:, istat],
                  fig=dtermplot,
                  levels=levels,
                  color='cornflowerblue',
                  fill_contours=True,
                  plot_datapoints=False,
                  plot_density=False,
                  plot_contours=True,
                  smooth=smooth)
    corner.hist2d(Dterm_reals_L[:, istat],
                  Dterm_imags_L[:, istat],
                  fig=dtermplot,
                  levels=levels,
                  color='salmon',
                  fill_contours=True,
                  plot_datapoints=False,
                  plot_density=False,
                  plot_contours=True,
                  smooth=smooth)

    # axis labels
    ax.set_xlabel('Real part')
    ax.set_ylabel('Imaginary part')

    # axis ranges
    limit = np.max(
        np.array([
            np.max(np.abs(Dterm_reals_R[:, istat])),
            np.max(np.abs(Dterm_imags_R[:, istat])),
            np.max(np.abs(Dterm_reals_L[:, istat])),
            np.max(np.abs(Dterm_imags_L[:, istat]))
        ]))
    ax.set_xlim(-1.1 * limit, 1.1 * limit)
    ax.set_ylim(-1.1 * limit, 1.1 * limit)
    for tick in ax.get_xticklabels():
        tick.set_rotation(45)
        tick.set_ha('right')

    # print out D-term values and uncertainties
    if print_dterms:
        realpart_R = np.percentile(Dterm_reals_R[:, istat], 50.0)
        realpart_R_lo = np.percentile(Dterm_reals_R[:, istat],
                                      50.0) - np.percentile(
                                          Dterm_reals_R[:, istat], 15.87)
        realpart_R_hi = np.percentile(Dterm_reals_R[:, istat],
                                      84.13) - np.percentile(
                                          Dterm_reals_R[:, istat], 50.0)

        realpart_L = np.percentile(Dterm_reals_L[:, istat], 50.0)
        realpart_L_lo = np.percentile(Dterm_reals_L[:, istat],
                                      50.0) - np.percentile(
                                          Dterm_reals_L[:, istat], 15.87)
        realpart_L_hi = np.percentile(Dterm_reals_L[:, istat],
                                      84.13) - np.percentile(
                                          Dterm_reals_L[:, istat], 50.0)

        imagpart_R = np.percentile(Dterm_imags_R[:, istat], 50.0)
        imagpart_R_lo = np.percentile(Dterm_imags_R[:, istat],
                                      50.0) - np.percentile(
                                          Dterm_imags_R[:, istat], 15.87)
        imagpart_R_hi = np.percentile(Dterm_imags_R[:, istat],
                                      84.13) - np.percentile(
                                          Dterm_imags_R[:, istat], 50.0)

        imagpart_L = np.percentile(Dterm_imags_L[:, istat], 50.0)
        imagpart_L_lo = np.percentile(Dterm_imags_L[:, istat],
                                      50.0) - np.percentile(
                                          Dterm_imags_L[:, istat], 15.87)
        imagpart_L_hi = np.percentile(Dterm_imags_L[:, istat],
                                      84.13) - np.percentile(
                                          Dterm_imags_L[:, istat], 50.0)

        str1 = r'$\rm{Re}(D_R) = ' + str(np.round(
            realpart_R, 5)) + r'_{-' + str(
                np.round(realpart_R_lo, 5)) + r'}^{+' + str(
                    np.round(realpart_R_hi, 5)) + r'}$'
        str2 = r'$\rm{Im}(D_R) = ' + str(np.round(
            imagpart_R, 5)) + r'_{-' + str(
                np.round(imagpart_R_lo, 5)) + r'}^{+' + str(
                    np.round(imagpart_R_hi, 5)) + r'}$'
        str3 = r'$\rm{Re}(D_L) = ' + str(np.round(
            realpart_L, 5)) + r'_{-' + str(
                np.round(realpart_L_lo, 5)) + r'}^{+' + str(
                    np.round(realpart_L_hi, 5)) + r'}$'
        str4 = r'$\rm{Im}(D_L) = ' + str(np.round(
            imagpart_L, 5)) + r'_{-' + str(
                np.round(imagpart_L_lo, 5)) + r'}^{+' + str(
                    np.round(imagpart_L_hi, 5)) + r'}$'
        strhere = str1 + '\n' + str2 + '\n' + str3 + '\n' + str4
        ax.text(-1.05 * limit,
                1.05 * limit,
                strhere,
                ha='left',
                va='top',
                fontsize=8)

    return dtermplot
示例#13
0
    def plot_corner(samples,
                    labels=("dt", 'dm'),
                    bnames=('', ),
                    axhist=None,
                    **kwargs):
        """
        To plot  a data set in a multi-dimensional space.

        Parameters
        ----------
        :param samples: The samples. This should be a 1- or 2-dimensional array. For a 1-D
        array this results in a simple histogram. For a 2-D array, the zeroth
        axis is the list of samples and the next axis are the dimensions of
        the space (see corner.corner).
        :param labels: the labels  Default: ("td", 'dm')
        :param bnames: the band names. Used for error labels
        :param bins
        :param axhist: axes to plot hist2d
        :return: fig
        """
        # from matplotlib import pyplot as plt
        import corner
        # plt.hist(sample_dt, bins=50, histtype="stepfilled", alpha=0.3)
        # # samples = sampler.chain[:, nburn:, :].reshape((-1, ndim))
        # fig = corner.corner(samples, labels=["$delay$", "$dm$", "$lnf$"],
        #                     truths=[dt, dm, lnf])
        lw = kwargs.get('lw', 2)
        bins = kwargs.get('bins', 30)
        alpha = kwargs.get('alpha', 1.)
        istex = kwargs.get('istex', False)
        title_fmt = kwargs.get('title_fmt', '.2f')
        quantiles = kwargs.get('quantiles', [0.16, 0.5, 0.84])
        figsize = kwargs.get('figsize', (16, 12))
        verbose = kwargs.get('verbose', False)

        if isinstance(labels, str):
            labels = [labels]

        if istex:
            labels += tuple(r'$\sigma_m(\mathrm{{{}}})$'.format(bn)
                            for bn in bnames)
        else:
            labels += tuple('sig+{}'.format(bn) for bn in bnames)
        hist_kwargs = dict(lw=lw, alpha=0.5)
        fig = corner.corner(samples,
                            labels=labels,
                            bins=bins,
                            hist_kwargs=hist_kwargs,
                            quantiles=quantiles,
                            show_titles=True,
                            title_fmt=title_fmt,
                            verbose=verbose,
                            title_kwargs={"fontsize": 12},
                            label_kwargs={
                                'labelpad': 1,
                                'fontsize': 14
                            },
                            fontsize=8)

        fig.set_size_inches(figsize)

        if axhist is not None:
            corner.hist2d(samples[:, 0], samples[:, 1], ax=axhist)

        return fig
示例#14
0
def explore_distances(name):
    '''
    '''
    dat_dir = os.environ['GALPOPFM_DIR']
    if name == 'simba': 
        fhdf5 = os.path.join(dat_dir, 'sed', 'simba.hdf5') 
    else: 
        raise NotImplementedError
    f = h5py.File(fhdf5, 'r') 
    sed = {} 
    sed['wave']         = f['wave'][...]
    sed['sed_neb']      = f['sed_neb'][...]
    sed['sed_noneb']    = f['sed_noneb'][...]
    sed['sed_onlyneb']  = sed['sed_neb'] - sed['sed_noneb'] # only nebular emissoins 
    sed['logmstar']     = f['logmstar'][...]
    sed['censat']       = f['censat'][...]
    f.close() 

    # only keep centrals
    cens = sed['censat'].astype(bool) 

    theta = np.array([1., 1.5, 1./0.44]) 
    sed_dusty = dustFM.Attenuate(
            theta, 
            sed['wave'], 
            sed['sed_noneb'][cens,:], 
            sed['sed_onlyneb'][cens,:], 
            sed['logmstar'][cens],
            dem='slab_calzetti') 
    
    # magnitudes
    F_mag_dust = measureObs.AbsMag_sed(sed['wave'], sed_dusty, band='galex_fuv') 
    N_mag_dust = measureObs.AbsMag_sed(sed['wave'], sed_dusty, band='galex_nuv') 
    R_mag_dust = measureObs.AbsMag_sed(sed['wave'], sed_dusty, band='r_sdss') 
    FUV_NUV_dust = F_mag_dust - N_mag_dust 
    #a_fuv_dust = measureObs.A_FUV(F_mag, N_mag, R_mag) 

    wlim = (sed['wave'] > 4e3) & (sed['wave'] < 7e3) 

    # balmer measurements 
    Ha_dust, Hb_dust = measureObs.L_em(['halpha', 'hbeta'], sed['wave'][wlim], sed_dusty[:,wlim]) 
    HaHb_dust = Ha_dust/Hb_dust

    # read in SDSS measurements 
    fsdss = os.path.join(dat_dir, 'obs', 'tinker_SDSS_centrals_M9.7.valueadd.hdf5') 
    sdss = h5py.File(fsdss, 'r') 
    
    F_mag_sdss = sdss['ABSMAG'][...][:,0]
    N_mag_sdss = sdss['ABSMAG'][...][:,1]
    R_mag_sdss = sdss['ABSMAG'][...][:,4]
    FUV_NUV_sdss = F_mag_sdss - N_mag_sdss
    #a_fuv_sdss = measureObs.A_FUV(F_mag_sdss, N_mag_sdss, R_mag_sdss) 
    #print(a_fuv)
    #print(a_fuv_sdss)

    Haflux_sdss = sdss['HAFLUX'][...]
    Hbflux_sdss = sdss['HBFLUX'][...]
    Ha_sdss = Haflux_sdss * (4.*np.pi * (sdss['Z'][...] * 2.9979e10/2.2685e-18)**2) * 1e-17
    Hb_sdss = Hbflux_sdss * (4.*np.pi * (sdss['Z'][...] * 2.9979e10/2.2685e-18)**2) * 1e-17
    HaHb_sdss = Ha_sdss/Hb_sdss 

    HaHb_I = 2.86 # intrinsic balmer ratio 

    fig = plt.figure(figsize=(11,5))

    # Mr - Balmer ratio
    sub = fig.add_subplot(121)

    DFM.hist2d(R_mag_sdss, np.log10(HaHb_sdss/HaHb_I), color='k', 
            levels=[0.68, 0.95], range=[[-15, -24], [-0.1, 0.5]], 
            plot_datapoints=True, fill_contours=False, plot_density=False, 
            ax=sub) 
    DFM.hist2d(R_mag_dust, np.log10(HaHb_dust/HaHb_I), color='C1', 
            levels=[0.68, 0.95], range=[[-15, -24], [-0.1, 0.5]], 
            plot_datapoints=True, fill_contours=False, plot_density=False, 
            ax=sub) 
    #subscatter(R_mag_sdss, np.log10(HaHb_sdss/HaHb_I), c='k', s=0.1, label='SDSS')
    #sub.scatter(R_mag_dust, np.log10(HaHb_dust/HaHb_I), c='C1', s=0.05, label='SIMBA dust')
    rmid_sdss, med_sdss = dustInfer.median_alongr(R_mag_sdss, np.log10(HaHb_sdss/HaHb_I), rmin=-16, rmax=-24, nbins=16)
    rmid_dust, med_dust = dustInfer.median_alongr(R_mag_dust, np.log10(HaHb_dust/HaHb_I), rmin=-16, rmax=-24, nbins=16)
    print(rmid_sdss, med_sdss) 
    sub.scatter(rmid_sdss, med_sdss, c='k', s=30, marker='x', label='SDSS')
    sub.scatter(rmid_dust, med_dust, c='C1', s=30, marker='x', label='SIMBA dust')
    sub.legend(loc='upper left', fontsize=15, handletextpad=0.2) 
    sub.set_xlabel(r'$M_r$', fontsize=20) 
    sub.set_xlim(-15, -24) 
    sub.set_ylabel(r'$\log (H_\alpha/H_\beta)/(H_\alpha/H_\beta)_I$', fontsize=20) 
    sub.set_ylim(-0.1, 0.5) 
    
    # Mr - A_FUV
    sub = fig.add_subplot(122)
    DFM.hist2d(R_mag_sdss, FUV_NUV_sdss, color='k', 
            levels=[0.68, 0.95], range=[[-15, -24], [-0.5, 2.5]], 
            plot_datapoints=True, fill_contours=False, plot_density=False, 
            ax=sub) 
    DFM.hist2d(R_mag_dust, FUV_NUV_dust, color='C1', 
            levels=[0.68, 0.95], range=[[-15, -24], [-0.5, 2.5]], 
            plot_datapoints=True, fill_contours=False, plot_density=False, 
            ax=sub) 
    rmid_sdss, med_sdss = dustInfer.median_alongr(R_mag_sdss, FUV_NUV_sdss, rmin=-16, rmax=-24, nbins=16)
    rmid_dust, med_dust = dustInfer.median_alongr(R_mag_dust, FUV_NUV_dust, rmin=-16, rmax=-24, nbins=16)
    sub.scatter(rmid_sdss, med_sdss, c='k', s=30, marker='x')
    sub.scatter(rmid_dust, med_dust, c='C1', s=30, marker='x')
    sub.set_xlabel(r'$M_r$', fontsize=20) 
    sub.set_xlim(-16, -24) 
    sub.set_ylabel(r'$FUV - NUV$', fontsize=20) 
    sub.set_ylim(-0.5, 2.5) 
    ffig = fhdf5.replace('.hdf5', '.explore_distance.png')
    fig.savefig(ffig, bbox_inches='tight') 

    # -- kNN divergence ---
    #from skl_groups.features import Features
    #from skl_groups.divergences import KNNDivergenceEstimator

    #X_sdss = np.vstack([R_mag_sdss, FUV_NUV_sdss, np.log10(HaHb_sdss/HaHb_I)]).T
    #X_dust = np.vstack([R_mag_dust, FUV_NUV_dust, np.log10(HaHb_dust/HaHb_I)]).T
    #        
    #kNN = KNNDivergenceEstimator(div_funcs=['kl'], Ks=5, version='slow', clamp=False, n_jobs=1)
    #feat = Features([X_sdss, X_dust])
    #div_knn = kNN.fit_transform(feat)
    #print(div_knn[0][0][0][1]) 
    return None 
示例#15
0
plt.rc('font', family='serif')

direc = '/users/annaho/Data/LAMOST/Label_Transfer'
f = pyfits.open("%s/table_for_paper.fits" %direc)
a = f[1].data
f.close()

feh = a['cannon_m_h']
am = a['cannon_alpha_m']
snr = a['snrg']

choose = snr > 20
print(sum(choose))
print(len(choose))

fig, ax = plt.subplots(1,1, sharex=True, sharey=True, figsize=(8,5))
hist2d(feh[choose], am[choose], ax=ax, bins=100, range=[[-2.2,.9],[-0.2,0.5]])
ax.set_xlabel("[Fe/H] (dex)" + " from Cannon/LAMOST", fontsize=16)
fig.text(
        0.04, 0.5, r"$\mathrm{[\alpha/M]}$" + " (dex) from Cannon/LAMOST", 
        fontsize=16, va = 'center', rotation='vertical')
label = r"Objects with SNR \textgreater 20"
props = dict(boxstyle='round', facecolor='white')
ax.tick_params(axis='x', labelsize=16)
ax.tick_params(axis='y', labelsize=16)
ax.text(0.05, 0.85, label, 
        horizontalalignment='left', verticalalignment='bottom', 
        transform=ax.transAxes, fontsize=16, bbox=props)
#plt.show()
plt.savefig("feh_alpha.png")
示例#16
0
def plot_mcmc_chain_with_prior(directory,
                               use_prior=False,
                               only_first_star=True,
                               plot_true_parameters=True,
                               plot_only_SSP_parameter=True):
    '''
	This routine takes the output from 'restructure_chain' function and plots the result in a corner plot
	set_scale and use_scale can be used to put different PDFs on the same scale, in the sense that the plot is shown with the same axis range.
	
	In the paper this is used to plot the Posterior in comparison to the prior distribution.
	'''
    how_many_ssp_parameters = 3

    if plot_true_parameters:
        true_parameters = [-2.37, -2.75, -1.2]
        true_parameters = np.array(true_parameters)

    if use_prior:
        from Chempy.cem_function import gaussian
        prior = [[-2.3, 0.3], [-2.75, 0.3], [-0.8, 0.3], [-0.3, 0.3],
                 [0.55, 0.1], [0.5, 0.1]]
        prior = np.array(prior)

    import corner
    plt.clf()
    text_size = 16
    cor_text = 22
    plt.rc('font', family='serif', size=text_size)
    plt.rc('xtick', labelsize=text_size)
    plt.rc('ytick', labelsize=text_size)
    plt.rc('axes', labelsize=text_size, lw=1.0)
    plt.rc('lines', linewidth=1)
    plt.rcParams['ytick.major.pad'] = '8'
    plt.rcParams['text.latex.preamble'] = [r"\usepackage{libertine}"]
    params = {
        'text.usetex': True,
        'font.size': 10,
        'font.family': 'libertine',
        'text.latex.unicode': True,
    }
    plt.rcParams.update(params)
    positions = np.load('%sposteriorPDF.npy' % (directory))
    parameter_names = np.load("%sparameter_names.npy" % (directory))
    posterior = np.load('%sposteriorvalues.npy' % (directory))
    positions_max = np.load('%sbest_parameter_values.npy' % (directory))

    if only_first_star:
        positions = positions[:, :6]
        parameter_names = parameter_names[:6]
        positions_max = positions_max[0][:6]
    if plot_only_SSP_parameter:
        positions = positions[:, :how_many_ssp_parameters]
        parameter_names = parameter_names[:how_many_ssp_parameters]
        positions_max = positions_max[:how_many_ssp_parameters]

    nparameter = len(positions[0])
    cor_matrix = np.zeros(shape=(nparameter, nparameter))
    for i in range(nparameter):
        for j in range(nparameter):
            cor_matrix[i, j] = np.corrcoef(
                (positions[:, i], positions[:, j]))[1, 0]
    np.save('%scor_matrix' % (directory), cor_matrix)

    #fig, axes = plt.subplots(nrows=nparameter, ncols=nparameter,figsize=(14.69,8.0), dpi=300)#,sharex=True, sharey=True)

    fig, axes = plt.subplots(nrows=nparameter,
                             ncols=nparameter,
                             figsize=(5.69, 3.0),
                             dpi=300)  #,sharex=True, sharey=True)

    left = 0.1  # the left side of the subplots of the figure
    right = 0.925  # the right side of the subplots of the figure
    bottom = 0.075  # the bottom of the subplots of the figure
    top = 0.97  # the top of the subplots of the figure
    wspace = 0.0  # the amount of width reserved for blank space between subplots
    hspace = 0.0  # the amount of height reserved for white space between subplots
    plt.subplots_adjust(left=left,
                        bottom=bottom,
                        right=right,
                        top=top,
                        wspace=wspace,
                        hspace=hspace)

    alpha = 0.5
    alpha_more = 0.8
    alpha_less = 0.1
    lw = 2

    for i in range(nparameter):
        for j in range(nparameter):
            axes[i, j].locator_params(nbins=4)
            if j == 1:
                axes[i, j].locator_params(nbins=4)
            if i == j:
                counts, edges = np.histogram(positions[:, j], bins=20)
                max_count = float(np.max(counts))
                counts = np.divide(counts, max_count)
                axes[i, j].bar(edges[:-1],
                               align='edge',
                               height=counts,
                               width=edges[1] - edges[0],
                               color='grey',
                               alpha=alpha,
                               linewidth=0,
                               edgecolor='blue')

                if use_prior:
                    xmin = prior[i][0] - 3.5 * prior[i][1]
                    xmax = prior[i][0] + 3.5 * prior[i][1]
                    x_axe = np.linspace(xmin, xmax, 100)
                    y_axe = gaussian(x_axe, prior[i][0], prior[i][1])
                    axes[i, j].set_xlim((xmin, xmax))
                    axes[i, j].plot(x_axe,
                                    y_axe / max(y_axe),
                                    c="k",
                                    linestyle='--',
                                    alpha=1,
                                    lw=lw)
                else:
                    axes[i, j].set_xlim(min(positions[:, j]),
                                        max(positions[:, j]))
                axes[i, j].set_ylim(0, 1.05)
                if j != 0:
                    plt.setp(axes[i, j].get_yticklabels(), visible=False)

                axes[i, j].vlines(np.percentile(positions[:, j], 15.865),
                                  axes[i, j].get_ylim()[0],
                                  axes[i, j].get_ylim()[1],
                                  color='k',
                                  alpha=alpha,
                                  linewidth=lw,
                                  linestyle='dashed')
                axes[i, j].vlines(np.percentile(positions[:, j], 100 - 15.865),
                                  axes[i, j].get_ylim()[0],
                                  axes[i, j].get_ylim()[1],
                                  color='k',
                                  alpha=alpha,
                                  linewidth=lw,
                                  linestyle='dashed')
                axes[i, j].vlines(np.percentile(positions[:, j], 50),
                                  axes[i, j].get_ylim()[0],
                                  axes[i, j].get_ylim()[1],
                                  color='k',
                                  alpha=alpha,
                                  linewidth=lw)
                axes[i, j].text(0.5,
                                1.03,
                                r'$%.2f_{-%.2f}^{+%.2f}$' %
                                (np.percentile(positions[:, j], 50),
                                 np.percentile(positions[:, j], 50) -
                                 np.percentile(positions[:, j], 15.865),
                                 np.percentile(positions[:, j], 100 - 15.865) -
                                 np.percentile(positions[:, j], 50)),
                                fontsize=text_size,
                                ha="center",
                                transform=axes[i, j].transAxes)

                if plot_true_parameters:
                    if i < 3:
                        axes[i, j].vlines(true_parameters[i],
                                          axes[i, j].get_ylim()[0],
                                          axes[i, j].get_ylim()[1],
                                          color='r',
                                          alpha=alpha,
                                          linewidth=lw,
                                          linestyle='solid')

            if i > j:
                if j != 0:
                    plt.setp(axes[i, j].get_yticklabels(), visible=False)

                corner.hist2d(positions[:, j],
                              positions[:, i],
                              ax=axes[i, j],
                              bins=15,
                              levels=(1 - np.exp(-0.5), 1 - np.exp(-2.0),
                                      1 - np.exp(-4.5)))
                #axes[i,j].plot(positions_max[:,j],positions_max[:,i],'kx',markersize = 10,mew=2.5)
                #im = axes[i,j].scatter(positions[:,j],positions[:,i],c='k',edgecolor='None',s=45,marker='o',alpha=alpha_less,rasterized=True)#,vmin=0,vmax=vmax)
                #axes[i,j].hist2d(positions[:,j],positions[:,i],cmap = my_cmap, vmin=1)

                if use_prior:
                    xmin = prior[j][0] - 3.5 * prior[j][1]
                    xmax = prior[j][0] + 3.5 * prior[j][1]
                    axes[i, j].set_xlim((xmin, xmax))

                    a_length = 3. * prior[j][1]
                    b_length = 3. * prior[i][1]
                    parametric = np.linspace(0, 2 * np.pi, 100)
                    x_axis = a_length * np.cos(parametric) + prior[j][0]

                    ymin = prior[i][0] - 3.5 * prior[i][1]
                    ymax = prior[i][0] + 3.5 * prior[i][1]
                    axes[i, j].set_ylim((ymin, ymax))
                    y_axis = b_length * np.sin(parametric) + prior[i][0]
                    axes[i, j].plot(x_axis,
                                    y_axis,
                                    c="k",
                                    linestyle='--',
                                    alpha=1,
                                    lw=lw)
                    #axes[i,j].set_ylim(borders[t][1])
                else:
                    axes[i, j].set_xlim(min(positions[:, j]),
                                        max(positions[:, j]))
                    axes[i, j].set_ylim(min(positions[:, i]),
                                        max(positions[:, i]))

                if plot_true_parameters:
                    #if i < 3:
                    #	axes[i,j].hlines(true_parameters[i],axes[i,j].get_xlim()[0],axes[i,j].get_xlim()[1], color = 'r',alpha=alpha,linewidth = lw,linestyle = 'solid')
                    #if j < 3:
                    #	axes[i,j].vlines(true_parameters[j],axes[i,j].get_ylim()[0],axes[i,j].get_ylim()[1], color = 'r',alpha=alpha,linewidth = lw,linestyle = 'solid')
                    if i < 3 and j < 3:
                        axes[i, j].plot(true_parameters[j],
                                        true_parameters[i],
                                        'rx',
                                        lw=lw,
                                        alpha=1)

            if j > i:
                correlation_coefficient = np.corrcoef(
                    (positions[:, i], positions[:, j]))
                axes[i, j].text(0.6,
                                0.5,
                                "%.2f" % (correlation_coefficient[1, 0]),
                                fontsize=cor_text,
                                ha="center",
                                transform=axes[i, j].transAxes)
                axes[i, j].axis('off')
            if i == nparameter - 1:
                axes[i, j].set_xlabel(parameter_names[j])
            if j == 0:
                axes[i, j].set_ylabel(parameter_names[i])
    if nparameter >= 2:
        axes[0, 1].set_title(
            'best posterior = %.2f, obtained at %s and %d evals (4992 total)' %
            (np.max(posterior), str(positions_max), len(posterior)))

    fig.savefig('%sparameter_space_sorted.png' % (directory),
                dpi=300,
                bbox_inches='tight')
示例#17
0
def main():
    train_covar = covar_gen(args.covar, args.n_train_points).astype(np.float32)
    train_data_clean = data_gen(args.data,
                                args.n_train_points)[0].astype(np.float32)

    # plt.scatter(train_data_clean[:, 0], train_data_clean[:, 1])

    train_data = np.zeros_like(train_data_clean)
    for i in range(args.n_train_points):
        train_data[i] = train_data_clean[i] + np.random.multivariate_normal(
            mean=np.zeros((2, )), cov=train_covar[i])

    # plt.scatter(train_data[:, 0], train_data[:, 1])
    # plt.show()

    train_covar = torch.from_numpy(train_covar)
    train_data = torch.from_numpy(train_data.astype(np.float32))

    train_dataset = DeconvDataset(train_data, train_covar)
    train_loader = DataLoader(train_dataset,
                              batch_size=args.batch_size,
                              shuffle=True)

    test_data_clean = torch.from_numpy(
        data_gen(args.data, args.n_test_points)[0].astype(np.float32))

    eval_covar = covar_gen(args.covar, args.n_eval_points).astype(np.float32)
    eval_data_clean = data_gen(args.data,
                               args.n_eval_points)[0].astype(np.float32)

    eval_data = np.zeros_like(eval_data_clean)
    for i in range(args.n_eval_points):
        eval_data[i] = eval_data_clean[i] + np.random.multivariate_normal(
            mean=np.zeros((2, )), cov=eval_covar[i])

    eval_covar = torch.from_numpy(eval_covar)
    eval_data = torch.from_numpy(eval_data.astype(np.float32))

    eval_dataset = DeconvDataset(eval_data, eval_covar)
    eval_loader = DataLoader(eval_dataset,
                             batch_size=args.test_batch_size,
                             shuffle=False)

    if args.infer == 'true_data':
        model = SVIFlowToy(dimensions=2,
                           objective=args.objective,
                           posterior_context_size=args.posterior_context_size,
                           batch_size=args.batch_size,
                           device=device,
                           maf_steps_prior=args.flow_steps_prior,
                           maf_steps_posterior=args.flow_steps_posterior,
                           maf_features=args.maf_features,
                           maf_hidden_blocks=args.maf_hidden_blocks,
                           K=args.K)

    else:
        model = SVIFlowToyNoise(
            dimensions=2,
            objective=args.objective,
            posterior_context_size=args.posterior_context_size,
            batch_size=args.batch_size,
            device=device,
            maf_steps_prior=args.flow_steps_prior,
            maf_steps_posterior=args.flow_steps_posterior,
            maf_features=args.maf_features,
            maf_hidden_blocks=args.maf_hidden_blocks,
            K=args.K)

    message = 'Total number of parameters: %s' % (sum(
        p.numel() for p in model.parameters()))
    logger.info(message)

    optimizer = torch.optim.Adam(params=model.parameters(), lr=args.lr)

    #training
    scheduler = list(map(int, args.eval_based_scheduler.split(',')))
    epoch = 0
    best_model = copy.deepcopy(model.state_dict())

    best_eval_loss = compute_eval_loss(model, eval_loader, device,
                                       args.n_eval_points)
    n_epochs_not_improved = 0

    model.train()
    while n_epochs_not_improved < scheduler[-1] and epoch < args.n_epochs:
        for batch_idx, data in enumerate(train_loader):
            data[0] = data[0].to(device)
            data[1] = data[1].to(device)

            loss = -model.score(data).mean()
            optimizer.zero_grad()
            loss.backward(retain_graph=True)
            optimizer.step()

        model.eval()
        eval_loss = compute_eval_loss(model, eval_loader, device,
                                      args.n_eval_points)

        if eval_loss < best_eval_loss:
            best_model = copy.deepcopy(model.state_dict())
            best_eval_loss = eval_loss
            n_epochs_not_improved = 0

        else:
            n_epochs_not_improved += 1

        lr_scheduler(n_epochs_not_improved, optimizer, scheduler, logger)

        if (epoch + 1) % args.test_freq == 0:
            if args.infer == 'true_data':
                test_loss_clean = -model.model._prior.log_prob(
                    test_data_clean.to(device)).mean()

            else:
                test_loss_clean = -model.model._likelihood.log_prob(
                    test_data_clean.to(device)).mean()

            message = 'Epoch %s:' % (
                epoch + 1
            ), 'train loss = %.5f' % loss, 'eval loss = %.5f' % eval_loss, 'test loss (clean) = %.5f' % test_loss_clean
            logger.info(message)

        else:
            message = 'Epoch %s:' % (
                epoch +
                1), 'train loss = %.5f' % loss, 'eval loss = %.5f' % eval_loss
            logger.info(message)

        if (epoch + 1) % args.viz_freq == 0:
            if args.infer == 'true_data':
                samples = model.model._prior.sample(
                    1000).detach().cpu().numpy()

            else:
                samples = model.model._likelihood.sample(
                    1000).detach().cpu().numpy()

            corner.hist2d(samples[:, 0], samples[:, 1])
            fig_filename = args.dir + 'out/' + name + '_corner_fig_' + str(
                epoch + 1) + '.png'
            plt.savefig(fig_filename)
            plt.close()

            plt.scatter(samples[:, 0], samples[:, 1])
            fig_filename = args.dir + 'out/' + name + '_scatter_fig_' + str(
                epoch + 1) + '.png'
            plt.savefig(fig_filename)
            plt.close()

        model.train()
        epoch += 1

    model.load_state_dict(best_model)
    model.eval()

    if args.infer == 'true_data':
        test_loss_clean = -model.model._prior.log_prob(
            test_data_clean.to(device)).mean()

    else:
        test_loss_clean = -model.model._likelihood.log_prob(
            test_data_clean.to(device)).mean()

    message = 'Final test loss (clean) = %.5f' % test_loss_clean
    logger.info(message)

    torch.save(model.state_dict(), args.dir + 'models/' + name + '.model')
    logger.info('Training has finished.')

    if args.data.split('_')[0] == 'mixture' or args.data.split(
            '_')[0] == 'gaussian':
        kl_points = data_gen(args.data, args.n_kl_points)[0].astype(np.float32)

        if args.infer == 'true_data':
            model_log_prob = model.model._prior.log_prob(
                torch.from_numpy(kl_points.astype(
                    np.float32)).to(device)).mean()

        else:
            model_log_prob = model.model._likelihood.log_prob(
                torch.from_numpy(kl_points.astype(
                    np.float32)).to(device)).mean()

        data_log_prob = compute_data_ll(args.data, kl_points).mean()

        approximate_KL = data_log_prob - model_log_prob
        message = 'KL div %.5f:' % approximate_KL
        logger.info(message)
示例#18
0
def Illustris_SFH(): 
    ''' Figure that uses Illustris SFHs to justify our model of galaxies 
    '''
    # read in illustris SFH file from Tijske
    dat = h5py.File(UT.dat_dir()+'binsv2all1e8Msunh_z0.hdf5', 'r')

    # formed stellar mass is in a grid of time bins and metallicity
    t_bins = np.array([0.0, 0.005, 0.015, 0.025, 0.035, 0.045, 0.055, 0.065, 0.075, 0.085, 0.095, 0.125,0.175,0.225,0.275,0.325,0.375,0.425,0.475,0.55,0.65,0.75,0.85,0.95,1.125,1.375,1.625,1.875,2.125,2.375,2.625,2.875,3.125,3.375,3.625,3.875,4.25,4.75,5.25,5.75,6.25,6.75,7.25,7.75,8.25,8.75,9.25,9.75,10.25,10.75,11.25,11.75,12.25,12.75,13.25,13.75])
    
    galpop = {}
    galpop['M*'] = dat['CurrentStellarMass'].value.flatten() * 1e10 # current stellar mass
    # calculate SFRs
    # first sum up all the metallicities so you have delta M* in a grid of galaxies and time 
    # then average SFR over the 0.015 Gyr time period 
    sfh_grid = dat['FormedStellarMass'].value
    dM_t = np.sum(sfh_grid, axis=1) 

    #galpop['sfr'] = (1e10 * (dM_t[:,0] + dM_t[:,1])/(0.015 * 1e9)).flatten() 
    sfhs = np.zeros((len(galpop['M*']), len(t_bins)-2))
    t_mid = np.zeros(len(t_bins)-2)
    # z=0 SFR averaged over 150Myr
    sfhs[:,0] = (1e10 * (dM_t[:,0] + dM_t[:,1])/(0.015 * 1e9)).flatten() 
    sfhs[:,1:] = 10.*(dM_t[:,2:-1]/(t_bins[3:] - t_bins[2:-1]))
    t_mid[0] = 0.0075
    t_mid[1:] = 0.5 *(t_bins[3:] + t_bins[2:-1])

    # stellar mass one timestep ago 
    M_t_1 = np.zeros((len(galpop['M*']), len(t_bins)-1))
    M_t_1[:,0] = galpop['M*'] - 1e10 * (dM_t[:,0] + dM_t[:,1]) 
    for i in range(1,M_t_1.shape[1]): 
        M_t_1[:,i] = M_t_1[:,i-1] - 1e10 * dM_t[:,i+1]
    # stellar mass history at t_mid 
    Msh = 0.5 * (M_t_1[:,:-1] + M_t_1[:,1:]) 
    
    # calculate delta log SFR for galaxies by fitting SFMS every 10 time bins 
    dlogsfrs, t_skip = [], [] 
    for i in range(len(t_mid)-12): 
        fSFMS = fstarforms() 
        fit_logm_i, fit_logsfr_i = fSFMS.fit(np.log10(Msh[:,i]), np.log10(sfhs[:,i]),
                method='gaussmix', fit_range=[9.0, 10.5], dlogm=0.2)
        sfms_fit_i = fSFMS.powerlaw(logMfid=10.5)

        fig = plt.figure() 
        sub = fig.add_subplot(111)
        DFM.hist2d(np.log10(Msh[:,i]), np.log10(sfhs[:,i]),
                levels=[0.68, 0.95], range=[[9., 12.], [-3., 2.]], 
                bins=16, ax=sub) 
        sub.scatter(fit_logm_i, fit_logsfr_i, c='r', marker='x')
        sub.text(0.95, 0.1, '$t_\mathrm{cosmic} = '+str(13.75 - t_mid[i])+'$',
                ha='right', va='center', transform=sub.transAxes, fontsize=20)

        m_arr = np.linspace(9.0, 12.0, 20) 
        sub.plot(m_arr, sfms_fit_i(m_arr), c='k', lw=2, ls='--') 
        fig.savefig(''.join([UT.fig_dir(), 'illustris.sfms.', str(i), '.png']), 
                bbox_inches='tight') 
        plt.close() 

        if fSFMS._powerlaw_m < 0.5 or fSFMS._powerlaw_m > 1.5 or len(fit_logm_i) < 3: 
            continue

        dlogsfrs.append(np.log10(sfhs[:,i]) - sfms_fit_i(np.log10(Msh[:,i]))) 
        t_skip.append(t_mid[i]) 

    # now fit SFMS at z ~ 0  
    fSFMS = fstarforms() 
    fit_logm, fit_logsfr = fSFMS.fit(np.log10(galpop['M*']), np.log10(sfhs[:,0]), 
            method='gaussmix', fit_range=[9.0, 10.5], dlogm=0.2)
    sfms_fit = fSFMS.powerlaw(logMfid=10.5)

    # star-forming galaxies at z ~ 0 
    z0sf = ((np.log10(sfhs[:,0]) > sfms_fit(np.log10(galpop['M*']))-0.5) & 
            (np.log10(galpop['M*']) > 10.5) &  (np.log10(galpop['M*']) < 10.6)) 
    #z0q = ((np.log10(sfhs[:,0]) < sfms_fit(np.log10(galpop['M*']))-0.9) & 
    #        (np.log10(galpop['M*']) > 10.5) &  (np.log10(galpop['M*']) < 10.6)) 

    fig = plt.figure()
    sub = fig.add_subplot(111)
    for i in np.arange(len(z0sf))[z0sf]: 
        sub.plot(t_bins[-1] - t_skip, np.array(dlogsfrs)[:,i], c='k', alpha=0.1, lw=0.1) 
    z0sf = ((np.log10(sfhs[:,0]) > sfms_fit(np.log10(galpop['M*']))-0.1) & 
            (np.log10(galpop['M*']) > 10.5) &  (np.log10(galpop['M*']) < 10.6)) 
    for ii, i in enumerate(np.random.choice(np.arange(len(z0sf))[z0sf], 10)): 
        sub.plot(t_bins[-1] - t_skip, np.array(dlogsfrs)[:,i], lw=1, c='C'+str(ii)) 
    sub.set_xlim([11., 13.75]) 
    sub.set_xlabel('$t_\mathrm{cosmic}$ [Gyr]', fontsize=25) 
    sub.set_ylim([-.6, .6]) 
    sub.set_yticks([-0.4, 0., 0.4]) 
    sub.set_ylabel('$\Delta$ log $(\;\mathrm{SFR}\;[M_\odot/\mathrm{yr}]\;)$', fontsize=25)
    fig.savefig(''.join([UT.tex_dir(), 'figs/illustris_sfh.pdf']), bbox_inches='tight', dpi=150) 
    plt.close() 
    return None 
示例#19
0
def qaplotABC(run, T, sumstat=['smf'], nsnap0=15, sigma_smhm=0.2, downsampled='20', theta=None, figure=None): 
    ''' Quality assurance plot for ABC runs. Plot summary statistic(s), SMHMR, SFMS
    '''
    # first compare data summary statistics to Sim(median theta) 
    if theta is None:
        abcout = ABC.readABC(run, T)
        # median theta 
        theta_med = [UT.median(abcout['theta'][:, i], weights=abcout['w'][:]) for i in range(len(abcout['theta'][0]))]
    else: 
        theta_med = theta
    
    # summary statistics of data 
    sumdata = ABC.dataSum(sumstat=sumstat)  
    # summary statistics of model 
    subcat_sim = ABC.model(run, theta_med, nsnap0=nsnap0, downsampled=downsampled) 
    sumsim = ABC.modelSum(subcat_sim, sumstat=sumstat)
    
    theta_info = ABC.Theta() 
    sim_lbl = ', \n'.join([ttt+'='+str(round(tm,2)) for ttt, tm in zip(theta_info['label'], theta_med)]) 
    
    fig = plt.figure(figsize=(5*(len(sumstat)+5),4))
    mbin = np.arange(8.1, 11.9, 0.1) - 2.*np.log10(0.7)
    for i_s, stat in enumerate(sumstat): 
        if stat == 'smf': 
            sub = fig.add_subplot(1, len(sumstat)+5, i_s+1)
            # Li-White SMF 
            marr, phi, phierr = Obvs.dataSMF(source='li-white')  
            phi *= (1. - np.array([Obvs.f_sat(mm, 0.05) for mm in marr])) # sallite fraction 

            # central SMF 
            phierr *= np.sqrt(1./(1.-np.array([Obvs.f_sat(mm, 0.05) for mm in marr])))
            #sub.errorbar(marr, phi, phierr, fmt='.k', label='Data')
            sub.fill_between(marr, phi-phierr, phi+phierr, color='k', alpha=0.5, linewidth=0, label='Data') 
            #sub.plot(0.5*(mbin[1:]+mbin[:-1]), sumdata[0], c='k', ls='--', label='Data')
            sub.plot(0.5*(mbin[1:]+mbin[:-1]), sumsim[0], c='C0', label='Sim.')# \n'+sim_lbl)
        
            # SF central SMF
            isSF = (subcat_sim['galtype'] == 'sf') 
            fsfs = np.clip(Evol.Fsfms(marr), 0., 1.) 
            fsfs_errscale = np.ones(len(marr))
            fsfs_errscale[fsfs < 1.] = np.sqrt(1./(1.-fsfs[fsfs < 1.]))
            sub.errorbar(marr, fsfs * phi, fsfs_errscale * phierr, fmt='.k') # data 
            mmm, smf_sf = Obvs.getMF(subcat_sim['m.star'][isSF], weights=subcat_sim['weights'][isSF])
            sub.plot(mmm, smf_sf, c='C0', ls='--')
            
            sub.set_xlabel('$log\;M_*$', fontsize=25)
            sub.set_xlim([9., 12.])
            sub.set_ylabel('$\Phi$', fontsize=25)
            sub.set_ylim([1e-6, 10**-1.75])
            sub.set_yscale('log')
            sub.legend(loc='lower left', fontsize=20) 
        else: 
            raise NotImplementedError

    # SHMR panel of SF galaxies  
    sub = fig.add_subplot(1,len(sumstat)+5,len(sumstat)+1) # SMHMR panel of SF galaxies  
    isSF = ((subcat_sim['galtype'] == 'sf') & (subcat_sim['weights'] > 0.)) # only SF galaxies 
    # SHAM SMHMR 
    DFM.hist2d(subcat_sim['m.max'][isSF], subcat_sim['m.sham'][isSF], 
            weights=subcat_sim['weights'][isSF], 
            levels=[0.68, 0.95], range=[[10., 14.], [9., 12.]], color='k', 
            plot_datapoints=True, fill_contours=False, plot_density=True, ax=sub) 
    # model 
    DFM.hist2d(subcat_sim['m.max'][isSF], subcat_sim['m.star'][isSF], 
            weights=subcat_sim['weights'][isSF], 
            levels=[0.68, 0.95], range=[[10., 14.], [9., 12.]], color='C0', 
            plot_datapoints=False, fill_contours=False, plot_density=False, ax=sub) 
    DFM.hist2d(subcat_sim['halo.m'][isSF], subcat_sim['m.star'][isSF], 
            weights=subcat_sim['weights'][isSF], 
            levels=[0.68, 0.95], range=[[10., 14.], [9., 12.]], color='C1', 
            plot_datapoints=True, fill_contours=False, plot_density=True, ax=sub) 
    sub.plot([0.,0.], [0.,0.], c='k', lw=2, label='SHAM') 
    sub.plot([0.,0.], [0.,0.], c='C1', lw=2, label='model') 
    sub.legend(loc='lower right', fontsize=15) 
    sub.set_xlabel('$log\;M_{halo}$', fontsize=25)
    sub.set_xlim([10.5, 14]) 
    sub.set_ylabel('$log\,M_*$', fontsize=25)
    sub.set_ylim([9.0, 11.5]) 

    # scatter in the SHMR panel of SF galaxies  
    sub = fig.add_subplot(1,len(sumstat)+5,len(sumstat)+2) 
    isSF = np.where(subcat_sim['galtype'] == 'sf') # only SF galaxies 
    smhmr = Obvs.Smhmr()
    # simulation 
    m_mid, mu_mhalo, sig_mhalo, cnts = smhmr.Calculate(subcat_sim['halo.m'][isSF], subcat_sim['m.star'][isSF], 
            dmhalo=0.2, weights=subcat_sim['weights'][isSF])
    enough = (cnts > 20) 
    sub.plot(m_mid[enough], sig_mhalo[enough], c='C0', lw=2, label='($M_{h}$)') 
    sig_sim = sig_mhalo[np.argmin(np.abs(m_mid-12.))]
    m_mid, mu_mhalo, sig_mhalo, cnts = smhmr.Calculate(subcat_sim['m.max'][isSF], subcat_sim['m.star'][isSF], 
            dmhalo=0.2, weights=subcat_sim['weights'][isSF])
    enough = (cnts > 20) 
    sub.plot(m_mid[enough], sig_mhalo[enough], c='#1F77B4', ls=':', lw=1, label='($M_{max}$)')
    # SHAM "data"
    m_mid, mu_mhalo, sig_mhalo, cnts = smhmr.Calculate(subcat_sim['halo.m'][isSF], subcat_sim['m.sham'][isSF], 
            dmhalo=0.2, weights=subcat_sim['weights'][isSF])
    enough = (cnts > 20) 
    sub.plot(m_mid[enough], sig_mhalo[enough], c='k', ls='--') 
    sig_dat = sig_mhalo[np.argmin(np.abs(m_mid-12.))]

    # mark sigma_M*(M_h = 10^12) 
    sub.text(0.95, 0.95, 
            ''.join(['$\sigma^{(s)}_{M_*}(M_h = 10^{12} M_\odot) = ', str(round(sig_sim,2)), '$ \n', 
                '$\sigma^{(d)}_{M_*}(M_h = 10^{12} M_\odot) = ', str(round(sig_dat,2)), '$']), 
            fontsize=15, ha='right', va='top', transform=sub.transAxes)
    sub.legend(loc='lower left', fontsize=15) 
    sub.set_xlabel('$log\;M_{halo}$', fontsize=25)
    sub.set_xlim([10.6, 15.])
    sub.set_ylabel('$\sigma_{log\,M_*}$', fontsize=25)
    sub.set_ylim([0., 0.6])

    # SFMS panel 
    tt = ABC._model_theta(run, theta_med)
    sub = fig.add_subplot(1, len(sumstat)+5, len(sumstat)+3)
    DFM.hist2d(
            subcat_sim['m.star'][isSF], 
            subcat_sim['sfr'][isSF], 
            weights=subcat_sim['weights'][isSF], 
            levels=[0.68, 0.95], range=[[8., 12.], [-4., 2.]], color='#1F77B4', 
            plot_datapoints=True, fill_contours=False, plot_density=True, ax=sub) 
    sub.set_xlabel('$\log\;M_*$', fontsize=25)
    sub.set_xlim([8., 12.])
    sub.set_ylabel('$\log\;\mathrm{SFR}$', fontsize=25)
    sub.set_ylim([-4., 2.])
    
    # dSFR as a function of t_cosmic 
    sub = fig.add_subplot(1, len(sumstat)+5, len(sumstat)+4)
    mbins = np.linspace(9., 12., 10) 

    i_r = [] # select random SF galaxies over mass bins
    for i_m in range(len(mbins)-1): 
        inmbin = np.where(
                (subcat_sim['galtype'] == 'sf') & 
                (subcat_sim['nsnap_start'] == nsnap0) & 
                (subcat_sim['weights'] > 0) & 
                (subcat_sim['m.star'] > mbins[i_m]) & 
                (subcat_sim['m.star'] <= mbins[i_m+1]))
        if len(inmbin[0]) > 0: 
            i_r.append(np.random.choice(inmbin[0], size=1)[0])
    i_r = np.array(i_r)

    # calculate d(logSFR)  = logSFR - logSFR_MS 
    dlogsfrs = np.zeros((len(i_r), nsnap0-1))
    for i_snap in range(1, nsnap0): 
        snap_str = ''
        if i_snap != 1: snap_str = '.snap'+str(i_snap)

        sfr = subcat_sim['sfr'+snap_str][i_r]
        sfr_ms = SFH.SFR_sfms(subcat_sim['m.star'+snap_str][i_r], UT.z_nsnap(i_snap), tt['sfms']) 
        dlogsfrs[:,i_snap-1] =  sfr - sfr_ms 

    for i in range(dlogsfrs.shape[0]): 
        sub.plot(UT.t_nsnap(range(1, nsnap0))[::-1], dlogsfrs[i,:][::-1]) 
    for i in range(1, nsnap0): 
        sub.vlines(UT.t_nsnap(i), -1., 1., color='k', linestyle='--', linewidth=0.5)
    

    sub.set_xlim([UT.t_nsnap(nsnap0-1), UT.t_nsnap(1)])
    #sub.set_xticks([13., 12., 11., 10., 9.])
    sub.set_xlabel('$t_\mathrm{cosmic}$ [Gyr]', fontsize=25)
    sub.set_ylim([-1., 1.]) 
    sub.set_yticks([-0.9, -0.6, -0.3, 0., 0.3, 0.6, 0.9])
    sub.set_ylabel('$\Delta \log\,\mathrm{SFR}$', fontsize=25)
    
    sub = fig.add_subplot(1, len(sumstat)+5, len(sumstat)+5)
    mbins = np.linspace(9., 12., 20) 
    fq = np.zeros(len(mbins)-1)
    fq_sham = np.zeros(len(mbins)-1)
    for im in range(len(mbins)-1): 
        inmbin = ((subcat_sim['m.star'] > mbins[im]) & (subcat_sim['m.star'] < mbins[im+1]) & (subcat_sim['weights'] > 0))
        inmbin0 = ((subcat_sim['m.sham'] > mbins[im]) & (subcat_sim['m.sham'] < mbins[im+1]) & (subcat_sim['weights'] > 0))
        if np.sum(inmbin) > 0:  
            fq[im] = np.sum(subcat_sim['weights'][inmbin & (subcat_sim['galtype'] == 'sf')])/np.sum(subcat_sim['weights'][inmbin])
        if np.sum(inmbin0) > 0: 
            fq_sham[im] = np.sum(subcat_sim['weights'][inmbin0 & (subcat_sim['galtype'] == 'sf')])/np.sum(subcat_sim['weights'][inmbin0])
    
    sub.plot(0.5*(mbins[1:] + mbins[:-1]), 1.-fq, c='C1')  
    sub.plot(0.5*(mbins[1:] + mbins[:-1]), 1.-fq_sham, c='k', ls='--')  
    sub.set_xlim([9., 12.]) 
    sub.set_ylim([0., 1.]) 

    fig.subplots_adjust(wspace=.3)
    if theta is None: 
        fig_name = ''.join([UT.dat_dir(), 'abc/', run, '/', 'qaplot.t', str(T), '.', run, '.png'])
        fig.savefig(fig_name, bbox_inches='tight')
        plt.close()
    else: 
        if figure is None: plt.show() 
        else: fig.savefig(figure, bbox_inches='tight')
        plt.close()
    return None 
示例#20
0
    for j in range(len(fspot)):

        # load posteriors
        dat = np.load('posteriors/' + name + '_' + fspot[j] +
                      '.age-mass.posterior.npz')
        logM, logAGE = dat['logM'], dat['logAGE']
        all_logM = np.append(all_logM, logM)
        all_logAGE = np.append(all_logAGE, logAGE)

        # corner plot
        if j == 0:
            corner.hist2d(logM,
                          logAGE,
                          plot_datapoints=False,
                          bins=nbins,
                          ax=ax0,
                          levels=[levs[0]],
                          range=prange,
                          no_fill_contours=True,
                          plot_density=False,
                          color=lcol[j])
            qf00 = corner.quantile(logAGE, quants)
        else:
            corner.hist2d(logM,
                          logAGE,
                          plot_datapoints=False,
                          bins=nbins,
                          levels=[levs[0]],
                          range=prange,
                          no_fill_contours=True,
                          plot_density=False,
                          color=lcol[j],
示例#21
0
def create_plot_binary(dist=100.0, num_sys=100, bins=25):
    """ Create a set of random binaries and plot the distribution
    of resulting theta vs pm

    Parameters
    ----------
    dist : float
        Distance to the population for angular (rather than physical) units (pc)
    num_sys : float
        Number of random binaries to generate
    bins : int
        Number of bins for contours

    Returns
    -------
    None
    """

    global binary_set

    if binary_set is None or len(binary_set) != num_sys:
        generate_binary_set(num_sys=num_sys, dist=dist)

    fig, ax1 = plt.subplots(1, 1, figsize=(6, 4))

    # Plot limits
    xmin, xmax = 0.0, 5000.0
    ymin, ymax = 0.0, 3.0
    ax1.set_xlim(xmin, xmax)
    ax1.set_ylim(ymin, ymax)

    # Plot labels
    ax1.set_xlabel("Projected separation (AU)")
    ax1.set_ylabel("Proper motion difference (km/s)")

    # Plot distribution
    contourf_kwargs = {'bins': bins}
    corner.hist2d(binary_set['proj_sep'] * c.Rsun_to_cm / c.AU_to_cm,
                  binary_set['pm'],
                  nbins=bins,
                  range=([xmin, xmax], [ymin, ymax]),
                  **contourf_kwargs)

    # Add angular separation at dist axis
    ax2 = ax1.twiny()
    xticks = np.linspace(xmin, xmax, 6)
    angles = (xticks * c.AU_to_cm) / (dist * c.pc_to_cm) * (180.0 * 3600.0 /
                                                            np.pi)
    ax2.set_xticks(angles)
    ax2.set_xlabel('Angular separation at distance of ' + str(dist) +
                   ' pc (arcsec)')

    # Add proper motion at dist axis
    ax3 = ax1.twinx()
    yticks = np.linspace(ymin, ymax, 7)

    def pm_at_dist(pm, dist=100.0):
        return (pm * 1.0e5) / (dist * c.pc_to_cm) * (
            1.0e3 * 180.0 * 3600.0 / np.pi) * c.day_to_sec * 365.25

    ax3.set_ylim(0.0, pm_at_dist(ax1.get_ylim()[1], dist=dist))
    ax3.set_ylabel('Proper motion at distance of ' + str(dist) +
                   ' pc (mas/yr)')

    plt.tight_layout()
    plt.show()
示例#22
0
   \usepackage{lmodern}
   '''.split()))
plt.rc('font', family='serif')

f = pyfits.open("../make_lamost_catalog/table_for_paper.fits")
a = f[1].data
f.close()

feh = a['cannon_m_h']
am = a['cannon_alpha_m']
snr = a['snrg']

choose = snr > 50

fig, axarr = plt.subplots(2,1, sharex=True, sharey=True, figsize=(8,10))
hist2d(feh[choose], am[choose], ax=axarr[0], bins=100, range=[[-2.2,.9],[-0.2,0.5]])
hist2d(feh, am, ax=axarr[1], bins=100, range=[[-2.2,.9],[-0.2,0.5]])
axarr[1].set_xlabel("[Fe/H] (dex)" + " from Cannon/LAMOST", fontsize=16)
#axarr[1].set_ylabel(r"$\mathrm{[\alphaup/M]}$" + " (dex) from Cannon/LAMOST", fontsize=16)
fig.text(0.04, 0.5, r"$\mathrm{[\alphaup/M]}$" + " (dex) from Cannon/LAMOST", fontsize=16,
        va = 'center', rotation='vertical')
labels = [r"Objects with SNR \textgreater 50", r"All Objects"]
props = dict(boxstyle='round', facecolor='white')
for i,ax in enumerate(axarr):
    ax.tick_params(axis='x', labelsize=16)
    ax.tick_params(axis='y', labelsize=16)
    ax.text(0.05, 0.85, labels[i], 
            horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes,
            fontsize=16, bbox=props)
#plt.show()
plt.savefig("feh_alpha.png")
示例#23
0
def qaplotABC(runs=['test0', 'randomSFH_0.5gyr'], Ts=[14, 11]): 
    ''' Figure that illustrates how the ABC fitting works using two different
    runs overplotted on it each other
    '''
    nsnap0 = 15
    sigma_smhm = 0.2
    sumstat = ['smf']

    # summary statistics of data (i.e. the SMF) 
    subcat_dat = ABC.Data(nsnap0=nsnap0, sigma_smhm=sigma_smhm) # 'data'
    sumdata = ABC.SumData(sumstat, info=True, nsnap0=nsnap0, sigma_smhm=sigma_smhm)  
    
    subcat_sims, sumsims = [], [] 
    for run, T in zip(runs, Ts): 
        # get median theta from ABC runs 
        abcout = ABC.readABC(run, T)
        theta_med = [np.median(abcout['theta'][:,i]) for i in range(abcout['theta'].shape[1])]
        # read in Model(theta_med) 
        abc_dir = UT.dat_dir()+'abc/'+run+'/model/' # directory where all the ABC files are stored
        f = h5py.File(''.join([abc_dir, 'model.theta_median0.t', str(T), '.hdf5']), 'r') 
        subcat_sim = {} 
        for key in f.keys(): 
            subcat_sim[key] = f[key].value
        subcat_sims.append(subcat_sim)
        sumsims.append(ABC.SumSim(sumstat, subcat_sim, info=True))
    colors = ['#EE6A50', '#1F77B4']
    #labels = [r'model($\theta_\mathrm{median}$)', r'model($\theta_\mathrm{median}$)']
    labels = [None for r in runs]
    if 'test0' in runs: 
        labels[runs.index('test0')] = 'No duty cycle'
    if 'randomSFH_1gyr' in runs: 
        labels[runs.index('randomSFH_1gyr')] = '$t_\mathrm{duty} = 1$ Gyr'
    if 'randomSFH_0.5gyr' in runs: 
        labels[runs.index('randomSFH_0.5gyr')] = '$t_\mathrm{duty} = 0.5$ Gyr'

    fig = plt.figure(figsize=(16,5))
    _, _, phi_err = Obvs.MF_data(source='li-white', m_arr=sumdata[0][0]) # get uncertainties of central SMF
    phi_err *= np.sqrt(1./(1.-np.array([Obvs.f_sat(mm, 0.05) for mm in sumdata[0][0]]))) # now scale err by f_cen 
    
    # --- SMF panel ---
    sub = fig.add_subplot(1,3,1)
    sub.errorbar(sumdata[0][0], sumdata[0][1], yerr=phi_err, fmt='.k', 
            label=r'$\Phi_\mathrm{cen}^{\footnotesize \mathrm{Li}\&\mathrm{White}(2009)}$') 
    #label='$f_\mathrm{cen} \Phi^{\mathrm{Li}\&\mathrm{White}(2009)}$')
    for i_s, sumsim in enumerate(sumsims): 
        if i_s == len(sumsims)-1: 
            sub.plot(sumsim[0][0], sumsim[0][1], c=colors[i_s], ls='--')#, label=r'model($\theta_\mathrm{median}$)')
        else: 
            sub.plot(sumsim[0][0], sumsim[0][1], c=colors[i_s])#, label=r'model($\theta_\mathrm{median}$)')
    sub.set_xlim([9.5, 11.75])
    sub.set_xlabel('log $(\; M_*\; [M_\odot]\;)$', fontsize=25)
    sub.set_ylim([1e-5, 10**-1.75])
    sub.set_yscale('log')
    sub.set_ylabel('log $(\;\Phi\; / \mathrm{Mpc}^{-3}\,\mathrm{dex}^{-1}\;)$', fontsize=25)
    sub.legend(loc='lower left', prop={'size':20}) 

    # --- SFMS panel ---
    sub = fig.add_subplot(1,3,2)
    #gc = Cat.Observations('group_catalog', Mrcut=18, position='central')
    #gc_cat = gc.Read() 
    #sub.scatter(gc_cat['mass'], gc_cat['sfr'], s=2)
    for i_s, subcat_sim in enumerate(subcat_sims): 
        isSF = np.where(subcat_sim['gclass'] == 'sf') # only SF galaxies 
        DFM.hist2d(
                subcat_sim['m.star'][isSF], 
                subcat_sim['sfr'][isSF], 
                weights=subcat_sim['weights'][isSF], 
                levels=[0.68, 0.95], range=[[9., 12.], [-3., 1.]], color=colors[i_s], 
                bins=20, plot_datapoints=False, fill_contours=False, plot_density=True, ax=sub) 
    
    # observations 
    #m_arr = np.arange(8., 12.1, 0.1)
    #sfr_arr = SFH.SFR_sfms(m_arr, UT.z_nsnap(1), subcat_sim['theta_sfms'])
    #sub.plot(m_arr, sfr_arr+0.3, ls='--', c='k') 
    #sub.plot(m_arr, sfr_arr-0.3, ls='--', c='k') 
    sub.set_xlim([9., 11.5])
    sub.set_xticks([9., 10., 11.]) 
    sub.set_xlabel('log $(\; M_*\; [M_\odot]\;)$', fontsize=25)
    sub.set_ylim([-2.5, 1.])
    sub.set_yticks([-2., -1., 0., 1.])
    sub.set_ylabel('log $(\;\mathrm{SFR}\;[M_\odot/\mathrm{yr}])$', fontsize=25)

    # --- sigma_logM* panel ---
    sub = fig.add_subplot(1,3,3)
    smhmr = Obvs.Smhmr()
    # simulation 
    mhalo_bin = np.linspace(10., 15., 11)
    for i_s, subcat_sim in enumerate(subcat_sims): 
        abc_dir = UT.dat_dir()+'abc/'+runs[i_s]+'/model/' # directory where all the ABC files are stored
        isSF = np.where(subcat_sim['gclass'] == 'sf') # only SF galaxies 
        m_mid, mu_mhalo, sig_mhalo, cnts = smhmr.Calculate(
                subcat_sim['halo.m'][isSF], subcat_sim['m.star'][isSF], 
                dmhalo=0.5, weights=subcat_sim['weights'][isSF], m_bin=mhalo_bin)
        #sub.plot(m_mid, sig_mhalo, c='#1F77B4', lw=2, label='Model') 
        for i in range(1000): 
            f = h5py.File(
                    ''.join([abc_dir, 'model.theta', str(i), '.t', str(Ts[i_s]), '.hdf5']), 
                    'r') 
            subcat_sim_i = {} 
            for key in f.keys(): 
                subcat_sim_i[key] = f[key].value
            #subcat_sim_i = ABC.model(run, theta_i, 
            #        nsnap0=nsnap0, sigma_smhm=sigma_smhm, downsampled='14') 
            isSF = np.where(subcat_sim_i['gclass'] == 'sf') # only SF galaxies 
            m_mid_i, _, sig_mhalo_i, cnt_i = smhmr.Calculate(subcat_sim_i['halo.m'][isSF], subcat_sim_i['m.star'][isSF], 
                    dmhalo=0.5, weights=subcat_sim_i['weights'][isSF], m_bin=mhalo_bin)
            #sub.plot(m_mid_i, sig_mhalo_i, c='k', lw=1, alpha=0.1) 
            if i == 0:  
                sig_mhalos = np.zeros((1000, len(cnt_i)))
                counts = np.zeros((1000, len(cnt_i)))
            sig_mhalos[i,:] = sig_mhalo_i
            counts[i,:] = cnt_i
        
        sig_mhalo_low = np.zeros(len(m_mid))
        sig_mhalo_high = np.zeros(len(m_mid))
        for im in range(len(m_mid)): 
            if np.mean(counts[:,im]) > 50.: 
                above_zero = np.where(counts[:,im] > 0) 
                sig_mhalo_low[im], sig_mhalo_high[im] = np.percentile((sig_mhalos[:,im])[above_zero], [16, 84])#, axis=0)

        above_zero = np.where(sig_mhalo_high > 0) 
        sub.fill_between(m_mid[above_zero], sig_mhalo_low[above_zero], sig_mhalo_high[above_zero], color=colors[i_s], linewidth=0, alpha=0.3, label=labels[i_s]) 
    sub.set_xlim([11.5, 13.25])
    sub.set_xlabel('log $(\; M_\mathrm{halo}\; [M_\odot]\;)$', fontsize=25)
    sub.set_ylim([0., 0.6])
    sub.set_ylabel('$\sigma_{\mathrm{log}\,M_*}$', fontsize=27)
    sub.legend(loc='lower left', prop={'size': 20}) 
    sub.text(0.95, 0.9, r'model($\theta_\mathrm{median}$)', 
            ha='right', va='center', transform=sub.transAxes, fontsize=20)

    fig.subplots_adjust(wspace=0.3)
    fig.savefig(''.join([UT.tex_dir(), 'figs/qaplot_abc.pdf']), bbox_inches='tight', dpi=150) 
    plt.close()
    return None 
示例#24
0
def compareSimpleGaia(ngauss=128, quantile=0.05, iter='10th', survey='2MASS', dataFilename='All.npz', contourColor='k'):
    setup_text_plots(fontsize=16, usetex=True)
    tgas, twoMass, Apass, bandDictionary, indices = testXD.dataArrays()
    xdgmm = XDGMM(filename=xdgmmFilename)
    absmag = 'J'
    mag1 = 'J'
    mag2 = 'K'
    xlabel = '$(J-K)^C$'
    ylabel = r'$M_J^C$'
    xlim = [-0.25, 1.25]
    ylim = [6, -6]

    ndim = 2
    data = np.load(dustFile)
    dustEBV = data['ebv']
    absMagKinda, apparentMagnitude = testXD.absMagKindaArray(absmag, dustEBV, bandDictionary, tgas['parallax'])

    color = testXD.colorArray(mag1, mag2, dustEBV, bandDictionary)
    color_err = np.sqrt(bandDictionary[mag1]['array'][bandDictionary[mag1]['err_key']]**2. + bandDictionary[mag2]['array'][bandDictionary[mag2]['err_key']]**2.)

    postFile = 'posteriorParallax.' + str(ngauss) + 'gauss.dQ' + str(quantile) + '.' + iter + '.' + survey + '.' + dataFilename
    yim = (-1, 5)
    for file in ['posteriorSimple.npz', postFile]:
        data = np.load(file)
        posterior = data['posterior']
        samples = np.zeros(np.shape(posterior)[0])
        xparallaxMAS = np.logspace(-2, 2, np.shape(posterior)[1])
        for i, p in enumerate(posterior):
            try: samples[i] = testXD.samples(xparallaxMAS, p, 1, plot=False)[0]
            except IndexError: samples[i] = -999
        mean = data['mean']
        var = data['var']
        absMag = testXD.absMagKinda2absMag(mean*10.**(0.2*apparentMagnitude))
        absMagSample = testXD.absMagKinda2absMag(samples*10.**(0.2*apparentMagnitude))


        neg = tgas['parallax'] < 0
        fig, ax = plt.subplots(1, 2)
        ax[0].plot(data['mean'][~neg], mean[~neg] - tgas['parallax'][~neg], 'ko', markersize=0.5)
        ax[0].plot(data['mean'][neg], mean[neg] - tgas['parallax'][neg], 'ro', markersize=0.5)
        ax[0].set_xscale('log')
        ax[1].plot(data['mean'][~neg], np.log(var[~neg]) - np.log(tgas['parallax_error'][~neg]**2.), 'ko', markersize=0.5)
        ax[1].plot(data['mean'][neg], np.log(var[neg]) - np.log(tgas['parallax_error'][neg]**2.), 'ro', markersize=0.5)
        ax[1].set_xscale('log')
        ax[0].set_xlabel(r'$E[\varpi]$', fontsize=18)
        ax[1].set_xlabel(r'$E[\varpi]$', fontsize=18)
        ax[0].set_ylabel(r'$E[\varpi] - \varpi$', fontsize=18)
        ax[1].set_ylabel(r'$\mathrm{ln} \, \tilde{\sigma}_{\varpi}^2 - \mathrm{ln} \, \sigma_{\varpi}^2$', fontsize=18)
        plt.tight_layout()
        #if file == 'posteriorSimple.npz':
        ax[0].set_ylim(-5, 5)
        ax[1].set_ylim(-6, 2)
        ax[0].set_xlim(1e-1, 1e1)
        ax[1].set_xlim(1e-1, 1e2)
        fig.savefig(file.split('.')[0] + '_Comparison2Gaia.png')
        notnans = ~np.isnan(var) & ~np.isnan(tgas['parallax_error'])
        print 'The median of the differences of the logs: ', np.median(np.log(var[notnans]) - np.log(tgas['parallax_error'][notnans]**2.))
        cNorm  = plt.matplotlib.colors.Normalize(vmin=-6, vmax=6)
        fig, ax = plt.subplots(1, 2, figsize=(14, 7))
        x = color[notnans]
        y = np.log(var[notnans]) - np.log(tgas['parallax_error'][notnans]**2.)
        levels = 1.0 - np.exp(-0.5 * np.arange(1.0, 2.1, 1.0) ** 2)
        #(counts, xedges, yedges, Image) = ax[0].hist2d(x, y, bins=100, cmap='Greys', norm=cNorm)
        #figcount, axcounts = plt.subplots()
        #nonzero = counts > 0
        #axcounts.hist(np.log10(counts[nonzero]), log=True)
        #axcounts.set_xlabel('log counts')
        #figcount.savefig('counts.png')
        norm = plt.matplotlib.colors.Normalize(vmin=-1.5, vmax=1)
        cmap = 'inferno'
        ax[0].scatter(x, y, c=y, s=1, lw=0, alpha=0.05, norm=norm, cmap=cmap)
        corner.hist2d(x, y, bins=200, ax=ax[0], levels=levels, no_fill_contours=True, plot_density=False, plot_data=False, color=contourColor)
        #ax[0].scatter(color[notnans], np.log(var[notnans]) - np.log(tgas['parallax_error'][notnans]**2.), lw=0, s=1, alpha=0.5, c=tesXD.absMagKinda2absMag(absMagKinda[notnans]), norm=cNorm, cmap='plasma')
        ax[0].set_xlabel(r'$(J-K)^c$', fontsize=18)
        ax[0].set_ylim(-6, 2)
        ax[0].set_xlim(-0.5, 2)
        ax[0].set_ylabel(r'$\mathrm{ln} \, \tilde{\sigma}_{\varpi}^2 - \mathrm{ln} \, \sigma_{\varpi}^2$', fontsize=18)
        #ax[0].errorbar(color, np.log(var[notnans]) - np.log(tgas['parallax_error'][notnans]**2.), fmt="none", zorder=0, lw=0.5, mew=0, color='grey')
        cNorm  = plt.matplotlib.colors.Normalize(vmin=0.1, vmax=2)
        ax[1].scatter(x, absMag[notnans], s=1, lw=0, c=y, alpha=0.05, norm=norm, cmap=cmap)
        ax[1].set_xlim(xlim)
        ax[1].set_ylim(ylim)
        ax[1].set_xlabel(xlabel, fontsize=18)
        ax[1].set_ylabel(ylabel, fontsize=18)
        #ax[1].hist(np.log(var[notnans]) - np.log(tgas['parallax_error'][notnans]**2.), bins=100, histtype='step', lw=2, log=True, color='black')
        #ax[1].set_xlabel(r'$\mathrm{ln} \, \tilde{\sigma}_{\varpi}^2 - \mathrm{ln} \, \sigma_{\varpi}^2$', fontsize=18)
        #ax[1].set_xlim(-6, 2)
        #ax[1].set_ylim(1,)
        fig.savefig('deltaLogVariance_' + file.split('.')[0] + '.png')

        figVarDiff = plt.figure(figsize=(14,7))

        ax1 = figVarDiff.add_subplot(121)
        ax2 = figVarDiff.add_subplot(122)

        ax1.scatter(x, absMag[notnans], s=1, lw=0, c=y, alpha=0.05, norm=norm, cmap=cmap)
        ax2.scatter(x, absMag[notnans], s=1, lw=0, c=tgas['parallax_error'][notnans]**2., alpha=0.05, cmap=cmap)

        titles = ["Colored by change in variance", "Colored by observed variance"]

        ax = [ax1, ax2]

        for i in range(2):
            ax[i].set_xlim(xlim)
            ax[i].set_ylim(ylim[0], ylim[1]*1.1)

            ax[i].text(0.05, 0.95, titles[i],
                   ha='left', va='top', transform=ax[i].transAxes, fontsize=18)

            ax[i].set_xlabel(xlabel, fontsize = 18)

        #if i in (1, 3):
            #ax[i].yaxis.set_major_formatter(plt.NullFormatter())
        #else:
            ax[i].set_ylabel(ylabel, fontsize = 18)

        figVarDiff.savefig('denoisedVariance_' + file.split('.')[0] + '.png')
        figVarDiff.clf()

        ax1 = figVarDiff.add_subplot(121)
        ax2 = figVarDiff.add_subplot(122)

        ax1.scatter(x, absMag[notnans], s=1, lw=0, c=y, alpha=0.05, norm=norm, cmap=cmap)
        ax2.scatter(x, absMagSample[notnans], s=1, lw=0, c=tgas['parallax_error'][notnans]**2., alpha=0.05, cmap=cmap)

        titles = ["Colored by change in variance", "Colored by observed variance"]

        ax = [ax1, ax2]

        for i in range(2):
            ax[i].set_xlim(xlim)
            ax[i].set_ylim(ylim[0], ylim[1]*1.1)

            ax[i].text(0.05, 0.95, titles[i],
                   ha='left', va='top', transform=ax[i].transAxes, fontsize=18)

            ax[i].set_xlabel(xlabel, fontsize = 18)

        #if i in (1, 3):
            #ax[i].yaxis.set_major_formatter(plt.NullFormatter())
        #else:
            ax[i].set_ylabel(ylabel, fontsize = 18)

        figVarDiff.savefig('denoisedVarianceSamples_' + file.split('.')[0] + '.png')
示例#25
0
    plt.xlabel('log(R151)')
    plt.legend(loc='lower right')
    plt.savefig('DR14vsTSB_R151vsR51.pdf', dpi=700)
    #plt.show()
    return


# Make 2D histograms and overlay scatter plot training set binaries on the histogram
plt.figure(figsize=(8, 8))
#Overplot the training set binaries by scatter plot
plt.scatter(dr14XR, dr14R2, s=6, marker='^', label='DR14 Stars')
#Corner is the package that generates the 2D histogram
corner.hist2d(kcXR,
              kcR2,
              bins=150,
              plot_contours=True,
              fill_contours=True,
              smooth=1.2,
              plot_datapoints=True)
plt.xlabel('max log(x-range)', fontsize=15)
plt.ylabel('min log($R_{101}/R_{51}$)', fontsize=15)
plt.legend(loc='lower right')
plt.ylim(-1, 0.2)
plt.xlim(-1, 0.2)
plt.savefig('dr14vsTSB(XR_vs_R2).pdf', dpi=800, bbox='tight')
plt.show()
'''#Generate histograms to find the number of most densely populated region
# XR vs R101
plt.figure(figsize=(8,8))
plt.hist2d(dr14XR,dr14R101,bins=(50,50), norm=colors.LogNorm(),cmap=plt.cm.BuPu)
plt.colorbar()
示例#26
0
def plot_mcmc_chain(directory,
                    set_scale=False,
                    use_scale=False,
                    only_first_star=True):
    '''
	This routine takes the output from 'restructure_chain' function and plots the result in a corner plot
	set_scale and use_scale can be used to put different PDFs on the same scale, in the sense that the plot is shown with the same axis range.
	
	In the paper this is used to plot the Posterior in comparison to the prior distribution.
	'''
    import corner
    plt.clf()
    text_size = 16
    cor_text = 22
    positions = np.load('%sposteriorPDF.npy' % (directory))
    parameter_names = np.load("%sparameter_names.npy" % (directory))

    if only_first_star:
        positions = positions[:, :6]
        parameter_names = parameter_names[:6]

    nparameter = len(positions[0])
    cor_matrix = np.zeros(shape=(nparameter, nparameter))
    for i in range(nparameter):
        for j in range(nparameter):
            cor_matrix[i, j] = np.corrcoef(
                (positions[:, i], positions[:, j]))[1, 0]
    np.save('%scor_matrix' % (directory), cor_matrix)

    fig, axes = plt.subplots(nrows=nparameter,
                             ncols=nparameter,
                             figsize=(14.69, 8.0),
                             dpi=300)  #,sharex=True, sharey=True)

    left = 0.1  # the left side of the subplots of the figure
    right = 0.925  # the right side of the subplots of the figure
    bottom = 0.075  # the bottom of the subplots of the figure
    top = 0.97  # the top of the subplots of the figure
    wspace = 0.0  # the amount of width reserved for blank space between subplots
    hspace = 0.0  # the amount of height reserved for white space between subplots
    plt.subplots_adjust(left=left,
                        bottom=bottom,
                        right=right,
                        top=top,
                        wspace=wspace,
                        hspace=hspace)

    alpha = 0.5
    alpha_more = 0.8
    alpha_less = 0.1
    lw = 2
    if set_scale:
        borders = []
    if use_scale:
        borders = np.load(directory + 'prior_borders.npy',
                          encoding='bytes',
                          allow_pickle=True)
    t = 0

    for i in range(nparameter):
        for j in range(nparameter):
            axes[i, j].locator_params(nbins=4)
            if j == 1:
                axes[i, j].locator_params(nbins=4)
            if i == j:
                counts, edges = np.histogram(positions[:, j], bins=10)
                max_count = float(np.max(counts))
                counts = np.divide(counts, max_count)
                axes[i, j].bar(edges[:-1],
                               align='edge',
                               height=counts,
                               width=edges[1] - edges[0],
                               color='grey',
                               alpha=alpha,
                               linewidth=0,
                               edgecolor='blue')
                if use_scale:
                    axes[i, j].set_xlim(borders[t][0])
                    axes[i, j].plot(borders[t][2],
                                    borders[t][3],
                                    c="k",
                                    linestyle='--',
                                    alpha=1,
                                    lw=lw)
                    t += 1
                else:
                    axes[i, j].set_xlim(min(positions[:, j]),
                                        max(positions[:, j]))
                axes[i, j].set_ylim(0, 1.05)
                if j != 0:
                    plt.setp(axes[i, j].get_yticklabels(), visible=False)

                if set_scale:
                    borders.append([
                        axes[i, j].get_xlim(), axes[i, j].get_ylim(),
                        edges[:-1], counts
                    ])

                axes[i, j].vlines(np.percentile(positions[:, j], 15.865),
                                  axes[i, j].get_ylim()[0],
                                  axes[i, j].get_ylim()[1],
                                  color='k',
                                  alpha=alpha,
                                  linewidth=lw,
                                  linestyle='dashed')
                axes[i, j].vlines(np.percentile(positions[:, j], 100 - 15.865),
                                  axes[i, j].get_ylim()[0],
                                  axes[i, j].get_ylim()[1],
                                  color='k',
                                  alpha=alpha,
                                  linewidth=lw,
                                  linestyle='dashed')
                axes[i, j].vlines(np.percentile(positions[:, j], 50),
                                  axes[i, j].get_ylim()[0],
                                  axes[i, j].get_ylim()[1],
                                  color='k',
                                  alpha=alpha,
                                  linewidth=lw)
                axes[i, j].text(0.5,
                                1.03,
                                r'$%.2f_{-%.2f}^{+%.2f}$' %
                                (np.percentile(positions[:, j], 50),
                                 np.percentile(positions[:, j], 50) -
                                 np.percentile(positions[:, j], 15.865),
                                 np.percentile(positions[:, j], 100 - 15.865) -
                                 np.percentile(positions[:, j], 50)),
                                fontsize=text_size,
                                ha="center",
                                transform=axes[i, j].transAxes)

            if i > j:
                if j != 0:
                    plt.setp(axes[i, j].get_yticklabels(), visible=False)

                corner.hist2d(positions[:, j],
                              positions[:, i],
                              ax=axes[i, j],
                              bins=15,
                              levels=(1 - np.exp(-0.5), 1 - np.exp(-2.0),
                                      1 - np.exp(-4.5)))
                #axes[i,j].plot(positions_max[:,j],positions_max[:,i],'kx',markersize = 10,mew=2.5)
                #im = axes[i,j].scatter(positions[:,j],positions[:,i],c='k',edgecolor='None',s=45,marker='o',alpha=alpha_less,rasterized=True)#,vmin=0,vmax=vmax)
                #axes[i,j].hist2d(positions[:,j],positions[:,i],cmap = my_cmap, vmin=1)

                if use_scale:
                    axes[i, j].set_xlim(borders[t][0])
                    axes[i, j].set_ylim(borders[t][1])
                    #axes[i,j].plot( borders[t][2], borders[t][3], "k",linestyle = '--', alpha=1, lw = lw )
                    t += 1
                else:
                    axes[i, j].set_xlim(min(positions[:, j]),
                                        max(positions[:, j]))
                    axes[i, j].set_ylim(min(positions[:, i]),
                                        max(positions[:, i]))

                if set_scale:
                    borders.append(
                        [axes[i, j].get_xlim(), axes[i, j].get_ylim(), i, j])

            if j > i:
                correlation_coefficient = np.corrcoef(
                    (positions[:, i], positions[:, j]))
                axes[i, j].text(0.6,
                                0.5,
                                "%.2f" % (correlation_coefficient[1, 0]),
                                fontsize=cor_text,
                                ha="center",
                                transform=axes[i, j].transAxes)
                axes[i, j].axis('off')
            if i == nparameter - 1:
                axes[i, j].set_xlabel(parameter_names[j])
            if j == 0:
                axes[i, j].set_ylabel(parameter_names[i])
    #if nparameter>3:
    #	axes[0,3].set_title('vmax = %.2f, obtained at %s' %(vmax,str(positions_max)))
    #	#axes[0,1].set_title('vmax = %.2f, obtained at %s and %d evals thrown out' %(vmax,str(positions_max),throw_out))

    fig.savefig('%sparameter_space_sorted.png' % (directory),
                dpi=300,
                bbox_inches='tight')

    if set_scale:
        np.save('%sprior_borders' % (directory), borders)
示例#27
0
def ABCvsMCMC_contour(obvs, nwalkers=100, nburns=9000, sigma=False):
    ''' Plots that compare the ABC posteriors to the MCMC posteriors 
    '''
    if obvs == 'nbargmf':
        abc_dir = ''.join([
            ut.dat_dir(),
            'paper/ABC',
            obvs,
            '/run1/',
        ])
        abc_theta_file = lambda tt: ''.join(
            [abc_dir, 'nbar_gmf_theta_t',
             str(tt), '.ABCnbargmf.dat'])
        tf = 8
        mcmc_dir = ''.join([ut.dat_dir(), 'paper/'])
        mcmc_filename = ''.join([mcmc_dir, 'nbar_gmf.mcmc.mcmc_chain.p'])
    elif obvs == 'nbarxi':
        abc_dir = ''.join([
            ut.dat_dir(),
            'paper/ABC',
            obvs,
            '/',
        ])
        abc_theta_file = lambda tt: ''.join(
            [abc_dir, 'nbar_xi_theta_t',
             str(tt), '.abc.dat'])
        tf = 9
        mcmc_dir = ''.join([ut.dat_dir(), 'paper/'])
        mcmc_filename = ''.join([mcmc_dir, 'nbar_xi.mcmc.mcmc_chain.p'])
    else:
        raise ValueError

    prior_min, prior_max = PriorRange('first_try')
    prior_range = np.zeros((len(prior_min), 2))
    prior_range[:, 0] = prior_min
    prior_range[:, 1] = prior_max

    # true HOD parameter
    true_dict = Data.data_hod_param(Mr=21)
    truths = [
        true_dict['logM0'],  # log M0
        np.log(true_dict['sigma_logM']),  # log(sigma)
        true_dict['logMmin'],  # log Mmin
        true_dict['alpha'],  # alpha
        true_dict['logM1']  # log M1
    ]

    mcmc_sample = pickle.load(open(mcmc_filename, 'rb'))[nburns * nwalkers:, :]
    abc_sample = np.loadtxt(abc_theta_file(tf))

    par_labels = [
        r'$\mathtt{log}\;\mathcal{M}_{0}$',
        r'$\mathtt{log}\;\sigma_\mathtt{log\;M}$',
        r'$\mathtt{log}\;\mathcal{M}_\mathtt{min}$', r'$\alpha$',
        r'$\mathtt{log}\;\mathcal{M}_{1}$'
    ]

    prettyplot()
    pretty_colors = prettycolors()
    fig = plt.figure(1, figsize=(20, 6))
    gs = gridspec.GridSpec(1, 3)

    # first panel
    for i in [0, 1, 2]:
        plot_range = np.zeros((2, 2))
        if i == 0:
            col_pair = [2, 3]
            plot_range[0, 0] = 12.5
            plot_range[0, 1] = 13.0
            plot_range[1, 0] = prior_range[3, 0]
            plot_range[1, 1] = prior_range[3, 1]
        elif i == 2:
            col_pair = [4, 2]
            plot_range[0, 0] = 13.6
            plot_range[0, 1] = 14.2
            plot_range[1, 0] = 12.5
            plot_range[1, 1] = 13.0
        elif i == 1:
            col_pair = [3, 4]
            plot_range[0, 0] = prior_range[3, 0]
            plot_range[0, 1] = prior_range[3, 1]
            plot_range[1, 0] = 13.6
            plot_range[1, 1] = 14.2

        if i == 2:
            mcmc_label = r'$\mathcal{L}^\mathtt{Gauss}$ MCMC'
            abc_label = 'ABC-PMC'
        else:
            mcmc_label = None
            abc_label = None

        mcmc_par1 = mcmc_sample[:, col_pair[0]]
        mcmc_par2 = mcmc_sample[:, col_pair[1]]

        abc_par1 = abc_sample[:, col_pair[0]]
        abc_par2 = abc_sample[:, col_pair[1]]

        ax = plt.subplot(gs[i])

        if sigma:
            lvls = [1 - np.exp(-0.5), 1 - np.exp(-0.125)]
        else:
            lvls = [0.68, 0.95]

        corner.hist2d(mcmc_par1,
                      mcmc_par2,
                      bins=20,
                      range=plot_range,
                      ax=ax,
                      plot_datapoints=False,
                      levels=lvls,
                      color='#1F77B4',
                      fill_contours=True,
                      smooth=1.0,
                      label=mcmc_label)

        corner.hist2d(abc_par1,
                      abc_par2,
                      bins=20,
                      range=plot_range,
                      ax=ax,
                      levels=lvls,
                      color='#FF7F0E',
                      fill_contours=True,
                      smooth=1.0,
                      label=abc_label)

        ax.scatter(np.repeat(truths[col_pair[0]], 2),
                   np.repeat(truths[col_pair[1]], 2),
                   s=100,
                   marker='*',
                   c='k',
                   lw=0,
                   label=None)
        #ax.axvline(truths[i_col], color='k', ls='--', linewidth=3)
        #ax.set_xticklabels([])
        ax.set_xlim([plot_range[0, 0], plot_range[0, 1]])
        ax.set_ylim([plot_range[1, 0], plot_range[1, 1]])

        if i == 2:
            thick_line1 = mlines.Line2D([], [],
                                        ls='-',
                                        c='#FF7F0E',
                                        linewidth=12,
                                        alpha=0.5,
                                        label='ABC-PMC')
            ax.legend(loc='upper right',
                      handles=[thick_line1],
                      frameon=False,
                      fontsize=25,
                      handletextpad=0.1,
                      scatteryoffsets=[0.5])
        elif i == 1:
            thick_line2 = mlines.Line2D(
                [], [],
                ls='-',
                c='#1F77B4',
                linewidth=12,
                alpha=0.5,
                label='$\mathcal{L}^\mathtt{Gauss}$ \nMCMC')
            ax.legend(loc='upper right',
                      handles=[thick_line2],
                      frameon=False,
                      fontsize=25,
                      handletextpad=0.1,
                      scatteryoffsets=[0.5])

        ax.set_xlabel(par_labels[col_pair[0]], fontsize=25, labelpad=15)
        ax.set_ylabel(par_labels[col_pair[1]], fontsize=25)

    if sigma:
        sigma_str = '.true1sigma'
    else:
        sigma_str = ''

    fig.subplots_adjust(wspace=0.3)
    fig_name = ''.join([
        ut.fig_dir(), 'paper.ABCvsMCMC.contour', '.', obvs, sigma_str, '.pdf'
    ])
    fig.savefig(fig_name, bbox_inches='tight', dpi=150)
    return None
示例#28
0
def plot_element_correlation(directory):
    '''
	This is an experimental plotting routine. It can read the mcmc folder content and plot element / parameter / posterior correlations.
	
	For that the name-list of the blobs needs to be provided which can be generated running Chempy in the 'testing_output' mode.
	'''
    import corner
    where_to_cut = 500
    blobs = np.load('%sflatblobs.npy' % (directory))
    positions = np.load('%sflatchain.npy' % (directory))
    posterior = np.load('%sflatlnprobability.npy' % (directory))
    positions = np.reshape(positions,
                           (-1, len(positions[0, 0])))[-where_to_cut:]
    posterior = np.hstack(posterior)[-where_to_cut:]
    blobs = np.swapaxes(blobs, 0, 1)
    blobs = np.reshape(blobs, (-1, len(blobs[0, 0])))[-where_to_cut:]
    blobs = np.concatenate((blobs, positions, posterior[:, None]), axis=1)
    names = [
        'He', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Ar',
        'K', 'Ca', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'IMF', 'NIa'
    ]  # Elements of ProtoSun run
    names = ['m-%s' % item for item in names]
    names = np.append(names, 'v-posterior')
    parameter_names = ['O', 'Mg', 'Si', 'Fe']
    parameter_names = ['m-%s' % item for item in parameter_names]
    parameter_names = [names[-1]] + parameter_names
    parameter_names += [names[-3]]
    parameter_names += [names[-2]]
    nparameter = len(parameter_names)

    positions = np.zeros(shape=(len(posterior), nparameter))

    #print(parameter_names)
    #print(names)
    #print(positions.shape)
    #print(blobs.shape)
    names = np.array(names)
    for i, item in enumerate(parameter_names):
        positions[:, i] = blobs[:, np.where(names == item)[0][0]]

    parameter_names = [item[2:] for item in parameter_names]
    fig, axes = plt.subplots(nrows=nparameter,
                             ncols=nparameter,
                             figsize=(14.69, 8.0),
                             dpi=300)  #,sharex=True, sharey=True)

    left = 0.1  # the left side of the subplots of the figure
    right = 0.925  # the right side of the subplots of the figure
    bottom = 0.075  # the bottom of the subplots of the figure
    top = 0.97  # the top of the subplots of the figure
    wspace = 0.0  # the amount of width reserved for blank space between subplots
    hspace = 0.0  # the amount of height reserved for white space between subplots
    plt.subplots_adjust(left=left,
                        bottom=bottom,
                        right=right,
                        top=top,
                        wspace=wspace,
                        hspace=hspace)

    alpha = 0.5
    alpha_more = 0.8
    alpha_less = 0.2
    lw = 2
    t = 0

    text_size = 14
    cor_text = 18
    for i in range(nparameter):
        for j in range(nparameter):
            axes[i, j].locator_params(nbins=4)
            if j == 1:
                axes[i, j].locator_params(nbins=4)
            if i == j:
                counts, edges = np.histogram(positions[:, j], bins=10)
                max_count = float(np.max(counts))
                counts = np.divide(counts, max_count)
                axes[i, j].bar(edges[:-1],
                               align='edge',
                               height=counts,
                               width=edges[1] - edges[0],
                               color='grey',
                               alpha=alpha,
                               linewidth=0,
                               edgecolor='blue')
                axes[i, j].set_xlim(min(positions[:, j]), max(positions[:, j]))
                axes[i, j].set_ylim(0, 1.05)
                if j != 0:
                    plt.setp(axes[i, j].get_yticklabels(), visible=False)

                axes[i, j].vlines(np.percentile(positions[:, j], 15.865),
                                  axes[i, j].get_ylim()[0],
                                  axes[i, j].get_ylim()[1],
                                  color='k',
                                  alpha=alpha,
                                  linewidth=lw,
                                  linestyle='dashed')
                axes[i, j].vlines(np.percentile(positions[:, j], 100 - 15.865),
                                  axes[i, j].get_ylim()[0],
                                  axes[i, j].get_ylim()[1],
                                  color='k',
                                  alpha=alpha,
                                  linewidth=lw,
                                  linestyle='dashed')
                axes[i, j].vlines(np.percentile(positions[:, j], 50),
                                  axes[i, j].get_ylim()[0],
                                  axes[i, j].get_ylim()[1],
                                  color='k',
                                  alpha=alpha,
                                  linewidth=lw)

                axes[i, j].text(0.5,
                                1.03,
                                r'$%.2f_{-%.2f}^{+%.2f}$' %
                                (np.percentile(positions[:, j], 50),
                                 np.percentile(positions[:, j], 50) -
                                 np.percentile(positions[:, j], 15.865),
                                 np.percentile(positions[:, j], 100 - 15.865) -
                                 np.percentile(positions[:, j], 50)),
                                fontsize=text_size,
                                ha="center",
                                transform=axes[i, j].transAxes)

            if i > j:
                if j != 0:
                    plt.setp(axes[i, j].get_yticklabels(), visible=False)

                corner.hist2d(positions[:, j],
                              positions[:, i],
                              ax=axes[i, j],
                              bins=15,
                              levels=(1 - np.exp(-0.5), 1 - np.exp(-2.0),
                                      1 - np.exp(-4.5)))

                axes[i, j].set_ylim(-0.2, 0.2)
                if parameter_names[j] == '[He/H]':
                    axes[i, j].set_xlim(0.01, 0.06)
                    #axes[i,j].plot(0.05,0.04,marker=r"$\odot$", mec = 'red')

                else:
                    axes[i, j].set_xlim(-0.2, 0.2)
                    #axes[i,j].plot(0.04,0.04,marker=r"$\odot$", mec = 'red')
                axes[i, j].set_xlim(min(positions[:, j]), max(positions[:, j]))
                axes[i, j].set_ylim(min(positions[:, i]), max(positions[:, i]))

            if j > i:
                correlation_coefficient = np.corrcoef(
                    (positions[:, i], positions[:, j]))
                axes[i, j].text(0.6,
                                0.5,
                                "%.2f" % (correlation_coefficient[1, 0]),
                                fontsize=cor_text,
                                ha="center",
                                transform=axes[i, j].transAxes)
                axes[i, j].axis('off')
            if i == nparameter - 1:
                axes[i, j].set_xlabel(parameter_names[j])
            if j == 0:
                axes[i, j].set_ylabel(parameter_names[i])

    #axes[0,3].set_title('vmax = %.2f, obtained at %s and %d evals thrown out' %(vmax,str(positions_max),throw_out))
    #axes[0,1].set_title('vmax = %.2f, obtained at %s and %d evals thrown out' %(vmax,str(positions_max),throw_out))

    fig.savefig('%selement_correlations.png' % (directory),
                dpi=300,
                bbox_inches='tight')
示例#29
0
def PoolEvolution(obvs):
    ''' Demostrative plot for the evolution of the pool. Illustrate the pool evolution for
    log M_min versus log M_1, which has the starkest evolution from its prior. 

    '''
    if obvs == 'nbargmf':
        result_dir = ''.join([
            ut.dat_dir(),
            'paper/ABC',
            obvs,
            '/run1/',
        ])
        theta_file = lambda tt: ''.join(
            [result_dir, 'nbar_gmf_theta_t',
             str(tt), '.ABCnbargmf.dat'])
        t_list = [0, 1, 2, 3, 5, 8]
    elif obvs == 'nbarxi':
        result_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/'])
        theta_file = lambda tt: ''.join(
            [result_dir, 'nbar_xi_theta_t',
             str(tt), '.abc.dat'])
        t_list = [0, 1, 2, 3, 7, 9]
    else:
        raise ValueError

    prior_min, prior_max = PriorRange('first_try')
    prior_range = np.zeros((2, 2))
    prior_range[:, 0] = np.array([prior_min[2], prior_min[4]])
    prior_range[:, 1] = np.array([prior_max[2], prior_max[4]])

    # true HOD parameter
    true_dict = Data.data_hod_param(Mr=21)
    true_pair = [true_dict['logMmin'], true_dict['logM1']]

    prettyplot()
    pretty_colors = prettycolors()
    fig = plt.figure(figsize=(12, 8))
    all_fig = fig.add_subplot(111)

    for i_t, t in enumerate(t_list):
        sub = fig.add_subplot(2, len(t_list) / 2, i_t + 1)

        theta_Mmin, theta_M1 = np.loadtxt(theta_file(t),
                                          unpack=True,
                                          usecols=[2, 4])
        corner.hist2d(theta_Mmin,
                      theta_M1,
                      bins=20,
                      range=prior_range,
                      levels=[0.68, 0.95],
                      color='c',
                      fill_contours=True,
                      smooth=1.0)

        t_label = r"$\mathtt{t = " + str(t) + "}$"
        sub.text(13.0, 15.0, t_label, fontsize=25)

        if i_t == len(t_list) - 1:
            true_label = r'$``\mathtt{true}"$'
        else:
            true_label = None

        plt.scatter(np.repeat(true_pair[0], 2),
                    np.repeat(true_pair[1], 2),
                    s=75,
                    marker='*',
                    c='k',
                    lw=0,
                    label=true_label)
        if i_t == len(t_list) - 1:
            plt.legend(loc='lower left',
                       scatterpoints=1,
                       markerscale=2.5,
                       handletextpad=-0.25,
                       scatteryoffsets=[0.5])

        if i_t == 0:
            sub.set_xticklabels([])
            sub.set_yticklabels([13., 13.5, 14., 14.5, 15., 15.5])
        elif (i_t > 0) and (i_t < len(t_list) / 2):
            sub.set_xticklabels([])
            sub.set_yticklabels([])
        elif i_t == len(t_list) / 2:
            sub.set_yticklabels([13., 13.5, 14., 14.5, 15.])
        elif i_t > len(t_list) / 2:
            sub.set_yticklabels([])

    all_fig.set_xticklabels([])
    all_fig.set_yticklabels([])
    all_fig.set_ylabel(r'$\mathtt{log}\;\mathcal{M}_\mathtt{1}$',
                       fontsize=30,
                       labelpad=50)
    all_fig.set_xlabel(r'$\mathtt{log}\;\mathcal{M}_\mathtt{min}$',
                       fontsize=30,
                       labelpad=25)

    fig.subplots_adjust(hspace=0.0)
    fig_name = ''.join(
        [ut.fig_dir(), 'paper_ABC_poolevolution', '.', obvs, '.pdf'])
    fig.savefig(fig_name, bbox_inches='tight', dpi=150)
    plt.close()
示例#30
0
from astropy.table import Table
import astropy.units as u
import matplotlib.pyplot as plt
import numpy as np
import corner
from astropy.stats import median_absolute_deviation as mad
t = Table.read('/mnt/ephem/ephem/erosolow/cohrs/cohrs_withlines.fits')

fig = plt.figure(figsize=(4.5,3.5))
ax = fig.add_subplot(111)
idx = np.isfinite(np.log10((t['13co10'])/t['12co32']))
t2 = t[idx]
corner.hist2d(np.log10(t[idx]['flux']),
              np.log10((t[idx]['13co10'])/t[idx]['12co32']),
              ax=ax)

# ax.scatter(t['flux'], t['13co10']/t['12co32'], edgecolor='k',
#            facecolor='gray', alpha=0.2)
ax.set_ylim(-3,0)
ax.set_xlabel(r'$\log_{10}[F_{\mathrm{CO(3-2)}}/(\mathrm{K\ km\ s^{-1}\ pc^{2}})]$')
ax.set_ylabel(r'$\log_{10}[F_{\mathrm{{}^{13}CO(1-0)}}/F_{\mathrm{CO(3-2)}}]$')
plt.tight_layout()
#ax.set_xscale("log", nonposx='clip')
#ax.set_yscale("log", nonposy='clip')

idx2 = t2['flux']>1e3
print(np.median(t2[idx2]['13co10']/t2[idx2]['12co32']))
print(mad(np.log(t2[idx2]['13co10']/t2[idx2]['12co32'])))
count = idx2.sum()
print('Number of clouds: {0}'.format(count))
示例#31
0
ra_J0045 = coor_J0045.ra.degree
dec_J0045 = coor_J0045.dec.degree


# Load pickled data
sampler = pickle.load( open( INDATA("J0045_MCMC_sampler.obj"), "rb" ) )
#init_params_J0045 = pickle.load( open( INDATA("J0045_pop_synth_init_conds.obj"), "rb" ) )
#HMXB_J0045 = pickle.load( open( INDATA("J0045_pop_synth_HMXB.obj"), "rb" ) )


# Specific distribution plots
plt.rc('font', size=18)
# M1 vs M2
M1 = sampler.flatchain.T[0]
M2 = sampler.flatchain.T[1]
corner.hist2d(M1, M2)
plt.xlabel(r'$M_1\ ({\rm M_{\odot}})$', fontsize=22)
plt.ylabel(r'$M_2\ ({\rm M_{\odot}})$', fontsize=22)
plt.savefig('../figures/J0045_M1_M2.pdf')
#plt.show()
# V_k vs theta
v_k = sampler.flatchain.T[4]
theta = sampler.flatchain.T[5]
corner.hist2d(v_k, theta)
plt.xlabel(r'$v_k\ ({\rm km/s})$', fontsize=22)
plt.ylabel(r'$\theta$', fontsize=22)
plt.savefig('../figures/J0045_vk_theta.pdf')
#plt.show()
# t_b histogram
t_b = sampler.flatchain.T[9]
plt.hist(t_b, histtype='step', color='k', bins=50)
示例#32
0
def plot_sample(x,
                y,
                samplex,
                sampley,
                xdgmm,
                xlabel='x',
                ylabel='y',
                xerr=None,
                yerr=None,
                ylim=(6, -6),
                xlim=(0.5, 1.5),
                errSubsample=1.2e6,
                thresholdScatter=0.1,
                binsScatter=200,
                c='black',
                norm=None,
                cmap=None,
                contourColor='k',
                posterior=False,
                ind=None,
                plot_contours=True,
                sdss5=False,
                whatsThatFeature=False,
                rasterized=False):

    prng = np.random.RandomState(1234567890)
    setup_text_plots(fontsize=16, usetex=True)
    plt.clf()
    alpha = 0.1
    alpha_points = 0.01
    figData = plt.figure(figsize=(12, 5.5))
    figPrior = plt.figure(figsize=(12, 5.5))
    for fig in [figData, figPrior]:
        fig.subplots_adjust(left=0.1,
                            right=0.95,
                            bottom=0.15,
                            top=0.95,
                            wspace=0.1,
                            hspace=0.1)

    ax1 = figData.add_subplot(121)
    levels = 1.0 - np.exp(-0.5 * np.arange(1.0, 2.1, 1.0)**2)
    cNorm = plt.matplotlib.colors.LogNorm(vmin=3, vmax=1e5)
    #ax1.hist2d(x, y, bins=100, norm=cNorm, cmap='Greys')

    if sdss5: plot_contours = False
    im = corner.hist2d(x,
                       y,
                       ax=ax1,
                       levels=levels,
                       bins=200,
                       no_fill_contours=True,
                       plot_density=False,
                       color=contourColor,
                       rasterized=rasterized,
                       plot_contours=plot_contours)

    #im = ax1.scatter(x, y, s=1, lw=0, c=c, alpha=alpha, norm=norm, cmap=cmap)

    ax2 = figData.add_subplot(122)
    if ind is None: ind = prng.randint(0, len(x), size=errSubsample)
    #ax2.scatter(x[ind], y[ind], s=1, lw=0, c='black', alpha=alpha_points)
    ax2.errorbar(x[ind],
                 y[ind],
                 xerr=xerr[ind],
                 yerr=[yerr[0][ind], yerr[1][ind]],
                 fmt="none",
                 zorder=0,
                 mew=0,
                 ecolor='black',
                 alpha=0.5,
                 elinewidth=0.5)

    ax3 = figPrior.add_subplot(121)
    #ax3.hist2d(x, y, bins=100, norm=cNorm, cmap='Greys')
    #kdeDensity(ax3, samplex, sampley, threshold=thresholdScatter, bins=binsScatter, s=1, lw=0, alpha=alpha)
    corner.hist2d(samplex,
                  sampley,
                  ax=ax3,
                  levels=levels,
                  bins=200,
                  no_fill_contours=True,
                  plot_density=False,
                  color=contourColor,
                  rasterized=rasterized,
                  plot_contours=False)
    ax3.scatter(samplex, sampley, s=1, lw=0, c='k', alpha=alpha)

    ax4 = figPrior.add_subplot(122)
    for i in range(xdgmm.n_components):
        points = drawEllipse.plotvector(xdgmm.mu[i], xdgmm.V[i])
        ax4.plot(points[0, :],
                 absMagKinda2absMag(points[1, :]),
                 'k-',
                 alpha=xdgmm.weights[i] / np.max(xdgmm.weights))
        #draw_ellipse(xdgmm.mu[i], xdgmm.V[i], scales=[2], ax=ax4,
        #         ec='None', fc='gray', alpha=xdgmm.weights[i]/np.max(xdgmm.weights)*0.1)

    #xlim = ax4.get_xlim()
    #ylim = ylim #ax3.get_ylim()

    titles = [
        "Observed Distribution", "Obs+Noise Distribution",
        "Extreme Deconvolution\n  resampling",
        "Extreme Deconvolution\n  cluster locations"
    ]
    if posterior:
        titles = [
            "De-noised Expectation Values", "Posterior Distributions",
            "Extreme Deconvolution\n  resampling",
            "Extreme Deconvolution\n  cluster locations"
        ]
    if sdss5:
        titles = ['', '', '', '']
    ax = [ax1, ax2, ax3, ax4]

    for i in range(4):
        ax[i].set_xlim(xlim)
        ax[i].set_ylim(ylim[0], ylim[1] * 1.1)

        #ax[i].xaxis.set_major_locator(plt.MultipleLocator([-1, 0, 1]))
        #ax[i].yaxis.set_major_locator(plt.MultipleLocator([3, 4, 5, 6]))

        ax[i].text(0.05,
                   0.95,
                   titles[i],
                   ha='left',
                   va='top',
                   transform=ax[i].transAxes,
                   fontsize=18)

        #if i in (0, 1):
        #    ax[i].xaxis.set_major_formatter(plt.NullFormatter())
        #else:
        ax[i].set_xlabel(xlabel, fontsize=18)

        if i in (1, 3):
            ax[i].yaxis.set_major_formatter(plt.NullFormatter())
        else:
            ax[i].set_ylabel(ylabel, fontsize=18)

    #ax[3].text(0.05, 0.95, titles[3],
    #               ha='left', va='top', transform=ax[3].transAxes)

    #ax[3].set_ylabel(r'$\varpi10^{0.2*m_G}$', fontsize=18)
    #ax[3].set_xlim(-2, 3)
    #ax[3].set_ylim(3, -1)
    #ax[3].yaxis.tick_right()
    #ax[3].yaxis.set_label_position("right")
    #plt.tight_layout()
    """
    if norm is not None:
        figData.subplots_adjust(left=0.2, right=0.95)
        cbar_ax = figData.add_axes([0.01, 0.125, 0.02, 0.75])
        cb = figData.colorbar(im, cax=cbar_ax)
        #cb = plt.colorbar(im, ax=axes[2])
        cb.set_label(r'$ln \, \tilde{\sigma}_{\varpi}^2 - ln \, \sigma_{\varpi}^2$', fontsize=20)
        cb.set_clim(-7, 2)
    """
    figData.savefig('plot_sample.data.pdf', format='pdf')
    figPrior.savefig('plot_sample.prior.pdf', format='pdf')
示例#33
0
    # if we want to plot gamma-Rc and p-Rout in two panels
    # from matplotlib import gridspec
    # fig = plt.figure(figsize=(10, 5))
    # gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1])
    # ax = plt.subplot(gs[0]), plt.subplot(gs[1])

    fig, ax = plt.subplots(1, 1, figsize=(5, 5))
    levels = 1. - np.exp(-0.5 * np.arange(
        1., 3.1, 1)**2.)  # correspond to 1, 2 and 3 sigma for a 2D hystogram

    hist2d(flatchain[:, 0],
           flatchain[:, 2],
           ax=ax,
           bins=30,
           levels=levels,
           plot_datapoints=False,
           smooth=1.5,
           plot_contours=True,
           fill_contours=True,
           plot_density=False,
           color='#2D89E5')

    # add point for best-fit model
    ax.plot(bestfit_pars[0],
            bestfit_pars[2],
            marker='*',
            markerfacecolor='#FFFF66',
            markeredgecolor='#FFFF66',
            markersize=16)

    if headers['fit']['sigma_prescription'] == 'g':
示例#34
0
文件: paper.py 项目: mjvakili/ccppabc
def ABCvsMCMC_contour(obvs, nwalkers=100, nburns=9000, sigma=False):  
    ''' Plots that compare the ABC posteriors to the MCMC posteriors 
    '''
    if obvs == 'nbargmf':
        abc_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/run1/',])
        abc_theta_file = lambda tt: ''.join([abc_dir, 'nbar_gmf_theta_t', str(tt), '.ABCnbargmf.dat']) 
        tf = 8
        mcmc_dir = ''.join([ut.dat_dir(), 'paper/']) 
        mcmc_filename = ''.join([mcmc_dir, 'nbar_gmf.mcmc.mcmc_chain.p'])
    elif obvs == 'nbarxi': 
        abc_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/',])
        abc_theta_file = lambda tt: ''.join([abc_dir, 'nbar_xi_theta_t', str(tt), '.abc.dat'])
        tf = 9
        mcmc_dir = ''.join([ut.dat_dir(), 'paper/']) 
        mcmc_filename = ''.join([mcmc_dir, 'nbar_xi.mcmc.mcmc_chain.p'])
    else: 
        raise ValueError

    prior_min, prior_max = PriorRange('first_try')
    prior_range = np.zeros((len(prior_min),2))
    prior_range[:,0] = prior_min 
    prior_range[:,1] = prior_max 

    # true HOD parameter
    true_dict = Data.data_hod_param(Mr=21)
    truths = [
            true_dict['logM0'],                 # log M0
            np.log(true_dict['sigma_logM']),    # log(sigma)
            true_dict['logMmin'],               # log Mmin
            true_dict['alpha'],                 # alpha
            true_dict['logM1']                  # log M1
            ]
    
    mcmc_sample = pickle.load(open(mcmc_filename, 'rb'))[nburns*nwalkers:,:]
    abc_sample = np.loadtxt(abc_theta_file(tf))

    par_labels = [
                r'$\mathtt{log}\;\mathcal{M}_{0}$',
                r'$\mathtt{log}\;\sigma_\mathtt{log\;M}$',
                r'$\mathtt{log}\;\mathcal{M}_\mathtt{min}$',
                r'$\alpha$',
                r'$\mathtt{log}\;\mathcal{M}_{1}$'
                ]

    prettyplot()
    pretty_colors = prettycolors()
    fig = plt.figure(1, figsize=(20,6))
    gs = gridspec.GridSpec(1, 3)
    
    # first panel 
    for i in [0, 1, 2]:
        plot_range = np.zeros((2,2))
        if i == 0: 
            col_pair = [2, 3] 
            plot_range[0, 0] = 12.5
            plot_range[0, 1] = 13.0
            plot_range[1, 0] = prior_range[3, 0] 
            plot_range[1, 1] = prior_range[3, 1] 
        elif i == 2: 
            col_pair = [4, 2] 
            plot_range[0, 0] = 13.6
            plot_range[0, 1] = 14.2
            plot_range[1, 0] = 12.5
            plot_range[1, 1] = 13.0
        elif i == 1:
            col_pair = [3, 4] 
            plot_range[0, 0] = prior_range[3, 0]
            plot_range[0, 1] = prior_range[3, 1] 
            plot_range[1, 0] = 13.6
            plot_range[1, 1] = 14.2

        if i == 2: 
            mcmc_label = r'$\mathcal{L}^\mathtt{Gauss}$ MCMC' 
            abc_label = 'ABC-PMC'
        else: 
            mcmc_label = None
            abc_label = None
        
        mcmc_par1 = mcmc_sample[:,col_pair[0]]
        mcmc_par2 = mcmc_sample[:,col_pair[1]]

        abc_par1 = abc_sample[:, col_pair[0]] 
        abc_par2 = abc_sample[:, col_pair[1]] 

        ax = plt.subplot(gs[i])

        if sigma: 
            lvls = [1-np.exp(-0.5), 1-np.exp(-0.125)]
        else: 
            lvls = [0.68, 0.95]

        corner.hist2d(mcmc_par1, mcmc_par2, bins=20, range=plot_range, ax = ax, plot_datapoints=False,
                levels=lvls, color='#1F77B4', fill_contours=True, smooth=1.0, label=mcmc_label)
        
        corner.hist2d(abc_par1, abc_par2, bins=20, range=plot_range, ax = ax, 
                levels=lvls, color='#FF7F0E', fill_contours=True, smooth=1.0, label=abc_label)

        ax.scatter(np.repeat(truths[col_pair[0]],2), np.repeat(truths[col_pair[1]],2), 
                s=100, marker='*', c='k', lw=0, label=None) 
        #ax.axvline(truths[i_col], color='k', ls='--', linewidth=3)
        #ax.set_xticklabels([])
        ax.set_xlim([plot_range[0, 0] , plot_range[0, 1]])
        ax.set_ylim([plot_range[1, 0] , plot_range[1, 1]])
        
        if i == 2: 
            thick_line1 = mlines.Line2D([], [], ls='-', c='#FF7F0E', linewidth=12, alpha=0.5,
                                        label='ABC-PMC')
            ax.legend(loc='upper right', handles=[thick_line1],
                      frameon=False, fontsize=25, handletextpad=0.1, scatteryoffsets=[0.5])
        elif i == 1: 
            thick_line2 = mlines.Line2D([], [], ls='-', c='#1F77B4', linewidth=12, alpha=0.5, 
                                        label='$\mathcal{L}^\mathtt{Gauss}$ \nMCMC')
            ax.legend(loc='upper right', handles=[thick_line2],
                      frameon=False, fontsize=25, handletextpad=0.1, scatteryoffsets=[0.5])

        ax.set_xlabel(par_labels[col_pair[0]], fontsize=25, labelpad=15)
        ax.set_ylabel(par_labels[col_pair[1]], fontsize=25)
    
    if sigma: 
        sigma_str = '.true1sigma'
    else: 
        sigma_str = ''

    fig.subplots_adjust(wspace=0.3)
    fig_name = ''.join([ut.fig_dir(), 
        'paper.ABCvsMCMC.contour', 
        '.', obvs, 
        sigma_str, 
        '.pdf'])
    fig.savefig(fig_name, bbox_inches='tight', dpi=150) 
    return None 
示例#35
0
文件: paper.py 项目: mjvakili/ccppabc
def PoolEvolution(obvs): 
    ''' Demostrative plot for the evolution of the pool. Illustrate the pool evolution for
    log M_min versus log M_1, which has the starkest evolution from its prior. 

    '''
    if obvs == 'nbargmf':
        result_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/run1/',])
        theta_file = lambda tt: ''.join([result_dir, 'nbar_gmf_theta_t', str(tt), '.ABCnbargmf.dat']) 
        t_list = [0, 1, 2, 3, 5, 8]
    elif obvs == 'nbarxi':
        result_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/'])
        theta_file = lambda tt: ''.join([result_dir, 'nbar_xi_theta_t', str(tt), '.abc.dat']) 
        t_list = [0, 1, 2, 3, 7, 9]
    else:
        raise ValueError
    
    prior_min, prior_max = PriorRange('first_try')
    prior_range = np.zeros((2,2))
    prior_range[:,0] = np.array([prior_min[2], prior_min[4]])
    prior_range[:,1] = np.array([prior_max[2], prior_max[4]])

    # true HOD parameter
    true_dict = Data.data_hod_param(Mr=21)
    true_pair = [true_dict['logMmin'], true_dict['logM1']]

    prettyplot()
    pretty_colors = prettycolors()
    fig = plt.figure(figsize=(12,8))
    all_fig = fig.add_subplot(111) 

    for i_t, t in enumerate(t_list): 
        sub = fig.add_subplot(2, len(t_list)/2, i_t+1)

        theta_Mmin, theta_M1 = np.loadtxt(theta_file(t), unpack=True, usecols=[2, 4]) 
        corner.hist2d(theta_Mmin, theta_M1, bins=20, range=prior_range, 
                levels=[0.68, 0.95], color='c', fill_contours=True, smooth=1.0)
        
        t_label = r"$\mathtt{t = "+str(t)+"}$"
        sub.text(13.0, 15.0, t_label, fontsize=25) 

        if i_t == len(t_list) - 1: 
            true_label = r'$``\mathtt{true}"$'
        else: 
            true_label = None 

        plt.scatter(np.repeat(true_pair[0],2), np.repeat(true_pair[1],2), 
                s=75, marker='*', c='k', lw=0, label=true_label) 
        if i_t == len(t_list) - 1: 
            plt.legend(loc='lower left', scatterpoints=1, markerscale=2.5, 
                    handletextpad=-0.25, scatteryoffsets=[0.5])

        if i_t == 0: 
            sub.set_xticklabels([])
            sub.set_yticklabels([13., 13.5, 14., 14.5, 15., 15.5])
        elif (i_t > 0) and (i_t < len(t_list)/2): 
            sub.set_xticklabels([])
            sub.set_yticklabels([])
        elif i_t == len(t_list)/2: 
            sub.set_yticklabels([13., 13.5, 14., 14.5, 15.])
        elif i_t > len(t_list)/2: 
            sub.set_yticklabels([])

    all_fig.set_xticklabels([])
    all_fig.set_yticklabels([])
    all_fig.set_ylabel(
            r'$\mathtt{log}\;\mathcal{M}_\mathtt{1}$', 
            fontsize=30, labelpad=50)
    all_fig.set_xlabel(
            r'$\mathtt{log}\;\mathcal{M}_\mathtt{min}$', 
            fontsize=30, labelpad=25)

    fig.subplots_adjust(hspace=0.0)
    fig_name = ''.join([ut.fig_dir(), 
        'paper_ABC_poolevolution', 
        '.', obvs, 
        '.pdf'])
    fig.savefig(fig_name, bbox_inches='tight', dpi=150) 
    plt.close()
示例#36
0
f = pyfits.open("%s/table_for_paper.fits" % direc)
a = f[1].data
f.close()

feh = a['cannon_m_h']
am = a['cannon_alpha_m']
snr = a['snrg']

choose = snr > 20
print(sum(choose))
print(len(choose))

fig, ax = plt.subplots(1, 1, sharex=True, sharey=True, figsize=(8, 5))
hist2d(feh[choose],
       am[choose],
       ax=ax,
       bins=100,
       range=[[-2.2, .9], [-0.2, 0.5]])
ax.set_xlabel("[Fe/H] (dex)" + " from Cannon/LAMOST", fontsize=16)
fig.text(0.04,
         0.5,
         r"$\mathrm{[\alpha/M]}$" + " (dex) from Cannon/LAMOST",
         fontsize=16,
         va='center',
         rotation='vertical')
label = r"Objects with SNR \textgreater 20"
props = dict(boxstyle='round', facecolor='white')
ax.tick_params(axis='x', labelsize=16)
ax.tick_params(axis='y', labelsize=16)
ax.text(0.05,
        0.85,
示例#37
0
# 
# # Save current HMXB parameters as an object
# HMXB = np.array([HMXB_ra, HMXB_dec, HMXB_Porb, HMXB_ecc, HMXB_M2, HMXB_vsys, HMXB_Lx, HMXB_theta])
# pickle.dump( HMXB, open( INDATA("SMC_MCMC_HMXB.obj"), "wb" ) )
# 
# print "Finished calculating forward evolution."





# HMXB Orbital period vs. eccentricity
fig, ax = plt.subplots(3, 1, figsize=(6,8))
plt.rc('font', size=14)
plt_range = ([0, 4], [0,1])
corner.hist2d(np.log10(HMXB[2]), HMXB[3], ax=ax[0], bins=40, range=plt_range, plot_datapoints=False)
ax[0].set_xlabel(r"${\rm log}\ P_{\rm orb}\ {\rm (days)}$", size=16)
ax[0].set_ylabel(r"$e$", size=16)
ax[0].set_xticks([0,1,2,3,4])
ax[0].set_yticks([0,0.25,0.5,0.75,1.0])

plt_range = ([8, 24], [0,80])
corner.hist2d(HMXB[4], HMXB[5], ax=ax[1], bins=40, range=plt_range, plot_datapoints=False)
ax[1].set_xlabel(r"$M_2\ ({\rm M}_{\odot})$", size=16)
ax[1].set_ylabel(r"$v_{\rm sys}\ {\rm (km\ s}^{-1})$", size=16)
ax[1].set_xticks([8,12,16,20,24])
ax[1].set_yticks([0,20, 40, 60, 80])


# Get flight time from birth time and M1 lifetime
load_sse.load_sse()
示例#38
0
tab = Table.read(
    fourteenB_HI_data_wGBT_path(
        "tables/hi_co_gaussfit_column_densities_perpix.fits"))

# Don't consider the "bad fits" that are probably due to multiple components
good_pts = np.logical_and(~tab['multicomp_flag_HI'], ~tab['multicomp_flag_CO'])
good_pts = np.logical_and(good_pts, tab["sigma_HI"] > 3800)
# Minimum CO line width of one channel.
good_pts = np.logical_and(good_pts, tab["sigma_CO"] >= 2600)

ax2 = fig.add_axes((0.15, 0.1, 0.8, 0.35))

hist2d(tab['sigma_HI'][good_pts] / 1000.,
       np.array(tab['sigma_CO'])[good_pts] / 1000.,
       bins=13,
       data_kwargs={"alpha": 0.5},
       ax=ax2)
plt.xlabel(r"$\sigma_{\rm HI}$ (km/s)")
plt.ylabel(r"$\sigma_{\rm CO}$ (km/s)")

# From line_ratios_analysis.py
slope_ratio = 0.56318
plt.plot([4, 12], [4. * slope_ratio, 12. * slope_ratio],
         '--',
         color=sb.color_palette()[1],
         linewidth=3,
         label='Ratio Fit',
         alpha=0.8)

# plt.plot([4, 12], [4, 12], '-.', linewidth=3, alpha=0.6,
示例#39
0
def logSFR_initiate(SHsnaps, indices, theta_sfh=None, theta_sfms=None, testing=False):
    ''' initiate log SFR function for Evolver.Evolve() method
    '''
    nsnap0 = SHsnaps['metadata']['nsnap0']
    mu_sfr0 = SFR_sfms(SHsnaps['m.star0'][indices], UT.z_nsnap(SHsnaps['nsnap_start'][indices]), theta_sfms)

    if theta_sfh['name'] == 'constant_offset':
        # constant d_logSFR 
        F_sfr = _logSFR_dSFR 
        sfr_kwargs = {'dSFR': SHsnaps['sfr0'][indices] - mu_sfr0,  # offset
                'theta_sfms': theta_sfms}

    elif theta_sfh['name'] == 'corr_constant_offset': 
        # constant d_logSFR (assigned based on halo accretion rate) 
        #dSFR0 = SHsnaps['sfr0'][indices] - mu_sfr

        # now rank order it based on halo accretion rate
        dMhalo = SHsnaps['halo.m'][indices] - SHsnaps['halo.m0'][indices]
        
        m_kind = SHsnaps[theta_sfh['m.kind']+'0'][indices] # how do we bin the SFRs?
        dm_kind = theta_sfh['dm.kind'] # bins to rank order
    
        # scatter from noise -- subtract intrinsic assembly bias scatter from sig_SFMS 
        sig_noise = np.sqrt(0.3**2 - theta_sfh['sig_abias']**2)

        # slow and inefficient 

        dsfr = np.repeat(-999., len(dMhalo))
        for nn in np.arange(1, 21): 
            started = np.where(SHsnaps['nsnap_start'][indices] == nn)[0]

            m_kind_bin = np.arange(m_kind[started].min(), m_kind[started].max()+dm_kind, dm_kind)
            ibin = np.digitize(m_kind[started], m_kind_bin) 
            for i in np.unique(ibin): 
                inbin = np.where(ibin == i)

                isort = np.argsort(dMhalo[started[inbin]])

                dsfr[started[inbin[0][isort]]] = \
                        np.sort(np.random.randn(len(inbin[0]))) * theta_sfh['sig_abias']

        assert dsfr.min() != -999.
        
        if sig_noise > 0.: 
            dsfr += sig_noise * np.random.randn(len(dMhalo))
        
        # correct initial SFR to match long-term assembly bias 
        SHsnaps['sfr0'][indices] = mu_sfr0 + dsfr

        F_sfr = _logSFR_dSFR 

        sfr_kwargs = {'dSFR': dsfr, 'theta_sfms': theta_sfms}

    elif theta_sfh['name'] == 'random_step': 
        # completely random 
        # amplitude is sampled randomly from a Gaussian with sig_logSFR = 0.3 
        # time steps are sampled randomly from a unifrom distribution [dt_min, dt_max]
        if 'dt_min' not in theta_sfh: 
            raise ValueError
        if 'dt_max' not in theta_sfh: 
            raise ValueError
                
        # Random step function duty cycle 
        del_t_max = UT.t_nsnap(1) - UT.t_nsnap(nsnap0) #'nsnap_start'][indices].max()) 
        
        # the range of the steps 
        tshift_min = theta_sfh['dt_min'] 
        tshift_max = theta_sfh['dt_max'] 

        # get the times when the amplitude changes 
        n_col = int(np.ceil(del_t_max/tshift_min))+1  # number of columns 
        n_gal = len(indices)

        tshift = np.zeros((n_gal, n_col))
        tshift[:,1:] = np.random.uniform(tshift_min, tshift_max, size=(n_gal, n_col-1))
        tsteps = np.cumsum(tshift , axis=1) + np.tile(UT.t_nsnap(SHsnaps['nsnap_start'][indices]), (n_col, 1)).T
        del tshift
        # make sure everything evolves properly until the end
        assert tsteps[range(n_gal), n_col-1].min() > UT.t_nsnap(1)

        dlogSFR_amp = np.random.randn(n_gal, n_col) * theta_sfh['sigma']
        dlogSFR_amp[:,0] = SHsnaps['sfr0'][indices] - mu_sfr0

        F_sfr = _logSFR_dSFR_tsteps
        
        sfr_kwargs = {'dlogSFR_amp': dlogSFR_amp, 'tsteps': tsteps,'theta_sfms': theta_sfms}
    
    elif theta_sfh['name'] == 'random_step_fluct': 
        # completely random amplitude that is sampled from a Gaussian with sig_logSFR
        # EXCEPT each adjacent timesteps have alternating sign amplitudes
        # time steps have width specified tduty 
        dt_tot= UT.t_nsnap(1) - UT.t_nsnap(nsnap0) # total cosmic time of evolution 

        tduty = theta_sfh['tduty'] # dutycycle time

        # get the times when the amplitude changes 
        n_col = int(np.ceil(dt_tot / tduty))+2  # number of columns 
        n_gal = len(indices)    # number of galaxies
        tshift = np.tile(tduty, (n_gal, n_col))
        tshift[:,0] = 0.
        tshift[:,1] = np.random.uniform(0., tduty, n_gal) 
        tsteps = np.cumsum(tshift , axis=1) + np.tile(UT.t_nsnap(SHsnaps['nsnap_start'][indices]), (n_col, 1)).T
        del tshift
        # make sure everything evolves properly until the end
        assert tsteps[range(n_gal), n_col-1].min() > UT.t_nsnap(1)
        
        # dlogSFR absolute amplitue 
        dlogSFR_amp = np.abs(np.random.randn(n_gal, n_col)) * theta_sfh['sigma']
        dlogSFR_amp[:,0] = SHsnaps['sfr0'][indices] - mu_sfr0 # dlogSFR at nsnap_start 

        # now make every other time step has fluctuating signs!
        pos = dlogSFR_amp[:,0] >= 0.
        neg = ~pos

        fluct = np.ones(n_col-1)
        fluct[2 * np.arange(int(np.ceil(float(n_col-1)/2.)))] *= -1.
        
        dlogSFR_amp[pos,1:] *= fluct 
        dlogSFR_amp[neg,1:] *= -1. * fluct

        # testing the distribution of delta SFR
        if testing: 
            for i in range(n_col):
                print('std of dlogSFR amp = %f' % np.std(dlogSFR_amp))
                plt.hist(dlogSFR_amp[:,i], range=(-1., 1.), 
                        linewidth=2, histtype='step', density=True, label='Step '+str(i)) 
                plt.legend()
                plt.xlabel(r'$\Delta\log\,\mathrm{SFR}$', fontsize=20)
                plt.xlim([-1., 1.])
            plt.savefig(''.join([UT.fig_dir(), 'random_step_fluct_test.png']), bbox_inches='tight')

        F_sfr = _logSFR_dSFR_tsteps
        sfr_kwargs = {'dlogSFR_amp': dlogSFR_amp, 'tsteps': tsteps,'theta_sfms': theta_sfms}

    elif theta_sfh['name'] == 'random_step_abias_dt': 
        # SFH where the amplitude w.r.t. the SFS is correlated with halo mass growth over 
        # tdyn(t). The amplitude is sampled at every time step specified by `tduty`
        dt_tot= UT.t_nsnap(1) - UT.t_nsnap(nsnap0) # total cosmic time of evolution 
        tduty = theta_sfh['tduty'] # dutycycle time
        sigma_int = np.sqrt(theta_sfh['sigma_tot']**2 - theta_sfh['sigma_corr']**2) # intrinsic sigma 

        # get the times when the amplitude changes 
        n_col = int(np.ceil(dt_tot / tduty))+2  # number of columns 
        n_gal = len(indices)    # number of galaxies
        tshift = np.tile(tduty, (n_gal, n_col))
        tshift[:,0] = 0.
        tshift[:,1] = np.random.uniform(0., tduty, n_gal) 
        tsteps = np.cumsum(tshift , axis=1) + \
                np.tile(UT.t_nsnap(SHsnaps['nsnap_start'][indices]), (n_col, 1)).T
        del tshift

        # M_h of the galaxies throughout the snapshots 
        Mh_snaps = np.zeros((n_gal, nsnap0+9), dtype=np.float32)
        Mh_snaps[:,0] = SHsnaps['halo.m'][indices]
        Mm_snaps = np.zeros((n_gal, nsnap0+9), dtype=np.float32)
        Mm_snaps[:,0] = SHsnaps['m.max'][indices]
        for isnap in range(2, nsnap0+10): 
            Mh_snaps[:,isnap-1] = SHsnaps['halo.m.snap'+str(isnap)][indices]
            Mm_snaps[:,isnap-1] = SHsnaps['m.max.snap'+str(isnap)][indices]
        
        t_snaps = UT.t_nsnap(range(1, nsnap0+10))

        # now we need to use these M_h to calculate the halo growth rates
        # at every time step t_i over the range t_i - dt_dMh to t_i. This means
        # we need to get M_h at both t_i and t_i-dt_dMh...
        f_dMh = np.zeros(tsteps.shape, dtype=np.float32) # growth rate of halos over t_i and t_i - dt_Mh
        #Mh_ts = np.zeros(tsteps.shape, dtype=np.float32)
        Mm_ts = np.zeros(tsteps.shape, dtype=np.float32)
        iii, iiii = 0, 0 
        for i_g in range(n_gal): 
            in_sim = (Mm_snaps[i_g,:] > 0.)
            t_snaps_i = t_snaps[in_sim]
            Mh_snaps_i = np.power(10., Mh_snaps[i_g, in_sim]) 
            Mm_snaps_i = np.power(10., Mm_snaps[i_g, in_sim]) 
            
            t_i = tsteps[i_g,:] # t_i
            t_imdt = t_i - UT.tdyn_t(t_i) # t_i - tdyn 
            #t_imdt = tsteps[i_g,:] - theta_sfh['dt_dMh'] # t_i - dt_dMh

            Mh_ti = np.interp(t_i, t_snaps_i[::-1], Mh_snaps_i[::-1]) 
            Mh_timdt = np.interp(t_imdt, t_snaps_i[::-1], Mh_snaps_i[::-1]) 
            
            f_dMh[i_g,:] = Mh_ti / Mh_timdt #1. - Mh_timdt /  
            #Mh_ts[i_g,:] = np.log10(Mh_ti)
            Mm_ti = np.interp(t_i, t_snaps_i[::-1], Mm_snaps_i[::-1]) 
            Mm_ts[i_g,:] = np.log10(Mm_ti)
            
            if testing: 
                if (SHsnaps['nsnap_start'][indices][i_g] == 15) and (iii < 10): 
                    fig = plt.figure(figsize=(8,4)) 
                    sub = fig.add_subplot(121)
                    sub.plot(t_snaps_i, Mh_snaps_i/Mh_snaps_i[0], c='k', ls='--') 
                    sub.fill_between([t_imdt[0], t_i[0]], [0., 0.], [1.2, 1.2], color='C0')
                    sub.fill_between([t_imdt[-2], t_i[-2]], [0., 0.], [1.2, 1.2], color='C1')
                    sub.vlines(UT.t_nsnap(15), 0., 1.2) 
                    sub.vlines(UT.t_nsnap(1),  0., 1.2) 
                    sub.set_xlim([0., 14.]) 
                    sub.set_ylim([0., 1.2]) 

                    sub = fig.add_subplot(122)
                    sub.plot(t_i, f_dMh[i_g,:], c='k', ls='--') 
                    sub.scatter([t_i[0]], [f_dMh[i_g,0]], c='C0')
                    sub.scatter([t_i[-2]], [f_dMh[i_g,-2]], c='C1')
                    sub.vlines(UT.t_nsnap(15), -0.2, 5.) 
                    sub.vlines(UT.t_nsnap(1), -0.2, 5.) 
                    sub.set_xlim([0., 14.]) 
                    sub.set_ylim([0., 5.]) 

                    fig.savefig(''.join([UT.fig_dir(), 'abiastest', str(iii), '.png']), bbox_inches='tight')
                    plt.close()
                    iii += 1 
                
                if in_sim[15]: 
                    if Mh_snaps_i[0] < Mh_snaps_i[14]: 
                        if iiii > 10: continue 
                        fig = plt.figure(figsize=(8,4)) 
                        sub = fig.add_subplot(121)
                        sub.plot(t_snaps_i, Mh_snaps_i/Mh_snaps_i[0], c='k', ls='--') 
                        sub.fill_between([t_imdt[0], t_i[0]], [0., 0.], [1.2, 1.2], color='C0')
                        sub.fill_between([t_imdt[-2], t_i[-2]], [0., 0.], [1.2, 1.2], color='C1')
                        sub.vlines(UT.t_nsnap(15), 0., 1.2) 
                        sub.vlines(UT.t_nsnap(1),  0., 1.2) 
                        sub.set_xlim([0., 14.]) 
                        sub.set_ylim([0., 1.2]) 

                        sub = fig.add_subplot(122)
                        sub.plot(t_i, f_dMh[i_g,:], c='k', ls='--') 
                        sub.scatter([t_i[0]], [f_dMh[i_g,0]], c='C0')
                        sub.scatter([t_i[-2]], [f_dMh[i_g,-2]], c='C1')
                        sub.vlines(UT.t_nsnap(15), -0.2, 1.) 
                        sub.vlines(UT.t_nsnap(1), -0.2, 1.) 
                        sub.set_xlim([0., 14.]) 
                        sub.set_ylim([0., 5.]) 
                        sub.text(0.05, 0.05, r'$\log\,M_h='+str(round(np.log10(Mh_snaps_i[0]),2))+'$',
                                fontsize=15, ha='left', va='bottom', transform=sub.transAxes)
                        fig.savefig(''.join([UT.fig_dir(), 'weird_abiastest', str(iiii), '.png']), bbox_inches='tight')
                        plt.close()
                        iiii += 1

        # calculate the d(log SFR) amplitude at t_steps 
        # at each t_step, for a given halo mass bin of 0.2 dex, 
        # rank order the SFRs and the halo growth rate 
        dlogSFR_amp = np.zeros(f_dMh.shape, dtype=np.float32)
        dlogMh = 0.1
        for ii in range(f_dMh.shape[1]): 
            f_dMh_i = f_dMh[:,ii]
            Mh_ti = Mm_ts[:,ii] 
            mh_bins = np.arange(Mh_ti.min(), Mh_ti.max(), dlogMh)
            #mh_bins = np.linspace(Mh_ti.min(), Mh_ti.max(), 100)
            
            if testing: 
                fig = plt.figure(1, figsize=(4*np.int(np.ceil(float(len(mh_bins))/3.)+1.), 8))
            for i_m in range(len(mh_bins)-1): 
                inbin = ((Mh_ti >= mh_bins[i_m]) & (Mh_ti < mh_bins[i_m]+dlogMh))
                #inbin = ((Mh_ti >= mh_bins[i_m]) & (Mh_ti < mh_bins[i_m+1]))
                n_bin = np.sum(inbin)
                 
                isort = np.argsort(-1. * f_dMh_i[inbin])

                irank = np.zeros(n_bin)
                irank[isort] = (np.arange(n_bin) + 0.5)/np.float(n_bin)
                
                # i_rank = 1/2 (1 - erf(x/sqrt(2)))
                # x = (SFR - avg_SFR)/sigma_SFR
                # dSFR = sigma_SFR * sqrt(2) * erfinv(1 - 2 i_rank)
                dlogSFR_amp[inbin, ii] = \
                        theta_sfh['sigma_corr'] * 1.41421356 * erfinv(1. - 2. * irank) 
                
                if testing: 
                    sub = fig.add_subplot(3, np.int(np.ceil(np.float(len(mh_bins))/3.)+1), i_m+1)
                    sub.scatter(f_dMh_i[inbin], 0.3 * np.random.randn(n_bin), c='k', s=2)
                    sub.scatter(f_dMh_i[inbin], dlogSFR_amp[inbin, ii] + sigma_int * np.random.randn(n_bin), c='r', s=2, lw=0)
                    sub.set_xlim([-0.5, 1.])
                    sub.set_ylim([-1., 1.])
            if testing: 
                plt.savefig(''.join([UT.fig_dir(), 'random_step_abias_dt_test', str(ii), '.png']), bbox_inches='tight')
                plt.close()

                fig = plt.figure(figsize=(8,4)) 
                sub = fig.add_subplot(121)
                DFM.hist2d(Mh_ti, f_dMh_i, levels=[0.68, 0.95], range=[[10., 14.],[0., 10.]], color='k', 
                        plot_datapoints=False, fill_contours=False, plot_density=True, ax=sub)
                sub.set_xlim([10., 14.]) 
                sub = fig.add_subplot(122)
                DFM.hist2d(Mh_ti, dlogSFR_amp[:,ii], levels=[0.68, 0.95], range=[[10., 14.],[-1., 1.]], color='k', 
                        plot_datapoints=False, fill_contours=False, plot_density=True, ax=sub)
                sub.set_xlim([10., 14.]) 
                fig.savefig(''.join([UT.fig_dir(), 'abias_fdMh', str(ii), '.png']), bbox_inches='tight')
                plt.close()

        del f_dMh
        del Mm_ts
        
        # add in intrinsic scatter
        dlogSFR_int = np.random.randn(n_gal, n_col) * sigma_int 
        dlogSFR_amp += dlogSFR_int
        SHsnaps['sfr0'][indices] = mu_sfr0 + dlogSFR_amp[:,0] # change SFR_0 so that it's assembly biased as well
        
        F_sfr = _logSFR_dSFR_tsteps
        sfr_kwargs = {'dlogSFR_amp': dlogSFR_amp, 'tsteps': tsteps,'theta_sfms': theta_sfms}
    else:
        raise NotImplementedError
    return F_sfr, sfr_kwargs
示例#40
0
def priorSample(ngauss=128,
                quantile=0.5,
                iter='8th',
                survey='2MASS',
                dataFilename='All.npz',
                Nsamples=1.2e6,
                xdgmmFilename='xdgmm.fit',
                xlabel='X',
                ylabel='Y',
                contourColor='k'):

    setup_text_plots(fontsize=16, usetex=True)

    xdgmm = XDGMM(filename=xdgmmFilename)
    figPrior = plt.figure(figsize=(12, 5.5))
    figPrior.subplots_adjust(left=0.1,
                             right=0.95,
                             bottom=0.15,
                             top=0.95,
                             wspace=0.1,
                             hspace=0.1)
    sample = xdgmm.sample(Nsamples)
    negParallax = sample[:, 1] < 0
    nNegP = np.sum(negParallax)
    while nNegP > 0:
        sampleNew = xdgmm.sample(nNegP)
        sample[negParallax] = sampleNew
        negParallax = sample[:, 1] < 0
        nNegP = np.sum(negParallax)

    samplex = sample[:, 0]
    sampley = testXD.absMagKinda2absMag(sample[:, 1])
    ax3 = figPrior.add_subplot(121)
    alpha = 0.1
    xlim = [-0.25, 1.25]
    ylim = [6, -6]

    levels = 1.0 - np.exp(-0.5 * np.arange(1.0, 2.1, 1.0)**2)
    corner.hist2d(samplex,
                  sampley,
                  ax=ax3,
                  levels=levels,
                  bins=200,
                  plot_datapoints=False,
                  no_fill_contours=True,
                  plot_density=False,
                  color=contourColor)
    ax3.scatter(samplex, sampley, s=1, lw=0, c='k', alpha=alpha)

    ax4 = figPrior.add_subplot(122)
    for i in range(xdgmm.n_components):
        points = drawEllipse.plotvector(xdgmm.mu[i], xdgmm.V[i])
        ax4.plot(points[0, :],
                 testXD.absMagKinda2absMag(points[1, :]),
                 'k-',
                 alpha=xdgmm.weights[i] / np.max(xdgmm.weights))

    titles = [
        "Extreme Deconvolution\n  resampling",
        "Extreme Deconvolution\n  cluster locations"
    ]

    ax = [ax3, ax4]

    for i in range(2):
        ax[i].set_xlim(xlim)
        ax[i].set_ylim(ylim[0], ylim[1] * 1.1)
        ax[i].text(0.05,
                   0.95,
                   titles[i],
                   ha='left',
                   va='top',
                   transform=ax[i].transAxes,
                   fontsize=18)

        ax[i].set_xlabel(xlabel, fontsize=18)
        if i in (1, 3):
            ax[i].yaxis.set_major_formatter(plt.NullFormatter())
        else:
            ax[i].set_ylabel(ylabel, fontsize=18)

    figPrior.savefig('prior_ngauss' + str(ngauss) + '.png')
示例#41
0
def corner_plot(s, labels, extents, bf, truth):
    """ Plotting function to visualise the gaussian peaks found by the sampler function. 2D contour plots of tq against tau are plotted along with kernelly smooth histograms for each parameter.
        
        :s:
         Array of shape (#, 2) for either the smooth or disc produced by the emcee EnsembleSampler in the sample function of length determined by the number of walkers which resulted at the specified peak.
        
        :labels:
        List of x and y axes labels i.e. disc or smooth parameters
        
        :extents:
        Range over which to plot the samples, list shape [[xmin, xmax], [ymin, ymax]]
        
        :bf:
        Best fit values for the distribution peaks in both tq and tau found from mapping the samples. List shape [(tq, poserrtq, negerrtq), (tau, poserrtau, negerrtau)]
        :truth:
        Known t and tau values - generally for test data
        
        RETURNS:
        :fig:
        The figure object
        """
    x, y = s[:,0], s[:,1]
    fig = P.figure(figsize=(6.25,6.25))
    ax2 = P.subplot2grid((3,3), (1,0), colspan=2, rowspan=2)
    ax2.set_xlabel(labels[0])
    ax2.set_ylabel(labels[1])
    corner.hist2d(x, y, ax=ax2, bins=100, extent=extents, plot_contours=True)
    ax2.axvline(x=bf[0][0], linewidth=1)
    ax2.axhline(y=bf[1][0], linewidth=1)
    ax2.axvline(x=truth[0], c='r', linewidth=1)
    ax2.axhline(y=truth[1], c='r', linewidth=1)
    [l.set_rotation(45) for l in ax2.get_xticklabels()]
    [j.set_rotation(45) for j in ax2.get_yticklabels()]
    ax2.tick_params(axis='x', labeltop='off')
    ax1 = P.subplot2grid((3,3), (0,0),colspan=2)
    #ax1.hist(x, bins=100, range=(extents[0][0], extents[0][1]), normed=True, histtype='step', color='k')
    den = kde.gaussian_kde(x[np.logical_and(x>=extents[0][0], x<=extents[0][1])])
    pos = np.linspace(extents[0][0], extents[0][1], 750)
    ax1.plot(pos, den(pos), 'k-', linewidth=1)
    ax1.axvline(x=bf[0][0], linewidth=1)
    ax1.axvline(x=bf[0][0]+bf[0][1], c='b', linestyle='--')
    ax1.axvline(x=bf[0][0]-bf[0][2], c='b', linestyle='--')
    ax1.set_xlim(extents[0][0], extents[0][1])
    ax1.axvline(x=truth[0], c='r', linewidth=1)
    #    ax12 = ax1.twiny()
    #    ax12.set_xlim((extent[0][0], extent[0][1])
    #ax12.set_xticks(np.array([1.87, 3.40, 6.03, 8.77, 10.9, 12.5]))
    #ax12.set_xticklabels(np.array([3.5, 2.0 , 1.0, 0.5, 0.25, 0.1]))
    #    [l.set_rotation(45) for l in ax12.get_xticklabels()]
    #    ax12.tick_params(axis='x', labelbottom='off')
    #    ax12.set_xlabel(r'$z$')
    ax1.tick_params(axis='x', labelbottom='off', labeltop='off')
    ax1.tick_params(axis='y', labelleft='off')
    ax3 = P.subplot2grid((3,3), (1,2), rowspan=2)
    ax3.tick_params(axis='x', labelbottom='off')
    ax3.tick_params(axis='y', labelleft='off')
    #ax3.hist(y, bins=100, range=(extents[1][0], extents[1][1]), normed=True, histtype='step',color='k', orientation ='horizontal')
    den = kde.gaussian_kde(y[np.logical_and(y>=extents[1][0], y<=extents[1][1])])
    pos = np.linspace(extents[1][0], extents[1][1], 750)
    ax3.plot(den(pos), pos, 'k-', linewidth=1)
    ax3.axhline(y=bf[1][0], linewidth=1)
    ax3.axhline(y=bf[1][0]+bf[1][1], c='b', linestyle='--')
    ax3.axhline(y=bf[1][0]-bf[1][2], c='b', linestyle='--')
    ax3.set_ylim(extents[1][0], extents[1][1])
    ax3.axhline(y=truth[1], c='r', linewidth=1)
    P.tight_layout()
    P.subplots_adjust(wspace=0.0)
    P.subplots_adjust(hspace=0.0)
    return fig
示例#42
0
def Evolve(sfh='constant_offset'): 
    sh = Cat.CentralSubhalos(
            sigma_smhm=0.2, 
            smf_source='li-march', 
            nsnap0=15) 
    shcat = sh.Read(downsampled='20') 
    tt =  Evo.defaultTheta(sfh) 

    t0 = time.time()
    shcat = Evo.Evolve(shcat, tt) 
    print('Evolve takes %f sec' % (time.time()-t0))

    nsnap0 = shcat['metadata']['nsnap0'] 
    isSF = (shcat['galtype'] == 'sf')
        
    fig = plt.figure(figsize=(12,4))
    # stellar mass function 
    sub = fig.add_subplot(131)
    for n in range(2, nsnap0)[::-1]: 
        # identify SF population at snapshot
        smf_sf = Obvs.getMF(shcat['m.star.snap'+str(n)][isSF], 
                weights=shcat['weights'][isSF])
        sub.plot(smf_sf[0], smf_sf[1], lw=2, c='b', alpha=0.05 * (21. - n))        

    smf_sf = Obvs.getMF(shcat['m.star'][isSF], weights=shcat['weights'][isSF])
    sub.plot(smf_sf[0], smf_sf[1], lw=3, c='b', ls='-', label='Integrated')

    smf_sf_msham = Obvs.getMF(shcat['m.sham'][isSF], weights=shcat['weights'][isSF])
    sub.plot(smf_sf_msham[0], smf_sf_msham[1], lw=3, c='k', ls='--', label='SHAM')
    sub.set_xlabel('Stellar Masses $(\mathcal{M}_*)$', fontsize=25)
    sub.set_xlim([9., 11.5])
    sub.set_ylim([1e-5, 10**-1.75])
    sub.set_yscale('log')
    sub.set_ylabel('log $\Phi$', fontsize=25)
    sub.legend(loc='upper right', prop={'size': 20}) 
    
    sub = fig.add_subplot(132) # Star Forming Sequence 
    DFM.hist2d(
            shcat['m.star'][isSF], 
            shcat['sfr'][isSF], 
            weights=shcat['weights'][isSF], 
            levels=[0.68, 0.95], range=[[9., 12.], [-3., 1.]], 
            bins=20, plot_datapoints=False, fill_contours=False, plot_density=True, ax=sub) 
    sub.set_xlabel('log $(\; M_*\; [M_\odot]\;)$', fontsize=25)
    sub.set_xlim([9., 11.5])
    sub.set_xticks([9., 10., 11.]) 
    sub.set_ylabel('log $(\;\mathrm{SFR}\;[M_\odot/\mathrm{yr}])$', fontsize=25)
    sub.set_ylim([-2.5, 1.])
    sub.set_yticks([-2., -1., 0., 1.])

    # stellar to halo mass relation 
    sub = fig.add_subplot(133)
    smhmr = Obvs.Smhmr()
    m_mid, mu_mstar, sig_mstar, cnts = smhmr.Calculate(shcat['halo.m'][isSF], shcat['m.star'][isSF])
    sub.errorbar(m_mid, mu_mstar, yerr=sig_mstar)
    sub.fill_between(m_mid, mu_mstar - 0.2, mu_mstar + 0.2, color='k', alpha=0.25, linewidth=0, edgecolor=None)

    sub.set_xlabel('Halo Mass $(\mathcal{M}_{halo})$', fontsize=25)
    sub.set_xlim([11., 14.])
    sub.set_ylabel('Stellar Mass $(\mathcal{M}_*)$', fontsize=25)
    sub.set_ylim([9., 12.])

    fig.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    fig.savefig(''.join([UT.fig_dir(), 'evolver.', sfh, '.png']), bbox_inches='tight')
    plt.close() 
    return None
示例#43
0
def groupcatSFMS(mrange=[10.6,10.8]): 
    '''Figure of the z~0 group catalog. 
    Panel a) SFR-M* relation 
    Panel b) P(SSFR) with SFMS fitting 
    '''
    # Read in Jeremy's group catalog  with Mr_cut = -18
    gc = Cat.Observations('group_catalog', Mrcut=18, position='central')
    gc_cat = gc.Read() 
    fig = plt.figure(figsize=(10,5)) 

    # fit the SFMS using lettalkaboutquench sfms fitting
    _fSFMS = fstarforms() 
    _fit_logm, _fit_logsfr = _fSFMS.fit(gc_cat['mass'], gc_cat['sfr'], method='gaussmix', fit_range=None)
    logsfr_ms = _fSFMS.powerlaw(logMfid=10.5) 
    print _fSFMS._powerlaw_m
    print _fSFMS._powerlaw_c

    fSFMS = fstarforms() 
    fit_logm, _ = fSFMS.fit(gc_cat['mass'], gc_cat['sfr'], method='gaussmix', fit_range=mrange)
    _, fit_fsfms = fSFMS.frac_SFMS()
    i_fit = np.abs(fit_logm - np.mean(mrange)).argmin()

    # log SFR - log M* highlighting where the SFMS lies 
    sub1 = fig.add_subplot(1,2,1)
    DFM.hist2d(gc_cat['mass'], gc_cat['sfr'], color='#ee6a50',
            levels=[0.68, 0.95], range=[[9., 12.], [-3.5, 1.5]], 
            plot_datapoints=True, fill_contours=False, plot_density=True, ax=sub1) 
    gc = Cat.Observations('group_catalog', Mrcut=18, position='central')
    gc_cat = gc.Read() 
    #sub1.vlines(mrange[0], -5., 2., color='k', linewidth=2, linestyle='--')
    #sub1.vlines(mrange[1], -5., 2., color='k', linewidth=2, linestyle='--')
    #sub1.fill_between(mrange, [2.,2.], [-5.,-5], color='#1F77B4', alpha=0.25)
    sub1.fill_between(mrange, [2.,2.], [-5.,-5], color='k', linewidth=0, alpha=0.25)
    print _fit_logm, _fit_logsfr
    sub1.plot(np.linspace(9.8, 11., 10), logsfr_ms(np.linspace(9.8, 11., 10)), c='k', linestyle='--') 
    sub1.set_xticks([9., 10., 11., 12.])
    sub1.set_xlabel('log$(\; M_*\; [M_\odot]\;)$', fontsize=20)
    sub1.set_yticks([-3., -2., -1., 0., 1.])
    sub1.set_ylabel('log$(\; \mathrm{SFR}\; [M_\odot/\mathrm{yr}]\;)$', fontsize=20)
    sub1.text(0.95, 0.1, 'SDSS central galaxies',
            ha='right', va='center', transform=sub1.transAxes, fontsize=20)

    # P(log SSFR) 
    sub2 = fig.add_subplot(1,2,2)
    inmbin = np.where((gc_cat['mass'] > mrange[0]) & (gc_cat['mass'] < mrange[1]))
    bedge, pp = np.histogram(gc_cat['ssfr'][inmbin], range=[-14., -9.], bins=32, normed=True)
    pssfr = UT.bar_plot(pp, bedge)
    sub2.plot(pssfr[0], pssfr[1], c='k', lw=2) 
    # overplot GMM component for SFMS
    gmm_weights = fSFMS._gmix_weights[i_fit]
    gmm_means = fSFMS._gmix_means[i_fit]
    gmm_vars = fSFMS._gmix_covariances[i_fit]
    icomp = gmm_means.argmax()
    xx = np.linspace(-14., -9, 100)
    sub2.fill_between(xx, np.zeros(len(xx)), gmm_weights[icomp]*MNorm.pdf(xx, gmm_means[icomp], gmm_vars[icomp]), 
            color='#1F77B4', linewidth=0)

    for i_comp in range(len(gmm_vars)): 
        if i_comp == 0: 
            gmm_tot = gmm_weights[i_comp]*MNorm.pdf(xx, gmm_means[i_comp], gmm_vars[i_comp])
        else: 
            gmm_tot += gmm_weights[i_comp]*MNorm.pdf(xx, gmm_means[i_comp], gmm_vars[i_comp])
    
    #sub2.plot(xx, gmm_tot, color='r', linewidth=2)

    sub2.set_xlim([-13.25, -9.5]) 
    sub2.set_xticks([-10., -11., -12., -13.][::-1])
    #sub2.set_xlim([-9.5, -13.25]) 
    #sub2.set_xticks([-10., -11., -12., -13.])
    sub2.set_xlabel('log$(\; \mathrm{SSFR}\; [\mathrm{yr}^{-1}]\;)$', fontsize=20)
    sub2.set_ylim([0., 1.5]) 
    sub2.set_yticks([0., 0.5, 1., 1.5])
    sub2.set_ylabel('$p\,(\;\mathrm{log}\; \mathrm{SSFR}\;)$', fontsize=20)
    # mass bin 
    sub2.text(0.5, 0.9, '$'+str(mrange[0])+'< \mathrm{log}\, M_* <'+str(mrange[1])+'$',
            ha='center', va='center', transform=sub2.transAxes, fontsize=20)
    sub2.text(0.9, 0.33, '$f_\mathrm{SFMS}='+str(round(fit_fsfms[i_fit],2))+'$',
            ha='right', va='center', transform=sub2.transAxes, fontsize=20)
    fig.subplots_adjust(wspace=.3)
    fig.savefig(''.join([UT.tex_dir(), 'figs/groupcat.pdf']), bbox_inches='tight', dpi=150) 
    plt.close()
    return None
示例#44
0
def plot_slice_2d(core,
                  x_pars,
                  y_pars,
                  titles,
                  ncols=3,
                  bins=30,
                  color='k',
                  title='',
                  suptitle='',
                  cmap='gist_rainbow',
                  fontsize=17,
                  publication_params=False,
                  save=False,
                  show=True,
                  thin=1,
                  plot_datapoints=True,
                  xlabel='',
                  ylabel='',
                  plot_density=False,
                  plot_contours=True,
                  no_fill_contours=True,
                  data_kwargs={
                      'alpha': 0.008,
                      'color': (0.12156, 0.466667, 0.70588, 1.0)
                  },
                  contour_kwargs={
                      'linewidths': 0.8,
                      'colors': 'k',
                      'levels': [150, 350]
                  },
                  **kwargs):
    """Function to plot 2d histograms of sliced analyses."""
    L = len(x_pars)
    if len(x_pars) != len(y_pars):
        raise ValueError('Lists x_pars and y_pars must be the same length!')

    nrows = int(L // ncols)
    if L % ncols > 0:
        nrows += 1

    fig = plt.figure()  # figsize=[6,8])
    for ii, (x_par, y_par, ti) in enumerate(zip(x_pars, y_pars, titles)):
        cell = ii + 1
        axis = fig.add_subplot(nrows, ncols, cell)
        corner.hist2d(core.get_param(x_par, to_burn=True)[::thin],
                      core.get_param(y_par, to_burn=True)[::thin],
                      bins=bins,
                      ax=axis,
                      color=color,
                      plot_datapoints=plot_datapoints,
                      no_fill_contours=no_fill_contours,
                      plot_density=plot_density,
                      plot_contours=plot_contours,
                      data_kwargs=data_kwargs,
                      contour_kwargs=contour_kwargs,
                      **kwargs)

        axis.set_title(ti)
        # axis.set_xlabel(x_par.decode())
        # axis.set_ylabel(y_par.decode())
        axis.set_xlim((0, 7))
        xticks = np.linspace(0, 7, 8)
        yticks = np.linspace(-18, -13, 6)

        axis.set_xticks(xticks)

        # Set inner xticks null
        if cell <= ((nrows - 1) * ncols):
            empty_x_labels = [''] * len(xticks)
            axis.set_xticklabels(empty_x_labels)
        elif (cell == (ncols * (nrows - 1))) and (L != ncols * nrows):
            empty_x_labels = [''] * len(xticks)
            axis.set_xticklabels(empty_x_labels)

        # Set inner yticks null
        if (cell % ncols != 1):
            empty_y_labels = [''] * len(yticks)
            axis.set_yticklabels(empty_y_labels)
        axis.set_ylim((-18, -13))

    fig.tight_layout(pad=0.4)
    fig.suptitle(suptitle, y=1.05, fontsize=19)
    fig.text(0.5, -0.02, xlabel, ha='center', usetex=False)
    fig.text(-0.02,
             0.5,
             ylabel,
             va='center',
             rotation='vertical',
             usetex=False)
    if save:
        plt.savefig(save, bbox_inches='tight')
    if show:
        plt.show()

    plt.close()
示例#45
0
axHistY = plt.subplot(gs1[3])
axHistY.set_ylim(TAUMIN,TAUMAX)
axHistY.xaxis.set_major_formatter( NullFormatter() )
axHistY.yaxis.set_major_formatter( NullFormatter() )
#axHistY.plot([0,1000],[tau_mcmc[0],tau_mcmc[0]],color='blue')

#axPic = plt.subplot(gs1[1])
#axPic.imshow(img)
#axPic.axis('off')

#perform smoothing of data
#unsmooth = N.histogram(x,bins=BINS,weights=weighted,range=[TQMIN,TQMAX])[0]
#x = N.transpose(lowess(unsmooth, N.arange(TQMAX/BINS,TQMAX+TQMAX/BINS,TQMAX/BINS), is_sorted=True, frac=0.025, it=0))[1]
#print(x)
#plot histogram
c.hist2d(x,y,ax=axScatter,weights=weighted,range=[[TQMIN,TQMAX],[TAUMIN,TAUMAX]],bins=BINS,max_n_ticks=8,normed=True,smooth=2)
axScatter.text(0.5,0.5,r'$0.5<z<1.0$')
axScatter.text(0.5,0.3,r'$\log(1+\delta)<-0.25$')

#axScatter.hist2d(x,y,weights=weighted,range=[[TQMIN,TQMAX],[TAUMIN,TAUMAX]],bins=150,normed=True)
#axScatter.plot([tq_mcmc[0],tq_mcmc[0]],[0,5],color='blue')
#axScatter.plot([0,100],[tau_mcmc[0],tau_mcmc[0]],color='blue')

#perform smoothing of data
#unsmooth = np.histogram(x,bins=BINS,weights=weighted,range=[TQMIN,TQMAX])[0]

#weighted = smooth(weighted)


axHistY.plot(yweights,N.arange(TAUMAX/BINS,TAUMAX + TAUMAX/BINS,TAUMAX/BINS),color='Black')
axHistX.plot(N.arange(TQMAX/BINS,TQMAX + TQMAX/BINS,TQMAX/BINS),xweights,color='Black')
#     raw_input("{0}, {1}?".format(y,x))
#     p.clf()

good_pts = np.logical_and(good_pts, tab["sigma_HI"] > 3800)

# Minimum CO line width of one channel.
good_pts = np.logical_and(good_pts, tab["sigma_CO"] >= 2600)

# Show that the Gaussian model is a decent representation
# twocolumn_twopanel_figure()
cpal = sb.color_palette()

ax1 = plt.subplot(121)

hist2d(tab["coldens_CO_gauss"][good_pts] / (inc * co21_mass_conversion).value,
       tab["coldens_CO_FWHM"][good_pts] / (inc * co21_mass_conversion).value,
       data_kwargs={"alpha": 0.6},
       ax=ax1)
# plt.plot([0, 60], [0, 60], c=cpal[0], linewidth=3, linestyle='dashed')
plt.plot([0, 15], [0, 15], c=cpal[0], linewidth=3, linestyle='dashed')
plt.grid()
# plt.xlabel("Gaussian Fit $\Sigma_{\mathrm{H2}}$ (M$_{\odot}$ pc$^{-2}$)")
# plt.ylabel("FWHM $\Sigma_{\mathrm{H2}}$ (M$_{\odot}$ pc$^{-2}$)")
plt.xlabel("CO Integrated Intensity\nfrom Gaussian Fit (K km s$^{-1}$)")
plt.ylabel("CO Integrated Intensity\nin FWHM fit window\n(K km s$^{-1}$)")

ax2 = plt.subplot(122)
hist2d(tab["coldens_HI_gauss"][good_pts] / (inc * hi_mass_conversion).value,
       tab["coldens_HI_FWHM"][good_pts] / (inc * hi_mass_conversion).value,
       data_kwargs={"alpha": 0.6},
       ax=ax2)
# plt.plot([0, 25], [0, 25], c=cpal[0], linewidth=3, linestyle='dashed')
示例#47
0
gs = GridSpec(3,3,figure=fig,height_ratios=[0.1,1,1], width_ratios=[1,1,0.15])

ax = [[],[],[],[]]
ax[0].append(plt.subplot(gs[0,0]))
ax[0].append(plt.subplot(gs[0,1]))
ax[0].append(plt.subplot(gs[0,2]))
ax[1].append(plt.subplot(gs[1,0]))
ax[1].append(plt.subplot(gs[1,1]))
ax[1].append(plt.subplot(gs[1,2]))
ax[2].append(plt.subplot(gs[2,:]))

qRan = (-2,2)
wRan = (-2,2)

hist2d(samples[:,0],samples[:,1],ax=ax[1][0],bins=30,plot_datapoints=False)
ax[1][0].set_xlabel('$Q$')
ax[1][0].set_ylabel('$W$')
ax[1][0].set_xlim((0,2))
ax[1][0].set_ylim((-2,-0.5))

hist2d(samples[:,0],samples[:,1],ax=ax[1][1],bins=100, plot_datapoints=False,range=[qRan, wRan])
ax[1][1].set_xlabel('$Q$')
ax[1][1].set_ylabel('$W$')

lims = [ax[1][0].get_xlim(), ax[1][0].get_ylim()]
rect = Rectangle((lims[0][0], lims[1][0]), lims[0][1]-lims[0][0], lims[1][1]-lims[1][0], linestyle='dashed',fill=False)
ax[1][1].add_patch(rect)
ax[1][1].scatter((1,),(1,))
ax[1][1].scatter((1,),(-1,))
ax[1][1].scatter((-1,),(1,))
示例#48
0
def dataViz(survey='2MASS', ngauss=128, quantile=0.05, dataFilename='All.npz', iter='10th', Nsamples=3e5, contourColor='k', dustFile='dust.npz', sdss5=False, whatsThatFeature=False):

    if survey == 'APASS':
        mag1 = 'B'
        mag2 = 'V'
        absmag = 'G'
        xlabel='B-V'
        ylabel = r'M$_\mathrm{G}$'
        xlim = [-0.2, 2]
        ylim = [9, -2]

    if survey == '2MASS':
        mag1 = 'J'
        mag2 = 'K'
        absmag = 'J'
        xlabel = r'$(J-K)^C$'
        ylabel = r'$M_J^C$'
        xlim = [-0.25, 1.25]
        ylim = [6, -6]

    xdgmmFilename = 'xdgmm.'             + str(ngauss) + 'gauss.dQ' + str(quantile) + '.' + iter + '.' + survey + '.' + dataFilename + '.fit'

    tgas, twoMass, Apass, bandDictionary, indices = testXD.dataArrays()
    dustEBV = 0.0
    color = testXD.colorArray(mag1, mag2, dustEBV, bandDictionary)
    absMagKinda, apparentMagnitude = testXD.absMagKindaArray(absmag, dustEBV, bandDictionary, tgas['parallax'])

    color_err = np.sqrt(bandDictionary[mag1]['array'][bandDictionary[mag1]['err_key']]**2. + bandDictionary[mag2]['array'][bandDictionary[mag2]['err_key']]**2.)
    absMagKinda_err = tgas['parallax_error']*10.**(0.2*bandDictionary[absmag]['array'][bandDictionary[absmag]['key']])

    xdgmm = XDGMM(filename=xdgmmFilename)
    sample = xdgmm.sample(Nsamples)
    negParallax = sample[:,1] < 0
    nNegP = np.sum(negParallax)
    while nNegP > 0:
        sampleNew = xdgmm.sample(nNegP)
        sample[negParallax] = sampleNew
        negParallax = sample[:,1] < 0
        nNegP = np.sum(negParallax)
    positive = absMagKinda > 0
    y = absMagKinda[positive]
    yplus  = y + absMagKinda_err[positive]
    yminus = y - absMagKinda_err[positive]
    parallaxErrGoesNegative = yminus < 0
    absMagYMinus = testXD.absMagKinda2absMag(yminus)
    absMagYMinus[parallaxErrGoesNegative] = -50.
    yerr_minus = testXD.absMagKinda2absMag(y) - absMagYMinus
    yerr_plus = testXD.absMagKinda2absMag(yplus) - testXD.absMagKinda2absMag(y)
    #yerr_minus = testXD.absMagKinda2absMag(yplus) - testXD.absMagKinda2absMag(y)
    #yerr_plus = testXD.absMagKinda2absMag(y) - absMagYMinus
    """
    testfig, testax = plt.subplots(3)
    testax[0].scatter(testXD.absMagKinda2absMag(y), y, s=1)
    testax[0].set_xlabel('absMag')
    testax[0].set_ylabel('absMagKinda')
    testax[1].scatter(testXD.absMagKinda2absMag(y), absMagYMinus, s=1)
    testax[1].set_xlabel('absMag')
    testax[1].set_ylabel('absMag Minus')
    testax[2].scatter(testXD.absMagKinda2absMag(y), testXD.absMagKinda2absMag(yplus), s=1)
    testax[2].set_xlabel('absMag')
    testax[2].set_ylabel('absMag Plus')
    plt.show()
    """

    dp.plot_sample(color[positive], testXD.absMagKinda2absMag(y), sample[:,0], testXD.absMagKinda2absMag(sample[:,1]),
                xdgmm, xerr=color_err[positive], yerr=[yerr_minus, yerr_plus], xlabel=xlabel, ylabel=ylabel, xlim=xlim, ylim=ylim, errSubsample=2.4e3, thresholdScatter=2., binsScatter=200, contourColor=contourColor)
    dataFile = 'data_noDust.pdf'
    priorFile = 'prior_' + str(ngauss) +'gauss.pdf'
    os.rename('plot_sample.data.pdf', dataFile)
    os.rename('plot_sample.prior.pdf', priorFile)
    #import pdb; pdb.set_trace()
    data = np.load(dustFile)
    dustEBV = data['ebv']
    color = testXD.colorArray(mag1, mag2, dustEBV, bandDictionary)
    absMagKinda, apparentMagnitude = testXD.absMagKindaArray(absmag, dustEBV, bandDictionary, tgas['parallax'])

    cNorm  = plt.matplotlib.colors.Normalize(vmin=-6, vmax=2)
    posteriorFile = 'posteriorParallax.' + str(ngauss) + 'gauss.dQ' + str(quantile) + '.' + iter + '.' + survey + '.' + dataFilename
    for file in [posteriorFile]:#, 'posteriorSimple.npz']:
        data = np.load(file)
        parallax = data['mean']
        parallax_err = np.sqrt(data['var'])
        notnans = ~np.isnan(parallax) & ~np.isnan(parallax_err)
        parallax = parallax[notnans]
        parallax_err = parallax_err[notnans]
        apparentMagnitudeGood = apparentMagnitude[notnans]
        c = np.log(data['var']) - np.log(tgas['parallax_error']**2.)
        absMagKinda = parallax*10.**(0.2*apparentMagnitudeGood)
        absMagKinda_err = parallax_err*10.**(0.2*apparentMagnitudeGood)
        y = absMagKinda
        yplus  = y + absMagKinda_err
        yminus = y - absMagKinda_err
        parallaxErrGoesNegative = yminus < 0
        absMagYMinus = testXD.absMagKinda2absMag(yminus)
        absMagYMinus[parallaxErrGoesNegative] = -50.
        absMag = testXD.absMagKinda2absMag(y)
        yerr_minus = absMag - absMagYMinus
        yerr_plus = testXD.absMagKinda2absMag(yplus) - absMag
        #notnan = ~np.isnan(color[notnans]) & ~np.isnan(absMag)
        contourColor = 'k'
        ascii.write([color[notnans], absMag, color_err[notnans], yerr_minus, yerr_plus, c[notnans]], 'cmdExpectation.txt', names=['color', 'absMag', 'color_err', 'absMag_errMinus', 'absMag_errPlus', 'logDeltaVar'])
        if whatsThatFeature & (file == posteriorFile):
            figFeature, axFeature = plt.subplots()
            x = color[notnans]
            y = absMag
            #levels = 1.0 - np.exp(-0.5 * np.arange(1.0, 2.1, 1.0) ** 2)
            im = corner.hist2d(x, y, ax=axFeature, levels=None, bins=200, no_fill_contours=True, plot_density=False, color=contourColor, rasterized=True, plot_contours=False)
            axFeature.set_xlim(xlim)
            axFeature.set_ylim(ylim)
            axFeature.set_xlabel(xlabel)
            axFeature.set_ylabel(ylabel)
            lowerMainSequence = (0.45, 5.5)
            upperMainSequence = (-0.225, 2)
            binarySequence = (0.75, 4)
            redClump = (0.35, -2)
            redGiantBranch = (1.0, -2)
            turnOff = (0.0, 3.5)
            features = [lowerMainSequence, upperMainSequence, binarySequence, redClump, redGiantBranch, turnOff]
            labels = ['lower MS', 'upper MS', 'binary sequence', 'red clump', 'RGB', 'MS turn off']
            for l, f in zip(labels, features): axFeature.text(f[0], f[1], l, fontsize=15)
            figFeature.savefig('whatsThatFeature.pdf', format='pdf')
示例#49
0
def assignHalo(nsnap=15): 
    # read in subhalo catalog
    cat = Cat.CentralSubhalos(sigma_smhm=0.2, smf_source='li-march', nsnap0=15) 
    sh = cat.Read()
    # read in Louis's catalog
    cat = LA2016.catalogAbramson2016() 
    i_z = np.argmin(np.abs(cat['REDSHIFT'] - UT.z_nsnap(nsnap))) #  index that best match to snapshot 
    mstar = np.log10(cat['MSTEL_T'][:,i_z]) # M* @ z ~ zsnap
    ws = LA2016.matchSMF(mstar, UT.z_nsnap(nsnap), logM_range=[9., 12.]) # get the weights to match SMF
    
    # assign Mhalo, Mmax to Louis's catalog
    mhalo, mmax, i_halo = LA2016.assignHalo(mstar, nsnap, sh, logM_range=[9., 12.], dlogMs=0.2) 
    
    # now lets compare the resulting SHMRs 
    fig = plt.figure(figsize=(20,5))
    # first a scatter plot
    sub = fig.add_subplot(141) 
    sub.scatter(sh['halo.m.snap'+str(nsnap)], sh['m.sham.snap'+str(nsnap)], c='k', s=1, label='TreePM SHAM') 
    sub.scatter(mhalo, mstar, c='C1', s=2, label='weighted Abramson+(2016)') 
    sub.set_xlabel('log\,$M_h$', fontsize=25) 
    sub.set_xlim([11., 14.]) 
    sub.set_ylabel('log\,$M_*$', fontsize=25) 
    sub.set_ylim([9., 12.]) 
    sub.legend(loc=(0., 0.8), markerscale=5, handletextpad=0., frameon=True, fontsize=15) 

    # a DFM contour plot
    sub = fig.add_subplot(142)
    DFM.hist2d(sh['halo.m.snap'+str(nsnap)], sh['m.sham.snap'+str(nsnap)], 
            levels=[0.68, 0.95], range=[[11., 15.], [9., 12.]], color='k', 
            bins=20, plot_datapoints=False, fill_contours=False, plot_density=False, ax=sub) 
    DFM.hist2d(mhalo, mstar, weights=ws, 
            levels=[0.68, 0.95], range=[[11., 15.], [9., 12.]], color='C1', 
            bins=20, plot_datapoints=False, fill_contours=False, plot_density=False, ax=sub) 
    sub.set_xlabel('log\,$M_h$', fontsize=25) 
    sub.set_xlim([11., 14.]) 
    sub.set_ylim([9., 12.]) 

    # SHMR plot
    smhmr = Obvs.Smhmr()
    sub = fig.add_subplot(143)
    mbin = np.arange(11., 15., 0.2) 
    ms_nonzero = (sh['m.sham.snap'+str(nsnap)] > 0.) 
    m_mid, mu_logMs, sig_logMs, _ = smhmr.Calculate(sh['halo.m.snap'+str(nsnap)][ms_nonzero], 
            sh['m.sham.snap'+str(nsnap)][ms_nonzero], m_bin=mbin)
    sub.errorbar(m_mid, mu_logMs, sig_logMs, fmt='.k') 
    m_mid, mu_logMs, sig_logMs, _ = smhmr.Calculate(mhalo, mstar, weights=ws, m_bin=mbin)
    sub.errorbar(m_mid[m_mid > 11.8], mu_logMs[m_mid > 11.8], sig_logMs[m_mid > 11.8], fmt='.C1') 
    sub.set_xlabel('log $M_h$', fontsize=25) 
    sub.set_xlim([11., 14.]) 
    #sub.set_ylabel('log $M_*$', fontsize=25) 
    sub.set_ylim([9., 12.]) 
    #sub.set_ylim([0., 0.5]) 

    sub = fig.add_subplot(144)
    mbin = np.arange(9., 12., 0.2) 
    m_mid, mu_logMs, sig_logMs, _ = smhmr.Calculate(sh['m.sham.snap'+str(nsnap)], sh['halo.m.snap'+str(nsnap)], m_bin=mbin)
    sub.errorbar(m_mid, mu_logMs, sig_logMs, fmt='.k') 
    m_mid, mu_logMs, sig_logMs, _ = smhmr.Calculate(mstar, mhalo, weights=ws, m_bin=mbin)
    sub.errorbar(m_mid, mu_logMs, sig_logMs, fmt='.C1') 
    sub.set_xlabel('log $M_*$', fontsize=25) 
    sub.set_xlim([9., 12.]) 
    sub.set_ylabel('log $M_h$', fontsize=25) 
    sub.set_ylim([11., 14.]) 

    ffig = ''.join([UT.fig_dir(), 'catalogAbramson.SHMRsnap', str(nsnap), '.png'])
    fig.savefig(ffig, bbox_inches="tight") 
    plt.close()
    return None
示例#50
0
plt.rc('font', size=10)




################# Posterior plots ##################
plt.rc('font', size=8)
fig, ax = plt.subplots(2,5, figsize=(14,6))

for i in np.arange(10):
    a = np.int(i/5)
    b = i%5

    xmin = np.min(sampler.chain[:,:,i])
    xmax = np.max(sampler.chain[:,:,i])
    corner.hist2d(sampler.chain[:,:,i], sampler.lnprobability, ax=ax[a,b], bins=30, range=((xmin,xmax),(-50,0)), plot_datapoints=False)
    ax[a,b].set_xlabel(labels[i])

    ax[a,b].set_ylim(-30,0)

plt.tight_layout()
plt.savefig('../figures/J0045_posterior_params_multiburn.pdf')


################# Chains plot #####################
fig, ax = plt.subplots(sampler1.dim, 5, sharex=False, figsize=(18.0,20.0))
for i in range(sampler1.dim):
    for j in np.arange(len(sampler1.chain[...])):

        chain1 = sampler1.chain[...,i][j]
        ax[i,0].plot(chain1, alpha=0.25, color='k', drawstyle='steps')
示例#51
0
        yloc = plt.MaxNLocator(3)
        ax[i].yaxis.set_major_locator(yloc)
        # ax[i].set_yticks(fontsize=8)
fig.subplots_adjust(hspace=0)
plt.yticks(fontsize = 8)
plt.savefig('../figures/sys2_chains.pdf', rasterized=True)



# Likelihood as a function of each parametersplt.rc('font', size=8)
fig, ax = plt.subplots(2,5, figsize=(14,6))
labels = [r"$M_1$", r"$M_2$", r"$A$", r"$e$", r"$v_k$", r"$\theta$", r"$\phi$", r"$\alpha_{\rm b}$", r"$\delta_{\rm b}$", r"$t_{\rm b}$"]
for i in np.arange(10):
    a = np.int(i/5)
    b = i%5
    corner.hist2d(sampler.chain[:,:,i], sampler.lnprobability, ax=ax[a,b], bins=20)
    ax[a,b].set_xlabel(labels[i])
plt.tight_layout()
plt.savefig('../figures/sys2_likelihoods.pdf')



# Corner plot
plt.rc('font', size=18)
labels = [r"$M_1$", r"$M_2$", r"$A$", r"$e$", r"$v_k$", r"$\theta$", r"$\phi$", r"$\alpha_{\rm b}$", r"$\delta_{\rm b}$", r"$t_{\rm b}$"]
truths = [M1_true, M2_true, A_true, ecc_true, v_k_true, theta_true, phi_true, ra_true, dec_true, t_b_true]
fig = corner.corner(sampler.flatchain, labels=labels, truths=truths)

ax2 = plt.subplot2grid((5,5), (0,3), colspan=2, rowspan=2)
ra_out = sampler.flatchain.T[7]
dec_out = sampler.flatchain.T[8]