本文整理汇总了Python中emcee.utils.MPIPool.wait方法的典型用法代码示例。如果您正苦于以下问题:Python MPIPool.wait方法的具体用法?Python MPIPool.wait怎么用?Python MPIPool.wait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类emcee.utils.MPIPool
的用法示例。
在下文中一共展示了MPIPool.wait方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_pool
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def get_pool(mpi=False, threads=None):
""" Get a pool object to pass to emcee for parallel processing.
If mpi is False and threads is None, pool is None.
Parameters
----------
mpi : bool
Use MPI or not. If specified, ignores the threads kwarg.
threads : int (optional)
If mpi is False and threads is specified, use a Python
multiprocessing pool with the specified number of threads.
"""
if mpi:
from emcee.utils import MPIPool
# Initialize the MPI pool
pool = MPIPool()
# Make sure the thread we're running on is the master
if not pool.is_master():
pool.wait()
sys.exit(0)
print("Running with MPI...")
elif threads > 1:
import multiprocessing
print("Running with multiprocessing on " + str(threads) + " cores...")
pool = multiprocessing.Pool(threads)
else:
print("Running serial...")
pool = None
return pool
示例2: pt_mpi_sample
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def pt_mpi_sample(gf, ntemps, nwalkers, burn_steps, sample_steps, thin=1,
pool=None, betas=None, pos=None, random_state=None,
pos_filename=None, convergence_interval=50):
pool = MPIPool(loadbalance=True)
if not pool.is_master():
pool.wait()
sys.exit(0)
return pt_sample(gf, ntemps, nwalkers, burn_steps, sample_steps,
thin=thin, pool=pool, betas=betas, pos=pos,
random_state=random_state, pos_filename=pos_filename,
convergence_interval=convergence_interval)
示例3: fit_bim_bh3_curves
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def fit_bim_bh3_curves(p0=None):
# Choose initial position
if p0 is None:
p0 = np.zeros((nwalkers, ndim))
for walk_ix in range(nwalkers):
for d_ix in range(len(data)):
p0[walk_ix, d_ix*3] = np.random.uniform(1, 6)
p0[walk_ix, d_ix*3 + 1] = np.random.uniform(6e-5, 1e-3)
p0[walk_ix, d_ix*3 + 2] = np.random.uniform(2, 3)
hp_ix = len(data)*3
p0[walk_ix, hp_ix] = np.random.uniform(1,6) # fmax mean
p0[walk_ix, hp_ix + 1] = np.random.uniform(0,1) # fmax sd
p0[walk_ix, hp_ix + 2] = np.random.uniform(6e-5, 1e-3) # k mean
p0[walk_ix, hp_ix + 3] = np.random.uniform(0,1e-1) # k sd
p0[walk_ix, hp_ix + 4] = np.random.uniform(2,3) # f0 mean
p0[walk_ix, hp_ix + 5] = np.random.uniform(0,1) # f0 sd
#plt.figure()
#for d_ix, data_i in enumerate(data):
# plt.plot(time, data_i, color=colors[d_ix])
# plt.plot(time, fit_func(p0[0, d_ix*3:(d_ix+1)*3]), color='k')
# Initialize the MPI pool
pool = MPIPool()
if not pool.is_master():
pool.wait()
sys.exit(0)
# Get the sampler
sampler = emcee.EnsembleSampler(nwalkers, ndim, posterior, pool=pool)
# Burn-in
print("Burn-in sampling...")
pos, prob, state = sampler.run_mcmc(p0, burn_steps, storechain=False)
sampler.reset()
# Main sampling
print("Main sampling...")
sampler.run_mcmc(pos, sample_steps)
# Close the pool!
pool.close()
# Pickle the sampler
sampler.pool = None
with open('bimbh3_141125_2.pck','w') as f:
pickle.dump(sampler, f)
return sampler
示例4: tdelay_dt_mcmc
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def tdelay_dt_mcmc(run, theta, Niter=20, Nwalkers=10, Ndim=2, sigma_smhm=0.2, nsnap0=15, downsampled='14', flag=None, continue_chain=False):
'''
'''
if Ndim == 2:
tdelay_range = [0., 3.]#np.arange(0., 3., 0.5)
dt_range = [0.1, 4.]
# new chain
chain_file = ''.join([UT.fig_dir(), run, '.tdelay_dt_mcmc.chain.dat'])
if os.path.isfile(chain_file) and continue_chain:
print 'Continuing previous MCMC chain!'
sample = np.loadtxt(chain_file)
Niter = Niter - (np.float(len(sample))/np.float(Nwalkers)) # Number of chains left to finish
if Niter <= 0:
raise ValueError
print Niter, ' iterations left to finish'
else:
f = open(chain_file, 'w')
f.close()
# Initializing Walkers
pos0 = [np.array([np.random.uniform(tdelay_range[0], tdelay_range[1]), np.random.uniform(dt_range[0], dt_range[1])]) for i in range(Nwalkers)]
pool = MPIPool()
if not pool.is_master():
pool.wait()
sys.exit(0)
# Initializing the emcee sampler
kwargs = {
'theta': theta,
'sigma_smhm': 0.2,
'nsnap0': 15,
'downsampled': '14',
}
sampler = emcee.EnsembleSampler(Nwalkers, Ndim, sigM, pool=pool, kwargs=kwargs)
for result in sampler.sample(pos0, iterations=Niter, storechain=False):
position = result[0]
#print position
f = open(chain_file, 'a')
for k in range(position.shape[0]):
output_str = '\t'.join(position[k].astype('str')) + '\n'
f.write(output_str)
f.close()
pool.close()
return None
示例5: run_pool
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def run_pool(pC, pW, walk, step): #pCenter and pWidths
steps = step
nwalkers = walk
ndim = len(pC)
## r in, del r, i, PA
p0 = [pC[0], pC[1], pC[2], pC[3], pC[4]]
widths = [pW[0], pW[1], pW[2], pW[3], pW[4]]
p = emcee.utils.sample_ball(p0,widths,size=nwalkers)
pool = MPIPool()
if not pool.is_master():
pool.wait()
sys.exit(0)
sampler = emcee.EnsembleSampler(nwalkers, ndim, lnlike_visonly, live_dangerously=True, pool=pool)
print 'Beginning the MCMC run.'
start = time.clock()
sampler.run_mcmc(p, steps)
stop = time.clock()
pool.close()
print 'MCMC finished successfully.\n'
print 'This was a visibility-only run with {} walkers and {} steps'.format(nwalkers,steps)
print "Mean acor time: "+str(np.mean(sampler.acor))
print "\nMean acceptance fraction: "+str(np.mean(sampler.acceptance_fraction))
print 'Run took %r minutes' % ((stop - start)/60.)
chain = sampler.chain
chi = (sampler.lnprobability)/(-0.5)
whatbywhat = str(nwalkers)+'x'+str(steps)
os.system('mkdir MCMCRUNS/vis_only/'+whatbywhat)
chainFile = 'MCMCRUNS/vis_only/'+whatbywhat+'/'+whatbywhat+'.chain.fits'
chiFile = 'MCMCRUNS/vis_only/'+whatbywhat+'/'+whatbywhat+'.chi.fits'
infoFile = 'MCMCRUNS/vis_only/'+whatbywhat+'/'+whatbywhat+'.runInfo.txt'
fits.writeto(chainFile,chain)
fits.writeto(chiFile,chi)
f = open(infoFile,'w')
f.write('run took %r minutes\n' % ((stop - start)/60.))
f.write('walkers: %r\n' % nwalkers)
f.write('steps: %r\n' % steps)
f.write('initial model: %r\n' % p0)
f.write('widths: %r\n' % widths)
f.write("mean acor time: "+str(np.mean(sampler.acor)))
f.write("\nmean acceptance fraction: "+str(np.mean(sampler.acceptance_fraction)))
f.close()
print 'Data written to: \n'+chainFile+'\n'+chiFile+'\n'+infoFile
示例6: lnPost
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def lnPost(theta):
'''log-posterior
'''
# prior calculations
if prior_min[0] < theta[0] < prior_max[0] and \
prior_min[1] < theta[1] < prior_max[1] and \
prior_min[2] < theta[2] < prior_max[2] and \
prior_min[3] < theta[3] < prior_max[3] and \
prior_min[4] < theta[4] < prior_max[4]:
lnPrior = 0.0
else:
lnPrior = -np.inf
if not np.isfinite(lnPrior):
return -np.inf
return lnPrior + lnLike(theta)
"""Initializing Walkers"""
pos = [np.array([11. , np.log(.4) , 11.5 , 1.0 , 13.5]) + 1e-3*np.random.randn(Ndim) for i in range(Nwalkers)]
"""Initializing MPIPool"""
pool = MPIPool(loadbalance=True)
if not pool.is_master():
pool.wait()
sys.exit(0)
"""Initializing the emcee sampler"""
sampler = emcee.EnsembleSampler(Nwalkers, Ndim, lnprob, pool=pool)
# Burn in + Production
sampler.run_mcmc(pos, Nchains_burn + Nchains_pro)
# Production.
samples = sampler.chain[:, Nchains_burn:, :].reshape((-1, Ndim))
#closing the pool
pool.close()
np.savetxt("mcmc_sample.dat" , samples)
示例7: main
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def main():
'''
A parallel run.
'''
pool = MPIPool(loadbalance=True)
if not pool.is_master():
pool.wait()
sys.exit(0)
clf = hbsgc.HBSGC(pool=pool)
# save start time
clf.last_clock = time.clock()
clf.filter_calcs()
clf.data_calcs()
clf.star_model_calcs()
# if clf.calc_model_mags:
# clf.star_model_mags()
clf.gal_model_calcs()
# if clf.calc_model_mags:
# clf.gal_model_mags()
clf.fit_calcs()
clf.count_tot = 0
clf.sample()
clf.save_proba()
if clf.min_chi2_write:
clf.save_min_chi2()
pool.close()
示例8: ens_mpi_sample
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def ens_mpi_sample(gf, nwalkers, burn_steps, sample_steps, pos=None,
random_state=None):
pool = MPIPool(loadbalance=True)
if not pool.is_master():
pool.wait()
sys.exit(0)
# Initialize the parameter array with initial values (in log10 units)
# Number of parameters to estimate
ndim = (len(gf.builder.global_params) +
(len(gf.data) * len(gf.builder.local_params)))
# Initialize the walkers with starting positions drawn from the priors
# Note that the priors are in log10 scale already, so they don't
# need to be transformed here
if pos is None:
p0 = np.zeros((nwalkers, ndim))
for walk_ix in range(nwalkers):
for p_ix in range(ndim):
p0[walk_ix, p_ix] = gf.priors[p_ix].random()
else:
p0 = pos
# Create the sampler object
sampler = emcee.EnsembleSampler(nwalkers, ndim, posterior,
args=[gf], pool=pool)
if random_state is not None:
sampler.random_state = random_state
print "Burn in sampling..."
pos, prob, state = sampler.run_mcmc(p0, burn_steps, storechain=False)
sampler.reset()
print "Main sampling..."
sampler.run_mcmc(pos, sample_steps)
# Close the pool!
pool.close()
print "Done sampling."
return sampler
示例9: main
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def main():
##################
#These change a lot
numWaveforms = 12
numThreads = 8
ndim = 6*numWaveforms + 7
nwalkers = 25*ndim
iter=10000
burnIn = 9000
######################
doPlots = 1
# plt.ion()
fitSamples = 350
timeStepSize = 1. #ns
#Prepare detector
tempGuess = 79.310080
gradGuess = 0.05
pcRadGuess = 2.5
pcLenGuess = 1.6
#Create a detector model
detName = "conf/P42574A_grad%0.2f_pcrad%0.2f_pclen%0.2f.conf" % (0.05,2.5, 1.65)
det = Detector(detName, temperature=tempGuess, timeStep=timeStepSize, numSteps=fitSamples*10 )
det.LoadFields("P42574A_fields_v3.npz")
det.SetFields(pcRadGuess, pcLenGuess, gradGuess)
b_over_a = 0.107213
c = -0.821158
d = 0.828957
rc1 = 74.4
rc2 = 1.79
rcfrac = 0.992
det.SetTransferFunction(b_over_a, c, d, rc1, rc2, rcfrac)
tempIdx = -7
#and the remaining 4 are for the transfer function
fig_size = (20,10)
#Create a decent start guess by fitting waveform-by-waveform
wfFileName = "P42574A_12_fastandslow_oldwfs.npz"
# wfFileName = "P42574A_5_fast.npz"
if os.path.isfile(wfFileName):
data = np.load(wfFileName)
results = data['results']
wfs = data['wfs']
# wfs = wfs[::3]
# results = results[::3]
numWaveforms = wfs.size
else:
print "No saved waveforms available. Exiting."
exit(0)
#prep holders for each wf-specific param
r_arr = np.empty(numWaveforms)
phi_arr = np.empty(numWaveforms)
z_arr = np.empty(numWaveforms)
scale_arr = np.empty(numWaveforms)
t0_arr = np.empty(numWaveforms)
smooth_arr = np.ones(numWaveforms)*7.
simWfArr = np.empty((1,numWaveforms, fitSamples))
#Prepare the initial value arrays
for (idx, wf) in enumerate(wfs):
wf.WindowWaveformTimepoint(fallPercentage=.97, rmsMult=2,)
r_arr[idx], phi_arr[idx], z_arr[idx], scale_arr[idx], t0_arr[idx], smooth_arr[idx] = results[idx]['x']
# t0_arr[idx] -= 15
#Initialize the multithreading
# p = Pool(numThreads, initializer=initializeDetectorAndWaveforms, initargs=[det, wfs])
initializeDetectorAndWaveforms(det, wfs)
p = MPIPool()
if not p.is_master():
p.wait()
sys.exit(0)
#Do the MCMC
mcmc_startguess = np.hstack((r_arr[:], phi_arr[:], z_arr[:], scale_arr[:], t0_arr[:], smooth_arr[:], # waveform-specific params
tempGuess, b_over_a, c, d, rc1, rc2, rcfrac)) # detector-specific
#number of walkers _must_ be even
if nwalkers % 2:
nwalkers +=1
pos0 = [mcmc_startguess + 1e-2*np.random.randn(ndim)*mcmc_startguess for i in range(nwalkers)]
rc1idx = -3
rc2idx = -2
rcfracidx = -1
#Make sure everything in the initial guess is within bounds
#.........这里部分代码省略.........
示例10: main
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def main():
#################################################
############Option parsing#######################
#################################################
#Parse command line options
parser = argparse.ArgumentParser()
parser.add_argument("-f","--file",dest="options_file",action="store",type=str,help="analysis options file")
parser.add_argument("-v","--verbose",dest="verbose",action="store_true",default=False,help="turn on verbosity")
parser.add_argument("-vv","--verbose_plus",dest="verbose_plus",action="store_true",default=False,help="turn on additional verbosity")
parser.add_argument("-m","--mask_scale",dest="mask_scale",action="store_true",default=False,help="scale peaks and power spectrum to unmasked area")
parser.add_argument("-c","--cut_convergence",dest="cut_convergence",action="store",default=None,help="select convergence values in (min,max) to compute the likelihood. Safe for single descriptor only!!")
parser.add_argument("-g","--group_subfields",dest="group_subfields",action="store_true",default=False,help="group feature realizations by taking the mean over subfields, this makes a big difference in the covariance matrix")
parser.add_argument("-s","--save_features",dest="save_features",action="store_true",default=False,help="save features profiles")
parser.add_argument("-ss","--save",dest="save",action="store_true",default=False,help="save the best fits and corresponding chi2")
parser.add_argument("-p","--prefix",dest="prefix",action="store",default="",help="prefix of the emulator to pickle")
parser.add_argument("-l","--likelihood",dest="likelihood",action="store_true",default=False,help="save the likelihood cubes for the mocks")
parser.add_argument("-o","--observation",dest="observation",action="store_true",default=False,help="append the actual observation results to the mock results for direct comparison")
parser.add_argument("-d","--differentiate",dest="differentiate",action="store_true",default=False,help="differentiate the first minkowski functional to get the PDF")
cmd_args = parser.parse_args()
if cmd_args.options_file is None:
parser.print_help()
sys.exit(0)
#Set verbosity level
if cmd_args.verbose_plus:
logging.basicConfig(level=DEBUG_PLUS)
elif cmd_args.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
#Initialize MPI Pool
try:
pool = MPIPool()
except:
pool = None
if (pool is not None) and (not pool.is_master()):
pool.wait()
sys.exit(0)
if pool is not None:
logging.info("Started MPI Pool.")
#################################################################################################################
#################Info gathering: covariance matrix, observation and emulator#####################################
#################################################################################################################
#start
start = time.time()
last_timestamp = start
#Instantiate a FeatureLoader object that will take care of the memory loading
feature_loader = FeatureLoader(cmd_args)
###########################################################################################################################################
#Use this model for the covariance matrix (from the new set of 50 N body simulations)
covariance_model = CFHTcov.getModels(root_path=feature_loader.options.get("simulations","root_path"))
logging.info("Measuring covariance matrix from model {0}".format(covariance_model))
#Load in the covariance matrix
fiducial_feature_ensemble = feature_loader.load_features(covariance_model)
fiducial_features = fiducial_feature_ensemble.mean()
features_covariance = fiducial_feature_ensemble.covariance()
#timestamp
now = time.time()
logging.info("covariance loaded in {0:.1f}s".format(now-last_timestamp))
last_timestamp = now
################################################################################################################################################
#Treat the 50N-body simulation set as data
observation = CFHTcov.getModels(root_path=feature_loader.options.get("observations","root_path"))
logging.info("Measuring the observations from {0}".format(observation))
#And load the observations
observed_feature = feature_loader.load_features(observation)
#timestamp
now = time.time()
logging.info("observation loaded in {0:.1f}s".format(now-last_timestamp))
last_timestamp = now
################################################################################################################################################
#Create a LikelihoodAnalysis instance by unpickling one of the emulators
emulators_dir = os.path.join(feature_loader.options.get("analysis","save_path"),"emulators")
emulator_file = os.path.join(emulators_dir,"emulator{0}_{1}.p".format(cmd_args.prefix,output_string(feature_loader.feature_string)))
logging.info("Unpickling emulator from {0}...".format(emulator_file))
analysis = LikelihoodAnalysis.load(emulator_file)
#timestamp
now = time.time()
logging.info("emulator unpickled in {0:.1f}s".format(now-last_timestamp))
#.........这里部分代码省略.........
示例11: main
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def main(nw=1000,th=9,bi=500,fr=2000,thin=20,runmpi=True,local=False,
dil=None,codedir='/Users/tom/Projects/doug_hz/code',
ldfileloc='/Users/tom/Projects/doug_hz/code/'):
if runmpi:
pool = MPIPool()
if not pool.is_master():
pool.wait()
sys.exit(0)
else:
pool=None
#if not local:
#sys.path.append('/u/tsbarcl2/svn_code/tom_code/')
#ldfileloc = '/u/tsbarcl2/svn_code/tom_code/'
#elif local:
#sys.path.append('/Users/tom/svn_code/tom_code/')
#ldfileloc = '/Users/tom/svn_code/tom_code/'
if dil is None:
dil = 0.0
files = os.listdir('.')
dat_d = get_data(files)
rho_prior = True
ldp_prior = False
#mcmc params
nwalkers = nw
threads = th
burnin = bi
fullrun = fr
#use quadratic or 4 parameter limb darkening
n_ldparams = 2
#lc time offset from BJD-24548333.
toffset = (54832.5 + 67.)
#photometric zeropoint
zpt_0 = 1.E-10
#plot?
#doplot=False
################
M = tmod.transitemcee_fitldp(dat_d['nplanets'],dat_d['cadence'],
ldfileloc=ldfileloc)
#M.get_stellar(dat_d['teff'],dat_d['logg'],dat_d['feh'],n_ldparams)
M.get_stellar(dat_d['teff'],
dat_d['logg'],
dat_d['feh'],
n_ldparams,ldp_prior=ldp_prior)
M.already_open(dat_d['time'],
dat_d['flux'],dat_d['err'],
timeoffset=toffset,normalize=False)
rho_vals = np.array([dat_d['rho'],dat_d['rho_unc']])
M.get_rho(rho_vals,rho_prior)
M.get_zpt(zpt_0)
if dil is not None:
M.get_sol(*dat_d['sol_guess'],dil=dil)
else:
M.get_sol(*dat_d['sol_guess'])
M.cut_non_transit(8)
################
stophere = False
if not stophere:
#for threadnum in np.arange(2,32,2):
p0 = M.get_guess(nwalkers)
l_var = np.shape(p0)[1]
N = len([indval for indval in xrange(fullrun)
if indval%thin == 0])
outfile = 'koi{0}_np{1}_prior{2}_dil{3}.hdf5'.format(
dat_d['koi'],dat_d['nplanets'],rho_prior,dil)
with h5py.File(outfile, u"w") as f:
f.create_dataset("time", data=M.time)
f.create_dataset("flux", data=M.flux)
f.create_dataset("err", data=M.err)
f.create_dataset("itime", data=M._itime)
f.create_dataset("ntt", data = M._ntt)
f.create_dataset("tobs", data = M._tobs)
f.create_dataset("omc",data = M._omc)
f.create_dataset("datatype",data = M._datatype)
f.attrs["rho_0"] = M.rho_0
f.attrs["rho_0_unc"] = M.rho_0_unc
f.attrs["nplanets"] = M.nplanets
f.attrs["ld1"] = M.ld1
f.attrs["ld2"] = M.ld2
f.attrs["koi"] = dat_d['koi']
#.........这里部分代码省略.........
示例12: main
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def main():
#################################################
############Option parsing#######################
#################################################
#Parse command line options
parser = argparse.ArgumentParser()
parser.add_argument("-f","--file",dest="options_file",action="store",type=str,help="analysis options file")
parser.add_argument("-v","--verbose",dest="verbose",action="store_true",default=False,help="turn on verbosity")
parser.add_argument("-vv","--verbose_plus",dest="verbose_plus",action="store_true",default=False,help="turn on additional verbosity")
parser.add_argument("-m","--mask_scale",dest="mask_scale",action="store_true",default=False,help="scale peaks and power spectrum to unmasked area")
parser.add_argument("-c","--cut_convergence",dest="cut_convergence",action="store",default=None,help="select convergence values in (min,max) to compute the likelihood. Safe for single descriptor only!!")
parser.add_argument("-g","--group_subfields",dest="group_subfields",action="store_true",default=False,help="group feature realizations by taking the mean over subfields, this makes a big difference in the covariance matrix")
parser.add_argument("-s","--save_points",dest="save_points",action="store",default=None,help="save points in parameter space to external npy file")
parser.add_argument("-ss","--save_debug",dest="save_debug",action="store_true",default=False,help="save a bunch of debugging info for the analysis")
parser.add_argument("-p","--prefix",dest="prefix",action="store",default="",help="prefix of the emulator to pickle")
parser.add_argument("-r","--realizations",dest="realizations",type=int,default=None,help="use only the first N realizations to estimate the covariance matrix")
parser.add_argument("-d","--differentiate",dest="differentiate",action="store_true",default=False,help="differentiate the first minkowski functional to get the PDF")
cmd_args = parser.parse_args()
if cmd_args.options_file is None:
parser.print_help()
sys.exit(0)
#Set verbosity level
if cmd_args.verbose_plus:
logging.basicConfig(level=DEBUG_PLUS)
elif cmd_args.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
#Initialize MPI Pool
try:
pool = MPIPool()
except:
pool = None
if (pool is not None) and (not pool.is_master()):
pool.wait()
sys.exit(0)
if pool is not None:
logging.info("Started MPI Pool.")
#################################################################################################################
#################Info gathering: covariance matrix, observation and emulator#####################################
#################################################################################################################
#start
start = time.time()
last_timestamp = start
#Instantiate a FeatureLoader object that will take care of the memory loading
feature_loader = FeatureLoader(cmd_args)
###########################################################################################################################################
#Use this model for the covariance matrix (from the new set of 50 N body simulations)
covariance_model = CFHTcov.getModels(root_path=feature_loader.options.get("simulations","root_path"))
logging.info("Measuring covariance matrix from model {0}".format(covariance_model))
#Load in the covariance matrix
fiducial_feature_ensemble = feature_loader.load_features(covariance_model)
#Create a LikelihoodAnalysis instance by unpickling one of the emulators
emulators_dir = os.path.join(feature_loader.options.get("analysis","save_path"),"emulators")
emulator_file = os.path.join(emulators_dir,"emulator{0}_{1}.p".format(cmd_args.prefix,output_string(feature_loader.feature_string)))
logging.info("Unpickling emulator from {0}...".format(emulator_file))
analysis = LikelihoodAnalysis.load(emulator_file)
#Return
return fiducial_feature_ensemble,analysis
示例13: runModel
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def runModel():
pool = MPIPool()
if not pool.is_master():
pool.wait()
sys.exit(0)
# pool=None
observation = simulateData()
nTrans = len(observation["spectype"])
ndim, nwalkers = 8 + nTrans, 1000
# mns = numpy.concatenate(([inputs.Om0, inputs.w0, inputs.rate_II_r, inputs.logL_snIa, inputs.sigma_snIa, \
# inputs.logL_snII,inputs.sigma_snII,inputs.Z], -.35*numpy.zeros(nTrans)))
sigs = numpy.concatenate(
(
[
0.1,
0.2,
0.1,
uncertainties.logL_snIa,
uncertainties.sigma_snIa,
uncertainties.logL_snII,
uncertainties.sigma_snII,
uncertainties.Z,
],
0.05 + numpy.zeros(nTrans),
)
)
p0 = []
for i in range(nwalkers):
dum = numpy.random.rand(nTrans)
dum = numpy.array(numpy.round(dum), dtype="int")
lnL_init = dum + (1 - dum) * 0.5
lnL_init = numpy.log(lnL_init)
mns = numpy.concatenate(
(
[
inputs.Om0,
inputs.w0,
inputs.rate_II_r,
inputs.logL_snIa,
inputs.sigma_snIa,
inputs.logL_snII,
inputs.sigma_snII,
inputs.Z,
],
lnL_init,
)
)
p0.append((numpy.random.rand(ndim) - 0.5) * sigs + mns)
# p0 = [numpy.random.randn(ndim)*sigs + mns for i in range(nwalkers)]
dco = 1e-11 # measurement error very small
sampler = emcee.EnsembleSampler(
nwalkers,
ndim,
lnprob,
args=[
observation["counts"],
observation["specz"],
numpy.zeros(nTrans) + dco,
observation["zprob"],
observation["spectype"],
],
pool=pool,
)
sampler.run_mcmc(p0, 2000)
pool.close()
output = open("data.pkl", "wb")
pickle.dump(sampler.chain, output)
output.close()
示例14: main
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
def main():
#################################################
############Option parsing#######################
#################################################
#Parse command line options
parser = argparse.ArgumentParser()
parser.add_argument("-f","--file",dest="options_file",action="store",type=str,help="analysis options file")
parser.add_argument("-v","--verbose",dest="verbose",action="store_true",default=False,help="turn on verbosity")
parser.add_argument("-vv","--verbose_plus",dest="verbose_plus",action="store_true",default=False,help="turn on additional verbosity")
parser.add_argument("-m","--mask_scale",dest="mask_scale",action="store_true",default=False,help="scale peaks and power spectrum to unmasked area")
parser.add_argument("-c","--cut_convergence",dest="cut_convergence",action="store",default=None,help="select convergence values in (min,max) to compute the likelihood. Safe for single descriptor only!!")
parser.add_argument("-g","--group_subfields",dest="group_subfields",action="store_true",default=False,help="group feature realizations by taking the mean over subfields, this makes a big difference in the covariance matrix")
parser.add_argument("-s","--save_points",dest="save_points",action="store",default=None,help="save points in parameter space to external npy file")
parser.add_argument("-ss","--save_debug",dest="save_debug",action="store_true",default=False,help="save a bunch of debugging info for the analysis")
parser.add_argument("-p","--prefix",dest="prefix",action="store",default="",help="prefix of the emulator to pickle")
parser.add_argument("-r","--remove",dest="remove",action="store",type=int,default=24,help="model to remove from the analysis")
parser.add_argument("-R","--random",dest="random",action="store",type=int,default=0,help="random seed initialization for realization picking")
cmd_args = parser.parse_args()
if cmd_args.options_file is None:
parser.print_help()
sys.exit(0)
#Set verbosity level
if cmd_args.verbose_plus:
logging.basicConfig(level=DEBUG_PLUS)
elif cmd_args.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
#Initialize MPI Pool
try:
pool = MPIPool()
except:
pool = None
if (pool is not None) and (not pool.is_master()):
pool.wait()
sys.exit(0)
if pool is not None:
logging.info("Started MPI Pool.")
#################################################################################################################
#################Info gathering: covariance matrix, observation and emulator#####################################
#################################################################################################################
#start
start = time.time()
last_timestamp = start
#Instantiate a FeatureLoader object that will take care of the memory loading
feature_loader = FeatureLoader(cmd_args)
###########################################################################################################################################
#Get the names of all the simulated models available for the CFHT analysis, including smoothing scales and subfields
all_simulated_models = CFHTemu1.getModels(root_path=feature_loader.options.get("simulations","root_path"))
#Use this model for the covariance matrix
covariance_model = all_simulated_models[feature_loader.options.getint("analysis","covariance_model")]
logging.info("Measuring covariance matrix from model {0}".format(covariance_model))
#Load in the covariance matrix
features_covariance = feature_loader.load_features(covariance_model).covariance()
#timestamp
now = time.time()
logging.info("covariance loaded in {0:.1f}s".format(now-last_timestamp))
last_timestamp = now
################################################################################################################################################
#Create a LikelihoodAnalysis instance by unpickling one of the emulators
emulators_dir = os.path.join(feature_loader.options.get("analysis","save_path"),"emulators")
emulator_file = os.path.join(emulators_dir,"emulator{0}_{1}.p".format(cmd_args.prefix,output_string(feature_loader.feature_string)))
logging.info("Unpickling emulator from {0}...".format(emulator_file))
analysis = LikelihoodAnalysis.load(emulator_file)
#timestamp
now = time.time()
logging.info("emulator unpickled in {0:.1f}s".format(now-last_timestamp))
last_timestamp = now
##################################################################################################################################################
#Initialize random seed
np.random.seed(cmd_args.random)
realization = np.random.randint(0,1000)
#Treat the removed model as data
model_to_remove = all_simulated_models[cmd_args.remove]
parameters_to_remove = model_to_remove.squeeze()
logging.info("Treating model {0}, realization {1} as data, loading features...".format(model_to_remove,realization+1))
observed_feature = feature_loader.load_features(model_to_remove)[np.random.randint(0,1000)]
#Compute the chi2 for this observed feature without removing it from the emulator (must be close to 0)
#.........这里部分代码省略.........
示例15: EnsembleSampler
# 需要导入模块: from emcee.utils import MPIPool [as 别名]
# 或者: from emcee.utils.MPIPool import wait [as 别名]
class EnsembleSampler(GenericSampler):
def __init__(self, num_walkers=None, num_steps=5000, num_burn=2000,
temp_dir=None, save_interval=300):
""" Uses ``emcee`` and the `EnsembleSampler
<http://dan.iel.fm/emcee/current/api/#emcee.EnsembleSampler>`_ to fit the supplied
model.
This method sets an emcee run using the ``EnsembleSampler`` and manual
chain management to allow for low to medium dimensional models. MPI running
is detected automatically for less hassle, and chain progress is serialised
to disk automatically for convenience.
Parameters
----------
num_walkers : int, optional
The number of walkers to run. If not supplied, it defaults to eight times the
framework dimensionality
num_steps : int, optional
The number of steps to run
num_burn : int, optional
The number of steps to discard for burn in
temp_dir : str
If set, specifies a directory in which to save temporary results, like the emcee chain
save_interval : float
The amount of seconds between saving the chain to file. Setting to ``None``
disables serialisation.
"""
self.logger = logging.getLogger(__name__)
import emcee
self.chain = None
self.pool = None
self.master = True
self.num_steps = num_steps
self.num_burn = num_burn
self.temp_dir = temp_dir
if temp_dir is not None and not os.path.exists(temp_dir):
os.makedirs(temp_dir)
self.save_interval = save_interval
self.num_walkers = num_walkers
def fit(self, kwargs):
""" Runs the sampler over the model and returns the flat chain of results
Parameters
----------
kwargs : dict
Containing the following information at a minimum:
- log_posterior : function
A function which takes a list of parameters and returns
the log posterior
- start : function|list|ndarray
Either a starting position, or a function that can be called
to generate a starting position
- save_dims : int, optional
Only return values for the first ``save_dims`` parameters.
Useful to remove numerous marginalisation parameters if running
low on memory or hard drive space.
- uid : str, optional
A unique identifier used to differentiate different fits
if two fits both serialise their chains and use the
same temporary directory
Returns
-------
dict
A dictionary with key "chains" containing the final
flattened chain of dimensions
``(num_dimensions, num_walkers * (num_steps - num_burn))``
"""
log_posterior = kwargs.get("log_posterior")
start = kwargs.get("start")
save_dims = kwargs.get("save_dims")
uid = kwargs.get("uid")
assert log_posterior is not None
assert start is not None
from emcee.utils import MPIPool
import emcee
try: # pragma: no cover
self.pool = MPIPool()
if not self.pool.is_master():
self.logger.info("Slave waiting")
self.master = False
self.pool.wait()
sys.exit(0)
else:
self.logger.info("MPIPool successful initialised and master found. "
"Running with %d cores." % self.pool.size)
except ImportError:
self.logger.info("mpi4py is not installed or not configured properly. "
"Ignore if running through python, not mpirun")
except ValueError as e: # pragma: no cover
self.logger.info("Unable to start MPI pool, expected normal python execution")
self.logger.info(str(e))
if callable(start):
num_dim = start().size
else:
num_dim = start.size
#.........这里部分代码省略.........