Skip to content
Snippets Groups Projects
Commit 0933eed0 authored by Moritz Huebner's avatar Moritz Huebner
Browse files

Fixed some of the issues that came up during restructuring

parent 4fe5c600
No related branches found
No related tags found
1 merge request!55Project restructuring
Pipeline #
......@@ -101,24 +101,24 @@ class TestDetector(unittest.TestCase):
def test_vertex_without_update(self):
_ = self.ifo.vertex
with mock.patch('tupak.utils.get_vertex_position_geocentric') as m:
with mock.patch('tupak.gw.utils.get_vertex_position_geocentric') as m:
m.return_value = np.array([1])
self.assertFalse(np.array_equal(self.ifo.vertex, np.array([1])))
def test_vertex_with_latitude_update(self):
with mock.patch('tupak.utils.get_vertex_position_geocentric') as m:
with mock.patch('tupak.gw.utils.get_vertex_position_geocentric') as m:
m.return_value = np.array([1])
self.ifo.latitude = 5
self.assertEqual(self.ifo.vertex, np.array([1]))
def test_vertex_with_longitude_update(self):
with mock.patch('tupak.utils.get_vertex_position_geocentric') as m:
with mock.patch('tupak.gw.utils.get_vertex_position_geocentric') as m:
m.return_value = np.array([1])
self.ifo.longitude = 5
self.assertEqual(self.ifo.vertex, np.array([1]))
def test_vertex_with_elevation_update(self):
with mock.patch('tupak.utils.get_vertex_position_geocentric') as m:
with mock.patch('tupak.gw.utils.get_vertex_position_geocentric') as m:
m.return_value = np.array([1])
self.ifo.elevation = 5
self.assertEqual(self.ifo.vertex, np.array([1]))
......@@ -212,14 +212,14 @@ class TestDetector(unittest.TestCase):
self.assertTrue(np.array_equal(self.ifo.power_spectral_density_array, np.array([1, 4])))
def test_antenna_response_default(self):
with mock.patch('tupak.utils.get_polarization_tensor') as m:
with mock.patch('tupak.gw.utils.get_polarization_tensor') as m:
with mock.patch('numpy.einsum') as n:
m.return_value = 0
n.return_value = 1
self.assertEqual(self.ifo.antenna_response(234, 52, 54, 76, 'plus'), 1)
def test_antenna_response_einsum(self):
with mock.patch('tupak.utils.get_polarization_tensor') as m:
with mock.patch('tupak.gw.utils.get_polarization_tensor') as m:
m.return_value = np.ones((3, 3))
self.assertAlmostEqual(self.ifo.antenna_response(234, 52, 54, 76, 'plus'), self.ifo.detector_tensor.sum())
......@@ -272,8 +272,8 @@ class TestDetector(unittest.TestCase):
def test_inject_signal_sets_data_with_existing_data_array(self):
self.ifo.get_detector_response = MagicMock(return_value=np.array([1]))
self.ifo.frequency_array = np.array([0, 1])
with mock.patch('tupak.utils.optimal_snr_squared') as m:
with mock.patch('tupak.utils.matched_filter_snr_squared') as n:
with mock.patch('tupak.gw.utils.optimal_snr_squared') as m:
with mock.patch('tupak.gw.utils.matched_filter_snr_squared') as n:
m.return_value = 0
n.return_value = 0
self.ifo.data = np.array([1])
......@@ -283,8 +283,8 @@ class TestDetector(unittest.TestCase):
def test_inject_signal_sets_data_without_data_array(self):
self.ifo.get_detector_response = MagicMock(return_value=np.array([1]))
self.ifo.frequency_array = np.array([0, 1])
with mock.patch('tupak.utils.optimal_snr_squared') as m:
with mock.patch('tupak.utils.matched_filter_snr_squared') as n:
with mock.patch('tupak.gw.utils.optimal_snr_squared') as m:
with mock.patch('tupak.gw.utils.matched_filter_snr_squared') as n:
m.return_value = 0
n.return_value = 0
self.ifo.data = 1
......@@ -321,7 +321,7 @@ class TestDetector(unittest.TestCase):
self.ifo.set_data(sampling_frequency=1, duration=1)
def test_set_data_sets_data_from_frequency_domain_strain(self):
with mock.patch('tupak.utils.create_frequency_series') as m:
with mock.patch('tupak.core.utils.create_frequency_series') as m:
m.return_value = np.array([1])
self.ifo.minimum_frequency = 0
self.ifo.maximum_frequency = 3
......@@ -330,7 +330,7 @@ class TestDetector(unittest.TestCase):
self.assertTrue(np.array_equal(self.ifo.data, np.array([1])))
def test_set_data_sets_frequencies_from_frequency_domain_strain(self):
with mock.patch('tupak.utils.create_frequency_series') as m:
with mock.patch('tupak.core.utils.create_frequency_series') as m:
m.return_value = np.array([1])
self.ifo.minimum_frequency = 0
self.ifo.maximum_frequency = 3
......@@ -339,7 +339,7 @@ class TestDetector(unittest.TestCase):
self.assertTrue(np.array_equal(self.ifo.frequency_array, np.array([1])))
def test_set_data_sets_frequencies_from_spectral_density(self):
with mock.patch('tupak.utils.create_frequency_series') as m:
with mock.patch('tupak.core.utils.create_frequency_series') as m:
m.return_value = np.array([1])
self.ifo.minimum_frequency = 0
self.ifo.maximum_frequency = 3
......@@ -357,7 +357,7 @@ class TestDetector(unittest.TestCase):
self.assertEqual(self.ifo.epoch, 4)
def test_set_data_sets_sampling_frequency(self):
with mock.patch('tupak.utils.create_frequency_series') as m:
with mock.patch('tupak.core.utils.create_frequency_series') as m:
m.return_value = np.array([1])
self.ifo.minimum_frequency = 0
self.ifo.maximum_frequency = 3
......@@ -366,7 +366,7 @@ class TestDetector(unittest.TestCase):
self.assertEqual(self.ifo.sampling_frequency, 1)
def test_set_data_sets_duration(self):
with mock.patch('tupak.utils.create_frequency_series') as m:
with mock.patch('tupak.core.utils.create_frequency_series') as m:
m.return_value = np.array([1])
self.ifo.minimum_frequency = 0
self.ifo.maximum_frequency = 3
......@@ -375,12 +375,12 @@ class TestDetector(unittest.TestCase):
self.assertEqual(self.ifo.duration, 1)
def test_time_delay_from_geocenter(self):
with mock.patch('tupak.utils.time_delay_geocentric') as m:
with mock.patch('tupak.gw.utils.time_delay_geocentric') as m:
m.return_value = 1
self.assertEqual(self.ifo.time_delay_from_geocenter(1, 2, 3), 1)
def test_vertex_position_geocentric(self):
with mock.patch('tupak.utils.get_vertex_position_geocentric') as m:
with mock.patch('tupak.gw.utils.get_vertex_position_geocentric') as m:
m.return_value = 1
self.assertEqual(self.ifo.vertex_position_geocentric(), 1)
......
......@@ -38,11 +38,11 @@ class Test(unittest.TestCase):
def test_examples(self):
""" Loop over examples to check they run """
examples = ['examples/injection_examples/basic_tutorial.py',
'examples/injection_examples/change_sampled_parameters.py',
'examples/injection_examples/marginalized_likelihood.py',
'examples/injection_examples/create_your_own_time_domain_source_model.py',
'examples/other_examples/linear_regression.py',
examples = ['../examples/injection_examples/basic_tutorial.py',
'../examples/injection_examples/change_sampled_parameters.py',
'../examples/injection_examples/marginalized_likelihood.py',
'../examples/injection_examples/create_your_own_time_domain_source_model.py',
'../examples/other_examples/linear_regression.py',
]
for filename in examples:
print("Testing {}".format(filename))
......
......@@ -38,8 +38,8 @@ class TestNoiseRealisation(unittest.TestCase):
interferometer.set_data(sampling_frequency, time_duration, from_power_spectral_density=True)
hf_tmp = interferometer.data
psd = interferometer.power_spectral_density
snr[x] = tupak.core.utils.inner_product(hf_tmp, muf, frequency_array, psd) \
/ np.sqrt(tupak.core.utils.inner_product(muf, muf, frequency_array, psd))
snr[x] = tupak.gw.utils.inner_product(hf_tmp, muf, frequency_array, psd) \
/ np.sqrt(tupak.gw.utils.inner_product(muf, muf, frequency_array, psd))
self.assertTrue(np.isclose(np.std(snr), 1.00, atol=1e-1))
......
......@@ -26,11 +26,11 @@ class TestPriorInstantiationWithoutOptionalPriors(unittest.TestCase):
self.assertIsInstance(self.prior, tupak.core.prior.Prior)
def test_magic_call_is_the_same_as_sampling(self):
self.prior = Mock(return_value=0.5)
self.assertEqual(self.prior.sample(), self.p())
self.prior.sample = Mock(return_value=0.5)
self.assertEqual(self.prior.sample(), self.prior())
def test_base_rescale_method(self):
self.assertIsNone(self.prior.rescale())
self.assertIsNone(self.prior.rescale(1))
def test_base_repr(self):
self.prior = tupak.core.prior.Prior(name='test_name', latex_label='test_label', minimum=0, maximum=1)
......
......@@ -48,7 +48,7 @@ class Test(unittest.TestCase):
self.assertAlmostEqual(all(self.msd['hf_signal_and_noise'] - hf_signal_and_noise_saved), 0.00000000, 5)
def test_recover_luminosity_distance(self):
likelihood = tupak.core.likelihood.GravitationalWaveTransient(
likelihood = tupak.gw.likelihood.GravitationalWaveTransient(
[self.msd['IFO']], self.msd['waveform_generator'])
priors = {}
......@@ -59,7 +59,6 @@ class Test(unittest.TestCase):
priors['luminosity_distance'] = tupak.core.prior.Uniform(
name='luminosity_distance', minimum=dL - 10, maximum=dL + 10)
result = tupak.core.sampler.run_sampler(
likelihood, priors, sampler='nestle', verbose=False, npoints=100)
self.assertAlmostEqual(np.mean(result.samples), dL,
......
......@@ -6,10 +6,10 @@ from scipy.special import logsumexp
from scipy.special._ufuncs import i0e
import tupak
from tupak import Likelihood
from tupak.core import likelihood as likelihood
class GravitationalWaveTransient(Likelihood):
class GravitationalWaveTransient(likelihood.Likelihood):
""" A gravitational-wave transient likelihood object
This is the usual likelihood object to use for transient gravitational
......@@ -44,7 +44,7 @@ class GravitationalWaveTransient(Likelihood):
def __init__(self, interferometers, waveform_generator, time_marginalization=False, distance_marginalization=False,
phase_marginalization=False, prior=None):
Likelihood.__init__(self, waveform_generator.parameters)
likelihood.Likelihood.__init__(self, waveform_generator.parameters)
self.interferometers = interferometers
self.waveform_generator = waveform_generator
self.non_standard_sampling_parameter_keys = self.waveform_generator.non_standard_sampling_parameter_keys
......@@ -79,9 +79,9 @@ class GravitationalWaveTransient(Likelihood):
def noise_log_likelihood(self):
log_l = 0
for interferometer in self.interferometers:
log_l -= tupak.core.utils.noise_weighted_inner_product(interferometer.data, interferometer.data,
interferometer.power_spectral_density_array,
self.waveform_generator.time_duration) / 2
log_l -= tupak.gw.utils.noise_weighted_inner_product(interferometer.data, interferometer.data,
interferometer.power_spectral_density_array,
self.waveform_generator.time_duration) / 2
return log_l.real
def log_likelihood_ratio(self):
......@@ -96,10 +96,10 @@ class GravitationalWaveTransient(Likelihood):
for interferometer in self.interferometers:
signal_ifo = interferometer.get_detector_response(waveform_polarizations,
self.waveform_generator.parameters)
matched_filter_snr_squared += tupak.core.utils.matched_filter_snr_squared(
matched_filter_snr_squared += tupak.gw.utils.matched_filter_snr_squared(
signal_ifo, interferometer, self.waveform_generator.time_duration)
optimal_snr_squared += tupak.core.utils.optimal_snr_squared(
optimal_snr_squared += tupak.gw.utils.optimal_snr_squared(
signal_ifo, interferometer, self.waveform_generator.time_duration)
if self.time_marginalization:
interferometer.time_marginalization = self.time_marginalization
......@@ -133,7 +133,7 @@ class GravitationalWaveTransient(Likelihood):
log_l = logsumexp(dist_marged_log_l_tc_array, axis=0, b=delta_tc)
elif self.phase_marginalization:
log_l = logsumexp(self.bessel_function_interped(abs(matched_filter_snr_squared_tc_array)), b=delta_tc)\
log_l = logsumexp(self.bessel_function_interped(abs(matched_filter_snr_squared_tc_array)), b=delta_tc) \
- optimal_snr_squared / 2. - tc_log_norm
else:
......@@ -220,7 +220,7 @@ class GravitationalWaveTransient(Likelihood):
bounds_error=False, fill_value=-np.inf)
class BasicGravitationalWaveTransient(Likelihood):
class BasicGravitationalWaveTransient(likelihood.Likelihood):
""" A basic gravitational wave transient likelihood
The simplest frequency-domain gravitational wave transient likelihood. Does
......@@ -237,14 +237,14 @@ class BasicGravitationalWaveTransient(Likelihood):
Returns
-------
Likelihood: `tupak.likelihood.Likelihood`
Likelihood: `tupak.gw.likelihood.BasicGravitationalWaveTransient`
A likelihood object, able to compute the likelihood of the data given
some model parameters
"""
def __init__(self, interferometers, waveform_generator):
Likelihood.__init__(self, waveform_generator.parameters)
likelihood.Likelihood.__init__(self, waveform_generator.parameters)
self.interferometers = interferometers
self.waveform_generator = waveform_generator
......@@ -301,7 +301,7 @@ def get_binary_black_hole_likelihood(interferometers):
return likelihood
class HyperparameterLikelihood(Likelihood):
class HyperparameterLikelihood(likelihood.Likelihood):
""" A likelihood for infering hyperparameter posterior distributions
See Eq. (1) of https://arxiv.org/abs/1801.02699 for a definition.
......@@ -323,7 +323,7 @@ class HyperparameterLikelihood(Likelihood):
"""
def __init__(self, samples, hyper_prior, run_prior):
Likelihood.__init__(self, parameters=hyper_prior.__dict__)
likelihood.Likelihood.__init__(self, parameters=hyper_prior.__dict__)
self.samples = samples
self.hyper_prior = hyper_prior
self.run_prior = run_prior
......@@ -349,4 +349,4 @@ class HyperparameterLikelihood(Likelihood):
L.append(
np.sum(self.hyper_prior.prob(samp) /
self.run_prior.prob(samp)))
return np.sum(np.log(L))
\ No newline at end of file
return np.sum(np.log(L))
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