Skip to content
Snippets Groups Projects
Commit 7ed8beda authored by Gregory Ashton's avatar Gregory Ashton
Browse files

Improvements to bilby_results CLI

1) Add support for color output on the terminal
2) Move print to a separate function
3) Make print work as a "match" rather than exact-match only
4) Allow results to be passed either as a positional or optional arg
parent fa9e0e2b
No related branches found
No related tags found
1 merge request!599Improvements to bilby results cli
......@@ -1006,3 +1006,10 @@ def decode_astropy_quantity(dct):
class IllegalDurationAndSamplingFrequencyException(Exception):
pass
class tcolors:
KEY = '\033[93m'
VALUE = '\033[91m'
HIGHLIGHT = '\033[95m'
END = '\033[0m'
......@@ -25,13 +25,18 @@ import argparse
import pandas as pd
import bilby
from bilby.core.utils import tcolors
def setup_command_line_args():
parser = argparse.ArgumentParser(
description="Helper tool for bilby result files")
parser.add_argument("-r", "--results", nargs='+', required=True,
help="List of results files.")
parser.add_argument(
"results", nargs='?',
help="List of results files.")
parser.add_argument(
"-r", "--results", nargs='+', dest="option_results", default=list(),
help="List of results files (alternative to passing results as a positional argument).")
parser.add_argument("-c", "--convert", type=str, choices=['json', 'hdf5'],
help="Convert all results.", default=False)
parser.add_argument("-m", "--merge", action='store_true',
......@@ -49,7 +54,16 @@ def setup_command_line_args():
parser.add_argument("--ipython", action='store_true',
help=("For each result given, drops the user into an "
"IPython shell with the result loaded in"))
args, _ = parser.parse_known_args()
args = parser.parse_args()
if args.results is None:
args.results = []
if isinstance(args.results, str):
args.results = [args.results]
args.results += args.option_results
if len(args.results) == 0:
raise ValueError("You have not passed any results to bilby_result")
return args
......@@ -81,6 +95,18 @@ def drop_to_ipython(results_list):
IPython.embed(header=message)
def print_matches(results_list, args):
for r in results_list:
print("\nResult file: {}/{}".format(r.outdir, r.label))
for key in args.print:
for attr in r.__dict__:
if key in attr:
print_line = [
" ", tcolors.KEY, attr, ":", tcolors.VALUE,
str(getattr(r, attr)), tcolors.END]
print(" ".join(print_line))
def main():
args = setup_command_line_args()
results_list = read_in_results(args.results)
......@@ -88,10 +114,7 @@ def main():
for r in results_list:
r.save_to_file(extension=args.convert, outdir=args.outdir)
if args.print is not None:
for r in results_list:
print("\nResult file: {}/{}".format(r.outdir, r.label))
for key in args.print:
print(" {}: {}".format(key, getattr(r, key, 'None')))
print_matches(results_list, args)
if args.call is not None:
for r in results_list:
for call in args.call:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment