Esempio n. 1
0
def plot_dist_corner_getdist(samples,
                             labels=None,
                             names=None,
                             filled=True,
                             sample_labels=None):
    if isinstance(samples, (list, tuple)):
        if sample_labels is None:
            sample_labels = [
                'samples {0:d}'.format(i) for i in range(len(samples))
            ]
        samps = [
            MCSamples(samples=samples0,
                      names=names,
                      labels=labels,
                      label=str(slabels0))
            for samples0, slabels0 in zip(samples, sample_labels)
        ]
        g = plots.get_subplot_plotter()
        g.triangle_plot(samps, filled=filled)
    else:
        samps = MCSamples(samples=samples, names=names, labels=labels)
        g = plots.get_subplot_plotter()
        g.triangle_plot(samps, filled=filled)
    plt.show()
    return None
Esempio n. 2
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 
Esempio n. 3
0
    def __init__(self, file_name, nwalkers, nsamples, skip_steps=0):
        self.file_name = file_name
        self.samples = np.loadtxt(dir +
                                  'chains/5/{}.txt'.format(self.file_name))
        self.cosmo_name = 'FlatIVCDM_binned'
        self.nwalkers = nwalkers
        self.nsamples = nsamples
        self.skip_steps = skip_steps

        # bin specifications
        self.Nq = 20
        self.zmax = 1.5
        self.xi0_lims = (0.2, 2.0)
        self.Nscale = 4
        self.amin = 1. / (1. + self.zmax)

        self.npars = self.Nq
        cos = cosmo(Nq=self.Nq)
        M = cos.model[self.cosmo_name]

        self.names_q = ['q{}'.format(i + 1) for i in range(self.Nq)]
        self.labels_q = [r'q_{{{}}}'.format(i + 1) for i in range(self.Nq)]

        samples_skipped = samples_skip(self.samples, self.nwalkers,
                                       self.nsamples, self.npars,
                                       self.skip_steps)
        self.MCsamps = MCSamples(samples=samples_skipped,
                                 names=self.names_q,
                                 labels=self.labels_q)
Esempio n. 4
0
def newGraph_Confidence(result):
    H_0 = [ii[0] for ii in result]
    omega_m0 = [ii[1] for ii in result]
    omega_q0 = [ii[2] for ii in result]
    alpha = [ii[3] for ii in result]
    alpha_x = [ii[4] for ii in result]
    m = [ii[5] for ii in result]

    names = [
        "H_0", "\Omega_{m_0}", "\Omega_{Q_0}", r"\tilde{\alpha}",
        r"\tilde{\alpha}_x", "m"
    ]
    #names = ["H_0"]
    labels = names

    newShape = [H_0, omega_m0, omega_q0, alpha, alpha_x, m]
    #newShape = [H_0]
    values = np.array(newShape).T
    #print(values.shape)
    s1 = MCSamples(samples=values, names=names, labels=labels)
    g = plots.get_subplot_plotter()
    s1.updateSettings({'contours': [0.68, 0.95, 0.99]})
    g.settings.colormap = "binary"
    g.settings.num_plot_contours = 3
    g.triangle_plot([s1], shaded=True, title_limit=1)

    plt.savefig("result.pdf")
Esempio n. 5
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
Esempio n. 6
0
def like1d(dat,weights=None,
           nbins=30,ranges=None,maxed=False,
           ax=None,smooth=False,
           kde=True,
           zero_endpoints=False,
           filled=False,
           **kw):
    
    from matplotlib.pyplot import gca
    if ax is None: ax = gca()
    
    if kde:
        try:
            from getdist import MCSamples
        except ImportError as e:
            raise Exception("Plotting with kde, kde1d, or kde2d set to True requires package 'getdist'. Install this package or set to False.") from e
        
        if ranges:
            i = bitwise_and(dat>(ranges[0] or -Inf), dat<(ranges[1] or Inf))
            dat = dat[i]
            weights = weights[i]
            
        d = MCSamples(samples=dat, weights=weights, names=['x'], ranges={'x':ranges or (None,None)}, settings={'smooth_scale_1D':(smooth or -1)}).get1DDensity(0)
        d.normalize('max' if maxed else 'integral')
        xem, H = d.x, d.P * (maxed or 1)
        
    else:
    
        from matplotlib.mlab import movavg
        H, xe = histogram(dat,bins=nbins,weights=weights,normed=True,range=ranges)
        xem=movavg(xe,2)
        
        if smooth:
            from scipy.interpolate import PchipInterpolator
            itp = PchipInterpolator(xem,H)
            xem = linspace(xem.min(),xem.max(),100)
            H = itp(xem) 
        
        if maxed: H = H/max(H) * (maxed or 1)
        
    if zero_endpoints:
        xem = hstack([[xem[0]],xem,[xem[-1]]])
        H = hstack([[0],H,[0]])
    
    if filled:
        ax.fill_between(xem,H,alpha=(0.5 if filled is True else filled),**kw)
        kw.pop('label')
    ax.plot(xem,H,**kw)
Esempio n. 7
0
 def __init__(self, Chains, ignore_rows=0.1):
     self.root = list(np.asarray(Chains)[:, 0])
     self.lengend = list(np.asarray(Chains)[:, 1])
     self.aic_g = True
     self.Samp = []
     self._n = len(Chains)
     self.minkaf = np.zeros(self._n)
     self.data_num = np.zeros(self._n)
     #        self.theta_fit=np.zeros(self._n)
     #        self.theta_fact=np.zeros(self._n)
     for i in range(self._n):
         savefile_name = './chains/' + self.root[i] + '.npy'
         self.samples, self.theta_name, self.theta_fit, self.theta_fact, self.minkaf[
             i], self.data_num[i], ranges = np.load(savefile_name,
                                                    allow_pickle=True)
         self.label_name = [
             x.replace('H_0', 'H_0 ~[\mathrm{km~s^{-1}~Mpc^{-1}}]')
             for x in self.theta_name
         ]
         self.Samp.append(
             MCSamples(samples=self.samples,
                       names=self.theta_name,
                       labels=self.label_name,
                       ranges=ranges,
                       settings={'ignore_rows': ignore_rows}))
     self.param_names = []
     for na in self.Samp[0].getParamNames().names:
         self.param_names.append(na.name)
Esempio n. 8
0
def plot2D(rdic, par=[1, 2], tex=1):
    snstyle(tex)
    rdic = np.asarray(rdic)
    root = list(rdic[:, 0])
    lengend = list(rdic[:, 1])
    rn = len(root)
    Samp = []
    minkaf = np.zeros(rn)
    data_num = np.zeros(rn)
    for i in range(rn):
        savefile_name = './chains/' + root[i] + '.npy'
        samples, theta_name, theta_fit, theta_fact, minkaf[i], data_num[
            i] = np.load(savefile_name)
        Samp.append(
            MCSamples(samples=samples, names=theta_name, labels=theta_name))
    pnames = Samp[0].getParamNames().names
    rn = len(root)
    g = plots.getSinglePlotter(width_inch=7)
    #samples.updateSettings({'contours': [0.68, 0.95, 0.99]})
    #g.settings.num_plot_contours = 3
    g.settings.lab_fontsize = 18
    g.settings.axes_fontsize = 14
    g.plot_2d(Samp, pnames[par[0] - 1].name, pnames[par[1] - 1].name)
    for i in range(rn):
        sns.kdeplot(Samp[i].samples[:, par[0] - 1],
                    Samp[i].samples[:, par[1] - 1],
                    cmap="Blues",
                    shade=True,
                    shade_lowest=False)
    g.add_legend(lengend, colored_text=True, fontsize=18)
    mpl.pyplot.tight_layout()
Esempio n. 9
0
def get_samples(outfile, par_names, w_rat, n_par, b_iter):
    marg_chains = np.loadtxt(outfile)
    # uncomment for when your chains have been complete
    #marg_chains = marg_chains[w_rat*n_par*b_iter:]
    marg_chains = marg_chains[3 * marg_chains.shape[0] // 4:]
    hsc = MCSamples(samples=marg_chains, names=par_names)
    return hsc
Esempio n. 10
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()
Esempio n. 11
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()
Esempio n. 12
0
 def _samples_to_mcsamples(self, samples):
     # 'samples' is whatever is returned by _sample, in this case it is
     # the entire EnsembleSampler
     return MCSamples(
         samples=samples.get_chain(flat=False),
         names=[a.name for a in self.likelihood.child_active_params],
         labels=[p.latex for p in self.likelihood.child_active_params],
     )
def triangulo(datos, labels = []):
    '''
    DESCRIPTION: Esta funcion realiza una grafica triangular para visualizar los resultados
    IN: datos = lista con puntos de datos (cada uno es una lista con valores para cada parametro)
        labels = nombre de los parametros (default p_i)
    OUT: Grafica triangular con histogramas 1d y 2d de todas la combinaciones
    '''
    g = plots.get_subplot_plotter(subplot_size=2)
    
    #Checamos si se asignó nombre a los parametros
    if labels!=[]:
        samples = MCSamples(samples=np.array(datos), labels = labels, names = labels)
    
    else:
        samples = MCSamples(samples=np.array(datos))
    
    #graficamos
    g.triangle_plot(samples, filled=True, title_limit=1)
Esempio n. 14
0
def cornerplot(sampledf, weights=None, labels=None, markers=None, ranges=None):
    names = sampledf.columns
    samples = MCSamples(samples=sampledf.to_numpy(),
                        names=names,
                        weights=weights,
                        labels=labels,
                        ranges=ranges)
    #samples.updateSettings({'contours': [0.68, 0.95, 0.99]})
    g = plots.get_subplot_plotter()
    return g.triangle_plot(samples, filled=True, markers=markers)
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()
Esempio n. 16
0
 def sampled_to_getdist_mcsamples(self, first=None, last=None):
     """
     Basic interface with getdist -- internal use only!
     (For analysis and plotting use `getdist.mcsamples.loadCobayaSamples`.)
     """
     names = list(self.sampled_params)
     samples = self.data[:self.n()].as_matrix(columns=names)
     return MCSamples(samples=samples[first:last],
                      weights=self.data[:self.n()][_weight].values[first:last],
                      loglikes=self.data[:self.n()][_minuslogpost].values[first:last],
                      names=names)
Esempio n. 17
0
def plot_lfs_kde(cmd_list):
    g = plots.get_single_plotter(width_inch=2 * 3.464, ratio=0.75)
    colors = ['k']
    colors.extend(['r'] * (len(cmd_list) - 1))
    ct = 0
    for cmd in cmd_list:
        cmd_samples = MCSamples(samples=cmd, names=['color', 'mag'])
        if ct > 0:
            g.settings.linewidth = 0.5
        g.plot_1d(cmd_samples, 'mag', colors=[colors[ct]])
        ct += 1
Esempio n. 18
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")
Esempio n. 19
0
def plot_corner(results, outprefix=None, **kwargs):
    """
    Store a simple corner plot in outprefix_corner.pdf, based on samples
    extracted from fit.

    Additional kwargs are passed to MCSamples.
    """
    la = get_flat_posterior(results)
    samples = []
    paramnames = []
    badlist = ['lp__']
    badlist += [
        k for k in la.keys()
        if 'log' in k and k.replace('log', '') in la.keys()
    ]

    for k in sorted(la.keys()):
        print('%20s: %.4f +- %.4f' % (k, la[k].mean(), la[k].std()))
        if k not in badlist and la[k].ndim == 2:
            samples.append(la[k])
            paramnames.append(k)

    if len(samples) == 0:
        arrays = [
            k for k in la.keys()
            if la[k].ndim == 3 and la[k].shape[2] <= 20 and k not in badlist
        ]
        if len(arrays) != 1:
            warnings.warn("no scalar variables found")
            return

        k = arrays[0]
        # flatten across chains and column for each variable
        samples = la[k]
        paramnames = ['%s[%d]' % (k, i + 1) for i in range(la[k].shape[1])]

    samples = numpy.transpose(samples)
    import matplotlib.pyplot as plt
    from getdist import MCSamples, plots
    settings = kwargs.pop('settings', dict(smooth_scale_2D=3.0))
    samples_g = MCSamples(samples=samples,
                          names=paramnames,
                          settings=settings,
                          **kwargs)
    g = plots.get_subplot_plotter()
    g.settings.num_plot_contours = 3
    g.triangle_plot([samples_g],
                    filled=False,
                    contour_colors=plt.cm.Set1.colors)
    if outprefix is not None:
        plt.savefig(outprefix + '_corner.pdf', bbox_inches='tight')
        plt.close()
Esempio n. 20
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)
Esempio n. 21
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")
Esempio n. 22
0
def get_separate_mcsamples(chain):
    """
    Function that returns separate :class:`~getdist.mcsamples.MCSamples`
    for each sampler chain.

    :param chain: :class:`~getdist.mcsamples.MCSamples` the input chain.
    :return: list of :class:`~getdist.mcsamples.MCSamples` with the separate
        chains.
    """
    # get separate chains:
    _chains = chain.getSeparateChains()
    # copy the param names and ranges:
    _mc_samples = []
    for ch in _chains:
        temp = MCSamples()
        temp.paramNames = chain.getParamNames()
        temp.setSamples(ch.samples, weights=ch.weights, loglikes=ch.loglikes)
        temp.sampler = chain.sampler
        temp.ranges = chain.ranges
        temp.updateBaseStatistics()
        _mc_samples.append(temp.copy())
    #
    return _mc_samples
Esempio n. 23
0
def OptimizeBandwidth_1D(diff_chain, param_names=None, num_bins=1000):
    """
    Compute an estimate of an optimal bandwidth for covariance scaling as in
    GetDist. This is performed on whitened samples (with identity covariance),
    in 1D, and then scaled up with a dimensionality correction.

    :param diff_chain: :class:`~getdist.mcsamples.MCSamples`
        input parameter difference chain
    :param param_names: (optional) parameter names of the parameters to be used
        in the calculation. By default all running parameters.
    :param num_bins: number of bins used for the 1D estimate
    :return: scaling vector for the whitened parameters
    """
    # initialize param names:
    if param_names is None:
        param_names = diff_chain.getParamNames().getRunningNames()
    else:
        chain_params = diff_chain.getParamNames().list()
        if not np.all([name in chain_params for name in param_names]):
            raise ValueError('Input parameter is not in the diff chain.\n',
                             'Input parameters ', param_names, '\n'
                             'Possible parameters', chain_params)
    # indexes:
    ind = [diff_chain.index[name] for name in param_names]
    # some initial calculations:
    _samples_cov = diff_chain.cov(pars=param_names)
    _num_params = len(ind)
    # whiten the samples:
    _temp = sqrtm(utils.QR_inverse(_samples_cov))
    white_samples = diff_chain.samples[:, ind].dot(_temp)
    # make these samples so that we can use GetDist band optization:
    temp_samples = MCSamples(samples=white_samples,
                             weights=diff_chain.weights,
                             ignore_rows=0, sampler=diff_chain.sampler)
    # now get optimal band for each parameter:
    bands = []
    for i in range(_num_params):
        # get the parameter:
        par = temp_samples._initParamRanges(i, paramConfid=None)
        # get the bins:
        temp_result = temp_samples._binSamples(temp_samples.samples[:, i],
                                               par, num_bins)
        bin_indices, bin_width, binmin, binmax = temp_result
        bins = np.bincount(bin_indices, weights=temp_samples.weights,
                           minlength=num_bins)
        # get the optimal smoothing scale:
        N_eff = temp_samples._get1DNeff(par, i)
        band = temp_samples.getAutoBandwidth1D(bins, par, i, N_eff=N_eff,
                                               mult_bias_correction_order=0,
                                               kernel_order=0) \
            * (binmax - binmin)
        # correction for dimensionality:
        dim_factor = Scotts_bandwidth(_num_params, N_eff)[0, 0]/Scotts_bandwidth(1., N_eff)[0, 0]
        #
        bands.append(band**2.*dim_factor)
    #
    return np.array(bands)
