Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALApps 10.1.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
lalapps_string_contour_plotter.py
Go to the documentation of this file.
1##python
2#
3# Copyright (C) 2010 Andrew Mergl
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#
31
32
33import math
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__ = "Andrew Mergl <>"
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, p, neventsUL, filename):
78 fig = Figure()
79 FigureCanvas(fig)
80 fig.set_size_inches(4.5, 4.5 / ((1 + math.sqrt(5)) / 2))
81 axes = fig.add_axes((.12, .15, .98 - .12, .90 - .15))
82 axes.loglog()
83 #axes.contourf(x, y, numpy.log(avg/p), 100, cmap = cm.bone)
84 #axes.contourf(x, y, numpy.log(min/p), 100, cmap = cm.bone)
85 #axes.contourf(x, y, numpy.log(max/p), 100, cmap = cm.bone)
86 axes.contour(x, y, avg/p, [neventsUL], linestyles='solid', colors='r')
87 axes.contour(x, y, min/p, [neventsUL], linestyles='dashed', colors='r')
88 axes.contour(x, y, max/p, [neventsUL], linestyles='dashed', colors='r')
89 axes.set_xlim([x.min(),x.max()])
90 axes.set_ylim([y.min(),y.max()])
91 axes.set_title(r"$P = %g$" % p)
92 axes.set_xlabel(r"$G\mu$")
93 axes.set_ylabel(r"$\epsilon$")
94 fig.savefig(filename)
95
96#main
97
98options, filenames = parse_command_line()
99
100#read cs_gamma file output
101#the columns should be p,index,epsilon,Gmu,gammaAverage,gammaMin,gammaMax
102p, n, eps, gmu, avg, min, max = [], [], [], [], [], [], []
103for line in open(filenames[0], 'r'):
104 line = line.strip()
105 if line[0] in "%#":
106 continue
107 for arr, val in zip((p, n, eps, gmu, avg, min, max), line.split()):
108 arr.append(float(val))
109
110epsindex = dict((b, a) for a, b in enumerate(sorted(set(eps))))
111gmuindex = dict((b, a) for a, b in enumerate(sorted(set(gmu))))
112
113# check for rounding errors producing a wrong count of x and y values
114assert len(epsindex) * len(gmuindex) == len(gmu)
115# program only works if all values in the data file are for the same p and that p is 1
116assert set(p) == set([1])
117
118avgarr = numpy.zeros([len(epsindex), len(gmuindex)], dtype = "double")
119minarr = numpy.zeros([len(epsindex), len(gmuindex)], dtype = "double")
120maxarr = numpy.zeros([len(epsindex), len(gmuindex)], dtype = "double")
121
122for e, g, val in zip(eps, gmu, avg):
123 avgarr[epsindex[e], gmuindex[g]] = val
124for e, g, val in zip(eps, gmu, min):
125 minarr[epsindex[e], gmuindex[g]] = val
126for e, g, val in zip(eps, gmu, max):
127 maxarr[epsindex[e], gmuindex[g]] = val
128
129avgarr *= options.livetime
130minarr *= options.livetime
131maxarr *= options.livetime
132
133numberofeventsUL = -numpy.log(1-0.90)
134
135x = numpy.array(sorted(gmuindex.keys()))
136y = numpy.array(sorted(epsindex.keys()))
137
138cosmo_contour(x, y, avgarr, minarr, maxarr, 1.000, numberofeventsUL, "gmu-eps-p1e+0")
139cosmo_contour(x, y, avgarr, minarr, maxarr, 0.100, numberofeventsUL, "gmu-eps-p1e-1")
140cosmo_contour(x, y, avgarr, minarr, maxarr, 0.010, numberofeventsUL, "gmu-eps-p1e-2")
141cosmo_contour(x, y, avgarr, minarr, maxarr, 0.001, numberofeventsUL, "gmu-eps-p1e-3")
def cosmo_contour(x, y, avg, min, max, p, neventsUL, filename)