bilby_pipe.utils

A set of generic utilities used in bilby_pipe

Attributes

CHECKPOINT_EXIT_CODE

ENVIRONMENT_DEFAULTS

nonestr

noneint

nonefloat

DEFAULT_DISTANCE_LOOKUPS

DURATION_LOOKUPS

MAXIMUM_FREQUENCY_LOOKUPS

SAMPLER_SETTINGS

logger

Exceptions

BilbyPipeError

Common base class for all non-exit exceptions.

BilbyPipeInternalError

Common base class for all non-exit exceptions.

Classes

tcolors

ArgumentsString

A convenience object to aid in the creation of argument strings

DataDump

NoneWrapper

Wrapper around other types so that "None" always evaluates to None.

DuplicateErrorDict

An dictionary with immutable key-value pairs

Functions

get_colored_string(msg_list[, color])

get_command_line_arguments()

Helper function to return the list of command line arguments

run_command_line(arguments[, directory])

parse_args(input_args, parser[, allow_unknown])

Parse an argument list using parser generated by create_parser()

check_directory_exists_and_if_not_mkdir(directory)

Checks if the given directory exists and creates it if it does not exist

setup_logger([outdir, label, log_level])

Setup logging output: call at the start of the script to use

get_outdir_name(outdir[, fail_on_match, base_increment])

generate_new_outdir_name(outdir)

Generate a new outdir name to avoid naming conflicts

log_version_information()

get_version_information()

convert_string_to_tuple(string[, key, n])

Convert a string to a tuple

convert_string_to_dict(string[, key])

Convert a string repr of a string to a python dictionary

convert_string_to_list(string)

Converts a string to a list, e.g. the mode_array waveform argument

sanitize_string_for_list(string)

convert_dict_values_if_possible(dic)

write_config_file(config_dict, filename[, comment, ...])

Writes ini file

test_connection()

A generic test to see if the network is reachable

strip_quotes(string)

string_to_int_float(s)

is_a_power_of_2(num)

next_power_of_2(x)

request_memory_generation_lookup(duration[, roq])

Function to determine memory required at the data generation step

get_time_prior(time, uncertainty[, name, latex_label])

Generate a time prior given some uncertainty.

get_geocent_time_with_uncertainty(geocent_time, ...)

Get a new geocent time within some uncertainty from the original geocent time.

convert_detectors_input(string)

Convert string inputs into a standard form for the detectors

convert_prior_string_input(string)

comma_partition(s)

Partitions s at top-level commas

make_partition_slices(ixs)

Yields partitioning slices, skipping each index of ixs

kv_parser(kv_str[, remove_leading_namespace])

Takes a string in 'K=V' format and returns dictionary.

check_if_represents_int(s)

Checks if the string/bytes-like object/number s represents an int

pretty_print_dictionary(dictionary)

Convert an input dictionary to a pretty-printed string

get_function_from_string_path(python_path)

convert_string_slice_syntax(string)

check_if_psd_is_from_built_in(psd_file)

Check if the psd_file can be found in the bilby built-in directory

resolve_filename_with_transfer_fallback(filename)

When absolute paths are transferred using HTCondor, the absolute path is

log_function_call(function, kwargs[, log])

Module Contents

bilby_pipe.utils.CHECKPOINT_EXIT_CODE = 77[source]
bilby_pipe.utils.ENVIRONMENT_DEFAULTS[source]
class bilby_pipe.utils.tcolors[source]
WARNING = '\x1b[31m'[source]
KEY = '\x1b[93m'[source]
VALUE = '\x1b[91m'[source]
HIGHLIGHT = '\x1b[95m'[source]
END = '\x1b[0m'[source]
bilby_pipe.utils.get_colored_string(msg_list, color='WARNING')[source]
exception bilby_pipe.utils.BilbyPipeError(message)[source]

Bases: Exception

Common base class for all non-exit exceptions.

exception bilby_pipe.utils.BilbyPipeInternalError(message)[source]

Bases: Exception

Common base class for all non-exit exceptions.

class bilby_pipe.utils.ArgumentsString[source]

