Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_uninit(tmpdir):
fn = str(tmpdir.join("EMCEE_TEST_FILE_DO_NOT_USE.h5"))
if os.path.exists(fn):
os.remove(fn)
with backends.HDFBackend(fn) as be:
run_sampler(be)
assert os.path.exists(fn)
os.remove(fn)
def get_mcmc_posterior_samples(datadir, Nsamples=None, QL=False, as_type='dic'):
config.init(datadir, QL=QL)
reader = emcee.backends.HDFBackend( os.path.join(config.BASEMENT.outdir,'save.h5'), read_only=True )
return draw_mcmc_posterior_samples(reader, Nsamples=Nsamples, as_type=as_type) #only 20 samples for plotting
:param backup_filename: name of the HDF5 file where sampling state is saved (through emcee backend engine)
:type backup_filename: string
:param start_from_backup: if True, start from the state saved in `backup_filename`.
Otherwise, create a new backup file with name `backup_filename` (any already existing file is overwritten!).
:type start_from_backup: bool
:return: samples, ln likelihood value of samples
:rtype: numpy 2d array, numpy 1d array
"""
num_param, _ = self.chain.param.num_param()
if initpos is None:
initpos = sampling_util.sample_ball(mean_start, sigma_start, n_walkers, dist='normal')
pool = choose_pool(mpi=mpi, processes=threadCount, use_dill=True)
if backup_filename is not None:
backend = emcee.backends.HDFBackend(backup_filename, name="lenstronomy_mcmc_emcee")
if pool.is_master():
print("Warning: All samples (including burn-in) will be saved in backup file '{}'.".format(backup_filename))
if start_from_backup:
initpos = None
n_run_eff = n_run
else:
n_run_eff = n_burn + n_run
backend.reset(n_walkers, num_param)
if pool.is_master():
print("Warning: backup file '{}' has been reset!".format(backup_filename))
else:
backend = None
n_run_eff = n_burn + n_run
time_start = time.time()
log_rho_init = np.log(systematics_timescale)
else:
log_rho_init = np.log(1.)
#::: log(yerr)
err_norm = np.nanmean(yyerr)
err_scale = np.nanmean(yyerr)
log_err_scale_init = np.log(err_scale)
#::: all
initial = np.array([log_sigma_init,log_rho_init,log_err_scale_init])
#::: set up MCMC
ndim = len(initial)
backend = emcee.backends.HDFBackend(os.path.join(outdir,fname+'mcmc_save.h5')) # Set up a new backend
backend.reset(nwalkers, ndim)
#::: run MCMC
def run_mcmc(sampler):
p0 = initial + 1e-8 * np.random.randn(nwalkers, ndim)
sampler.run_mcmc(p0, total_steps/thin_by, thin_by=thin_by, progress=True);
if multiprocess:
with closing(Pool(processes=(multiprocess_cores))) as pool:
sampler = emcee.EnsembleSampler(nwalkers, ndim, log_probability, pool=pool, backend=backend)
run_mcmc(sampler)
else:
sampler = emcee.EnsembleSampler(nwalkers, ndim, log_probability, backend=backend)
run_mcmc(sampler)
if not os.path.exists( outdir ): os.makedirs( outdir )
###############################################################################
#::: safety check: ask user for permission
###############################################################################
f = os.path.join(outdir,'fit.jpg')
if os.path.exists( f ):
overwrite = raw_input('Output already exists in '+outdir+'. Overwrite output files? Y = yes, N = no\n')
if not (overwrite.lower() == 'y'):
raise ValueError('User aborted operation.')
if QL:
copyfile(os.path.join(datadir,'results','save.h5'),
os.path.join(outdir,'save.h5'))
reader = emcee.backends.HDFBackend( os.path.join(outdir,'save.h5'), read_only=True )
if QL:
settings['total_steps'] = reader.get_chain().shape[0]
settings['burn_steps'] = int(0.75*settings['thin_by']*reader.get_chain().shape[0])
###############################################################################
#::: update params to the median MCMC result
###############################################################################
params, params_ll, params_ul = update_params_with_MCMC_results(settings, params, fitkeys, reader)
###############################################################################
#::: create plots and output
###############################################################################
print_autocorr(reader, settings, fitkeys)