Ejemplo n.º 1
0
def plot_true_histogram(true_samps, n_bins=(10, 50), plot_loc='', prepend='', plot_name='true_hist.png'):
    """
    Plots a histogram of true input values

    Parameters
    ----------
    true_samps: numpy.ndarray, float
        vector of true values of scalar input
    n_bins: tuple, int, optional
        number of histogram bins in which to place input values, coarse and fine
    plot_loc: string, optional
        location in which to store plot
    plot_name: string, optional
        filename for plot
    prepend: str, optional
        prepend string to plot name
    """
    pu.set_up_plot()
    f = plt.figure(figsize=(5, 5))
    sps = f.add_subplot(1, 1, 1)
    sps.hist(true_samps, bins=n_bins[1], density=1, color='k', alpha=0.5, log=True)
    sps.hist(true_samps, bins=n_bins[0], density=1, color='y', alpha=0.5, log=True)
    sps.set_xlabel(r'$z_{true}$')
    sps.set_ylabel(r'$n(z_{true})$')
    f.savefig(os.path.join(plot_loc, prepend+plot_name), bbox_inches='tight', pad_inches = 0, dpi=d.dpi)

    return
Ejemplo n.º 2
0
def plot_obs_scatter(true_vals, pfs, z_grid, plot_loc='', prepend='', plot_name='obs_scatter.png'):
    """
    Plots a scatterplot of true and observed redshift values

    Parameters
    ----------
    true_vals: numpy.ndarray, float
        2 * n_gals array of true values of scalar input
    pfs: numpy.ndarray, float
        matrix of interim posteriors evaluated on a fine grid
    z_grid: numpy.ndarray, float
        vector of bin midpoints
    plot_loc: string, optional
        location in which to store plot
    plot_name: string, optional
        filename for plot
    prepend: str, optional
        prepend string to plot name
    """
    true_zs = true_vals[0]
    obs_zs = true_vals[1]#np.array([z_grid[np.argmax(pf)] for pf in pfs])
    max_pfs = np.max(pfs)
    n = len(true_zs)
    dz = (max(z_grid) - min(z_grid)) / len(z_grid)
    jitters = np.random.uniform(-1. * dz / 2., dz / 2., n)
    obs_zs = obs_zs + jitters
    pu.set_up_plot()
    f = plt.figure(figsize=(5, 5))
    sps = f.add_subplot(1, 1, 1)
    sps.scatter(true_zs, obs_zs, c='k', marker='.', s = 1., alpha=0.1)
    randos = np.floor(n / (d.plot_colors + 1)) * np.arange(1., d.plot_colors + 1)# np.random.choice(range(len(z_grid)), d.plot_colors)
    randos = randos.astype(int)
    ordered = np.argsort(obs_zs)
    sorted_pfs = pfs[ordered]
    sorted_true = true_zs[ordered]
    sorted_obs = obs_zs[ordered]
    for r in range(d.plot_colors):
        pf = sorted_pfs[randos[r]]
        plt.scatter(sorted_true[randos[r]], sorted_obs[randos[r]], marker='+', c='r')
        norm_pf = pf / max_pfs / (d.plot_colors + 1)
        plt.step(z_grid, norm_pf + sorted_obs[randos[r]], c='k', where='mid')
        plt.hlines(sorted_obs[randos[r]], min(z_grid), max(z_grid), color='k', alpha=0.5, linestyle='--')
    sps.set_xlabel(r'$z_{true}$')
    sps.set_ylabel(r'$\hat{z}_{MAP}$')
    f.savefig(os.path.join(plot_loc, prepend+plot_name), bbox_inches='tight', pad_inches = 0, dpi=d.dpi)

    return
Ejemplo n.º 3
0
def plot_scatter(zs, pfs, z_grid, plot_loc='', prepend='', plot_name='scatter.png'):
    """
    Plots a scatterplot of true and observed redshift values

    Parameters
    ----------
    zs: numpy.ndarray, float
        matrix of spec, phot values
    z_grid: numpy.ndarray, float
        fine grid of redshifts
    pfs: numpy.ndarray, float
        matrix of posteriors evaluated on a fine grid
    plot_loc: string, optional
        location in which to store plot
    plot_name: string, optional
        filename for plot
    prepend: str, optional
        prepend string to plot name
    """
    n = len(zs)
    zs = zs.T
    true_zs = zs[0]
    obs_zs = zs[1]
    pu.set_up_plot()
    f = plt.figure(figsize=(5, 5))
    sps = f.add_subplot(1, 1, 1)
    sps.plot(z_grid, z_grid, color='r', alpha=0.5, linewidth=2.)
    sps.scatter(true_zs, obs_zs, c='g', marker='.', s = 1., alpha=0.1)
    randos = np.floor(n / (d.plot_colors + 1)) * np.arange(1., d.plot_colors + 1)# np.random.choice(range(len(z_grid)), d.plot_colors)
    randos = randos.astype(int)
    max_pfs = np.max(pfs)
    sort_inds = np.argsort(obs_zs)
    sorted_pfs = pfs[sort_inds]
    sorted_true = true_zs[sort_inds]
    sorted_obs = obs_zs[sort_inds]
    for r in range(d.plot_colors):
        pf = sorted_pfs[randos[r]]
        norm_pf = pf / max_pfs / (d.plot_colors + 1)
        plt.step(z_grid, norm_pf + sorted_obs[randos[r]], c='k', where='mid')# plt.plot(z_grid, norm_pf + sorted_obs[randos[r]], c='k')
        plt.hlines(sorted_obs[randos[r]], min(z_grid), max(z_grid), color='k', alpha=0.5, linestyle='--')
        plt.scatter(sorted_true[randos[r]], sorted_obs[randos[r]], marker='+', c='r')
    sps.set_xlabel(r'$z_{true}$')
    sps.set_ylabel(r'$z_{obs}$')
    f.savefig(os.path.join(plot_loc, prepend+plot_name), bbox_inches='tight', pad_inches = 0, dpi=d.dpi)
    # plt.close()
    return
Ejemplo n.º 4
0
def plot_prob_space(z_grid, p_space, plot_loc='', prepend='', plot_name='prob_space.png'):
    """
    Plots the 2D probability space of z_spec, z_phot

    Parameters
    ----------
    p_space: numpy.ndarray, float
        probabilities on the grid
    z_grid: numpy.ndarray, float
        fine grid of redshifts
    plot_loc: string, optional
        location in which to store plot
    plot_name: string, optional
        filename for plot
    prepend: str, optional
        prepend string to plot name
    """
    pu.set_up_plot()
    f = plt.figure(figsize=(5, 5))
    plt.subplot(1, 1, 1)
    grid_len = len(z_grid)
    grid_range = range(grid_len)
    # to_plot = u.safe_log(p_space.evaluate(all_points.reshape((grid_len**2, 2))).reshape((grid_len, grid_len)))
    # to_plot.reshape((len(z_grid), len(z_grid)))
    # plt.pcolormesh(z_grid, z_grid, to_plot, cmap='viridis')
    all_points = np.array([[(z_grid[kk], z_grid[jj]) for kk in grid_range] for jj in grid_range])
    orig_shape = np.shape(all_points)
    # all_vals = np.array([[p_space.evaluate_one(np.array([z_grid[jj], z_grid[kk]])) for jj in range(len(z_grid))] for kk in range(len(z_grid))])
    all_vals = p_space.evaluate(all_points.reshape((orig_shape[0]*orig_shape[1], orig_shape[2]))).reshape((orig_shape[0], orig_shape[1]))
    plt.pcolormesh(z_grid, z_grid, u.safe_log(all_vals), cmap='viridis')
    plt.plot(z_grid, z_grid, color='k')
    plt.colorbar()
    plt.xlabel(r'$z_{\mathrm{true}}$')
    plt.ylabel(r'$\mathrm{``data"}$')#z_{\mathrm{phot}}$')
    plt.axis([z_grid[0], z_grid[-1], z_grid[0], z_grid[-1]])
    f.savefig(os.path.join(plot_loc, prepend+plot_name), bbox_inches='tight', pad_inches = 0, dpi=d.dpi)
    return
Ejemplo n.º 5
0
def plot_mega_scatter(zs, pfs, z_grid, grid_ends, truth=None, plot_loc='', prepend='', plot_name='mega_scatter.png', int_pr=None):
    """
    Plots a scatterplot of true and observed redshift values

    Parameters
    ----------
    zs: numpy.ndarray, float
        matrix of spec, phot values
    z_grid: numpy.ndarray, float
        fine grid of redshifts
    grid_ends: numpy.ndarray, float
        coarse bin ends
    pfs: numpy.ndarray, float
        matrix of posteriors evaluated on a fine grid
    truth: numpy.ndarray, float
        (x, y) coordinates of the true distribution on a fine grid
    plot_loc: string, optional
        location in which to store plot
    plot_name: string, optional
        filename for plot
    prepend: str, optional
        prepend string to plot name
    int_pr: numpy.ndarray, float, optional
        plit the interim prior with the histograms?
    """
    n = len(zs)
    zs = zs.T
    true_zs = zs[0]
    obs_zs = zs[1]

    pu.set_up_plot()
    f, scatplot = plt.subplots(figsize=(7.5, 7.5))
    f.subplots_adjust(hspace=0)

    # true_hist = np.hist(true_zs, grid_ends)
    # sps_x.step(true_hist[0], true_hist[1], c='k', where='mid')
    # sps_x.hist(obs_zs, bins=info['bin_ends'][0])
    # sps_y.hist(true_zs)

    scatplot.plot(z_grid, z_grid, color='r', alpha=0.5, linewidth=1.)
    scatplot.scatter(true_zs, obs_zs, c='k', marker='.', s=1., alpha=0.1)
    randos = np.floor(n / (d.plot_colors + 1)) * np.arange(1., d.plot_colors + 1)# np.random.choice(range(len(z_grid)), d.plot_colors)
    randos = randos.astype(int)
    max_pfs = np.max(pfs)
    sort_inds = np.argsort(obs_zs)
    sorted_pfs = pfs[sort_inds]
    sorted_true = true_zs[sort_inds]
    sorted_obs = obs_zs[sort_inds]
    for r in range(d.plot_colors):
        pf = sorted_pfs[randos[r]]
        norm_pf = pf / max_pfs
        pu.plot_h(scatplot, [min(z_grid), max(z_grid)], [sorted_obs[randos[r]], sorted_obs[randos[r]]], c='k', s=':', w=0.75)
        pu.plot_v(scatplot, [min(z_grid), sorted_true[randos[r]], max(z_grid)], [sorted_obs[randos[r]], max(norm_pf)+sorted_obs[randos[r]]], c='k', s=':', w=0.75)
        scatplot.step(z_grid, norm_pf + sorted_obs[randos[r]], c=pu.colors[r], where='mid')# plt.plot(z_grid, norm_pf + sorted_obs[randos[r]], c='k')
    scatplot.set_xlabel(r'$z_{spec}$')
    scatplot.set_ylabel(r'$z_{phot}$')

    # scatplot.set_aspect(1.)
    divider = make_axes_locatable(scatplot)
    histx = divider.append_axes('top', 1.2, pad=0., sharex=scatplot)
    histy = divider.append_axes('right', 1.2, pad=0., sharey=scatplot)

    histx.xaxis.set_tick_params(labelbottom=False)
    histy.yaxis.set_tick_params(labelleft=False)
    histx.hist(true_zs, bins=grid_ends, alpha=0.5, color='k', density=True, stacked=False)
    histy.hist(obs_zs, bins=grid_ends, orientation='horizontal', alpha=0.5, color='k', density=True, stacked=False)
    if truth is not None:
        histx.plot(truth[0], truth[1] / np.max(truth[1]), color='b', alpha=0.75)
        histy.plot(truth[1] / np.max(truth[1]), truth[0], color='b', alpha=0.75)
    if int_pr is not None:
        histx.plot(int_pr[0], int_pr[1] / np.max(int_pr[1]), color='r', alpha=0.75)
        histy.plot(int_pr[1] / np.max(int_pr[1]), int_pr[0], color='r', alpha=0.75)
    histx.set_yticks([])
    histy.set_xticks([])

    f.savefig(os.path.join(plot_loc, prepend+plot_name), bbox_inches='tight', pad_inches=0, dpi=d.dpi)
    return
Ejemplo n.º 6
0
def plot_samples(info, plot_dir, prepend=''):
    """
    Plots a few random samples from the posterior distribution

    Parameters
    ----------
    info: dict
        dictionary of stored information from log_z_dens object
    plot_dir: string
        directory where plot should be stored
    prepend: str, optional
        prepend string to file names
    """
    pu.set_up_plot()

    f = plt.figure(figsize=(5, 10))
    sps = [f.add_subplot(2, 1, l + 1) for l in xrange(0, 2)]
    f.subplots_adjust(hspace=0, wspace=0)
    sps_log = sps[0]
    sps = sps[1]

    sps_log.set_xlim(info['bin_ends'][0], info['bin_ends'][-1])
    sps_log.set_ylabel(r'$\ln[n(z)]$')
    sps.set_xlim(info['bin_ends'][0], info['bin_ends'][-1])
    sps.set_xlabel(r'$z$')
    sps.set_ylabel(r'$n(z)$')
    sps.ticklabel_format(style='sci', axis='y')

    pu.plot_step(sps,
                 info['bin_ends'],
                 np.exp(info['log_interim_prior']),
                 w=w_int,
                 s=s_int,
                 a=a_int,
                 c=c_int,
                 d=d_int,
                 l=l_int + nz)
    pu.plot_step(sps_log,
                 info['bin_ends'],
                 info['log_interim_prior'],
                 w=w_int,
                 s=s_int,
                 a=a_int,
                 c=c_int,
                 d=d_int,
                 l=l_int + lnz)
    if info['truth'] is not None:
        sps.plot(info['truth']['z_grid'],
                 info['truth']['nz_grid'],
                 linewidth=w_tru,
                 alpha=a_tru,
                 color=c_tru,
                 label=l_tru + nz)
        sps_log.plot(info['truth']['z_grid'],
                     u.safe_log(info['truth']['nz_grid']),
                     linewidth=w_tru,
                     alpha=a_tru,
                     color=c_tru,
                     label=l_tru + lnz)

    (locs, scales) = s.norm_fit(info['log_sampled_nz_meta_data']['chains'])
    for k in range(len(info['bin_ends']) - 1):
        x_errs = [
            info['bin_ends'][k], info['bin_ends'][k], info['bin_ends'][k + 1],
            info['bin_ends'][k + 1]
        ]
        log_y_errs = [
            locs[k] - scales[k], locs[k] + scales[k], locs[k] + scales[k],
            locs[k] - scales[k]
        ]
        sps_log.fill(x_errs, log_y_errs, color='k', alpha=0.1, linewidth=0.)
        sps.fill(x_errs,
                 np.exp(log_y_errs),
                 color='k',
                 alpha=0.1,
                 linewidth=0.)
    shape = np.shape(info['log_sampled_nz_meta_data']['chains'])
    flat = info['log_sampled_nz_meta_data']['chains'].reshape(
        np.prod(shape[:-1]), shape[-1])
    random_samples = [
        np.random.randint(0, len(flat)) for i in range(d.plot_colors)
    ]
    for i in range(d.plot_colors):
        pu.plot_step(sps_log,
                     info['bin_ends'],
                     flat[random_samples[i]],
                     s=s_smp,
                     d=d_smp,
                     w=w_smp,
                     a=1.,
                     c=pu.colors[i])
        pu.plot_step(sps,
                     info['bin_ends'],
                     np.exp(flat[random_samples[i]]),
                     s=s_smp,
                     d=d_smp,
                     w=w_smp,
                     a=1.,
                     c=pu.colors[i])
    pu.plot_step(sps_log,
                 info['bin_ends'],
                 locs,
                 s=s_smp,
                 d=d_smp,
                 w=2.,
                 a=1.,
                 c='k',
                 l=l_bfe + lnz)
    pu.plot_step(sps,
                 info['bin_ends'],
                 np.exp(locs),
                 s=s_smp,
                 d=d_smp,
                 w=2.,
                 a=1.,
                 c='k',
                 l=l_bfe + nz)

    sps_log.legend(fontsize='x-small', loc='lower left')
    sps.set_xlabel('x')
    sps_log.set_ylabel('Log probability density')
    sps.set_ylabel('Probability density')
    f.savefig(os.path.join(plot_dir, prepend + 'samples.png'),
              bbox_inches='tight',
              pad_inches=0)

    return
Ejemplo n.º 7
0
def plot_ivals(ivals, info, plot_dir, prepend=''):
    """
    Plots the initial values given to the sampler

    Parameters
    ----------
    ivals: np.ndarray, float
        (n_walkers, n_bins) array of initial values for sampler
    info: dict
        dictionary of stored information from log_z_dens object
    plot_dir: string
        location into which the plot will be saved
    prepend: str, optional
        prepend string to file names

    Returns
    -------
    f: matplotlib figure
        figure object
    """
    pu.set_up_plot()
    n_walkers = len(ivals)
    walkers = [np.random.randint(0, n_walkers) for i in range(d.plot_colors)]

    f = plt.figure(figsize=(10, 5))
    sps_samp = f.add_subplot(1, 2, 1)
    for i in range(d.plot_colors):
        pu.plot_step(sps_samp,
                     info['bin_ends'],
                     ivals[walkers[i]],
                     c=pu.colors[i])
    pu.plot_step(sps_samp,
                 info['bin_ends'],
                 info['log_interim_prior'],
                 w=w_int,
                 s=s_int,
                 a=a_int,
                 c=c_int,
                 d=d_int,
                 l=l_int + nz)
    if info['truth'] is not None:
        sps_samp.plot(info['truth']['z_grid'],
                      np.log(info['truth']['nz_grid']),
                      linewidth=w_tru,
                      alpha=a_tru,
                      color=c_tru,
                      label=l_tru + nz)
    sps_samp.set_xlabel(r'$z$')
    sps_samp.set_ylabel(r'$\ln\left[n(z)\right]$')

    sps_sum = f.add_subplot(1, 2, 2)
    bin_difs = info['bin_ends'][1:] - info['bin_ends'][:-1]
    ival_integrals = np.dot(np.exp(ivals), bin_difs)
    log_ival_integrals = u.safe_log(ival_integrals)
    sps_sum.hist(log_ival_integrals, color='k', normed=1)
    sps_sum.vlines(np.log(np.dot(np.exp(info['log_interim_prior']), bin_difs)),
                   0.,
                   1.,
                   linewidth=w_int,
                   linestyle=s_int,
                   alpha=a_int,
                   color=c_int,
                   dashes=d_int,
                   label=l_int + nz)
    sps_sum.vlines(np.mean(log_ival_integrals),
                   0.,
                   1.,
                   linewidth=w_bfe,
                   linestyle=s_bfe,
                   alpha=a_bfe,
                   color=c_bfe,
                   dashes=d_bfe,
                   label=l_bfe + lnz)

    sps_sum.set_xlabel(r'$\ln\left[\int n(z)dz\right]$')
    sps_sum.set_ylabel(r'$p\left(\ln\left[\int n(z)dz\right]\right)$')

    f.savefig(os.path.join(plot_dir, prepend + 'ivals.png'),
              bbox_inches='tight',
              pad_inches=0)

    return
