Commit 469e377d authored by Gregory Ashton's avatar Gregory Ashton
Browse files

Add a new test which runs through the examples

- Adds a tests flag for which the sampler only runs 100 steps
- Set the samples as random samples to test plotting
- Fix problems in a few of the examples
parent a8081f1d
Pipeline #19735 failed with stages
in 5 minutes and 38 seconds
......@@ -26,6 +26,7 @@ exitcode-jessie:
- coverage run --include=tupak/* -a test/tests.py
- coverage run --include=tupak/* -a test/waveform_generator_tests.py
- coverage run --include=tupak/* -a test/noise_realisation_tests.py
- coverage run --include=tupak/* -a test/example_tests.py
- coverage html
- coverage-badge -o coverage_badge.svg -f
artifacts:
......
......@@ -64,7 +64,7 @@ prior['phase'] = tupak.prior.Uniform(-np.pi/2, np.pi/2, r'$\phi$')
# define likelihood
likelihood = tupak.likelihood.Likelihood(IFOs, waveform)
likelihood = tupak.likelihood.GravitationalWaveTransient(IFOs, waveform)
# launch sampler
result = tupak.sampler.run_sampler(likelihood, prior, sampler='dynesty', npoints=1000,
......
......@@ -20,7 +20,7 @@ outdir = 'outdir'
# use of the `tupak` waveform_generator to make the signal (more on this later)
# But, one could make this work without the waveform generator.
class GaussianLikelihood():
class GaussianLikelihood(tupak.likelihood.Likelihood):
def __init__(self, x, y, waveform_generator):
self.x = x
self.y = y
......
import unittest
import os
import shutil
from context import tupak
import logging
import glob
import subprocess
from past.builtins import execfile
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 all examples to check they run """
examples = glob.glob("examples/*/*.py")
examples = ['examples/injection_examples/how_to_specify_the_prior.py',
'examples/injection_examples/change_sampled_parameters.py',
'examples/injection_examples/marginalized_likelihood.py',
'examples/injection_examples/create_your_own_source_model.py',
'examples/injection_examples/create_your_own_time_domain_source_model.py',
'examples/other_examples/alternative_likelihoods.py',
'examples/open_data_examples/GW150914.py',
'examples/open_data_examples/GW150914_minimal.py',
]
for filename in examples:
print("Testing {}".format(filename))
out = subprocess.check_output(["python", filename, "--test"])
print(out)
if __name__ == '__main__':
unittest.main()
......@@ -165,7 +165,7 @@ class Result(dict):
if save:
kwargs['filename'] = '{}/{}_walks.png'.format(self.outdir, self.label)
logging.info('Saving walker plot to {}'.format(kwargs['filename']))
if self.injection_parameters is not None:
if getattr(self, 'injection_parameters', None) is not None:
kwargs['truth'] = [self.injection_parameters[key] for key in self.search_parameter_keys]
c = ChainConsumer()
c.add_chain(self.samples, parameters=self.parameter_labels)
......@@ -190,7 +190,7 @@ class Result(dict):
if save:
kwargs['filename'] = '{}/{}_distributions.png'.format(self.outdir, self.label)
logging.info('Saving distributions plot to {}'.format(kwargs['filename']))
if self.injection_parameters is not None:
if getattr(self, 'injection_parameters', None) is not None:
kwargs['truth'] = [self.injection_parameters[key] for key in self.search_parameter_keys]
c = ChainConsumer()
c.add_chain(self.samples, parameters=self.parameter_labels)
......
......@@ -190,6 +190,9 @@ class Sampler(object):
def run_sampler(self):
pass
def _run_test(self):
raise ValueError("Method not yet implemented")
def check_cached_result(self):
""" Check if the cached data file exists and can be used """
......@@ -263,6 +266,18 @@ class Nestle(Sampler):
self.result.logzerr = out.logzerr
return self.result
def _run_test(self):
nestle = self.external_sampler
self.external_sampler_function = nestle.sample
self.external_sampler_function(
loglikelihood=self.log_likelihood,
prior_transform=self.prior_transform,
ndim=self.ndim, maxiter=10, **self.kwargs)
self.result.samples = np.random.uniform(0, 1, (100, self.ndim))
self.result.logz = np.nan
self.result.logzerr = np.nan
return self.result
class Dynesty(Sampler):
......@@ -312,6 +327,22 @@ class Dynesty(Sampler):
self.result.logzerr = out.logzerr[-1]
return self.result
def _run_test(self):
dynesty = self.external_sampler
nested_sampler = dynesty.NestedSampler(
loglikelihood=self.log_likelihood,
prior_transform=self.prior_transform,
ndim=self.ndim, **self.kwargs)
nested_sampler.run_nested(
dlogz=self.kwargs['dlogz'],
print_progress=self.kwargs['verbose'],
maxiter=10)
self.result.samples = np.random.uniform(0, 1, (100, self.ndim))
self.result.logz = np.nan
self.result.logzerr = np.nan
return self.result
class Pymultinest(Sampler):
......@@ -458,7 +489,11 @@ def run_sampler(likelihood, priors=None, label='label', outdir='outdir',
logging.info("Using cached result")
return sampler.cached_result
result = sampler.run_sampler()
if utils.command_line_args.test:
result = sampler._run_test()
else:
result = sampler.run_sampler()
result.noise_logz = likelihood.noise_log_likelihood()
if use_ratio:
result.log_bayes_factor = result.logz
......
......@@ -530,6 +530,9 @@ def set_up_command_line_arguments():
default=['H1', 'L1', 'V1'],
help=("List of detectors to use in open data calls, "
"e.g. -d H1 L1 for H1 and L1"))
parser.add_argument("-t", "--test", action="store_true",
help=("Used for testing only: don't run full PE, but"
" just check nothing breaks"))
args, _ = parser.parse_known_args()
if args.quite:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment