Commit 6bbcc99d authored by Gregory Ashton's avatar Gregory Ashton

Clean up Emcee and color burn-in walkers

parent 307ab12a
Pipeline #22966 passed with stages
in 10 minutes and 38 seconds
......@@ -754,23 +754,23 @@ class Pymultinest(Sampler):
class Emcee(Sampler):
def _run_external_sampler(self):
nwalkers = self.kwargs.pop('nwalkers', 100)
nsteps = self.kwargs.pop('nsteps', 100)
nburn = self.kwargs.pop('nburn', 50)
self.nwalkers = self.kwargs.pop('nwalkers', 100)
self.nsteps = self.kwargs.pop('nsteps', 100)
self.nburn = self.kwargs.pop('nburn', 50)
emcee = self.external_sampler
tqdm = utils.get_progress_bar(self.kwargs.pop('tqdm', 'tqdm'))
sampler = emcee.EnsembleSampler(
nwalkers=nwalkers, dim=self.ndim, lnpostfn=self.lnpostfn,
nwalkers=self.nwalkers, dim=self.ndim, lnpostfn=self.lnpostfn,
**self.kwargs)
pos0 = [self.get_random_draw_from_prior() for i in range(nwalkers)]
pos0 = [self.get_random_draw_from_prior() for i in range(self.nwalkers)]
for result in tqdm(
sampler.sample(pos0, iterations=nsteps), total=nsteps):
sampler.sample(pos0, iterations=self.nsteps), total=self.nsteps):
pass
self.result.sampler_output = np.nan
self.result.samples = sampler.chain[:, nburn:, :].reshape(
self.result.samples = sampler.chain[:, self.nburn:, :].reshape(
(-1, self.ndim))
self.result.walkers = sampler.chain[:, :, :]
self.result.log_evidence = np.nan
......@@ -786,12 +786,22 @@ class Emcee(Sampler):
def lnpostfn(self, theta):
return self.log_likelihood(theta) + self.log_prior(theta)
def _get_walkers_to_plot(self):
return self.result.walkers[:, :, :]
def plot_walkers(self, save=True, **kwargs):
nwalkers, nsteps, ndim = self.result.walkers.shape
idxs = np.arange(nsteps)
fig, axes = plt.subplots(nrows=ndim, figsize=(6, 3*self.ndim))
walkers = self._get_walkers_to_plot()
for i, ax in enumerate(axes):
ax.plot(idxs[:self.nburn+1], walkers[:, :self.nburn+1, i].T,
lw=0.1, color='r')
ax.set_ylabel(self.result.parameter_labels[i])
for i, ax in enumerate(axes):
ax.plot(idxs, self.result.walkers[:, :, i].T, lw=0.1, color='k')
ax.plot(idxs[self.nburn:], walkers[:, self.nburn:, i].T, lw=0.1,
color='k')
ax.set_ylabel(self.result.parameter_labels[i])
fig.tight_layout()
......@@ -800,30 +810,31 @@ class Emcee(Sampler):
fig.savefig(filename)
class Ptemcee(Sampler):
class Ptemcee(Emcee):
def _run_external_sampler(self):
ntemps = self.kwargs.pop('ntemps', 2)
nwalkers = self.kwargs.pop('nwalkers', 100)
nsteps = self.kwargs.pop('nsteps', 100)
nburn = self.kwargs.pop('nburn', 50)
self.ntemps = self.kwargs.pop('ntemps', 2)
self.nwalkers = self.kwargs.pop('nwalkers', 100)
self.nsteps = self.kwargs.pop('nsteps', 100)
self.nburn = self.kwargs.pop('nburn', 50)
ptemcee = self.external_sampler
tqdm = utils.get_progress_bar(self.kwargs.pop('tqdm', 'tqdm'))
sampler = ptemcee.Sampler(
ntemps=ntemps, nwalkers=nwalkers, dim=self.ndim,
ntemps=self.ntemps, nwalkers=self.nwalkers, dim=self.ndim,
logl=self.log_likelihood, logp=self.log_prior,
**self.kwargs)
pos0 = [[self.get_random_draw_from_prior()
for i in range(nwalkers)]
for j in range(ntemps)]
for i in range(self.nwalkers)]
for j in range(self.ntemps)]
for result in tqdm(
sampler.sample(pos0, iterations=nsteps, adapt=True), total=nsteps):
sampler.sample(pos0, iterations=self.nsteps, adapt=True),
total=self.nsteps):
pass
self.result.sampler_output = np.nan
self.result.samples = sampler.chain[0, :, nburn:, :].reshape(
self.result.samples = sampler.chain[0, :, self.nburn:, :].reshape(
(-1, self.ndim))
self.result.walkers = sampler.chain[0, :, :, :]
self.result.log_evidence = np.nan
......@@ -833,19 +844,6 @@ class Ptemcee(Sampler):
logging.info("Tswap frac = {}".format(sampler.tswap_acceptance_fraction))
return self.result
def plot_walkers(self, save=True, **kwargs):
nwalkers, nsteps, ndim = self.result.walkers.shape
idxs = np.arange(nsteps)
fig, axes = plt.subplots(nrows=ndim, figsize=(6, 3*self.ndim))
for i, ax in enumerate(axes):
ax.plot(idxs, self.result.walkers[:, :, i].T, lw=0.1, color='k')
ax.set_ylabel(self.result.parameter_labels[i])
fig.tight_layout()
filename = '{}/{}_walkers.png'.format(self.outdir, self.label)
logging.debug('Saving walkers plot to {}'.format('filename'))
fig.savefig(filename)
def run_sampler(likelihood, priors=None, label='label', outdir='outdir',
sampler='dynesty', use_ratio=None, injection_parameters=None,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment