コード例 #1
0
    def cornerplot(self, **kwargs):

        dyplot.cornerplot(results=self.samples.results,
                          labels=self.model.parameter_labels_latex,
                          **kwargs)

        self.output.to_figure(structure=None, auto_filename="cornerplot")
コード例 #2
0
ファイル: plotting.py プロジェクト: arm61/model_select
 def plot_for_all(self):
     """
     Plot the corner plot where all parameters are varied.
     """
     file_h5 = h5py.File('output/model^th_mvh_tt_phit_mvt_rough.h5', 'r')
     axes = plt.subplots(figsize=(10, 5))[1]
     axes.errorbar(
         range(1,
               len(file_h5['logz']) + 1),
         file_h5['logz'],
         file_h5['logzerr'],
         c=list(fig_params.TABLEAU.values())[0],
     )
     axes.set_xscale('log')
     axes.set_xlabel(r'Samples')
     axes.set_ylabel(r'$\ln\{p(\mathbf{D}|H)\}$')
     plt.savefig('paper/iterations.pdf')
     plt.close()
     plt.subplots(figsize=(11.25, 11.25))
     plotting.cornerplot(
         file_h5,
         labels=self.units_p[-1],
         color=list(fig_params.TABLEAU.values())[0],
     )
     plt.savefig('paper/post_all.pdf')
     plt.close()
コード例 #3
0
def draw_cornerplot(results,
                    ndim,
                    labels=None,
                    truths=None,
                    figsize=(16, 14),
                    save=False,
                    save_dir='.',
                    suffix=''):
    from dynesty import plotting as dyplot

    fig = plt.subplots(ndim, ndim, figsize=figsize)
    dyplot.cornerplot(results,
                      truths=truths,
                      labels=labels,
                      color="royalblue",
                      truth_color="indianred",
                      title_kwargs={
                          'fontsize': 18,
                          'y': 1.04
                      },
                      label_kwargs={'fontsize': 16},
                      show_titles=True,
                      fig=fig)

    if save:
        plt.savefig(os.path.join(save_dir, "Cornerplot%s.png" % suffix),
                    dpi=150)
        plt.show()
        plt.close()
    else:
        plt.show()
コード例 #4
0
 def cornerplot(self, truths=None, labels=None, figsize=(12,10)):
     
     fig, axes = plt.subplots(self.ndim, self.ndim,
                              figsize=figsize)
     
     dyplot.cornerplot(self.results, truths=truths, labels=labels, 
                       color="royalblue", truth_color="indianred",
                       title_kwargs={'fontsize':18, 'y': 1.04},
                       label_kwargs={'fontsize':16}, show_titles=True,
                       fig=(fig, axes))
コード例 #5
0
    def cornerplot(self, **kwargs):

        if conf.instance["general"]["test"]["test_mode"]:
            return None

        dyplot.cornerplot(results=self.samples.results,
                          labels=self.model.parameter_labels_latex,
                          **kwargs)

        self.output.to_figure(structure=None, auto_filename="cornerplot")
        self.mat_plot_1d.figure.close()
コード例 #6
0
ファイル: plotting.py プロジェクト: arm61/model_select
 def plot_for_any(self, vars, i):
     """
     Plot the corner plot where all parameters are varied.
     """
     file_h5 = h5py.File('output/model^{}.h5'.format(vars), 'r')
     plt.subplots(figsize=(11.25, 11.25))
     plotting.cornerplot(
         file_h5,
         labels=self.units_p[i],
         color=list(fig_params.TABLEAU.values())[0],
     )
     plt.savefig('paper/post_{}.pdf'.format(vars))
     plt.close()
コード例 #7
0
ファイル: plotting.py プロジェクト: arm61/model_select
 def plot_correlation_best(self):
     """
     Plot the corner plot for the best evidence.
     """
     self.best_file = self.variables_p[np.argsort(self.logz)[-1]]
     file_h5 = h5py.File('output/model^{}.h5'.format(self.best_file), 'r')
     plt.subplots(figsize=(11.25, 11.25))
     plotting.cornerplot(
         file_h5,
         labels=self.units_p[np.argsort(self.logz)[-1]],
         color=list(fig_params.TABLEAU.values())[0],
     )
     plt.savefig('paper/post_best.pdf')
     plt.close()
コード例 #8
0
def test_gaussian():
    logz_tol = 1
    sampler = dynesty.NestedSampler(loglikelihood_gau,
                                    prior_transform_gau,
                                    ndim_gau,
                                    nlive=nlive)
    sampler.run_nested(print_progress=printing)

    # add samples
    # check continuation behavior
    sampler.run_nested(dlogz=0.1, print_progress=printing)

    # get errors
    nerr = 2
    for i in range(nerr):
        sampler.reset()
        sampler.run_nested(print_progress=False)
        results = sampler.results
        pos = results.samples
        wts = np.exp(results.logwt - results.logz[-1])
        mean, cov = dyfunc.mean_and_cov(pos, wts)
        logz = results.logz[-1]
        assert (np.abs(logz - logz_truth_gau) < logz_tol)
    # check summary
    res = sampler.results
    res.summary()

    # check plots
    dyplot.runplot(sampler.results)
    plt.close()
    dyplot.traceplot(sampler.results)
    plt.close()
    dyplot.cornerpoints(sampler.results)
    plt.close()
    dyplot.cornerplot(sampler.results)
    plt.close()
    dyplot.boundplot(sampler.results,
                     dims=(0, 1),
                     it=3000,
                     prior_transform=prior_transform_gau,
                     show_live=True,
                     span=[(-10, 10), (-10, 10)])
    plt.close()
    dyplot.cornerbound(sampler.results,
                       it=3500,
                       prior_transform=prior_transform_gau,
                       show_live=True,
                       span=[(-10, 10), (-10, 10)])
    plt.close()
コード例 #9
0
ファイル: plotting.py プロジェクト: NGC4676/elderflower
def draw_cornerplot(results,
                    dims,
                    labels=None,
                    truths=None,
                    figsize=(16, 14),
                    save=False,
                    save_dir='.',
                    suffix='',
                    **kwargs):
    from dynesty import plotting as dyplot

    nsamps, ndim = results.samples.shape

    # if truth is given, show all dimensions
    if truths is not None:
        dims = None

    # show subsets of dimensions
    if dims is not None:
        labels = labels[1:]
        ndim = ndim - 1

    fig = plt.subplots(ndim, ndim, figsize=figsize)
    plot_kw = {
        'color': "royalblue",
        'truth_color': "indianred",
        'dims': dims,
        'truths': truths,
        'labels': labels,
        'title_kwargs': {
            'fontsize': 16,
            'y': 1.04
        },
        'title_fmt': '.3f',
        'show_titles': True,
        'label_kwargs': {
            'fontsize': 16
        }
    }
    plot_kw.update(kwargs)

    fg, axes = dyplot.cornerplot(results, fig=fig, **plot_kw)

    if save:
        plt.savefig(os.path.join(save_dir, "Cornerplot%s.png" % suffix),
                    dpi=120)
        plt.show()
        plt.close()
    else:
        return fg, axes
コード例 #10
0
ファイル: dynesty_gaussian.py プロジェクト: keflavich/nestfit
def plot_corner(dsampler):
    mix = test_mixture()
    truths = np.concatenate(
        (mix.amp.flatten(), mix.cen.flatten(), mix.std.flatten()))
    labels = ['a1', 'a2', 'a3', 'c1', 'c2', 'c3', 's1', 's2', 's3']
    plt.rc('font', size=6, family='serif')
    plt.rc('xtick', direction='in', top=True)
    plt.rc('ytick', direction='in', right=True)
    fig, axes = dyplot.cornerplot(dsampler.results,
                                  show_titles=True,
                                  labels=labels,
                                  fig=plt.subplots(9, 9, figsize=(8, 8)))
    plt.savefig(PLOT_DIR / Path('test_corner_3gauss.pdf'))
    plt.close('all')
コード例 #11
0
 def plot_unweighted(weightedouput, population):
     """
     Produces the corner, trace and runplot for the results out of the bayesian analysis
     using the individual populations without normalisation.
     input:
         weightedoutput: Result dictionary from the bayesian analysis.
         population: Label of the population to used.
     """
     fig1, _ = dyplot.cornerplot(weightedouput, color='blue', \
                 truth_color='black', show_titles=True, max_n_ticks=3, quantiles=None)
     fig2, _ = dyplot.traceplot(weightedouput, truth_color='black', \
                  show_titles=True, trace_cmap='viridis')
     fig3, _ = dyplot.runplot(weightedouput, lnz_error=False)
     fig1.savefig("./output/"+population+"_corner_unweighted.png")
     fig2.savefig("./output/"+population+"_trace_unweighted.png")
     fig3.savefig("./output/"+population+"_runplot_unweighted.png")
