Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
in_root = ini.params['file_root']
if not in_root:
do_error('Chain Root file name not given ')
rootname = os.path.basename(in_root)
if args.ignore_rows is not None:
ignorerows = args.ignore_rows
else:
ignorerows = ini.float('ignore_rows', 0.0)
samples_are_chains = ini.bool('samples_are_chains', True)
paramnames = ini.string('parameter_names', '')
# Create instance of MCSamples
mc = MCSamples(in_root, ini=ini, files_are_chains=samples_are_chains, paramNamesFile=paramnames)
if ini.bool('adjust_priors', False) or ini.bool('map_params', False):
do_error('To adjust priors or define new parameters, use a separate python script; '
'see the python getdist docs for examples')
plot_ext = ini.string('plot_ext', 'py')
finish_run_command = ini.string('finish_run_command', '')
no_plots = ini.bool('no_plots', no_plots)
plots_only = ini.bool('plots_only', False)
no_tests = plots_only or ini.bool('no_tests', False)
thin_factor = ini.int('thin_factor', 0)
thin_cool = ini.float('thin_cool', 1.0)
cool = ini.float('cool', 1.0)
print()
for log_dir in log_dim_dir:
with open(os.path.join(log_dir, 'info', 'params.txt')) as f:
data = json.load(f)
if os.path.exists(os.path.join(log_dir, 'chains', 'chain.txt')):
names = ['p%i' % i for i in range(int(data['x_dim']))]
labels = [r'x_{%i}' % i for i in range(int(data['x_dim']))]
files = getdist.chains.chainFiles(os.path.join(log_dir, 'chains', 'chain.txt'))
if data['sampler'] == 'nested':
mc = getdist.MCSamples(os.path.join(log_dir, 'chains', 'chain.txt'), names=names, labels=labels,
ignore_rows=0.0, sampler='nested')
else:
mc = getdist.MCSamples(os.path.join(log_dir, 'chains', 'chain.txt'), names=names, labels=labels,
ignore_rows=0.3)
mc.readChains(files)
print(mc.getMargeStats())
if not args.no_plot:
g = getdist.plots.getSubplotPlotter()
g.triangle_plot(mc, filled=True)
g.export(os.path.join(os.path.join(log_dir, 'plots', 'triangle.png')))
if data['sampler'] == 'nested':
if os.path.exists(os.path.join(log_dir, 'results', 'final.csv')):
results = pd.read_csv(os.path.join(log_dir, 'results', 'final.csv'))
print(results)
logzs.append(results['logz'])
dlogzs.append(results['logzerr'])
nlikes.append(results['ncall'])
print('--------')
print('Dim: %s' % x_dims[ix])
print('--------')
print()
for log_dir in log_dim_dir:
with open(os.path.join(log_dir, 'info', 'params.txt')) as f:
data = json.load(f)
if os.path.exists(os.path.join(log_dir, 'chains', 'chain.txt')):
names = ['p%i' % i for i in range(int(data['x_dim']))]
labels = [r'x_{%i}' % i for i in range(int(data['x_dim']))]
files = getdist.chains.chainFiles(os.path.join(log_dir, 'chains', 'chain.txt'))
if data['sampler'] == 'nested':
mc = getdist.MCSamples(os.path.join(log_dir, 'chains', 'chain.txt'), names=names, labels=labels,
ignore_rows=0.0, sampler='nested')
else:
mc = getdist.MCSamples(os.path.join(log_dir, 'chains', 'chain.txt'), names=names, labels=labels,
ignore_rows=0.3)
mc.readChains(files)
print(mc.getMargeStats())
if not args.no_plot:
g = getdist.plots.getSubplotPlotter()
g.triangle_plot(mc, filled=True)
g.export(os.path.join(os.path.join(log_dir, 'plots', 'triangle.png')))
if data['sampler'] == 'nested':
if os.path.exists(os.path.join(log_dir, 'results', 'final.csv')):
results = pd.read_csv(os.path.join(log_dir, 'results', 'final.csv'))
print(results)
def triangle_plot(self):
if not HAS_GETDIST:
raise RuntimeError(
'you need to install getdist to get the triangle plot.')
if 0 < self.m_plot < self.dim:
plot_data = self._data[:, :self.m_plot]
else:
plot_data = self._data
samples = MCSamples(samples=plot_data)
g = plots.getSubplotPlotter()
g.triangle_plot([samples,], filled=True, contour_args={'alpha':0.8},
diag1d_kwargs={'normalized':True})
if self.i_iter:
plt.suptitle("triangle plot after iteration " + str(self.i_iter),
fontsize=plot_data.shape[-1] * 4, ha='left')
else:
plt.suptitle('triangle plot for the initial data',
fontsize=plot_data.shape[-1] * 4, ha='left')
plt.show()
def triangle_plot(self, samples = None, weights = None, savefig = False, filename = None):
# Set samples to the posterior samples by default
if samples is None:
samples = self.posterior_samples
mc_samples = [MCSamples(samples=s, weights=weights[i], names=self.names, labels=self.labels, ranges=self.ranges) for i, s in enumerate(samples)]
# Triangle plot
plt.close()
with mpl.rc_context():
g = plots.getSubplotPlotter(width_inch = 12)
g.settings.figure_legend_frame = False
g.settings.alpha_filled_add=0.6
g.settings.axes_fontsize=14
g.settings.legend_fontsize=16
g.settings.lab_fontsize=20
g.triangle_plot(mc_samples, filled_compare=True, normalized=True)
for i in range(0, len(samples[0][0,:])):
for j in range(0, i+1):
ax = g.subplots[i,j]
xtl = ax.get_xticklabels()
ax.set_xticklabels(xtl, rotation=45)