diff --git a/gstlal-inspiral/bin/gstlal_inspiral_plot_rankingstats_horizon b/gstlal-inspiral/bin/gstlal_inspiral_plot_rankingstats_horizon index 5549ec28f95e5e720b02a24485d4f56bf57f886a..093801aba4468eac4b4932e4e1ada7bc5d0fa8b3 100755 --- a/gstlal-inspiral/bin/gstlal_inspiral_plot_rankingstats_horizon +++ b/gstlal-inspiral/bin/gstlal_inspiral_plot_rankingstats_horizon @@ -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) diff --git a/gstlal-inspiral/python/plots/horizon.py b/gstlal-inspiral/python/plots/horizon.py index 85257bb4bdcb2e101660b8cdecfefb664bb76baf..dd8fad93ba7e9dd1cbe41aabfe40c537c65e5288 100644 --- a/gstlal-inspiral/python/plots/horizon.py +++ b/gstlal-inspiral/python/plots/horizon.py @@ -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)