From 9439e3fa3cae7185e1bd9426a41eceb57dcc67dd Mon Sep 17 00:00:00 2001 From: mj-will Date: Wed, 15 May 2024 08:55:07 +0100 Subject: [PATCH 1/3] add value error for invalid approximant --- BBHX_Phenom.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/BBHX_Phenom.py b/BBHX_Phenom.py index b8eeaf2..f338375 100644 --- a/BBHX_Phenom.py +++ b/BBHX_Phenom.py @@ -55,13 +55,14 @@ def imr_duration(**params): nparams = {'mass1':params['mass1'], 'mass2':params['mass2'], 'spin1z':params['spin1z'], 'spin2z':params['spin2z'], 'f_lower':params['f_lower']} - if params['approximant'] == 'BBHX_PhenomD': from pycbc.waveform.waveform import imrphenomd_length_in_time time_length = np.float64(imrphenomd_length_in_time(**nparams)) elif params['approximant'] == 'BBHX_PhenomHM': from pycbc.waveform.waveform import imrphenomhm_length_in_time time_length = np.float64(imrphenomhm_length_in_time(**nparams)) + else: + raise ValueError(f"Invalid approximant: {params['approximant']}") if time_length < 2678400: warnings.warn("Waveform duration is too short! Setting it to 1 month (2678400 s).") From 85a8096ab38459aa714d2bdeaa6a1517c7e4fc6a Mon Sep 17 00:00:00 2001 From: mj-will Date: Wed, 15 May 2024 08:55:16 +0100 Subject: [PATCH 2/3] add test for get filter length --- tests.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests.py b/tests.py index 138c6cc..98846b0 100644 --- a/tests.py +++ b/tests.py @@ -1,5 +1,9 @@ import numpy as np -from pycbc.waveform import get_fd_det_waveform, get_fd_det_waveform_sequence +from pycbc.waveform import ( + get_fd_det_waveform, + get_fd_det_waveform_sequence, + get_waveform_filter_length_in_time, +) import pytest @@ -72,3 +76,10 @@ def test_phenomhm_mode_array(params, mode_array): params["mode_array"] = mode_array wf = get_fd_det_waveform(**params) assert len(wf) == 3 + + +def test_length_in_time(params, approximant): + params["approximant"] = approximant + # print(params) + duration = get_waveform_filter_length_in_time(**params) + assert duration > 0 From eb75cd5cb46dbcd5779a32ee03a0bc9ecfcc7d3f Mon Sep 17 00:00:00 2001 From: mj-will Date: Wed, 15 May 2024 10:17:40 +0100 Subject: [PATCH 3/3] fix length in time calculation --- BBHX_Phenom.py | 67 +++++++++++++++++++++++++++++++------------------- setup.py | 4 +-- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/BBHX_Phenom.py b/BBHX_Phenom.py index f338375..9a842dc 100644 --- a/BBHX_Phenom.py +++ b/BBHX_Phenom.py @@ -46,31 +46,48 @@ def chirptime(m1, m2, f_lower, m_mode=None): return duration -def imr_duration(**params): - # More accurate duration (within LISA frequency band) of the waveform, - # including merge, ringdown, and aligned spin effects. - # This is used in the time-domain signal injection in PyCBC. - import warnings - - nparams = {'mass1':params['mass1'], 'mass2':params['mass2'], - 'spin1z':params['spin1z'], 'spin2z':params['spin2z'], - 'f_lower':params['f_lower']} - if params['approximant'] == 'BBHX_PhenomD': - from pycbc.waveform.waveform import imrphenomd_length_in_time - time_length = np.float64(imrphenomd_length_in_time(**nparams)) - elif params['approximant'] == 'BBHX_PhenomHM': - from pycbc.waveform.waveform import imrphenomhm_length_in_time - time_length = np.float64(imrphenomhm_length_in_time(**nparams)) - else: - raise ValueError(f"Invalid approximant: {params['approximant']}") - - if time_length < 2678400: - warnings.warn("Waveform duration is too short! Setting it to 1 month (2678400 s).") - time_length = 2678400 - if time_length >= params['t_obs_start']: - warnings.warn("Waveform duration is longer than data length! Setting it to `t_obs_start`.") - time_length = params['t_obs_start'] - return time_length * 1.1 +def validate_length_in_time(length_in_time, t_obs_start): + """Validate the estimate length in time compared to t_obs_start. + + Parameters + ---------- + length_in_time : float + Estimated length in time. + t_obs_start + Observation start time. + + Returns + ------- + float + Valid length in time. + """ + if length_in_time < 2678400: + warn("Waveform duration is too short! Setting it to 1 month (2678400 s).") + length_in_time = 2678400 + if length_in_time >= t_obs_start: + warn("Waveform duration is longer than data length! Setting it to `t_obs_start`.") + length_in_time = t_obs_start + return length_in_time * 1.1 + + +def bbhx_phenomd_length_in_time(**params): + """Compute the length in for BBHx PhenomD""" + from pycbc.waveform.waveform import get_imr_length + + time_length = np.float64(get_imr_length("IMRPhenomD", **params)) + return validate_length_in_time(time_length, params["t_obs_start"]) + + +def bbhx_phenomhm_length_in_time(**params): + """Compute the length in for BBHx PhenomHM""" + from pycbc.waveform.waveform import get_hm_length_in_time + + # Max m mode is 4 for BBHx PhenomHM, use if the `mode_array` is not + # specified. + length_in_time = np.float64( + get_hm_length_in_time("IMRPhenomD", 4, **params) + ) + return validate_length_in_time(length_in_time, params["t_obs_start"]) def interpolated_tf(m1, m2, m_mode=None, num_interp=100, f_lower=1e-4): diff --git a/setup.py b/setup.py index 2db814a..fbeb388 100644 --- a/setup.py +++ b/setup.py @@ -29,8 +29,8 @@ "BBHX_PhenomHM=BBHX_Phenom:waveform_setup", ], "pycbc.waveform.length": [ - "BBHX_PhenomD=BBHX_Phenom:imr_duration", - "BBHX_PhenomHM=BBHX_Phenom:imr_duration", + "BBHX_PhenomD=BBHX_Phenom:bbhx_phenomd_length_in_time", + "BBHX_PhenomHM=BBHX_Phenom:bbhx_phenomhm_length_in_time", ] },