Exemple #1
0
    def testMixtures(self):
        from getdist.gaussian_mixtures import Mixture2D, GaussianND

        cov1 = [[0.001 ** 2, 0.0006 * 0.05], [0.0006 * 0.05, 0.05 ** 2]]
        cov2 = [[0.01 ** 2, -0.005 * 0.03], [-0.005 * 0.03, 0.03 ** 2]]
        mean1 = [0.02, 0.2]
        mean2 = [0.023, 0.09]
        mixture = Mixture2D([mean1, mean2], [cov1, cov2], names=['zobs', 't'], labels=[r'z_{\rm obs}', 't'],
                            label='Model')
        tester = 0.03
        cond = mixture.conditionalMixture(['zobs'], [tester])
        marge = mixture.marginalizedMixture(['zobs'])
        # test P(x,y) = P(y)P(x|y)
        self.assertAlmostEqual(mixture.pdf([tester, 0.15]), marge.pdf([tester]) * cond.pdf([0.15]))

        samples = mixture.MCSamples(3000, label='Samples')
        g = plots.getSubplotPlotter()
        g.triangle_plot([samples, mixture], filled=False)
        g.plot_1d(cond, 't')

        s1 = 0.0003
        covariance = [[s1 ** 2, 0.6 * s1 * 0.05, 0], [0.6 * s1 * 0.05, 0.05 ** 2, 0.2 ** 2], [0, 0.2 ** 2, 2 ** 2]]
        mean = [0.017, 1, -2]
        gauss = GaussianND(mean, covariance)
        g = plots.getSubplotPlotter()
        g.triangle_plot(gauss, filled=True)
Exemple #2
0
    def testMixtures(self):
        from getdist.gaussian_mixtures import Mixture2D, GaussianND

        cov1 = [[0.001**2, 0.0006 * 0.05], [0.0006 * 0.05, 0.05**2]]
        cov2 = [[0.01**2, -0.005 * 0.03], [-0.005 * 0.03, 0.03**2]]
        mean1 = [0.02, 0.2]
        mean2 = [0.023, 0.09]
        mixture = Mixture2D([mean1, mean2], [cov1, cov2],
                            names=['zobs', 't'],
                            labels=[r'z_{\rm obs}', 't'],
                            label='Model')
        tester = 0.03
        cond = mixture.conditionalMixture(['zobs'], [tester])
        marge = mixture.marginalizedMixture(['zobs'])
        # test P(x,y) = P(y)P(x|y)
        self.assertAlmostEqual(mixture.pdf([tester, 0.15]),
                               marge.pdf([tester]) * cond.pdf([0.15]))

        samples = mixture.MCSamples(3000, label='Samples')
        g = plots.getSubplotPlotter()
        g.triangle_plot([samples, mixture], filled=False)
        g.plot_1d(cond, 't')

        s1 = 0.0003
        covariance = [[s1**2, 0.6 * s1 * 0.05, 0],
                      [0.6 * s1 * 0.05, 0.05**2, 0.2**2], [0, 0.2**2, 2**2]]
        mean = [0.017, 1, -2]
        gauss = GaussianND(mean, covariance)
        g = plots.getSubplotPlotter()
        g.triangle_plot(gauss, filled=True)
Exemple #3
0
def main():
    args = sys.argv
    if(len(args)<2):
        print("error: the number of input parameters is not correct; input command must be")
        print(">$ python post.py input_file_name[e.g. dist.ini]")
        sys.exit(1)

    Ini = inifile.IniFile(args[1])
    Ini.Dump()

    section = "POSTPROCESS"
    postroot = Ini.ReadString(section,"postroot")
    Ini.Dump(postroot+"_dist.ini")

    chains = Ini.ReadString(section,"chains").split(",")
    chainlabels = Ini.ReadString(section,"chainlabels").split(",")
    names = Ini.ReadString(section,"paramnames").split(",")
    labels = Ini.ReadString(section,"paramlabels").split(",")
    dnames = Ini.ReadString(section,"dparamnames").split(",")
    dlabels = Ini.ReadString(section,"dparamlabels").split(",")

    PP = PostProcess(chains,chainlabels,names,labels,dnames,dlabels)
    PP.ReadChains()

    # triangle plots
    g = plots.getSubplotPlotter()
    g.triangle_plot(PP.getdist_samples,filled=True)
    g.export(postroot+"_triangle.pdf")
Exemple #4
0
 def triangle_plot(self):
     if not HAS_GETDIST:
         raise RuntimeError(
             'you need to install getdist to get the triangle plot.')
     if 0 < self.m_plot < self.dim:
         plot_data = self._data[:, :self.m_plot]
     else:
         plot_data = self._data
     samples = MCSamples(samples=plot_data)
     g = plots.getSubplotPlotter()
     g.triangle_plot([
         samples,
     ],
                     filled=True,
                     contour_args={'alpha': 0.8},
                     diag1d_kwargs={'normalized': True})
     if self.i_iter:
         plt.suptitle("triangle plot after iteration " + str(self.i_iter),
                      fontsize=plot_data.shape[-1] * 4,
                      ha='left')
     else:
         plt.suptitle('triangle plot for the initial data',
                      fontsize=plot_data.shape[-1] * 4,
                      ha='left')
     plt.show()
Exemple #5
0
def main():
    mean = [0, 0, 0]
    cov = [[1, 0, 0], [0, 100, 0], [0, 0, 8]]
    x1, x2, x3 = np.random.multivariate_normal(mean, cov, 50000).T
    s1 = np.c_[x1, x2, x3]

    mean = [0.2, 0.5, 6.]
    cov = [[0.7, 0.3, 0.1], [0.3, 10, 0.25], [0.1, 0.25, 7]]
    x1, x2, x3 = np.random.multivariate_normal(mean, cov, 50000).T
    s2 = np.c_[x1, x2, x3]

    names = ['x_1', 'x_2', 'x_3']
    samples1 = MCSamples(samples=s1, labels=names, label='sample1')
    samples2 = MCSamples(samples=s2, labels=names, label='sample2')

    get_constraints(samples1)
    get_constraints(samples2)
    
    print("cov(x_1, x_3) = ", samples2.cov(pars=[0,2]))
    print("cov(x_1, x_2) = ", samples2.cov(pars=[0,1]))

    g = plots.getSubplotPlotter()
    g.triangle_plot([samples1, samples2], filled=True)
    g.export('triangle_plot.png')
    g.export('triangle_plot.pdf')
    return 
Exemple #6
0
    def triangle_plot(self, samples=None, savefig=False, filename=None):

        # Set samples to the posterior samples by default
        if samples is None:
            samples = [self.posterior_samples]
        mc_samples = [
            MCSamples(samples=s,
                      names=self.names,
                      labels=self.labels,
                      ranges=self.ranges) for s in samples
        ]

        # Triangle plot
        with mpl.rc_context():
            g = plots.getSubplotPlotter(width_inch=12)
            g.settings.figure_legend_frame = False
            g.settings.alpha_filled_add = 0.6
            g.settings.axes_fontsize = 14
            g.settings.legend_fontsize = 16
            g.settings.lab_fontsize = 20
            g.triangle_plot(mc_samples, filled_compare=True, normalized=True)
            for i in range(0, len(samples[0][0, :])):
                for j in range(0, i + 1):
                    ax = g.subplots[i, j]
                    xtl = ax.get_xticklabels()
                    ax.set_xticklabels(xtl, rotation=45)
            plt.tight_layout()
            plt.subplots_adjust(hspace=0, wspace=0)

            if savefig:
                plt.savefig(filename)
            if self.show_plot:
                plt.show()
            else:
                plt.close()
Exemple #7
0
def main(path2config):
    # read parameters
    config = yaml.load(open(path2config))
    fit_params = config['fit_params']
    ch_params = config['ch_config_params']

    # parameters
    n_iter = ch_params['sampleIterations']
    w_rat = ch_params['walkersRatio']
    b_iter = ch_params['burninIterations']
    par_names = fit_params.keys()
    lab_names = par_names
    n_par = len(par_names)

    # what are we plotting
    HOD_pars = par_names
    filename = "triangle_test.png"
    dir_chains = ch_params['path2output']

    # walkers ratio, number of params and burn in iterations
    marg_outfile = os.path.join(dir_chains,
                                (ch_params['chainsPrefix'] + ".txt"))

    # read the samples after removing burnin
    marg_hsc = get_samples(marg_outfile, par_names, w_rat, n_par, b_iter)

    # Triangle plot
    g = plots.getSubplotPlotter()
    g.settings.legend_fontsize = 20
    g.settings.scaling_factor = 0.1
    g.triangle_plot([marg_hsc], params=HOD_pars)
    plt.savefig(filename)
    plt.close()
Exemple #8
0
def plot_sample(sample, params):
    import matplotlib.pyplot as plt
    import getdist as gd
    import getdist.plots as gdplt
    gdsamples = sample.as_getdist_mcsamples()
    gdplot = gdplt.getSubplotPlotter()
    gdplot.triangle_plot(gdsamples, params, filled=True)
    gdplot.export("test.png")
Exemple #9
0
def triangleplot(samples, pinfo='cmbruler'):
    g = gplt.getSubplotPlotter(width_inch=9.0)
    g.settings.axes_fontsize = 12.0
    g.settings.figure_legend_frame = False
    g.settings.num_plot_contours = 2
    pars = info[pinfo]
    g.triangle_plot(samples, pars)
    gplt.plt.show()
    return
