Commit 89151b51 authored by Moritz Huebner's avatar Moritz Huebner

Restructure tests part 2

parent 5428c2ee
[run]
omit =
test/core/example_test.py
test/gw/example_test.py
test/gw/noise_realisation_test.py
test/gw/other_test.py
test/integration/example_test.py
test/integration/noise_realisation_test.py
test/integration/other_test.py
......@@ -91,8 +91,8 @@ python-3.7-samplers:
script:
- python -m pip install .
- pytest test/core/sampler/sampler_run_test.py --durations 10
- pytest test/gw/sample_from_the_prior_test.py
- pytest test/integration/sampler_run_test.py --durations 10
- pytest test/integration/sample_from_the_prior_test.py
# test samplers on python 3.6
python-3.6-samplers:
......@@ -101,7 +101,7 @@ python-3.6-samplers:
script:
- python -m pip install .
- pytest test/core/sampler/sampler_run_test.py
- pytest test/integration/sampler_run_test.py
# Tests run at a fixed schedule rather than on push
scheduled-python-3.7:
......@@ -113,9 +113,8 @@ scheduled-python-3.7:
- python -m pip install .
# Run tests which are only done on schedule
- pytest test/core/example_test.py
- pytest test/gw/example_test.py
- pytest test/gw/sample_from_the_prior_test.py
- pytest test/integration/example_test.py
- pytest test/integration/sample_from_the_prior_test.py
plotting:
stage: test
......
......@@ -444,9 +444,6 @@ class Constraint(Prior):
def prob(self, val):
return (val > self.minimum) & (val < self.maximum)
def ln_prob(self, val):
return np.log((val > self.minimum) & (val < self.maximum))
class PriorException(Exception):
""" General base class for all prior exceptions """
......@@ -279,6 +279,16 @@ def optimal_snr_squared(signal, power_spectral_density, duration):
return noise_weighted_inner_product(signal, signal, power_spectral_density, duration)
def overlap(signal_a, signal_b, power_spectral_density=None, delta_frequency=None,
lower_cut_off=None, upper_cut_off=None, norm_a=None, norm_b=None):
low_index = int(lower_cut_off / delta_frequency)
up_index = int(upper_cut_off / delta_frequency)
integrand = np.conj(signal_a) * signal_b
integrand = integrand[low_index:up_index] / power_spectral_density[low_index:up_index]
integral = (4 * delta_frequency * integrand) / norm_a / norm_b
return sum(integral).real
__cached_euler_matrix = None
__cached_delta_x = None
......
......@@ -5,12 +5,11 @@ ignore = E129 W503 W504 W605 E203 E402
[tool:pytest]
addopts =
--ignore test/gw/other_test.py
--ignore test/gw/example_test.py
--ignore test/core/example_test.py
--ignore test/gw/sample_from_the_prior_test.py
--ignore test/integration/other_test.py
--ignore test/integration/example_test.py
--ignore test/integration/sample_from_the_prior_test.py
--ignore test/gw/plot_test.py
--ignore test/core/sampler/sampler_run_test.py
--ignore test/integration/sampler_run_test.py
[metadata]
license_file = LICENSE.md
from __future__ import absolute_import
import matplotlib
matplotlib.use("Agg")
import unittest
import os
import shutil
import logging
# Required to run the tests
from past.builtins import execfile
import bilby.core.utils
# Imported to ensure the examples run
import numpy as np # noqa: F401
import inspect # noqa: F401
bilby.core.utils.command_line_args.bilby_test_mode = True
class Test(unittest.TestCase):
outdir = "outdir"
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = os.path.abspath(os.path.join(dir_path, os.path.pardir))
@classmethod
def setUpClass(self):
if os.path.isdir(self.outdir):
try:
shutil.rmtree(self.outdir)
except OSError:
logging.warning("{} not removed prior to tests".format(self.outdir))
@classmethod
def tearDownClass(self):
if os.path.isdir(self.outdir):
try:
shutil.rmtree(self.outdir)
except OSError:
logging.warning("{} not removed prior to tests".format(self.outdir))
def test_examples(self):
""" Loop over examples to check they run """
examples = [
"examples/core_examples/linear_regression.py",
"examples/core_examples/linear_regression_unknown_noise.py",
]
for filename in examples:
print("Testing {}".format(filename))
execfile(filename)
if __name__ == "__main__":
unittest.main()
from __future__ import absolute_import, division
import unittest
import numpy as np
import shutil
......@@ -107,65 +105,42 @@ class TestGrid(unittest.TestCase):
self.grid.marginalize_posterior(parameters="jkgsd")
def test_parameter_names(self):
assert list(self.priors.keys()) == self.grid.parameter_names
assert self.grid.n_dims == 2
self.assertListEqual(list(self.priors.keys()), self.grid.parameter_names)
self.assertEqual(2, self.grid.n_dims)
def test_no_marginalization(self):
# test arrays are the same if no parameters are given to marginalize
# over
assert np.array_equal(
self.assertTrue(np.array_equal(
self.grid.ln_likelihood,
self.grid.marginalize_ln_likelihood(
not_parameters=self.grid.parameter_names
),
)
self.grid.marginalize_ln_likelihood(not_parameters=self.grid.parameter_names)))
def test_marginalization_shapes(self):
assert len(self.grid.marginalize_ln_likelihood().shape) == 0
marg1 = self.grid.marginalize_ln_likelihood(
parameters=self.grid.parameter_names[0]
)
assert marg1.shape == (self.grid_size,)
marg2 = self.grid.marginalize_ln_likelihood(
parameters=self.grid.parameter_names[1]
)
assert marg2.shape == (self.grid_size,)
assert self.grid.ln_likelihood.shape == (self.grid_size, self.grid_size)
assert self.grid.ln_posterior.shape == (self.grid_size, self.grid_size)
self.assertEqual(0, len(self.grid.marginalize_ln_likelihood().shape))
marg1 = self.grid.marginalize_ln_likelihood(parameters=self.grid.parameter_names[0])
marg2 = self.grid.marginalize_ln_likelihood(parameters=self.grid.parameter_names[1])
self.assertTupleEqual((self.grid_size,), marg1.shape)
self.assertTupleEqual((self.grid_size,), marg2.shape)
self.assertTupleEqual((self.grid_size, self.grid_size), self.grid.ln_likelihood.shape)
self.assertTupleEqual((self.grid_size, self.grid_size), self.grid.ln_posterior.shape)
def test_marginalization_opposite(self):
assert np.array_equal(
self.grid.marginalize_ln_likelihood(
parameters=self.grid.parameter_names[0]
),
self.grid.marginalize_ln_likelihood(
not_parameters=self.grid.parameter_names[1]
),
)
assert np.array_equal(
self.grid.marginalize_ln_likelihood(
parameters=self.grid.parameter_names[1]
),
self.grid.marginalize_ln_likelihood(
not_parameters=self.grid.parameter_names[0]
),
)
self.assertTrue(np.array_equal(
self.grid.marginalize_ln_likelihood(parameters=self.grid.parameter_names[0]),
self.grid.marginalize_ln_likelihood(not_parameters=self.grid.parameter_names[1])))
self.assertTrue(np.array_equal(
self.grid.marginalize_ln_likelihood(parameters=self.grid.parameter_names[1]),
self.grid.marginalize_ln_likelihood(not_parameters=self.grid.parameter_names[0])))
def test_max_marginalized_likelihood(self):
# marginalised likelihoods should have max values of 1 (as they are not
# properly normalised)
assert (
self.grid.marginalize_likelihood(self.grid.parameter_names[0]).max() == 1.0
)
assert (
self.grid.marginalize_likelihood(self.grid.parameter_names[1]).max() == 1.0
)
self.assertEqual(1.0, self.grid.marginalize_likelihood(self.grid.parameter_names[0]).max())
self.assertEqual(1.0, self.grid.marginalize_likelihood(self.grid.parameter_names[1]).max())
def test_ln_evidence(self):
assert np.isclose(self.grid.ln_evidence, self.expected_ln_evidence)
self.assertAlmostEqual(self.expected_ln_evidence, self.grid.ln_evidence, places=5)
def test_fail_grid_size(self):
with self.assertRaises(TypeError):
......@@ -179,156 +154,100 @@ class TestGrid(unittest.TestCase):
)
def test_mesh_grid(self):
assert self.grid.mesh_grid[0].shape == (self.grid_size, self.grid_size)
assert (
self.grid.mesh_grid[0][0, 0]
== self.priors[self.grid.parameter_names[0]].minimum
)
assert (
self.grid.mesh_grid[0][-1, -1]
== self.priors[self.grid.parameter_names[1]].maximum
)
def test_different_grids(self):
npoints = [10, 20]
self.assertTupleEqual((self.grid_size, self.grid_size), self.grid.mesh_grid[0].shape)
self.assertEqual(self.priors[self.grid.parameter_names[0]].minimum, self.grid.mesh_grid[0][0, 0])
self.assertEqual(self.priors[self.grid.parameter_names[1]].maximum, self.grid.mesh_grid[0][-1, -1])
def test_grid_integer_points(self):
n_points = [10, 20]
grid = bilby.core.grid.Grid(
label="label",
outdir="outdir",
priors=self.priors,
grid_size=npoints,
likelihood=self.likelihood,
grid_size=n_points,
likelihood=self.likelihood
)
assert grid.mesh_grid[0].shape == tuple(npoints)
assert (
grid.mesh_grid[0][0, 0] == self.priors[self.grid.parameter_names[0]].minimum
)
assert (
grid.mesh_grid[0][-1, -1]
== self.priors[self.grid.parameter_names[1]].maximum
)
del grid
npoints = {"x0": 15, "x1": 18}
self.assertTupleEqual(tuple(n_points), grid.mesh_grid[0].shape)
self.assertEqual(grid.mesh_grid[0][0, 0], self.priors[self.grid.parameter_names[0]].minimum)
self.assertEqual(grid.mesh_grid[0][-1, -1], self.priors[self.grid.parameter_names[1]].maximum)
def test_grid_dict_points(self):
n_points = {"x0": 15, "x1": 18}
grid = bilby.core.grid.Grid(
label="label",
outdir="outdir",
priors=self.priors,
grid_size=npoints,
likelihood=self.likelihood,
grid_size=n_points,
likelihood=self.likelihood
)
self.assertTupleEqual((n_points["x0"], n_points["x1"]), grid.mesh_grid[0].shape)
self.assertEqual(grid.mesh_grid[0][0, 0], self.priors[self.grid.parameter_names[0]].minimum)
self.assertEqual(grid.mesh_grid[0][-1, -1], self.priors[self.grid.parameter_names[1]].maximum)
assert grid.mesh_grid[0].shape == (npoints["x0"], npoints["x1"])
assert (
grid.mesh_grid[0][0, 0] == self.priors[self.grid.parameter_names[0]].minimum
)
assert (
grid.mesh_grid[0][-1, -1]
== self.priors[self.grid.parameter_names[1]].maximum
)
del grid
def test_grid_from_array(self):
x0s = np.linspace(self.priors["x0"].minimum, self.priors["x0"].maximum, 13)
x1s = np.linspace(self.priors["x0"].minimum, self.priors["x0"].maximum, 14)
npoints = {"x0": x0s, "x1": x1s}
n_points = {"x0": x0s, "x1": x1s}
grid = bilby.core.grid.Grid(
label="label",
outdir="outdir",
priors=self.priors,
grid_size=npoints,
grid_size=n_points,
likelihood=self.likelihood,
)
assert grid.mesh_grid[0].shape == (len(x0s), len(x1s))
assert (
grid.mesh_grid[0][0, 0] == self.priors[self.grid.parameter_names[0]].minimum
)
assert (
grid.mesh_grid[0][-1, -1]
== self.priors[self.grid.parameter_names[1]].maximum
)
assert np.array_equal(grid.sample_points["x0"], x0s)
assert np.array_equal(grid.sample_points["x1"], x1s)
self.assertTupleEqual((len(x0s), len(x1s)), grid.mesh_grid[0].shape)
self.assertEqual(grid.mesh_grid[0][0, 0], self.priors[self.grid.parameter_names[0]].minimum)
self.assertEqual(grid.mesh_grid[0][-1, -1], self.priors[self.grid.parameter_names[1]].maximum)
def test_save_and_load(self):
filename = os.path.join("outdir", "test_output.json")
self.assertTrue(np.array_equal(grid.sample_points["x0"], x0s))
self.assertTrue(np.array_equal(grid.sample_points["x1"], x1s))
def test_save_and_load_from_filename(self):
filename = os.path.join("outdir", "test_output.json")
self.grid.save_to_file(filename=filename)
# load file
newgrid = bilby.core.grid.Grid.read(filename=filename)
assert newgrid.parameter_names == self.grid.parameter_names
assert newgrid.n_dims == self.grid.n_dims
assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0])
for par in newgrid.parameter_names:
assert np.array_equal(
newgrid.sample_points[par], self.grid.sample_points[par]
)
assert newgrid.ln_evidence == self.grid.ln_evidence
assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood)
assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior)
del newgrid
new_grid = bilby.core.grid.Grid.read(filename=filename)
self.assertListEqual(new_grid.parameter_names, self.grid.parameter_names)
self.assertEqual(new_grid.n_dims, self.grid.n_dims)
self.assertTrue(np.array_equal(new_grid.mesh_grid[0], self.grid.mesh_grid[0]))
for par in new_grid.parameter_names:
self.assertTrue(np.array_equal(new_grid.sample_points[par], self.grid.sample_points[par]))
self.assertEqual(new_grid.ln_evidence, self.grid.ln_evidence)
self.assertTrue(np.array_equal(new_grid.ln_likelihood, self.grid.ln_likelihood))
self.assertTrue(np.array_equal(new_grid.ln_posterior, self.grid.ln_posterior))
def test_save_and_load_from_outdir_label(self):
self.grid.save_to_file(overwrite=True, outdir="outdir")
# load file
newgrid = bilby.core.grid.Grid.read(outdir="outdir", label="label")
assert newgrid.parameter_names == self.grid.parameter_names
assert newgrid.n_dims == self.grid.n_dims
assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0])
for par in newgrid.parameter_names:
assert np.array_equal(
newgrid.sample_points[par], self.grid.sample_points[par]
new_grid = bilby.core.grid.Grid.read(outdir="outdir", label="label")
self.assertListEqual(self.grid.parameter_names, new_grid.parameter_names)
self.assertEqual(self.grid.n_dims, new_grid.n_dims)
self.assertTrue(np.array_equal(new_grid.mesh_grid[0], self.grid.mesh_grid[0]))
for par in new_grid.parameter_names:
self.assertTrue(np.array_equal(
new_grid.sample_points[par], self.grid.sample_points[par])
)
assert newgrid.ln_evidence == self.grid.ln_evidence
assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood)
assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior)
del newgrid
self.assertEqual(self.grid.ln_evidence, new_grid.ln_evidence)
self.assertTrue(np.array_equal(self.grid.ln_likelihood, new_grid.ln_likelihood))
self.assertTrue(np.array_equal(self.grid.ln_posterior, new_grid.ln_posterior))
del new_grid
def test_save_and_load_gzip(self):
filename = os.path.join("outdir", "test_output.json.gz")
self.grid.save_to_file(filename=filename)
new_grid = bilby.core.grid.Grid.read(filename=filename)
# load file
newgrid = bilby.core.grid.Grid.read(filename=filename)
assert newgrid.parameter_names == self.grid.parameter_names
assert newgrid.n_dims == self.grid.n_dims
assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0])
for par in newgrid.parameter_names:
assert np.array_equal(
newgrid.sample_points[par], self.grid.sample_points[par]
)
assert newgrid.ln_evidence == self.grid.ln_evidence
assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood)
assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior)
del newgrid
self.assertListEqual(self.grid.parameter_names, new_grid.parameter_names)
self.assertEqual(self.grid.n_dims, new_grid.n_dims)
self.assertTrue(np.array_equal(self.grid.mesh_grid[0], new_grid.mesh_grid[0]))
for par in new_grid.parameter_names:
self.assertTrue(np.array_equal(self.grid.sample_points[par], new_grid.sample_points[par]))
self.assertEqual(self.grid.ln_evidence, new_grid.ln_evidence)
self.assertTrue(np.array_equal(self.grid.ln_likelihood, new_grid.ln_likelihood))
self.assertTrue(np.array_equal(self.grid.ln_posterior, new_grid.ln_posterior))
self.grid.save_to_file(overwrite=True, outdir="outdir", gzip=True)
# load file
newgrid = bilby.core.grid.Grid.read(outdir="outdir", label="label", gzip=True)
assert newgrid.parameter_names == self.grid.parameter_names
assert newgrid.n_dims == self.grid.n_dims
assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0])
for par in newgrid.parameter_names:
assert np.array_equal(
newgrid.sample_points[par], self.grid.sample_points[par]
)
assert newgrid.ln_evidence == self.grid.ln_evidence
assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood)
assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior)
del newgrid
_ = bilby.core.grid.Grid.read(outdir="outdir", label="label", gzip=True)
from __future__ import absolute_import
# import bilby
import unittest
from mock import MagicMock
import mock
......
......@@ -77,29 +77,21 @@ class TestPriorInstantiationWithoutOptionalPriors(unittest.TestCase):
self.assertIsNone(self.prior.boundary)
class TestPriorName(unittest.TestCase):
class TestPrior(unittest.TestCase):
def setUp(self):
self.test_name = "test_name"
self.prior = bilby.core.prior.Prior(self.test_name)
self.boundary = None
self.prior = bilby.core.prior.Prior(name=self.test_name, boundary=self.boundary)
def tearDown(self):
del self.prior
del self.test_name
del self.boundary
del self.prior
def test_name_assignment(self):
self.prior.name = "other_name"
self.assertEqual(self.prior.name, "other_name")
class TestPriorLatexLabel(unittest.TestCase):
def setUp(self):
self.test_name = "test_name"
self.prior = bilby.core.prior.Prior(self.test_name)
def tearDown(self):
del self.test_name
del self.prior
def test_label_assignment(self):
test_label = "test_label"
self.prior.latex_label = "test_label"
......@@ -113,15 +105,7 @@ class TestPriorLatexLabel(unittest.TestCase):
def test_default_label_assignment_default(self):
self.assertTrue(self.prior.latex_label, self.prior.name)
class TestPriorIsFixed(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
del self.prior
def test_is_fixed_parent_class(self):
def test_is_fixed_base_class(self):
self.prior = bilby.core.prior.Prior()
self.assertFalse(self.prior.is_fixed)
......@@ -129,25 +113,60 @@ class TestPriorIsFixed(unittest.TestCase):
self.prior = bilby.core.prior.DeltaFunction(peak=0)
self.assertTrue(self.prior.is_fixed)
def test_is_fixed_uniform_class(self):
self.prior = bilby.core.prior.Uniform(minimum=0, maximum=10)
self.assertFalse(self.prior.is_fixed)
def test_set_boundary_valid(self):
self.prior.boundary = "periodic"
self.assertEqual(self.prior.boundary, "periodic")
def test_set_boundary_invalid(self):
with self.assertRaises(ValueError):
self.prior.boundary = "else"
class TestPriorBoundary(unittest.TestCase):
class TestConstraint(unittest.TestCase):
def setUp(self):
self.prior = bilby.core.prior.Prior(boundary=None)
self.prior = bilby.core.prior.Constraint(minimum=0, maximum=1)
def tearDown(self):
del self.prior
def test_set_boundary_valid(self):
self.prior.boundary = "periodic"
self.assertEqual(self.prior.boundary, "periodic")
def test_is_fixed_constraint(self):
self.assertTrue(self.prior.is_fixed)
def test_set_boundary_invalid(self):
with self.assertRaises(ValueError):
self.prior.boundary = "else"
def test_prob_outside(self):
self.assertEqual(0, self.prior.prob(-0.5))
def test_prob_inside(self):
self.assertEqual(1, self.prior.prob(0.5))
class TestConstraintPriorNormalisation(unittest.TestCase):
def setUp(self):
self.priors = dict(
a=bilby.core.prior.Uniform(name="a", minimum=5, maximum=10),
b=bilby.core.prior.Uniform(name="b", minimum=5, maximum=10),
c=bilby.core.prior.Constraint(name="c", minimum=0, maximum=1),
)
def conversion_func(parameters):
return dict(a=parameters["a"], b=parameters["b"], c=parameters["a"] / parameters["b"])
self.priors = bilby.core.prior.PriorDict(self.priors, conversion_function=conversion_func)
def test_prob_integrate_to_one(self):
keys = ["a", "b", "c"]
n_samples = 1000000
samples = self.priors.sample_subset(keys=keys, size=n_samples)
prob = self.priors.prob(samples, axis=0)
dm1 = self.priors["a"].maximum - self.priors["a"].minimum
dm2 = self.priors["b"].maximum - self.priors["b"].minimum
prior_volume = (dm1 * dm2)
n_accepted = np.sum(prob)
integral = prior_volume * n_accepted / n_samples
# binomial random distribution
p, q = 0.5, 0.5
sigma = np.sqrt(0.5 * n_samples * p * q)