Esempio n. 24
0
 def _sampled_to_getdist_mcsamples(self, first=None, last=None):
     """
     Basic interface with getdist -- internal use only!
     (For analysis and plotting use `getdist.mcsamples.MCSamplesFromCobaya
     <https://getdist.readthedocs.io/en/latest/mcsamples.html#getdist.mcsamples.loadMCSamples>`_.)
     """
     names = list(self.sampled_params)
     # No logging of warnings temporarily, so getdist won't complain unnecessarily
     logging.disable(logging.WARNING)
     mcsamples = MCSamples(
         samples=self.data[:len(self)][names].values[first:last],
         weights=self.data[:len(self)][_weight].values[first:last],
         loglikes=self.data[:len(self)][_minuslogpost].values[first:last], names=names)
     logging.disable(logging.NOTSET)
     return mcsamples
Esempio n. 25
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")
Esempio n. 26
0
 def _sampled_to_getdist_mcsamples(self, first=None, last=None):
     """
     Basic interface with getdist -- internal use only!
     (For analysis and plotting use `getdist.mcsamples.loadCobayaSamples`.)
     """
     names = list(self.sampled_params)
     # No logging of warnings temporarily, so getdist won't complain unnecessarily
     logging.disable(logging.WARNING)
     mcsamples = MCSamples(
         samples=self.data[:self.n()][names].values[first:last],
         weights=self.data[:self.n()][_weight].values[first:last],
         loglikes=self.data[:self.n()][_minuslogpost].values[first:last],
         names=names)
     logging.disable(logging.NOTSET)
     return mcsamples
Esempio n. 27
0
    def _lngrid_from_trace(self, trace, make_plot):
        extents = {'x0': self.par_limits[1], 'x1': self.par_limits[2]}

        samps = MCSamples(samples=trace, names=['x0', 'x1'], ranges=extents)
        density = samps.get2DDensity('x0', 'x1')

        # set up the grid on which to evaluate the likelihood
        x_bins, y_bins = self.get_x0_x1

        xx, yy = np.meshgrid(x_bins, y_bins)
        pos = np.vstack([xx.ravel(), yy.ravel()]).T

        # Evalaute density on a grid
        prob = np.array([density.Prob(*ele) for ele in pos])
        prob[prob < 0] = 1e-50
        ln_prob = np.log(prob)
        ln_prob -= ln_prob.max()
        ln_prob = ln_prob.reshape(xx.shape)

        if make_plot:
            plt.pcolormesh(x_bins, y_bins, ln_prob)
            plt.colorbar()
            plt.clim(0, -5)
        return ln_prob
Esempio n. 28
0
 def sampled_to_getdist_mcsamples(self, first=None, last=None):
     """
     Basic interface with getdist -- internal use only!
     (For analysis and plotting use `getdist.mcsamples.MCSamplesFromCobaya
     <https://getdist.readthedocs.io/en/latest/mcsamples.html#getdist.mcsamples.loadMCSamples>`_.)
     """
     names = list(self.sampled_params)
     # No logging of warnings temporarily, so getdist won't complain unnecessarily
     with NoLogging():
         mcsamples = MCSamples(
             samples=self.data[:len(self)][names].to_numpy(dtype=np.float64)[
                     first:last],
             weights=self.data[:len(self)][OutPar.weight].to_numpy(dtype=np.float64)[
                     first:last],
             loglikes=self.data[:len(self)][OutPar.minuslogpost].to_numpy(
                 dtype=np.float64)[first:last],
             names=names)
     return mcsamples
Esempio n. 29
0
 def addChains(self, Chains, ignore_rows=0.3):
     root = list(np.asarray(Chains)[:, 0])
     lengend = list(np.asarray(Chains)[:, 1])
     n = len(Chains)
     for i in range(n):
         savefile_name = './chains/' + root[i] + '.npy'
         samples, theta_name, theta_fit, theta_fact, minkaf, data_num, ranges = np.load(
             savefile_name, allow_pickle=True)
         self.Samp.append(
             MCSamples(samples=samples,
                       names=theta_name,
                       labels=theta_name,
                       ranges=ranges,
                       settings={'ignore_rows': ignore_rows}))
     self.param_names = []
     for na in self.Samp[-1].getParamNames().names:
         self.param_names.append(na.name)
     self.lengend += lengend
     self._n = len(self.Samp)
     self.root += root
Esempio n. 30
0
    def get_gdobj(self):
        datapts = []
        weight = self.weight_col()
        for col in self.source.colnames:
            datapts.append(self.reduced_col(col))
        datapts = np.array(datapts).T
        vlfile = self.source.extract_ini("VALUES")#test
        vlpars = Parameter.load_parameters(vlfile)#test
        rangedict = {}
        for vlpar in vlpars:
            rangedict[str(vlpar)] = np.array(vlpar.limits)
        try: 
            loglikes = self.source.reduced_col("post")
        except: 
            loglikes = self.source.reduced_col("like")

        if MCSamples:
            gdc = MCSamples(samples = datapts,weights=weight,loglikes=loglikes, names=self.source.colnames,name_tag = self.source.name,ranges=rangedict)# ranges from value file
        else:
            raise ImportError('GetDist is not installed')
        self.source.gdc = gdc
        return gdc
Esempio n. 31
0
def plot_chain(folder, name_tag):
    files = glob.glob(folder + "*__*.txt")
    params = glob.glob(folder + "*_.paramnames")

    datalist = []
    for f in files:
        datalist.append(loadMCMC(f, params[0]))

    data = astropy.table.vstack(datalist)
    data_sim = data[:]
    weights_act = data['acceptance'][:]
    for col in [
            'likelihood', 'acceptance', 'omega_b', 'omega_cdm', '100theta_s',
            'tau_reio'
    ]:
        data.remove_column(col)
    if 'A_planck' in data.colnames:
        data.remove_column('A_planck')
    nparr_act = np.array(data.as_array().tolist()[:])
    return MCSamples(samples=nparr_act,
                     names=data.colnames,
                     labels=data.colnames,
                     name_tag=name_tag)
Esempio n. 32
0
def main(args):
    no_plots = False
    chain_root = args.chain_root
    if args.ini_file is None and chain_root is None:
        doError('Must give either a .ini file of parameters or a chain file root name. Run "GetDist.py -h" for help.')
    if not '.ini' in args.ini_file and chain_root is None:
        # use default settings acting on chain_root, no plots
        chain_root = args.ini_file
        args.ini_file = getdist.default_getdist_settings
        no_plots = True
    if not os.path.isfile(args.ini_file):
        doError('Parameter file does not exist: ' + args.ini_file)
    if chain_root and chain_root.endswith('.txt'):
        chain_root = chain_root[:-4]

    # Input parameters
    ini = IniFile(args.ini_file)

    # File root
    if chain_root is not None:
        in_root = chain_root
    else:
        in_root = ini.params['file_root']
    if not in_root:
        doError('Chain Root file name not given ')
    rootname = os.path.basename(in_root)

    if args.ignore_rows is not None:
        ignorerows = args.ignore_rows
    else:
        ignorerows = ini.float('ignore_rows', 0.0)

    samples_are_chains = ini.bool('samples_are_chains', True)
    
    paramnames = ini.string('parameter_names', '')

    # Create instance of MCSamples
    mc = MCSamples(in_root, files_are_chains=samples_are_chains, paramNamesFile=paramnames)

    mc.initParameters(ini)

    if ini.bool('adjust_priors', False) or ini.bool('map_params', False):
        doError(
            'To adjust priors or define new parameters, use a separate python script; see the python getdist docs for examples')

    plot_ext = ini.string('plot_ext', 'py')
    finish_run_command = ini.string('finish_run_command', '')

    no_plots = ini.bool('no_plots', no_plots)
    plots_only = ini.bool('plots_only', False)
    no_tests = plots_only or ini.bool('no_tests', False)

    thin_factor = ini.int('thin_factor', 0)
    thin_cool = ini.float('thin_cool', 1.0)

    make_single_samples = ini.bool('make_single_samples', False)
    single_thin = ini.int('single_thin', 1)
    cool = ini.float('cool', 1.0)

    chain_exclude = ini.int_list('exclude_chain')

    shade_meanlikes = ini.bool('shade_meanlikes', False)
    plot_meanlikes = ini.bool('plot_meanlikes', False)

    dumpNDbins = ini.bool('dump_ND_bins', False)

    out_dir = ini.string('out_dir', './')
    if out_dir:
        if not os.path.isdir(out_dir):
            os.mkdir(out_dir)
        print('producing files in directory ', out_dir)
    mc.out_dir = out_dir

    out_root = ini.string('out_root', '')
    if out_root:
        rootname = out_root
        print('producing files with with root ', out_root)
    mc.rootname = rootname

    rootdirname = os.path.join(out_dir, rootname)
    mc.rootdirname = rootdirname

    if 'do_minimal_1d_intervals' in ini.params:
        doError('do_minimal_1d_intervals no longer used; set credible_interval_threshold instead')

    line = ini.string('PCA_params', '')
    if line.lower() == 'all':
        PCA_params = mc.paramNames.list()
    else:
        PCA_params = line.split()
    PCA_num = ini.int('PCA_num', len(PCA_params))
    if PCA_num != 0:
        if PCA_num < 2:
            doError('Can only do PCA for 2 or more parameters')
        PCA_func = ini.string('PCA_func', '')
        # Characters representing functional mapping
        if PCA_func == '':
            PCA_func = ['N'] * PCA_num  # No mapping
        PCA_NormParam = ini.string('PCA_normparam', '') or None

    make_scatter_samples = ini.bool('make_scatter_samples', False)

    # ==============================================================================

    first_chain = ini.int('first_chain', 0)
    last_chain = ini.int('chain_num', -1)
    # -1 means keep reading until one not found

    # Chain files
    chain_files = chains.chainFiles(in_root, first_chain=first_chain, last_chain=last_chain,
                                    chain_exclude=chain_exclude)

    mc.loadChains(in_root, chain_files)

    mc.removeBurnFraction(ignorerows)
    mc.deleteFixedParams()
    mc.makeSingle()

    def filterParList(namestring, num=None):
        if not namestring.strip():
            pars = mc.paramNames.list()
        else:
            pars = []
            for name in namestring.split():
                if '?' in name or '*' in name:
                    pars += mc.paramNames.getMatches(name, strings=True)
                elif mc.paramNames.parWithName(name):
                    pars.append(name)
        if num is not None and len(pars) != num:
            print('%iD plot has missing parameter or wrong number of parameters: %s' % (num, pars))
            pars = None
        return pars


    if cool != 1:
        print('Cooling chains by ', cool)
        mc.cool(cool)

    mc.updateBaseStatistics()

    if not no_tests:
        mc.getConvergeTests(mc.converge_test_limit, writeDataToFile=True, feedback=True)

    mc.writeCovMatrix()
    mc.writeCorrelationMatrix()

    # Output thinned data if requested
    # Must do this with unsorted output
    if thin_factor != 0:
        thin_ix = mc.thin_indices(thin_factor)
        filename = rootdirname + '_thin.txt'
        mc.writeThinData(filename, thin_ix, thin_cool)

    print(mc.getNumSampleSummaryText().strip())
    if mc.likeStats: print(mc.likeStats.likeSummary().strip())

    if PCA_num > 0 and not plots_only:
        mc.PCA(PCA_params, PCA_func, PCA_NormParam, writeDataToFile=True)

    if not no_plots or dumpNDbins:
        # set plot_data_dir before we generate the 1D densities below
        plot_data_dir = ini.string('plot_data_dir', default='', allowEmpty=True)
        if plot_data_dir and not os.path.isdir(plot_data_dir):
            os.mkdir(plot_data_dir)
    else:
        plot_data_dir = None
    mc.plot_data_dir = plot_data_dir

    # Do 1D bins
    mc._setDensitiesandMarge1D(writeDataToFile=not no_plots and plot_data_dir, meanlikes=plot_meanlikes)

    if not no_plots:
        # Output files for 1D plots
        print('Calculating plot data...')

        plotparams = []
        line = ini.string('plot_params', '')
        if line not in ['', '0']:
            plotparams = filterParList(line)

        line = ini.string('plot_2D_param', '').strip()
        plot_2D_param = None
        if line and line != '0':
            plot_2D_param = line

        cust2DPlots = []
        if not plot_2D_param:
            # Use custom array of specific plots
            num_cust2D_plots = ini.int('plot_2D_num', 0)
            for i in range(1, num_cust2D_plots + 1):
                line = ini.string('plot' + str(i))
                pars = filterParList(line, 2)
                if pars is not None:
                    cust2DPlots.append(pars)
                else:
                    num_cust2D_plots -= 1

                
        triangle_params = []
        triangle_plot = ini.bool('triangle_plot', False)
        if triangle_plot:
            line = ini.string('triangle_params', '')
            triangle_params = filterParList(line)
            triangle_num = len(triangle_params)
            triangle_plot = triangle_num > 1

        num_3D_plots = ini.int('num_3D_plots', 0)
        plot_3D = []
        for ix in range(1, num_3D_plots + 1):
            line = ini.string('3D_plot' + str(ix))
            pars = filterParList(line, 3)
            if pars is not None:
                plot_3D.append(pars)
            else:
                num_3D_plots -= 1
            
      
        # Produce file of weight-1 samples if requested
        if (num_3D_plots and not make_single_samples or make_scatter_samples) and not no_plots:
            make_single_samples = True
            single_thin = max(1, int(round(mc.norm / mc.max_mult)) // mc.max_scatter_points)

        if plot_data_dir:
            if make_single_samples:
                filename = os.path.join(plot_data_dir, rootname.strip() + '_single.txt')
                mc.makeSingleSamples(filename, single_thin)

            # Write paramNames file
            mc.getParamNames().saveAsText(os.path.join(plot_data_dir, rootname + '.paramnames'))
            mc.getBounds().saveToFile(os.path.join(plot_data_dir, rootname + '.bounds'))

        make_plots = ini.bool('make_plots', False)

        done2D = {}

        filename = rootdirname + '.' + plot_ext
        mc.writeScriptPlots1D(filename, plotparams)
        if make_plots: runScript(filename)

        # Do 2D bins
        if plot_2D_param == 'corr':
            # In this case output the most correlated variable combinations
            print('...doing 2D plots for most correlated variables')
            cust2DPlots = mc.getCorrelatedVariable2DPlots()
            plot_2D_param = None
        elif plot_2D_param:
            mc.paramNames.parWithName(plot_2D_param, error=True)  # just check

        if cust2DPlots or plot_2D_param:
            print('...producing 2D plots')
            filename = rootdirname + '_2D.' + plot_ext
            done2D = mc.writeScriptPlots2D(filename, plot_2D_param, cust2DPlots,
                                           writeDataToFile=plot_data_dir, shade_meanlikes=shade_meanlikes)
            if make_plots: runScript(filename)

        if triangle_plot:
            # Add the off-diagonal 2D plots
            print('...producing triangle plot')
            filename = rootdirname + '_tri.' + plot_ext
            mc.writeScriptPlotsTri(filename, triangle_params)
            for i, p2 in enumerate(triangle_params):
                for p1 in triangle_params[i + 1:]:
                    if not done2D.get((p1, p2)) and plot_data_dir:
                        mc.get2DDensityGridData(p1, p2, writeDataToFile=True, meanlikes=shade_meanlikes)
            if make_plots: runScript(filename)

        # Do 3D plots (i.e. 2D scatter plots with coloured points)
        if num_3D_plots:
            print('...producing ', num_3D_plots, '2D colored scatter plots')
            filename = rootdirname + '_3D.' + plot_ext
            mc.writeScriptPlots3D(filename, plot_3D)
            if make_plots: runScript(filename)

    if not plots_only:
        # Write out stats marginalized
        mc.getMargeStats().saveAsText(rootdirname + '.margestats')

        # Limits from global likelihood
        if mc.loglikes is not None: mc.getLikeStats().saveAsText(rootdirname + '.likestats')


    if dumpNDbins:
        num_bins_ND = ini.int('num_bins_ND', 10)
        line = ini.string('ND_params','')
        
        if line not in ["",'0']:
            ND_params = filterParList(line)
            print(ND_params)

            ND_dim=len(ND_params)
            print(ND_dim)
           
            mc.getRawNDDensityGridData(ND_params, writeDataToFile=True,
                                       meanlikes=shade_meanlikes)
    



    # System command
    if finish_run_command:
        finish_run_command = finish_run_command.replace('%ROOTNAME%', rootname)
        finish_run_command = finish_run_command.replace('%PLOTDIR%', plot_data_dir)
        finish_run_command = finish_run_command.replace('%PLOTROOT%', os.path.join(plot_data_dir, rootname))
        os.system(finish_run_command)
Esempio n. 33
0
def main(args):
    no_plots = False
    chain_root = args.chain_root
    if args.ini_file is None and chain_root is None:
        doError('Must give either a .ini file of parameters or a chain file root name. Run "GetDist.py -h" for help.')
    if not ".ini" in args.ini_file and chain_root is None:
        # use default settings acting on chain_root, no plots
        chain_root = args.ini_file
        args.ini_file = getdist.default_getdist_settings
        no_plots = True
    if not os.path.isfile(args.ini_file):
        doError("Parameter file does not exist: " + args.ini_file)
    if chain_root and chain_root.endswith(".txt"):
        chain_root = chain_root[:-4]

    # Input parameters
    ini = IniFile(args.ini_file)

    # File root
    if chain_root is not None:
        in_root = chain_root
    else:
        in_root = ini.params["file_root"]
    if not in_root:
        doError("Chain Root file name not given ")
    rootname = os.path.basename(in_root)

    if args.ignore_rows is not None:
        ignorerows = args.ignore_rows
    else:
        ignorerows = ini.float("ignore_rows", 0.0)

    samples_are_chains = ini.bool("samples_are_chains", True)

    # Create instance of MCSamples
    mc = MCSamples(in_root, files_are_chains=samples_are_chains)

    mc.initParameters(ini)

    if ini.bool("adjust_priors", False) or ini.bool("map_params", False):
        doError(
            "To adjust priors or define new parameters, use a separate python script; see the python getdist docs for examples"
        )

    plot_ext = ini.string("plot_ext", "py")
    finish_run_command = ini.string("finish_run_command", "")

    no_plots = ini.bool("no_plots", no_plots)
    plots_only = ini.bool("plots_only", False)
    no_tests = plots_only or ini.bool("no_tests", False)

    thin_factor = ini.int("thin_factor", 0)
    thin_cool = ini.float("thin_cool", 1.0)

    make_single_samples = ini.bool("make_single_samples", False)
    single_thin = ini.int("single_thin", 1)
    cool = ini.float("cool", 1.0)

    chain_exclude = ini.int_list("exclude_chain")

    shade_meanlikes = ini.bool("shade_meanlikes", False)
    plot_meanlikes = ini.bool("plot_meanlikes", False)

    out_dir = ini.string("out_dir", "./")
    if out_dir:
        if not os.path.isdir(out_dir):
            os.mkdir(out_dir)
        print("producing files in directory ", out_dir)
    mc.out_dir = out_dir

    out_root = ini.string("out_root", "")
    if out_root:
        rootname = out_root
        print("producing files with with root ", out_root)
    mc.rootname = rootname

    rootdirname = os.path.join(out_dir, rootname)
    mc.rootdirname = rootdirname

    if "do_minimal_1d_intervals" in ini.params:
        doError("do_minimal_1d_intervals no longer used; set credible_interval_threshold instead")

    line = ini.string("PCA_params", "")
    if line.lower() == "all":
        PCA_params = mc.paramNames.list()
    else:
        PCA_params = line.split()
    PCA_num = ini.int("PCA_num", len(PCA_params))
    if PCA_num != 0:
        if PCA_num < 2:
            doError("Can only do PCA for 2 or more parameters")
        PCA_func = ini.string("PCA_func", "")
        # Characters representing functional mapping
        if PCA_func == "":
            PCA_func = ["N"] * PCA_num  # No mapping
        PCA_NormParam = ini.string("PCA_normparam", "") or None

    make_scatter_samples = ini.bool("make_scatter_samples", False)

    # ==============================================================================

    first_chain = ini.int("first_chain", 0)
    last_chain = ini.int("chain_num", -1)
    # -1 means keep reading until one not found

    # Chain files
    chain_files = chains.chainFiles(
        in_root, first_chain=first_chain, last_chain=last_chain, chain_exclude=chain_exclude
    )

    mc.loadChains(in_root, chain_files)

    mc.removeBurnFraction(ignorerows)
    mc.deleteFixedParams()
    mc.makeSingle()

    def filterParList(namestring, num=None):
        if not namestring.strip():
            pars = mc.paramNames.list()
        else:
            pars = []
            for name in namestring.split():
                if "?" in name or "*" in name:
                    pars += mc.paramNames.getMatches(name, strings=True)
                elif mc.paramNames.parWithName(name):
                    pars.append(name)
        if num is not None and len(pars) != num:
            raise Exception("%iD plot has missing parameter or wrong number of parameters: %s" % (num, pars))
        return pars

    if cool != 1:
        print("Cooling chains by ", cool)
        mc.cool(cool)

    mc.updateBaseStatistics()

    if not no_tests:
        mc.getConvergeTests(mc.converge_test_limit, writeDataToFile=True, feedback=True)

    mc.writeCovMatrix()
    mc.writeCorrelationMatrix()

    # Output thinned data if requested
    # Must do this with unsorted output
    if thin_factor != 0:
        thin_ix = mc.thin_indices(thin_factor)
        filename = rootdirname + "_thin.txt"
        mc.writeThinData(filename, thin_ix, thin_cool)

    print(mc.getNumSampleSummaryText().strip())
    if mc.likeStats:
        print(mc.likeStats.likeSummary().strip())

    if PCA_num > 0 and not plots_only:
        mc.PCA(PCA_params, PCA_func, PCA_NormParam, writeDataToFile=True)

    if not no_plots:
        # set plot_data_dir before we generate the 1D densities below
        plot_data_dir = ini.string("plot_data_dir", default="", allowEmpty=True)
        if plot_data_dir and not os.path.isdir(plot_data_dir):
            os.mkdir(plot_data_dir)
    else:
        plot_data_dir = None
    mc.plot_data_dir = plot_data_dir

    # Do 1D bins
    mc._setDensitiesandMarge1D(writeDataToFile=not no_plots and plot_data_dir, meanlikes=plot_meanlikes)

    if not no_plots:
        # Output files for 1D plots
        print("Calculating plot data...")

        plotparams = []
        line = ini.string("plot_params", "")
        if line not in ["", "0"]:
            plotparams = filterParList(line)

        line = ini.string("plot_2D_param", "").strip()
        plot_2D_param = None
        if line and line != "0":
            plot_2D_param = line

        cust2DPlots = []
        if not plot_2D_param:
            # Use custom array of specific plots
            num_cust2D_plots = ini.int("plot_2D_num", 0)
            for i in range(1, num_cust2D_plots + 1):
                line = ini.string("plot" + str(i))
                pars = filterParList(line, 2)
                cust2DPlots.append(pars)

        triangle_params = []
        triangle_plot = ini.bool("triangle_plot", False)
        if triangle_plot:
            line = ini.string("triangle_params", "")
            triangle_params = filterParList(line)
            triangle_num = len(triangle_params)
            triangle_plot = triangle_num > 1

        num_3D_plots = ini.int("num_3D_plots", 0)
        plot_3D = []
        for ix in range(1, num_3D_plots + 1):
            line = ini.string("3D_plot" + str(ix))
            plot_3D.append(filterParList(line, 3))

        # Produce file of weight-1 samples if requested
        if (num_3D_plots and not make_single_samples or make_scatter_samples) and not no_plots:
            make_single_samples = True
            single_thin = max(1, int(round(mc.norm / mc.max_mult)) // mc.max_scatter_points)

        if plot_data_dir:
            if make_single_samples:
                filename = os.path.join(plot_data_dir, rootname.strip() + "_single.txt")
                mc.makeSingleSamples(filename, single_thin)

            # Write paramNames file
            mc.getParamNames().saveAsText(os.path.join(plot_data_dir, rootname + ".paramnames"))
            mc.getBounds().saveToFile(os.path.join(plot_data_dir, rootname + ".bounds"))

        make_plots = ini.bool("make_plots", False)

        done2D = {}

        filename = rootdirname + "." + plot_ext
        mc.writeScriptPlots1D(filename, plotparams)
        if make_plots:
            runScript(filename)

        # Do 2D bins
        if plot_2D_param == "corr":
            # In this case output the most correlated variable combinations
            print("...doing 2D plots for most correlated variables")
            cust2DPlots = mc.getCorrelatedVariable2DPlots()
            plot_2D_param = None
        elif plot_2D_param:
            mc.paramNames.parWithName(plot_2D_param, error=True)  # just check

        if cust2DPlots or plot_2D_param:
            print("...producing 2D plots")
            filename = rootdirname + "_2D." + plot_ext
            done2D = mc.writeScriptPlots2D(
                filename, plot_2D_param, cust2DPlots, writeDataToFile=plot_data_dir, shade_meanlikes=shade_meanlikes
            )
            if make_plots:
                runScript(filename)

        if triangle_plot:
            # Add the off-diagonal 2D plots
            print("...producing triangle plot")
            filename = rootdirname + "_tri." + plot_ext
            mc.writeScriptPlotsTri(filename, triangle_params)
            for i, p2 in enumerate(triangle_params):
                for p1 in triangle_params[i + 1 :]:
                    if not done2D.get((p1, p2)) and plot_data_dir:
                        mc.get2DDensityGridData(p1, p2, writeDataToFile=True, meanlikes=shade_meanlikes)
            if make_plots:
                runScript(filename)

        # Do 3D plots (i.e. 2D scatter plots with coloured points)
        if num_3D_plots:
            print("...producing ", num_3D_plots, "2D colored scatter plots")
            filename = rootdirname + "_3D." + plot_ext
            mc.writeScriptPlots3D(filename, plot_3D)
            if make_plots:
                runScript(filename)

    if not plots_only:
        # Write out stats marginalized
        mc.getMargeStats().saveAsText(rootdirname + ".margestats")

        # Limits from global likelihood
        if mc.loglikes is not None:
            mc.getLikeStats().saveAsText(rootdirname + ".likestats")

    # System command
    if finish_run_command:
        finish_run_command = finish_run_command.replace("%ROOTNAME%", rootname)
        finish_run_command = finish_run_command.replace("%PLOTDIR%", plot_data_dir)
        finish_run_command = finish_run_command.replace("%PLOTROOT%", os.path.join(plot_data_dir, rootname))
        os.system(finish_run_command)