Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
lscsoft
bilby
Commits
89151b51
Commit
89151b51
authored
Sep 04, 2020
by
Moritz Huebner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Restructure tests part 2
parent
5428c2ee
Changes
33
Hide whitespace changes
Inline
Side-by-side
Showing
33 changed files
with
794 additions
and
796 deletions
+794
-796
.coveragerc
.coveragerc
+3
-4
.gitlab-ci.yml
.gitlab-ci.yml
+5
-6
bilby/core/prior/base.py
bilby/core/prior/base.py
+0
-3
bilby/gw/utils.py
bilby/gw/utils.py
+10
-0
setup.cfg
setup.cfg
+4
-5
test/core/example_test.py
test/core/example_test.py
+0
-55
test/core/grid_test.py
test/core/grid_test.py
+82
-163
test/core/likelihood_test.py
test/core/likelihood_test.py
+0
-3
test/core/prior/base_test.py
test/core/prior/base_test.py
+52
-33
test/core/prior/dict_test.py
test/core/prior/dict_test.py
+73
-0
test/core/prior/prior_test.py
test/core/prior/prior_test.py
+0
-99
test/core/result_test.py
test/core/result_test.py
+0
-2
test/core/sampler/sampler_run_test.py
test/core/sampler/sampler_run_test.py
+0
-1
test/core/series_test.py
test/core/series_test.py
+0
-1
test/core/utils_test.py
test/core/utils_test.py
+0
-1
test/gw/conversion_test.py
test/gw/conversion_test.py
+0
-1
test/gw/cosmology_test.py
test/gw/cosmology_test.py
+0
-1
test/gw/eos/eos_test.py
test/gw/eos/eos_test.py
+0
-1
test/gw/likelihood_test.py
test/gw/likelihood_test.py
+0
-1
test/gw/plot_test.py
test/gw/plot_test.py
+0
-2
test/gw/prior_test.py
test/gw/prior_test.py
+0
-1
test/gw/result_test.py
test/gw/result_test.py
+0
-2
test/gw/source_test.py
test/gw/source_test.py
+0
-1
test/gw/utils_test.py
test/gw/utils_test.py
+0
-2
test/gw/waveform_generator_test.py
test/gw/waveform_generator_test.py
+0
-390
test/integration/__init__.py
test/integration/__init__.py
+0
-0
test/integration/example_test.py
test/integration/example_test.py
+19
-10
test/integration/make_standard_data.py
test/integration/make_standard_data.py
+0
-2
test/integration/noise_realisation_test.py
test/integration/noise_realisation_test.py
+0
-1
test/integration/other_test.py
test/integration/other_test.py
+2
-4
test/integration/sample_from_the_prior_test.py
test/integration/sample_from_the_prior_test.py
+0
-1
test/integration/sampler_run_test.py
test/integration/sampler_run_test.py
+164
-0
test/integration/test_waveforms.py
test/integration/test_waveforms.py
+380
-0
No files found.
.coveragerc
View file @
89151b51
[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
.gitlab-ci.yml
View file @
89151b51
...
...
@@ -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
...
...
bilby/core/prior/base.py
View file @
89151b51
...
...
@@ -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 """
bilby/gw/utils.py
View file @
89151b51
...
...
@@ -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
...
...
setup.cfg
View file @
89151b51
...
...
@@ -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
test/core/example_test.py
deleted
100644 → 0
View file @
5428c2ee
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
()
test/core/grid_test.py
View file @
89151b51
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
.
assert
ListEqual
(
list
(
self
.
priors
.
keys
())
,
self
.
grid
.
parameter_names
)
self
.
assert
Equal
(
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
.
assert
True
(
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
.
assert
AlmostEqual
(
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
)
test/core/likelihood_test.py
View file @
89151b51
from
__future__
import
absolute_import
# import bilby
import
unittest
from
mock
import
MagicMock
import
mock
...
...
test/core/prior/base_test.py
View file @
89151b51
...
...
@@ -77,29 +77,21 @@ class TestPriorInstantiationWithoutOptionalPriors(unittest.TestCase):
self
.
assertIsNone
(
self
.
prior
.
boundary
)
class
TestPrior
Name
(
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
Test
PriorBoundary
(
unittest
.
TestCase
):
class
Test
Constraint
(
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
)