Ejemplo n.º 8
0
def plot_estimators(info,
                    plot_dir,
                    log=True,
                    prepend='',
                    metrics=True,
                    mini=True,
                    both=False):
    """
    Makes a log and linear plot of n(z) estimators from a log_z_dens object

    Parameters
    ----------
    info: dict
        dictionary of stored information from log_z_dens object
    plot_dir: string
        location where the plot will be saved
    log: boolean, optional
        plot in log-quantities
    prepend: str, optional
        prepend string to file names
    metrics: Boolean, optional
        include value of metrics in legend
    mini: Boolean, optional
        plot percent difference underneath
    both: Boolean, optional
        show log and linear side by side
    """
    pu.set_up_plot()
    # black_plots = []
    # black_labels =[]
    # color_plots = [0]
    # color_labels = [0]

    if info['truth'] is not None:
        bin_log_true = info['log_tru_nz']
        bin_true = np.exp(bin_log_true)
        if mini:
            f = plt.figure(figsize=(5, 7.5))
            gs = gridspec.GridSpec(3, 1)
            sps_log = f.add_subplot(gs[:-1, :])
            sps_log.set_xticklabels([])
            mini_sps = f.add_subplot(gs[-1, :])
            mini_sps.set_ylim(-50, 50)
            mini_sps.set_xlim(info['bin_ends'][0], info['bin_ends'][-1])
            mini_sps.set_xlabel(r'$z$')
            mini_sps.set_ylabel(
                r'$\left(\hat{n}(z) / n_{true}(z) - 1\right) \times 100\%$')
            pu.plot_step(mini_sps,
                         info['bin_ends'], ((bin_true / bin_true) - 1.) * 100.,
                         w=w_tbp,
                         s=s_tbp,
                         a=a_tbp,
                         c=c_tbp,
                         d=d_tbp)
        else:
            f = plt.figure(figsize=(5, 5))
            sps_log = f.add_subplot(1, 1, 1)
            sps_log.set_xlabel(r'$z$')
        # mini_sps.ticklabel_format(style='sci',axis='y')

        # tru, =
        # sps_log.plot(info['truth']['z_grid'], u.safe_log(info['truth']['nz_grid']),
        #                 linewidth=w_tru, alpha=a_tru, color=c_tru,
        #                 label=l_tru+lnz)
        # black_plots.append(tru)
        # black_labels.append(l_tru+lnz)
        #tbp, =
        if log:
            pu.plot_step(sps_log,
                         info['bin_ends'],
                         bin_log_true,
                         w=w_tbp,
                         s=s_tbp,
                         a=a_tbp,
                         c=c_tbp,
                         d=d_tbp,
                         l=l_tbp + lnz)
        else:
            pu.plot_step(sps_log,
                         info['bin_ends'],
                         bin_true,
                         w=w_tbp,
                         s=s_tbp,
                         a=a_tbp,
                         c=c_tbp,
                         d=d_tbp,
                         l=l_tbp + lnz)
        # black_plots.append(tbp)
        # black_labels.append(l_tbp+lnz)
    else:
        f = plt.figure(figsize=(5, 5))
        sps_log = f.add_subplot(1, 1, 1)
        sps_log.set_xlabel(r'$z$')

    # sps_log.set_yscale("log")
    sps_log.ticklabel_format(style='sci', axis='y')
    if log:
        sps_log.set_ylim(-4., 1.)
        sps_log.set_ylabel(r'$\ln[n(z)]$')
    else:
        sps_log.set_ylim(-0.15, 1.5)
        sps_log.set_ylabel(r'$n(z)$')
    sps_log.set_xlim(info['bin_ends'][0], info['bin_ends'][-1])

    # ipr, =
    if log:
        pu.plot_step(sps_log,
                     info['bin_ends'],
                     info['log_interim_prior'],
                     w=w_int,
                     s=s_int,
                     a=a_int,
                     c=c_int,
                     d=d_int,
                     l=l_int + lnz)
    else:
        pu.plot_step(sps_log,
                     info['bin_ends'],
                     np.exp(info['log_interim_prior']),
                     w=w_int,
                     s=s_int,
                     a=a_int,
                     c=c_int,
                     d=d_int,
                     l=l_int + lnz)
    # if info['truth'] is not None:
    #     pu.plot_step(mini_sps, info['bin_ends'],
    #                     np.exp(info['log_interim_prior']) / bin_true,
    #                     w=w_int, s=s_int, a=a_int, c=c_int, d=d_int)
    # black_plots.append(ipr)
    # black_labels.append(l_int+lnz)

    # sps_log.legend(fontsize='x-small', loc='lower center', frameon=False)
    # f.subplots_adjust(hspace=0, wspace=0)
    # f.savefig(os.path.join(plot_dir, 'input.png'), bbox_inches='tight', pad_inches = 0, dpi=d.dpi)

    # sps_log.legend(handles=black_plots, fontsize='x-small', loc='upper left', frameon=False)
    # lb = plt.gca().add_artist(black_legend)

    if 'log_stacked_nz' in info['estimators']:
        # stk, =
        # color_plots.insert(0, stk)
        # color_labels.insert(0, l_stk+lnz)
        err_txt = None
        if info['truth'] is not None:
            err_txt = make_err_txt(info, 'log_stacked_nz')
            if mini:
                pu.plot_step(
                    mini_sps,
                    info['bin_ends'],
                    (np.exp(info['estimators']['log_stacked_nz']) / bin_true -
                     1.) * 100.,
                    w=w_stk,
                    s=s_stk,
                    a=a_stk,
                    c=c_stk,
                    d=d_stk)
        if log:
            pu.plot_step(sps_log,
                         info['bin_ends'],
                         info['estimators']['log_stacked_nz'],
                         w=w_stk,
                         s=s_stk,
                         a=a_stk,
                         c=c_stk,
                         d=d_stk,
                         l=l_stk + lnz + err_txt)
        else:
            pu.plot_step(sps_log,
                         info['bin_ends'],
                         np.exp(info['estimators']['log_stacked_nz']),
                         w=w_stk,
                         s=s_stk,
                         a=a_stk,
                         c=c_stk,
                         d=d_stk,
                         l=l_stk + lnz + err_txt)

    if 'log_mexp_nz' in info['estimators']:
        # exp, =
        # color_plots.insert(0, exp)
        # color_labels.insert(0, l_exp+lnz)
        err_txt = None
        if info['truth'] is not None:
            err_txt = make_err_txt(info, 'log_mexp_nz')
            if mini:
                pu.plot_step(
                    mini_sps,
                    info['bin_ends'],
                    (np.exp(info['estimators']['log_mexp_nz']) / bin_true - 1.)
                    * 100.,
                    w=w_exp,
                    s=s_exp,
                    a=a_exp,
                    c=c_exp,
                    d=d_exp)
        if log:
            pu.plot_step(sps_log,
                         info['bin_ends'],
                         info['estimators']['log_mexp_nz'],
                         w=w_exp,
                         s=s_exp,
                         a=a_exp,
                         c=c_exp,
                         d=d_exp,
                         l=l_exp + lnz + err_txt)
        else:
            pu.plot_step(sps_log,
                         info['bin_ends'],
                         np.exp(info['estimators']['log_mexp_nz']),
                         w=w_exp,
                         s=s_exp,
                         a=a_exp,
                         c=c_exp,
                         d=d_exp,
                         l=l_exp + lnz + err_txt)

    if 'log_mmap_nz' in info['estimators']:
        # mmp, =
        # color_plots.insert(0, mmp)
        # color_labels.insert(0, l_map+lnz)
        err_txt = None
        if info['truth'] is not None:
            err_txt = make_err_txt(info, 'log_mmap_nz')
            if mini:
                pu.plot_step(
                    mini_sps,
                    info['bin_ends'],
                    (np.exp(info['estimators']['log_mmap_nz']) / bin_true - 1.)
                    * 100.,
                    w=w_map,
                    s=s_map,
                    a=a_map,
                    c=c_map,
                    d=d_map)
        if log:
            pu.plot_step(sps_log,
                         info['bin_ends'],
                         info['estimators']['log_mmap_nz'],
                         w=w_map,
                         s=s_map,
                         a=a_map,
                         c=c_map,
                         d=d_map,
                         l=l_map + lnz + err_txt)
        else:
            pu.plot_step(sps_log,
                         info['bin_ends'],
                         np.exp(info['estimators']['log_mmap_nz']),
                         w=w_map,
                         s=s_map,
                         a=a_map,
                         c=c_map,
                         d=d_map,
                         l=l_map + lnz + err_txt)

    if 'log_mean_sampled_nz' in info['estimators']:
        # plot_samples(info, plot_dir)
        (locs, scales) = s.norm_fit(info['log_sampled_nz_meta_data']['chains'])
        # bfe, =
        # pu.plot_step(sps_log, info['bin_ends'],
        #                 info['estimators']['log_mean_sampled_nz'],
        #                 w=w_bfe, s=s_bfe, a=a_bfe, c=c_bfe, d=d_bfe, l=l_bfe+lnz)
        # color_plots.insert(0, bfe)
        # color_labels.insert(0, l_bfe+lnz)
        err_txt = None
        if info['truth'] is not None:
            err_txt = make_err_txt(info, 'log_mean_sampled_nz')
            for k in range(len(info['bin_ends']) - 1):
                x_errs = [
                    info['bin_ends'][k], info['bin_ends'][k],
                    info['bin_ends'][k + 1], info['bin_ends'][k + 1]
                ]
                y_errs_1 = np.exp(
                    np.array([
                        locs[k] - scales[k], locs[k] + scales[k],
                        locs[k] + scales[k], locs[k] - scales[k]
                    ])) / bin_true[k]
                y_errs_2 = np.exp(
                    np.array([
                        locs[k] - 2 * scales[k], locs[k] + 2 * scales[k],
                        locs[k] + 2 * scales[k], locs[k] - 2 * scales[k]
                    ])) / bin_true[k]
                if mini:
                    mini_sps.fill(x_errs, (y_errs_1 - 1.) * 100.,
                                  color=c_bfe,
                                  alpha=0.5,
                                  linewidth=0.)
                    mini_sps.fill(x_errs, (y_errs_2 - 1.) * 100.,
                                  color=c_bfe,
                                  alpha=0.25,
                                  linewidth=0.)
            # pu.plot_step(mini_sps, info['bin_ends'],
            #                 (1. - np.exp(info['estimators']['log_mean_sampled_nz']) / bin_true) * 100.,
            #                 w=w_bfe, s=s_bfe, a=a_bfe, c=c_bfe, d=d_bfe)
        for k in range(len(info['bin_ends']) - 1):
            x_errs = [
                info['bin_ends'][k], info['bin_ends'][k],
                info['bin_ends'][k + 1], info['bin_ends'][k + 1]
            ]
            log_y_errs_1 = np.array([
                locs[k] - scales[k], locs[k] + scales[k], locs[k] + scales[k],
                locs[k] - scales[k]
            ])
            log_y_errs_2 = np.array([
                locs[k] - 2 * scales[k], locs[k] + 2 * scales[k],
                locs[k] + 2 * scales[k], locs[k] - 2 * scales[k]
            ])
            # y_errs_1 = [np.exp(locs[k] - scales[k]), np.exp(locs[k] + scales[k]),
            #             np.exp(locs[k] + scales[k]), np.exp(locs[k] - scales[k])]
            # y_errs_2 = [np.exp(locs[k] - 2 * scales[k]), np.exp(locs[k] + 2 * scales[k]),
            #             np.exp(locs[k] + 2 * scales[k]), np.exp(locs[k] - 2 * scales[k])]
            if log:
                sps_log.fill(x_errs,
                             log_y_errs_1,
                             color=c_bfe,
                             alpha=0.5,
                             linewidth=0.)
                sps_log.fill(x_errs,
                             log_y_errs_2,
                             color=c_bfe,
                             alpha=0.25,
                             linewidth=0.)
            else:
                sps_log.fill(x_errs,
                             np.exp(log_y_errs_1),
                             color=c_bfe,
                             alpha=0.5,
                             linewidth=0.)
                sps_log.fill(x_errs,
                             np.exp(log_y_errs_2),
                             color=c_bfe,
                             alpha=0.25,
                             linewidth=0.)
        plt.plot([100.], [100.],
                 linewidth=w_bfe,
                 linestyle=s_bfe,
                 alpha=a_bfe,
                 color=c_bfe,
                 dashes=d_bfe[0][-1],
                 label=l_bfe + lnz + err_txt)

    if 'log_mmle_nz' in info['estimators']:
        # mle, =
        # color_plots.insert(0, mle)
        # color_labels.insert(0, l_mle+lnz)
        err_txt = None
        if info['truth'] is not None:
            err_txt = make_err_txt(info, 'log_mmle_nz')
            if mini:
                pu.plot_step(
                    mini_sps,
                    info['bin_ends'],
                    (np.exp(info['estimators']['log_mmle_nz']) / bin_true - 1.)
                    * 100.,
                    w=w_mle,
                    s=s_mle,
                    a=a_mle,
                    c=c_mle,
                    d=d_mle)
        if log:
            pu.plot_step(sps_log,
                         info['bin_ends'],
                         info['estimators']['log_mmle_nz'],
                         w=w_mle,
                         s=s_mle,
                         a=a_mle,
                         c=c_mle,
                         d=d_mle,
                         l=l_mle + lnz + err_txt)
        else:
            pu.plot_step(sps_log,
                         info['bin_ends'],
                         np.exp(info['estimators']['log_mmle_nz']),
                         w=w_mle,
                         s=s_mle,
                         a=a_mle,
                         c=c_mle,
                         d=d_mle,
                         l=l_mle + lnz + err_txt)

    # sps_log.legend(handles=color_plots[:-1], fontsize='x-small', loc='lower center', frameon=False)
    sps_log.legend(fontsize='x-small', loc='upper right', frameon=False)
    f.subplots_adjust(hspace=0, wspace=0)
    f.savefig(os.path.join(plot_dir, prepend + 'estimators.png'),
              bbox_inches='tight',
              pad_inches=0,
              dpi=d.dpi)
    print(info['stats'])
    return
