30 matplotlib.rcParams.update({
32 "axes.titlesize": 10.0,
33 "axes.labelsize": 10.0,
34 "xtick.labelsize": 8.0,
35 "ytick.labelsize": 8.0,
36 "legend.fontsize": 8.0,
41 from matplotlib
import figure
42 from matplotlib.backends.backend_agg
import FigureCanvasAgg
as FigureCanvas
44 from gstlal
import plotutil
45 from gstlal
import far
48 def plot_snr_chi_pdf(coinc_param_distributions, instrument, binnedarray_string, snr_max, dynamic_range_factor = 1e-10):
49 key =
"%s_snr_chi" % instrument
50 if binnedarray_string ==
"LR":
51 binnedarray = getattr(coinc_param_distributions,
"injection_pdf")[key]
53 binnedarray = getattr(coinc_param_distributions, binnedarray_string)[key]
54 tag = {
"background_pdf":
"Noise",
"injection_pdf":
"Signal",
"zero_lag_pdf":
"Candidates",
"LR":
"LR"}[binnedarray_string]
58 fig.set_size_inches((4.5, 3.5))
62 x = binnedarray.bins[0].centres()[:-1]
63 y = binnedarray.bins[1].centres()[:-1]
64 z = binnedarray.array[:-1,:-1]
66 if binnedarray_string ==
"LR":
67 denom_binnedarray = coinc_param_distributions.background_pdf[key]
68 assert (denom_binnedarray.bins[0].centres()[:-1] == x).all()
69 assert (denom_binnedarray.bins[1].centres()[:-1] == y).all()
70 z /= denom_binnedarray.array[:-1,:-1]
71 if numpy.isnan(z).any():
72 warnings.warn(
"%s PDF contains NaNs" % instrument)
73 z = numpy.ma.masked_where(numpy.isnan(z), z)
75 warnings.warn(
"%s PDF is 0, skipping" % instrument)
79 xlo, xhi = far.ThincaCoincParamsDistributions.snr_min, snr_max
82 x = x[binnedarray.bins[xlo:xhi, ylo:yhi][0]]
83 y = y[binnedarray.bins[xlo:xhi, ylo:yhi][1]]
84 z = z[binnedarray.bins[xlo:xhi, ylo:yhi]]
91 numpy.clip(z, z.max() * dynamic_range_factor, float(
"+inf"), out = z)
93 mesh = axes.pcolormesh(x, y, z.T, norm = matplotlib.colors.LogNorm(), cmap =
"afmhot", shading =
"gouraud")
94 axes.contour(x, y, z.T, norm = matplotlib.colors.LogNorm(), colors =
"k", linewidths = .5)
96 axes.grid(which =
"both")
99 fig.colorbar(mesh, ax = axes)
100 axes.set_xlabel(
r"$\mathrm{SNR}$")
101 axes.set_ylabel(
r"$\chi^{2} / \mathrm{SNR}^{2}$")
102 if tag.lower()
in (
"signal",):
103 axes.set_title(
r"%s %s $P(\chi^{2} / \mathrm{SNR}^{2} | \mathrm{SNR})$" % (instrument, tag))
104 elif tag.lower()
in (
"noise",
"candidates"):
105 axes.set_title(
r"%s %s $P(\mathrm{SNR}, \chi^{2} / \mathrm{SNR}^{2})$" % (instrument, tag))
106 elif tag.lower()
in (
"lr",):
107 axes.set_title(
r"%s $P(\chi^{2} / \mathrm{SNR}^{2} | \mathrm{SNR}) / P(\mathrm{SNR}, \chi^{2} / \mathrm{SNR}^{2})$" % instrument)
109 raise ValueError(tag)
113 def plot_rates(coinc_param_distributions, ranking_data = None):
114 fig = figure.Figure()
116 fig.set_size_inches((6., 6.))
117 axes0 = fig.add_subplot(2, 2, 1)
118 axes1 = fig.add_subplot(2, 2, 2)
119 axes2 = fig.add_subplot(2, 2, 3)
120 axes3 = fig.add_subplot(2, 2, 4)
126 for instrument, category
in sorted(coinc_param_distributions.instrument_categories.items()):
127 count = coinc_param_distributions.background_rates[
"instruments"][category,]
130 labels.append(
"%s\n(%d)" % (instrument, count))
132 colours.append(plotutil.colour_from_instruments((instrument,)))
133 axes0.pie(sizes, labels = labels, colors = colours, autopct =
"%.3g%%", pctdistance = 0.4, labeldistance = 0.8)
134 axes0.set_title(
"Observed Background Event Counts")
140 for instruments
in sorted(sorted(instruments)
for instruments
in coinc_param_distributions.count_above_threshold
if instruments
is not None):
141 count = coinc_param_distributions.background_rates[
"instruments"][coinc_param_distributions.instrument_categories.category(instruments),]
142 if len(instruments) < 2
or not count:
144 labels.append(
"%s\n(%d)" % (
", ".join(instruments), count))
146 colours.append(plotutil.colour_from_instruments(instruments))
147 axes1.pie(sizes, labels = labels, colors = colours, autopct =
"%.3g%%", pctdistance = 0.4, labeldistance = 0.8)
148 axes1.set_title(
"Projected Background Coincidence Counts")
152 if ranking_data
is not None:
156 for instruments, fraction
in sorted(coinc_param_distributions.Pinstrument_signal.items(), key =
lambda (instruments, fraction): sorted(instruments)):
157 if len(instruments) < 2
or not fraction:
159 labels.append(
", ".join(sorted(instruments)))
160 sizes.append(fraction)
161 colours.append(plotutil.colour_from_instruments(instruments))
162 axes2.pie(sizes, labels = labels, colors = colours, autopct =
"%.3g%%", pctdistance = 0.4, labeldistance = 0.8)
163 axes2.set_title(
r"Projected Recovered Signal Distribution")
169 for instruments, count
in sorted((sorted(instruments), count)
for instruments, count
in coinc_param_distributions.count_above_threshold.items()
if instruments
is not None):
170 if len(instruments) < 2
or not count:
172 labels.append(
"%s\n(%d)" % (
", ".join(instruments), count))
174 colours.append(plotutil.colour_from_instruments(instruments))
175 axes3.pie(sizes, labels = labels, colors = colours, autopct =
"%.3g%%", pctdistance = 0.4, labeldistance = 0.8)
176 axes3.set_title(
"Observed Coincidence Counts")
179 fig.tight_layout(pad = .8)
181 except AttributeError:
184 def plot_snr_joint_pdf(coinc_param_distributions, instruments, horizon_distances, max_snr):
185 if len(instruments) > 2:
188 ignored, binnedarray, ignored = coinc_param_distributions.snr_joint_pdf_cache[(instruments, horizon_distances)]
189 instruments = sorted(instruments)
190 horizon_distances = dict(horizon_distances)
191 fig = figure.Figure()
193 fig.set_size_inches((5, 4))
195 x = binnedarray.bins[0].centres()
196 y = binnedarray.bins[1].centres()
197 z = binnedarray.array
198 if numpy.isnan(z).any():
199 warnings.warn(
"%s SNR PDF for %s contains NaNs" % (
", ".join(instruments),
", ".join(
"%s=%g" % instdist
for instdist
in sorted(horizon_distances.items()))))
200 z = numpy.ma.masked_where(numpy.isnan(z), z)
203 xlo, xhi = far.ThincaCoincParamsDistributions.snr_min, max_snr
205 x = x[binnedarray.bins[xlo:xhi, xlo:xhi][0]]
206 y = y[binnedarray.bins[xlo:xhi, xlo:xhi][1]]
207 z = z[binnedarray.bins[xlo:xhi, xlo:xhi]]
215 numpy.clip(z, z.max() * 1e-20, float(
"+inf"), out = z)
219 assert not numpy.isnan(z).any()
220 assert not (z <= 0.).any()
222 mesh = axes.pcolormesh(x, y, z.T, norm = matplotlib.colors.LogNorm(), cmap =
"afmhot", shading =
"gouraud")
223 axes.contour(x, y, z.T, norm = matplotlib.colors.LogNorm(), colors =
"k", linewidths = .5)
225 axes.grid(which =
"both", linestyle =
"-", linewidth = 0.2)
228 fig.colorbar(mesh, ax = axes)
230 axes.set_xlabel(
r"$\mathrm{SNR}_{\mathrm{%s}}$" % instruments[0])
231 axes.set_ylabel(
r"$\mathrm{SNR}_{\mathrm{%s}}$" % instruments[1])
232 axes.set_title(
r"$P(%s)$" %
", ".join(
"\mathrm{SNR}_{\mathrm{%s}}" % instrument
for instrument
in instruments))
237 def plot_likelihood_ratio_pdf(ranking_data, instruments, (xlo, xhi), tag, binnedarray_string =
"background_likelihood_pdfs"):
238 pdf = getattr(ranking_data, binnedarray_string)[instruments]
239 if binnedarray_string ==
"background_likelihood_pdfs":
240 zerolag_pdf = ranking_data.zero_lag_likelihood_pdfs[instruments]
244 fig = figure.Figure()
246 fig.set_size_inches((4., 4. / plotutil.golden_ratio))
248 axes.semilogy(pdf.bins[0].centres(), pdf.array, color =
"k")
249 if zerolag_pdf
is not None:
250 axes.semilogy(zerolag_pdf.bins[0].centres(), zerolag_pdf.array, color =
"k", linestyle =
"--")
251 axes.grid(which =
"major", linestyle =
"-", linewidth = 0.2)
252 if instruments
is None:
253 axes.set_title(
r"%s Log Likelihood Ratio PDF" % tag)
255 axes.set_title(
r"%s %s Log Likelihood Ratio PDF" % (
", ".join(sorted(instruments)), tag))
256 axes.set_xlabel(
r"$\ln \mathcal{L}$")
257 axes.set_ylabel(
r"$P(\ln \mathcal{L} | \mathrm{%s})$" % tag.lower())
258 yhi = pdf[xlo:xhi,].max()
259 ylo = pdf[xlo:xhi,].min()
260 if zerolag_pdf
is not None:
261 yhi = max(yhi, zerolag_pdf[xlo:xhi,].max())
262 ylo = min(ylo, zerolag_pdf[xlo:xhi,].min())
263 ylo = max(yhi * 1e-40, ylo)
264 axes.set_ylim((10**math.floor(math.log10(ylo) - .5), 10**math.ceil(math.log10(yhi) + .5)))
265 axes.set_xlim((xlo, xhi))
268 fig.tight_layout(pad = .8)
270 except AttributeError:
273 def plot_likelihood_ratio_ccdf(fapfar, (xlo, xhi), tag, zerolag_ln_likelihood_ratios =
None):
274 ccdf = fapfar.ccdf_interpolator
276 fig = figure.Figure()
278 fig.set_size_inches((4., 4. / plotutil.golden_ratio))
280 x = numpy.linspace(xlo, xhi, 10000)
282 axes.semilogy(x, y, color =
"k")
284 ylo = max(yhi * 1e-40, y.min())
285 if zerolag_ln_likelihood_ratios
is not None:
286 x = numpy.array(sorted(zerolag_ln_likelihood_ratios), dtype =
"double")
287 y = numpy.arange(len(zerolag_ln_likelihood_ratios), 0, -1, dtype =
"double")
288 y *= (1. - 1. / math.e) / y[0]
289 axes.semilogy(x, y, color =
"k", linestyle =
"--")
290 yhi = max(yhi, y.max())
291 ylo = min(ylo, y.min())
292 ylo = max(ylo, yhi * (y.min() / yhi)**1.5)
293 axes.set_ylim((10**math.floor(math.log10(ylo) - .5), 10**math.ceil(math.log10(yhi) + .5)))
294 axes.set_xlim((xlo, xhi))
295 axes.grid(which =
"major", linestyle =
"-", linewidth = 0.2)
296 axes.set_title(
r"%s Log Likelihood Ratio CCDF" % tag)
297 axes.set_xlabel(
r"$\ln \mathcal{L}$")
298 axes.set_ylabel(
r"$P(\geq \ln \mathcal{L} | \mathrm{%s})$" % tag.lower())
301 fig.tight_layout(pad = .8)
303 except AttributeError: