Skip to content
Snippets Groups Projects
Commit 721e2ce2 authored by ChiWai Chan's avatar ChiWai Chan Committed by Madeline Wade
Browse files

gstlal_inspiral_plot_rankingstats_horizon: fix IO bugs for plotting

horizon history from multiple ranking statistic files.
parent a0bdf780
No related branches found
No related tags found
No related merge requests found
......@@ -16,31 +16,58 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""Plot horizon history from ranking statistics
"""Plot horizon history from ranking statistics.
"""
from itertools import groupby
from optparse import OptionParser
from pathlib import Path
from gstlal.plots import horizon
from lal.utils import CacheEntry
def parse_command_line():
parser = OptionParser(description = __doc__)
parser.add_option("-o", "--output", help = "Set plot filename.")
parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose")
parser.add_option("-o", "--outdir", default = ".", help = "Set the output directory. By default, current directory will be used.")
parser.add_option("-f", "--format", default = "png", help = "Set the output format. By default, the output will be a png file.")
parser.add_option("-s", "--fig-size", default = "12,4", help = "Set the output figure size in inch. By default, the figure size will be 12,4. (Optional)")
parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose. (Optional)")
options, args = parser.parse_args()
options.format = options.format.strip(".")
try:
figsize = options.fig_size.split(",")
w, h = float(figsize[0]), float(figsize[1])
except:
raise ValueError("Please provide valid --fig-size, expecting a format of 'width,height', got '%s'." % options.fig_size)
else:
options.fig_size = (w, h)
if len(args) == 0:
raise ValueError("Must provide at least one ranking statistics file.")
if options.output is None:
raise ValueError("Must provide --output (output filename).")
return options, args
options, files = parse_command_line()
horizon_distance = horizon.HorizonDistance.from_rankingstats(files, verbose = options.verbose)
horizon_distance.savefig(options.output)
urls = []
for f in files:
if f.endswith(".cache"):
urls.extend([CacheEntry(line).url for line in open(f)])
elif f.endswith(".gz"):
urls.append(f)
outdir = Path(options.outdir)
for key, group in groupby(sorted(urls, key = lambda x: CacheEntry.from_T050017(x).description), lambda x: CacheEntry.from_T050017(x).description):
rankfiles = list(group)
ce = CacheEntry.from_T050017(rankfiles[0])
desc = ce.description.replace('MARG_DIST_STATS', 'HORIZON')
start = ce.segment[0]
duration = ce.segment[1] - ce.segment[0]
output = outdir / f"{ce.observatory}-{desc}-{start}-{duration}.{options.format}"
horizon_distance = horizon.HorizonDistance.from_rankingstats(rankfiles, verbose = options.verbose)
horizon_distance.savefig(output, options.fig_size)
......@@ -76,7 +76,7 @@ class HorizonDistance:
"""
loader = cls.datasource_loader(sources)
urls = loader(sources)
rankingstat = far.marginalize_pdf_urls(list(urls), "RankingStat", verbose = verbose)
rankingstat = far.marginalize_pdf_urls(urls, "RankingStat", verbose = verbose)
horizon_history_dict = rankingstat.numerator.horizon_history
return cls(horizon_history_dict)
......
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