Commit 71d76f11 authored by Gregory Ashton's avatar Gregory Ashton
Browse files

Fix flake8 issues in tests and docs

- Also applies Black for future inclusion
parent 16d28028
......@@ -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
from __future__ import division, absolute_import
import unittest
import mock
import numpy as np
......@@ -9,36 +8,55 @@ from bilby.gw import conversion
class TestBasicConversions(unittest.TestCase):
def setUp(self):
self.mass_1 = 1.4
self.mass_2 = 1.3
self.mass_ratio = 13/14
self.mass_ratio = 13 / 14
self.total_mass = 2.7
self.chirp_mass = (1.4 * 1.3)**0.6 / 2.7**0.2
self.symmetric_mass_ratio = (1.4 * 1.3) / 2.7**2
self.chirp_mass = (1.4 * 1.3) ** 0.6 / 2.7 ** 0.2
self.symmetric_mass_ratio = (1.4 * 1.3) / 2.7 ** 2
self.cos_angle = -1
self.angle = np.pi
self.lambda_1 = 300
self.lambda_2 = 300 * (14 / 13)**5
self.lambda_tilde = 8 / 13 * (
(1 + 7 * self.symmetric_mass_ratio
- 31 * self.symmetric_mass_ratio**2)
* (self.lambda_1 + self.lambda_2)
+ (1 - 4 * self.symmetric_mass_ratio)**0.5
* (1 + 9 * self.symmetric_mass_ratio
- 11 * self.symmetric_mass_ratio**2)
* (self.lambda_1 - self.lambda_2)
self.lambda_2 = 300 * (14 / 13) ** 5
self.lambda_tilde = (
8
/ 13
* (
(
1
+ 7 * self.symmetric_mass_ratio
- 31 * self.symmetric_mass_ratio ** 2
)
* (self.lambda_1 + self.lambda_2)
+ (1 - 4 * self.symmetric_mass_ratio) ** 0.5
* (
1
+ 9 * self.symmetric_mass_ratio
- 11 * self.symmetric_mass_ratio ** 2
)
* (self.lambda_1 - self.lambda_2)
)
)
self.delta_lambda_tilde = 1 / 2 * (
(1 - 4 * self.symmetric_mass_ratio)**0.5
* (1 - 13272 / 1319 * self.symmetric_mass_ratio
+ 8944 / 1319 * self.symmetric_mass_ratio**2)
self.delta_lambda_tilde = (
1
/ 2
* (
(1 - 4 * self.symmetric_mass_ratio) ** 0.5
* (
1
- 13272 / 1319 * self.symmetric_mass_ratio
+ 8944 / 1319 * self.symmetric_mass_ratio ** 2
)
* (self.lambda_1 + self.lambda_2)
+ (1 - 15910 / 1319 * self.symmetric_mass_ratio
+ 32850 / 1319 * self.symmetric_mass_ratio**2
+ 3380 / 1319 * self.symmetric_mass_ratio**3)
+ (
1
- 15910 / 1319 * self.symmetric_mass_ratio
+ 32850 / 1319 * self.symmetric_mass_ratio ** 2
+ 3380 / 1319 * self.symmetric_mass_ratio ** 3
)
* (self.lambda_1 - self.lambda_2)
)
)
def tearDown(self):
......@@ -50,25 +68,35 @@ class TestBasicConversions(unittest.TestCase):
del self.symmetric_mass_ratio
def test_total_mass_and_mass_ratio_to_component_masses(self):
mass_1, mass_2 = conversion.total_mass_and_mass_ratio_to_component_masses(self.mass_ratio, self.total_mass)
self.assertTrue(all([abs(mass_1 - self.mass_1) < 1e-5,
abs(mass_2 - self.mass_2) < 1e-5]))
mass_1, mass_2 = conversion.total_mass_and_mass_ratio_to_component_masses(
self.mass_ratio, self.total_mass
)
self.assertTrue(
all([abs(mass_1 - self.mass_1) < 1e-5, abs(mass_2 - self.mass_2) < 1e-5])
)
def test_chirp_mass_and_primary_mass_to_mass_ratio(self):
mass_ratio = conversion.chirp_mass_and_primary_mass_to_mass_ratio(
self.chirp_mass, self.mass_1)
self.chirp_mass, self.mass_1
)
self.assertAlmostEqual(self.mass_ratio, mass_ratio)
def test_symmetric_mass_ratio_to_mass_ratio(self):
mass_ratio = conversion.symmetric_mass_ratio_to_mass_ratio(self.symmetric_mass_ratio)
mass_ratio = conversion.symmetric_mass_ratio_to_mass_ratio(
self.symmetric_mass_ratio
)
self.assertAlmostEqual(self.mass_ratio, mass_ratio)
def test_chirp_mass_and_total_mass_to_symmetric_mass_ratio(self):
symmetric_mass_ratio = conversion.chirp_mass_and_total_mass_to_symmetric_mass_ratio(self.chirp_mass, self.total_mass)
symmetric_mass_ratio = conversion.chirp_mass_and_total_mass_to_symmetric_mass_ratio(
self.chirp_mass, self.total_mass
)
self.assertAlmostEqual(self.symmetric_mass_ratio, symmetric_mass_ratio)
def test_chirp_mass_and_mass_ratio_to_total_mass(self):
total_mass = conversion.chirp_mass_and_mass_ratio_to_total_mass(self.chirp_mass, self.mass_ratio)
total_mass = conversion.chirp_mass_and_mass_ratio_to_total_mass(
self.chirp_mass, self.mass_ratio
)
self.assertAlmostEqual(self.total_mass, total_mass)
def test_component_masses_to_chirp_mass(self):
......@@ -80,7 +108,9 @@ class TestBasicConversions(unittest.TestCase):
self.assertAlmostEqual(self.total_mass, total_mass)
def test_component_masses_to_symmetric_mass_ratio(self):
symmetric_mass_ratio = conversion.component_masses_to_symmetric_mass_ratio(self.mass_1, self.mass_2)
symmetric_mass_ratio = conversion.component_masses_to_symmetric_mass_ratio(
self.mass_1, self.mass_2
)
self.assertAlmostEqual(self.symmetric_mass_ratio, symmetric_mass_ratio)
def test_component_masses_to_mass_ratio(self):
......@@ -88,196 +118,281 @@ class TestBasicConversions(unittest.TestCase):
self.assertAlmostEqual(self.mass_ratio, mass_ratio)
def test_mass_1_and_chirp_mass_to_mass_ratio(self):
mass_ratio = conversion.mass_1_and_chirp_mass_to_mass_ratio(self.mass_1, self.chirp_mass)
mass_ratio = conversion.mass_1_and_chirp_mass_to_mass_ratio(
self.mass_1, self.chirp_mass
)
self.assertAlmostEqual(self.mass_ratio, mass_ratio)
def test_lambda_tilde_to_lambda_1_lambda_2(self):
lambda_1, lambda_2 =\
conversion.lambda_tilde_to_lambda_1_lambda_2(
self.lambda_tilde, self.mass_1, self.mass_2)
self.assertTrue(all([abs(self.lambda_1 - lambda_1) < 1e-5,
abs(self.lambda_2 - lambda_2) < 1e-5]))
lambda_1, lambda_2 = conversion.lambda_tilde_to_lambda_1_lambda_2(
self.lambda_tilde, self.mass_1, self.mass_2
)
self.assertTrue(
all(
[
abs(self.lambda_1 - lambda_1) < 1e-5,
abs(self.lambda_2 - lambda_2) < 1e-5,
]
)
)
def test_lambda_tilde_delta_lambda_tilde_to_lambda_1_lambda_2(self):
lambda_1, lambda_2 =\
conversion.lambda_tilde_delta_lambda_tilde_to_lambda_1_lambda_2(
self.lambda_tilde, self.delta_lambda_tilde, self.mass_1, self.mass_2)
self.assertTrue(all([abs(self.lambda_1 - lambda_1) < 1e-5,
abs(self.lambda_2 - lambda_2) < 1e-5]))
(
lambda_1,
lambda_2,
) = conversion.lambda_tilde_delta_lambda_tilde_to_lambda_1_lambda_2(
self.lambda_tilde, self.delta_lambda_tilde, self.mass_1, self.mass_2
)
self.assertTrue(
all(
[
abs(self.lambda_1 - lambda_1) < 1e-5,
abs(self.lambda_2 - lambda_2) < 1e-5,
]
)
)
def test_lambda_1_lambda_2_to_lambda_tilde(self):
lambda_tilde = \
conversion.lambda_1_lambda_2_to_lambda_tilde(
self.lambda_1, self.lambda_2, self.mass_1, self.mass_2)
lambda_tilde = conversion.lambda_1_lambda_2_to_lambda_tilde(
self.lambda_1, self.lambda_2, self.mass_1, self.mass_2
)
self.assertTrue((self.lambda_tilde - lambda_tilde) < 1e-5)
def test_lambda_1_lambda_2_to_delta_lambda_tilde(self):
delta_lambda_tilde = \
conversion.lambda_1_lambda_2_to_delta_lambda_tilde(
self.lambda_1, self.lambda_2, self.mass_1, self.mass_2)
delta_lambda_tilde = conversion.lambda_1_lambda_2_to_delta_lambda_tilde(
self.lambda_1, self.lambda_2, self.mass_1, self.mass_2
)
self.assertTrue((self.delta_lambda_tilde - delta_lambda_tilde) < 1e-5)
class TestConvertToLALParams(unittest.TestCase):
def setUp(self):
self.search_keys = []
self.parameters = dict()
self.component_mass_pars = dict(mass_1=1.4, mass_2=1.4)
self.mass_parameters = self.component_mass_pars.copy()
self.mass_parameters['mass_ratio'] =\
conversion.component_masses_to_mass_ratio(**self.component_mass_pars)
self.mass_parameters['symmetric_mass_ratio'] = \
conversion.component_masses_to_symmetric_mass_ratio(**self.component_mass_pars)
self.mass_parameters['chirp_mass'] = \
conversion.component_masses_to_chirp_mass(**self.component_mass_pars)
self.mass_parameters['total_mass'] = \
conversion.component_masses_to_total_mass(**self.component_mass_pars)
self.mass_parameters["mass_ratio"] = conversion.component_masses_to_mass_ratio(
**self.component_mass_pars
)
self.mass_parameters[
"symmetric_mass_ratio"
] = conversion.component_masses_to_symmetric_mass_ratio(
**self.component_mass_pars
)
self.mass_parameters["chirp_mass"] = conversion.component_masses_to_chirp_mass(
**self.component_mass_pars
)
self.mass_parameters["total_mass"] = conversion.component_masses_to_total_mass(
**self.component_mass_pars
)
self.component_tidal_parameters = dict(lambda_1=300, lambda_2=300)
self.all_component_pars = self.component_tidal_parameters.copy()
self.all_component_pars.update(self.component_mass_pars)
self.tidal_parameters = self.component_tidal_parameters.copy()
self.tidal_parameters['lambda_tilde'] = \
conversion.lambda_1_lambda_2_to_lambda_tilde(**self.all_component_pars)
self.tidal_parameters['delta_lambda_tilde'] = \
conversion.lambda_1_lambda_2_to_delta_lambda_tilde(**self.all_component_pars)
self.tidal_parameters[
"lambda_tilde"
] = conversion.lambda_1_lambda_2_to_lambda_tilde(**self.all_component_pars)
self.tidal_parameters[
"delta_lambda_tilde"
] = conversion.lambda_1_lambda_2_to_delta_lambda_tilde(
**self.all_component_pars
)
def tearDown(self):
del self.search_keys
del self.parameters
def bbh_convert(self):
self.parameters, self.added_keys =\
conversion.convert_to_lal_binary_black_hole_parameters(
self.parameters)
(
self.parameters,
self.added_keys,
) = conversion.convert_to_lal_binary_black_hole_parameters(self.parameters)
def bns_convert(self):
self.parameters, self.added_keys = \
conversion.convert_to_lal_binary_neutron_star_parameters(
self.parameters)
(
self.parameters,
self.added_keys,
) = conversion.convert_to_lal_binary_neutron_star_parameters(self.parameters)
def test_redshift_to_luminosity_distance(self):
self.parameters['redshift'] = 1
dl = conversion.redshift_to_luminosity_distance(
self.parameters['redshift'])
self.parameters["redshift"] = 1
dl = conversion.redshift_to_luminosity_distance(self.parameters["redshift"])
self.bbh_convert()
self.assertEqual(self.parameters['luminosity_distance'], dl)
self.assertEqual(self.parameters["luminosity_distance"], dl)
def test_comoving_to_luminosity_distance(self):
self.parameters['comoving_distance'] = 1
self.parameters["comoving_distance"] = 1
dl = conversion.comoving_distance_to_luminosity_distance(
self.parameters['comoving_distance'])
self.parameters["comoving_distance"]
)
self.bbh_convert()
self.assertEqual(self.parameters['luminosity_distance'], dl)
self.assertEqual(self.parameters["luminosity_distance"], dl)
def test_source_to_lab_frame(self):
self.parameters['test_source'] = 1
self.parameters['redshift'] = 1
lab = self.parameters['test_source'] * (1 + self.parameters['redshift'])
self.parameters["test_source"] = 1
self.parameters["redshift"] = 1
lab = self.parameters["test_source"] * (1 + self.parameters["redshift"])
self.bbh_convert()
self.assertEqual(self.parameters['test'], lab)
self.assertEqual(self.parameters["test"], lab)
def _conversion_to_component_mass(self, keys):
for key in keys:
self.parameters[key] = self.mass_parameters[key]
self.bbh_convert()
self.assertAlmostEqual(
max([abs(self.parameters[key] - self.component_mass_pars[key])
for key in ['mass_1', 'mass_2']]), 0)
max(
[
abs(self.parameters[key] - self.component_mass_pars[key])
for key in ["mass_1", "mass_2"]
]
),
0,
)
def test_chirp_mass_total_mass(self):
self._conversion_to_component_mass(['chirp_mass', 'total_mass'])
self._conversion_to_component_mass(["chirp_mass", "total_mass"])
def test_chirp_mass_sym_mass_ratio(self):
self._conversion_to_component_mass(['chirp_mass', 'symmetric_mass_ratio'])
self._conversion_to_component_mass(["chirp_mass", "symmetric_mass_ratio"])
def test_chirp_mass_mass_ratio(self):
self._conversion_to_component_mass(['chirp_mass', 'mass_ratio'])
self._conversion_to_component_mass(["chirp_mass", "mass_ratio"])
def test_total_mass_sym_mass_ratio(self):
self._conversion_to_component_mass(['total_mass', 'symmetric_mass_ratio'])
self._conversion_to_component_mass(["total_mass", "symmetric_mass_ratio"])
def test_total_mass_mass_ratio(self):
self._conversion_to_component_mass(['total_mass', 'mass_ratio'])
self._conversion_to_component_mass(["total_mass", "mass_ratio"])
def test_total_mass_mass_1(self):
self._conversion_to_component_mass(['total_mass', 'mass_1'])
self._conversion_to_component_mass(["total_mass", "mass_1"])
def test_total_mass_mass_2(self):
self._conversion_to_component_mass(['total_mass', 'mass_2'])
self._conversion_to_component_mass(["total_mass", "mass_2"])
def test_sym_mass_ratio_mass_1(self):
self._conversion_to_component_mass(['symmetric_mass_ratio', 'mass_1'])
self._conversion_to_component_mass(["symmetric_mass_ratio", "mass_1"])
def test_sym_mass_ratio_mass_2(self):
self._conversion_to_component_mass(['symmetric_mass_ratio', 'mass_2'])
self._conversion_to_component_mass(["symmetric_mass_ratio", "mass_2"])
def test_mass_ratio_mass_1(self):
self._conversion_to_component_mass(['mass_ratio', 'mass_1'])
self._conversion_to_component_mass(["mass_ratio", "mass_1"])
def test_mass_ratio_mass_2(self):
self._conversion_to_component_mass(['mass_ratio', 'mass_2'])
self._conversion_to_component_mass(["mass_ratio", "mass_2"])
def test_bbh_aligned_spin_to_spherical(self):
self.parameters['chi_1'] = -0.5
a_1 = abs(self.parameters['chi_1'])
tilt_1 = np.arccos(np.sign(self.parameters['chi_1']))
self.parameters["chi_1"] = -0.5
a_1 = abs(self.parameters["chi_1"])
tilt_1 = np.arccos(np.sign(self.parameters["chi_1"]))
phi_jl = 0.0
phi_12 = 0.0
self.bbh_convert()
self.assertDictEqual(
{key: self.parameters[key]
for key in ['a_1', 'tilt_1', 'phi_12', 'phi_jl']},
dict(a_1=a_1, tilt_1=tilt_1, phi_jl=phi_jl, phi_12=phi_12))
{
key: self.parameters[key]
for key in ["a_1", "tilt_1", "phi_12", "phi_jl"]
},
dict(a_1=a_1, tilt_1=tilt_1, phi_jl=phi_jl, phi_12=phi_12),
)
def test_bbh_cos_angle_to_angle_conversion(self):
self.parameters['cos_tilt_1'] = 1
t1 = np.arccos(self.parameters['cos_tilt_1'])
self.parameters["cos_tilt_1"] = 1
t1 = np.arccos(self.parameters["cos_tilt_1"])
self.bbh_convert()
self.assertEqual(self.parameters['tilt_1'], t1)
self.assertEqual(self.parameters["tilt_1"], t1)
def _conversion_to_component_tidal(self, keys):
for key in keys:
self.parameters[key] = self.tidal_parameters[key]
for key in ['mass_1', 'mass_2']:
for key in ["mass_1", "mass_2"]:
self.parameters[key] = self.mass_parameters[key]
self.bns_convert()
component_dict = {
key: self.parameters[key] for key in ['lambda_1', 'lambda_2']}
component_dict = {key: self.parameters[key] for key in ["lambda_1", "lambda_2"]}
self.assertDictEqual(component_dict, self.component_tidal_parameters)
def test_lambda_tilde_delta_lambda_tilde(self):