LALApps 10.1.1.1-bf6a62b
lalapps_string_contour_plotter_largeloops.py
Go to the documentation of this file.
1##python
2#
3# Copyright (C) 2018 Daichi Tsuna
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the
7# Free Software Foundation; either version 2 of the License, or (at your
8# option) any later version.
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13# Public License for more details.
14#
15# You should have received a copy of the GNU General Public License along
16# with this program; if not, write to the Free Software Foundation, Inc.,
17# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19
20#
21# =============================================================================
22#
23# Preamble
24#
25# =============================================================================
26#
27
28#
29# This is a Python replacement for the contour_plotter.m MATLAB code
30# Specific for the "large loop" case, where constraints are drawn on the Gmu-p plane.
31#
32
33
34import matplotlib
35matplotlib.rcParams.update({
36 "font.size": 8.0,
37 "axes.titlesize": 10.0,
38 "axes.labelsize": 10.0,
39 "xtick.labelsize": 8.0,
40 "ytick.labelsize": 8.0,
41 "legend.fontsize": 8.0,
42 "figure.dpi": 300,
43 "savefig.dpi": 300,
44 "text.usetex": True
45})
46from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
47from matplotlib.figure import Figure
48
49import numpy
50from optparse import OptionParser
51
52from lalburst import git_version
53
54__author__ = "Daichi Tsuna <>"
55__version__ = "git id %s" % git_version.id
56__date__ = git_version.date
57
58
60 parser = OptionParser(
61 version = "Name: %%prog\n%s" % git_version.verbose_msg
62 )
63 parser.add_option("-v", "--verbose", action = "store_true",
64 help = "Be verbose.")
65 parser.add_option("-t", "--live-time", dest="livetime",
66 type = "float",
67 help = "The total amount of live time in the run")
68
69 options, filenames = parser.parse_args()
70 if options.livetime is None:
71 raise ValueError("No live time specified. Use -t or --live-time.")
72 if filenames is None:
73 raise ValueError("No data file specified.")
74 return options, filenames
75
76
77def cosmo_contour(x, y, avg, min, max, neventsUL, filename):
78 fig = Figure()
79 FigureCanvas(fig)
80 fig.set_size_inches(4.5, 4.5)
81 axes = fig.add_axes((.12, .15, .98 - .12, .90 - .15))
82 axes.loglog()
83 axes.contour(x, y, avg, [neventsUL], linestyles='solid', colors='r')
84 axes.contour(x, y, min, [neventsUL], linestyles='dashed', colors='r')
85 axes.contour(x, y, max, [neventsUL], linestyles='dashed', colors='r')
86 axes.set_xlim([x.min(),x.max()])
87 axes.set_ylim([y.min(),y.max()])
88 axes.set_title(r"95\% upper limit")
89 axes.set_xlabel(r"$G\mu$")
90 axes.set_ylabel(r"$p$")
91 fig.savefig(filename)
92
93#main
94
95options, filenames = parse_command_line()
96
97#read cs_gamma file output
98#the columns should be p,Gmu,gammaAverage,gammaMin,gammaMax
99prob, gmu, avg, min, max = [], [], [], [], []
100for line in open(filenames[0], 'r'):
101 line = line.strip()
102 if line[0] in "%#":
103 continue
104 for arr, val in zip((prob, gmu, avg, min, max), line.split()):
105 arr.append(float(val))
106
107gmuindex = dict((b, a) for a, b in enumerate(sorted(set(gmu))))
108pindex = dict((b, a) for a, b in enumerate(sorted(set(prob))))
109
110# check for rounding errors producing a wrong count of x and y values
111assert len(pindex) * len(gmuindex) == len(gmu)
112
113avgarr = numpy.zeros([len(pindex), len(gmuindex)], dtype = "double")
114minarr = numpy.zeros([len(pindex), len(gmuindex)], dtype = "double")
115maxarr = numpy.zeros([len(pindex), len(gmuindex)], dtype = "double")
116
117for p, g, val in zip(prob, gmu, avg):
118 avgarr[pindex[p], gmuindex[g]] = val
119for p, g, val in zip(prob, gmu, min):
120 minarr[pindex[p], gmuindex[g]] = val
121for p, g, val in zip(prob, gmu, max):
122 maxarr[pindex[p], gmuindex[g]] = val
123
124avgarr *= options.livetime
125minarr *= options.livetime
126maxarr *= options.livetime
127
128numberofeventsUL = -numpy.log(1-0.95)
129
130x = numpy.array(sorted(gmuindex.keys()))
131y = numpy.array(sorted(pindex.keys()))
132
133cosmo_contour(x, y, avgarr, minarr, maxarr, numberofeventsUL, "constraint")
def cosmo_contour(x, y, avg, min, max, neventsUL, filename)