コード例 #12
0
ファイル: plot_utils.py プロジェクト: astroshrey/ttvnest
def dynesty_plots(system, truthvals = None, outname = None):
	if system.results == None:
		raise ValueError("No retrieval found in your system object!")

	if outname is not None:
		names = [outname + '_cornerplot.png',
			outname + '_traceplot.png',
			outname + '_runplot.png']
	#cornerplot
	plt.figure(figsize = (20, 20))
	cfig, caxes = dyplot.cornerplot(system.results, color = 'blue',
		max_n_ticks = 3, labels = system.fit_param_names,
		truths = truthvals)
	if outname == None:
		plt.show()
	else:
		plt.savefig(names[0])
		plt.close('all')

	#traceplot
	plt.figure(figsize = (20, 20))
	tfig, taxes = dyplot.traceplot(system.results, truths = truthvals,
		show_titles = True, trace_cmap = 'viridis',
		labels = system.fit_param_names)
	if outname == None:
		plt.show()
	else:
		plt.savefig(names[1])
		plt.close('all')

	#runplot
	try:
		plt.figure(figsize = (20, 20))
		rfig, raxes = dyplot.runplot(system.results)
		if outname == None:
			plt.show()
		else:
			plt.savefig(names[2])
			plt.close('all')
			return names
	except ValueError:
		plt.close('all')
		print("Axis limits error on runplot; internal to dynesty")
		pass

	return None
コード例 #13
0
 def run(self, aposcale=6., psbin=20, lmin=None, lmax=None, kwargs=dict()):
     self.preprocess(aposcale, psbin, lmin, lmax)
     result = self.analyse(kwargs)
     # visualise data and result
     bestpar = None
     bestbp = None
     if self._solver == 'dynesty':
         from dynesty import plotting as dyplot
         fig, ax = dyplot.cornerplot(result,
                                     labels=self._paramlist,
                                     quantiles=[0.025, 0.5, 0.975],
                                     color='midnightblue',
                                     title_fmt='.3f',
                                     show_titles=1,
                                     smooth=0.04)
         plt.savefig('posterior.pdf')
         bestpar = result.samples[np.where(
             result['logl'] == max(result['logl']))][0]
     elif self._solver == 'emcee':
         import corner
         fig = corner.corner(result, labels=self._paramlist)
         plt.savefig('posterior.pdf')
         bestpar = np.median(result, axis=0)
     elif self._solver == 'minuit':
         print('params: {}\n bestfit: {}\n stderr: {}'.format(
             self._paramlist, result[0], result[1]))
         bestpar = result[0]
     else:
         raise ValueError('unsupported solver: {}'.format(self._solver))
     for i in range(len(bestpar)):
         if self._foreground_obj is not None:
             self._foreground_obj.reset({self._paramlist[i]: bestpar[i]})
         if self._background_obj is not None:
             self._background_obj.reset({self._paramlist[i]: bestpar[i]})
     if self._foreground_obj is None:
         bestbp = self._background_obj.bandpower()
     elif self._background_obj is None:
         bestbp = self._foreground_obj.bandpower()
     else:
         bestbp = self._foreground_obj.bandpower(
         ) + self._background_obj.bandpower()
     self.plot_result(bestbp)
     self.plot_residule(bestbp)
     return result
コード例 #14
0
 def drun(self, dlogz=0.1, ndim=4):
     """ simple main function for sampling. """
     # initialize our nested sampler
     sampler = dynesty.NestedSampler(self.log_likelihood, likemod.pt_te,
                                     ndim)
     sampler.run_nested(dlogz=dlogz)
     self.res = sampler.results
     self.res.summary()
     fig, _ = dyplot.runplot(self.res, lnz_error=False)
     fig1, _ = dyplot.traceplot(self.res, truths=np.zeros(ndim), \
                                 truth_color='black', show_titles=True, \
                                 trace_cmap='viridis', connect=True, \
                                 connect_highlight=range(10))
     fig2, _ = dyplot.cornerplot(self.res, color='blue', \
                             truth_color='black', show_titles=True, \
                             max_n_ticks=3, quantiles=None)
     fig.savefig('./output/evidence.png')
     fig1.savefig('./output/tracers.png')
     fig2.savefig('./output/cornerplot.png')
コード例 #15
0
def pyorbit_dynesty(config_in, input_datasets=None, return_output=None):

    output_directory = './' + config_in['output'] + '/dynesty/'

    mc = ModelContainerDynesty()
    pars_input(config_in, mc, input_datasets)

    if mc.nested_sampling_parameters['shutdown_jitter']:
        for dataset in mc.dataset_dict.itervalues():
            dataset.shutdown_jitter()

    mc.model_setup()
    mc.create_variables_bounds()
    mc.initialize_logchi2()

    mc.create_starting_point()

    results_analysis.results_resumen(mc, None, skip_theta=True)

    mc.output_directory = output_directory

    print()
    print('Reference Time Tref: ', mc.Tref)
    print()
    print('*************************************************************')
    print()

    import dynesty

    # "Standard" nested sampling.
    sampler = dynesty.NestedSampler(mc.dynesty_call, mc.dynesty_priors,
                                    mc.ndim)
    sampler.run_nested()
    results = sampler.results

    # "Dynamic" nested sampling.
    dsampler = dynesty.DynamicNestedSampler(mc.dynesty_call, mc.dynesty_priors,
                                            mc.ndim)
    dsampler.run_nested()
    dresults = dsampler.results

    from dynesty import plotting as dyplot

    # Plot a summary of the run.
    rfig, raxes = dyplot.runplot(results)

    # Plot traces and 1-D marginalized posteriors.
    tfig, taxes = dyplot.traceplot(results)

    # Plot the 2-D marginalized posteriors.
    cfig, caxes = dyplot.cornerplot(results)

    from dynesty import utils as dyfunc

    # Extract sampling results.
    samples = results.samples  # samples
    weights = np.exp(results.logwt - results.logz[-1])  # normalized weights

    # Compute 5%-95% quantiles.
    quantiles = dyfunc.quantile(samples, [0.05, 0.95], weights=weights)

    # Compute weighted mean and covariance.
    mean, cov = dyfunc.mean_and_cov(samples, weights)

    # Resample weighted samples.
    samples_equal = dyfunc.resample_equal(samples, weights)

    # Generate a new set of results with statistical+sampling uncertainties.
    results_sim = dyfunc.simulate_run(results)
    """ A dummy file is created to let the cpulimit script to proceed with the next step"""
    nested_sampling_create_dummy_file(mc)

    if return_output:
        return mc
    else:
        return
コード例 #16
0
def main(args=None):
    if args.results == "none":
        ndim = len(JOINT_PRIOR)
        date = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        image_positions = pd.read_csv(args.image_positions)
        pprint(image_positions)
        # A=0, B=1, C=2, D=3
        x_image = image_positions["theta_x"].to_numpy()
        y_image = image_positions["theta_y"].to_numpy()
        quad_model = QuadPseudoNIELensModel(x_image, y_image, args.plate_scale)
        time_delays = pd.read_csv(args.time_delays)
        pprint(time_delays)

        # Expected: DA=0, DB=1, DC=2 (index)
        sigma_t = time_delays["sigma"].to_numpy()  # units of days
        delta_t = time_delays["delta_t"].to_numpy()
        joint_loglikelihood = joint_loglikelihood_func(quad_model, delta_t,
                                                       sigma_t)

        with Pool(args.nworkers) as pool:
            sampler = DynamicNestedSampler(joint_loglikelihood,
                                           joint_prior_transform,
                                           ndim,
                                           pool=pool,
                                           nlive=args.nlive,
                                           queue_size=args.nworkers)
            sampler.run_nested()
            res = sampler.results

        # save results
        with open(f"../results/joint_result_{date}.p", "wb") as f:
            pickle.dump(res, f)
    else:  # we just want to plot the result from an older run
        with open(args.results, "rb") as f:
            res = pickle.load(f)
        ndim = res.samples.shape[1]

    if args.plot_results:
        # trace plot
        fig, axs = dyplot.traceplot(
            res,
            show_titles=True,
            trace_cmap='plasma',
            connect=True,
            connect_highlight=range(5),
            labels=LABELS,
        )
        fig.tight_layout(pad=2.0)
        fig.savefig("../figures/joint_inference_trace_plot.png",
                    bbox_inches="tight")

        # corner points plot
        fig, axes = plt.subplots(ndim - 1, ndim - 1, figsize=(15, 15))
        axes.reshape([ndim - 1, ndim - 1])
        fg, ax = dyplot.cornerpoints(res,
                                     cmap='plasma',
                                     kde=False,
                                     fig=(fig, axes),
                                     labels=LABELS)
        fg.savefig("../figures/joint_inference_cornerpoints.png",
                   bbox_inches="tight")

        # corner plot
        fig, axes = plt.subplots(ndim, ndim, figsize=(15, 15))
        axes.reshape([ndim, ndim])
        fg, ax = dyplot.cornerplot(res,
                                   fig=(fig, axes),
                                   color="b",
                                   labels=LABELS,
                                   show_titles=True)
        fg.savefig("../figures/joint_inference_corner_plot.png",
                   bbox_inches="tight")

        #### marginalized posterior #####
        Ddt = res.samples[:, 0]  # histogram of Ddt
        weights = np.exp(
            res['logwt'] -
            res['logz'][-1])  # posterior probability (Bayes theorem)

        # eliminate outliers with low and high + estimate confidance interval
        low, fifth, median, ninety_fifth, high = weighted_quantile(
            Ddt, [0.0001, 0.05, 0.5, 0.95, 0.9999], weights)
        error_plus = ninety_fifth - median  # error estimate with 5th percentile
        error_minus = median - fifth  # error estimate with 95th percentile
        good = (Ddt > low) & (Ddt < high)  # remove outliers
        Ddt = Ddt[good]
        weights = weights[good]

        plt.figure(figsize=(8, 8))
        plt.hist(Ddt, bins=100, weights=weights)
        plt.title(r"$D_{\Delta t}$=%.2f$^{+%.2f}_{-%.2f}$" %
                  (median, error_plus, error_minus))
        plt.xlabel(r"$D_{\Delta t}$")
        plt.savefig("../figures/marginalized_posterior_Ddt.png")

        # assume a flat LambdCDM model (with negligible radiation)

        # We need to model kappa_ext for this step
        def integrand(z):
            return 1 / np.sqrt(args.omega_m * (1 + z)**3 + args.omega_l)

        Dd = quad(integrand, 0, args.z_lens)[0] / (1 + args.z_lens)
        Ds = quad(integrand, 0, args.z_source)[0] / (1 + args.z_source)
        Dds = quad(integrand, args.z_lens,
                   args.z_source)[0] / (1 + args.z_source)
        factor = (1 + args.z_lens) * Ds * Dd / Dds
        H0 = (c * factor / Ddt / u.Mpc).to(u.km / u.s / u.Mpc).value

        plt.figure(figsize=(8, 8))
        fifth, median, ninety_fifth = weighted_quantile(
            H0, [0.05, 0.5, 0.95], weights)
        error_plus = ninety_fifth - median
        error_minus = median - fifth
        plt.hist(H0, bins=100, weights=weights)
        plt.title(r"$H_0$=%.2f$^{+%.2f}_{-%.2f}$" %
                  (median, error_plus, error_minus))
        plt.xlabel(r"$H_0$ [km s$^{-1}$ Mpc$^{-1}$]")
        plt.savefig("../figures/marginalized_posterior_H0.png")
コード例 #17
0
def ns_output(datadir):
    '''
    Inputs:
    -------
    datadir : str
        the working directory for allesfitter
        must contain all the data files
        output directories and files will also be created inside datadir
            
    Outputs:
    --------
    This will output information into the console, and create a output files 
    into datadir/results/ (or datadir/QL/ if QL==True)    
    '''
    config.init(datadir)
    
    #::: security check
    if os.path.exists(os.path.join(config.BASEMENT.outdir,'ns_table.csv')):
        try:
            overwrite = str(input('Nested Sampling output files already exists in '+config.BASEMENT.outdir+'.\n'+\
                                  'What do you want to do?\n'+\
                                  '1 : overwrite the output files\n'+\
                                  '2 : abort\n'))
            if (overwrite == '1'):
                pass
            else:
                raise ValueError('User aborted operation.')
        except EOFError:
            warnings.warn("Nested Sampling output files already existed from a previous run, and were automatically overwritten.")
            pass
    
    #::: load the save_ns.pickle
    f = gzip.GzipFile(os.path.join(config.BASEMENT.outdir,'save_ns.pickle.gz'), 'rb')
    results = pickle.load(f)
    f.close()
           
    
    #::: plot the fit        
    posterior_samples_for_plot = draw_ns_posterior_samples(results, Nsamples=20) #only 20 samples for plotting
    
    for companion in config.BASEMENT.settings['companions_all']:
        fig, axes = afplot(posterior_samples_for_plot, companion)
        if fig is not None:
            fig.savefig( os.path.join(config.BASEMENT.outdir,'ns_fit_'+companion+'.pdf'), bbox_inches='tight' )       
            plt.close(fig)

    for companion in config.BASEMENT.settings['companions_phot']:
        for inst in config.BASEMENT.settings['inst_phot']:
            fig, axes = afplot_per_transit(posterior_samples_for_plot, inst, companion)
            fig.savefig( os.path.join(config.BASEMENT.outdir,'ns_fit_per_transit_'+inst+'_'+companion+'.pdf'), bbox_inches='tight' )
            plt.close(fig)
          
    
    #::: retrieve the results
    posterior_samples = draw_ns_posterior_samples(results)                               # all weighted posterior_samples
    params_median, params_ll, params_ul = get_params_from_samples(posterior_samples)     # params drawn form these posterior_samples
    
    #::: output the results
    logprint('\nResults:')
    logprint('--------------------------')
#    print(results.summary())
    logZdynesty = results.logz[-1]                                                       # value of logZ
    logZerrdynesty = results.logzerr[-1]                                                 # estimate of the statistcal uncertainty on logZ
    logprint('log(Z) = {} +- {}'.format(logZdynesty, logZerrdynesty))
    logprint('Nr. of posterior samples: {}'.format(len(posterior_samples)))
    
    
    #::: make pretty titles for the plots  
    labels, units = [], []
    for i,l in enumerate(config.BASEMENT.fitlabels):
        labels.append( str(config.BASEMENT.fitlabels[i]) )
        units.append( str(config.BASEMENT.fitunits[i]) )
        
    results2 = results.copy()                    
    params_median2, params_ll2, params_ul2 = params_median.copy(), params_ll.copy(), params_ul.copy()     # params drawn form these posterior_samples; only needed for plots (subtract epoch offset)  
    fittruths2 = config.BASEMENT.fittruths.copy()
    for companion in config.BASEMENT.settings['companions_all']:
        
        if companion+'_epoch' in config.BASEMENT.fitkeys:
            ind    = np.where(config.BASEMENT.fitkeys==companion+'_epoch')[0][0]
            results2['samples'][:,ind] -= int(params_median[companion+'_epoch'])                #np.round(params_median[companion+'_epoch'],decimals=0)
            units[ind] = str(units[ind]+'-'+str(int(params_median[companion+'_epoch']))+'d')    #np.format_float_positional(params_median[companion+'_epoch'],0)+'d')
            fittruths2[ind] -= int(params_median[companion+'_epoch'])
            params_median2[companion+'_epoch'] -= int(params_median[companion+'_epoch'])
                

    for i,l in enumerate(labels):
        if len( units[i].strip(' ') ) > 0:
            labels[i] = str(labels[i]+' ('+units[i]+')')
        
        
    #::: traceplot    
    cmap = truncate_colormap( 'Greys', minval=0.2, maxval=0.8, n=256 )
    tfig, taxes = dyplot.traceplot(results2, labels=labels, quantiles=[0.16, 0.5, 0.84], truths=fittruths2, post_color='grey', trace_cmap=[cmap]*config.BASEMENT.ndim, trace_kwargs={'rasterized':True})
    plt.tight_layout()
    
    
    #::: cornerplot
    # ndim = results2['samples'].shape[1]
    fontsize = np.min(( 24. + 0.5*config.BASEMENT.ndim, 40 ))
    cfig, caxes = dyplot.cornerplot(results2, labels=labels, span=[0.997 for i in range(config.BASEMENT.ndim)], quantiles=[0.16, 0.5, 0.84], truths=fittruths2, hist_kwargs={'alpha':0.25,'linewidth':0,'histtype':'stepfilled'}, 
                                    label_kwargs={"fontsize":fontsize, "rotation":45, "horizontalalignment":'right'})


    #::: runplot
#    rfig, raxes = dyplot.runplot(results)
#    rfig.savefig( os.path.join(config.BASEMENT.outdir,'ns_run.jpg'), dpi=100, bbox_inches='tight' )
#    plt.close(rfig)
    
    
    #::: set allesfitter titles and labels
    for i, key in enumerate(config.BASEMENT.fitkeys): 
        
        value = round_tex(params_median2[key], params_ll2[key], params_ul2[key])
        ctitle = r'' + labels[i] + '\n' + r'$=' + value + '$'
        ttitle = r'' + labels[i] + r'$=' + value + '$'
        if len(config.BASEMENT.fitkeys)>1:
            # caxes[i,i].set_title(ctitle)
            caxes[i,i].set_title(ctitle, fontsize=fontsize, rotation=45, horizontalalignment='left')
            taxes[i,1].set_title(ttitle)
            for i in range(caxes.shape[0]):
                for j in range(caxes.shape[1]):
                    caxes[i,j].xaxis.set_label_coords(0.5, -0.5)
                    caxes[i,j].yaxis.set_label_coords(-0.5, 0.5)
        
                    if i==(caxes.shape[0]-1): 
                        fmt = ScalarFormatter(useOffset=False)
                        caxes[i,j].xaxis.set_major_locator(MaxNLocator(nbins=3))
                        caxes[i,j].xaxis.set_major_formatter(fmt)
                    if (i>0) and (j==0):
                        fmt = ScalarFormatter(useOffset=False)
                        caxes[i,j].yaxis.set_major_locator(MaxNLocator(nbins=3))
                        caxes[i,j].yaxis.set_major_formatter(fmt)
                        
                    for tick in caxes[i,j].xaxis.get_major_ticks(): tick.label.set_fontsize(24) 
                    for tick in caxes[i,j].yaxis.get_major_ticks(): tick.label.set_fontsize(24)    
        else:
            caxes.set_title(ctitle)
            taxes[1].set_title(ttitle)
            caxes.xaxis.set_label_coords(0.5, -0.5)
            caxes.yaxis.set_label_coords(-0.5, 0.5)
               
            
    #::: save and close the trace- and cornerplot
    tfig.savefig( os.path.join(config.BASEMENT.outdir,'ns_trace.pdf'), bbox_inches='tight' )
    plt.close(tfig)
    cfig.savefig( os.path.join(config.BASEMENT.outdir,'ns_corner.pdf'), bbox_inches='tight' )
    plt.close(cfig)


    #::: save the tables
    save_table(posterior_samples, 'ns')
    save_latex_table(posterior_samples, 'ns')
    

    #::: derive values (using stellar parameters from params_star.csv)
    deriver.derive(posterior_samples, 'ns')
   
    
    #::: make top-down orbit plot (using stellar parameters from params_star.csv)
    try:
        params_star = np.genfromtxt( os.path.join(config.BASEMENT.datadir,'params_star.csv'), delimiter=',', names=True, dtype=None, encoding='utf-8', comments='#' )
        fig, ax = plot_top_down_view(params_median, params_star)
        fig.savefig( os.path.join(config.BASEMENT.outdir,'top_down_view.pdf'), bbox_inches='tight' )
        plt.close(fig)        
    except:
        logprint('\nOrbital plots could not be produced.')
    
    
    #::: plot TTV results (if wished for)
    if config.BASEMENT.settings['fit_ttvs'] == True:
        plot_ttv_results(params_median, params_ll, params_ul)
    
    
    #::: clean up
    logprint('\nDone. For all outputs, see', config.BASEMENT.outdir)
    
    
    #::: return a nerdy quote
    try:
        with open(os.path.join(os.path.dirname(__file__), 'utils', 'quotes.txt')) as dataset:
            return(np.random.choice([l for l in dataset]))
    except:
        return('42')
コード例 #18
0
def Model2_output(data1,data2,bins_,label,sampler_results='sampler_results_model2_'):
    '''
    sampler_results: path of pickle file where sampler results are saved
    '''
    x,n1,n2,dn1,dn2 = bin_data(data1,data2,bins_,label)

    with open(sampler_results+label, 'rb') as dres2_file:
        dres2 = pickle.load(dres2_file)
        
    print("plotting corner plots...")
    labels = [r'$\alpha$', r'$\mu_\alpha$', r'$\sigma_\alpha$', r'$\xi_\alpha$',
              r'$\beta$',  r'$\mu_\beta$',  r'$\sigma_\beta$',  r'$\xi_\beta$',
              r'$\gamma$',  r'$\mu_\gamma$',  r'$\sigma_\gamma$',  r'$\xi_\gamma$',
              r'$\delta$',  r'$\mu_\delta$',  r'$\sigma_\delta$',  r'$\xi_\delta$']

    fig, axes = dyplot.cornerplot(dres2, smooth=0.03,
                                  labels=labels,
                                  show_titles=True,
                                  quantiles_2d=[1-np.exp(-0.5*r**2) for r in [1.,2.,3]],
                                  quantiles=[0.16, 0.5, 0.84],
                                  fig=plt.subplots(16, 16, figsize=(2.5*16,2.6*16)),
                                  color='#1f77d4')

    fig.savefig('corner_model2_'+label+'.png', dpi=100)
    
    
    mapvals2 = np.zeros(16, dtype=float)
    for i in range(16):
        x16, x50, x84 = dynesty.utils.quantile(dres2.samples[:,i],
                                               np.asarray([0.16, 0.5, 0.84]))
        mapvals2[i] = x50
    print("The maximum a posteriori (MAP) values of the parameters: ",mapvals2)
    
    print("Best fit results: ")
    fig, axes = plt.subplots(2,2, figsize=(10,5), sharex=True,
                             gridspec_kw={'height_ratios':[3,1], 'hspace':0},
                             tight_layout=True)

    ax = axes[0,0]
    ep = ax.errorbar(x, n1, yerr=dn1, fmt='.', alpha=0.5)
    ax.plot(x, mixturemodel_skew(mapvals2[:8], x), color=ep[0].get_color(), label='data set 1')
    ax.set(ylabel='count',
           title=r'$\mathcal{M}_2$ (Skew normal model)')
    ax.grid(ls=':')
    ax.legend(fontsize=10)

    ax = axes[1,0]
    ax.errorbar(x, n1 - mixturemodel_skew(mapvals2[:8], x), yerr=dn1, fmt='.')
    ax.grid(ls=':')

    ax.set(#xlim=(0, 4),
           xlabel=label,)
           #ylim=(-750,750))

    ax = axes[0,1]
    ep = ax.errorbar(x, n2, yerr=dn1, fmt='.', color='#ff7f0e', alpha=0.5);
    ax.plot(x, mixturemodel_skew(mapvals2[8:], x), color=ep[0].get_color(), label='data set 2')
    ax.grid(ls=':')
    ax.set(title='Parameter MAP values')
    ax.legend(fontsize=10)

    ax = axes[1,1]
    ax.errorbar(x, n2 - mixturemodel_skew(mapvals2[8:], x), yerr=dn2, fmt='.', color='#ff7f0e')
    ax.grid(ls=':')

    ax.set(#xlim=(0, 4),
           xlabel=label,)
           #ylim=(-275,275))

    fig.savefig('map_model2_'+label+'.png', dpi=100)
    
    lnZ2 = dres2.logz[-1]
    print("Bayesian Evidence for model 2 : ", lnZ2)
    
    return lnZ2
コード例 #19
0
def subcorner(res, eout, parnames, outname=None, maxprob=False, truth_dict=None, truths=None, **opts):
    """ wrapper around dyplot.cornerplot()
    adds in a star formation history and marginalized parameters
    for some key outputs (stellar mass, SFR, sSFR, half-mass time)
    """

    # write down some keywords
    title_kwargs = {'fontsize':fs*.7}
    label_kwargs = {'fontsize':fs*.7}

    if truth_dict is not None:
        truths = []
        for par in parnames:
            if par in truth_dict.keys():
                truths += [truth_dict[par]]
            else:
                truths += [np.nan]

    if (maxprob) & (truths is None):
        truths = res['samples'][eout['sample_idx'][0],:]

    # create dynesty plot
    # maximum probability solution in red
    fig, axes = dyplot.cornerplot(res, show_titles=True, labels=parnames, truths=truths,
                                  truth_color=truth_color,
                                  label_kwargs=label_kwargs, title_kwargs=title_kwargs)
    for ax in axes.ravel():
        ax.xaxis.set_tick_params(labelsize=tick_fs*.7)
        ax.yaxis.set_tick_params(labelsize=tick_fs*.7)

    # extra parameters
    eout_toplot = ['stellar_mass','sfr_100', 'ssfr_100', 'avg_age', 'H alpha 6563', 'H alpha/H beta']
    not_log = ['half_time','H alpha/H beta']
    ptitle = [r'log(M$_*$)',r'log(SFR$_{\mathrm{100 Myr}}$)',
              r'log(sSFR$_{\mathrm{100 Myr}}$)',r'log(t$_{\mathrm{avg}}$) [Gyr]',
              r'log(EW$_{\mathrm{H \alpha}}$)',r'Balmer decrement']

    # either we create a new figure for extra parameters
    # or add to old figure
    # depending on dimensionality of model (and thus of the plot)
    #if axes.shape[0] <= 10:
    #    label_kwargs['fontsize'] *= 0.7
    #    title_kwargs['fontsize'] *= 0.7


    if (axes.shape[0] <= 6):

        # only plot a subset of parameters
        eout_toplot, ptitle = eout_toplot[:4], ptitle[:4]

        # generate fake results file for dynesty
        nsamp, nvar = eout['weights'].shape[0], len(eout_toplot)
        fres = {'samples': np.empty(shape=(nsamp,nvar)), 'weights': eout['weights']}
        for i in range(nvar): 
            the_chain = eout['extras'][eout_toplot[i]]['chain']
            if eout_toplot[i] in not_log:
                fres['samples'][:,i] = the_chain
            else:
                fres['samples'][:,i] = np.log10(the_chain)

        fig2, axes2 = dyplot.cornerplot(fres, show_titles=True, labels=ptitle, label_kwargs=label_kwargs, title_kwargs=title_kwargs)
       
        # add SFH plot
        sfh_ax = fig2.add_axes([0.7,0.7,0.25,0.25],zorder=32)
        add_sfh_plot([eout], fig2,
                     main_color = ['black'],
                     ax_inset=sfh_ax,
                     text_size=1.5,lw=2,truth_dict=truth_dict)
        fig2.savefig('{0}.corner.extra.pdf'.format(outname))
        plt.close(fig2)

    else:

        # add SFH plot
        sfh_ax = fig.add_axes([0.75,0.435,0.22,0.22],zorder=32)
        add_sfh_plot([eout], fig, main_color = ['black'], ax_inset=sfh_ax, text_size=2,lw=4,truth_dict=truth_dict)

        # create extra parameters
        axis_size = fig.get_axes()[0].get_position().size
        xs, ys = 0.4, 1.0-axis_size[1]*1.3
        xdelta, ydelta = axis_size[0]*1.2, axis_size[1]*1.8
        plotloc = 0
        for jj, ename in enumerate(eout_toplot):

            # pull out chain, quantiles
            weights = eout['weights']
            if 'H alpha' not in ename:
                pchain = eout['extras'][ename]['chain']
                qvalues = [eout['extras'][ename]['q16'],
                           eout['extras'][ename]['q50'],
                           eout['extras'][ename]['q84']]
            elif '6563' in ename:
                pchain = eout['obs']['elines'][ename]['ew']['chain']
                qvalues = [eout['obs']['elines'][ename]['ew']['q16'],
                           eout['obs']['elines'][ename]['ew']['q50'],
                           eout['obs']['elines'][ename]['ew']['q84']]
            else:
                pchain = eout['obs']['elines']['H alpha 6563']['flux']['chain'] / eout['obs']['elines']['H beta 4861']['flux']['chain']
                qvalues = dyplot._quantile(pchain,np.array([0.16, 0.50, 0.84]),weights=weights)

            # logify. 
            if ename not in not_log:
                pchain = np.log10(pchain)
                qvalues = np.log10(qvalues)

            # make sure we're not producing infinities.
            # if we are, replace them with minimum.
            # if everything is infinity, skip and don't add the axis!
            # one failure mode here: if qvalues include an infinity!
            infty = ~np.isfinite(pchain)
            if infty.sum() == pchain.shape[0]:
                continue
            if infty.sum():
                pchain[infty] = pchain[~infty].min()

            # total obfuscated way to add in axis
            ax = fig.add_axes([xs+(jj%4)*xdelta, ys-int(jj/4)*ydelta, axis_size[0], axis_size[1]])

            # complex smoothing routine to match dynesty
            bins = int(round(10. / 0.02))
            n, b = np.histogram(pchain, bins=bins, weights=weights,
                                range=[pchain.min(),pchain.max()])
            n = norm_kde(n, 10.)
            x0 = 0.5 * (b[1:] + b[:-1])
            y0 = n
            ax.fill_between(x0, y0, color='k', alpha = 0.6)

            # plot and show quantiles
            for q in qvalues: ax.axvline(q, ls="dashed", color='red')

            q_m = qvalues[1]-qvalues[0]
            q_p = qvalues[2]-qvalues[1]
            fmt = "{{0:{0}}}".format(".2f").format
            title = r"${{{0}}}_{{-{1}}}^{{+{2}}}$"
            title = title.format(fmt(float(qvalues[1])), fmt(float(q_m)), fmt(float(q_p)))
            #title = "{0}\n={1}".format(ptitle[jj], title)
            ax.set_title(title, va='bottom',**title_kwargs)
            ax.set_xlabel(ptitle[jj],**label_kwargs)

            # look for truth
            min, max = np.percentile(pchain,0.5), np.percentile(pchain,99.5)
            if truth_dict is not None:
                if ename in truth_dict.keys():
                    if ename not in not_log:
                        tplt = np.log10(truth_dict[ename])
                    else:
                        tplt = truth_dict[ename]
                    ax.axvline(tplt, ls=":", color=truth_color,lw=1.5)

                    min = np.min([min,tplt.min()])
                    max = np.max([max,tplt.max()])

            if ename in not_log:
                min, max = min*0.99, max*1.01
            else:
                min = min - 0.02
                max = max + 0.02

            # set range
            ax.set_xlim(min,max)
            ax.set_ylim(0, 1.1 * np.max(n))
            ax.set_yticklabels([])
            ax.xaxis.set_major_locator(MaxNLocator(5))
            [l.set_rotation(45) for l in ax.get_xticklabels()]
            ax.xaxis.set_tick_params(labelsize=label_kwargs['fontsize'])

    fig.savefig('{0}.corner.pdf'.format(outname))
    plt.close(fig)
コード例 #20
0
        # "Standard" nested sampling.
        sampler = dynesty.DynamicNestedSampler(lnlike, prior_transform, ndim, nlive=nlive,
                                               bound="multi", method="slice", bootstrap=0)
        t0 = time.time()
        sampler.run_nested(nlive_init=int(nlive/2), nlive_batch=int(nlive),
                           wt_kwargs={'pfrac': 1.0}, stop_kwargs={"post_thresh":0.2})
        dur = time.time() - t0
        results = sampler.results
        results['duration'] = dur
        indmax = results['logl'].argmax()
        best = results['samples'][indmax, :]

        from dynesty import plotting as dyplot
        truths = ptrue.copy()
        label = filters + ["ra", "dec", "q", "pa", "n", "rh"]
        cfig, caxes = dyplot.cornerplot(results, fig=pl.subplots(ndim, ndim, figsize=(13., 10)),
                                        labels=label, show_titles=True, title_fmt='.8f', truths=truths)
        tfig, taxes = dyplot.traceplot(results, fig=pl.subplots(ndim, 2, figsize=(13., 13.)),
                                    labels=label)

    # -- hmc ---
    if False:
        p0 = ptrue.copy()
        prange = upper - lower
        scales = np.array(nsource * [ 5., plate_scale, plate_scale, 1.0, 1. ])

        from hmc import BasicHMC
        model = Posterior(scene, plans, upper=upper, lower=lower)
        sampler = BasicHMC(model, verbose=False)
        sampler.ndim = len(p0)

コード例 #21
0
# Define our uniform prior via the prior transform.
def ptform(u):
    return 1.0 * u


#dynamic nested sampling
dsampler = dynesty.DynamicNestedSampler(loglike, ptform, ndim)
dsampler.run_nested()
dresults = dsampler.results

#plot the results
# initialize figure
fig, axes = plt.subplots(3, 7, figsize=(35, 15))
axes = axes.reshape((3, 7))  # reshape axes

# add white space
[a.set_frame_on(False) for a in axes[:, 3]]
[a.set_xticks([]) for a in axes[:, 3]]
[a.set_yticks([]) for a in axes[:, 3]]

# plot initial run (res1; left)
fg, ax = dyplot.cornerplot(dresults,
                           color='blue',
                           truths=np.zeros(ndim),
                           truth_color='black',
                           show_titles=True,
                           max_n_ticks=3,
                           quantiles=None,
                           fig=(fig, axes[:, :4]))
コード例 #22
0
        nlive_init=1000,
        nlive_batch=100,
        maxiter=maxiter,
        use_stop=False,
        # wt_kwargs={'pfrac': 1.0}
    )

    res = sampler.results
    pickle.dump(res, open(chain_file, "wb"))

t_run = (time.time() - t0) / 60.
print('\n============================================')
print("Sampling took {0:.10f} mins".format(t_run))
print('============================================')

# =====================================================================
# Plots
if plot:
    rfig, raxes = dyplot.runplot(
        res,
        span=[0.0, (0., 1.1), 0.0, (0., 1.05 * np.exp(np.nanmax(res.logz)))])
    plt.savefig(chain_file.replace('.pickle', '_runplot.png'))

    tfig, taxes = dyplot.traceplot(res, labels=labels)
    plt.savefig(chain_file.replace('.pickle', '_traceplot.png'))

    cfig, caxes = dyplot.cornerplot(res, labels=labels, show_titles=True)
    plt.savefig(chain_file.replace('.pickle', '_cornerplot.png'))

del res
コード例 #23
0
ファイル: linear_test.py プロジェクト: ExoCTK/chimera
                                        update_interval=3.,
                                        pool=pool,
                                        queue_size=4)
dsampler.run_nested()
dres = dsampler.results

from dynesty import plotting as dyplot

truths = [m_true, b_true, np.log(f_true)]

# trace plot
fig, axes = dyplot.traceplot(dsampler.results,
                             truths=truths,
                             fig=plt.subplots(3, 2, figsize=(16, 12)))
fig.tight_layout()

# corner plot
fig, axes = dyplot.cornerplot(dres,
                              truths=truths,
                              show_titles=True,
                              title_kwargs={'y': 1.04},
                              fig=plt.subplots(3, 3, figsize=(15, 15)))
'''
results=dsampler.results
samples=results['samples']
weights = np.exp(results['logwt'] - results['logz'][-1])  #weights when plotting histogram....
    
'''

pdb.set_trace()
コード例 #24
0
    rho_halo = uniform(0.0001, 1, u[2])
    R_halo = uniform(0.1, 100, u[3])
    return M_disk, R_disk, rho_halo, R_halo


dsampler = dynesty.DynamicNestedSampler(loglike_Bur_nb,
                                        prior_xforBB,
                                        ndim=4,
                                        logl_args=(np.array(r_denorm),
                                                   np.array(v),
                                                   np.array(v_err), 90),
                                        nlive=2000,
                                        bound='multi',
                                        sample='auto')
dsampler.run_nested()
dres1 = dsampler.results

labels = ['$M_{disk}$', '$R_{disk}$', '$Rho_{halo}$', '$R_{halo}$']
nParams = len(labels)
fig, axes = dyplot.cornerplot(
    dres1,
    smooth=0.03,
    labels=labels,
    show_titles=True,
    quantiles_2d=[1 - np.exp(-0.5 * r**2) for r in [1., 2., 3]],
    quantiles=[0.16, 0.5, 0.84],
    fig=plt.subplots(nParams, nParams, figsize=(2.5 * nParams, 2.6 * nParams)),
    color='#1f77d4')
plt.show()
#################################################################################
コード例 #25
0
ファイル: test_plot.py プロジェクト: joshspeagle/dynesty
def test_gaussian(dynamic, periodic, ndim, bound):
    rstate = get_rstate()
    g = Gaussian(ndim=ndim)
    if periodic:
        periodic = [0]
    else:
        periodic = None
    if dynamic:
        sampler = dynesty.DynamicNestedSampler(g.loglikelihood,
                                               g.prior_transform,
                                               g.ndim,
                                               nlive=nlive,
                                               rstate=rstate,
                                               periodic=periodic,
                                               bound=bound)
    else:
        sampler = dynesty.NestedSampler(g.loglikelihood,
                                        g.prior_transform,
                                        g.ndim,
                                        nlive=nlive,
                                        rstate=rstate,
                                        periodic=periodic,
                                        bound=bound)
    sampler.run_nested(print_progress=printing)
    results = sampler.results
    # check plots
    dyplot.runplot(results)
    dyplot.runplot(results, span=[(0., 10.), 0.001, 0.2, (5., 6.)])
    dyplot.runplot(results, logplot=True)
    dyplot.runplot(results,
                   fig=(plt.gcf(), plt.gcf().axes),
                   max_x_ticks=0,
                   max_y_ticks=0)
    plt.close()
    dyplot.traceplot(results)
    dyplot.traceplot(results, smooth=[10] * ndim)
    dyplot.traceplot(results, connect=True)
    dyplot.traceplot(results,
                     fig=(plt.gcf(), plt.gcf().axes),
                     show_titles=True,
                     truths=np.zeros(ndim),
                     verbose=True,
                     max_n_ticks=0)
    plt.close()

    truths = np.zeros(ndim)
    truths[0] = -.1
    span = [[-10, 10]] * ndim
    if ndim > 1:
        truths[1] = .1
        span[1] = .9

    dyplot.cornerplot(results, show_titles=True, truths=truths)
    dyplot.cornerplot(results,
                      smooth=10,
                      verbose=True,
                      hist2d_kwargs=dict(plot_datapoints=True),
                      max_n_ticks=0)
    plt.close()
    if ndim != 1:
        # cornerbound
        dyplot.cornerbound(results,
                           it=500,
                           prior_transform=g.prior_transform,
                           show_live=True,
                           span=span)
        dyplot.cornerbound(results,
                           it=500,
                           show_live=True,
                           span=span,
                           fig=(plt.gcf(), plt.gcf().axes))
        plt.close()
        # boundplot
        dyplot.boundplot(results,
                         dims=(0, 1)[:min(ndim, 2)],
                         it=1000,
                         prior_transform=g.prior_transform,
                         show_live=True,
                         span=span)
        dyplot.boundplot(results, dims=(0, 1)[:min(ndim, 2)], it=1000)
        plt.close()

        # cornerpoints
        dyplot.cornerpoints(results)
        plt.close()
        dyplot.cornerpoints(results, span=span, truths=truths, max_n_ticks=0)
        plt.close()
コード例 #26
0
ファイル: int.py プロジェクト: pchaubal/DINGO
#                 np.log(np.linalg.det(C)))

L = Likelihood(data)
# def loglike(x):
#     return -0.5 * np.dot(x, np.dot(Cinv, x)) + lnorm

# Define our uniform prior via the prior transform.
def ptform(u):
    return 10. * u

# Sample from our distribution.
sampler = dynesty.NestedSampler(L.lnL, ptform, ndim,
                                bound='single', nlive=500)
sampler.run_nested(dlogz=0.1)
res = sampler.results


# Plot results.
# from dynesty import plotting as dyplot

# Plot a summary of the run.
# rfig, raxes = dyplot.runplot(res)

# Plot traces and 1-D marginalized posteriors.
# tfig, taxes = dyplot.traceplot(res)

# Plot the 2-D marginalized posteriors.
cfig, caxes = dyplot.cornerplot(res)

plt.show()
コード例 #27
0
ファイル: sed_fitter.py プロジェクト: vedantchandra/WD_models
    def fit(self,
            sed,
            e_sed,
            parallax=[100, 0.001],
            nlive=250,
            distance=None,
            binary=False,
            plot_fit=True,
            plot_trace=False,
            plot_corner=False,
            progress=False,
            textx=0.025,
            textsize=12):

        if self.to_flux:
            sed = self.mag_to_flux(sed)
            e_sed = sed * e_sed  # magnitude error to flux error

        if not binary:
            ndim = 3

            def loglike(theta):
                teff, logg, plx = theta
                model = self.model_sed(teff, logg, plx)
                ivar = 1 / e_sed**2
                logchi = -0.5 * np.sum((sed - model)**2 * ivar)
                if np.isnan(logchi):
                    return -np.Inf
                else:
                    return logchi

            def prior_transform(u):
                x = np.array(u)
                x[0] = u[0] * (self.teff_range[1] -
                               self.teff_range[0]) + self.teff_range[0]
                x[1] = u[1] * (self.logg_range[1] -
                               self.logg_range[0]) + self.logg_range[0]
                t = stats.norm.ppf(u[2])
                x[2] = parallax[1] * t
                x[2] += parallax[0]
                return x

        elif binary:
            ndim = 5

            def loglike(theta):
                teff1, logg1, teff2, logg2, plx = theta

                model = self.model_binary_sed(teff1, logg1, teff2, logg2, plx)

                ivar = 1 / e_sed**2
                logchi = -0.5 * np.sum((sed - model)**2 * ivar)
                if np.isnan(logchi):
                    return -np.Inf
                elif teff1 > teff2:
                    return -np.Inf
                else:
                    return logchi

            def prior_transform(u):
                x = np.array(u)
                x[0] = u[0] * (self.teff_range[1] -
                               self.teff_range[0]) + self.teff_range[0]
                x[1] = u[1] * (self.logg_range[1] -
                               self.logg_range[0]) + self.logg_range[0]
                x[2] = u[2] * (self.teff_range[1] -
                               self.teff_range[0]) + self.teff_range[0]
                x[3] = u[3] * (self.logg_range[1] -
                               self.logg_range[0]) + self.logg_range[0]
                t = stats.norm.ppf(u[4])
                x[4] = parallax[1] * t
                x[4] += parallax[0]
                return x

        ########## DYNESTY ###################

        dsampler = dynesty.NestedSampler(loglike,
                                         prior_transform,
                                         ndim=ndim,
                                         nlive=nlive)
        dsampler.run_nested(print_progress=progress)

        result = dsampler.results

        samples, weights = result.samples, np.exp(result.logwt -
                                                  result.logz[-1])
        chis = -2 * np.array([loglike(sample) for sample in result.samples])
        bestfit = np.argmin(chis)
        resampled = dyfunc.resample_equal(samples, weights)
        cov = np.var(resampled, axis=0)

        mean = result.samples[bestfit]

        print(result.samples[bestfit])

        bandwls = []
        for band in self.bands:
            bandwls.append(self.mean_wl[band])

        ########## PLOTTING #################

        if plot_trace:

            f = dyplot.traceplot(dsampler.results,
                                 show_titles=True,
                                 trace_cmap='viridis')
            plt.tight_layout()
        if plot_corner:

            if binary:
                f = dyplot.cornerplot(dsampler.results,
                                      show_titles=True,
                                      labels=[
                                          '$T_{\mathrm{eff,1}}$',
                                          '$\log{g}_1$',
                                          '$T_{\mathrm{eff,2}}$',
                                          '$\log{g}_2$', r'$\varpi$'
                                      ])
            if not binary:
                f = dyplot.cornerplot(
                    dsampler.results,
                    show_titles=True,
                    labels=['$T_{\mathrm{eff}}$', '$\log{g}$', r'$\varpi$'])

            plt.tight_layout()

        if not binary:

            model = self.model_sed(*mean)
            ivar = 1 / e_sed**2
            redchi = np.sum((sed - model)**2 * ivar) / (len(sed) - ndim)

            if plot_fit:

                plt.figure(figsize=(10, 5))
                plt.errorbar(bandwls,
                             sed,
                             yerr=e_sed,
                             linestyle='none',
                             capsize=5,
                             color='k')
                plt.scatter(bandwls, model, color='k')
                plt.text(textx,
                         0.35,
                         '$T_{\mathrm{eff}}$ = %i ± %i' %
                         (mean[0], np.sqrt(cov[0])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.25,
                         '$\log{g}$ = %.2f ± %.2f' %
                         (mean[1], np.sqrt(cov[1])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.15,
                         'atm = %s' % (self.atm_type),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.05,
                         '$\chi_r^2$ = %.2f' % (redchi),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.xlabel('Wavelength ($\mathrm{\AA}$', fontsize=16)
                plt.ylabel(
                    '$f_\lambda\ [erg\ cm^{-2}\ s^{-1}\ \mathrm{\AA}^{-1}]$',
                    fontsize=16)
                plt.yscale('log')

            return [mean[0], np.sqrt(cov[0]), mean[1], np.sqrt(cov[1])], redchi

        elif binary:

            model = self.model_binary_sed(*mean)

            ivar = 1 / e_sed**2
            redchi = np.sum((sed - model)**2 * ivar) / (len(sed) - ndim)

            if plot_fit:

                plt.figure(figsize=(10, 5))
                plt.errorbar(bandwls,
                             sed,
                             yerr=e_sed,
                             linestyle='none',
                             capsize=5,
                             color='k')
                plt.scatter(bandwls, model, color='k')
                plt.text(textx,
                         0.45,
                         '$T_{\mathrm{eff,1}}$ = %i ± %i' %
                         (mean[0], np.sqrt(cov[0])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.35,
                         '$\log{g}_1$ = %.2f ± %.2f' %
                         (mean[1], np.sqrt(cov[1])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.25,
                         '$T_{\mathrm{eff,2}}$ = %i ± %i' %
                         (mean[2], np.sqrt(cov[2])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.text(textx,
                         0.15,
                         '$\log{g}_2$ = %.2f ± %.2f' %
                         (mean[3], np.sqrt(cov[3])),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                #plt.text(0.15, 0.2, 'atm = %s' %(self.atm_type), transform = plt.gca().transAxes, fontsize = 12)
                plt.text(textx,
                         0.05,
                         '$\chi_r^2$ = %.2f' % (redchi),
                         transform=plt.gca().transAxes,
                         fontsize=textsize)
                plt.xlabel('Wavelength ($\mathrm{\AA}$)', fontsize=16)
                plt.ylabel(
                    '$f_\lambda\ [erg\ cm^{-2}\ s^{-1}\ \mathrm{\AA}^{-1}]$',
                    fontsize=16)
                plt.yscale('log')

            return [
                mean[0],
                np.sqrt(cov[0]), mean[1],
                np.sqrt(cov[1]), mean[2],
                np.sqrt(cov[2]), mean[3],
                np.sqrt(cov[3])
            ], redchi
コード例 #28
0
full_results = []
with Pool() as pool:
    for transit_data in noisey_transit_depth:
        sampler = dynesty.NestedSampler(log_likelihood_h2o, prior_trans, ndim,
                                        nlive=500, pool=pool, queue_size=pool._processes, logl_args=(transit_data, fixed_h2o))
        sampler.run_nested()
        full_results.append(sampler.results)

if plot:
    # make a plot of results
    labels = ["Rad_planet", "T", "log H2O"]
    truths = [rad_planet, T, log_fh2o]
    for result in full_results:

        fig, axes = dyplot.cornerplot(result, truths=truths, show_titles=True,
                                      title_kwargs={'y': 1.04}, labels=labels,
                                      fig=plt.subplots(len(truths), len(truths), figsize=(10, 10)))
        fig.suptitle('Red lines are true values', fontsize=14)
        # fig.savefig('/test/my_first_cornerplot.png')



# from planet_sim.transit_toolbox import transit_model_H2OCH4


# define a new prior function, with only H2O and CH4
# this is basically the same as only H20
def loglike_ch4(theta, y, fixed_parameters):
    # retrieve the global variables
    global pixel_bins
    global transit_data
コード例 #29
0
def ns_output(datadir):
    '''
    Inputs:
    -------
    datadir : str
        the working directory for allesfitter
        must contain all the data files
        output directories and files will also be created inside datadir
            
    Outputs:
    --------
    This will output information into the console, and create a output files 
    into datadir/results/ (or datadir/QL/ if QL==True)    
    '''
    config.init(datadir)
    
    #::: security check
    if os.path.exists(os.path.join(config.BASEMENT.outdir,'ns_table.csv')):
        try:
            overwrite = str(input('Nested Sampling output files already exists in '+config.BASEMENT.outdir+'.\n'+\
                                  'What do you want to do?\n'+\
                                  '1 : overwrite the output files\n'+\
                                  '2 : abort\n'))
            if (overwrite == '1'):
                pass
            else:
                raise ValueError('User aborted operation.')
        except EOFError:
            warnings.warn("Nested Sampling output files already existed from a previous run, and were automatically overwritten.")
            pass
    
    #::: load the save_ns.pickle
#    with open( os.path.join(config.BASEMENT.outdir,'save_ns.pickle'),'rb' ) as f:
#        results = pickle.load(f)
#    f = bzip2.BZ2File(os.path.join(config.BASEMENT.outdir,'save_ns.pickle.bz2'), 'rb')
    f = gzip.GzipFile(os.path.join(config.BASEMENT.outdir,'save_ns.pickle.gz'), 'rb')
    results = pickle.load(f)
    f.close()
           
        
    #::: plot the fit        
    posterior_samples_for_plot = draw_ns_posterior_samples(results, Nsamples=20) #only 20 samples for plotting
    for companion in config.BASEMENT.settings['companions_all']:
        fig, axes = afplot(posterior_samples_for_plot, companion)
        fig.savefig( os.path.join(config.BASEMENT.outdir,'ns_fit_'+companion+'.pdf'), bbox_inches='tight' )
        f = gzip.GzipFile(os.path.join(config.BASEMENT.outdir,'ns_fit.pickle.gz'), 'wb')
        pickle.dump((fig,axes), f)
        f.close()        
        plt.close(fig)

    
    #::: retrieve the results
    posterior_samples = draw_ns_posterior_samples(results)                               # all weighted posterior_samples
    params_median, params_ll, params_ul = get_params_from_samples(posterior_samples)     # params drawn form these posterior_samples
    
    #::: output the results
    logprint('\nResults:')
    logprint('--------------------------')
#    print(results.summary())
    logZdynesty = results.logz[-1]                                                       # value of logZ
    logZerrdynesty = results.logzerr[-1]                                                 # estimate of the statistcal uncertainty on logZ
    logprint('log(Z) = {} +- {}'.format(logZdynesty, logZerrdynesty))
    logprint('Nr. of posterior samples: {}'.format(len(posterior_samples)))
    
    
    #::: make pretty titles for the plots  
    labels, units = [], []
    for i,l in enumerate(config.BASEMENT.fitlabels):
        labels.append( str(config.BASEMENT.fitlabels[i]) )
        units.append( str(config.BASEMENT.fitunits[i]) )
        
    results2 = results.copy()                    
    posterior_samples2 = draw_ns_posterior_samples(results2)                               # all weighted posterior_samples
    params_median2, params_ll2, params_ul2 = get_params_from_samples(posterior_samples2)     # params drawn form these posterior_samples                              #only needed for plots (subtract epoch offset)  
    for companion in config.BASEMENT.settings['companions_all']:
        
        if companion+'_epoch' in config.BASEMENT.fitkeys:
            ind    = np.where(config.BASEMENT.fitkeys==companion+'_epoch')[0][0]
            results2['samples'][:,ind] -= int(params_median[companion+'_epoch']) #np.round(params_median[companion+'_epoch'],decimals=0)
            units[ind] = str(units[ind]+'-'+str(int(params_median[companion+'_epoch']))+'d') #np.format_float_positional(params_median[companion+'_epoch'],0)+'d')
            config.BASEMENT.fittruths[ind] -= int(params_median[companion+'_epoch'])
                
    for i,l in enumerate(labels):
        if units[i]!='':
            labels[i] = str(labels[i]+' ('+units[i]+')')
        
        
    #::: traceplot    
    cmap = truncate_colormap( 'Greys', minval=0.2, maxval=0.8, n=256 )
    tfig, taxes = dyplot.traceplot(results2, labels=labels, truths=config.BASEMENT.fittruths, post_color='grey', trace_cmap=[cmap]*config.BASEMENT.ndim, trace_kwargs={'rasterized':True})
    plt.tight_layout()
    
    
    #::: cornerplot
    cfig, caxes = dyplot.cornerplot(results2, labels=labels, truths=config.BASEMENT.fittruths, hist_kwargs={'alpha':0.25,'linewidth':0,'histtype':'stepfilled'})


    #::: runplot
#    rfig, raxes = dyplot.runplot(results)
#    rfig.savefig( os.path.join(config.BASEMENT.outdir,'ns_run.jpg'), dpi=100, bbox_inches='tight' )
#    plt.close(rfig)
    

    #::: set allesfitter titles
    for i, key in enumerate(config.BASEMENT.fitkeys):  
        value = round_tex(params_median2[key], params_ll2[key], params_ul2[key])
        ttitle = r'' + labels[i] + r'$=' + value + '$'
        ctitle = r'' + labels[i] + '\n' + r'$=' + value + '$'
        if len(config.BASEMENT.fitkeys)>1:
            caxes[i,i].set_title(ctitle)
            taxes[i,1].set_title(ttitle)
            for i in range(caxes.shape[0]):
                for j in range(caxes.shape[1]):
                    caxes[i,j].xaxis.set_label_coords(0.5, -0.5)
                    caxes[i,j].yaxis.set_label_coords(-0.5, 0.5)
        else:
            caxes.set_title(ctitle)
            taxes[1].set_title(ttitle)
            caxes.xaxis.set_label_coords(0.5, -0.5)
            caxes.yaxis.set_label_coords(-0.5, 0.5)
        
               
            
    #::: save and close the trace- and cornerplot
    tfig.savefig( os.path.join(config.BASEMENT.outdir,'ns_trace.pdf'), bbox_inches='tight' )
    plt.close(tfig)
    cfig.savefig( os.path.join(config.BASEMENT.outdir,'ns_corner.pdf'), bbox_inches='tight' )
    plt.close(cfig)


    #::: save the tables
    save_table(posterior_samples, 'ns')
    save_latex_table(posterior_samples, 'ns')
    

    #::: derive values (using stellar parameters from params_star.csv)
    if os.path.exists(os.path.join(config.BASEMENT.datadir,'params_star.csv')):
        deriver.derive(posterior_samples, 'ns')
    else:
        print('File "params_star.csv" not found. Cannot derive final parameters.')
    
    
    logprint('Done. For all outputs, see', config.BASEMENT.outdir)
コード例 #30
0
ファイル: plot_dyn.py プロジェクト: ExoCTK/chimera
    'xRp'
]

#import past run
#samples=pickle.load(open('dyn_output_100LP.pic','rb')) #an example 100 live point run
samples = pickle.load(open('dyn_output_500LP.pic', 'rb'))
#samples=pickle.load(open('dyn_output_1000LP.pic','rb'))  #an example 1000 live point run

#printing evidence:
print('ln(Z)= ', samples.logz[-1])

# corner plot
fig, axes = dyplot.cornerplot(samples,
                              smooth=0.05,
                              color='red',
                              show_titles=True,
                              labels=labels,
                              truths=truth,
                              title_kwargs={'y': 1.04},
                              fig=plt.subplots(8, 8, figsize=(12, 12)))
plt.savefig('stair_pairs.pdf', fmt='pdf')
plt.show()
plt.close()

#GENERATING RANDOMLY SAMPLES SPECTRA-----------------------------------------------------
import numpy as np
xsecs = xsects_HST(2000, 30000)

Nspectra = 200

#loading in data again just to be safe
wlgrid, y_meas, err = pickle.load(open('data.pic', 'rb'))
コード例 #31
0
# View the fitted model (the MAP)
mod_fit['model'].display()
mod_fit['model'].blur_circ(20. * eh.RADPERUAS).display()

# View a few random samples from the posterior
eh.imaging.dynamical_imaging.plot_im_List([
    _.make_image(100. * eh.RADPERUAS, 128)
    for _ in mod_fit['posterior_models'][:10]
])

# Compare the fitted model to the data
eh.comp_plots.plotall_obs_im_compare(obs, mod_fit['model'], 'uvdist', 'amp')

# Compare a sample of fitted models drawn from the posterior to the data
eh.comp_plots.plotall_obs_im_compare(obs, mod_fit['posterior_models'],
                                     'uvdist', 'amp')

# Compare posteriors from with the true model values ('natural' uses natural units and rescalings)
from dynesty import plotting as dyplot

cfig, caxes = dyplot.cornerplot(mod_fit['res_natural'],
                                labels=mod_fit['labels_natural'],
                                truths=[1.5, 40, 1.0, 20, -15, 20])
cfig.set_size_inches(
    (2.5 * len(mod_fit['labels']), 2.5 * len(mod_fit['labels'])))
cfig.show()

# Save the MAP
mod_fit['model'].save_txt('sample_modelfit.txt')