Bases: object

A convenience object to aid in the creation of argument strings

argument_list = [][source]
append(argument)[source]
add_positional_argument(value)[source]
add_flag(flag)[source]
add(argument, value)[source]
add_unknown_args(unknown_args)[source]
add_command_line_arguments()[source]

Adds command line arguments given in addition to the ini file

print()[source]
class bilby_pipe.utils.DataDump(label, outdir, trigger_time, likelihood_lookup_table, likelihood_roq_weights, likelihood_roq_params, likelihood_multiband_weights, priors_dict, priors_class, interferometers, meta_data, idx)[source]

Bases: object

trigger_time[source]
label[source]
outdir[source]
interferometers[source]
likelihood_lookup_table[source]
likelihood_roq_weights[source]
likelihood_roq_params[source]
likelihood_multiband_weights[source]
priors_dict[source]
priors_class[source]
meta_data[source]
idx[source]
static get_filename(outdir, label)[source]
property filename[source]
to_pickle()[source]
classmethod from_pickle(filename=None)[source]

Loads in a data dump

Parameters:
filename: str

If given, try to load from this filename

class bilby_pipe.utils.NoneWrapper(type)[source]

Bases: object

Wrapper around other types so that “None” always evaluates to None.

This is needed to properly read None from ini files.

type[source]
__call__(val)[source]
bilby_pipe.utils.nonestr[source]
bilby_pipe.utils.noneint[source]
bilby_pipe.utils.nonefloat[source]
bilby_pipe.utils.DEFAULT_DISTANCE_LOOKUPS[source]
bilby_pipe.utils.DURATION_LOOKUPS[source]
bilby_pipe.utils.MAXIMUM_FREQUENCY_LOOKUPS[source]
bilby_pipe.utils.SAMPLER_SETTINGS[source]
bilby_pipe.utils.get_command_line_arguments()[source]

Helper function to return the list of command line arguments

bilby_pipe.utils.run_command_line(arguments, directory=None)[source]
bilby_pipe.utils.parse_args(input_args, parser, allow_unknown=True)[source]

Parse an argument list using parser generated by create_parser()

Parameters:
input_args: list

A list of arguments

Returns:
args: argparse.Namespace

A simple object storing the input arguments

unknown_args: list

A list of any arguments in input_args unknown by the parser

bilby_pipe.utils.check_directory_exists_and_if_not_mkdir(directory)[source]

Checks if the given directory exists and creates it if it does not exist

Parameters:
directory: str

Name of the directory

bilby_pipe.utils.setup_logger(outdir=None, label=None, log_level='INFO')[source]

Setup logging output: call at the start of the script to use

Parameters:
outdir, label: str

If supplied, write the logging output to outdir/label.log

log_level: str, optional

[‘debug’, ‘info’, ‘warning’] Either a string from the list above, or an integer as specified in https://docs.python.org/2/library/logging.html#logging-levels

bilby_pipe.utils.get_outdir_name(outdir, fail_on_match=False, base_increment='A')[source]
bilby_pipe.utils.generate_new_outdir_name(outdir)[source]

Generate a new outdir name to avoid naming conflicts

The incrementing name format is “_A”, “_B”, etc. Once “_Z” is reached, we then move to “_AA” etc.

bilby_pipe.utils.log_version_information()[source]
bilby_pipe.utils.get_version_information()[source]
bilby_pipe.utils.convert_string_to_tuple(string, key=None, n=None)[source]

Convert a string to a tuple

Parameters:
string: str

The string to convert

key: str

Name used for printing useful debug messages

n: int

The length of the string to check against, if None no check performed.

Returns:
tup: tuple

A tuple

bilby_pipe.utils.convert_string_to_dict(string, key=None)[source]

Convert a string repr of a string to a python dictionary

Parameters:
string: str

The string to convert

key: str (None)

A key, used for debugging

bilby_pipe.utils.convert_string_to_list(string)[source]

Converts a string to a list, e.g. the mode_array waveform argument

See tests/utils_test for tested behaviour.

Returns:
new_list: list

A list (or lists)

bilby_pipe.utils.sanitize_string_for_list(string)[source]
bilby_pipe.utils.convert_dict_values_if_possible(dic)[source]
bilby_pipe.utils.write_config_file(config_dict, filename, comment=None, remove_none=False)[source]

Writes ini file

Parameters:
config_dict: dict

Dictionary of parameters for ini file

filename: str

Filename to write the config file to

comment: str

Additional information on ini file generation

remove_none: bool

If true, remove None’s from the config_dict before writing otherwise a ValueError is raised

bilby_pipe.utils.test_connection()[source]

A generic test to see if the network is reachable

bilby_pipe.utils.strip_quotes(string)[source]
bilby_pipe.utils.string_to_int_float(s)[source]
bilby_pipe.utils.is_a_power_of_2(num)[source]
bilby_pipe.utils.next_power_of_2(x)[source]
bilby_pipe.utils.request_memory_generation_lookup(duration, roq=False)[source]

Function to determine memory required at the data generation step

bilby_pipe.utils.get_time_prior(time, uncertainty, name='geocent_time', latex_label='$t_c$')[source]

Generate a time prior given some uncertainty.

Parameters:
time: float

The GPS geocent_time (time of coalescence at the center of the Earth)

uncertainty: float

The +/- uncertainty based around the geocenter time.

name: str

The name of the time parameter

latex_label: str

The latex label for the time parameter

Returns:
A bilby.core.prior.Uniform for the time parameter.
bilby_pipe.utils.get_geocent_time_with_uncertainty(geocent_time, uncertainty)[source]

Get a new geocent time within some uncertainty from the original geocent time.

Parameters:
geocent_time: float

The GPS geocent_time (time of coalescence at the center of the Earth)

uncertainty: float

The +/- uncertainty based around the geocenter time.

Returns:
A geocent GPS time (float) inside the range of geocent time - uncertainty and
geocent time + uncertainty.
bilby_pipe.utils.convert_detectors_input(string)[source]

Convert string inputs into a standard form for the detectors

Parameters:
string: str

A string representation to be converted

Returns:
detectors: list

A sorted list of detectors

bilby_pipe.utils.convert_prior_string_input(string)[source]
bilby_pipe.utils.comma_partition(s)[source]

Partitions s at top-level commas

bilby_pipe.utils.make_partition_slices(ixs)[source]

Yields partitioning slices, skipping each index of ixs

bilby_pipe.utils.kv_parser(kv_str, remove_leading_namespace=False)[source]

Takes a string in ‘K=V’ format and returns dictionary.

bilby_pipe.utils.check_if_represents_int(s)[source]

Checks if the string/bytes-like object/number s represents an int

bilby_pipe.utils.pretty_print_dictionary(dictionary)[source]

Convert an input dictionary to a pretty-printed string

Parameters:
dictionary: dict

Input dictionary

Returns:
pp: str

The dictionary pretty-printed as a string

class bilby_pipe.utils.DuplicateErrorDict(color=True, *args)[source]

Bases: dict

An dictionary with immutable key-value pairs

Once a key-value pair is initialized, any attempt to update the value for an existing key will raise a BilbyPipeError.

Raises:
BilbyPipeError:

When a user attempts to update an existing key.

color[source]
__setitem__(key, val)[source]

Set self[key] to value.

bilby_pipe.utils.get_function_from_string_path(python_path)[source]
bilby_pipe.utils.convert_string_slice_syntax(string)[source]
bilby_pipe.utils.check_if_psd_is_from_built_in(psd_file)[source]

Check if the psd_file can be found in the bilby built-in directory

bilby_pipe.utils.resolve_filename_with_transfer_fallback(filename)[source]

When absolute paths are transferred using HTCondor, the absolute path is replaced with the basename of the file. This function checks if the file exists, if not, checks if the basename exists. If the basename exists, returns the basename, otherwise returns None.

Parameters:
filename: str

The filename to check

Returns:
filename: str

The filename, or basename, if it exists, otherwise None

bilby_pipe.utils.log_function_call(function, kwargs, log=True)[source]
bilby_pipe.utils.logger[source]