diff --git a/examples/injection_examples/basic_tutorial.py b/examples/injection_examples/basic_tutorial.py index dd990be32f856d842a49386009d90852fd5bec04..83e1556f4c8b0315d83a4459139040c99516ef03 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 500cf91a9635a9c0034beb7e2e96db97570622bf..8829a71f7846b7cf1130d0d606b13f0b37ce2f8c 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 7fcd0acee31f1c19b266007f6cc98d3e4e77d024..67ad222560cad941c2d23a3a6de6987a95a51ca5 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 dd735af89c49280262d861dd6e8d26550f9f3b7e..0d31bd09ddf4ee9c582917568bf52182f16d6284 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 c9f028c86c275fcacfb529520f0c5d05b35316b5..0f965bb4eee0d8214012bf02c39f206fd2817781 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 a6405f5883a583d40967d01b102eabaad37cc3ae..71eb761fa4ad05a896e724cca3abc97df5ff8fa8 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() + + +