def plot_mcmc_results(chain):
    # Pull m and b arrays out of the Markov chain.
    mm = [m for b,m in chain]
    bb = [b for b,m in chain]
    # Scatterplot of m,b posterior samples
    plt.clf()
    plt.contour(bgrid, mgrid, posterior, pdf_contour_levels(posterior))
    plt.plot(bb, mm, 'b.', alpha=0.1)
    plot_mb_setup()
    plt.show()
    
    # Histograms
    import triangle
    triangle.corner(chain, labels=['b','m'], extents=[0.99]*2)
    plt.show()
    
    # Traces
    plt.clf()
    plt.subplot(2,1,1)
    plt.plot(mm, 'k-')
    plt.ylim(mlo,mhi)
    plt.ylabel('m')
    plt.subplot(2,1,2)
    plt.plot(bb, 'k-')
    plt.ylabel('b')
    plt.ylim(blo,bhi)
    plt.show()
def triangle_plot_chain(chain, lnprob, prefix):
    """
        Make a 7x7 triangle.
        """
    nx, nq = chain.shape
    maxlnp = np.max(lnprob)
    lnpextent = [(maxlnp-14.5, maxlnp+0.5)]
    bar = SixPosition(chain[0]) # temporary variable to get names
    foo = np.concatenate((chain, lnprob.reshape((nx, 1))), axis=1)
    labels = np.append(bar.get_sixpos_names(), [r"$\ln p$"])
    extents = bar.get_sixpos_extents() + lnpextent
    fig = tri.corner(foo, labels=labels, extents=extents, plot_contours=False)
    fn = prefix + "a.png"
    print "triangle_plot_chain(): writing " + fn
    fig.savefig(fn)
    obsfoo = 1. * foo # copy
    for i in range(nx):
        obsfoo[i,:6] = SixPosition(foo[i,:6]).get_observables_array()
    labels = np.append(bar.get_observables_names(), [r"$\ln p$"])
    extents = bar.get_observables_extents() + lnpextent
    fig = tri.corner(obsfoo, labels=labels, extents=extents, plot_contours=False)
    fn = prefix + "b.png"
    print "triangle_plot_chain(): writing " + fn
    fig.savefig(fn)
    intfoo = 1. * foo[:,2:] # copy
    for i in range(nx):
        intfoo[i,:4] = SixPosition(foo[i,:6]).get_integrals_of_motion()
    labels = np.append(bar.get_integrals_of_motion_names(), [r"$\ln p$"])
    extents = bar.get_integrals_of_motion_extents() + lnpextent
    fig = tri.corner(intfoo, labels=labels, extents=extents, plot_contours=False)
    fn = prefix + "c.png"
    print "triangle_plot_chain(): writing " + fn
    fig.savefig(fn)
    return None
예제 #3
0
def plot(spergel_sampler, series_samplers, args):
    p_initial = [args.x0, args.y0, args.HLR, args.flux, args.e1, args.e2]
    ndim = spergel_sampler.chain.shape[-1]

    # triangle plots
    spergel_samples = spergel_sampler.chain[:, args.nburn:, :].reshape(
        (-1, ndim))

    extents = []
    for i in range(ndim):
        vals = spergel_samples[:, i]
        for jmax in range(args.jmaxmin, args.jmaxmax + 1):
            series_samples = series_samplers[jmax].chain[:, args.
                                                         nburn:, :].reshape(
                                                             (-1, ndim))
            vals = np.concatenate([vals, series_samples[:, i]])
        extents.append(np.percentile(vals, [0.5, 99.5]))

    for jmax in range(args.jmaxmin, args.jmaxmax + 1):
        series_samples = series_samplers[jmax].chain[:,
                                                     args.nburn:, :].reshape(
                                                         (-1, ndim))
        labels = ["x0", "y0", "HLR", "flux", "e1", "e2"]
        fig = triangle.corner(spergel_samples,
                              labels=labels,
                              truths=p_initial,
                              extents=extents)
        fig = triangle.corner(series_samples,
                              color='red',
                              extents=extents,
                              fig=fig)

        fig.savefig(args.plot_prefix +
                    "_jmax_{:02d}_triangle.png".format(jmax))
        plt.close(fig)
예제 #4
0
def plot_corner(logpost, chain, outdir=None):
    flatchain = chain.reshape((-1, chain.shape[2]))

    tri.corner(flatchain, labels=logpost.pnames, quantiles=[0.05, 0.95])

    if outdir is not None:
        pp.savefig(op.join(outdir, 'corner.pdf'))
	def plot_PDFtriangle(self,parameterset, labels):		

		if parameterset=='10pars':
			figure = triangle.corner(self.chain.flatchain, labels= labels, plot_contours=True, plot_datapoints = False, show_titles=True, quantiles=[0.16, 0.50, 0.84])
		elif parameterset == 'int_lums':
			figure = triangle.corner(self.int_lums.T, labels= labels,   plot_contours=True, plot_datapoints = False, show_titles=True, quantiles=[0.16, 0.50, 0.84])
		return figure
예제 #6
0
def MCParTemp(nwalkers,steps,ntemps=40,burnin=100,threads=1):
    t1 = time.time()
    # use emcee parallel tempering sampler to find prob. surface
    ndim = 4
    startpos = [[[0,0,0,0] + 5e-3*np.random.randn(ndim) for i in range(nwalkers)] for i in range(ntemps)]

    # time how long it should take
    t0 = time.time()
    testpos = [[[0,0,0,0] + 5e-3*np.random.randn(4) for i in range(8)] for i in range(8)]
    sampler  = emcee.PTSampler(8,8, 4, lnlike, xs.lnprior, threads=threads)
    for ptest,logprobtest, logliketest in sampler.sample(testpos,iterations=10):
        pass
    sampler.reset()
    tdiff = time.time() - t0
    esttime = ntemps/8.*nwalkers/8.*(burnin+steps)/10.*tdiff/60.
    print 'Estimated time = {} minutes'.format(esttime)

    # burn-in for defined number of steps (default=100)
    sampler  = emcee.PTSampler(ntemps,nwalkers, ndim, lnlike, xs.lnprior, threads=threads)
    for p,logprob, loglike in sampler.sample(startpos,iterations=burnin):
        pass
    sampler.reset()

    # run mcmc
    sampler = emcee.PTSampler(ntemps, nwalkers, ndim, lnlike, xs.lnprior, threads=threads)
    for p, logprob, loglike in sampler.sample(p, lnprob0=logprob, lnlike0=loglike,iterations=steps):
        pass

    print 'Time: {} minutes'.format(time.time() - t0)
    samples = sampler.chain[:,:,:,:].reshape((-1, ndim))
    triangle.corner(samples,labels=[r'$\Delta s$',r'$M_A$',r'$F_1^S$',r'$\mu_S$'],quantiles=[0.05,0.5,0.95])
    print sampler.thermodynamic_integration_log_evidence(fburnin=0)
    return samples
예제 #7
0
def triangleMAPs(savefilename,basename):
    with open(savefilename,'rb') as savefile:
        bf= numpy.array(pickle.load(savefile))
        samples= numpy.array(pickle.load(savefile))
        bf_g15= numpy.array(pickle.load(savefile))
        samples_g15= numpy.array(pickle.load(savefile))
        bf_zero= numpy.array(pickle.load(savefile))
        samples_zero= numpy.array(pickle.load(savefile))
    labels= []
    for jj in range(samples.shape[2]):
        labels.append(r"$\mathrm{param}\ %i$" % jj)
    maps= define_rcsample.MAPs()
    for ii, map in enumerate(maps.map()):
        if ii >= len(bf): break
        tfeh= numpy.nanmedian(map['FE_H'])
        tafe= numpy.nanmedian(map[define_rcsample._AFETAG])
        for tbf,tsamples,ext in zip([bf,bf_g15,bf_zero],
                                    [samples,samples_g15,samples_zero],
                                    ['fid','g15','zero']):
            try:
                triangle.corner(tsamples[ii,].T,quantiles=[0.16, 0.5, 0.84],
                                labels=labels,
                                show_titles=True,title_args={"fontsize": 12},
                                bins=21)
            except ValueError: pass
            else:
                bovy_plot.bovy_text(r'$[\mathrm{{Fe/H}}] = {feh:.1f},$'\
                                        .format(feh=tfeh)+'\n'
                                    +r'$[\alpha/\mathrm{{Fe}}] = {afe:.2f}$'\
                                        .format(afe=tafe),
                                    top_left=True,size=16.)
                bovy_plot.bovy_end_print(basename+"_%i_%s.png" % (ii,ext))
    return None
예제 #8
0
파일: base.py 프로젝트: specgrid/starkit
 def plot_triangle(self, **kwargs):
     try:
         from triangle import corner
     except ImportError:
         raise ImportError('Plotting requires trianglepy')
     corner(self.posterior_data[self.parameter_names],
            labels=self.parameter_names, **kwargs)
예제 #9
0
    def plotCorner(self):
        model = self.model

        mcmcVParams = self.mcmcRes.vparam_names
        nestVParams = self.nestRes.vparam_names

        mcmcSamples = self.mcmcRes.samples
        nestSamples = self.nestRes.samples

        mcmc_ndim, mcmc_nsamples = len(mcmcVParams), len(mcmcSamples)
        nest_ndim, nest_nsamples = len(nestVParams), len(nestSamples)

        # make figure

        figure_mcmc = triangle.corner(mcmcSamples, labels=[mcmcVParams[0], mcmcVParams[1], mcmcVParams[2], mcmcVParams[3]],
                     truths=[model.get(mcmcVParams[0]), model.get(mcmcVParams[1]),
                             model.get(mcmcVParams[2]), model.get(mcmcVParams[3])],
                     range=mcmc_ndim*[0.9999],
                     show_titles=True, title_args={"fontsize": 12})

        figure_mcmc.gca().annotate("mcmc sampling", xy=(0.5, 1.0), xycoords="figure fraction",
                  xytext=(0, -5), textcoords="offset points",
                  ha="center", va="top")

        figure_nest = triangle.corner(nestSamples, labels=[nestVParams[0], nestVParams[1], nestVParams[2], nestVParams[3]],
                     truths=[model.get(nestVParams[0]), model.get(nestVParams[1]),
                             model.get(nestVParams[2]), model.get(nestVParams[3])],
                     weights=self.nestRes.weights, range=nest_ndim*[0.9999],
                     show_titles=True, title_args={"fontsize": 12})

        figure_nest.gca().annotate("nest sampling", xy=(0.5, 1.0), xycoords="figure fraction",
                  xytext=(0, -5), textcoords="offset points",
                  ha="center", va="top")

        return figure_mcmc, figure_nest
def plot_mcmc_results(chain,mlimits,blimits):
    # Pull m and b arrays out of the Markov chain.
    mm = [m for b,m in chain]
    bb = [b for b,m in chain]
    # Scatterplot of m,b posterior samples
    plt.clf()
    plt.contour(bgrid, mgrid, posterior, pdf_contour_levels(posterior))
    plt.plot(bb, mm, 'b.', alpha=0.1)
    plot_mb_setup(mlimits,blimits)
    plt.show()

    # Histograms
    import triangle
    triangle.corner(chain, labels=['b','m'], extents=[0.99]*2)
    plt.show()

    # Traces
    plt.clf()
    plt.subplot(2,1,1)
    plt.plot(mm, 'k-')
    plt.ylim(mlimits[0],mlimits[1])
    plt.ylabel('m')
    plt.subplot(2,1,2)
    plt.plot(bb, 'k-')
    plt.ylabel('b')
    plt.ylim(blimits[0],blimits[1])
    plt.show()
예제 #11
0
def subtriangle(sample_results, outname=None, showpars=None,
                start=0, thin=1, truths=None, trim_outliers=None,
                extents=None, **kwargs):
    """Make a triangle plot of the (thinned, latter) samples of the posterior
    parameter space.  Optionally make the plot only for a supplied subset of
    the parameters.

    :param start:
        The iteration number to start with when drawing samples to plot.

    :param thin:
        The thinning of each chain to perform when drawing samples to plot.

    :param showpars:
        List of string names of parameters to include in the corner plot.

    :param truths:
        List of truth values for the chosen parameters
    """
    try:
        import triangle
    except(ImportError):
        import corner as triangle

    # pull out the parameter names and flatten the thinned chains
    try:
        parnames = np.array(sample_results['theta_labels'])
    except(KeyError):
        parnames = np.array(sample_results['model'].theta_labels())
    flatchain = sample_results['chain'][:, start::thin, :]
    flatchain = flatchain.reshape(flatchain.shape[0] * flatchain.shape[1],
                                  flatchain.shape[2])

    # logify mass
    if 'mass' in parnames:
        midx = [l=='mass' for l in parnames]
        flatchain[:,midx] = np.log10(flatchain[:,midx])
        parnames[midx] = 'logmass'

    # restrict to parameters you want to show
    if showpars is not None:
        ind_show = np.array([p in showpars for p in parnames], dtype=bool)
        flatchain = flatchain[:, ind_show]
        #truths = truths[ind_show]
        parnames = parnames[ind_show]
    if trim_outliers is not None:
        trim_outliers = len(parnames) * [trim_outliers]
    try:
        fig = triangle.corner(flatchain, labels=parnames, truths=truths,  verbose=False,
                              quantiles=[0.16, 0.5, 0.84], range=trim_outliers, **kwargs)
    except:
        fig = triangle.corner(flatchain, labels=parnames, truths=truths,  verbose=False,
                              quantiles=[0.16, 0.5, 0.84], range=trim_outliers, **kwargs)

    if outname is not None:
        fig.savefig('{0}.triangle.png'.format(outname))
        #pl.close(fig)
    else:
        return fig
예제 #12
0
파일: specfit.py 프로젝트: lewyh/specfit
def mcmctriangle(ID):
	filename=('tables/ndim_' + str(ndim) + '_walkers_' + str(nwalkers) + '.fits')
	data = Table.read(filename)
	data_t = np.array([data[key] for key in param_keys]).transpose()
	truths = [np.median(data[key]) for key in param_keys]
	triangle.corner(data_t,labels=param_labels,quantiles=[0.1587,0.5000,0.8413])
	plt.savefig('plots/triangle_ndim_' + str(ndim) + '_walkers_' + str(nwalkers) + '.pdf')
	plt.close()
예제 #13
0
 def plot_triangle(self, **kwargs):
     try:
         from triangle import corner
     except ImportError:
         raise ImportError('Plotting requires trianglepy')
     data_columns = self.posterior_data.columns[2:]
     corner(self.posterior_data[data_columns],
            weights=self.posterior_data.posterior, **kwargs)
예제 #14
0
파일: run.py 프로젝트: farr/RVChallenge
def convergence_plots(sampler):
    plt.figure()
    plt.plot(sampler.lnprobability.T)
    plt.figure()
    pu.plot_emcee_chains(sampler.chain)
    triangle.corner(sampler.flatchain)

    print 'Autocorrelation lengths: ', ac.emcee_chain_autocorrelation_lengths(sampler.chain)
    print 'Gelman-Rubin R: ', ac.emcee_gelman_rubin_r(sampler.chain)
예제 #15
0
def convergence_plots(sampler):
    plt.figure()
    plt.plot(sampler.lnprobability.T)
    plt.figure()
    pu.plot_emcee_chains(sampler.chain)
    triangle.corner(sampler.flatchain)

    print 'Autocorrelation lengths: ', ac.emcee_chain_autocorrelation_lengths(
        sampler.chain)
    print 'Gelman-Rubin R: ', ac.emcee_gelman_rubin_r(sampler.chain)
예제 #16
0
def PlotTriangle(fileroot,usetruths=True):
	data = np.loadtxt(fileroot+'post_equal_weights.dat', usecols=(0,1,2,3,4,5))
	if (usetruths):
		truths = np.loadtxt(fileroot+'injected_values.txt')
		figure = triangle.corner(data, labels=names, truths=truths, bins=30, quantiles=[0.05, 0.5, 0.95],
								 show_titles=True, title_args={"fontsize": 12})
	else:
		figure = triangle.corner(data, labels=names, bins=30, quantiles=[0.05, 0.5, 0.95],
								 show_titles=True, title_args={"fontsize": 12})
	figure.savefig(fileroot+'posterior_plot.png')
예제 #17
0
파일: cleanfit.py 프로젝트: bmorris3/hat-11
def plot_triangle(samples):
    import triangle
    if samples.shape[1] == 2:
        fig = triangle.corner(samples, labels=["$t_0$", "$P$"])
    if samples.shape[1] == 6:
        fig = triangle.corner(samples, labels=["$t_0$", r"depth", r"duration",
                                               r"$b$", "$q_1$", "$q_2$"])
    elif samples.shape[1] == 8:
        fig = triangle.corner(samples, labels=["$t_0$", r"depth", r"duration",
                                               r"$b$", "$q_1$", "$q_2$", "$q_3$", "$q_4$"])
    plt.show()
예제 #18
0
def subtriangle(sample_results, outname=None, showpars=None,
                start=0, thin=1, truths=None, trim_outliers=None,
                extents=None, **kwargs):
    """Make a triangle plot of the (thinned, latter) samples of the posterior
    parameter space.  Optionally make the plot only for a supplied subset of
    the parameters.

    :param start:
        The iteration number to start with when drawing samples to plot.

    :param thin:
        The thinning of each chain to perform when drawing samples to plot.

    :param showpars:
        List of string names of parameters to include in the corner plot.

    :param truths:
        List of truth values for the chosen parameters
    """
    try:
        import triangle
    except(ImportError):
        import corner as triangle

    # pull out the parameter names and flatten the thinned chains
    try:
        parnames = np.array(sample_results['theta_labels'])
    except(KeyError):
        parnames = np.array(sample_results['model'].theta_labels())
    flatchain = sample_results['chain'][:, start::thin, :]
    flatchain = flatchain.reshape(flatchain.shape[0] * flatchain.shape[1],
                                  flatchain.shape[2])

    # restrict to parameters you want to show
    if showpars is not None:
        ind_show = np.array([p in showpars for p in parnames], dtype=bool)
        flatchain = flatchain[:, ind_show]
        #truths = truths[ind_show]
        parnames = parnames[ind_show]
    if trim_outliers is not None:
        trim_outliers = len(parnames) * [trim_outliers]
    try:
        fig = triangle.corner(flatchain, labels=parnames, truths=truths,  verbose=False,
                              quantiles=[0.16, 0.5, 0.84], extents=trim_outliers, **kwargs)
    except:
        fig = triangle.corner(flatchain, labels=parnames, truths=truths,  verbose=False,
                              quantiles=[0.16, 0.5, 0.84], range=trim_outliers, **kwargs)

    if outname is not None:
        fig.savefig('{0}.triangle.png'.format(outname))
        #pl.close(fig)
    else:
        return fig
예제 #19
0
def main():
    pot = agama.Potential(type="Dehnen", mass=1, scaleRadius=1.)
    actf = agama.ActionFinder(pot)
    particles, masses = createHernquistModel(100000)
    actions = actf(particles)

    # do a parameter search to find best-fit distribution function describing these particles
    initparams = numpy.array([2.0, 4.0, 1.0, 1.0, 0.0])
    result = minimize(model_search_fnc,
                      initparams,
                      args=(actions, ),
                      method='Nelder-Mead',
                      options=dict(maxiter=1000, maxfev=1000, disp=True))

    # explore the parameter space around the best-fit values using the MCMC chain
    import emcee, matplotlib.pyplot as plt, triangle as corner
    print 'Starting MCMC'
    ndim = len(initparams)
    nwalkers = 16  # number of parallel walkers in the chain
    nsteps = 300  # number of steps in MCMC chain
    nburnin = 100  # number of initial steps to discard
    # initial coverage of parameter space - around the best-fit solution with a small dispersion
    initwalkers = [
        result.x + 0.01 * numpy.random.randn(ndim) for i in range(nwalkers)
    ]
    sampler = emcee.EnsembleSampler(nwalkers,
                                    ndim,
                                    model_search_emcee,
                                    args=(actions, ))
    sampler.run_mcmc(initwalkers, nsteps)

    # show the time evolution of parameters carried by the ensemble of walkers (time=number of MC steps)
    fig, axes = plt.subplots(ndim + 1, 1, sharex=True)
    for i in range(ndim):
        axes[i].plot(sampler.chain[:, :, i].T, color='k', alpha=0.5)
        axes[i].set_ylabel(labels[i])
    # last panel shows the evolution of log-likelihood for the ensemble of walkers
    axes[-1].plot(sampler.lnprobability.T, color='k', alpha=0.5)
    axes[-1].set_ylabel('log(L)')
    maxloglike = numpy.max(sampler.lnprobability)
    axes[-1].set_ylim(maxloglike - 3 * ndim, maxloglike)
    fig.tight_layout(h_pad=0.)
    plt.show()

    # show the posterior distribution of parameters
    samples = sampler.chain[:, nburnin:, :].reshape((-1, ndim))
    corner.corner(samples, \
        labels=labels, quantiles=[0.16, 0.5, 0.84], truths=result.x)
    plt.show()
    print "Acceptance fraction: ", numpy.mean(
        sampler.acceptance_fraction)  # should be in the range 0.2-0.5
    print "Autocorrelation time: ", sampler.acor  # should be considerably shorter than the total number of steps
예제 #20
0
def triangle_plot_chain(chain, lnprob, prefix, truth=None, truth_lnprob=0.):
    """
    Make a 7x7 triangle.
    """
    nx, nq = chain.shape
    maxlnp = np.max(lnprob)
    lnpextent = [(maxlnp-14.5, maxlnp+0.5)]
    bar = SixPosition(chain[0]) # temporary variable to get names
    foo = np.concatenate((chain, lnprob.reshape((nx, 1))), axis=1)
    if truth is None:
        truths = None
    else:
        truths = np.append(truth, [truth_lnprob])
    labels = np.append(bar.get_sixpos_names(), [r"$\ln p$"])
    extents = bar.get_sixpos_extents() + lnpextent
    fig = tri.corner(foo, labels=labels, extents=extents,
                     truths=truths, plot_contours=False)
    fn = prefix + "a.png"
    print "triangle_plot_chain(): writing " + fn
    fig.savefig(fn)
    obsfoo = 1. * foo # copy
    for i in range(nx):
        obsfoo[i,:6] = SixPosition(foo[i,:6]).get_observables_array()
    if truth is None:
        trueobs = None
    else:
        trueobs = 1. * truths # copy
        trueobs[:6] = SixPosition(truth).get_observables_array()
    labels = np.append(bar.get_observables_names(), [r"$\ln p$"])
    extents = bar.get_observables_extents() + lnpextent
    fig = tri.corner(obsfoo, labels=labels, extents=extents,
                     truths=trueobs, plot_contours=False)
    fn = prefix + "b.png"
    print "triangle_plot_chain(): writing " + fn
    fig.savefig(fn)
    intfoo = 1. * foo[:,2:] # copy
    for i in range(nx):
        intfoo[i,:4] = SixPosition(foo[i,:6]).get_integrals_of_motion()
    if truth is None:
        trueint = None
    else:
        trueint = 1. * truths[2:] # copy
        trueint[:4] = SixPosition(truth).get_integrals_of_motion()
    labels = np.append(bar.get_integrals_of_motion_names(), [r"$\ln p$"])
    extents = bar.get_integrals_of_motion_extents() + lnpextent
    fig = tri.corner(intfoo, labels=labels, extents=extents,
                     truths=trueint, plot_contours=False)
    fn = prefix + "c.png"
    print "triangle_plot_chain(): writing " + fn
    fig.savefig(fn)
    return None
예제 #21
0
파일: model.py 프로젝트: j-dr/ADDHALOS
    def visModel(self, labels=None, fname=None):

        nSamples = 1e6
        samples = self.reg.sample(n_samples=nSamples)
        #quick fix for better plotting
        samples = np.log10(samples)

        if self.pred == None:
            if hasTriangle:
                figure = triangle.corner(samples, labels=labels,
                                         quantiles=[0.16, 0.5, 0.84],
                                         show_titles=True, title_args={"fontsize": 12})
                if fname!=None:
                    plt.savefig('predicted_'+fname)

            elif samples.size[1]==2:
                f, ax = plt.subplots(1)
                ax.hist2d(samples[:,0], samples[:,1])
                if fname!=None:
                    plt.savefig('predicted_'+fname)

            else:
                raise NotImplementedError("Plotting datasets w/ dim > 2 without Triangle not implemented")

        else:
            if hasTriangle:
                figure = triangle.corner(samples, labels=labels,
                                         quantiles=[0.16, 0.5, 0.84],
                                         show_titles=True, title_args={"fontsize": 12})
                if fname!=None:
                    plt.savefig('predicted_'+fname)
                
                figure = triangle.corner(self.X, labels=labels,
                                         quantiles=[0.16, 0.5, 0.84],
                                         show_titles=True, title_args={"fontsize": 12})
                if fname!=None:
                    plt.savefig('original_'+fname)

            elif samples.size[1]==2:
                f, ax = plt.subplots(2)
                ax[0].hist2d(samples[:,0], samples[:,1])
                ax[1].hist2d(self.X[:,0], self.X[:,1])
                ax[1].set_xlabel(labels[0])
                ax[1].set_ylabel(labels[1])
                plt.tight_layout()
                if fname!=None:
                    plt.savefig(fname)

            else:
                raise NotImplementedError("Plotting datasets w/ dim > 2 without Triangle not implemented")
예제 #22
0
파일: mcmc.py 프로젝트: psharda/gidget
def trianglePlot(restart, fn, burnIn=0):
    shp = np.shape(restart['chain'])
    prs = shp[0] * (shp[1] - burnIn) * shp[2]
    prior = [sampleFromPrior() for i in range(prs)]
    shape = np.shape(restart['chain'])
    ndim = shape[2]
    trifig = triangle.corner(restart['chain'][:,burnIn:,:].reshape((-1,ndim)), \
             labels=[r'$f_{g,0}$',r'$\mu_0$',r'$\alpha_\mu$', r'$r_\mathrm{acc}/r_\mathrm{vir}$', \
             r'$\epsilon_0$',r'$\alpha_z$',r'$\alpha_{M_h}$',r'$\epsilon_\mathrm{max}$',r'$f_\mathrm{cool}$'])
    trifigPrior = triangle.corner(prior,
                                  color='red',
                                  fig=trifig,
                                  plot_datapoints=False)
    trifig.savefig(fn)
def plot_grid_points(db='phoenix2016.db3',saveplot=False):
    # plot the grid points
    connection = sqlite.connect(db)
    sql_query = 'SELECT teff,logg,mh,alpha FROM parameter_sets'
    tab = pd.read_sql_query(sql_query,connection)

    connection.close()

    arr = np.array([np.array(tab['teff']),np.array(tab['logg']),np.array(tab['mh']),tab['alpha']])
    corner(tab,plot_contours=False,labels=['Teff','log g', '[M/H]', '[alpha/Fe]'])
    if saveplot:
        fname = os.path.basename(db)
        fname = os.path.splitext(fname)
        plt.savefig(fname[0]+'.pdf')
예제 #24
0
def make_chain_plots(model_filename):
    """
    Create analysis plots:
        - chain values vs. iteration number
        - histogram of chain values 
        - triangle plot of chains

    Args:
        model_filename (str): Name of input zipped model pickle file.
    """
    try:
        p_data = pickle.loads(gzip.open(model_filename).read())
    except UnicodeDecodeError:
        p_data = pickle.loads(gzip.open(model_filename).read(),
                              encoding="latin1")
    model = p_data["model"]
    params = p_data["comp_params"]

    #   We can do this intelligently based on number of iterations.
    samples = model.sampler.chain[:, 50:, :].reshape(
        (-1, model.total_parameter_count))

    if np.size(samples) == 0:
        print("WARNING, size of samples is 0! Exiting analysis code now...")
        exit()

    fig = triangle.corner(samples, labels=model.model_parameter_names())
    figname = "plots/{0}_triangle.png".format(model_filename)
    fig.savefig(figname)
    print("\tWrote {0}".format(figname))

    # First, calculate the median and confidence intervals
    #	where frac is the fraction of samples within the
    #	quoted uncertainties.  frac = 0.68 is the default.
    #	Columns are median, -error1, +error2.
    print(median_values(samples, frac=0.68))
    # Second, calculate the mean and standard deviation.
    #	Columns are mean, standard deviation.
    print(mean_values(samples))

    # Third, plot the MCMC chains as a function of iteration.
    #	You can easily tell if the chains are converged because you can
    #	no longer tell where the individual particle chains are sliced together.
    #	For testing, I saved 2000 iterations and ignored the first 1000.
    fig = plot_chains(samples, labels=model.model_parameter_names())
    figname = "plots/{0}_chain.png".format(model_filename)
    fig.savefig(figname)
    print("\tWrote {0}".format(figname))

    # Fourth, plot the posterior PDFs for each parameter.
    #	These are histograms of the MCMC chains.  We should add
    #	parameter names to this and the previous plots at some point.
    #	boxes = 20 is the default.
    fig = plot_posteriors(samples,
                          labels=model.model_parameter_names(),
                          boxes=20,
                          params=params)
    figname = "plots/{0}_posterior.png".format(model_filename)
    fig.savefig(figname)
    print("\tWrote {0}".format(figname))
예제 #25
0
    def triangle_plot(self,*args,**kwargs):
        
        if not args:
            args=self.keys
            
        assert len(args)>1
        
        labels=[]
        idx=[]
        for key in args:
            if key.startswith('_sigma_'):
                name=key.split('_sigma_')[1]
                label=r'$\sigma_{%s}$' % name
            else:
                namestr=key
                for g in greek:
                    if key.startswith(g):
                        namestr=r'\%s' % key

                label='$%s$' % namestr

            labels.append(label)
            idx.append(self.index[key])
        
        fig = triangle.corner(self.samples[:,idx], labels=labels)
예제 #26
0
파일: main.py 프로젝트: RuthAngus/flicker
def make_plots(whichx, fname):

    x, y, xerr, yerr = load_data(whichx)

    with h5py.File("%s_samples_%s.h5" % (whichx, fname)) as f:
        samp = f["samples"][...]
    m, c, sig = map(lambda v: (v[1], v[2] - v[1], v[1] - v[0]),
                    zip(*np.percentile(samp, [16, 50, 84], axis=0)))
    pars = [m[0], c[0], sig[0]]
    print pars

    plt.clf()
    plt.errorbar(x, y, xerr=xerr, yerr=yerr, fmt="k.", capsize=0, ecolor=".7")
    plt.plot(x, model1(pars, x), "k")
    ndraws = 100
    p0s = np.random.choice(samp[:, 0], ndraws)
    p1s = np.random.choice(samp[:, 1], ndraws)
    p2s = np.random.choice(samp[:, 2], ndraws)
    for i in range(ndraws):
        y = p0s[i] * x + p1s[i]
        plt.plot(x, (y + p2s[i]), "k", alpha=.1)
    plt.savefig("mcmc_%s_%s" % (whichx, fname))
    labels = ["$m$", "$c$", "$\sigma$"]
    plt.clf()
    fig = triangle.corner(samp, labels=labels)
    fig.savefig("triangle_%s_%s" % (whichx, fname))
예제 #27
0
파일: results.py 프로젝트: DiNAi/george
def results(fn):
    model, sampler = pickle.load(open(fn, "rb"))
    mu = np.median(model.f)
    ppm = lambda f: (f / mu - 1) * 1e6

    # Plot the data.
    fig = pl.figure(figsize=(6, 6))
    ax = fig.add_subplot(111)
    ax.plot(model.t, ppm(model.f), ".k")
    ax.set_xlim(np.min(model.t), np.max(model.t))
    ax.set_xlabel("time since transit [days]")
    ax.set_ylabel("relative flux [ppm]")
    fig.subplots_adjust(left=0.2, bottom=0.2, top=0.9, right=0.9)

    # Plot the predictions.
    samples = sampler.flatchain
    t = np.linspace(model.t.min(), model.t.max(), 1000)
    for i in np.random.randint(len(samples), size=10):
        model.vector = samples[i]
        ax.plot(t, ppm(model.predict(t)), color="#4682b4", alpha=0.5)

    fig.savefig(os.path.splitext(fn)[0] + "-results.pdf")

    # Plot the corner plot.
    fig = triangle.corner(samples, labels=model.labels,
                          truths=model.true_vector)
    fig.savefig(os.path.splitext(fn)[0] + "-triangle.png")
예제 #28
0
def plot(flatchain,
         base=args.outdir,
         triangle_plot=args.triangle,
         chain_plot=args.chain,
         lnprob=args.lnprob,
         clip_stellar='all',
         format=".pdf"):
    '''
    Make a bunch of plots to diagnose how the run went.
    '''

    import matplotlib
    matplotlib.rc("font", size=16)

    #Navigate the flatchain tree, and each time we encounter a flatchain, plot it.

    if flatchain.id == "stellar" and clip_stellar != "all":
        flatchain.clip_param(clip_stellar)

    params = flatchain.param_tuple
    samples = flatchain.samples
    labels = [label_dict.get(key, "unknown") for key in params]

    figure = triangle.corner(samples,
                             labels=labels,
                             quantiles=[0.16, 0.5, 0.84],
                             show_titles=True,
                             title_args={"fontsize": 16},
                             plot_contours=True,
                             plot_datapoints=False)
    figure.savefig(base + flatchain.id + format)
예제 #29
0
def animate_triangle(pos_T, labels=None, truths=None, samps_per_frame=10, fps=30, rough_length=10.0, outname='triangle.mp4'):
    from matplotlib import animation
    import triangle

    nframes, nwalkers, ndim = pos_T.shape

    final_bins = 50  #number of bins covering final posterior
    # Use last time step to get y-limits of histograms
    bins = []
    ymaxs = []
    for x in range(ndim):
        dx = (pos_T[-1,:,x].max() - pos_T[-1,:,x].min())/final_bins
        nbins = int((pos_T[0,:,x].max() - pos_T[0,:,x].min())/dx)
        bins.append(np.linspace(pos_T[0,:,x].min(), pos_T[0,:,x].max(), nbins+1)[:-1])
        hist, _ = np.histogram(pos_T[-1,:,x], bins=bins[-1], normed=True)
        ymaxs.append(1.1*max(hist))

    # Use the first time sample as the initial frame
    fig = triangle.corner(pos_T[0], labels=labels, plot_contours=False, truths=truths)
    axes = np.array(fig.axes).reshape((ndim, ndim))
    for x in range(ndim):
        axes[x,x].set_ylim(top=ymaxs[x])

    # Determine number of frames
    thin_factor = int(nframes/rough_length)/fps
    if thin_factor > 1:
        pos_T = pos_T[::thin_factor]
        samps_per_frame *= thin_factor
    samps_per_sec = fps * samps_per_frame

    # Make the movie
    anim = animation.FuncAnimation(fig, update_triangle, frames=xrange(len(pos_T)), blit=True,
                                             fargs=(pos_T, fig, bins, truths))
    return anim
예제 #30
0
def triangle_plot(fname, mcmc_result, flatchain, fig_labels):
    print 'yes'
    mres = np.array(mcmc_result)[:, 0]
    print 'mcmc_result = ', mres
    print len(mres), len(fig_labels)
    fig = triangle.corner(flatchain, truths=mres, labels=fig_labels)
    fig.savefig("triangle_%s.png" % fname)
예제 #31
0
def corner_plot(results, showpars=None, start=0, thin=1):
    #just wrap subtriangle
    """
    Make a triangle plot of the (thinned, latter) samples of the posterior
    parameter space.  Optionally make the plot only for a supplied subset
    of the parameters.
    """

    # pull out the parameter names and flatten the thinned chains
    parnames = np.array(results['model'].theta_labels())
    flatchain = results['chain'][:,start::thin,:]
    flatchain = flatchain.reshape(flatchain.shape[0] * flatchain.shape[1],
                                  flatchain.shape[2])
    truths = results['initial_center']

    # restrict to parameters you want to show
    if showpars is not None:
        ind_show = np.array([p in showpars for p in parnames], dtype= bool)
        flatchain = flatchain[:,ind_show]
        truths = truths[ind_show]
        parnames= parnames[ind_show]
        parlabels = [pardict[p] for p in parnames]
    fig = triangle.corner(flatchain, labels = parlabels,
                          quantiles=[0.16, 0.5, 0.84], verbose=False,
                          truths = truths)

    return fig
예제 #32
0
def plotTriangle(new_flatchain, field, objid, dataIDX, save_opt=None, copy_opt=None):
    import triangle
    import matplotlib.pylab as plt
    vNames = ['tau (dust2)','delta (dust_index)', 'Eb (uvb)', 'log10(age)', 
              'log10(M_star)']
    triangle_figure = triangle.corner(new_flatchain, labels=vNames,
                                      quantiles=[0.16, 0.5, 0.84],
                                      show_titles=True, title_args={"fontsize": 12},
                                      plot_contours=True,
                                      plot_datapoints=False,
                                      verbose=False
                                      )
                                    
    
    triangle_figure.gca().annotate(field+'-'+objid+', hunters index: '+str(dataIDX)+', after trimming', 
                        xy=(0.5, 1.0), xycoords="figure fraction",
                      xytext=(0, -5), textcoords="offset points",
                      ha="center", va="top")
    
                      
    if save_opt != None:
        triangle_figure.savefig(save_opt+'triangle.png')
        plt.close('all')
        if copy_opt != None:
            shutil.copyfile(save_opt+'triangle.png', copy_opt+'triangle-'+str(field)+'-'+str(objid)+'.png')
            
    else:
        triangle_figure.show()
예제 #33
0
파일: main.py 프로젝트: dfm/flicker
def make_plots(whichx, fname):

    x, y, xerr, yerr = load_data(whichx)

    with h5py.File("%s_samples.h5" % whichx) as f:
        samp = f["samples"][...]
    m, c, sig = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),
               zip(*np.percentile(samp, [16, 50, 84], axis=0)))
    pars = [m[0], c[0], sig[0]]
    print pars

    plt.clf()
    plt.errorbar(x, y, xerr=xerr, yerr=yerr, fmt="k.", capsize=0, ecolor=".7")
    plt.plot(x, model1(pars, x), "k")
    ndraws = 100
    p0s = np.random.choice(samp[:, 0], ndraws)
    p1s = np.random.choice(samp[:, 1], ndraws)
    p2s = np.random.choice(samp[:, 2], ndraws)
    for i in range(ndraws):
        y = p0s[i] * x + p1s[i]
        plt.plot(x, (y + p2s[i]), "k", alpha=.1)
    plt.savefig("mcmc_%s_%s" % (whichx, fname))
    labels = ["$m$", "$c$", "$\sigma$"]
    plt.clf()
    fig = triangle.corner(samp, labels=labels)
    fig.savefig("triangle_%s_%s" % (whichx, fname))
예제 #34
0
def plot_corner(posterior, percentiles, parvals=None):
    """
    Local version of a corner plot to allow bespoke truth values

    posterior: posterior object
    percentiles: percentiles to draw on 1D histograms
    parvals: dictionary of parameters with true values.  These parameters are
    used to select which params in posterior to use for the triangle plot, as
    well as to draw the target values

    """
    if parvals == None:
        print >> sys.stderr, "need param names and values"
    parnames = parvals.keys()

    parnames = filter(lambda x: x in posterior.names, parnames)
    truths = [parvals[p] for p in parnames]

    data = np.hstack([posterior[p].samples for p in parnames])
    extents = [get_extent(posterior, name, parvals) for name in parnames]

    trifig = triangle.corner(data,
                             labels=parnames,
                             truths=truths,
                             quantiles=percentiles,
                             truth_color='r',
                             extents=extents)

    return trifig
예제 #35
0
def MCMC(theta, x, y, yerr, M1, ecc, fname, n, sub, plot=False):

    nwalkers, ndim = 32, len(theta)
    p0 = [theta+1e-4*np.random.rand(ndim) for i in range(nwalkers)]
    sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob,
                                    args=(x, y, yerr, M1, ecc))
    p0, lp, state = sampler.run_mcmc(p0, 500)
    sampler.reset()
    p0, lp, state = sampler.run_mcmc(p0, 5000)

    if plot == True:
        fig_labels = ['P', 'M2', 'T0', 'V0', 'omega']
        flatchain = sampler.chain[:, 50:, :].reshape((-1, ndim))
        fig = triangle.corner(flatchain, truths=theta, labels=fig_labels)
        plt.savefig("%s_triangle" % fname)

    print "saving samples"
    f = h5py.File("%s/results/%s_%s_%s_samples" % (DIR, n, fname, sub), "w")
    data = f.create_dataset("samples", np.shape(sampler.chain))
    data[:, :] = np.array(sampler.chain)
    f.close()

    flat = sampler.chain[:, 50:, :].reshape((-1, ndim))
    mcmc_result = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),
                      zip(*np.percentile(flat, [16, 50, 84], axis=0)))
    np.savetxt("%s/results/%s_%s_%s_results.txt" % (DIR, n, fname, sub),
               mcmc_result)

    return mcmc_result
예제 #36
0
def run_EMCEE(initial_position, dimensions, walkers, step_number, burn):
        ndim, nwalkers = dimensions, walkers
        pos = [initial_position + 1e-2*np.random.randn(ndim) for i in range(nwalkers)]
        sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(x, y, yerr))
        # Clear and run the production chain.
        print("Running MCMC...")
        sampler.run_mcmc(pos, step_number, rstate0=np.random.get_state())
        print("Done.")
        plt.clf()
        fig, axes = plt.subplots(4, 1, sharex=True, figsize=(8, 9))
        axes[0].plot(sampler.chain[:, :, 0].T, color="k", alpha=0.4)
        axes[0].yaxis.set_major_locator(MaxNLocator(5))
        axes[0].axhline(T1_ml, color="#888888", lw=2)
        axes[0].set_ylabel("$T1$")
        axes[1].plot(sampler.chain[:, :, 1].T, color="k", alpha=0.4)
        axes[1].yaxis.set_major_locator(MaxNLocator(5))
        axes[1].axhline(P2_ml, color="#888888", lw=2)
        axes[1].set_ylabel("$P2$")
        axes[2].plot(sampler.chain[:, :, 2].T, color="k", alpha=0.4)
        axes[2].yaxis.set_major_locator(MaxNLocator(5))
        axes[2].axhline(T2_ml, color="#888888", lw=2)
        axes[2].set_ylabel("$T2$")
        axes[3].plot(sampler.chain[:, :, 3].T, color="k", alpha=0.4)
        axes[3].yaxis.set_major_locator(MaxNLocator(5))
        axes[3].axhline(P3_ml, color="#888888", lw=2)
        axes[3].set_ylabel("$P3$")
        fig.tight_layout(h_pad=0.0)
        fig.savefig("line-time_4_variables_"+str(counter)+".png")
        # Make the triangle plot.
        burnin = burn
        samples = sampler.chain[:, burnin:, :].reshape((-1, ndim))
        fig = triangle.corner(samples, labels=["$T1$","$P2$", "$T2$","$P3$"], truths=[T1_ml, P2_ml, T2_ml, P3_ml])
        fig.savefig("line-triangle_4_variables_"+str(counter)+".png")
예제 #37
0
def triangle_gen():
    plt.clf()
    plot_chi()
    burnIn = input('Burn off how many steps? ')
    samples = chainDat[:,burnIn:,:].reshape((-1,ndim))
    triangle.corner(samples, labels=['$R_{In}$ [AU]','$\Delta$R [AU]','log($M_{D}$ [$M_{Earth}$])','i [degrees]','PA [degrees]'], truths=[best_disk[0],best_disk[1],best_disk[2],best_disk[3],best_disk[4]], truth_color='g', quantiles=[0.16,.5,.84], show_titles=True, plot_contours=True)
    plt.savefig('MCMCRUNS/'+append+'/'+whatbywhat+'/'+disk_name+'_'+whatbywhat+'_'+append+'_Triangle.png')
    plt.show()
    
    values = np.zeros((ndim,3))
    for i in range(ndim):
            chainSqueeze=chainDat[:,burnIn:,i].reshape(-1)
        quantiles = mstats.mquantiles(chainSqueeze,prob=[.16,.5,.84],axis=None)
        values[i,0] = quantiles[1] #median
        values[i,1] = quantiles[2]-quantiles[1] #plus one sigma
        values[i,2] = quantiles[0]-quantiles[1] #minus one sigma
 def triangle_plot(self, labels=None, truths=None):
     samples = self.model.get_burned_in_samples()
     if labels is None:
         labels = greek_label_mapping(self.model.all_param_names)
     fig = triangle.corner(samples,
         labels=labels,
         truths=truths)
예제 #39
0
def sample(ndim, nwalkers, nsteps, start, w, ur, sigma_ur, nuvu, sigma_nuvu, age, pd, ps):
    if len(age) != len(ur):
        raise SystemExit("Number of ages does not coincide with number of galaxies...")
    p0 = [start + 1e-4 * N.random.randn(ndim) for i in range(nwalkers)]
    sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(w, ur, sigma_ur, nuvu, sigma_nuvu, age, pd, ps))
    # burn in
    pos, prob, state = sampler.run_mcmc(p0, 50)
    # reset and run with last positions of burn in run
    sampler.reset()
    print "RESET", pos
    sampler.run_mcmc(pos, nsteps)
    samples = sampler.chain[:, :, :].reshape((-1, ndim))
    samples_save = (
        "/Users/becky/Projects/Green-Valley-Project/bayesian/find_t_tau/gv/not_clean/samples_gv_not_clean_"
        + str(len(samples))
        + "_"
        + str(len(age))
        + "_"
        + str(time.strftime("%H_%M_%d_%m_%y"))
        + ".npy"
    )
    N.save(samples_save, samples)
    fig = triangle.corner(samples, labels=[r"$ t_{smooth} $", r"$ \tau_{smooth} $", r"$ t_{disc} $", r"$ \tau_{disc}$"])
    fig.savefig(
        "triangle_t_tau_gv_not_clean_"
        + str(len(samples))
        + "_"
        + str(len(age))
        + "_"
        + str(time.strftime("%H_%M_%d_%m_%y"))
        + ".pdf"
    )
    return samples, fig
예제 #40
0
파일: results.py 프로젝트: zdn123/george
def results(fn):
    model, sampler = pickle.load(open(fn, "rb"))
    mu = np.median(model.f)
    ppm = lambda f: (f / mu - 1) * 1e6

    # Plot the data.
    fig = pl.figure(figsize=(6, 6))
    ax = fig.add_subplot(111)
    ax.plot(model.t, ppm(model.f), ".k")
    ax.set_xlim(np.min(model.t), np.max(model.t))
    ax.set_xlabel("time since transit [days]")
    ax.set_ylabel("relative flux [ppm]")
    fig.subplots_adjust(left=0.2, bottom=0.2, top=0.9, right=0.9)

    # Plot the predictions.
    samples = sampler.flatchain
    t = np.linspace(model.t.min(), model.t.max(), 1000)
    for i in np.random.randint(len(samples), size=10):
        model.vector = samples[i]
        ax.plot(t, ppm(model.predict(t)), color="#4682b4", alpha=0.5)

    fig.savefig(os.path.splitext(fn)[0] + "-results.pdf")

    # Plot the corner plot.
    fig = triangle.corner(samples,
                          labels=model.labels,
                          truths=model.true_vector)
    fig.savefig(os.path.splitext(fn)[0] + "-triangle.png")
예제 #41
0
def bayesian_fitting(x, y, y_error, distance, nwalkers, ndim, cores, nburn,
                     nsteps, galaxy_name):
    np.random.seed(0)
    print('Number of free parameters in our model =', ndim)
    starting_guesses = np.random.rand(nwalkers, ndim)
    starting_guesses[:, 0] *= 5
    starting_guesses[:, 1] *= 25
    starting_guesses[:, 2] *= 2.5
    pool = Pool(cores)
    sampler = emcee.EnsembleSampler(nwalkers,
                                    ndim,
                                    log_post_one_with_beta,
                                    args=[x, y, y_error, distance],
                                    pool=pool)
    sampler.run_mcmc(starting_guesses, nsteps)
    sample = sampler.chain  # shape = (nwalkers, nsteps, ndim)
    sample = sampler.chain[:, nburn:, :].reshape(-1, ndim)
    logposteriors = sampler.lnprobability[:, nburn:].reshape(-1)
    answers = np.mean(sample[np.where(logposteriors == np.max(logposteriors))],
                      0).reshape(ndim)
    figure = triangle.corner(sample,
                             labels=['Log(Dust Mass)', 'Temp', 'Beta'],
                             truths=answers)
    figure.savefig("triangle_three_dims_" + galaxy_name + ".png", format='png')
    sampler.pool.terminate()
    plt.clf()
    plt.close()
    return answers
예제 #42
0
def _run_corner(nm, pandas=False, N=10000, seed=1234, ndim=3, ret=False,
                factor=None, **kwargs):
    print(" .. {0}".format(nm))

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

    np.random.seed(seed)
    data1 = np.random.randn(ndim*4*N/5.).reshape([4*N/5., ndim])
    data2 = (5 * np.random.rand(ndim)[None, :]
             + np.random.randn(ndim*N/5.).reshape([N/5., ndim]))
    data = np.vstack([data1, data2])
    if factor is not None:
        data[:, 0] *= factor
        data[:, 1] /= factor
    if pandas:
        data = pd.DataFrame.from_items(zip(map("d{0}".format, range(ndim)),
                                           data.T))

    fig = triangle.corner(data, **kwargs)
    fig.savefig(os.path.join(FIGURE_PATH, "triangle_{0}.png".format(nm)))
    if ret:
        return fig
    else:
        pl.close(fig)
예제 #43
0
def triangle_plot(fname, mcmc_result, flatchain, fig_labels):
    print 'yes'
    mres = np.array(mcmc_result)[:, 0]
    print 'mcmc_result = ', mres
    print len(mres), len(fig_labels)
    fig = triangle.corner(flatchain, truths=mres, labels=fig_labels)
    fig.savefig("triangle_%s.png" % fname)
예제 #44
0
def sample_lnprob(weight_index):
    import emcee

    ndim = 4
    nwalkers = 8 * ndim
    print("using {} walkers".format(nwalkers))
    p0 = np.vstack((np.random.uniform(-0.5, 2, size=(1, nwalkers)),
                    np.random.uniform(50, 300, size=(1, nwalkers)),
                    np.random.uniform(0.2, 1.5, size=(1, nwalkers)),
                    np.random.uniform(0.2, 1.5, size=(1, nwalkers)))).T

    sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(weight_index,), threads=cfg['threads'])

    print("Running Sampler")
    pos, prob, state = sampler.run_mcmc(p0, cfg['burn_in'])

    print("Burn-in complete")
    sampler.reset()
    sampler.run_mcmc(pos, cfg['samples'])

    samples = sampler.flatchain
    np.save(cfg['outdir'] + "samples_w{}.npy".format(weight_index), samples)

    import triangle
    fig = triangle.corner(samples)
    fig.savefig(cfg['outdir'] + "triangle_w{}.png".format(weight_index))
예제 #45
0
    def plot_PDFtriangle(self, parameterset, labels):

        if parameterset == '10pars':
            figure = triangle.corner(self.chain.flatchain,
                                     labels=labels,
                                     plot_contours=True,
                                     plot_datapoints=False,
                                     show_titles=True,
                                     quantiles=[0.16, 0.50, 0.84])
        elif parameterset == 'int_lums':
            figure = triangle.corner(self.int_lums.T,
                                     labels=labels,
                                     plot_contours=True,
                                     plot_datapoints=False,
                                     show_titles=True,
                                     quantiles=[0.16, 0.50, 0.84])
        return figure
예제 #46
0
 def weights_triangle_plot(self, labels=None, thin=1):
     samples = self.model.get_burned_in_samples()
     if labels is None:
         labels = ["w%d" % idx for idx in range(len(self.model.fit_models))]
         #labels = greek_label_mapping(labels)
         print(labels)
     fig = triangle.corner(samples[::thin,
                                   -len(self.model.fit_models):])  #,
예제 #47
0
def thumbPlot(chain, labels, **kwargs):
    seaborn.set(style='ticks')
    seaborn.set_style({"xtick.direction": "in", "ytick.direction": "in"})
    fig = triangle.corner(chain,
                          labels=labels,
                          bins=50,
                          label_kwargs=dict(fontsize=18),
                          **kwargs)
    return fig
예제 #48
0
    def triangle_plot(self):
        samples = self.model.get_burned_in_samples()

        labels = [
            "$%s$" % param_name for model in self.model.fit_models
            for param_name in model.all_param_names
        ]
        labels.extend(["$%s$" for model in self.model.fit_models])
        fig = triangle.corner(samples, labels=labels)
예제 #49
0
def MCMC(theta, x, y, yerr, fname, burn_in, nsteps, nruns):

    # calculate initial likelihood and plot initial hparams
    xs = np.linspace(min(x), max(x), 1000)
    k = theta[0] * ExpSquaredKernel(theta[1]) * ExpSine2Kernel(theta[2], theta[4])
    k += WhiteKernel(theta[3])
    gp = george.GP(k)
    print 'initial lnlike = ', lnlike(theta, x, y, yerr)
    mu, cov = predict(theta, xs, x, y, yerr)
    plt.clf()
    plt.errorbar(x, y, yerr=yerr, fmt='k.', capsize=0)
    plt.plot(xs, mu, 'r')
    std = np.sqrt(np.diag(cov))
#     plt.fill_between(mu-std, mu+std, color='r', alpha='.5')
    plt.savefig('%s_init' % fname)

    # setup sampler
    nwalkers, ndim = 32, len(theta)
    p0 = [theta+1e-4*np.random.rand(ndim) for i in range(nwalkers)]
    args = [x, y, yerr]
    sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=args)

    print("Burning in...")
    p0, lp, state = sampler.run_mcmc(p0, burn_in)
    sampler.reset()

    for i in range(nruns):

        print 'Running... ', i
        p0, lp, state = sampler.run_mcmc(p0, nsteps)

        # results
        samples = sampler.chain[:, 50:, :].reshape((-1, ndim))
        mcmc_result = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),
                          zip(*np.percentile(samples, [16, 50, 84], axis=0)))
        mres = np.array(mcmc_result)[:, 0]
        print 'mcmc_result = ', np.exp(mres)
        np.savetxt("parameters_%s.txt" % fname, np.array(mcmc_result))

        print "saving samples"
        f = h5py.File("samples%s" % fname, "w")
        data = f.create_dataset("samples", np.shape(sampler.chain))
        data[:,:] = np.array(sampler.chain)
        f.close()

    # make triangle plot
    fig_labels = ["$A$", "$l1$", "$l2$", "$wn$", "$P$"]
    fig = triangle.corner(samples, truths=mres, labels=fig_labels)
    fig.savefig("triangle_%s.png" % fname)

    # plot result
    mu, cov = predict(mres, xs, x, y, yerr)
    plt.clf()
    plt.errorbar(x, y, yerr=yerr, fmt='k.', capsize=0)
    plt.plot(xs, mu, 'r')
    plt.savefig('%s_final' % fname)
예제 #50
0
def make_plot(sampler,
              x,
              y,
              yerr,
              ID,
              DIR,
              traces=False,
              tri=False,
              prediction=True):

    nwalkers, nsteps, ndims = np.shape(sampler)
    flat = np.reshape(sampler, (nwalkers * nsteps, ndims))
    mcmc_result = map(lambda v: (v[1], v[2] - v[1], v[1] - v[0]),
                      zip(*np.percentile(flat, [16, 50, 84], axis=0)))
    mcmc_result = np.array([i[0] for i in mcmc_result])
    print("\n", np.exp(np.array(mcmc_result[-1])), "period (days)", "\n")
    print(mcmc_result)
    np.savetxt("%s/%s_result.txt" % (DIR, ID), mcmc_result)

    fig_labels = ["A", "l", "G", "s", "P"]

    if traces:
        print("Plotting traces")
        for i in range(ndims):
            plt.clf()
            plt.plot(sampler[:, :, i].T, 'k-', alpha=0.3)
            plt.ylabel(fig_labels[i])
            plt.savefig("%s/%s_%s.png" % (DIR, ID, fig_labels[i]))

    if tri:
        print("Making triangle plot")
        flat[:, -1] = np.exp(flat[:, -1])
        try:
            fig = corner.corner(flat, labels=fig_labels)
        except:
            fig = triangle.corner(flat, labels=fig_labels)
        fig.savefig("%s/%s_triangle" % (DIR, ID))
        print("%s/%s_triangle.png" % (DIR, ID))

    if prediction:
        print("plotting prediction")
        theta = np.exp(np.array(mcmc_result))
        k = theta[0] * ExpSquaredKernel(theta[1]) \
                * ExpSine2Kernel(theta[2], theta[4])
        gp = george.GP(k, solver=george.HODLRSolver)
        gp.compute(x, yerr)
        xs = np.linspace(x[0], x[-1], 1000)
        mu, cov = gp.predict(y, xs)
        plt.clf()
        plt.errorbar(x - x[0], y, yerr=yerr, **reb)
        plt.xlabel("$\mathrm{Time~(days)}$")
        plt.ylabel("$\mathrm{Normalised~Flux}$")
        plt.plot(xs, mu, color=cols.lightblue)
        plt.xlim(min(x), max(x))
        plt.savefig("%s/%s_prediction" % (DIR, ID))
        print("%s/%s_prediction.png" % (DIR, ID))
예제 #51
0
def plot_global(nsne, under_model):
    samples = np.load("samples_%s/globalsamples_%s.npy"%(under_model.name(nsne),under_model.name(nsne)))

    labels = under_model.labels 
    truths = under_model.initial

    fig = triangle.corner(samples, labels=labels, bins=15,
                      truths=truths)
    plt.savefig('globalsamples_%s.png'%(under_model.name(nsne)))
    return
예제 #52
0
def plot_triangle(model, quantiles=[0.16, 0.5, 0.84]):

    import triangle
    seaborn.set_style('ticks')
    fixed  = np.array([model.fixed[n] for n in model.param_names])
    labels = [tex_names[n] for n in np.array(model.param_names)[~fixed]]

    fig = triangle.corner(model.chain_nb, quantiles=quantiles,
                          labels=labels, verbose=False, plot_contours=False)
    seaborn.despine()
    return fig
예제 #53
0
def MCMC(theta, x, y, yerr, bm, bp):

    # Sample the posterior probability for m.
    nwalkers, ndim = 64, len(theta)
    p0 = [theta + 1e-4 * np.random.rand(ndim) for i in range(nwalkers)]
    sampler = emcee.EnsembleSampler(nwalkers,
                                    ndim,
                                    lnprob,
                                    args=(x, y, yerr, bm, bp))
    bi, pr = 200, 2000
    start = time.clock()
    print("Burn-in")
    p0, lp, state = sampler.run_mcmc(p0, bi)
    sampler.reset()
    print("Production run")
    sampler.run_mcmc(p0, pr)
    elapsed = time.clock() - start
    print 'time = ', elapsed / 60., 'mins'

    print("Making triangle plots")
    fig_labels = ["$A$", "$l_2$", "$l_1$", "$s$", "$P$"]
    fig = triangle.corner(sampler.flatchain,
                          truths=theta,
                          labels=fig_labels[:len(theta)])
    fig.savefig("triangle.png")

    print("Plotting traces")
    pl.figure()
    for i in range(ndim):
        pl.clf()
        pl.axhline(theta[i], color="r", zorder=2)
        pl.plot(sampler.chain[:, :, i].T, 'k-', alpha=0.3, zorder=1)
        pl.savefig("{0}.png".format(i))

    # Flatten chain
    samples = sampler.chain[:, 50:, :].reshape((-1, ndim))

    # Find values
    mcmc_result = map(lambda v: (v[1], v[2] - v[1], v[1] - v[0]),
                      zip(*np.percentile(samples, [16, 50, 84], axis=0)))

    theta = np.array(mcmc_result)[:, 0]
    print 'mcmc result = ', theta

    like = lnlike(theta, x, y, yerr)
    print "Final lnlike = ", like

    # plot mcmc result
    pl.clf()
    pl.errorbar(x, y, yerr=yerr, fmt='k.')
    xs = np.arange(min(x), max(x), 0.01)
    pl.plot(xs, predict(xs, x, y, yerr, theta, theta[4])[0], 'r-')
    pl.xlabel('time (days)')
    pl.savefig('result')
예제 #54
0
 def triangle_plot(self, labels=None):
     samples = self.model.get_burned_in_samples()
     if labels is None:
         labels = [
             "$%s$" % (param_name) for model in self.model.fit_models
             for param_name in model.function_params
         ]
         labels.append("$sigma$")
         labels.extend(
             ["$w%d$" % idx for idx in range(len(self.model.fit_models))])
         labels = greek_label_mapping(labels)
     fig = triangle.corner(samples, labels=labels)
예제 #55
0
def vis2hammer(cpo,ivar=[1., 0.5],ndim=2,nwalcps=50,plot=False,nsteps=1000,burnin=100,paramlimits=[0.5,5.,0.,1.],muprior=[0.64,0.03]):

	'''Default implementation of emcee, the MCMC Hammer, for closure phase
	fitting. Requires a closure phase object cpo, and is best called with 
	ivar chosen to be near the peak - it can fail to converge otherwise.'''

	def lnprior(params):
	    if paramlimits[0] < params[0] < paramlimits[1] and paramlimits[2] < params[1] < paramlimits[3]:
	        return -0.5*((params[1]-muprior[0])/muprior[1])**2. #0.0
	    return -np.inf

	def lnprob(params,u,v,wavels,visdata,viserr):
		return lnprior(params) + vis_loglikelihood(params,u,v,wavels,visdata,viserr)

	ivar = np.array(ivar)  # initial parameters for model-fit

	p0 = [ivar + 0.1*ivar*np.random.rand(ndim) for i in range(nwalcps)] # initialise walcps in a ball

	print 'Running emcee now!'

	t0 = time.time()

	sampler = emcee.EnsembleSampler(nwalcps, ndim, lnprob, args=[cpo.u,cpo.v,cpo.wavels,cpo.vis2,cpo.vis2err])
	
	# burn in
	pos,prob,state = sampler.run_mcmc(p0, burnin)
	print 'Burnt in'
	sampler.reset()

	# restart
	sampler.run_mcmc(pos,nsteps)
	tf = time.time()

	print 'Time elapsed =', tf-t0,'s'

	ds = sampler.flatchain[:,0]
	mus = sampler.flatchain[:,1]

	meand = np.mean(ds)
	derr = np.std(ds)

	meanmu = np.mean(mus)
	dmu = np.std(mus)

	print 'Diameter',meand,'pm',derr,'mas'
	print 'Limb-Darkening',meanmu,'pm',dmu

	if plot==True:
		fig = triangle.corner(sampler.flatchain, labels=["$d$ (mas)", "$\mu$"])
		plt.show()

	return sampler.flatchain