Skip to content
Snippets Groups Projects
Commit bc94d3df authored by Gregory Ashton's avatar Gregory Ashton
Browse files

Merge branch 'add-pre-commits-slowly' into 'master'

Add pre commits: flake8 and merge conflicts

See merge request !763
parents 16d28028 71d76f11
No related branches found
No related tags found
1 merge request!763Add pre commits: flake8 and merge conflicts
Pipeline #122171 passed
Showing
with 2314 additions and 1215 deletions
......@@ -157,3 +157,18 @@ deploy_release:
- twine upload dist/*
only:
- tags
precommits-py3.7:
stage: test
image: bilbydev/v2-dockerfile-test-suite-python37
script:
- source activate python37
- mkdir -p .pip37
- pip install --upgrade pip
- pip --cache-dir=.pip37 install --upgrade bilby
- pip --cache-dir=.pip37 install .
- pip --cache-dir=.pip37 install pre-commit
# Run precommits (flake8, spellcheck, isort, no merge conflicts, etc)
- pre-commit run --all-files --verbose --show-diff-on-failure
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-merge-conflict # prevent committing files with merge conflicts
- id: flake8 # checks for flake8 errors
#- repo: https://github.com/codespell-project/codespell
# rev: v1.16.0
# hooks:
# - id: codespell # Spellchecker
# args: [-L, nd, --skip, "*.ipynb,*.html", --ignore-words=.dictionary.txt]
# exclude: ^examples/tutorials/
#- repo: https://github.com/asottile/seed-isort-config
# rev: v1.3.0
# hooks:
# - id: seed-isort-config
# args: [--application-directories, 'bilby/']
#- repo: https://github.com/pre-commit/mirrors-isort
# rev: v4.3.21
# hooks:
# - id: isort # sort imports alphabetically and separates import into sections
# args: [-w=88, -m=3, -tc, -sp=setup.cfg ]
......@@ -49,6 +49,33 @@ def my_new_function(x, y, print=False):
5. Don't repeat yourself. If code is repeated in multiple places, wrap it up into a function.
6. Add tests. The C.I. is there to do the work of "checking" the code, both now and into the future. Use it.
## Automated code checking
In order to automate checking of the code quality, we use
[pre-commit](https://pre-commit.com/). For more details, see the documentation,
here we will give a quick-start guide:
1. Install and configure:
```console
$ pip install pre-commit # install the pre-commit package
$ cd bilby
$ pre-commit install
```
2. Now, when you run `$ git commit`, there will be a pre-commit check.
This is going to search for issues in your code: spelling, formatting, etc.
In some cases, it will automatically fix the code, in other cases, it will
print a warning. If it automatically fixed the code, you'll need to add the
changes to the index (`$ git add FILE.py`) and run `$ git commit` again. If
it didn't automatically fix the code, but still failed, it will have printed
a message as to why the commit failed. Read the message, fix the issues,
then recommit.
3. The pre-commit checks are done to avoid pushing and then failing. But, you
can skip them by running `$ git commit --no-verify`, but note that the C.I.
still does the check so you won't be able to merge until the issues are
resolved.
If you experience any issues with pre-commit, please ask for support on the
usual help channels.
## Code relevance
The bilby code base is intended to be highly modular and flexible. We encourage
......
......@@ -173,5 +173,3 @@ texinfo_documents = [
]
numpydoc_show_class_members = False
[flake8]
exclude = .git,docs,build,dist,test,*__init__.py
max-line-length = 120
ignore = E129 W503 W504 W605 E203
ignore = E129 W503 W504 W605 E203 E402
[tool:pytest]
addopts =
......
......@@ -4,7 +4,6 @@ import numpy as np
class TestBaseClass(unittest.TestCase):
def setUp(self):
self.model = calibration.Recalibrate()
......@@ -12,7 +11,7 @@ class TestBaseClass(unittest.TestCase):
del self.model
def test_repr(self):
expected = 'Recalibrate(prefix={})'.format('\'recalib_\'')
expected = "Recalibrate(prefix={})".format("'recalib_'")
actual = repr(self.model)
self.assertEqual(expected, actual)
......@@ -23,18 +22,22 @@ class TestBaseClass(unittest.TestCase):
class TestCubicSpline(unittest.TestCase):
def setUp(self):
self.prefix = 'recalib_'
self.prefix = "recalib_"
self.minimum_frequency = 20
self.maximum_frequency = 1024
self.n_points = 5
self.model = calibration.CubicSpline(
prefix=self.prefix, minimum_frequency=self.minimum_frequency,
maximum_frequency=self.maximum_frequency, n_points=self.n_points)
self.parameters = {'recalib_{}_{}'.format(param, ii): 0.0
for ii in range(5)
for param in ['amplitude', 'phase']}
prefix=self.prefix,
minimum_frequency=self.minimum_frequency,
maximum_frequency=self.maximum_frequency,
n_points=self.n_points,
)
self.parameters = {
"recalib_{}_{}".format(param, ii): 0.0
for ii in range(5)
for param in ["amplitude", "phase"]
}
def tearDown(self):
del self.prefix
......@@ -46,27 +49,28 @@ class TestCubicSpline(unittest.TestCase):
def test_calibration_factor(self):
frequency_array = np.linspace(20, 1024, 1000)
cal_factor = self.model.get_calibration_factor(frequency_array,
**self.parameters)
cal_factor = self.model.get_calibration_factor(
frequency_array, **self.parameters
)
assert np.alltrue(cal_factor.real == np.ones_like(frequency_array))
def test_repr(self):
expected = 'CubicSpline(prefix=\'{}\', minimum_frequency={}, maximum_frequency={}, n_points={})'\
.format(self.prefix, self.minimum_frequency, self.maximum_frequency, self.n_points)
expected = "CubicSpline(prefix='{}', minimum_frequency={}, maximum_frequency={}, n_points={})".format(
self.prefix, self.minimum_frequency, self.maximum_frequency, self.n_points
)
actual = repr(self.model)
self.assertEqual(expected, actual)
class TestCubicSplineRequiresFourNodes(unittest.TestCase):
def test_cannot_instantiate_with_too_few_nodes(self):
for ii in range(6):
if ii < 4:
with self.assertRaises(ValueError):
calibration.CubicSpline('test', 1, 10, ii)
calibration.CubicSpline("test", 1, 10, ii)
else:
calibration.CubicSpline('test', 1, 10, ii)
calibration.CubicSpline("test", 1, 10, ii)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import bilby
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
import bilby # noqa
This diff is collapsed.
......@@ -6,18 +6,17 @@ from bilby.gw import cosmology
class TestSetCosmology(unittest.TestCase):
def setUp(self):
pass
def test_setting_cosmology_with_string(self):
cosmology.set_cosmology('WMAP9')
self.assertEqual(cosmology.COSMOLOGY[1], 'WMAP9')
cosmology.set_cosmology('Planck15')
cosmology.set_cosmology("WMAP9")
self.assertEqual(cosmology.COSMOLOGY[1], "WMAP9")
cosmology.set_cosmology("Planck15")
def test_setting_cosmology_with_astropy_object(self):
cosmology.set_cosmology(WMAP9)
self.assertEqual(cosmology.COSMOLOGY[1], 'WMAP9')
self.assertEqual(cosmology.COSMOLOGY[1], "WMAP9")
cosmology.set_cosmology(Planck15)
def test_setting_cosmology_with_default(self):
......@@ -27,30 +26,29 @@ class TestSetCosmology(unittest.TestCase):
def test_setting_cosmology_with_flat_lambda_cdm_dict(self):
cosmo_dict = dict(H0=67.7, Om0=0.3)
cosmology.set_cosmology(cosmo_dict)
self.assertEqual(cosmology.COSMOLOGY[1][:13], 'FlatLambdaCDM')
self.assertEqual(cosmology.COSMOLOGY[1][:13], "FlatLambdaCDM")
def test_setting_cosmology_with_lambda_cdm_dict(self):
cosmo_dict = dict(H0=67.7, Om0=0.3, Ode0=0.7)
cosmology.set_cosmology(cosmo_dict)
self.assertEqual(cosmology.COSMOLOGY[1][:9], 'LambdaCDM')
self.assertEqual(cosmology.COSMOLOGY[1][:9], "LambdaCDM")
def test_setting_cosmology_with_w_cdm_dict(self):
cosmo_dict = dict(H0=67.7, Om0=0.3, Ode0=0.7, w0=-1.0)
cosmology.set_cosmology(cosmo_dict)
self.assertEqual(cosmology.COSMOLOGY[1][:4], 'wCDM')
self.assertEqual(cosmology.COSMOLOGY[1][:4], "wCDM")
class TestGetCosmology(unittest.TestCase):
def setUp(self):
pass
def test_getting_cosmology_with_string(self):
self.assertEqual(cosmology.get_cosmology('WMAP9').name, 'WMAP9')
self.assertEqual(cosmology.get_cosmology("WMAP9").name, "WMAP9")
def test_getting_cosmology_with_default(self):
self.assertEqual(cosmology.get_cosmology(), cosmology.COSMOLOGY[0])
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
This diff is collapsed.
from __future__ import absolute_import
import matplotlib
matplotlib.use('Agg')
matplotlib.use("Agg")
import unittest
import os
......@@ -12,14 +13,14 @@ from past.builtins import execfile
import bilby.core.utils
# Imported to ensure the examples run
import numpy as np
import inspect
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'
outdir = "outdir"
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = os.path.abspath(os.path.join(dir_path, os.path.pardir))
......@@ -29,8 +30,7 @@ class Test(unittest.TestCase):
try:
shutil.rmtree(self.outdir)
except OSError:
logging.warning(
"{} not removed prior to tests".format(self.outdir))
logging.warning("{} not removed prior to tests".format(self.outdir))
@classmethod
def tearDownClass(self):
......@@ -38,20 +38,18 @@ class Test(unittest.TestCase):
try:
shutil.rmtree(self.outdir)
except OSError:
logging.warning(
"{} not removed prior to tests".format(self.outdir))
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',
]
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__':
if __name__ == "__main__":
unittest.main()
......@@ -2,10 +2,8 @@ from __future__ import absolute_import, division
import unittest
import numpy as np
import pandas as pd
import shutil
import os
import json
from scipy.stats import multivariate_normal
import bilby
......@@ -30,32 +28,41 @@ class MultiGaussian(bilby.Likelihood):
class TestGrid(unittest.TestCase):
def setUp(self):
np.random.seed(7)
# set 2D multivariate Gaussian (zero mean, unit variance)
self.mus = [0., 0.]
self.cov = [[1., 0.], [0., 1.]]
self.mus = [0.0, 0.0]
self.cov = [[1.0, 0.0], [0.0, 1.0]]
dim = len(self.mus)
self.likelihood = MultiGaussian(self.mus, self.cov)
# set priors out to +/- 5 sigma
self.priors = bilby.core.prior.PriorDict()
self.priors.update(
{"x{0}".format(i): bilby.core.prior.Uniform(-5, 5, "x{0}".format(i)) for i in range(dim)}
{
"x{0}".format(i): bilby.core.prior.Uniform(-5, 5, "x{0}".format(i))
for i in range(dim)
}
)
# expected evidence integral should be (1/V) where V is the prior volume
log_prior_vol = np.sum(np.log([prior.maximum - prior.minimum for key, prior in self.priors.items()]))
log_prior_vol = np.sum(
np.log(
[prior.maximum - prior.minimum for key, prior in self.priors.items()]
)
)
self.expected_ln_evidence = -log_prior_vol
self.grid_size = 100
grid = bilby.core.grid.Grid(
label='label', outdir='outdir', priors=self.priors,
grid_size=self.grid_size, likelihood=self.likelihood,
save=True
label="label",
outdir="outdir",
priors=self.priors,
grid_size=self.grid_size,
likelihood=self.likelihood,
save=True,
)
self.grid = grid
......@@ -71,29 +78,33 @@ class TestGrid(unittest.TestCase):
pass
def test_grid_file_name_default(self):
outdir = 'outdir'
label = 'label'
self.assertEqual(bilby.core.grid.grid_file_name(outdir, label),
'{}/{}_grid.json'.format(outdir, label))
self.assertEqual(bilby.core.grid.grid_file_name(outdir, label, True),
'{}/{}_grid.json.gz'.format(outdir, label))
outdir = "outdir"
label = "label"
self.assertEqual(
bilby.core.grid.grid_file_name(outdir, label),
"{}/{}_grid.json".format(outdir, label),
)
self.assertEqual(
bilby.core.grid.grid_file_name(outdir, label, True),
"{}/{}_grid.json.gz".format(outdir, label),
)
def test_fail_save_and_load(self):
with self.assertRaises(ValueError):
bilby.core.grid.Grid.read()
with self.assertRaises(IOError):
bilby.core.grid.Grid.read(filename='not/a/file.json')
bilby.core.grid.Grid.read(filename="not/a/file.json")
def test_fail_marginalize(self):
with self.assertRaises(TypeError):
self.grid.marginalize_posterior(parameters=2.4)
with self.assertRaises(TypeError):
self.grid.marginalize_posterior(not_parameters=4.7)
with self.assertRaises(ValueError):
self.grid.marginalize_posterior(parameters='jkgsd')
self.grid.marginalize_posterior(parameters="jkgsd")
def test_parameter_names(self):
assert list(self.priors.keys()) == self.grid.parameter_names
......@@ -102,94 +113,150 @@ class TestGrid(unittest.TestCase):
def test_no_marginalization(self):
# test arrays are the same if no parameters are given to marginalize
# over
assert np.array_equal(self.grid.ln_likelihood,
self.grid.marginalize_ln_likelihood(not_parameters=self.grid.parameter_names))
assert np.array_equal(
self.grid.ln_likelihood,
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])
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])
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)
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]))
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]
),
)
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.
assert self.grid.marginalize_likelihood(self.grid.parameter_names[1]).max() == 1.
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
)
def test_ln_evidence(self):
assert np.isclose(self.grid.ln_evidence, self.expected_ln_evidence)
def test_fail_grid_size(self):
with self.assertRaises(TypeError):
grid = bilby.core.grid.Grid(
label='label', outdir='outdir', priors=self.priors,
grid_size=2.3, likelihood=self.likelihood,
save=True
bilby.core.grid.Grid(
label="label",
outdir="outdir",
priors=self.priors,
grid_size=2.3,
likelihood=self.likelihood,
save=True,
)
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
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]
grid = bilby.core.grid.Grid(
label='label', outdir='outdir', priors=self.priors,
grid_size=npoints, likelihood=self.likelihood
label="label",
outdir="outdir",
priors=self.priors,
grid_size=npoints,
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
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}
npoints = {"x0": 15, "x1": 18}
grid = bilby.core.grid.Grid(
label='label', outdir='outdir', priors=self.priors,
grid_size=npoints, likelihood=self.likelihood
label="label",
outdir="outdir",
priors=self.priors,
grid_size=npoints,
likelihood=self.likelihood,
)
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
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
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}
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}
grid = bilby.core.grid.Grid(
label='label', outdir='outdir', priors=self.priors,
grid_size=npoints, likelihood=self.likelihood
label="label",
outdir="outdir",
priors=self.priors,
grid_size=npoints,
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)
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)
def test_save_and_load(self):
filename = os.path.join('outdir', 'test_output.json')
filename = os.path.join("outdir", "test_output.json")
self.grid.save_to_file(filename=filename)
......@@ -200,24 +267,27 @@ class TestGrid(unittest.TestCase):
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 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.grid.save_to_file(overwrite=True, outdir='outdir')
self.grid.save_to_file(overwrite=True, outdir="outdir")
# load file
newgrid = bilby.core.grid.Grid.read(outdir='outdir',
label='label')
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])
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)
......@@ -225,7 +295,7 @@ class TestGrid(unittest.TestCase):
del newgrid
def test_save_and_load_gzip(self):
filename = os.path.join('outdir', 'test_output.json.gz')
filename = os.path.join("outdir", "test_output.json.gz")
self.grid.save_to_file(filename=filename)
......@@ -236,25 +306,27 @@ class TestGrid(unittest.TestCase):
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 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.grid.save_to_file(overwrite=True, outdir='outdir',
gzip=True)
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)
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 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)
......
from __future__ import absolute_import
import matplotlib
matplotlib.use('Agg')
matplotlib.use("Agg")
import unittest
import os
......@@ -12,14 +13,14 @@ from past.builtins import execfile
import bilby.core.utils
# Imported to ensure the examples run
import numpy as np
import inspect
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'
outdir = "outdir"
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = os.path.abspath(os.path.join(dir_path, os.path.pardir))
......@@ -29,8 +30,7 @@ class Test(unittest.TestCase):
try:
shutil.rmtree(self.outdir)
except OSError:
logging.warning(
"{} not removed prior to tests".format(self.outdir))
logging.warning("{} not removed prior to tests".format(self.outdir))
@classmethod
def tearDownClass(self):
......@@ -38,18 +38,18 @@ class Test(unittest.TestCase):
try:
shutil.rmtree(self.outdir)
except OSError:
logging.warning(
"{} not removed prior to tests".format(self.outdir))
logging.warning("{} not removed prior to tests".format(self.outdir))
def test_examples(self):
""" Loop over examples to check they run """
examples = ['examples/gw_examples/injection_examples/fast_tutorial.py',
'examples/gw_examples/data_examples/GW150914.py',
]
examples = [
"examples/gw_examples/injection_examples/fast_tutorial.py",
"examples/gw_examples/data_examples/GW150914.py",
]
for filename in examples:
print("Testing {}".format(filename))
execfile(filename)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
This diff is collapsed.
......@@ -8,11 +8,10 @@ import bilby
class TestCBCResult(unittest.TestCase):
def setUp(self):
bilby.utils.command_line_args.bilby_test_mode = False
priors = bilby.gw.prior.BBHPriorDict()
priors['geocent_time'] = 2
priors["geocent_time"] = 2
injection_parameters = priors.sample()
self.meta_data = dict(
likelihood=dict(
......@@ -21,11 +20,12 @@ class TestCBCResult(unittest.TestCase):
time_marginalization=True,
frequency_domain_source_model=bilby.gw.source.lal_binary_black_hole,
waveform_arguments=dict(
reference_frequency=20.0,
waveform_approximant='IMRPhenomPv2'),
reference_frequency=20.0, waveform_approximant="IMRPhenomPv2"
),
interferometers=dict(
H1=dict(optimal_SNR=1, parameters=injection_parameters),
L1=dict(optimal_SNR=1, parameters=injection_parameters)),
L1=dict(optimal_SNR=1, parameters=injection_parameters),
),
sampling_frequency=4096,
duration=4,
start_time=0,
......@@ -34,12 +34,16 @@ class TestCBCResult(unittest.TestCase):
)
)
self.result = bilby.gw.result.CBCResult(
label='label', outdir='outdir', sampler='nestle',
search_parameter_keys=list(priors.keys()), fixed_parameter_keys=list(),
priors=priors, sampler_kwargs=dict(test='test', func=lambda x: x),
label="label",
outdir="outdir",
sampler="nestle",
search_parameter_keys=list(priors.keys()),
fixed_parameter_keys=list(),
priors=priors,
sampler_kwargs=dict(test="test", func=lambda x: x),
injection_parameters=injection_parameters,
meta_data=self.meta_data,
posterior=pd.DataFrame(priors.sample(100))
posterior=pd.DataFrame(priors.sample(100)),
)
if not os.path.isdir(self.result.outdir):
os.mkdir(self.result.outdir)
......@@ -63,7 +67,9 @@ class TestCBCResult(unittest.TestCase):
label="recalib_H1_",
n_nodes=5,
)
calibration_filename = f"{self.result.outdir}/{self.result.label}_calibration.png"
calibration_filename = (
f"{self.result.outdir}/{self.result.label}_calibration.png"
)
for key in calibration_prior:
self.result.posterior[key] = calibration_prior[key].sample(100)
self.result.plot_calibration_posterior()
......@@ -71,7 +77,9 @@ class TestCBCResult(unittest.TestCase):
def test_calibration_plot_returns_none_with_no_calibration_parameters(self):
self.assertIsNone(self.result.plot_calibration_posterior())
calibration_filename = f"{self.result.outdir}/{self.result.label}_calibration.png"
calibration_filename = (
f"{self.result.outdir}/{self.result.label}_calibration.png"
)
self.assertFalse(os.path.exists(calibration_filename))
def test_calibration_pdf_plot(self):
......@@ -83,7 +91,9 @@ class TestCBCResult(unittest.TestCase):
label="recalib_H1_",
n_nodes=5,
)
calibration_filename = f"{self.result.outdir}/{self.result.label}_calibration.pdf"
calibration_filename = (
f"{self.result.outdir}/{self.result.label}_calibration.pdf"
)
for key in calibration_prior:
self.result.posterior[key] = calibration_prior[key].sample(100)
self.result.plot_calibration_posterior(format="pdf")
......@@ -96,20 +106,31 @@ class TestCBCResult(unittest.TestCase):
def test_waveform_plotting_png(self):
self.result.plot_waveform_posterior(n_samples=200)
for ifo in self.result.interferometers:
self.assertTrue(os.path.exists(
f"{self.result.outdir}/{self.result.label}_{ifo}_waveform.png")
self.assertTrue(
os.path.exists(
f"{self.result.outdir}/{self.result.label}_{ifo}_waveform.png"
)
)
def test_plot_skymap_meta_data(self):
from ligo.skymap import io
expected_keys = {
"HISTORY", "build_date", "creator", "distmean", "diststd",
"gps_creation_time", "gps_time", "nest", "objid", "origin",
"vcs_revision", "vcs_version", "instruments"
"HISTORY",
"build_date",
"creator",
"distmean",
"diststd",
"gps_creation_time",
"gps_time",
"nest",
"objid",
"origin",
"vcs_revision",
"vcs_version",
"instruments",
}
self.result.plot_skymap(
maxpts=50, geo=False, objid="test", instruments="H1L1"
)
self.result.plot_skymap(maxpts=50, geo=False, objid="test", instruments="H1L1")
fits_filename = f"{self.result.outdir}/{self.result.label}_skymap.fits"
skymap_filename = f"{self.result.outdir}/{self.result.label}_skymap.png"
pickle_filename = f"{self.result.outdir}/{self.result.label}_skypost.obj"
......@@ -118,10 +139,14 @@ class TestCBCResult(unittest.TestCase):
self.assertTrue(os.path.exists(skymap_filename))
self.assertTrue(os.path.exists(pickle_filename))
self.result.plot_skymap(
maxpts=50, geo=False, objid="test", instruments="H1L1",
load_pickle=True, colorbar=True
maxpts=50,
geo=False,
objid="test",
instruments="H1L1",
load_pickle=True,
colorbar=True,
)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment