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: ...@@ -157,3 +157,18 @@ deploy_release:
- twine upload dist/* - twine upload dist/*
only: only:
- tags - 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): ...@@ -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. 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. 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 ## Code relevance
The bilby code base is intended to be highly modular and flexible. We encourage The bilby code base is intended to be highly modular and flexible. We encourage
......
...@@ -173,5 +173,3 @@ texinfo_documents = [ ...@@ -173,5 +173,3 @@ texinfo_documents = [
] ]
numpydoc_show_class_members = False numpydoc_show_class_members = False
[flake8] [flake8]
exclude = .git,docs,build,dist,test,*__init__.py exclude = .git,docs,build,dist,test,*__init__.py
max-line-length = 120 max-line-length = 120
ignore = E129 W503 W504 W605 E203 ignore = E129 W503 W504 W605 E203 E402
[tool:pytest] [tool:pytest]
addopts = addopts =
......
...@@ -4,7 +4,6 @@ import numpy as np ...@@ -4,7 +4,6 @@ import numpy as np
class TestBaseClass(unittest.TestCase): class TestBaseClass(unittest.TestCase):
def setUp(self): def setUp(self):
self.model = calibration.Recalibrate() self.model = calibration.Recalibrate()
...@@ -12,7 +11,7 @@ class TestBaseClass(unittest.TestCase): ...@@ -12,7 +11,7 @@ class TestBaseClass(unittest.TestCase):
del self.model del self.model
def test_repr(self): def test_repr(self):
expected = 'Recalibrate(prefix={})'.format('\'recalib_\'') expected = "Recalibrate(prefix={})".format("'recalib_'")
actual = repr(self.model) actual = repr(self.model)
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
...@@ -23,18 +22,22 @@ class TestBaseClass(unittest.TestCase): ...@@ -23,18 +22,22 @@ class TestBaseClass(unittest.TestCase):
class TestCubicSpline(unittest.TestCase): class TestCubicSpline(unittest.TestCase):
def setUp(self): def setUp(self):
self.prefix = 'recalib_' self.prefix = "recalib_"
self.minimum_frequency = 20 self.minimum_frequency = 20
self.maximum_frequency = 1024 self.maximum_frequency = 1024
self.n_points = 5 self.n_points = 5
self.model = calibration.CubicSpline( self.model = calibration.CubicSpline(
prefix=self.prefix, minimum_frequency=self.minimum_frequency, prefix=self.prefix,
maximum_frequency=self.maximum_frequency, n_points=self.n_points) minimum_frequency=self.minimum_frequency,
self.parameters = {'recalib_{}_{}'.format(param, ii): 0.0 maximum_frequency=self.maximum_frequency,
for ii in range(5) n_points=self.n_points,
for param in ['amplitude', 'phase']} )
self.parameters = {
"recalib_{}_{}".format(param, ii): 0.0
for ii in range(5)
for param in ["amplitude", "phase"]
}
def tearDown(self): def tearDown(self):
del self.prefix del self.prefix
...@@ -46,27 +49,28 @@ class TestCubicSpline(unittest.TestCase): ...@@ -46,27 +49,28 @@ class TestCubicSpline(unittest.TestCase):
def test_calibration_factor(self): def test_calibration_factor(self):
frequency_array = np.linspace(20, 1024, 1000) frequency_array = np.linspace(20, 1024, 1000)
cal_factor = self.model.get_calibration_factor(frequency_array, cal_factor = self.model.get_calibration_factor(
**self.parameters) frequency_array, **self.parameters
)
assert np.alltrue(cal_factor.real == np.ones_like(frequency_array)) assert np.alltrue(cal_factor.real == np.ones_like(frequency_array))
def test_repr(self): def test_repr(self):
expected = 'CubicSpline(prefix=\'{}\', minimum_frequency={}, maximum_frequency={}, n_points={})'\ expected = "CubicSpline(prefix='{}', minimum_frequency={}, maximum_frequency={}, n_points={})".format(
.format(self.prefix, self.minimum_frequency, self.maximum_frequency, self.n_points) self.prefix, self.minimum_frequency, self.maximum_frequency, self.n_points
)
actual = repr(self.model) actual = repr(self.model)
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
class TestCubicSplineRequiresFourNodes(unittest.TestCase): class TestCubicSplineRequiresFourNodes(unittest.TestCase):
def test_cannot_instantiate_with_too_few_nodes(self): def test_cannot_instantiate_with_too_few_nodes(self):
for ii in range(6): for ii in range(6):
if ii < 4: if ii < 4:
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
calibration.CubicSpline('test', 1, 10, ii) calibration.CubicSpline("test", 1, 10, ii)
else: else:
calibration.CubicSpline('test', 1, 10, ii) calibration.CubicSpline("test", 1, 10, ii)
if __name__ == '__main__': if __name__ == "__main__":
unittest.main() unittest.main()
import os import os
import sys 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 ...@@ -6,18 +6,17 @@ from bilby.gw import cosmology
class TestSetCosmology(unittest.TestCase): class TestSetCosmology(unittest.TestCase):
def setUp(self): def setUp(self):
pass pass
def test_setting_cosmology_with_string(self): def test_setting_cosmology_with_string(self):
cosmology.set_cosmology('WMAP9') cosmology.set_cosmology("WMAP9")
self.assertEqual(cosmology.COSMOLOGY[1], 'WMAP9') self.assertEqual(cosmology.COSMOLOGY[1], "WMAP9")
cosmology.set_cosmology('Planck15') cosmology.set_cosmology("Planck15")
def test_setting_cosmology_with_astropy_object(self): def test_setting_cosmology_with_astropy_object(self):
cosmology.set_cosmology(WMAP9) cosmology.set_cosmology(WMAP9)
self.assertEqual(cosmology.COSMOLOGY[1], 'WMAP9') self.assertEqual(cosmology.COSMOLOGY[1], "WMAP9")
cosmology.set_cosmology(Planck15) cosmology.set_cosmology(Planck15)
def test_setting_cosmology_with_default(self): def test_setting_cosmology_with_default(self):
...@@ -27,30 +26,29 @@ class TestSetCosmology(unittest.TestCase): ...@@ -27,30 +26,29 @@ class TestSetCosmology(unittest.TestCase):
def test_setting_cosmology_with_flat_lambda_cdm_dict(self): def test_setting_cosmology_with_flat_lambda_cdm_dict(self):
cosmo_dict = dict(H0=67.7, Om0=0.3) cosmo_dict = dict(H0=67.7, Om0=0.3)
cosmology.set_cosmology(cosmo_dict) 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): def test_setting_cosmology_with_lambda_cdm_dict(self):
cosmo_dict = dict(H0=67.7, Om0=0.3, Ode0=0.7) cosmo_dict = dict(H0=67.7, Om0=0.3, Ode0=0.7)
cosmology.set_cosmology(cosmo_dict) 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): def test_setting_cosmology_with_w_cdm_dict(self):
cosmo_dict = dict(H0=67.7, Om0=0.3, Ode0=0.7, w0=-1.0) cosmo_dict = dict(H0=67.7, Om0=0.3, Ode0=0.7, w0=-1.0)
cosmology.set_cosmology(cosmo_dict) cosmology.set_cosmology(cosmo_dict)
self.assertEqual(cosmology.COSMOLOGY[1][:4], 'wCDM') self.assertEqual(cosmology.COSMOLOGY[1][:4], "wCDM")
class TestGetCosmology(unittest.TestCase): class TestGetCosmology(unittest.TestCase):
def setUp(self): def setUp(self):
pass pass
def test_getting_cosmology_with_string(self): 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): def test_getting_cosmology_with_default(self):
self.assertEqual(cosmology.get_cosmology(), cosmology.COSMOLOGY[0]) self.assertEqual(cosmology.get_cosmology(), cosmology.COSMOLOGY[0])
if __name__ == '__main__': if __name__ == "__main__":
unittest.main() unittest.main()
This diff is collapsed.
from __future__ import absolute_import from __future__ import absolute_import
import matplotlib import matplotlib
matplotlib.use('Agg')
matplotlib.use("Agg")
import unittest import unittest
import os import os
...@@ -12,14 +13,14 @@ from past.builtins import execfile ...@@ -12,14 +13,14 @@ from past.builtins import execfile
import bilby.core.utils import bilby.core.utils
# Imported to ensure the examples run # Imported to ensure the examples run
import numpy as np import numpy as np # noqa: F401
import inspect import inspect # noqa: F401
bilby.core.utils.command_line_args.bilby_test_mode = True bilby.core.utils.command_line_args.bilby_test_mode = True
class Test(unittest.TestCase): class Test(unittest.TestCase):
outdir = 'outdir' outdir = "outdir"
dir_path = os.path.dirname(os.path.realpath(__file__)) dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = os.path.abspath(os.path.join(dir_path, os.path.pardir)) dir_path = os.path.abspath(os.path.join(dir_path, os.path.pardir))
...@@ -29,8 +30,7 @@ class Test(unittest.TestCase): ...@@ -29,8 +30,7 @@ class Test(unittest.TestCase):
try: try:
shutil.rmtree(self.outdir) shutil.rmtree(self.outdir)
except OSError: except OSError:
logging.warning( logging.warning("{} not removed prior to tests".format(self.outdir))
"{} not removed prior to tests".format(self.outdir))
@classmethod @classmethod
def tearDownClass(self): def tearDownClass(self):
...@@ -38,20 +38,18 @@ class Test(unittest.TestCase): ...@@ -38,20 +38,18 @@ class Test(unittest.TestCase):
try: try:
shutil.rmtree(self.outdir) shutil.rmtree(self.outdir)
except OSError: except OSError:
logging.warning( logging.warning("{} not removed prior to tests".format(self.outdir))
"{} not removed prior to tests".format(self.outdir))
def test_examples(self): def test_examples(self):
""" Loop over examples to check they run """ """ Loop over examples to check they run """
examples = ['examples/core_examples/linear_regression.py', examples = [
'examples/core_examples/linear_regression_unknown_noise.py', "examples/core_examples/linear_regression.py",
] "examples/core_examples/linear_regression_unknown_noise.py",
]
for filename in examples: for filename in examples:
print("Testing {}".format(filename)) print("Testing {}".format(filename))
execfile(filename) execfile(filename)
if __name__ == '__main__': if __name__ == "__main__":
unittest.main() unittest.main()
...@@ -2,10 +2,8 @@ from __future__ import absolute_import, division ...@@ -2,10 +2,8 @@ from __future__ import absolute_import, division
import unittest import unittest
import numpy as np import numpy as np
import pandas as pd
import shutil import shutil
import os import os
import json
from scipy.stats import multivariate_normal from scipy.stats import multivariate_normal
import bilby import bilby
...@@ -30,32 +28,41 @@ class MultiGaussian(bilby.Likelihood): ...@@ -30,32 +28,41 @@ class MultiGaussian(bilby.Likelihood):
class TestGrid(unittest.TestCase): class TestGrid(unittest.TestCase):
def setUp(self): def setUp(self):
np.random.seed(7) np.random.seed(7)
# set 2D multivariate Gaussian (zero mean, unit variance) # set 2D multivariate Gaussian (zero mean, unit variance)
self.mus = [0., 0.] self.mus = [0.0, 0.0]
self.cov = [[1., 0.], [0., 1.]] self.cov = [[1.0, 0.0], [0.0, 1.0]]
dim = len(self.mus) dim = len(self.mus)
self.likelihood = MultiGaussian(self.mus, self.cov) self.likelihood = MultiGaussian(self.mus, self.cov)
# set priors out to +/- 5 sigma # set priors out to +/- 5 sigma
self.priors = bilby.core.prior.PriorDict() self.priors = bilby.core.prior.PriorDict()
self.priors.update( 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 # 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.expected_ln_evidence = -log_prior_vol
self.grid_size = 100 self.grid_size = 100
grid = bilby.core.grid.Grid( grid = bilby.core.grid.Grid(
label='label', outdir='outdir', priors=self.priors, label="label",
grid_size=self.grid_size, likelihood=self.likelihood, outdir="outdir",
save=True priors=self.priors,
grid_size=self.grid_size,
likelihood=self.likelihood,
save=True,
) )
self.grid = grid self.grid = grid
...@@ -71,29 +78,33 @@ class TestGrid(unittest.TestCase): ...@@ -71,29 +78,33 @@ class TestGrid(unittest.TestCase):
pass pass
def test_grid_file_name_default(self): def test_grid_file_name_default(self):
outdir = 'outdir' outdir = "outdir"
label = 'label' label = "label"
self.assertEqual(bilby.core.grid.grid_file_name(outdir, label), self.assertEqual(
'{}/{}_grid.json'.format(outdir, label)) bilby.core.grid.grid_file_name(outdir, label),
self.assertEqual(bilby.core.grid.grid_file_name(outdir, label, True), "{}/{}_grid.json".format(outdir, label),
'{}/{}_grid.json.gz'.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): def test_fail_save_and_load(self):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
bilby.core.grid.Grid.read() bilby.core.grid.Grid.read()
with self.assertRaises(IOError): 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): def test_fail_marginalize(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
self.grid.marginalize_posterior(parameters=2.4) self.grid.marginalize_posterior(parameters=2.4)
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
self.grid.marginalize_posterior(not_parameters=4.7) self.grid.marginalize_posterior(not_parameters=4.7)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.grid.marginalize_posterior(parameters='jkgsd') self.grid.marginalize_posterior(parameters="jkgsd")
def test_parameter_names(self): def test_parameter_names(self):
assert list(self.priors.keys()) == self.grid.parameter_names assert list(self.priors.keys()) == self.grid.parameter_names
...@@ -102,94 +113,150 @@ class TestGrid(unittest.TestCase): ...@@ -102,94 +113,150 @@ class TestGrid(unittest.TestCase):
def test_no_marginalization(self): def test_no_marginalization(self):
# test arrays are the same if no parameters are given to marginalize # test arrays are the same if no parameters are given to marginalize
# over # over
assert np.array_equal(self.grid.ln_likelihood, assert np.array_equal(
self.grid.marginalize_ln_likelihood(not_parameters=self.grid.parameter_names)) self.grid.ln_likelihood,
self.grid.marginalize_ln_likelihood(
not_parameters=self.grid.parameter_names
),
)
def test_marginalization_shapes(self): def test_marginalization_shapes(self):
assert len(self.grid.marginalize_ln_likelihood().shape) == 0 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,) 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 marg2.shape == (self.grid_size,)
assert self.grid.ln_likelihood.shape == (self.grid_size, 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) assert self.grid.ln_posterior.shape == (self.grid_size, self.grid_size)
def test_marginalization_opposite(self): def test_marginalization_opposite(self):
assert np.array_equal(self.grid.marginalize_ln_likelihood(parameters=self.grid.parameter_names[0]), assert np.array_equal(
self.grid.marginalize_ln_likelihood(not_parameters=self.grid.parameter_names[1])) self.grid.marginalize_ln_likelihood(
assert np.array_equal(self.grid.marginalize_ln_likelihood(parameters=self.grid.parameter_names[1]), parameters=self.grid.parameter_names[0]
self.grid.marginalize_ln_likelihood(not_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): def test_max_marginalized_likelihood(self):
# marginalised likelihoods should have max values of 1 (as they are not # marginalised likelihoods should have max values of 1 (as they are not
# properly normalised) # properly normalised)
assert self.grid.marginalize_likelihood(self.grid.parameter_names[0]).max() == 1. assert (
assert self.grid.marginalize_likelihood(self.grid.parameter_names[1]).max() == 1. 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): def test_ln_evidence(self):
assert np.isclose(self.grid.ln_evidence, self.expected_ln_evidence) assert np.isclose(self.grid.ln_evidence, self.expected_ln_evidence)
def test_fail_grid_size(self): def test_fail_grid_size(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
grid = bilby.core.grid.Grid( bilby.core.grid.Grid(
label='label', outdir='outdir', priors=self.priors, label="label",
grid_size=2.3, likelihood=self.likelihood, outdir="outdir",
save=True priors=self.priors,
grid_size=2.3,
likelihood=self.likelihood,
save=True,
) )
def test_mesh_grid(self): def test_mesh_grid(self):
assert self.grid.mesh_grid[0].shape == (self.grid_size, self.grid_size) 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 (
assert self.grid.mesh_grid[0][-1,-1] == self.priors[self.grid.parameter_names[1]].maximum 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): def test_different_grids(self):
npoints = [10, 20] npoints = [10, 20]
grid = bilby.core.grid.Grid( grid = bilby.core.grid.Grid(
label='label', outdir='outdir', priors=self.priors, label="label",
grid_size=npoints, likelihood=self.likelihood outdir="outdir",
priors=self.priors,
grid_size=npoints,
likelihood=self.likelihood,
) )
assert grid.mesh_grid[0].shape == tuple(npoints) assert grid.mesh_grid[0].shape == tuple(npoints)
assert grid.mesh_grid[0][0,0] == self.priors[self.grid.parameter_names[0]].minimum assert (
assert grid.mesh_grid[0][-1,-1] == self.priors[self.grid.parameter_names[1]].maximum 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 del grid
npoints = {'x0': 15, 'x1': 18} npoints = {"x0": 15, "x1": 18}
grid = bilby.core.grid.Grid( grid = bilby.core.grid.Grid(
label='label', outdir='outdir', priors=self.priors, label="label",
grid_size=npoints, likelihood=self.likelihood 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].shape == (npoints["x0"], npoints["x1"])
assert grid.mesh_grid[0][0,0] == self.priors[self.grid.parameter_names[0]].minimum assert (
assert grid.mesh_grid[0][-1,-1] == self.priors[self.grid.parameter_names[1]].maximum 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 del grid
x0s = np.linspace(self.priors['x0'].minimum, self.priors['x0'].maximum, 13) x0s = np.linspace(self.priors["x0"].minimum, self.priors["x0"].maximum, 13)
x1s = np.linspace(self.priors['x0'].minimum, self.priors['x0'].maximum, 14) x1s = np.linspace(self.priors["x0"].minimum, self.priors["x0"].maximum, 14)
npoints = {'x0': x0s, npoints = {"x0": x0s, "x1": x1s}
'x1': x1s}
grid = bilby.core.grid.Grid( grid = bilby.core.grid.Grid(
label='label', outdir='outdir', priors=self.priors, label="label",
grid_size=npoints, likelihood=self.likelihood 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].shape == (len(x0s), len(x1s))
assert grid.mesh_grid[0][0,0] == self.priors[self.grid.parameter_names[0]].minimum assert (
assert grid.mesh_grid[0][-1,-1] == self.priors[self.grid.parameter_names[1]].maximum grid.mesh_grid[0][0, 0] == self.priors[self.grid.parameter_names[0]].minimum
assert np.array_equal(grid.sample_points['x0'], x0s) )
assert np.array_equal(grid.sample_points['x1'], x1s) 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): 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) self.grid.save_to_file(filename=filename)
...@@ -200,24 +267,27 @@ class TestGrid(unittest.TestCase): ...@@ -200,24 +267,27 @@ class TestGrid(unittest.TestCase):
assert newgrid.n_dims == self.grid.n_dims assert newgrid.n_dims == self.grid.n_dims
assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0]) assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0])
for par in newgrid.parameter_names: 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 newgrid.ln_evidence == self.grid.ln_evidence
assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood) assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood)
assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior) assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior)
del newgrid del newgrid
self.grid.save_to_file(overwrite=True, outdir='outdir') self.grid.save_to_file(overwrite=True, outdir="outdir")
# load file # load file
newgrid = bilby.core.grid.Grid.read(outdir='outdir', newgrid = bilby.core.grid.Grid.read(outdir="outdir", label="label")
label='label')
assert newgrid.parameter_names == self.grid.parameter_names assert newgrid.parameter_names == self.grid.parameter_names
assert newgrid.n_dims == self.grid.n_dims assert newgrid.n_dims == self.grid.n_dims
assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0]) assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0])
for par in newgrid.parameter_names: 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 newgrid.ln_evidence == self.grid.ln_evidence
assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood) assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood)
assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior) assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior)
...@@ -225,7 +295,7 @@ class TestGrid(unittest.TestCase): ...@@ -225,7 +295,7 @@ class TestGrid(unittest.TestCase):
del newgrid del newgrid
def test_save_and_load_gzip(self): 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) self.grid.save_to_file(filename=filename)
...@@ -236,25 +306,27 @@ class TestGrid(unittest.TestCase): ...@@ -236,25 +306,27 @@ class TestGrid(unittest.TestCase):
assert newgrid.n_dims == self.grid.n_dims assert newgrid.n_dims == self.grid.n_dims
assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0]) assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0])
for par in newgrid.parameter_names: 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 newgrid.ln_evidence == self.grid.ln_evidence
assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood) assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood)
assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior) assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior)
del newgrid del newgrid
self.grid.save_to_file(overwrite=True, outdir='outdir', self.grid.save_to_file(overwrite=True, outdir="outdir", gzip=True)
gzip=True)
# load file # load file
newgrid = bilby.core.grid.Grid.read(outdir='outdir', label='label', newgrid = bilby.core.grid.Grid.read(outdir="outdir", label="label", gzip=True)
gzip=True)
assert newgrid.parameter_names == self.grid.parameter_names assert newgrid.parameter_names == self.grid.parameter_names
assert newgrid.n_dims == self.grid.n_dims assert newgrid.n_dims == self.grid.n_dims
assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0]) assert np.array_equal(newgrid.mesh_grid[0], self.grid.mesh_grid[0])
for par in newgrid.parameter_names: 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 newgrid.ln_evidence == self.grid.ln_evidence
assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood) assert np.array_equal(newgrid.ln_likelihood, self.grid.ln_likelihood)
assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior) assert np.array_equal(newgrid.ln_posterior, self.grid.ln_posterior)
......
from __future__ import absolute_import from __future__ import absolute_import
import matplotlib import matplotlib
matplotlib.use('Agg')
matplotlib.use("Agg")
import unittest import unittest
import os import os
...@@ -12,14 +13,14 @@ from past.builtins import execfile ...@@ -12,14 +13,14 @@ from past.builtins import execfile
import bilby.core.utils import bilby.core.utils
# Imported to ensure the examples run # Imported to ensure the examples run
import numpy as np import numpy as np # noqa: F401
import inspect import inspect # noqa: F401
bilby.core.utils.command_line_args.bilby_test_mode = True bilby.core.utils.command_line_args.bilby_test_mode = True
class Test(unittest.TestCase): class Test(unittest.TestCase):
outdir = 'outdir' outdir = "outdir"
dir_path = os.path.dirname(os.path.realpath(__file__)) dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = os.path.abspath(os.path.join(dir_path, os.path.pardir)) dir_path = os.path.abspath(os.path.join(dir_path, os.path.pardir))
...@@ -29,8 +30,7 @@ class Test(unittest.TestCase): ...@@ -29,8 +30,7 @@ class Test(unittest.TestCase):
try: try:
shutil.rmtree(self.outdir) shutil.rmtree(self.outdir)
except OSError: except OSError:
logging.warning( logging.warning("{} not removed prior to tests".format(self.outdir))
"{} not removed prior to tests".format(self.outdir))
@classmethod @classmethod
def tearDownClass(self): def tearDownClass(self):
...@@ -38,18 +38,18 @@ class Test(unittest.TestCase): ...@@ -38,18 +38,18 @@ class Test(unittest.TestCase):
try: try:
shutil.rmtree(self.outdir) shutil.rmtree(self.outdir)
except OSError: except OSError:
logging.warning( logging.warning("{} not removed prior to tests".format(self.outdir))
"{} not removed prior to tests".format(self.outdir))
def test_examples(self): def test_examples(self):
""" Loop over examples to check they run """ """ Loop over examples to check they run """
examples = ['examples/gw_examples/injection_examples/fast_tutorial.py', examples = [
'examples/gw_examples/data_examples/GW150914.py', "examples/gw_examples/injection_examples/fast_tutorial.py",
] "examples/gw_examples/data_examples/GW150914.py",
]
for filename in examples: for filename in examples:
print("Testing {}".format(filename)) print("Testing {}".format(filename))
execfile(filename) execfile(filename)
if __name__ == '__main__': if __name__ == "__main__":
unittest.main() unittest.main()
This diff is collapsed.
...@@ -8,11 +8,10 @@ import bilby ...@@ -8,11 +8,10 @@ import bilby
class TestCBCResult(unittest.TestCase): class TestCBCResult(unittest.TestCase):
def setUp(self): def setUp(self):
bilby.utils.command_line_args.bilby_test_mode = False bilby.utils.command_line_args.bilby_test_mode = False
priors = bilby.gw.prior.BBHPriorDict() priors = bilby.gw.prior.BBHPriorDict()
priors['geocent_time'] = 2 priors["geocent_time"] = 2
injection_parameters = priors.sample() injection_parameters = priors.sample()
self.meta_data = dict( self.meta_data = dict(
likelihood=dict( likelihood=dict(
...@@ -21,11 +20,12 @@ class TestCBCResult(unittest.TestCase): ...@@ -21,11 +20,12 @@ class TestCBCResult(unittest.TestCase):
time_marginalization=True, time_marginalization=True,
frequency_domain_source_model=bilby.gw.source.lal_binary_black_hole, frequency_domain_source_model=bilby.gw.source.lal_binary_black_hole,
waveform_arguments=dict( waveform_arguments=dict(
reference_frequency=20.0, reference_frequency=20.0, waveform_approximant="IMRPhenomPv2"
waveform_approximant='IMRPhenomPv2'), ),
interferometers=dict( interferometers=dict(
H1=dict(optimal_SNR=1, parameters=injection_parameters), 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, sampling_frequency=4096,
duration=4, duration=4,
start_time=0, start_time=0,
...@@ -34,12 +34,16 @@ class TestCBCResult(unittest.TestCase): ...@@ -34,12 +34,16 @@ class TestCBCResult(unittest.TestCase):
) )
) )
self.result = bilby.gw.result.CBCResult( self.result = bilby.gw.result.CBCResult(
label='label', outdir='outdir', sampler='nestle', label="label",
search_parameter_keys=list(priors.keys()), fixed_parameter_keys=list(), outdir="outdir",
priors=priors, sampler_kwargs=dict(test='test', func=lambda x: x), 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, injection_parameters=injection_parameters,
meta_data=self.meta_data, 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): if not os.path.isdir(self.result.outdir):
os.mkdir(self.result.outdir) os.mkdir(self.result.outdir)
...@@ -63,7 +67,9 @@ class TestCBCResult(unittest.TestCase): ...@@ -63,7 +67,9 @@ class TestCBCResult(unittest.TestCase):
label="recalib_H1_", label="recalib_H1_",
n_nodes=5, 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: for key in calibration_prior:
self.result.posterior[key] = calibration_prior[key].sample(100) self.result.posterior[key] = calibration_prior[key].sample(100)
self.result.plot_calibration_posterior() self.result.plot_calibration_posterior()
...@@ -71,7 +77,9 @@ class TestCBCResult(unittest.TestCase): ...@@ -71,7 +77,9 @@ class TestCBCResult(unittest.TestCase):
def test_calibration_plot_returns_none_with_no_calibration_parameters(self): def test_calibration_plot_returns_none_with_no_calibration_parameters(self):
self.assertIsNone(self.result.plot_calibration_posterior()) 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)) self.assertFalse(os.path.exists(calibration_filename))
def test_calibration_pdf_plot(self): def test_calibration_pdf_plot(self):
...@@ -83,7 +91,9 @@ class TestCBCResult(unittest.TestCase): ...@@ -83,7 +91,9 @@ class TestCBCResult(unittest.TestCase):
label="recalib_H1_", label="recalib_H1_",
n_nodes=5, 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: for key in calibration_prior:
self.result.posterior[key] = calibration_prior[key].sample(100) self.result.posterior[key] = calibration_prior[key].sample(100)
self.result.plot_calibration_posterior(format="pdf") self.result.plot_calibration_posterior(format="pdf")
...@@ -96,20 +106,31 @@ class TestCBCResult(unittest.TestCase): ...@@ -96,20 +106,31 @@ class TestCBCResult(unittest.TestCase):
def test_waveform_plotting_png(self): def test_waveform_plotting_png(self):
self.result.plot_waveform_posterior(n_samples=200) self.result.plot_waveform_posterior(n_samples=200)
for ifo in self.result.interferometers: for ifo in self.result.interferometers:
self.assertTrue(os.path.exists( self.assertTrue(
f"{self.result.outdir}/{self.result.label}_{ifo}_waveform.png") os.path.exists(
f"{self.result.outdir}/{self.result.label}_{ifo}_waveform.png"
)
) )
def test_plot_skymap_meta_data(self): def test_plot_skymap_meta_data(self):
from ligo.skymap import io from ligo.skymap import io
expected_keys = { expected_keys = {
"HISTORY", "build_date", "creator", "distmean", "diststd", "HISTORY",
"gps_creation_time", "gps_time", "nest", "objid", "origin", "build_date",
"vcs_revision", "vcs_version", "instruments" "creator",
"distmean",
"diststd",
"gps_creation_time",
"gps_time",
"nest",
"objid",
"origin",
"vcs_revision",
"vcs_version",
"instruments",
} }
self.result.plot_skymap( self.result.plot_skymap(maxpts=50, geo=False, objid="test", instruments="H1L1")
maxpts=50, geo=False, objid="test", instruments="H1L1"
)
fits_filename = f"{self.result.outdir}/{self.result.label}_skymap.fits" fits_filename = f"{self.result.outdir}/{self.result.label}_skymap.fits"
skymap_filename = f"{self.result.outdir}/{self.result.label}_skymap.png" skymap_filename = f"{self.result.outdir}/{self.result.label}_skymap.png"
pickle_filename = f"{self.result.outdir}/{self.result.label}_skypost.obj" pickle_filename = f"{self.result.outdir}/{self.result.label}_skypost.obj"
...@@ -118,10 +139,14 @@ class TestCBCResult(unittest.TestCase): ...@@ -118,10 +139,14 @@ class TestCBCResult(unittest.TestCase):
self.assertTrue(os.path.exists(skymap_filename)) self.assertTrue(os.path.exists(skymap_filename))
self.assertTrue(os.path.exists(pickle_filename)) self.assertTrue(os.path.exists(pickle_filename))
self.result.plot_skymap( self.result.plot_skymap(
maxpts=50, geo=False, objid="test", instruments="H1L1", maxpts=50,
load_pickle=True, colorbar=True geo=False,
objid="test",
instruments="H1L1",
load_pickle=True,
colorbar=True,
) )
if __name__ == '__main__': if __name__ == "__main__":
unittest.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