From c820de4b3a517af9ae1110b47ea78a06d898e180 Mon Sep 17 00:00:00 2001 From: Gregory Ashton <gregory.ashton@ligo.org> Date: Wed, 16 May 2018 10:19:30 +1000 Subject: [PATCH] Adds a basic command line interface Closes #68 by giving a simple interface to set the log-level (unless set at the script level) and whether or not to use cached data. --- examples/injection_examples/basic_tutorial.py | 2 +- .../how_to_specify_the_prior.py | 2 +- .../marginalized_likelihood.py | 2 +- .../other_examples/alternative_likelihoods.py | 2 +- tupak/sampler.py | 13 +++++-- tupak/utils.py | 34 ++++++++++++++++++- 6 files changed, 48 insertions(+), 7 deletions(-) diff --git a/examples/injection_examples/basic_tutorial.py b/examples/injection_examples/basic_tutorial.py index dd990be32..83e1556f4 100644 --- a/examples/injection_examples/basic_tutorial.py +++ b/examples/injection_examples/basic_tutorial.py @@ -14,7 +14,7 @@ time_duration = 4. sampling_frequency = 2048. outdir = 'outdir' label = 'basic_tutorial' -tupak.utils.setup_logger(outdir=outdir, label=label, log_level="info") +tupak.utils.setup_logger(outdir=outdir, label=label) np.random.seed(170809) diff --git a/examples/injection_examples/how_to_specify_the_prior.py b/examples/injection_examples/how_to_specify_the_prior.py index 500cf91a9..8829a71f7 100644 --- a/examples/injection_examples/how_to_specify_the_prior.py +++ b/examples/injection_examples/how_to_specify_the_prior.py @@ -6,7 +6,7 @@ from __future__ import division, print_function import tupak import numpy as np -tupak.utils.setup_logger(log_level="info") +tupak.utils.setup_logger() time_duration = 4. sampling_frequency = 2048. diff --git a/examples/injection_examples/marginalized_likelihood.py b/examples/injection_examples/marginalized_likelihood.py index 7fcd0acee..67ad22256 100644 --- a/examples/injection_examples/marginalized_likelihood.py +++ b/examples/injection_examples/marginalized_likelihood.py @@ -7,7 +7,7 @@ from __future__ import division, print_function import tupak import numpy as np -tupak.utils.setup_logger(log_level="info") +tupak.utils.setup_logger() time_duration = 4. sampling_frequency = 2048. diff --git a/examples/other_examples/alternative_likelihoods.py b/examples/other_examples/alternative_likelihoods.py index dd735af89..0d31bd09d 100644 --- a/examples/other_examples/alternative_likelihoods.py +++ b/examples/other_examples/alternative_likelihoods.py @@ -9,7 +9,7 @@ import numpy as np import matplotlib.pyplot as plt # A few simple setup steps -tupak.utils.setup_logger(log_level="info") +tupak.utils.setup_logger() label = 'test' outdir = 'outdir' diff --git a/tupak/sampler.py b/tupak/sampler.py index c9f028c86..0f965bb4e 100644 --- a/tupak/sampler.py +++ b/tupak/sampler.py @@ -191,8 +191,17 @@ class Sampler(object): def check_cached_result(self): """ Check if the cached data file exists and can be used """ - logging.debug("Checking cached data") + + if utils.command_line_args.clean: + logging.debug("Command line argument clean given, forcing rerun") + self.cached_result = None + return self.cached_result = read_in_result(self.outdir, self.label) + if utils.command_line_args.use_cached: + logging.debug("Command line argument cached given, no cache check performed") + return + + logging.debug("Checking cached data") if self.cached_result: check_keys = ['search_parameter_keys', 'fixed_parameter_keys', 'kwargs'] @@ -206,7 +215,7 @@ class Sampler(object): self.cached_result = None def log_summary_for_sampler(self): - if self.cached_result is False: + if self.cached_result is None: logging.info("Using sampler {} with kwargs {}".format( self.__class__.__name__, self.kwargs)) diff --git a/tupak/utils.py b/tupak/utils.py index a6405f588..71eb761fa 100644 --- a/tupak/utils.py +++ b/tupak/utils.py @@ -4,6 +4,7 @@ import os import numpy as np from math import fmod from gwpy.timeseries import TimeSeries +import argparse # Constants speed_of_light = 299792458.0 # speed of light in m/s @@ -281,7 +282,7 @@ def get_vertex_position_geocentric(latitude, longitude, elevation): return np.array([x_comp, y_comp, z_comp]) -def setup_logger(outdir=None, label=None, log_level='info'): +def setup_logger(outdir=None, label=None, log_level=None): """ Setup logging output: call at the start of the script to use Parameters @@ -298,6 +299,8 @@ def setup_logger(outdir=None, label=None, log_level='info'): LEVEL = getattr(logging, log_level.upper()) except AttributeError: raise ValueError('log_level {} not understood'.format(log_level)) + elif log_level is None: + LEVEL = command_line_args.log_level else: LEVEL = int(log_level) @@ -509,4 +512,33 @@ def get_open_strain_data( return strain +def set_up_command_line_arguments(): + parser = argparse.ArgumentParser( + description="Command line interface for tupak scripts") + parser.add_argument("-v", "--verbose", action="store_true", + help=("Increase output verbosity [logging.DEBUG]." + + " Overridden by script level settings")) + parser.add_argument("-q", "--quite", action="store_true", + help=("Decrease output verbosity [logging.WARNING]." + + " Overridden by script level settings")) + parser.add_argument("-c", "--clean", action="store_true", + help="Force clean data, never use cached data") + parser.add_argument("-u", "--use-cached", action="store_true", + help="Force cached data and do not check its validity") + args, _ = parser.parse_known_args() + + if args.quite: + args.log_level = logging.WARNING + elif args.verbose: + args.log_level = logging.DEBUG + else: + args.log_level = logging.INFO + + return args + + +command_line_args = set_up_command_line_arguments() + + + -- GitLab