Commit 0088caa2 authored by Gregory Ashton's avatar Gregory Ashton

Merge branch 'maite.mateu-lucena/bilby_pipe-imrphenomx-nestcheck' into 'master'

Add waveform-arguments-dict and numerical-relativity file

See merge request lscsoft/bilby_pipe!361
parents 854d22b6 1e2c6d35
Pipeline #157464 passed with stage
in 3 minutes and 9 seconds
......@@ -82,6 +82,8 @@ class DataAnalysisInput(Input):
self.pn_phase_order = args.pn_phase_order
self.pn_amplitude_order = args.pn_amplitude_order
self.mode_array = args.mode_array
self.waveform_arguments_dict = args.waveform_arguments_dict
self.numerical_relativity_file = args.numerical_relativity_file
self.frequency_domain_source_model = args.frequency_domain_source_model
self.likelihood_type = args.likelihood_type
self.reference_frame = args.reference_frame
......
......@@ -134,6 +134,8 @@ class DataGenerationInput(Input):
self.pn_phase_order = args.pn_phase_order
self.pn_amplitude_order = args.pn_amplitude_order
self.mode_array = args.mode_array
self.waveform_arguments_dict = args.waveform_arguments_dict
self.numerical_relativity_file = args.numerical_relativity_file
self.injection_waveform_approximant = args.injection_waveform_approximant
self.frequency_domain_source_model = args.frequency_domain_source_model
self.likelihood_type = args.likelihood_type
......
......@@ -24,6 +24,7 @@ from .utils import (
get_colored_string,
get_time_prior,
logger,
pretty_print_dictionary,
)
......@@ -350,7 +351,7 @@ class Input(object):
raise BilbyPipeError(f"mode_array {self._mode_array} is invalid")
def get_default_waveform_arguments(self):
return dict(
wfa = dict(
reference_frequency=self.reference_frequency,
waveform_approximant=self.waveform_approximant,
minimum_frequency=self.minimum_frequency,
......@@ -363,6 +364,12 @@ class Input(object):
mode_array=self.mode_array,
)
if self.waveform_arguments_dict is not None:
wfa.update(convert_string_to_dict(self.waveform_arguments_dict))
logger.debug(f"Default waveform_arguments: {pretty_print_dictionary(wfa)}")
return wfa
def get_injection_waveform_arguments(self):
"""Get the dict of the waveform arguments needed for creating injections.
......@@ -374,6 +381,7 @@ class Input(object):
self.injection_waveform_approximant = self.waveform_approximant
waveform_arguments = self.get_default_waveform_arguments()
waveform_arguments["waveform_approximant"] = self.injection_waveform_approximant
waveform_arguments["numerical_relativity_file"] = self.numerical_relativity_file
return waveform_arguments
@property
......@@ -1299,6 +1307,5 @@ class Input(object):
raise BilbyPipeError(
get_colored_string(f"Unable to parse prior, exception raised {e}")
)
prior_as_str = {key: str(val) for key, val in prior.items()}
pp = json.dumps(prior_as_str, indent=2)
pp = pretty_print_dictionary(prior)
logger.info(f"Input prior = {pp}")
......@@ -853,7 +853,24 @@ def create_parser(top_level=True):
help="Post-Newtonian order to use for the amplitude. Also "
"used to determine the waveform starting frequency.",
)
waveform_parser.add(
"--numerical-relativity-file",
default=None,
type=nonestr,
help=(
"Path to a h5 numerical relativity file to inject, see"
"https://git.ligo.org/waveforms/lvcnr-lfs for examples"
),
)
waveform_parser.add(
"--waveform-arguments-dict",
default=None,
type=nonestr,
help=(
"A dictionary of arbitrary additional waveform-arguments to pass"
" to the bilby waveform generator's `waveform_arguments`"
),
)
waveform_parser.add(
"--mode-array",
default=None,
......@@ -862,7 +879,6 @@ def create_parser(top_level=True):
help="Array of modes to use for the waveform. Should be "
"a list of lists, eg. [[2,2], [2,-2]]",
)
waveform_parser.add(
"--frequency-domain-source-model",
default="lal_binary_black_hole",
......
......@@ -2,6 +2,7 @@
A set of generic utilities used in bilby_pipe
"""
import ast
import json
import logging
import math
import os
......@@ -300,7 +301,7 @@ def setup_logger(outdir=None, label=None, log_level="INFO"):
in https://docs.python.org/2/library/logging.html#logging-levels
"""
if "-v" in sys.argv:
if "-v" in sys.argv or "--verbose" in sys.argv:
log_level = "DEBUG"
if isinstance(log_level, str):
......@@ -709,5 +710,23 @@ def check_if_represents_int(s):
return False
def pretty_print_dictionary(dictionary):
"""Convert an input dictionary to a pretty-printed string
Parameters
----------
dictionary: dict
Input dictionary
Returns
-------
pp: str
The dictionary pretty-printed as a string
"""
dict_as_str = {key: str(val) for key, val in dictionary.items()}
return json.dumps(dict_as_str, indent=2)
setup_logger()
logger = logging.getLogger("bilby_pipe")
......@@ -235,6 +235,7 @@ class TestInput(unittest.TestCase):
inputs.pn_phase_order = -1
inputs.pn_amplitude_order = 0
inputs.mode_array = None
inputs.waveform_arguments_dict = None
inputs.catch_waveform_errors = False
wfa = inputs.get_default_waveform_arguments()
self.assertEqual(wfa["reference_frequency"], 20)
......@@ -249,6 +250,39 @@ class TestInput(unittest.TestCase):
self.assertFalse(wfa["catch_waveform_errors"])
self.assertEqual(len(wfa), 10)
def test_added_waveform_arguments(self):
inputs = bilby_pipe.main.Input()
inputs.detectors = ["H1"]
inputs.reference_frequency = 20
inputs.minimum_frequency = 20
inputs.maximum_frequency = 1024
inputs.waveform_approximant = "IMRPhenomPv2"
inputs.pn_spin_order = -1
inputs.pn_tidal_order = -1
inputs.pn_phase_order = -1
inputs.pn_amplitude_order = 0
inputs.mode_array = None
inputs.waveform_arguments_dict = "{a: 10, b=test, c=[1, 2]}"
inputs.catch_waveform_errors = False
wfa = inputs.get_default_waveform_arguments()
self.assertEqual(wfa["reference_frequency"], 20)
self.assertEqual(wfa["minimum_frequency"], 20)
self.assertEqual(wfa["maximum_frequency"], 1024)
self.assertEqual(wfa["waveform_approximant"], "IMRPhenomPv2")
self.assertEqual(wfa["pn_spin_order"], -1)
self.assertEqual(wfa["pn_tidal_order"], -1)
self.assertEqual(wfa["pn_phase_order"], -1)
self.assertEqual(wfa["pn_amplitude_order"], 0)
self.assertIsNone(wfa["mode_array"])
self.assertFalse(wfa["catch_waveform_errors"])
# Check of added arguments
self.assertEqual(wfa["a"], 10)
self.assertEqual(wfa["b"], "test")
self.assertEqual(wfa["c"], ["1", "2"])
self.assertEqual(len(wfa), 13)
def test_mode_array(self):
inputs = bilby_pipe.main.Input()
inputs.detectors = ["H1"]
......@@ -261,6 +295,14 @@ class TestInput(unittest.TestCase):
inputs.pn_tidal_order = -1
inputs.pn_phase_order = -1
inputs.pn_amplitude_order = 0
inputs.phenomXPHMTwistPhenomHM = None
inputs.phenomXPFinalSpinMod = None
inputs.phenomXPConvention = None
inputs.phenomXPrecVersion = None
inputs.phenomXPHMMband = None
inputs.phenomXHMMband = None
inputs.numerical_relativity_file = None
inputs.waveform_arguments_dict = None
inputs.mode_array = "[[2, 2], [2, -2]]"
wfa = inputs.get_default_waveform_arguments()
......@@ -287,8 +329,10 @@ class TestInput(unittest.TestCase):
inputs.pn_tidal_order = -1
inputs.pn_phase_order = -1
inputs.pn_amplitude_order = 0
inputs.numerical_relativity_file = None
inputs.catch_waveform_errors = False
inputs.mode_array = None
inputs.waveform_arguments_dict = None
# injection-waveform-approx not provided
inputs.waveform_approximant = "IMRPhenomPv2"
......@@ -303,7 +347,8 @@ class TestInput(unittest.TestCase):
self.assertEqual(wfa["pn_phase_order"], -1)
self.assertEqual(wfa["pn_amplitude_order"], 0)
self.assertIsNone(wfa["mode_array"])
self.assertEqual(len(wfa), 10)
self.assertIsNone(wfa["numerical_relativity_file"])
self.assertEqual(len(wfa), 11)
# injection-waveform-approx provided
inputs.waveform_approximant = "IMRPhenomPv2"
......@@ -312,7 +357,35 @@ class TestInput(unittest.TestCase):
self.assertEqual(wfa["reference_frequency"], 20)
self.assertEqual(wfa["minimum_frequency"], 20)
self.assertEqual(wfa["waveform_approximant"], "SEOBNRv4")
self.assertEqual(len(wfa), 10)
self.assertEqual(len(wfa), 11)
def test_numerical_relativity_file(self):
inputs = bilby_pipe.main.Input()
inputs.detectors = ["H1"]
inputs.reference_frequency = 20
inputs.minimum_frequency = 20
inputs.maximum_frequency = 1024
inputs.pn_spin_order = -1
inputs.pn_tidal_order = -1
inputs.pn_phase_order = -1
inputs.pn_amplitude_order = 0
inputs.phenomXPHMTwistPhenomHM = None
inputs.phenomXPFinalSpinMod = None
inputs.phenomXPConvention = None
inputs.phenomXPrecVersion = None
inputs.phenomXPHMMband = None
inputs.phenomXHMMband = None
inputs.catch_waveform_errors = False
inputs.mode_array = None
inputs.waveform_approximant = "IMRPhenomPv2"
inputs.injection_waveform_approximant = None
inputs.waveform_arguments_dict = None
# numerical-relativity-file given
filename = "somedir/file.h5"
inputs.numerical_relativity_file = filename
wfa = inputs.get_injection_waveform_arguments()
self.assertEqual(wfa["numerical_relativity_file"], filename)
def test_injection_number(self):
inputs = bilby_pipe.main.Input()
......
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