Ejemplo n.º 9
0
def set_up_burn_in_plots(n_bins, n_walkers):
    """
    Creates plotting objects for sampler progress

    Parameters
    ----------
    n_bins: int
        number of parameters defining n(z)
    n_walkers: int
        number of walkers for the sampler

    Returns
    -------
    plot_information: tuple
        contains figure and subplot objects for Gelman-Rubin evolution,
        autocorrelation times, acceptance fractions, posterior probabilities,
        and chain evolution
    """
    pu.set_up_plot()

    f_gelman_rubin_evolution = plt.figure(figsize=(5, 5))
    sps_gelman_rubin_evolution = f_gelman_rubin_evolution.add_subplot(1, 1, 1)
    f_gelman_rubin_evolution.subplots_adjust(hspace=0, wspace=0)
    sps_gelman_rubin_evolution.set_ylabel(r'Gelman-Rubin Statistic')
    sps_gelman_rubin_evolution.set_xlabel(r'accepted sample number')
    gelman_rubin_evolution_plot = [
        f_gelman_rubin_evolution, sps_gelman_rubin_evolution
    ]

    f_autocorrelation_times = plt.figure(figsize=(5, 5))
    sps_autocorrelation_times = f_autocorrelation_times.add_subplot(1, 1, 1)
    f_autocorrelation_times.subplots_adjust(hspace=0, wspace=0)
    sps_autocorrelation_times.set_ylabel(r'autocorrelation time')
    sps_autocorrelation_times.set_xlabel(r'accepted sample number')
    sps_autocorrelation_times.set_ylim(0, 100)
    autocorrelation_times_plot = [
        f_autocorrelation_times, sps_autocorrelation_times
    ]

    f_acceptance_fractions = plt.figure(figsize=(5, 5))
    sps_acceptance_fractions = f_acceptance_fractions.add_subplot(1, 1, 1)
    f_acceptance_fractions.subplots_adjust(hspace=0, wspace=0)
    sps_acceptance_fractions.set_ylim(0, 1)
    sps_acceptance_fractions.set_ylabel('acceptance fraction per bin')
    sps_acceptance_fractions.set_xlabel('number of iterations')
    acceptance_fractions_plot = [
        f_acceptance_fractions, sps_acceptance_fractions
    ]

    f_posterior_probabilities = plt.figure(figsize=(5, 5))
    sps_posterior_probabilities = f_posterior_probabilities.add_subplot(
        1, 1, 1)
    f_posterior_probabilities.subplots_adjust(hspace=0, wspace=0)
    sps_posterior_probabilities.set_ylabel(r'log probability per walker')
    sps_posterior_probabilities.set_xlabel(r'accepted sample number')
    posterior_probabilities_plot = [
        f_posterior_probabilities, sps_posterior_probabilities
    ]

    bin_range = range(n_bins)
    f_chain_evolution = plt.figure(figsize=(5, 5 * n_bins))
    sps_chain_evolution = [
        f_chain_evolution.add_subplot(n_bins, 1, k + 1) for k in bin_range
    ]
    for k in bin_range:
        sps_chain_evolution[k].set_ylabel(r'parameter value ' + str(k))
        sps_chain_evolution[k].set_xlabel(r'sample number')
    f_chain_evolution.subplots_adjust(hspace=0, wspace=0)
    random_walkers = [
        np.random.randint(0, n_walkers) for i in range(d.plot_colors)
    ]
    chain_evolution_plot = [
        f_chain_evolution, sps_chain_evolution, random_walkers
    ]

    plot_information = (gelman_rubin_evolution_plot,
                        autocorrelation_times_plot, acceptance_fractions_plot,
                        posterior_probabilities_plot, chain_evolution_plot)

    return plot_information