|
| 1 | +import pytest |
| 2 | +import numpy as np |
| 3 | +import pypolychord |
| 4 | +from pypolychord.settings import PolyChordSettings |
| 5 | +from pypolychord.priors import UniformPrior |
| 6 | + |
| 7 | + |
| 8 | +def gaussian_likelihood(theta): |
| 9 | + """ Simple Gaussian Likelihood""" |
| 10 | + |
| 11 | + sigma = 0.1 |
| 12 | + |
| 13 | + nDims = len(theta) |
| 14 | + r2 = sum(theta**2) |
| 15 | + logL = -np.log(2*np.pi*sigma*sigma)*nDims/2.0 |
| 16 | + logL += -r2/2/sigma/sigma |
| 17 | + |
| 18 | + return logL, [r2] |
| 19 | + |
| 20 | + |
| 21 | +default_settings = PolyChordSettings(4, 1) |
| 22 | +default_settings.file_root = 'settings' |
| 23 | +default_settings.nlive = 200 |
| 24 | +default_settings.read_resume = False |
| 25 | +default_settings.feedback = 0 |
| 26 | + |
| 27 | +cube_samples_settings = PolyChordSettings(4, 1) |
| 28 | +cube_samples_settings.file_root = 'cube_samples' |
| 29 | +cube_samples_settings.nlive = 200 |
| 30 | +cube_samples_settings.read_resume = False |
| 31 | +cube_samples_settings.cube_samples = np.array([[0.1, 0.2, 0.3, 0.4], |
| 32 | + [0.5, 0.6, 0.7, 0.8]]) |
| 33 | +cube_samples_settings.feedback = 0 |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize("settings, likelihood, nDims, nDerived", |
| 37 | + [(default_settings, gaussian_likelihood, 4, 1), |
| 38 | + (cube_samples_settings, gaussian_likelihood, 4, 1)]) |
| 39 | +def test_run(settings, likelihood, nDims, nDerived): |
| 40 | + # Define a box uniform prior from -1 to 1 |
| 41 | + def prior(hypercube): |
| 42 | + """ Uniform prior from [-1,1]^D. """ |
| 43 | + return UniformPrior(-1, 1)(hypercube) |
| 44 | + |
| 45 | + # Optional dumper function giving run-time read access to |
| 46 | + # the live points, dead points, weights and evidences |
| 47 | + |
| 48 | + def dumper(live, dead, logweights, logZ, logZerr): |
| 49 | + print("Last dead point:", dead[-1]) |
| 50 | + |
| 51 | + # Run PolyChord |
| 52 | + |
| 53 | + print("Running PolyChord") |
| 54 | + output = pypolychord.run_polychord(likelihood, nDims, nDerived, settings, prior, dumper) |
| 55 | + |
| 56 | + # Create a paramnames file |
| 57 | + |
| 58 | + paramnames = [('p%i' % i, r'\theta_%i' % i) for i in range(nDims)] |
| 59 | + paramnames += [('r*', 'r')] |
| 60 | + output.make_paramnames_files(paramnames) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize("settings, likelihood, nDims, nDerived", |
| 64 | + [(default_settings, gaussian_likelihood, 4, 1), |
| 65 | + (cube_samples_settings, gaussian_likelihood, 4, 1)]) |
| 66 | +@pytest.mark.mpi |
| 67 | +def test_run_mpi(settings, likelihood, nDims, nDerived): |
| 68 | + from mpi4py import MPI |
| 69 | + |
| 70 | + settings.file_root += '_mpi' |
| 71 | + |
| 72 | + # Define a box uniform prior from -1 to 1 |
| 73 | + def prior(hypercube): |
| 74 | + """ Uniform prior from [-1,1]^D. """ |
| 75 | + return UniformPrior(-1, 1)(hypercube) |
| 76 | + |
| 77 | + # Optional dumper function giving run-time read access to |
| 78 | + # the live points, dead points, weights and evidences |
| 79 | + |
| 80 | + def dumper(live, dead, logweights, logZ, logZerr): |
| 81 | + print("Last dead point:", dead[-1]) |
| 82 | + |
| 83 | + # Run PolyChord |
| 84 | + |
| 85 | + print("Running PolyChord") |
| 86 | + output = pypolychord.run_polychord(likelihood, nDims, nDerived, settings, prior, dumper) |
| 87 | + |
| 88 | + # Create a paramnames file |
| 89 | + |
| 90 | + paramnames = [('p%i' % i, r'\theta_%i' % i) for i in range(nDims)] |
| 91 | + paramnames += [('r*', 'r')] |
| 92 | + output.make_paramnames_files(paramnames) |
0 commit comments