Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_test_program(plots=['dists_2D', 'dists_1D', 'ISE_1D', 'ISE_2D'], sims=100, nsamp=default_nsamp, mbc=1, bco=1):
import time
chains.print_load_details = False
plt.rc("ytick", direction="in")
plt.rc("xtick", direction="in")
test1D = Test1DDistributions()
test2D = Test2DDistributions()
test_settings = {'mult_bias_correction_order': mbc, 'boundary_correction_order': bco,
'smooth_scale_1D': -1, 'smooth_scale_2D': -1}
g = getSubplotPlotter(subplot_size=2)
colors = ['k', 'C0', 'C1', 'C2', 'C3', 'C4']
if 'ISE_1D' in plots:
compare_method(test1D.distributions(), nx=3,
test_settings=[{'mult_bias_correction_order': 1, 'boundary_correction_order': 1},
{'mult_bias_correction_order': 2, 'boundary_correction_order': 1},
{'mult_bias_correction_order': 0, 'boundary_correction_order': 0},
{'mult_bias_correction_order': 0, 'boundary_correction_order': 1},
{'mult_bias_correction_order': 0, 'boundary_correction_order': 2},
], colors=colors, linestyles=['-', '-', ':', '-.', '--'],
fname='compare_method_1d_N%s.pdf' % nsamp,
sims=sims, nsamp=nsamp
)
if 'ISE_2D' in plots:
import time
import argparse
parser = argparse.ArgumentParser(description='make getdist test plots from test Gaussian mixture distributions')
parser.add_argument('--sims', type=int, default=100, help='Number of simulations per case')
parser.add_argument('--nsamp', type=int, default=10000, help='Number of (independent) samples per simulation')
parser.add_argument('--plots', nargs='*', default=['dists_1D', 'dists_2D'], help='names of plots to make')
parser.add_argument('--mbc', type=int, default=1, help='mult_bias_correction_order')
parser.add_argument('--bco', type=int, default=1, help='boundary_correction_order')
args = parser.parse_args()
test1D = Test1DDistributions()
test2D = Test2DDistributions()
test_settings = {'mult_bias_correction_order':args.mbc, 'boundary_correction_order':args.bco, 'smooth_scale_1D':-1, 'smooth_scale_2D':-1}
g = plots.getSubplotPlotter(subplot_size=2)
if 'ISE_1D' in args.plots:
compare_method(test1D.distributions(), nx=3,
test_settings=[ {'mult_bias_correction_order':1, 'boundary_correction_order':1},
{'mult_bias_correction_order':2, 'boundary_correction_order':1},
{'mult_bias_correction_order':0, 'boundary_correction_order':0},
{'mult_bias_correction_order':0, 'boundary_correction_order':1},
{'mult_bias_correction_order':0, 'boundary_correction_order':2},
], colors=['k', 'b', 'r', 'm', 'c', 'g'], linestyles=['-', '-', ':', '-.', '--'],
fname='compare_method_1d_N%s.pdf' % args.nsamp,
sims=args.sims, nsamp=args.nsamp
)
if 'ISE_2D' in args.plots:
compare_method(test2D.distributions(), nx=4,
test_settings=[ {'mult_bias_correction_order':1, 'boundary_correction_order':1},
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'])
if len(logzs) > 1:
print()
print(r'Log Z: $%4.2f \pm %4.2f$' % (np.mean(logzs), np.std(logzs)))
print(r'Log Z error estimate: $%4.2f \pm %4.2f$' % (np.mean(dlogzs), np.std(dlogzs)))
print(r'N_like: $%.0f \pm %.0f$' % (np.mean(nlikes), np.std(nlikes)))
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)
plt.subplots_adjust(hspace=0, wspace=0)
if savefig:
plt.savefig(filename, bbox_inches='tight')
if self.show_plot: