def test_plot_2d_types(): np.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') params_x = ['x0', 'x1', 'x2', 'x3'] params_y = ['x0', 'x1', 'x2'] params = [params_x, params_y] fig, axes = ns.plot_2d(params, types={'lower': 'kde'}) assert((~axes.isnull()).sum().sum() == 3) fig, axes = ns.plot_2d(params, types={'upper': 'scatter'}) assert((~axes.isnull()).sum().sum() == 6) fig, axes = ns.plot_2d(params, types={'upper': 'kde', 'diagonal': 'kde'}) assert((~axes.isnull()).sum().sum() == 9) fig, axes = ns.plot_2d(params, types={'lower': 'kde', 'diagonal': 'kde'}) assert((~axes.isnull()).sum().sum() == 6) fig, axes = ns.plot_2d(params, types={'lower': 'kde', 'diagonal': 'kde'}) assert((~axes.isnull()).sum().sum() == 6) fig, axes = ns.plot_2d(params, types={'lower': 'kde', 'diagonal': 'kde', 'upper': 'scatter'}) assert((~axes.isnull()).sum().sum() == 12) with pytest.raises(NotImplementedError): fig, axes = ns.plot_2d(params, types={'lower': 'not a plot type'}) with pytest.raises(NotImplementedError): fig, axes = ns.plot_2d(params, types={'diagonal': 'not a plot type'}) plt.close("all")
def sample(self, nlive=500): """Generate samples from a perfect nested sampling run.""" points = np.zeros((0, self.D)) death_likes = np.zeros(0) birth_likes = np.zeros(0) live_points = np.random.rand(nlive, self.D) live_likes = self.logL(live_points) live_birth_likes = np.ones(nlive)*-np.inf while True: logL_ = live_likes.min() j = live_likes == logL_ death_likes = np.concatenate([death_likes, live_likes[j]]) birth_likes = np.concatenate([birth_likes, live_birth_likes[j]]) points = np.concatenate([points, live_points[j]]) i_ = self.i(live_points[j][0])+1 r_ = self.alpha**(i_/self.D)/2 x_ = np.random.uniform(0.5-r_, 0.5+r_, size=(j.sum(), self.D)) live_birth_likes[j] = logL_ live_points[j] = x_ live_likes[j] = self.logL(x_) samps = NestedSamples(points, logL=death_likes, logL_birth=birth_likes) if samps.live_points().weights.sum()/samps.weights.sum() < 0.001: break death_likes = np.concatenate([death_likes, live_likes]) birth_likes = np.concatenate([birth_likes, live_birth_likes]) points = np.concatenate([points, live_points]) return NestedSamples(points, logL=death_likes, logL_birth=birth_likes)
def test_copy(): np.random.seed(3) pc = NestedSamples(root='./tests/example_data/pc') new = pc.copy() assert new is not pc assert new.tex is not pc.tex assert new.limits is not pc.limits
def test_xmin_xmax_1d(): """Test to provide a solution to #89""" np.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') fig, ax = ns.plot_1d('x0', plot_type='hist') assert ax['x0'].get_xlim() != (-1, 1) fig, ax = ns.plot_1d('x0', plot_type='hist', xmin=-1, xmax=1) assert ax['x0'].get_xlim() == (-1, 1)
def test_hist_levels(): numpy.random.seed(3) mcmc = NestedSamples(root='./tests/example_data/pc') mcmc.plot_2d(['x0', 'x1', 'x2', 'x3'], types={'lower': 'hist'}, levels=[0.68, 0.95], bins=20) plt.close("all")
def run_iteration(file_root, prior, log_likelihood, nDims=2, nDerived=0): global settings settings.file_root = file_root settings.nDims = nDims settings.nDerived = nDerived settings.feedback = 0 run_polychord(log_likelihood, nDims, nDerived, settings, prior) _samples = NestedSamples(root='chains/' + file_root) _samples.rename(columns={0: 'm', 1: 'c'}, inplace=True) return _samples
def test_p_values_from_sample(): np.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') ns._compute_insertion_indexes() nlive = len(ns.live_points()) ks_results = insertion_p_value(ns.insertion[nlive:-nlive], nlive) assert ks_results['p-value'] > 0.05 ks_results = insertion_p_value(ns.insertion[nlive:-nlive], nlive, batch=1) assert ks_results['p-value'] > 0.05
def test_recompute(): np.random.seed(3) pc = NestedSamples(root='./tests/example_data/pc') recompute = pc.recompute() assert recompute is not pc pc.loc[1000, 'logL'] = pc.logL_birth.iloc[1000]-1 with pytest.raises(RuntimeError): pc.recompute() mn = NestedSamples(root='./tests/example_data/mn_old') with pytest.raises(RuntimeError): mn.recompute()
def test_weighted_merging(): # Generate some data to try it out: samples_1 = NestedSamples(root='./tests/example_data/pc') samples_2 = NestedSamples(root='./tests/example_data/pc_250') samples_1['xtest'] = 7*samples_1['x3'] samples_2['xtest'] = samples_2['x3'] mean1 = samples_1.mean()['xtest'] mean2 = samples_2.mean()['xtest'] # Test with evidence weights weight1 = np.exp(samples_1.logZ()) weight2 = np.exp(samples_2.logZ()) samples = merge_samples_weighted([samples_1, samples_2]) mean = samples.mean()['xtest'] assert np.isclose(mean, (mean1*weight1+mean2*weight2)/(weight1+weight2)) # Test with explicit weights weight1 = 31 weight2 = 13 samples = merge_samples_weighted( [samples_1, samples_2], weights=[weight1, weight2]) mean = samples.mean()['xtest'] assert np.isclose(mean, (mean1*weight1+mean2*weight2)/(weight1+weight2)) # Test if correct exceptions are raised: # MCMCSamples are passed without weights with pytest.raises(ValueError): merge_samples_weighted([MCMCSamples(samples_1)]) # len(weights) != len(samples) with pytest.raises(ValueError): merge_samples_weighted([samples_1, samples_2], weights=[1, 2, 3]) # A samples is passed and not a sequence with pytest.raises(TypeError): merge_samples_weighted(samples_1, weights=[1, 2, 3])
def test_gui(): plotter = NestedSamples(root='./tests/example_data/pc').gui() # Type buttons plotter.type.buttons.set_active(0) assert (plotter.type() == 'live') plotter.type.buttons.set_active(1) assert (plotter.type() == 'posterior') # Parameter choice buttons plotter.param_choice.buttons.set_active(1) assert (len(plotter.triangle.ax) == 2) plotter.param_choice.buttons.set_active(0) assert (len(plotter.triangle.ax) == 1) plotter.param_choice.buttons.set_active(0) plotter.param_choice.buttons.set_active(2) plotter.param_choice.buttons.set_active(3) assert (len(plotter.triangle.ax) == 4) # Sliders plotter.evolution.slider.set_val(100) assert (plotter.evolution() == 100) plotter.type.buttons.set_active(1) plotter.temperature.slider.set_val(0) assert (plotter.temperature() == 1) plotter.temperature.slider.set_val(1) assert (plotter.temperature() == 10) plotter.temperature.slider.set_val(2) assert (plotter.temperature() == 100) plotter.type.buttons.set_active(0)
def test_different_parameters(): np.random.seed(3) params_x = ['x0', 'x1', 'x2', 'x3', 'x4'] params_y = ['x0', 'x1', 'x2'] fig, axes = make_1d_axes(params_x) ns = NestedSamples(root='./tests/example_data/pc') ns.plot_1d(axes) fig, axes = make_2d_axes(params_y) ns.plot_2d(axes) fig, axes = make_2d_axes(params_x) ns.plot_2d(axes) fig, axes = make_2d_axes([params_x, params_y]) ns.plot_2d(axes) plt.close('all')
def test_manual_columns(): old_params = ['x0', 'x1', 'x2', 'x3', 'x4'] mcmc_params = ['logL'] ns_params = ['logL', 'logL_birth', 'nlive'] mcmc = MCMCSamples(root='./tests/example_data/gd') ns = NestedSamples(root='./tests/example_data/pc') assert_array_equal(mcmc.columns, old_params + mcmc_params) assert_array_equal(ns.columns, old_params + ns_params) new_params = ['y0', 'y1', 'y2', 'y3', 'y4'] mcmc = MCMCSamples(root='./tests/example_data/gd', columns=new_params) ns = NestedSamples(root='./tests/example_data/pc', columns=new_params) assert_array_equal(mcmc.columns, new_params + mcmc_params) assert_array_equal(ns.columns, new_params + ns_params)
def test_logzero_mask_prior_level(): np.random.seed(3) ns0 = NestedSamples(root='./tests/example_data/pc') pi0 = ns0.set_beta(0) NS0 = ns0.ns_output(nsamples=2000) mask = ((ns0.x0 > -0.3) & (ns0.x2 > 0.2) & (ns0.x4 < 3.5)).to_numpy() V_prior = pi0[mask].weights.sum() / pi0.weights.sum() V_posterior = ns0[mask].weights.sum() / ns0.weights.sum() logZ_V = NS0.logZ.mean() + np.log(V_posterior) - np.log(V_prior) ns1 = merge_nested_samples((ns0[mask],)) NS1 = ns1.ns_output(nsamples=2000) assert abs(NS1.logZ.mean() - logZ_V) < 1.5 * NS1.logZ.std()
def test_live_points(): numpy.random.seed(4) pc = NestedSamples(root="./tests/example_data/pc") for i, logL in pc.logL.iteritems(): live_points = pc.live_points(logL) assert len(live_points) == pc.nlive[i] live_points_from_int = pc.live_points(i) assert_array_equal(live_points_from_int, live_points) last_live_points = pc.live_points() logL = pc.logL_birth.max() assert (last_live_points.logL > logL).all() assert len(last_live_points) == pc.nlive.mode()[0]
def test_compute_insertion(): np.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') assert 'insertion' not in ns ns._compute_insertion_indexes() assert 'insertion' in ns nlive = ns.nlive.mode()[0] assert_array_less(ns.insertion, nlive) u = ns.insertion.values/nlive assert kstest(u[nlive:-nlive], 'uniform').pvalue > 0.05 pvalues = [kstest(u[i:i+nlive], 'uniform').pvalue for i in range(nlive, len(ns)-2*nlive, nlive)] assert kstest(pvalues, 'uniform').pvalue > 0.05
def test_root_and_label(): np.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') assert(ns.root == './tests/example_data/pc') assert(ns.label == 'pc') ns = NestedSamples() assert(ns.root is None) assert(ns.label is None) mc = MCMCSamples(root='./tests/example_data/gd') assert (mc.root == './tests/example_data/gd') assert(mc.label == 'gd') mc = MCMCSamples() assert(mc.root is None) assert(mc.label is None)
def exec_polychord(series_name, settings, loglike, prior): print('running {}'.format(series_name)) settings.file_root = series_name output = pypolychord.run_polychord(loglike, settings.nDims, settings.nDerived, settings, prior) sample = NestedSamples(root='./chains/{}'.format(settings.file_root)) outputs[series_name] = output samples[series_name] = sample return output, sample
def test_equal_min_max(): """Test to provide a solution to #89""" np.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') with pytest.raises(ValueError): ns.plot_2d(['x0', 'x1', 'x2'], xmin=3, xmax=3) ns.limits['x0'] = (3, 3) ns.plot_2d(['x0', 'x1', 'x2'])
def nested_sample(self, **kwargs): self.test_log_like() self.test_quantile() _settings = self.setup_settings(**kwargs) output = run_polychord(self.log_likelihood, self.dimensionality, self.num_derived, _settings, self.quantile) try: samples = NestedSamples( root=f'./chains/{_settings.file_root}') except ValueError as e: print(e) samples = None return output, samples
def test_limit_assignment(): np.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') # `None` in .ranges file: assert ns.limits['x0'][0] is None assert ns.limits['x0'][1] is None # parameter not listed in .ranges file: assert ns.limits['x1'][0] == ns.x1.min() assert ns.limits['x1'][1] == ns.x1.max() # `None` for only one limit in .ranges file: assert ns.limits['x2'][0] == 0 assert ns.limits['x2'][1] is None # both limits specified in .ranges file: assert ns.limits['x3'][0] == 0 assert ns.limits['x3'][1] == 1 # limits for logL, weights, nlive assert ns.limits['logL'][0] == -777.0115456428716 assert ns.limits['logL'][1] == 5.748335384373301 assert ns.limits['nlive'][0] == 1 assert ns.limits['nlive'][1] == 125 # limits for derived parameters: ns['x5'] = ns.x0 + ns.x1 assert 'x5' in ns.columns and 'x5' not in ns.limits ns.plot_1d(['x5']) assert 'x5' in ns.columns and 'x5' in ns.limits ns['x6'] = ns.x2 + ns.x3 assert 'x6' in ns.columns and 'x6' not in ns.limits ns.plot_2d(['x5', 'x6']) assert 'x6' in ns.columns and 'x6' in ns.limits
def test_beta(): pc = NestedSamples(root="./tests/example_data/pc") weights = pc.weights assert_array_equal(weights, pc.weights) assert_array_equal(pc.index.get_level_values('weights'), pc.weights) assert pc.beta == 1 prior = pc.set_beta(0) assert prior.beta == 0 assert_array_equal(prior.index.get_level_values('weights'), prior.weights) assert pc.beta == 1 assert_array_equal(pc.index.get_level_values('weights'), pc.weights) assert_array_almost_equal(sorted(prior.weights, reverse=True), prior.weights) for beta in np.linspace(0, 2, 10): pc.set_beta(beta, inplace=True) assert pc.beta == beta assert_array_equal(pc.index.get_level_values('weights'), pc.weights) assert not np.array_equal(pc.index.get_level_values('weights'), weights) for beta in np.linspace(0, 2, 10): pc.beta = beta assert pc.beta == beta assert_array_equal(pc.index.get_level_values('weights'), pc.weights) assert not np.array_equal(pc.index.get_level_values('weights'), weights)
def test_contour_plot_2d_nan(): """Contour plots with nans arising from issue #96""" np.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') ns.loc[:9, 'x0'] = np.nan with pytest.raises((np.linalg.LinAlgError, RuntimeError, ValueError)): ns.plot_2d(['x0', 'x1']) # Check this error is removed in the case of zero weights weights = ns.weights weights[:10] = 0 ns.weights = weights ns.plot_2d(['x0', 'x1'])
def test_plot_2d_types_multiple_calls(): np.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') params = ['x0', 'x1', 'x2', 'x3'] fig, axes = ns.plot_2d(params, types={'diagonal': 'kde', 'lower': 'kde', 'upper': 'scatter'}) ns.plot_2d(axes, types={'diagonal': 'hist'}) fig, axes = ns.plot_2d(params, types={'diagonal': 'hist'}) ns.plot_2d(axes, types={'diagonal': 'kde', 'lower': 'kde', 'upper': 'scatter'}) plt.close('all')
def test_merging(): numpy.random.seed(3) samples_1 = NestedSamples(root='./tests/example_data/pc') samples_2 = NestedSamples(root='./tests/example_data/pc_250') samples = merge_nested_samples([samples_1, samples_2]) nlive_1 = samples_1.nlive.mode()[0] nlive_2 = samples_2.nlive.mode()[0] nlive = samples.nlive.mode()[0] assert nlive_1 == 125 assert nlive_2 == 250 assert nlive == nlive_1 + nlive_2 assert (samples.logZ() < samples_1.logZ() and samples.logZ() > samples_2.logZ() or samples.logZ() > samples_1.logZ() and samples.logZ() < samples_2.logZ())
def test_beta_with_logL_infinities(): ns = NestedSamples(root="./tests/example_data/pc") for i in range(10): ns.loc[i, 'logL'] = -np.inf prior = ns.set_beta(0) assert np.all(prior.logL[:10] == -np.inf) assert np.all(prior.weights[:10] == 0) ns.plot_1d(['x0', 'x1'])
def test_plot_2d_types(): numpy.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') params_x = ['x0', 'x1', 'x2', 'x3'] params_y = ['x0', 'x1', 'x2'] params = [params_x, params_y] # Test old interface fig, axes = ns.plot_2d(params, types=['kde', 'scatter']) assert ((~axes.isnull()).sum().sum() == 12) fig, axes = ns.plot_2d(params, types='kde') assert ((~axes.isnull()).sum().sum() == 6) fig, axes = ns.plot_2d(params, types='kde', diagonal=False) assert ((~axes.isnull()).sum().sum() == 3) # Test new interface fig, axes = ns.plot_2d(params, types={'lower': 'kde'}) assert ((~axes.isnull()).sum().sum() == 3) fig, axes = ns.plot_2d(params, types={'upper': 'scatter'}) assert ((~axes.isnull()).sum().sum() == 6) fig, axes = ns.plot_2d(params, types={'upper': 'kde', 'diagonal': 'kde'}) assert ((~axes.isnull()).sum().sum() == 9) fig, axes = ns.plot_2d(params, types={'lower': 'kde', 'diagonal': 'kde'}) assert ((~axes.isnull()).sum().sum() == 6) fig, axes = ns.plot_2d(params, types={ 'lower': 'kde', 'diagonal': 'kde', 'upper': 'scatter' }) assert ((~axes.isnull()).sum().sum() == 12) plt.close("all")
def test_masking(): pc = NestedSamples(root="./tests/example_data/pc") mask = pc['x0'] > 0 plot_types = ['kde', 'hist'] if 'fastkde' in sys.modules: plot_types += ['fastkde'] for ptype in plot_types: fig, axes = make_1d_axes(['x0', 'x1', 'x2']) pc[mask].plot_1d(axes=axes, plot_type=ptype) for ptype in plot_types + ['scatter']: fig, axes = make_2d_axes(['x0', 'x1', 'x2'], upper=False) pc[mask].plot_2d(axes=axes, types=dict(lower=ptype, diagonal='hist'))
def test_gui(): plotter = NestedSamples(root='./tests/example_data/pc').gui() # Type buttons plotter.type.buttons.set_active(1) assert (plotter.type() == 'posterior') plotter.type.buttons.set_active(0) assert (plotter.type() == 'live') # Parameter choice buttons plotter.param_choice.buttons.set_active(1) assert (len(plotter.triangle.ax) == 2) plotter.param_choice.buttons.set_active(0) assert (len(plotter.triangle.ax) == 1) plotter.param_choice.buttons.set_active(0) plotter.param_choice.buttons.set_active(2) plotter.param_choice.buttons.set_active(3) assert (len(plotter.triangle.ax) == 4) # Sliders plotter.evolution.slider.set_val(100) assert (plotter.evolution() == 100) plotter.type.buttons.set_active(1) plotter.temperature.slider.set_val(0) assert (plotter.temperature() == 1) plotter.temperature.slider.set_val(1) assert (plotter.temperature() == 10) plotter.temperature.slider.set_val(2) assert (plotter.temperature() == 100) plotter.type.buttons.set_active(0) if version.parse(mpl_version) >= version.parse('3.4.0'): # Reload button plotter.reload.button.observers[0]() # Reset button plotter.reset.button.observers[0]() else: # Reload button plotter.reload.button.observers[0](None) # Reset button plotter.reset.button.observers[0](None)
def test_read_polychord(): numpy.random.seed(3) ns = NestedSamples(root='./tests/example_data/pc') ns.plot_2d(['x0', 'x1', 'x2', 'x3']) ns.plot_1d(['x0', 'x1', 'x2', 'x3']) plt.close("all") os.rename('./tests/example_data/pc_phys_live-birth.txt', './tests/example_data/pc_phys_live-birth.txt_') ns_nolive = NestedSamples(root='./tests/example_data/pc') os.rename('./tests/example_data/pc_phys_live-birth.txt_', './tests/example_data/pc_phys_live-birth.txt') cols = ['x0', 'x1', 'x2', 'x3', 'x4', 'logL', 'logL_birth'] assert_array_equal(ns_nolive[cols], ns[cols][:ns_nolive.shape[0]])
def test_logzero_mask_likelihood_level(): np.random.seed(3) ns0 = NestedSamples(root='./tests/example_data/pc') NS0 = ns0.ns_output(nsamples=2000) mask = ((ns0.x0 > -0.3) & (ns0.x2 > 0.2) & (ns0.x4 < 3.5)).to_numpy() V_posterior = ns0[mask].weights.sum() / ns0.weights.sum() logZ_V = NS0.logZ.mean() + np.log(V_posterior) ns1 = NestedSamples(root='./tests/example_data/pc') ns1.logL = np.where(mask, ns1.logL, -1e30) ns1 = merge_nested_samples((ns1[ns1.logL > ns1.logL_birth],)) NS1 = ns1.ns_output(nsamples=2000) assert abs(NS1.logZ.mean() - logZ_V) < 1.5 * NS1.logZ.std()