def plot_sampling(mcmc_config,
                  param_info_wod,
                  burned_chain,
                  theta_ml,
                  include_outlier_dist=False,
                  include_ml=True):

    chain_tag = mcmc_config.chain_tag
    param_names_wod, param_labels_wod, param_guess_wod, param_priors_wod = param_info_wod

    print('\nProcessing MCMC chain...')

    if include_outlier_dist:
        ndim = burned_chain.shape[1]
    elif not include_outlier_dist:
        ndim = burned_chain.shape[1] - 3

    names = param_labels_wod[0:ndim].copy()

    ranges_gd = {}

    for i in range(ndim):
        ranges_gd[param_labels_wod[i]] = (param_priors_wod[i][1],
                                          param_priors_wod[i][1])

    gd_samples = MCSamples(samples=burned_chain[:, 0:ndim],
                           names=param_names_wod[0:ndim],
                           labels=param_labels_wod[0:ndim])  #,
    #ranges=ranges_gd[0:ndim])

    if ndim == 1:
        fig = plots.getSinglePlotter(width_inch=5)
        fig.plot_1d(gd_samples, names[0], normalized=True)
    elif ndim > 1:
        fig = plots.getSubplotPlotter()
        fig.triangle_plot([gd_samples], filled=True)
        if include_ml:
            for i in range(ndim):
                ax = fig.subplots[i, i]
                ax.axvline(theta_ml[i], color='r', ls='-', alpha=0.75)
            for i in range(ndim - 1):
                for j in range(i + 1):
                    ax = fig.subplots[i + 1, j]
                    ax.plot(theta_ml[j],
                            theta_ml[i + 1],
                            'w*',
                            zorder=3,
                            markersize=5.)
    plt.show()
    fig_name = 'MCMC_sampling_{}'.format(chain_tag)
    save_figure(mcmc_config, fig, fig_name, gd_plot=True)
    #     plot_file = '{}/MCMC_sampling_{}.png'.format(plots_dir, chain_tag)
    #     mcmc_fig.export(plot_file)
    plt.close()
Exemple #11
0
 def triangle_plot(self):
     samples = MCSamples(samples=self._data)
     g = plots.getSubplotPlotter()
     g.triangle_plot([
         samples,
     ],
                     filled=True,
                     contour_args={'alpha': 0.8},
                     diag1d_kwargs={'normalized': True})
     plt.show()
     print("triangle plot at iteration " + str(iteration))
     print("\n---------- ---------- ---------- ---------- ----------\n")
Exemple #12
0
    def testPlots(self):
        self.samples = self.testdists.bimodal[0].MCSamples(12000,
                                                           logLikes=True)
        g = plots.getSinglePlotter()
        samples = self.samples
        p = samples.getParams()
        samples.addDerived(p.x + (5 + p.y)**2, name='z')
        samples.addDerived(p.x, name='x.yx', label='forPattern')
        samples.addDerived(p.y, name='x.2', label='x_2')
        samples.updateBaseStatistics()

        g.plot_1d(samples, 'x')
        g.newPlot()
        g.plot_1d(samples, 'y', normalized=True, marker=0.1, marker_color='b')
        g.newPlot()
        g.plot_2d(samples, 'x', 'y')
        g.newPlot()
        g.plot_2d(samples, 'x', 'y', filled=True)
        g.newPlot()
        g.plot_2d(samples, 'x', 'y', shaded=True)
        g.newPlot()
        g.plot_2d_scatter(samples, 'x', 'y', color='red', colors=['blue'])
        g.newPlot()
        g.plot_3d(samples, ['x', 'y', 'z'])

        g = plots.getSubplotPlotter(width_inch=8.5)
        g.plots_1d(samples, ['x', 'y'], share_y=True)
        g.newPlot()
        g.triangle_plot(samples, ['x', 'y', 'z'])
        g.newPlot()
        g.triangle_plot(samples, ['x', 'y'], plot_3d_with_param='z')
        g.newPlot()
        g.rectangle_plot(['x', 'y'], ['z'], roots=samples, filled=True)
        prob2 = self.testdists.bimodal[1]
        samples2 = prob2.MCSamples(12000)
        g.newPlot()
        g.triangle_plot([samples, samples2], ['x', 'y'])
        g.newPlot()
        g.plots_2d([samples, samples2], param_pairs=[['x', 'y'], ['x', 'z']])
        g.newPlot()
        g.plots_2d([samples, samples2], 'x', ['z', 'y'])
        g.newPlot()
        self.assertEqual(
            [name.name for name in samples.paramNames.parsWithNames('x.*')],
            ['x.yx', 'x.2'])
        g.triangle_plot(samples, 'x.*')
        samples.updateSettings({'contours': '0.68 0.95 0.99'})
        g.settings.num_contours = 3
        g.plot_2d(samples, 'x', 'y', filled=True)
        g.add_y_bands(0.2, 1.5)
        g.add_x_bands(-0.1, 1.2, color='red')
