Skip to content

Commit

Permalink
[pycbc_live] Simplify and fix how CLI options are passed to the SNR o…
Browse files Browse the repository at this point in the history
…ptimizer (gwastro#4628)

* adding non-optimizer specific options to args_to_string

* correct formatting

* simplifying if snr opt seed

* Adding extra-opts arg

* updating options in live example run.sh

* restoring deleted space

* removing redundant default

* moving all snr optimizer options to snr_opt_extra_opts

* updating argument help descriptions

* removing snr_opt options from pycbc live

* removing seed option from example

* removing args_to_string

* Actually, even simpler

---------

Co-authored-by: Tito Dal Canton <tito.dalcanton@ijclab.in2p3.fr>
  • Loading branch information
2 people authored and bhooshan-gadre committed Mar 4, 2024
1 parent f7642fe commit c701861
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 41 deletions.
12 changes: 6 additions & 6 deletions bin/pycbc_live
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class LiveEventManager(object):
self.enable_gracedb_upload = args.enable_gracedb_upload
self.run_snr_optimization = args.run_snr_optimization
self.snr_opt_label = args.snr_opt_label
self.snr_opt_options = args.snr_opt_extra_opts
self.gracedb = None

# Keep track of which events have been uploaded
Expand All @@ -121,7 +122,6 @@ class LiveEventManager(object):
if platform.system() != 'Darwin':
available_cores = len(os.sched_getaffinity(0))
self.fu_cores = available_cores - analysis_cores
self.optimizer = args.snr_opt_method
if self.fu_cores <= 0:
logging.warning(
'Insufficient number of CPU cores (%d) to '
Expand All @@ -133,10 +133,6 @@ class LiveEventManager(object):
else:
# To enable mac testing, this is just set to 1
self.fu_cores = 1
# Convert SNR optimizer options into a string
self.snr_opt_options = snr_optimizer.args_to_string(args)
else:
self.snr_opt_options = None

if args.enable_embright_has_massgap:
if args.embright_massgap_max < self.mc_area_args['mass_bdary']['ns_max']:
Expand Down Expand Up @@ -987,6 +983,11 @@ parser.add_argument('--snr-opt-timeout', type=int, default=400, metavar='SECONDS
help='Maximum allowed duration of followup process to maximize SNR')
parser.add_argument('--snr-opt-label', default='SNR_OPTIMIZED',
help='Label to apply to snr-optimized GraceDB uploads')
parser.add_argument('--snr-opt-extra-opts',
help='Extra options to pass to the optimizer subprocess. Example: '
'--snr-opt-extra-opts "--snr-opt-method differential_evolution '
'--snr-opt-di-maxiter 50 --snr-opt-di-popsize 100 '
'--snr-opt-seed 42 --snr-opt-include-candidate "')

parser.add_argument('--enable-embright-has-massgap', action='store_true', default=False,
help='Estimate HasMassGap probability for EMBright info. Lower limit '
Expand All @@ -1010,7 +1011,6 @@ Coincer.insert_args(parser)
SingleDetSGChisq.insert_option_group(parser)
mchirp_area.insert_args(parser)
livepau.insert_live_pastro_option_group(parser)
snr_optimizer.insert_snr_optimizer_options(parser)

args = parser.parse_args()

Expand Down
22 changes: 13 additions & 9 deletions examples/live/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,11 @@ python -m mpi4py `which pycbc_live` \
--src-class-eff-to-lum-distance 0.74899 \
--src-class-lum-distance-to-delta -0.51557 -0.32195 \
--run-snr-optimization \
--snr-opt-di-maxiter 50 \
--snr-opt-di-popsize 100 \
--snr-opt-include-candidate \
--snr-opt-seed 42 \
--snr-opt-extra-opts \
"--snr-opt-method differential_evolution \
--snr-opt-di-maxiter 50 \
--snr-opt-di-popsize 100 \
--snr-opt-include-candidate " \
--sngl-ifar-est-dist conservative \
--single-newsnr-threshold 9 \
--single-duration-threshold 7 \
Expand All @@ -210,11 +211,14 @@ python -m mpi4py `which pycbc_live` \
# If you would like to use the pso optimizer, change --optimizer to pso
# and include these arguments while removing other optimizer args.
# You will need to install the pyswarms package into your environment.
# --snr-opt-pso-iters 5 \
# --snr-opt-pso-particles 250 \
# --snr-opt-pso-c1 0.5 \
# --snr-opt-pso-c2 2.0 \
# --snr-opt-pso-w 0.01 \
# --snr-opt-extra-opts \
# "--snr-opt-method pso \
# --snr-opt-pso-iters 5 \
# --snr-opt-pso-particles 250 \
# --snr-opt-pso-c1 0.5 \
# --snr-opt-pso-c2 2.0 \
# --snr-opt-pso-w 0.01 \
# --snr-opt-include-candidate " \

# note that, at this point, some SNR optimization processes may still be
# running, so the checks below may ignore their results
Expand Down
31 changes: 5 additions & 26 deletions pycbc/live/snr_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,13 @@ def insert_snr_optimizer_options(parser):
opt_opt_group.add_argument('--snr-opt-include-candidate',
action='store_true',
help='Include parameters of the candidate event in the initialized '
'array for the optimizer. Only relevant for --optimizer pso or '
'differential_evolution')
'array for the optimizer. Only relevant for --snr-opt-method pso '
'or differential_evolution')
opt_opt_group.add_argument('--snr-opt-seed',
default='42',
help='Seed to supply to the random generation of initial array to '
'pass to the optimizer. Only relevant for --optimizer pso or '
'differential_evolution. Set to ''random'' for a random seed')
'pass to the optimizer. Only relevant for --snr-opt-method pso '
'or differential_evolution. Set to ''random'' for a random seed')

# For each optimizer, add the possible options
for optimizer, option_subdict in option_dict.items():
Expand All @@ -345,7 +345,7 @@ def insert_snr_optimizer_options(parser):
option_name = f"--snr-opt-{optimizer_name}-{opt_name}"
opt_opt_group.add_argument(option_name,
type=float,
help=f'Only relevant for --optimizer {optimizer}: ' +
help=f'Only relevant for --snr-opt-method {optimizer}: ' +
opt_help_default[0] +
f' Default = {opt_help_default[1]}')

Expand Down Expand Up @@ -381,24 +381,3 @@ def check_snr_optimizer_options(args, parser):
key_name = f'snr_opt_{optimizer_name}_{key}'
if not getattr(args, key_name):
setattr(args, key_name, value[1])


def args_to_string(args):
"""
Convert the supplied arguments for SNR optimization config into
a string - this is to be used when running subprocesses
"""
argstr = f'--snr-opt-method {args.snr_opt_method} '
optimizer_name = args.snr_opt_method.replace('_', '-')
if optimizer_name == 'differential-evolution':
optimizer_name = 'di'
for opt in option_dict[args.snr_opt_method]:
key_name = f'snr_opt_{optimizer_name}_{opt}'
option_value = getattr(args, key_name)
# If the option is not given, don't pass it and use default
if option_value is None:
continue
option_fullname = f'--snr-opt-{optimizer_name}-{opt}'
argstr += f'{option_fullname} {option_value} '

return argstr

0 comments on commit c701861

Please sign in to comment.