Skip to content

Commit

Permalink
Add unit test for likelihood evaluator (gwastro#1077)
Browse files Browse the repository at this point in the history
* Add unittest

* Add test to travis script.

* Do not use ROM in test.

* Fix approximant.
  • Loading branch information
cmbiwer authored and ahnitz committed Sep 14, 2016
1 parent c2493e4 commit a4055d0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
78 changes: 78 additions & 0 deletions test/test_inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright (C) 2016 Christopher M. Biwer
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

#
# =============================================================================
#
# Preamble
#
# =============================================================================
#
"""
These are the unittests for the pycbc.inference subpackage
"""
import sys
import pycbc
import unittest
import numpy
from pycbc import inference, waveform
from pycbc import psd as pypsd
from utils import parse_args_cpu_only, simple_exit

# tests only need to happen on the CPU
parse_args_cpu_only("Inference")

class TestInference(unittest.TestCase):
def setUp(self, *args):
numpy.random.seed(1024)

def test_likelihood_evaluator_init(self):

# data args
seglen = 4
sample_rate = 2048
N = seglen * sample_rate/2 + 1
fmin = 30.

# setup waveform generator and signal parameters
m1, m2, s1z, s2z, tsig = 38.6, 29.3, 0., 0., 3.1
ra, dec, pol, dist = 1.37, -1.26, 2.76, 3*500.
generator = waveform.FDomainDetFrameGenerator(
waveform.FDomainCBCGenerator, 0., variable_args=["tc"],
detectors=["H1", "L1"], delta_f=1./seglen,
f_lower=fmin, approximant="TaylorF2",
mass1=m1, mass2=m2, spin1z=s1z, spin2z=s2z, ra=ra,
dec=dec, polarization=pol, distance=dist)
signal = generator.generate(tsig)

# get PSDs
psd = pypsd.aLIGOZeroDetHighPower(N, 1./seglen, 20.)
psds = {"H1": psd, "L1": psd}

# get a prior evaluator
uniform_prior = inference.distributions.Uniform(tc=(tsig-0.2,tsig+0.2))
prior_eval = inference.prior.PriorEvaluator(["tc"], uniform_prior)

# setup likelihood evaluator
likelihood_eval = inference.GaussianLikelihood(generator, signal, fmin,
psds=psds, prior=prior_eval, return_meta=False)

suite = unittest.TestSuite()
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestInference))

if __name__ == "__main__":
results = unittest.TextTestRunner(verbosity=2).run(suite)
simple_exit(results)
3 changes: 3 additions & 0 deletions tools/run_travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ test $? -ne 0 && RESULT=1
python test/test_spatmplt.py
test $? -ne 0 && RESULT=1

python test/test_inference.py
test $? -ne 0 && RESULT=1

# check for trivial failures of important executables

function test_exec_help {
Expand Down

0 comments on commit a4055d0

Please sign in to comment.