Exemple #13
0
    def plot_chain(self,
                   chain,
                   save_figure=False,
                   prefix=None,
                   extension='pdf'):
        """
        Produces a triangle plot from a chain, which can be
        saved to file automatically.

        Args:
            chain (array): 2D array with shape [n_samples,n_params],
                where `n_samples` is the number of samples in the
                chain and `n_params` is the number of free parameters
                for this likelihood run.
            save_figures (bool): if true, figures will be saved to
                file. File names will take the form:
                <`prefix`>triangle.<`extension`>
            prefix (str): output prefix.
            extension (str): plot extension (pdf, pdf etc.).

        Returns:
            figure object
        """
        from getdist import MCSamples
        from getdist import plots as gplots

        nsamples = len(chain)
        # Generate samples
        ranges = {}
        for n, pr in zip(self.p_free_names, self.p_free_prior):
            if pr['type'] == 'TopHat':
                ranges[n] = pr['values']
        samples = MCSamples(samples=chain[nsamples // 4:],
                            names=self.p_free_names,
                            labels=self.p_free_labels,
                            ranges=ranges)
        samples.smooth_scale_2D = 0.2

        # Triangle plot
        g = gplots.getSubplotPlotter()
        g.triangle_plot([samples], filled=True)

        if save_figure:
            if prefix is None:
                prefix = self.prefix_out
            fname = prefix + 'triangle.' + extension
            g.export(fname)

        return g
Exemple #14
0
 def triangle_plot(self):
     if not HAS_GETDIST:
         raise RuntimeError(
             'you need to install getdist to get the triangle plot.')
     samples = MCSamples(samples=self._data)
     g = plots.getSubplotPlotter()
     g.triangle_plot([
         samples,
     ],
                     filled=True,
                     contour_args={'alpha': 0.8},
                     diag1d_kwargs={'normalized': True})
     plt.show()
     print("triangle plot at iteration " + str(iteration))
     print("\n---------- ---------- ---------- ---------- ----------\n")
Exemple #15
0
    def testPlots(self):
        self.samples = self.testdists.bimodal[0].MCSamples(12000, logLikes=True)
        g = plots.getSinglePlotter()
        samples = self.samples
        p = samples.getParams()
        samples.addDerived(p.x + (5 + p.y) ** 2, name='z')
        samples.addDerived(p.x, name='x.yx', label='forPattern')
        samples.addDerived(p.y, name='x.2', label='x_2')
        samples.updateBaseStatistics()

        g.plot_1d(samples, 'x')
        g.newPlot()
        g.plot_1d(samples, 'y', normalized=True, marker=0.1, marker_color='b')
        g.newPlot()
        g.plot_2d(samples, 'x', 'y')
        g.newPlot()
        g.plot_2d(samples, 'x', 'y', filled=True)
        g.newPlot()
        g.plot_2d(samples, 'x', 'y', shaded=True)
        g.newPlot()
        g.plot_2d_scatter(samples, 'x', 'y', color='red', colors=['blue'])
        g.newPlot()
        g.plot_3d(samples, ['x', 'y', 'z'])

        g = plots.getSubplotPlotter(width_inch=8.5)
        g.plots_1d(samples, ['x', 'y'], share_y=True)
        g.newPlot()
        g.triangle_plot(samples, ['x', 'y', 'z'])
        g.newPlot()
        g.triangle_plot(samples, ['x', 'y'], plot_3d_with_param='z')
        g.newPlot()
        g.rectangle_plot(['x', 'y'], ['z'], roots=samples, filled=True)
        prob2 = self.testdists.bimodal[1]
        samples2 = prob2.MCSamples(12000)
        g.newPlot()
        g.triangle_plot([samples, samples2], ['x', 'y'])
        g.newPlot()
        g.plots_2d([samples, samples2], param_pairs=[['x', 'y'], ['x', 'z']])
        g.newPlot()
        g.plots_2d([samples, samples2], 'x', ['z', 'y'])
        g.newPlot()
        self.assertEquals([name.name for name in samples.paramNames.parsWithNames('x.*')], ['x.yx', 'x.2'])
        g.triangle_plot(samples, 'x.*')
        samples.updateSettings({'contours': '0.68 0.95 0.99'})
        g.settings.num_contours = 3
        g.plot_2d(samples, 'x', 'y', filled=True)
        g.add_y_bands(0.2, 1.5)
        g.add_x_bands(-0.1, 1.2, color='red')
Exemple #16
0
def main():
    import getdist
    from getdist import plots, MCSamples,  loadMCSamples
    import numpy as np
    import pandas as pd
    args = parse_args()
    out = os.path.expanduser(args.out)
    out = os.path.join(out,'plots')
    if not os.path.isdir(out):
        os.makedirs(out)

    allnames = np.array(['Om','h0','Ob','ns','a_s','Onuh2','b1','b2','b3','b4','b5','m1','m2','m3','m4','ia_a','ia_alpha', 'wpz_b1','wpz_b2','wpz_b3','wpz_b4','lpz_b1','lpz_bin2','lpz_bin3','lpz_bin4','lpz_bin5','s8','like','post','weight'])
    alllabels = np.array(['\Omega_m', 'h', '\Omega_b', 'n_s','a_s', r'\Omega_{\nu}','b1','b2','b3','b4','b5','m1','m2','m3','m4','ia_a','ia_alpha', 'wpz_b1','wpz_b2','wpz_b3','wpz_b4','lpz_b1','lpz_bin2','lpz_bin3','lpz_bin4','lpz_bin5',r'\sigma_{8}','like','post','weight'])
    #useindex = [0, 1, 2, 3, 4, 5,- 4]
    useindex = [0, 1, 2, 3, 4, 5,- 4]
    usednames = allnames[useindex]
    usedlabels = alllabels[useindex]

    
    nsample =  get_nsample(args.samplesfile_forecast)
    allsamplestable = np.loadtxt(args.samplesfile_forecast)
    allsamplestable =  allsamplestable[ -nsample:, : ]                     
    usedsamples = allsamplestable[:, useindex]
    usedweights = allsamplestable[: , -1]
    usedpost =  allsamplestable[:, -2]
    samples = MCSamples(samples=usedsamples, names=usednames,
                        labels=usedlabels, weights=usedweights , loglikes=usedpost,
                        label='Forecast' )
    samples.removeBurn(remove=0.1)

    nsample_cont =  get_nsample(args.samplesfile_contaminated)
    allsamplestable_cont = np.loadtxt(args.samplesfile_contaminated)
    allsamplestable_cont =  allsamplestable_cont[-nsample_cont:, : ]
    usedsamples_cont = allsamplestable_cont[:, useindex]
    usedweights_cont = allsamplestable_cont[: , -1]
    usedpost_cont =  allsamplestable_cont[:, -2]
    samples_cont = MCSamples(samples=usedsamples_cont,
                             names=usednames, labels=usedlabels, weights=usedweights_cont ,loglikes=usedpost_cont,
                             label='PSF contamination' )
    samples_cont.removeBurn(remove=0.1)



    
    g = plots.getSubplotPlotter()
    g.triangle_plot([samples, samples_cont], filled_compare=True, contour_colors=['green','darkblue'])
    #g.add_legend(legend_labels=[legend_name], fontsize=36, legend_loc=(-3.5,7))
    g.export("getdistplot.png")
def make_plot(chainfile, savefile, true_parameter_values=None, pnames=None, ranges=None):
    """Make a getdist plot"""
    samples = np.loadtxt(chainfile)
    ticks = {}
    if pnames is None:
        #Default emulator parameters
        pnames = [r"d\tau_0", r"\tau_0", r"n_s", r"A_\mathrm{P} \times 10^9", r"H_S", r"H_A", r"h"]
        samples[:,3] *= 1e9
        true_parameter_values[3] *= 1e9
        #Ticks we want to show for each parameter
        ticks = {pnames[3]: [1.5, 2.0, 2.5], pnames[4]: [-0.6,-0.3, 0.], pnames[5]: [0.5,0.7,1.0,1.3], pnames[6]: [0.66, 0.70, 0.74]}
    prange = None
    if ranges is not None:
        prange = {pnames[i] : ranges[i] for i in range(len(pnames))}
    posterior_MCsamples = gd.MCSamples(samples=samples, names=pnames, labels=pnames, label='', ranges=prange)

    print("Sim=",savefile)
    #Get and print the confidence limits
    for i in range(len(pnames)):
        strr = pnames[i]+" 1-sigma, 2-sigma: "
        for j in (0.16, 1-0.16, 0.025, 1-0.025):
            strr += str(round(posterior_MCsamples.confidence(i, j),5)) + " "
        print(strr)
    subplot_instance = gdp.getSubplotPlotter()
    subplot_instance.triangle_plot([posterior_MCsamples], filled=True)
#     colour_array = np.array(['black', 'red', 'magenta', 'green', 'green', 'purple', 'turquoise', 'gray', 'red', 'blue'])

    for pi in range(samples.shape[1]):
        for pi2 in range(pi + 1):
            #Place horizontal and vertical lines for the true point
            ax = subplot_instance.subplots[pi, pi2]
            ax.yaxis.label.set_size(16)
            ax.xaxis.label.set_size(16)
            if pi == samples.shape[1]-1 and pnames[pi2] in ticks:
                ax.set_xticks(ticks[pnames[pi2]])
            if pi2 == 0 and pnames[pi] in ticks:
                ax.set_yticks(ticks[pnames[pi]])
            ax.axvline(true_parameter_values[pi2], color='gray', ls='--', lw=2)
            if pi2 < pi:
                ax.axhline(true_parameter_values[pi], color='gray', ls='--', lw=2)
                #Plot the emulator points
#                 if parameter_index > 1:
#                     ax.scatter(simulation_parameters_latin[:, parameter_index2 - 2], simulation_parameters_latin[:, parameter_index - 2], s=54, color=colour_array[-1], marker='+')

#     legend_labels = ['+ Initial Latin hypercube']
#     subplot_instance.add_legend(legend_labels, legend_loc='upper right', colored_text=True, figure=True)
    plt.savefig(savefile)
Exemple #18
0
def plot_Tchain(Tchain, axes_labels, ranges, names=None):
    """Plot the Tchain using getdist."""
    Tsample = mcsamples.MCSamples(samples=Tchain,
                                  names=names,
                                  labels=axes_labels,
                                  ranges=ranges)

    Tsample.updateSettings({'contours': [0.90, 0.99]})
    Tsample.num_bins_2D = 10
    Tsample.fine_bins_2D = 50
    Tsample.smooth_scale_2D = 0.05

    g = plots.getSubplotPlotter()
    g.settings.num_plot_contours = 2
    g.settings.axes_fontsize = 10
    g.settings.figure_legend_frame = False
    g.settings.lab_fontsize = 20
    g.triangle_plot(
        [Tsample],
        filled=True,  # contour_colors=['green', 'lightgreen']
    )
    return g
Exemple #19
0
    def _triangle_plot(self, these_samples, these_y, plotname):
        names = self._parameters_list
        labels = []

        level_lines = [0.2, 0.4, 0.6, 0.8, 0.95, 0.98]

        num_level_lines = len(level_lines)
        g = plots.getSubplotPlotter(width_inch=9)
        g.settings.num_plot_contours = num_level_lines

        mcsamples = MCSamples(samples=these_samples, names=names, labels=names)
        mcsamples.updateSettings({'contours': level_lines})

        g.triangle_plot(
            [mcsamples],
            names,
            # filled_compare=True,
            legend_labels=labels,
            legend_loc='upper right',
            # filled=False,
            contour_colors=['darkblue', 'green'],
            #                     filled=True,
            #                     contour_lws=[.2, .4, .68, .95, .98]
        )

        n_params = len(names)

        for i in range(n_params):
            for j in range(n_params):
                if j > i:
                    continue

                ax = g.subplots[i, j]
                ax.axvline(these_y[j], color='black', ls='--', alpha=0.4)
                if i != j:
                    ax.axhline(these_y[i], color='black', ls='-.', alpha=0.4)

        g.export(plotname)
Exemple #20
0
    def triangle_plot(self, samples, savefig = False, filename = None):

        mc_samples = [MCSamples(samples=s, names = self.names, labels = self.labels, ranges = self.ranges) for s in samples]

        # Triangle plot
        g = plots.getSubplotPlotter(width_inch = 12)
        g.settings.figure_legend_frame = False
        g.settings.alpha_filled_add=0.6
        g.settings.axes_fontsize=14
        g.settings.legend_fontsize=16
        g.settings.lab_fontsize=20
        g.triangle_plot(mc_samples, filled_compare=True, normalized=True, legend_labels=['Density estimation likelihood-free inference'])
        for i in range(0, len(samples[0][0,:])):
            for j in range(0, i+1):
                ax = g.subplots[i,j]
                xtl = ax.get_xticklabels()
                ax.set_xticklabels(xtl, rotation=45)
        plt.tight_layout()
        plt.subplots_adjust(hspace=0, wspace=0)
        
        if savefig == True:
            plt.savefig(filename)
        plt.show()
colors = [colors[i] for i in order]
labels = [labels[i] for i in order]

samples = []
for root in [d+r for d, r in zip(dirs, roots)]:
    samples.append(loadMCSamples(root))

for samp in samples:
    p = samp.getParams()
    samp.addDerived((p.ombh2+p.omch2)/p.h**2, name="omm", label="\Omega_M")

# Single plot
#g = plots.getSinglePlotter()
#g.plot_2d(samples, "omm", "sigma8", filled=True)#, lims=[0.20, 0.40, 0.55, 0.80])
#g.settings.legend_fontsize = 12
#g.add_legend(["Planck15", "Planck15+BAO", "Planck15+LRG", "Planck15+WiggleZ", "Planck+Clusters"], legend_loc="lower left")

# Subplots
g = plots.getSubplotPlotter()
g.settings.figure_legend_frame = False
g.settings.legend_frac_subplot_margin = 0.2
g.settings.legend_fontsize = 12
g.settings.fig_width_inch = 5
#g.settings.axes_fontsize = 20
g.rectangle_plot(['omm', 'omm'], ['sigma8', 'sigma8'], roots=samples, filled=True,
                colors=colors, param_limits={'omm': (0.2, 0.46), 'sigma8': (0.63, 0.97)},
                plot_texts=[['$\\Lambda CDM$', '$+N_{eff}$'], ['$+M_{\\nu}$', '$+N_{eff}+M_{\\nu}$']],
                legend_labels = labels, legend_ncol=2);

g.export("sigma8_v_om_nEff+Mnu.pdf")
Exemple #22
0
def main():

    if (len(sys.argv) != 5):
        print()
        print("##################################")
        print("Ariel J. Amsellem")
        print("*****@*****.**")
        print("KICP UChicago")
        print("##################################\n")

        print(
            "run_Multinest.py - Run Multinest on Sigmag Measurements to determine measure of best fit with errors."
        )
        print(
            "Usage: python run_Multinest.py [output directory/file name] [data filename] [color] [plot label/title]"
        )
        print(
            "Example: python run_Multinest.py Fiducial_RM splashback_cov_Fiducial_RM.npz    r    Fiducial_Redmapper"
        )
        sys.exit(0)

    out_directory = str(sys.argv[1])
    dat_filename = str(sys.argv[2])
    color = str(sys.argv[3])
    label = str(sys.argv[4])
    label = label.replace("_", " ")

    # Load Data
    data = np.load(
        '/Users/arielamsellem/Desktop/Research/splashback_codes_master/npzs/' +
        dat_filename)
    sigmag = data['sg_mean'][:-4]
    sigmag_sig = data['sg_sig'][:-4]
    sigmag_cov = data['cov'][:-4]
    sigmag_cov = np.delete(sigmag_cov, np.s_[16:20], axis=1)
    rperp = data['r_data'][:-4]

    # Priors
    log_alpha = -0.32085983 - 0.1
    log_beta = 0.16309539
    log_gamma = 0.64815634
    log_r_s = 0.85387196 - 0.1
    log_r_t = 0.08325509
    log_rho_0 = -0.8865869 - 0.5
    log_rho_s = -0.19838697 - 0.3
    se = 1.3290722
    ln_mis = -1.146114384
    f_mis = 0.15857366
    # Chihway: alpha, beta, gamma, r_s, r_t, rho_0, rho_s, se, ln_mis, f_mis
    params = np.array([
        log_alpha, log_beta, log_gamma, log_r_s, log_r_t, log_rho_0, log_rho_s,
        se, ln_mis, f_mis
    ])

    # Minimized Splashback Model of Data
    print('Running Scipy Minimize...')
    print('')
    nll = lambda *args: -1 * lnlikelihood(*args)
    p0 = params.copy()
    bounds = ((None, None), (None, None), (None, None), (np.log10(0.1 / h0),
                                                         np.log10(5.0 / h0)),
              (np.log10(0.1 / h0), np.log10(5.0 / h0)), (None, None),
              (None, None), (-10., 10.), (np.log(0.01), np.log(0.99)), (0.01,
                                                                        0.99))
    data_vec = sigmag.copy()
    invcov = np.linalg.inv(sigmag_cov.copy())
    args = (rperp, z, data_vec, invcov, h0, 1)

    result = op.minimize(nll,
                         p0,
                         args=args,
                         options={'maxiter': 200},
                         bounds=bounds)
    best_params = result.x
    best_lnlike = -result.fun

    model = Sigmag(rperp, z, best_params, h0, 1)
    diff = data_vec - model
    chisq_min = np.dot(diff, np.dot(invcov, diff))

    # Defining the Multinest Function
    def run_multinest(rperp, sigmag, invcov, splashback, outfile):
        def Prior(cube, ndim, nparams):
            # Sigma Values are from Chang 2018 Table 2. Each sigma is half a prior range
            cube[0] = uniform(-0.92, -0.22, cube[0])  # log(alpha)
            cube[1] = uniform(0.28, 1.28, cube[1])  # log(beta)
            cube[2] = uniform(-0.4, 1.5, cube[2])  # log(gamma)
            cube[3] = uniform(-1.02, -0.62, cube[3])  # r_s
            cube[4] = uniform(-1., 19.,
                              cube[4])  # r_t  #17 is probably better than 19
            cube[5] = uniform(-1.4, -0.9, cube[5])  # rho_0
            cube[6] = uniform(0.8, 2.4, cube[6])  # rho_s # 1.6 \pm 0.8
            cube[7] = uniform(1.17, 1.77, cube[7])  # s_e
            cube[8] = uniform(-0.9, 0., cube[8])  # ln(c_mis)
            cube[9] = uniform(0.11, 0.7, cube[9])  # f_mis

        def Loglike(cube, ndim, nparams):
            # Read in parameters
            log_alpha = cube[0]
            log_beta = cube[1]
            log_gamma = cube[2]
            r_s = cube[3]
            r_t = cube[4]
            rho_0 = cube[5]
            rho_s = cube[6]
            se = cube[7]
            ln_mis = cube[8]
            f_mis = cube[9]
            params = [
                log_alpha, log_beta, log_gamma, r_s, r_t, rho_0, rho_s, se,
                ln_mis, f_mis
            ]

            # Calculate likelihood
            sig_m = Sigmag(rperp, z, params, h0, splashback)
            vec = sig_m - sigmag
            likelihood = -0.5 * np.matmul(np.matmul(vec, invcov), vec.T)

            # Calculate prior
            prior = -0.5 * (-1.13 - ln_mis)**2 / 0.22**2 - 0.5 * (
                log_alpha - np.log10(0.19))**2 / 0.4**2 - 0.5 * (
                    log_beta - np.log10(6.0))**2 / 0.4**2 - 0.5 * (
                        log_gamma - np.log10(4.0))**2 / 0.4**2 - 0.5 * (
                            f_mis - 0.22)**2 / 0.11**2
            #prior = 0.

            # Total probability
            tot = likelihood + prior

            return tot

        # Run Multinest
        mult.run(Loglike,
                 Prior,
                 10,
                 outputfiles_basename=outfile,
                 verbose=False)

    # Saving Results
    #os.mkdir('/Users/arielamsellem/Desktop/Research/Multinest/' + out_directory)
    out_filename = out_directory
    out_directory = '/Users/arielamsellem/Desktop/Research/Multinest/' + out_directory + '/'
    out_filename = out_directory + out_filename

    # Run Multinest
    #run_multinest(rperp, sigmag, invcov, 1, out_filename)

    # Save Output to File "log.txt"
    stdoutOrigin = sys.stdout
    sys.stdout = open(out_directory + "log.txt", "w")
    # Read in Multinest Results
    multinest_out = np.genfromtxt(out_filename + 'post_equal_weights.dat')
    samples_txt = multinest_out[:, :-1]
    likelihood_txt = multinest_out[:, -1]

    # Multinest Best Parameters
    analyzer = mult.analyse.Analyzer(10,
                                     outputfiles_basename=(out_filename),
                                     verbose=False)
    bestfit_params_multinest = analyzer.get_best_fit()
    best_params_mult = bestfit_params_multinest['parameters']
    best_loglike_mult = bestfit_params_multinest['log_likelihood']

    model_mult = Sigmag(rperp, z, best_params_mult, h0, 1)
    diff_mult = data_vec - model_mult
    chisq_mult = np.dot(diff_mult, np.dot(invcov, diff_mult))

    print("Best Parameters From Minimization: " + str(best_params))
    print("Loglike From Minimization: " + str(best_lnlike))
    print("Best Parameters From Multinest: " + str(best_params_mult))
    print("Loglike From Multinest: " + str(best_loglike_mult))
    print("Chi-Squared Scipy Minimize: " + str(chisq_min))
    print("Chi-Squared Multinest: " + str(chisq_mult))
    sys.stdout.close()
    sys.stdout = stdoutOrigin

    # Get Rho Values and Error Range
    low, high = profile_range(samples_txt, rperp, z)
    r_rho, r_rhoderiv, rho, drho, rho_i, rho_o = find_rho_drho(best_params)
    r_rho_mult, r_rhoderiv_mult, rho_mult, drho_mult, rho_i_mult, rho_o_mult = find_rho_drho(
        best_params_mult)

    # Plot Results
    print('')
    print('Plotting Results...')
    samples = MCSamples(samples=samples_txt,
                        loglikes=likelihood_txt,
                        names=[
                            'alpha', 'beta', 'gamma', 'rs', 'rt', 'rho0',
                            'rhos', 'se', 'lnmis', 'fmis'
                        ],
                        labels=[
                            '\\alpha', '\\beta', '\\gamma', 'r_s', 'r_t',
                            '\\rho_0', '\\rho_s', 's_e', 'ln(c_{mis})',
                            'f_{mis}'
                        ])

    # Triangle Plot
    sns.set_style("white")
    g = plots.getSubplotPlotter(width_inch=12)
    g.triangle_plot(samples,
                    filled=True,
                    colors=[color],
                    lw=[3],
                    line_args=[{
                        'lw': 2,
                        'color': 'k'
                    }])
    plt.savefig(out_directory + 'Triangle_Multinest.png', dpi=600)

    # Plot Error Region Around \\rho Derivative
    sns.set_style("whitegrid")
    fig = plt.figure(figsize=(20, 10))
    plt.suptitle(label, fontsize=23, fontweight=900)
    plt.subplot(121)
    plt.semilogx(r_rho,
                 rho,
                 color=color,
                 label='Splashback Fit (Scipy)',
                 linewidth=1)
    plt.semilogx(r_rho,
                 rho_i,
                 color=color,
                 label='Scipy Inner Profile',
                 linewidth=1,
                 linestyle='--')
    plt.semilogx(r_rho,
                 rho_o,
                 color=color,
                 label='Scipy Outer Profile',
                 linewidth=1,
                 linestyle='-.')
    plt.semilogx(r_rho_mult,
                 rho_mult,
                 color="fuchsia",
                 label='Splashback Fit (Multinest)',
                 linewidth=1)
    plt.semilogx(r_rho_mult,
                 rho_i_mult,
                 color="fuchsia",
                 label='Multinest Inner Profile',
                 linewidth=1,
                 linestyle='--')
    plt.semilogx(r_rho_mult,
                 rho_o_mult,
                 color="fuchsia",
                 label='Multinest Outer Profile',
                 linewidth=1,
                 linestyle='-.')
    plt.xlabel('$R  [Mpc]$', fontsize=15)
    plt.ylabel('$\\rho(R)$', fontsize=15)
    plt.xscale('log')
    plt.yscale('log')
    plt.ylim(bottom=10**-4)
    plt.legend(fontsize=18, loc='lower left')
    plt.subplot(122)
    plt.semilogx(r_rhoderiv,
                 drho,
                 color=color,
                 label='Splashback Fit (Scipy)',
                 linewidth=1)
    plt.semilogx(r_rhoderiv_mult,
                 drho_mult,
                 color=color,
                 label='Splashback Fit (Multinest)',
                 linestyle='--',
                 linewidth=1)
    plt.fill_between(r_rhoderiv, low, high, color=color, alpha=0.25)
    plt.xlim(0.1, 10.)
    plt.xlabel('$R  [Mpc]$', fontsize=15)
    plt.ylabel('$\\frac{dlog(\\rho(R))}{dlog(R)}$', fontsize=23)
    fig.tight_layout(rect=[0, 0.03, 1, 0.95])
    plt.savefig(out_directory + 'rho_Multinest.png', dpi=600)

    # Plot Sigmag Bestfit from Multinest
    plt.figure(figsize=(7, 5))
    plt.errorbar(rperp,
                 sigmag,
                 yerr=sigmag_sig,
                 capsize=4,
                 label=label,
                 color=color,
                 ls='none')
    plt.semilogx(rperp,
                 Sigmag(rperp, z, best_params, h0, 1),
                 label='Splashback Fit (Scipy)',
                 color=color)
    plt.semilogx(rperp,
                 Sigmag(rperp, z, best_params_mult, h0, 1),
                 label='Splashback Fit (Multinest)',
                 linestyle='--',
                 color=color)
    plt.xlabel('$R [Mpc]$', fontsize=15)
    plt.ylabel('$\Sigma_{g} [(1/Mpc)^2]$', fontsize=15)
    plt.xscale('log')
    plt.yscale('log')
    plt.legend(fontsize=14, loc='lower left')
    plt.savefig(out_directory + 'Sigmag.png', dpi=600)
Exemple #23
0
number_of_parameters = number_model_parameters + number_hyperparameters

samples = loadMCSamples('../output/mcmc_final_output_HP',settings={'ignore_rows':2}) 

g = plots.getSinglePlotter()

g.settings.rcSizes(axes_fontsize = 4,lab_fontsize = 7)

g.plot_2d(samples,'A','bw')

#g.triangle_plot(samples,filled=True)

g.export('2D_plot_HP_as_MCMC.pdf')

k = plots.getSubplotPlotter(width_inch=10)

k.settings.rcSizes(axes_fontsize = 7,lab_fontsize = 7)

k.plots_1d(samples,['alpha_01','alpha_02','alpha_03','alpha_04','alpha_05','alpha_06','alpha_07','alpha_08','alpha_09','alpha_10','alpha_11','alpha_12','alpha_13','alpha_14','alpha_15','alpha_16','alpha_17','alpha_18','alpha_19','alpha_20','alpha_21','alpha_22','alpha_23','alpha_24','alpha_25'],nx=5)

k.export('1D_plot_HP_as_MCMC_1-25.pdf')

k.plots_1d(samples,['alpha_26','alpha_27','alpha_28','alpha_29','alpha_30','alpha_31','alpha_32','alpha_33','alpha_34','alpha_35','alpha_36','alpha_37','alpha_38','alpha_39','alpha_40','alpha_41','alpha_42','alpha_43','alpha_44','alpha_45','alpha_46','alpha_47','alpha_48','alpha_49','alpha_50'],nx=5)

k.export('1D_plot_HP_as_MCMC_25-50.pdf')

k.plots_1d(samples,['alpha_51','alpha_52','alpha_53'],nx=2)

k.export('1D_plot_HP_as_MCMC_51-53.pdf')
Exemple #24
0
print(" \\hline")
print(" $\\chi^2/\\nu$ & 88.54/88 & 87.67/88 & 88.56/88 & 78.61/88 \\\\ ")
margs = [marg_hsc, marg_test1, marg_test2, marg_test3]
print_bounds(margs)
print(" \\hline")
print(" \\hline")
print("\\end{tabular}")
print("\\end{center}")
print("\\label{tab:chi2_tests}")
print("\\caption{Table}")
print("\\end{table}")



# Triangle plot
g = plots.getSubplotPlotter()
g.settings.legend_fontsize = 20
g.settings.scaling_factor = 0.1
g.triangle_plot([marg_test3,marg_test1,marg_test2,marg_hsc],params=HOD_pars,legend_labels=[lab_test3,lab_test1,lab_test2,lab_marg],filled=True)
lw = 1.5
for i,key_i in enumerate(hod_dic.keys()):
    for j,key_j in enumerate(hod_dic.keys()):
        if j == i:
            print("i,j = ",i,j)
            ax = g.get_axes_for_params(key_i)
            print(ax)
            g.add_x_marker(marker=hod_dic[key_i],color='blue',ax=ax,lw=lw,ls='--')
            g.add_x_marker(marker=new_hod_dic[key_i],color='gray',ax=ax,lw=lw,ls='--')
            ax = None
        else:#if j > i:
            print("i,j = ",i,j)
def testProgram():
    import time
    import argparse

    parser = argparse.ArgumentParser(description='make getdist test plots from test Gaussian mixture distributions')
    parser.add_argument('--sims', type=int, default=100, help='Number of simulations per case')
    parser.add_argument('--nsamp', type=int, default=10000, help='Number of (independent) samples per simulation')
    parser.add_argument('--plots', nargs='*', default=['dists_1D', 'dists_2D'], help='names of plots to make')
    parser.add_argument('--mbc', type=int, default=1, help='mult_bias_correction_order')
    parser.add_argument('--bco', type=int, default=1, help='boundary_correction_order')

    args = parser.parse_args()

    test1D = Test1DDistributions()
    test2D = Test2DDistributions()
    test_settings = {'mult_bias_correction_order':args.mbc, 'boundary_correction_order':args.bco, 'smooth_scale_1D':-1, 'smooth_scale_2D':-1}
    g = plots.getSubplotPlotter(subplot_size=2)

    if 'ISE_1D' in args.plots:
        compare_method(test1D.distributions(), nx=3,
                      test_settings=[ {'mult_bias_correction_order':1, 'boundary_correction_order':1},
                         {'mult_bias_correction_order':2, 'boundary_correction_order':1},
                         {'mult_bias_correction_order':0, 'boundary_correction_order':0},
                         {'mult_bias_correction_order':0, 'boundary_correction_order':1},
                         {'mult_bias_correction_order':0, 'boundary_correction_order':2},
                         ], colors=['k', 'b', 'r', 'm', 'c', 'g'], linestyles=['-', '-', ':', '-.', '--'],
                      fname='compare_method_1d_N%s.pdf' % args.nsamp,
                       sims=args.sims, nsamp=args.nsamp
                       )

    if 'ISE_2D' in args.plots:
        compare_method(test2D.distributions(), nx=4,
                      test_settings=[ {'mult_bias_correction_order':1, 'boundary_correction_order':1},
                         {'mult_bias_correction_order':2, 'boundary_correction_order':1},
                         {'mult_bias_correction_order':0, 'boundary_correction_order':0},
                         {'mult_bias_correction_order':0, 'boundary_correction_order':1},
                         ], colors=['k', 'b', 'r', 'm', 'c', 'g'], linestyles=['-', '-', ':', '-.', '--'],
                      fname='compare_method_2d_N%s.pdf' % args.nsamp,
                       sims=args.sims, nsamp=args.nsamp
                       )

    if args.plots is None or 'dists_1D' in args.plots:
        g.newPlot()
        start = time.time()
        compare1D(g, test1D.distributions(), nsamp=args.nsamp, settings=test_settings)
        print('1D timing:', time.time() - start)
        join_subplots(g.subplots)
        plt.savefig('test_dists_1D_mbc%s_bco%s_N%s.pdf' % (args.mbc, args.bco, args.nsamp), bbox_inches='tight')

    if args.plots is None or 'dists_2D' in args.plots:
        g.newPlot()
        start = time.time()
        compare2D(g, test2D.distributions(), nsamp=args.nsamp, settings=test_settings)
        print('2D timing:', time.time() - start)
        join_subplots(g.subplots)
        plt.savefig('test_dists_2D_mbc%s_bco%s_N%s.pdf' % (args.mbc, args.bco, args.nsamp), bbox_inches='tight')

    plt.show()
    if False:
        print('testing 1D gaussian MISE...')
        scales, MISEs = get1DMises(test1D.gauss)
        for scale, MISE in zip(scales, MISEs):
            print(scale, MISE, np.sqrt(MISE))
        print('testing 2D gaussian MISE...')
        scales, MISEs = get2DMises(test2D.gauss)
        for scale, MISE in zip(scales, MISEs):
            print(scale, MISE, np.sqrt(MISE))
Exemple #26
0
    def plot_triangle(self, ID, 
            params_to_plot=None, 
            suffix=None, 
            replot=False, 
            M_star=False, 
            show=False):
        """ 
        Draw a "triangle plot" with the 1D and 2D posterior probability

        Parameters
        ----------
        ID : str or int
            Object ID


        M_star : bool, optional
            If set, the routine will plot the mass currenly locked into stars
            instead of the mass of star formed (i.e., the plotted mass will
            accout for the return fraction)

        """ 
        # NB: you changed the getdist/plot.py _set_locator function
        # replacing line 1172-1176
        #if x and (abs(xmax - xmin) < 0.01 or max(abs(xmin), abs(xmax)) >= 1000):
        #    axis.set_major_locator(plt.MaxNLocator(self.settings.subplot_size_inch / 2 + 3, prune=prune))
        #else:
        #    axis.set_major_locator(plt.MaxNLocator(self.settings.subplot_size_inch / 2 + 4, prune=prune))
        # with
        #if x and (abs(xmax - xmin) < 0.01 or max(abs(xmin), abs(xmax)) >= 1000):
        #    axis.set_major_locator(plt.MaxNLocator(self.settings.subplot_size_inch / 2 + 2, prune=prune))
        #else:
        #    axis.set_major_locator(plt.MaxNLocator(self.settings.subplot_size_inch / 2 + 3, prune=prune))
        # to have a fewer number of tick marks, and hence less crowded triangle plots

        # for "cosmetics" reasons you also changed the getdist/plot.py setWithSubplotSize function
        # replacing line 166-167
        # self.lab_fontsize = 7 + 2 * self.subplot_size_inch
        # self.axes_fontsize = 4 + 2 * self.subplot_size_inch
        # with
        # self.lab_fontsize = 7 + 4 * self.subplot_size_inch
        # self.axes_fontsize = 4 + 4 * self.subplot_size_inch
        # to have larger, hence more readable, label and axes font sizes
    
        # Name of the output plot
        if suffix is None:
            plot_name = str(ID)+'_BEAGLE_triangle.pdf'
        else:
            plot_name = str(ID)+'_BEAGLE_triangle_' + suffix + '.pdf'

        # Check if the plot already exists
        if plot_exists(plot_name) and not replot and not show:
            logging.warning('The plot "' + plot_name + '" already exists. \n Exiting the function.')
            return

        fits_file = os.path.join(BeagleDirectories.results_dir,
                str(ID) + '_' + BeagleDirectories.suffix + '.fits.gz')

        hdulist = fits.open(fits_file)

        param_values = OrderedDict()
        for key, value in six.iteritems(self.adjust_params):
            extName = "POSTERIOR PDF"
            if "extName" in value:
                extName = value["extName"]

            colName = key
            if "colName" in value:
                colName = value["colName"]

            param_values[key] = hdulist[extName].data[colName]

        probability = hdulist['posterior pdf'].data['probability']

        n_rows = probability.size

       # ParamsToPlot = ['mass', 'redshift', 'tauV_eff', 'metallicity', 'specific_sfr', 'tau']

        # By default you plot all parameters
        if params_to_plot is None:
            _params_to_plot = list()
            for key, value in six.iteritems(self.adjust_params):
                _params_to_plot.append(key)
        else: 
            _params_to_plot = params_to_plot

        # Here you check whether you want to plot the mass currently locked
        # into stars or not (i.e. accounting for the return fraction as well)
        if M_star and 'mass' in _params_to_plot:
            param_values['mass'][:] = np.log10(hdulist['galaxy properties'].data['M_star'][:])

        nParamsToPlot = len(_params_to_plot)

        names = list()
        labels = list()
        ranges = dict()
        samps = np.zeros((n_rows, nParamsToPlot))
        keys = list()

        j = 0
        for key, par in six.iteritems(self.adjust_params):
            keys.append(key)
            for par_name in _params_to_plot:
                if key == par_name:
                    names.append(key)
                    label = par['label'].replace("$","")
                    labels.append(label)

                    samps[:,j] = param_values[key]
                    ranges.update({key:par['range']})
                    if 'log' in par:
                        if par["log"]:
                            samps[:,j] = np.log10(param_values[key])
                            ranges.update({key:np.log10(par['range'])})
                    j += 1
                    break

        settings = {
                    "contours":[0.68, 0.95, 0.99], 
                    "range_ND_contour":1, 
                    "range_confidence":0.001,
                    "fine_bins":200,
                    "fine_bins_2d":80,
                    "smooth_scale_1D":0.3,
                    "smooth_scale_2D":0.5,
                    "tight_gap_fraction":0.15
                    }

        samples = MCSamples(samples=samps, names=names, ranges=ranges, \
                weights=probability, labels=labels, settings=settings )

        g = plots.getSubplotPlotter()
        g.settings.num_plot_contours = 3
        g.settings.prob_y_ticks = True

        # Change the size of the labels 
        if self.triangle_font_size is None:
            g.settings.lab_fontsize = 7 + 4 * g.settings.subplot_size_inch
            g.settings.axes_fontsize = 4 + 4 * g.settings.subplot_size_inch
        else:
            g.settings.lab_fontsize = self.triangle_font_size
            g.settings.axes_fontsize = self.triangle_font_size

        line_args = {"lw":2, "color":colorConverter.to_rgb("#006FED") } 

        g.triangle_plot(samples, filled=True, line_args=line_args)

        g.fig.subplots_adjust(wspace=0.1, hspace=0.1)

        prune  = 'both'

        #for i, ax in enumerate([g.subplots[i,i] for i in range(nParamsToPlot)]):
        #    ax.set_autoscalex_on(True)

        for i in range(len(names)):
            for i2 in range(i, len(names)):
                _ax = g._subplot(i, i2)
                _ax.xaxis.set_major_locator(plt.MaxNLocator(3, prune=prune))
                _ax.yaxis.set_major_locator(plt.MaxNLocator(3, prune=prune))

        # Add tick labels at top of diagonal panels
        for i, ax in enumerate([g.subplots[i,i] for i in range(nParamsToPlot)]):
            par_name = keys[i]

            if i < nParamsToPlot-1: 
                ax.tick_params(which='both', labelbottom=False, 
                        top=True, labeltop=True, left=False, labelleft=False)
            else:
                ax.tick_params(which='both', labelbottom=True, 
                        top=True, labeltop=False, left=False, labelleft=False)

            # Add shaded region showing 1D 68% credible interval
            y0, y1 = ax.get_ylim()
            lev = samples.get1DDensity(par_name).getLimits(settings['contours'][0])

            ax.add_patch(
                    Rectangle((lev[0], y0), 
                    lev[1]-lev[0], 
                    y1-y0, 
                    facecolor="grey", 
                    alpha=0.5)
                    )

            # Indicate the value of the "true" parameter
            if self.mock_catalogue is not None:
                name = names[i]
                value = self.mock_catalogue.get_param_values(ID, (name,))
                if "log" in self.adjust_params[name]:
                    if self.adjust_params[name]["log"]:
                        value = np.log10(value)

                ax.plot(value,
                        y0+(y1-y0)*0.05,
                        marker="D",
                        ms=8,
                        color="green") 

        if self.single_solutions is not None:
            row =  self.single_solutions['row'][self.single_solutions['ID']==ID]

            for i in range(nParamsToPlot):
                parX = keys[i]
                valueX = param_values[parX][row]

                if "log" in self.adjust_params[parX]:
                    if self.adjust_params[parX]["log"]:
                        valueX = np.log10(valueX)

                for j in range(nParamsToPlot):
                    ax = g.subplots[i,j]
                    if ax is None:
                        continue

                    if i == j:

                        ax.plot(valueX,
                                y0+(y1-y0)*0.05,
                                marker="*",
                                ms=12,
                                color="darkorange") 
                    else:

                        parY = keys[j]
                        valueY = param_values[parY][row]

                        if "log" in self.adjust_params[parY]:
                            if self.adjust_params[parY]["log"]:
                                valueY = np.log10(valueY)

                        ax.plot(valueY,
                                valueX,
                                marker="*",
                                ms=12,
                                color="darkorange") 

        if show:
            plt.show()
        else:
            # Now save the plot
            name = prepare_plot_saving(plot_name)
            g.export(name)

        plt.close()
        hdulist.close()
Exemple #27
0
    S = Simple_plots(dir_name, roots, labels)
    #S.Show_limits(params_1D)
    #S.Covariance(params_1D)
    S.Plots1D(params_1D)
    #S.Plots2D(params_2D)
    #S.triangle(params_1D)

elif plotter == 'corner':
    S = Simple_plots(dir_name, roots)
    S.cornerPlotter(params_1D)

elif plotter == 'getdist':
    sys.path = ['getdist', 'corner'] + sys.path
    from getdist import plots
    g = plots.getSubplotPlotter(chain_dir=dir_name,
                                width_inch=10,
                                analysis_settings={'ignore_rows': 0.2})
    #g.plots_1d(roots, params=params_1D)
    #g.plots_2d(roots, param_pairs=params_2D, nx=2, filled=True)
    g.triangle_plot(roots, params_1D, filled=True)  #, plot_3d_with_param='h')
    g.add_legend(labels, legend_loc='best')
    g.export('Plot_getdist.pdf')
    plt.show()

elif plotter == 'fgivenx':
    S = Simple_plots(dir_name, roots[0])

    z = np.linspace(0, 4, 100)

    def func(z, theta1):
        Omega_m, h = theta1
def mcmcSkewer(bundleObj,
               logdef=3,
               binned=False,
               niter=2500,
               do_mcmc=True,
               return_sampler=False,
               evalgrid=True,
               in_axes=None,
               viz=False,
               VERBOSITY=False,
               seed=None,
               truths=[0.002, 3.8]):
    """
    Script to fit simple flux model on each restframe wavelength skewer

    Parameters:
    -----------
        bundleObj : A list of [z, f, ivar] with the skewer_index
        logdef : Which model to use
        niter : The number of iterations to run the mcmc (40% for burn-in)
        do_mcmc : Flag whether to perform mcmc
        plt_pts : Plot the data along with best fit from scipy and mcmc
        return_sampler : Whether to return the raw sampler  without flatchaining
        triangle : Display triangle plot of the parameters
        evalgrid : Whether to compute loglikelihood on a specified grid
        in_axes : axes over which to draw the plots
        xx_viz : draw marginalized contour in modifed space
        VERBOSITY : print extra information
        seed : how to seed the random state
        truths : used with logdef=4, best-fit values of tau0 and gamma

    Returns:
        mcmc_chains if return_sampler, else None
    """

    z, f, ivar = bundleObj[0].T

    ind = (ivar > 0) & (np.isfinite(f))
    z, f, sigma = z[ind], f[ind], 1.0 / np.sqrt(ivar[ind])
    # -------------------------------------------------------------------------
    # continuum flux estimate given a value of (tau0, gamma)
    if logdef == 4:
        if VERBOSITY:
            print('Continuum estimates using optical depth parameters:',
                  truths)
        chisq4 = lambda *args: -outer(*truths)(*args)

        opt_res = minimize(chisq4,
                           1.5,
                           args=(z, f, sigma),
                           method='Nelder-Mead')
        return opt_res['x']

    if VERBOSITY:
        print('Carrying analysis for skewer', bundleObj[1])

    if logdef == 1:
        nll, names, labels, guess = chisq1, names1, labels1, guess1
        ndim, kranges, lnlike = 4, kranges1, lnlike1

    elif logdef == 2:
        nll, names, labels, guess = chisq2, names2, labels2, guess2
        ndim, kranges, lnlike = 5, kranges2, lnlike2

    elif logdef == 3:
        nll, names, labels, guess = chisq3, names3, labels3, guess3
        ndim, kranges, lnlike = 3, kranges3, lnlike3

    # Try to fit with scipy optimize routine
    opt_res = minimize(nll, guess, args=(z, f, sigma), method='Nelder-Mead')
    print('Scipy optimize results:')
    print('Success =', opt_res['success'], 'params =', opt_res['x'], '\n')

    if viz:
        if in_axes is None:
            fig, in_axes = plt.subplots(1)
        in_axes.errorbar(z, f, sigma, fmt='o', color='gray', alpha=0.2)
        in_axes.plot(
            zline, opt_res['x'][0] * np.exp(-np.exp(opt_res['x'][1]) *
                                            (1 + zline)**opt_res['x'][2]))

    if binned:
        mu = binned_statistic(z, f, bins=binx).statistic
        sig = binned_statistic(z, f, bins=binx, statistic=sig_func).statistic

        ixs = sig > 0
        z, f, sigma = centers[ixs], mu[ixs], sig[ixs]

        if viz:
            in_axes.errorbar(z, f, sigma, fmt='o', color='r')

        nll, names, labels, guess = lsq, names3, labels3, guess3
        ndim, kranges, lnlike = 3, kranges3, simpleln

    # --------------------------------------------------------------------------
    if do_mcmc:
        np.random.seed()

        nwalkers = 100
        p0 = [guess + 1e-4 * np.random.randn(ndim) for i in range(nwalkers)]

        # configure the sampler
        sampler = emcee.EnsembleSampler(nwalkers,
                                        ndim,
                                        lnlike,
                                        args=(z, f, sigma))

        # burn-in time - Is this enough?
        p0, __, __ = sampler.run_mcmc(p0, 500)
        sampler.reset()

        # Production step
        sampler.run_mcmc(p0, niter)
        print("Burn-in and production completed \n")

        if return_sampler:
            return sampler.chain
        else:
            # pruning 40 percent of the samples as extra burn-in
            lInd = int(niter * 0.4)
            samps = sampler.chain[:, lInd:, :].reshape((-1, ndim))

            # using percentiles as confidence intervals
            CenVal = np.median(samps, axis=0)

            # print BIC at the best estimate point, BIC = - 2 * ln(L_0) + k ln(n)
            print('CHISQ_R', -2 * lnlike(CenVal, z, f, sigma) / (len(z) - 3))
            print('BIC:',
                  -2 * lnlike(CenVal, z, f, sigma) + ndim * np.log(len(z)))

            # Rotate the points to the other basis and 1D estimates
            # and write them to the file

            # Format : center, top error, bottom error
            tg_est = list(
                map(lambda v: (v[1], v[2] - v[1], v[1] - v[0]),
                    zip(*np.percentile(samps, [16, 50, 84], axis=0))))

            xx = xfm(samps[:, 1:], shift, tilt, direction='up')
            xx_est = list(
                map(lambda v: (v[1], v[2] - v[1], v[1] - v[0]),
                    zip(*np.percentile(xx, [16, 50, 84], axis=0))))

            f_name2 = 'tg_est_' + str(bundleObj[1]) + '.dat'
            np.savetxt(f_name2, tg_est)
            f_name3 = 'xx_est_' + str(bundleObj[1]) + '.dat'
            np.savetxt(f_name3, xx_est)

            if viz:
                in_axes.plot(
                    zline, CenVal[0] * np.exp(-np.exp(CenVal[1]) *
                                              (1 + zline)**CenVal[2]), '-g')

            # instantiate a getdist object
            MC = MCSamples(samples=samps,
                           names=names,
                           labels=labels,
                           ranges=kranges)

            # MODIFY THIS TO BE PRETTIER
            if viz:
                g = plots.getSubplotPlotter()
                g.triangle_plot(MC)

            # Evaluate the pdf on a rotated grid for better estimation
            if evalgrid:
                print('Evaluating on the grid specified \n')
                pdist = MC.get2DDensity('t0', 'gamma')

                # Evalaute density on a grid
                pgrid = np.array([pdist.Prob(*ele) for ele in modPos])
                # Prune to remove negative densities
                pgrid[pgrid < 0] = 1e-50

                # Convert to logLikelihood
                logP = np.log(pgrid)
                logP -= logP.max()
                logP = logP.reshape(x0.shape)

                # Visualize the contour in modified space per skewer
                if viz:
                    fig, ax2 = plt.subplots(1)
                    ax2.contour(x0,
                                x1,
                                cts(logP),
                                levels=[
                                    0.683,
                                    0.955,
                                ],
                                colors='k')
                    ax2.axvline(xx_est[0][0] + xx_est[0][1])
                    ax2.axvline(xx_est[0][0] - xx_est[0][2])
                    ax2.axhline(xx_est[1][0] + xx_est[1][1])
                    ax2.axhline(xx_est[1][0] - xx_est[1][2])
                    ax2.set_xlabel(r'$x_0$')
                    ax2.set_ylabel(r'$x_1$')
                    plt.show()

                # fileName1: the log-probability evaluated in the tilted grid
                f_name1 = 'gridlnlike_' + str(bundleObj[1]) + '.dat'
                np.savetxt(f_name1, logP)
Exemple #29
0
 											 for j in range(n_plot)}, \
 											{'c_{:d}'.format(j): (-1.0, 1.0) \
 											 for j in range(n_plot)})
 					gd_samples = gd.MCSamples(samples=plot_samples, \
											  names=gd_names, \
											  labels=gd_labels, \
											  ranges=gd_limits)
					for j in range(n_plot):
						gd_name = 'i_{:d}'.format(j)
						gd_label = r'\iota\,(\degree)'
						c_j = gw_samples[:, i_plot[j] + n_pars / 2]
						gd_samples.addDerived(np.arccos(c_j) * 180.0 / np.pi, \
											  name=gd_name, label=gd_label)
						gd_samples.setRanges({'i_{:d}'.format(j):(0.0, 180.0)})
					gd_samples.updateBaseStatistics()
					g = gdp.getSubplotPlotter(subplot_size=3)
					g.settings.lw_contour = lw
					g.plots_2d(gd_samples, param_pairs=gd_pairs, \
							   nx=3, filled=True, \
							   colors=[mpc.rgb2hex(cols[2])])
					axes = g.subplots.flatten()
					for j in range(n_plot):
						#axes[j].set_xlim(0.0, 500.0)
						#axes[j].set_ylim(0.0, 180.0)
						g.add_x_marker(d_true[i_plot[j]], ax=axes[j], \
									   lw=lw, color='k')
						g.add_y_marker(np.arccos(cos_i_true[i_plot[j]]) * \
									   180.0 / np.pi, ax=axes[j], lw=lw, \
									   color='k')
					mp.savefig(base + '_dist_inc_posts.pdf', \
							   bbox_inches='tight')
Exemple #30
0
def body_of_sampler_test(info_sampler: SamplersDict,
                         dimension=1,
                         n_modes=1,
                         tmpdir="",
                         packages_path=None,
                         skip_not_installed=False,
                         fixed=False,
                         random_state=None):
    # Info of likelihood and prior
    ranges = np.array([[-1, 1] for _ in range(dimension)])
    if fixed:
        info = fixed_info.copy()
    else:
        info = generate_random_info(n_modes, ranges, random_state=random_state)

    if mpi.is_main_process():
        print("Original mean of the gaussian mode:")
        print(info["likelihood"]["gaussian_mixture"]["means"])
        print("Original covmat of the gaussian mode:")
        print(info["likelihood"]["gaussian_mixture"]["covs"])
    info["sampler"] = info_sampler
    sampler_name = list(info_sampler)[0]
    if random_state is not None:
        info_sampler[sampler_name]["seed"] = random_state.integers(0, 2**31)
    if sampler_name == "mcmc":
        if "covmat" in info_sampler["mcmc"]:
            info["sampler"]["mcmc"]["covmat_params"] = (list(
                info["params"])[:dimension])
    info["debug"] = False
    info["debug_file"] = None
    info["output"] = os.path.join(tmpdir, 'out_chain')
    if packages_path:
        info["packages_path"] = process_packages_path(packages_path)

    updated_info, sampler = install_test_wrapper(skip_not_installed, run, info)
    products = sampler.products()
    products["sample"] = mpi.gather(products["sample"])
    # Done! --> Tests
    if mpi.is_main_process():
        if sampler_name == "mcmc":
            ignore_rows = 0.5
        else:
            ignore_rows = 0
        results = MCSamplesFromCobaya(updated_info,
                                      products["sample"],
                                      ignore_rows=ignore_rows,
                                      name_tag="sample")
        clusters = None
        if "clusters" in products:
            clusters = [
                MCSamplesFromCobaya(updated_info,
                                    products["clusters"][i]["sample"],
                                    name_tag="cluster %d" % (i + 1))
                for i in products["clusters"]
            ]
        # Plots!
        if not is_travis():
            try:
                import getdist.plots as gdplots
                from getdist.gaussian_mixtures import MixtureND
                sampled_params = [
                    p for p, v in info["params"].items() if "prior" not in v
                ]
                mixture = MixtureND(
                    info["likelihood"]["gaussian_mixture"]["means"],
                    info["likelihood"]["gaussian_mixture"]["covs"],
                    names=sampled_params,
                    label="truth")
                g = gdplots.getSubplotPlotter()
                to_plot = [mixture, results]
                if clusters:
                    to_plot += clusters
                g.triangle_plot(to_plot, params=sampled_params)
                g.export("test.png")
            except:
                print("Plotting failed!")
        # 1st test: KL divergence
        if n_modes == 1:
            cov_sample, mean_sample = results.getCov(
                dimension), results.getMeans()
            KL_final = KL_norm(
                m1=info["likelihood"]["gaussian_mixture"]["means"][0],
                S1=info["likelihood"]["gaussian_mixture"]["covs"][0],
                m2=mean_sample[:dimension],
                S2=cov_sample)
            print("Final KL: ", KL_final)
            assert KL_final <= KL_tolerance
        # 2nd test: clusters
        else:
            if "clusters" in products:
                assert len(products["clusters"]) >= n_modes, (
                    "Not all clusters detected!")
                for i, c2 in enumerate(clusters):
                    cov_c2, mean_c2 = c2.getCov(), c2.getMeans()
                    KLs = [
                        KL_norm(m1=info["likelihood"]["gaussian_mixture"]
                                ["means"][i_c1],
                                S1=info["likelihood"]["gaussian_mixture"]
                                ["covs"][i_c1],
                                m2=mean_c2[:dimension],
                                S2=cov_c2[:dimension, :dimension])
                        for i_c1 in range(n_modes)
                    ]
                    extra_tol = 4 * n_modes if n_modes > 1 else 1
                    print("Final KL for cluster %d: %g", i, min(KLs))
                    assert min(KLs) <= KL_tolerance * extra_tol
            else:
                assert 0, "Could not check sample convergence: multimodal but no clusters"
        # 3rd test: Evidence
        if "logZ" in products:
            logZprior = sum(np.log(ranges[:, 1] - ranges[:, 0]))
            assert (products["logZ"] - logZ_nsigmas * products["logZstd"] <
                    -logZprior <
                    products["logZ"] + logZ_nsigmas * products["logZstd"])
Exemple #31
0
from getdist import plots


dir_name = '/Users/josevazquezgonzalez/Desktop/BOSS/BOSS_files/DR12/chains'


roots = ['owcdmdr12','OkwoCDM_PLK+BAO12','OkwoCDM_PLK+DR12']
g = plots.getSubplotPlotter(chain_dir= dir_name, width_inch=16,
                            analysis_settings={'smooth_scale_2D': -1. , 'ignore_rows': 0.2})
g.settings.axes_fontsize = 18
g.settings.lab_fontsize = 20
g.settings.alpha_filled_add =0.9
g.plots_2d(roots, param_pairs=[['omegak','w'], ['omegak','H0'], ['w','H0']], nx=3, filled=True,
           legend_labels=False, line_args={'lw':2,  'ls':'-'})
g.add_legend(['PLANCK + LOWZ+CMASS', 'PLANCK + BAO', 'PLANCK + BAO+FS'],
             colored_text=True,  legend_loc='upper right')
g.export('comp_lowz.pdf')




roots = ['OkwowaCDM_PLK+BAO12','OkwowaCDM_PLK+DR12', 'OkwowaCDM_PLK+DR12+JLA']
g = plots.getSubplotPlotter(chain_dir= dir_name, width_inch=16,
                            analysis_settings={'smooth_scale_2D': -1. , 'ignore_rows': 0.2})
g.settings.axes_fontsize = 18
g.settings.lab_fontsize = 20
g.settings.alpha_filled_add =0.9
g.plots_2d(roots, param_pairs=[['omegak','w'], ['omegak','wa'], ['w','wa']], nx=3, filled=True,
           legend_labels=False)
g.add_legend(['PLANCK + DR12 LOWZ+CMASS', 'PLANCK + DR12 BAO', 'PLANCK + DR12 BAO+FS'],
Exemple #32
0
    prefix.sort()
    chain = loadMCSamples(os.path.join(path, prefix[0][:-6]),
                          no_cache=False,
                          settings={
                              'ignore_rows': args.ignore_rows,
                              'smooth_scale_1D': args.smooth_scale_1D,
                              'smooth_scale_2D': args.smooth_scale_2D
                          })
    chains.append(chain)

# Load legends
legends = []
if args.legends and len(args.legends) == len(chains):
    legends = args.legends
else:
    legends = [re.split('/', x.root)[-2] for x in chains]

# Load output file
if args.output:
    output_file = args.output
else:
    output_file = os.path.abspath('_vs_'.join(legends) + '.pdf')

# Do the plot
g = plots.getSubplotPlotter(subplot_size=4)
g.settings.alpha_filled_add = 0.4
g.triangle_plot(chains, filled=True, legend_labels=legends)
# Save the plot
plt.savefig(output_file)
print('Success!! Saved plot at {}'.format(output_file))
Exemple #33
0
def body_of_test(dimension=1,
                 n_modes=1,
                 info_sampler={},
                 tmpdir="",
                 modules=None):
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    # Info of likelihood and prior
    ranges = np.array([[-1, 1] for _ in range(dimension)])
    while True:
        info = info_random_gaussian_mixture(ranges=ranges,
                                            n_modes=n_modes,
                                            prefix="a_",
                                            O_std_min=O_std_min,
                                            O_std_max=O_std_max,
                                            derived=True)
        if n_modes == 1:
            break
        means = info["likelihood"]["gaussian_mixture"]["means"]
        distances = chain(*[[np.linalg.norm(m1 - m2) for m2 in means[i + 1:]]
                            for i, m1 in enumerate(means)])
        if min(distances) >= distance_factor * O_std_max:
            break
    if rank == 0:
        print("Original mean of the gaussian mode:")
        print(info["likelihood"]["gaussian_mixture"]["means"])
        print("Original covmat of the gaussian mode:")
        print(info["likelihood"]["gaussian_mixture"]["covs"])
    info[_sampler] = info_sampler
    if list(info_sampler.keys())[0] == "mcmc":
        if "covmat" in info_sampler["mcmc"]:
            info[_sampler]["mcmc"]["covmat_params"] = (list(
                info["params"].keys())[:dimension])
    info[_debug] = False
    info[_debug_file] = None
    info[_output_prefix] = getattr(tmpdir, "realpath()", lambda: tmpdir)()
    if modules:
        info[_path_install] = process_modules_path(modules)
    # Delay to one chain to check that MPI communication of the sampler is non-blocking
    #    if rank == 1:
    #        info["likelihood"]["gaussian_mixture"]["delay"] = 0.1
    updated_info, products = run(info)
    # Done! --> Tests
    if rank == 0:
        if list(info_sampler.keys())[0] == "mcmc":
            ignore_rows = 0.5
        else:
            ignore_rows = 0
        results = loadCobayaSamples(updated_info,
                                    products["sample"],
                                    ignore_rows=ignore_rows,
                                    name_tag="sample")
        clusters = None
        if "clusters" in products:
            clusters = [
                loadCobayaSamples(updated_info,
                                  products["clusters"][i]["sample"],
                                  name_tag="cluster %d" % (i + 1))
                for i in products["clusters"]
            ]
        # Plots!
        try:
            import getdist.plots as gdplots
            from getdist.gaussian_mixtures import MixtureND
            mixture = MixtureND(
                info[_likelihood]["gaussian_mixture"]["means"],
                info[_likelihood]["gaussian_mixture"]["covs"],
                names=[p for p in info[_params] if "deriv" not in p],
                label="truth")
            g = gdplots.getSubplotPlotter()
            to_plot = [mixture, results]
            if clusters:
                to_plot = to_plot + clusters
            g.triangle_plot(to_plot, )
            g.export("test.png")
        except:
            print("Plotting failed!")
        # 1st test: KL divergence
        if n_modes == 1:
            cov_sample, mean_sample = results.getCov(), results.getMeans()
            KL_final = KL_norm(
                m1=info[_likelihood]["gaussian_mixture"]["means"][0],
                S1=info[_likelihood]["gaussian_mixture"]["covs"][0],
                m2=mean_sample[:dimension],
                S2=cov_sample[:dimension, :dimension])
            print("Final KL: ", KL_final)
            assert KL_final <= KL_tolerance
        # 2nd test: clusters
        else:
            if "clusters" in products:
                assert len(products["clusters"].keys()) >= n_modes, (
                    "Not all clusters detected!")
                for c2 in clusters:
                    cov_c2, mean_c2 = c2.getCov(), c2.getMeans()
                    KLs = [
                        KL_norm(m1=info[_likelihood]["gaussian_mixture"]
                                ["means"][i_c1],
                                S1=info[_likelihood]["gaussian_mixture"]
                                ["covs"][i_c1],
                                m2=mean_c2[:dimension],
                                S2=cov_c2[:dimension, :dimension])
                        for i_c1 in range(n_modes)
                    ]
                    extra_tol = 4 * n_modes if n_modes > 1 else 1
                    assert min(KLs) <= KL_tolerance * extra_tol
            else:
                assert 0, "Could not check sample convergence: multimodal but no clusters"
        # 3rd test: Evidence
        if "logZ" in products:
            logZprior = sum(np.log(ranges[:, 1] - ranges[:, 0]))
            assert (products["logZ"] - logZ_nsigmas * products["logZstd"] <
                    -logZprior <
                    products["logZ"] + logZ_nsigmas * products["logZstd"])
def run_test_program():
    import time
    import argparse

    parser = argparse.ArgumentParser(description='make getdist test plots from test Gaussian mixture distributions')
    parser.add_argument('--sims', type=int, default=100, help='Number of simulations per case')
    parser.add_argument('--nsamp', type=int, default=10000, help='Number of (independent) samples per simulation')
    parser.add_argument('--plots', nargs='*', default=['dists_1D', 'dists_2D'], help='names of plots to make')
    parser.add_argument('--mbc', type=int, default=1, help='mult_bias_correction_order')
    parser.add_argument('--bco', type=int, default=1, help='boundary_correction_order')

    args = parser.parse_args()

    test1D = Test1DDistributions()
    test2D = Test2DDistributions()
    test_settings = {'mult_bias_correction_order': args.mbc, 'boundary_correction_order': args.bco,
                     'smooth_scale_1D': -1, 'smooth_scale_2D': -1}
    g = plots.getSubplotPlotter(subplot_size=2)

    if 'ISE_1D' in args.plots:
        compare_method(test1D.distributions(), nx=3,
                       test_settings=[{'mult_bias_correction_order': 1, 'boundary_correction_order': 1},
                                      {'mult_bias_correction_order': 2, 'boundary_correction_order': 1},
                                      {'mult_bias_correction_order': 0, 'boundary_correction_order': 0},
                                      {'mult_bias_correction_order': 0, 'boundary_correction_order': 1},
                                      {'mult_bias_correction_order': 0, 'boundary_correction_order': 2},
                                      ], colors=['k', 'b', 'r', 'm', 'c', 'g'], linestyles=['-', '-', ':', '-.', '--'],
                       fname='compare_method_1d_N%s.pdf' % args.nsamp,
                       sims=args.sims, nsamp=args.nsamp
                       )

    if 'ISE_2D' in args.plots:
        compare_method(test2D.distributions(), nx=4,
                       test_settings=[{'mult_bias_correction_order': 1, 'boundary_correction_order': 1},
                                      {'mult_bias_correction_order': 2, 'boundary_correction_order': 1},
                                      {'mult_bias_correction_order': 0, 'boundary_correction_order': 0},
                                      {'mult_bias_correction_order': 0, 'boundary_correction_order': 1},
                                      ], colors=['k', 'b', 'r', 'm', 'c', 'g'], linestyles=['-', '-', ':', '-.', '--'],
                       fname='compare_method_2d_N%s.pdf' % args.nsamp,
                       sims=args.sims, nsamp=args.nsamp
                       )

    if args.plots is None or 'dists_1D' in args.plots:
        g.newPlot()
        start = time.time()
        compare1D(g, test1D.distributions(), nsamp=args.nsamp, settings=test_settings)
        print('1D timing:', time.time() - start)
        join_subplots(g.subplots)
        plt.savefig('test_dists_1D_mbc%s_bco%s_N%s.pdf' % (args.mbc, args.bco, args.nsamp), bbox_inches='tight')

    if args.plots is None or 'dists_2D' in args.plots:
        g.newPlot()
        start = time.time()
        compare2D(g, test2D.distributions(), nsamp=args.nsamp, settings=test_settings)
        print('2D timing:', time.time() - start)
        join_subplots(g.subplots)
        plt.savefig('test_dists_2D_mbc%s_bco%s_N%s.pdf' % (args.mbc, args.bco, args.nsamp), bbox_inches='tight')

    plt.show()
    if False:
        print('testing 1D gaussian MISE...')
        scales, MISEs = get1DMises(test1D.gauss)
        for scale, MISE in zip(scales, MISEs):
            print(scale, MISE, np.sqrt(MISE))
        print('testing 2D gaussian MISE...')
        scales, MISEs = get2DMises(test2D.gauss)
        for scale, MISE in zip(scales, MISEs):
            print(scale, MISE, np.sqrt(MISE))
Exemple #35
0
# Export the results to GetDist
from getdist.mcsamples import MCSamplesFromCobaya

gd_sample = MCSamplesFromCobaya(updated_info, products["sample"])
# Analyze and plot
mean = gd_sample.getMeans()[:2]
covmat = gd_sample.getCovMat().matrix[:2, :2]
print("Mean:")
print(mean)
print("Covariance matrix:")
print(covmat)
# %matplotlib inline  # uncomment if running from the Jupyter notebook
import getdist.plots as gdplt

gdplot = gdplt.getSubplotPlotter()
gdplot.triangle_plot(gd_sample, ["a", "b"], filled=True)