def finish(self): self.axes.set_title(r"\begin{center}Distribution of Coincident Events (%d Foreground, %d Background Events, %d Injections Found in Coincidence, Logarithmic Density Contours)\end{center}" % (self.n_foreground, self.n_background, self.n_injections)) xcoords, ycoords = self.background_bins.centres() # prepare the data rate.filter_array(self.foreground_bins.array, rate.gaussian_window(8, 8)) rate.filter_array(self.background_bins.array, rate.gaussian_window(8, 8)) rate.filter_array(self.coinc_injection_bins.array, rate.gaussian_window(8, 8)) rate.filter_array(self.incomplete_coinc_injection_bins.array, rate.gaussian_window(8, 8)) self.foreground_bins.logregularize() self.background_bins.logregularize() self.coinc_injection_bins.logregularize() self.incomplete_coinc_injection_bins.logregularize() # plot background contours max_density = math.log(self.background_bins.array.max()) self.axes.contour(xcoords, ycoords, numpy.transpose(numpy.log(self.background_bins.array)), [max_density - n for n in xrange(0, 10, 1)], cmap = matplotlib.cm.Greys) # plot foreground (zero-lag) contours max_density = math.log(self.foreground_bins.array.max()) self.axes.contour(xcoords, ycoords, numpy.transpose(numpy.log(self.foreground_bins.array)), [max_density - n for n in xrange(0, 10, 1)], cmap = matplotlib.cm.Reds) #self.axes.plot(self.foreground_x, self.foreground_y, "r+") # plot coincident injection contours max_density = math.log(self.coinc_injection_bins.array.max()) self.axes.contour(xcoords, ycoords, numpy.transpose(numpy.log(self.coinc_injection_bins.array)), [max_density - n for n in xrange(0, 10, 1)], cmap = matplotlib.cm.Blues) # plot incomplete coincident injection contours max_density = math.log(self.incomplete_coinc_injection_bins.array.max()) self.axes.contour(xcoords, ycoords, numpy.transpose(numpy.log(self.incomplete_coinc_injection_bins.array)), [max_density - n for n in xrange(0, 10, 1)], cmap = matplotlib.cm.Greens) # fix axes limits self.axes.set_xlim([self.background_bins.bins.min[0], self.background_bins.bins.max[0]]) self.axes.set_ylim([self.background_bins.bins.min[1], self.background_bins.bins.max[1]])
def finish(self): for key, pdf in self.densities.items(): if key.endswith("_snr2_chi2"): rate.filter_array(pdf.array, rate.gaussian_window(11, 11, sigma = 20)) elif key.endswith("_dt") or key.endswith("_dA") or key.endswith("_df"): rate.filter_array(pdf.array, rate.gaussian_window(11, sigma = 20)) elif key.startswith("instrumentgroup"): # instrument group filter is a no-op pass else: # shouldn't get here raise Exception pdf.normalize() self.mkinterps()
def density_estimate(lnpdf, name, kernel=rate.gaussian_window(4.)): """ For internal use only. """ assert not numpy.isnan(lnpdf.array).any( ), "%s log likelihood ratio PDF contain NaNs" % name rate.filter_array(lnpdf.array, kernel)
def finish(self, binning=None): # compute the binning if needed, and set the injections # into the numerator and denominator bins. also compute # the smoothing window's parameters. self._bin_events(binning) # smooth the efficiency data. print("Sum of numerator bins before smoothing = %g" % self.efficiency.numerator.array.sum(), file=sys.stderr) print("Sum of denominator bins before smoothing = %g" % self.efficiency.denominator.array.sum(), file=sys.stderr) rate.filter_binned_ratios( self.efficiency, rate.gaussian_window(self.window_size_x, self.window_size_y)) print("Sum of numerator bins after smoothing = %g" % self.efficiency.numerator.array.sum(), file=sys.stderr) print("Sum of denominator bins after smoothing = %g" % self.efficiency.denominator.array.sum(), file=sys.stderr) # regularize to prevent divide-by-zero errors self.efficiency.regularize()
def finish(self): livetime = rate.BinnedArray(self.counts.bins) for offsets in self.tisi_rows: self.seglists.offsets.update(offsets) livetime[offsets[self.x_instrument], offsets[self.y_instrument]] += float( abs(self.seglists.intersection(self.seglists.keys()))) zvals = self.counts.at_centres() / livetime.at_centres() rate.filter_array(zvals, rate.gaussian_window(3, 3)) xcoords, ycoords = self.counts.centres() self.axes.contour(xcoords, ycoords, numpy.transpose(numpy.log(zvals))) for offsets in self.tisi_rows: if any(offsets): # time slide vector is non-zero-lag self.axes.plot((offsets[self.x_instrument], ), (offsets[self.y_instrument], ), "k+") else: # time slide vector is zero-lag self.axes.plot((offsets[self.x_instrument], ), (offsets[self.y_instrument], ), "r+") self.axes.set_xlim( [self.counts.bins().min[0], self.counts.bins().max[0]]) self.axes.set_ylim( [self.counts.bins().min[1], self.counts.bins().max[1]]) self.axes.set_title( r"Coincident Event Rate vs. Instrument Time Offset (Logarithmic Rate Contours)" )
def finish(self): self.axes.set_title("Trigger Rate vs. Peak Frequency\n(%d Triggers)" % self.nevents) # 21 bins per filter width rate.filter_array(self.rate.array, rate.gaussian_window(21)) xvals = self.rate.centres()[0] self.axes.plot(xvals, self.rate.at_centres(), "k") self.axes.semilogy() self.axes.set_xlim((min(xvals), max(xvals)))
def finish(self): self.axes.set_title("Trigger Peak Time - Injection Peak Time\n(%d Found Injections)" % self.found) # 21 bins per filter width filter = rate.gaussian_window(21) rate.filter_array(self.offsets.array, filter) rate.filter_array(self.coinc_offsets.array, filter) self.axes.plot(self.offsets.centres()[0], self.offsets.at_centres(), "k") self.axes.plot(self.coinc_offsets.centres()[0], self.coinc_offsets.at_centres(), "r") self.axes.legend(["%s residuals" % self.instrument, "SNR-weighted mean of residuals in all instruments"], loc = "lower right")
def finish(self): for instrument, data in sorted(self.data.items()): fig, axes = SnglBurstUtils.make_burst_plot(r"$t_{\mathrm{recovered}} - t_{\mathrm{injected}}$ (s)", "Triggers per Unit Offset") axes.semilogy() axes.set_title("Trigger Peak Time - Injection Peak Time in %s\n(%d Found Injections)" % (instrument, data.found)) # 21 bins per filter width rate.filter_array(data.offsets.array, rate.gaussian_window(21)) axes.plot(data.offsets.centres()[0], data.offsets.at_centres(), "k") #axes.legend(["%s residuals" % instrument, "SNR-weighted mean of residuals in all instruments"], loc = "lower right") yield fig
def finish(self): self.axes.set_title("Time Between Triggers\n(%d Triggers)" % self.nevents) rate.filter_array(self.bins.array, rate.gaussian_window(21)) xvals = self.bins.centres()[0] yvals = self.bins.at_centres() self.axes.plot(xvals, yvals, "k") self.axes.set_xlim((0, xvals[-1])) self.axes.set_ylim((1, 10.0**(int(math.log10(yvals.max())) + 1)))
def finish(self): self.axes.set_title("Trigger Peak Frequency / Injection Centre Frequency\n(%d Found Injections)" % self.found) # 21 bins per filter width filter = rate.gaussian_window(21) rate.filter_array(self.offsets.array, filter) rate.filter_array(self.coinc_offsets.array, filter) self.axes.plot(10**self.offsets.centres()[0], self.offsets.at_centres(), "k") self.axes.plot(10**self.coinc_offsets.centres()[0], self.coinc_offsets.at_centres(), "r") self.axes.legend(["%s triggers" % self.instrument, "SNR-weighted mean of all matching triggers"], loc = "lower right") ymin, ymax = self.axes.get_ylim() if ymax / ymin > 1e6: ymin = ymax / 1e6 self.axes.set_ylim((ymin, ymax))
def create_efficiency_plot(axes, all_injections, found_injections, detection_threshold, cal_uncertainty): filter_width = 16.7 # formats axes.semilogx() axes.set_position([0.10, 0.150, 0.86, 0.77]) # set desired yticks axes.set_yticks((0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)) axes.set_yticklabels((r"\(0\)", r"\(0.1\)", r"\(0.2\)", r"\(0.3\)", r"\(0.4\)", r"\(0.5\)", r"\(0.6\)", r"\(0.7\)", r"\(0.8\)", r"\(0.9\)", r"\(1.0\)")) axes.xaxis.grid(True, which = "major,minor") axes.yaxis.grid(True, which = "major,minor") # put made and found injections in the denominators and # numerators of the efficiency bins bins = rate.NDBins((rate.LogarithmicBins(min(sim.amplitude for sim in all_injections), max(sim.amplitude for sim in all_injections), 400),)) efficiency_num = rate.BinnedArray(bins) efficiency_den = rate.BinnedArray(bins) for sim in found_injections: efficiency_num[sim.amplitude,] += 1 for sim in all_injections: efficiency_den[sim.amplitude,] += 1 # generate and plot trend curves. adjust window function # normalization so that denominator array correctly # represents the number of injections contributing to each # bin: make w(0) = 1.0. note that this factor has no # effect on the efficiency because it is common to the # numerator and denominator arrays. we do this for the # purpose of computing the Poisson error bars, which # requires us to know the counts for the bins windowfunc = rate.gaussian_window(filter_width) windowfunc /= windowfunc[len(windowfunc) / 2 + 1] rate.filter_array(efficiency_num.array, windowfunc) rate.filter_array(efficiency_den.array, windowfunc) # regularize: adjust unused bins so that the efficiency is # 0, not NaN assert (efficiency_num.array <= efficiency_den.array).all() efficiency_den.array[(efficiency_num.array == 0) & (efficiency_den.array == 0)] = 1 line1, A50, A50_err = render_data_from_bins(file("string_efficiency.dat", "w"), axes, efficiency_num, efficiency_den, cal_uncertainty, filter_width, colour = "k", linestyle = "-", erroralpha = 0.2) # add a legend to the axes axes.legend((line1,), (r"\noindent Injections recovered with $\log \Lambda > %.2f$" % detection_threshold,), loc = "lower right") # adjust limits axes.set_xlim([3e-22, 3e-19]) axes.set_ylim([0.0, 1.0])
def finish(self, binning = None): # compute the binning if needed, and set the injections # into the numerator and denominator bins. also compute # the smoothing window's parameters. self._bin_events(binning) # smooth the efficiency data. print >>sys.stderr, "Sum of numerator bins before smoothing = %g" % self.efficiency.numerator.array.sum() print >>sys.stderr, "Sum of denominator bins before smoothing = %g" % self.efficiency.denominator.array.sum() rate.filter_binned_ratios(self.efficiency, rate.gaussian_window(self.window_size_x, self.window_size_y)) print >>sys.stderr, "Sum of numerator bins after smoothing = %g" % self.efficiency.numerator.array.sum() print >>sys.stderr, "Sum of denominator bins after smoothing = %g" % self.efficiency.denominator.array.sum() # regularize to prevent divide-by-zero errors self.efficiency.regularize()
def finish(self): self.axes.set_title( "Trigger Peak Time - Injection Peak Time\n(%d Found Injections)" % self.found) # 21 bins per filter width filter = rate.gaussian_window(21) rate.filter_array(self.offsets.array, filter) rate.filter_array(self.coinc_offsets.array, filter) self.axes.plot(self.offsets.centres()[0], self.offsets.at_centres(), "k") self.axes.plot(self.coinc_offsets.centres()[0], self.coinc_offsets.at_centres(), "r") self.axes.legend([ "%s residuals" % self.instrument, "SNR-weighted mean of residuals in all instruments" ], loc="lower right")
def finish(self): livetime = rate.BinnedArray(self.counts.bins) for offsets in self.tisi_rows: self.seglists.offsets.update(offsets) livetime[offsets[self.x_instrument], offsets[self.y_instrument]] += float(abs(self.seglists.intersection(self.seglists.keys()))) zvals = self.counts.at_centres() / livetime.at_centres() rate.filter_array(zvals, rate.gaussian_window(3, 3)) xcoords, ycoords = self.counts.centres() self.axes.contour(xcoords, ycoords, numpy.transpose(numpy.log(zvals))) for offsets in self.tisi_rows: if any(offsets): # time slide vector is non-zero-lag self.axes.plot((offsets[self.x_instrument],), (offsets[self.y_instrument],), "k+") else: # time slide vector is zero-lag self.axes.plot((offsets[self.x_instrument],), (offsets[self.y_instrument],), "r+") self.axes.set_xlim([self.counts.bins().min[0], self.counts.bins().max[0]]) self.axes.set_ylim([self.counts.bins().min[1], self.counts.bins().max[1]]) self.axes.set_title(r"Coincident Event Rate vs. Instrument Time Offset (Logarithmic Rate Contours)")
def rate_posterior_from_samples(samples): """ Construct and return a BinnedArray containing a histogram of a sequence of samples. If limits is None (default) then the limits of the binning will be determined automatically from the sequence, otherwise limits is expected to be a tuple or other two-element sequence providing the (low, high) limits, and in that case the sequence can be a generator. """ nbins = int(math.sqrt(len(samples)) / 40.) assert nbins >= 1, "too few samples to construct histogram" lo = samples.min() * (1. - nbins / len(samples)) hi = samples.max() * (1. + nbins / len(samples)) ln_pdf = rate.BinnedLnPDF( rate.NDBins((rate.LogarithmicBins(lo, hi, nbins), ))) count = ln_pdf.count # avoid attribute look-up in loop for sample in samples: count[sample, ] += 1. rate.filter_array(ln_pdf.array, rate.gaussian_window(5), use_fft=False) ln_pdf.normalize() return ln_pdf
def finish(self): self.axes.set_title( "Trigger Peak Frequency / Injection Centre Frequency\n(%d Found Injections)" % self.found) # 21 bins per filter width filter = rate.gaussian_window(21) rate.filter_array(self.offsets.array, filter) rate.filter_array(self.coinc_offsets.array, filter) self.axes.plot(10**self.offsets.centres()[0], self.offsets.at_centres(), "k") self.axes.plot(10**self.coinc_offsets.centres()[0], self.coinc_offsets.at_centres(), "r") self.axes.legend([ "%s triggers" % self.instrument, "SNR-weighted mean of all matching triggers" ], loc="lower right") ymin, ymax = self.axes.get_ylim() if ymax / ymin > 1e6: ymin = ymax / 1e6 self.axes.set_ylim((ymin, ymax))
def finish(self): for key, pdf in self.densities.items(): rate.filter_array(pdf.array, rate.gaussian_window(11, 5)) pdf.normalize() self.mkinterps()
def finish(self): self.axes.set_title( r"\begin{center}Distribution of Coincident Events (%d Foreground, %d Background Events, %d Injections Found in Coincidence, Logarithmic Density Contours)\end{center}" % (self.n_foreground, self.n_background, self.n_injections)) xcoords, ycoords = self.background_bins.centres() # prepare the data rate.filter_array(self.foreground_bins.array, rate.gaussian_window(8, 8)) rate.filter_array(self.background_bins.array, rate.gaussian_window(8, 8)) rate.filter_array(self.coinc_injection_bins.array, rate.gaussian_window(8, 8)) rate.filter_array(self.incomplete_coinc_injection_bins.array, rate.gaussian_window(8, 8)) self.foreground_bins.logregularize() self.background_bins.logregularize() self.coinc_injection_bins.logregularize() self.incomplete_coinc_injection_bins.logregularize() # plot background contours max_density = math.log(self.background_bins.array.max()) self.axes.contour(xcoords, ycoords, numpy.transpose(numpy.log( self.background_bins.array)), [max_density - n for n in xrange(0, 10, 1)], cmap=matplotlib.cm.Greys) # plot foreground (zero-lag) contours max_density = math.log(self.foreground_bins.array.max()) self.axes.contour(xcoords, ycoords, numpy.transpose(numpy.log( self.foreground_bins.array)), [max_density - n for n in xrange(0, 10, 1)], cmap=matplotlib.cm.Reds) #self.axes.plot(self.foreground_x, self.foreground_y, "r+") # plot coincident injection contours max_density = math.log(self.coinc_injection_bins.array.max()) self.axes.contour(xcoords, ycoords, numpy.transpose( numpy.log(self.coinc_injection_bins.array)), [max_density - n for n in xrange(0, 10, 1)], cmap=matplotlib.cm.Blues) # plot incomplete coincident injection contours max_density = math.log( self.incomplete_coinc_injection_bins.array.max()) self.axes.contour(xcoords, ycoords, numpy.transpose( numpy.log( self.incomplete_coinc_injection_bins.array)), [max_density - n for n in xrange(0, 10, 1)], cmap=matplotlib.cm.Greens) # fix axes limits self.axes.set_xlim([ self.background_bins.bins.min[0], self.background_bins.bins.max[0] ]) self.axes.set_ylim([ self.background_bins.bins.min[1], self.background_bins.bins.max[1] ])
def finish(self, threshold): # bin the injections self._bin_events() # use the same binning for the found injection density as # was constructed for the efficiency self.found_density = rate.BinnedArray(self.efficiency.denominator.bins) # construct the amplitude weighting function amplitude_weight = rate.BinnedArray(rate.NDBins((rate.LinearBins(threshold - 100, threshold + 100, 10001),))) # gaussian window's width is the number of bins # corresponding to 10 units of amplitude, which is computed # by dividing 10 by the "volume" of the bin corresponding # to threshold. index is the index of the element in # amplitude_weight corresponding to the threshold. index, = amplitude_weight.bins[threshold,] window = rate.gaussian_window(10.0 / amplitude_weight.bins.volumes()[index]) window /= 10 * window[(len(window) - 1) / 2] # set the window data into the BinnedArray object. the # Gaussian peaks on the middle element of the window, which # we want to place at index in the amplitude_weight array. lo = index - (len(window) - 1) / 2 hi = lo + len(window) if lo < 0 or hi > len(amplitude_weight.array): raise ValueError("amplitude weighting window too large") amplitude_weight.array[lo:hi] = window # store the recovered injections in the found density bins # weighted by amplitude for x, y, z in self.recovered_xyz: try: weight = amplitude_weight[z,] except IndexError: # beyond the edge of the window weight = 0.0 self.found_density[x, y] += weight # the efficiency is only valid up to the highest energy # that has been injected. this creates problems later on # so, instead, for each frequency, identify the highest # energy that has been measured and copy the values for # that bin's numerator and denominator into the bins for # all higher energies. do the same for the counts in the # found injection density bins. # # numpy.indices() returns two arrays array, the first of # which has each element set equal to its x index, the # second has each element set equal to its y index, we keep # the latter. meanwhile numpy.roll() cyclically permutes # the efficiency bins down one along the y (energy) axis. # from this, the conditional finds bins where the # efficiency is greater than 0.9 but <= 0.9 in the bin # immediately above it in energy. we select the elements # from the y index array where the conditional is true, and # then use numpy.max() along the y direction to return the # highest such y index for each x index, which is a 1-D # array. finally, enumerate() is used to iterate over x # index and corresponding y index, and if the y index is # not negative (was found) the value from that x-y bin is # copied to all higher bins. n = self.efficiency.numerator.array d = self.efficiency.denominator.array f = self.found_density.array bady = -1 for x, y in enumerate(numpy.max(numpy.where((d > 0) & (numpy.roll(d, -1, axis = 1) <= 0), numpy.indices(d.shape)[1], bady), axis = 1)): if y != bady: n[x, y + 1:] = n[x, y] d[x, y + 1:] = d[x, y] f[x, y + 1:] = f[x, y] # now do the same for the bins at energies below those that # have been measured. bady = d.shape[1] for x, y in enumerate(numpy.min(numpy.where((d > 0) & (numpy.roll(d, 1, axis = 1) <= 0), numpy.indices(d.shape)[1], bady), axis = 1)): if y != bady: n[x, 0:y] = n[x, y] d[x, 0:y] = d[x, y] f[x, 0:y] = f[x, y] diagnostic_plot(self.efficiency.numerator.array, self.efficiency.denominator.bins, r"Efficiency Numerator (Before Averaging)", self.amplitude_lbl, "lalapps_excesspowerfinal_efficiency_numerator_before.png") diagnostic_plot(self.efficiency.denominator.array, self.efficiency.denominator.bins, r"Efficiency Denominator (Before Averaging)", self.amplitude_lbl, "lalapps_excesspowerfinal_efficiency_denominator_before.png") diagnostic_plot(self.found_density.array, self.efficiency.denominator.bins, r"Injections Lost / Unit of Threshold (Before Averaging)", self.amplitude_lbl, "lalapps_excesspowerfinal_found_density_before.png") # smooth the efficiency bins and the found injection # density bins using the same 2D window. window = rate.gaussian_window(self.window_size_x, self.window_size_y) window /= window[tuple((numpy.array(window.shape, dtype = "double") - 1) / 2)] rate.filter_binned_ratios(self.efficiency, window) rate.filter_array(self.found_density.array, window) diagnostic_plot(self.efficiency.numerator.array, self.efficiency.denominator.bins, r"Efficiency Numerator (After Averaging)", self.amplitude_lbl, "lalapps_excesspowerfinal_efficiency_numerator_after.png") diagnostic_plot(self.efficiency.denominator.array, self.efficiency.denominator.bins, r"Efficiency Denominator (After Averaging)", self.amplitude_lbl, "lalapps_excesspowerfinal_efficiency_denominator_after.png") diagnostic_plot(self.found_density.array, self.efficiency.denominator.bins, r"Injections Lost / Unit of Threshold (After Averaging)", self.amplitude_lbl, "lalapps_excesspowerfinal_found_density_after.png") # compute the uncertainties in the efficiency and its # derivative by assuming these to be the binomial counting # fluctuations in the numerators. p = self.efficiency.ratio() self.defficiency = numpy.sqrt(p * (1 - p) / self.efficiency.denominator.array) p = self.found_density.array / self.efficiency.denominator.array self.dfound_density = numpy.sqrt(p * (1 - p) / self.efficiency.denominator.array)
fig = figure.Figure() canvas = FigureCanvas(fig) fig.set_size_inches(width, height) fig.gca().set_position([ border[0] / width, border[1] / height, (width - border[0] - border[2]) / width, (height - border[1] - border[3]) / height ]) return fig fig = newfig(options.segment) axes = fig.gca() rate.filter_array(trigger_rate.array, rate.gaussian_window(bins_per_filterwidth)) axes.plot(trigger_rate.centres()[0], trigger_rate.at_centres()) axes.set_xlim(list(options.segment)) axes.grid(True) for seg in ~seglist & segments.segmentlist([options.segment]): axes.axvspan(seg[0], seg[1], facecolor="k", alpha=0.2) axes.set_title( "%s Excess Power Trigger Rate vs. Time\n(%d Triggers, %g s Moving Average)" % (options.instrument, num_triggers, options.window)) ticks = make_xticks(options.segment) axes.set_xticks(ticks[0])
def finish(self): fig, axes = SnglBurstUtils.make_burst_plot(r"Injection Amplitude (\(\mathrm{s}^{-\frac{1}{3}}\))", "Detection Efficiency", width = 108.0) axes.set_title(r"Detection Efficiency vs.\ Amplitude") axes.semilogx() axes.set_position([0.10, 0.150, 0.86, 0.77]) # set desired yticks axes.set_yticks((0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)) axes.set_yticklabels((r"\(0\)", r"\(0.1\)", r"\(0.2\)", r"\(0.3\)", r"\(0.4\)", r"\(0.5\)", r"\(0.6\)", r"\(0.7\)", r"\(0.8\)", r"\(0.9\)", r"\(1.0\)")) axes.xaxis.grid(True, which = "major,minor") axes.yaxis.grid(True, which = "major,minor") # put made and found injections in the denominators and # numerators of the efficiency bins bins = rate.NDBins((rate.LogarithmicBins(min(sim.amplitude for sim in self.all), max(sim.amplitude for sim in self.all), 400),)) efficiency_num = rate.BinnedArray(bins) efficiency_den = rate.BinnedArray(bins) for sim in self.found: efficiency_num[sim.amplitude,] += 1 for sim in self.all: efficiency_den[sim.amplitude,] += 1 # generate and plot trend curves. adjust window function # normalization so that denominator array correctly # represents the number of injections contributing to each # bin: make w(0) = 1.0. note that this factor has no # effect on the efficiency because it is common to the # numerator and denominator arrays. we do this for the # purpose of computing the Poisson error bars, which # requires us to know the counts for the bins windowfunc = rate.gaussian_window(self.filter_width) windowfunc /= windowfunc[len(windowfunc) / 2 + 1] rate.filter_array(efficiency_num.array, windowfunc) rate.filter_array(efficiency_den.array, windowfunc) # regularize: adjust unused bins so that the efficiency is # 0, not NaN assert (efficiency_num.array <= efficiency_den.array).all() efficiency_den.array[(efficiency_num.array == 0) & (efficiency_den.array == 0)] = 1 line1, A50, A50_err = render_data_from_bins(file("string_efficiency.dat", "w"), axes, efficiency_num, efficiency_den, self.cal_uncertainty, self.filter_width, colour = "k", linestyle = "-", erroralpha = 0.2) print("Pipeline's 50%% efficiency point for all detections = %g +/- %g%%\n" % (A50, A50_err * 100), file=sys.stderr) # add a legend to the axes axes.legend((line1,), (r"\noindent Injections recovered with $\log \Lambda > %.2f$" % self.detection_threshold,), loc = "lower right") # adjust limits axes.set_xlim([3e-22, 3e-19]) axes.set_ylim([0.0, 1.0]) # # dump some information about the highest-amplitude missed # and quietest-amplitude found injections # self.loudest_missed.sort(reverse = True) self.quietest_found.sort(reverse = True) f = file("string_loud_missed_injections.txt", "w") print("Highest Amplitude Missed Injections", file=f) print("===================================", file=f) for amplitude, sim, offsetvector, filename, ln_likelihood_ratio in self.loudest_missed: print(file=f) print("%s in %s:" % (str(sim.simulation_id), filename), file=f) if ln_likelihood_ratio is None: print("Not recovered", file=f) else: print("Recovered with \\log \\Lambda = %.16g, detection threshold was %.16g" % (ln_likelihood_ratio, self.detection_threshold), file=f) for instrument in self.seglists: print("In %s:" % instrument, file=f) print("\tInjected amplitude:\t%.16g" % SimBurstUtils.string_amplitude_in_instrument(sim, instrument, offsetvector), file=f) print("\tTime of injection:\t%s s" % sim.time_at_instrument(instrument, offsetvector), file=f) print("Amplitude in waveframe:\t%.16g" % sim.amplitude, file=f) t = sim.get_time_geocent() print("Time at geocentre:\t%s s" % t, file=f) print("Segments within 60 seconds:\t%s" % segmentsUtils.segmentlistdict_to_short_string(self.seglists & segments.segmentlistdict((instrument, segments.segmentlist([segments.segment(t-offsetvector[instrument]-60, t-offsetvector[instrument]+60)])) for instrument in self.seglists)), file=f) print("Vetoes within 60 seconds:\t%s" % segmentsUtils.segmentlistdict_to_short_string(self.vetoseglists & segments.segmentlistdict((instrument, segments.segmentlist([segments.segment(t-offsetvector[instrument]-60, t-offsetvector[instrument]+60)])) for instrument in self.vetoseglists)), file=f) f = file("string_quiet_found_injections.txt", "w") print("Lowest Amplitude Found Injections", file=f) print("=================================", file=f) for inv_amplitude, sim, offsetvector, filename, ln_likelihood_ratio in self.quietest_found: print(file=f) print("%s in %s:" % (str(sim.simulation_id), filename), file=f) if ln_likelihood_ratio is None: print("Not recovered", file=f) else: print("Recovered with \\log \\Lambda = %.16g, detection threshold was %.16g" % (ln_likelihood_ratio, self.detection_threshold), file=f) for instrument in self.seglists: print("In %s:" % instrument, file=f) print("\tInjected amplitude:\t%.16g" % SimBurstUtils.string_amplitude_in_instrument(sim, instrument, offsetvector), file=f) print("\tTime of injection:\t%s s" % sim.time_at_instrument(instrument, offsetvector), file=f) print("Amplitude in waveframe:\t%.16g" % sim.amplitude, file=f) t = sim.get_time_geocent() print("Time at geocentre:\t%s s" % t, file=f) print("Segments within 60 seconds:\t%s" % segmentsUtils.segmentlistdict_to_short_string(self.seglists & segments.segmentlistdict((instrument, segments.segmentlist([segments.segment(t-offsetvector[instrument]-60, t-offsetvector[instrument]+60)])) for instrument in self.seglists)), file=f) print("Vetoes within 60 seconds:\t%s" % segmentsUtils.segmentlistdict_to_short_string(self.vetoseglists & segments.segmentlistdict((instrument, segments.segmentlist([segments.segment(t-offsetvector[instrument]-60, t-offsetvector[instrument]+60)])) for instrument in self.vetoseglists)), file=f) # # done # return fig,
def finish(self): snrsq_kernel_width_at_64 = 16., chisq_kernel_width = 0.02, sigma = 10. for key, lnpdf in self.densities.items(): if key.endswith("_snr2_chi2"): numsamples = max(lnpdf.array.sum() / 10. + 1., 1e3) snrsq_bins, chisq_bins = lnpdf.bins snrsq_per_bin_at_64 = (snrsq_bins.upper() - snrsq_bins.lower())[snrsq_bins[64.]] chisq_per_bin_at_0_02 = (chisq_bins.upper() - chisq_bins.lower())[chisq_bins[0.02]] # apply Silverman's rule so that the width scales # with numsamples**(-1./6.) for a 2D PDF snrsq_kernel_bins = snrsq_kernel_width_at_64 / snrsq_per_bin_at_64 / numsamples**( 1. / 6.) chisq_kernel_bins = chisq_kernel_width / chisq_per_bin_at_0_02 / numsamples**( 1. / 6.) # check the size of the kernel. We don't ever let # it get smaller than the 2.5 times the bin size if snrsq_kernel_bins < 2.5: snrsq_kernel_bins = 2.5 warnings.warn("Replacing snrsq kernel bins with 2.5") if chisq_kernel_bins < 2.5: chisq_kernel_bins = 2.5 warnings.warn("Replacing chisq kernel bins with 2.5") # convolve bin count with density estimation kernel rate.filter_array( lnpdf.array, rate.gaussian_window(snrsq_kernel_bins, chisq_kernel_bins, sigma=sigma)) # zero everything below the SNR cutoff. need to do the slicing # ourselves to avoid zero-ing the at-threshold bin lnpdf.array[:lnpdf.bins[0][self.snr_threshold], :] = 0. elif key.endswith("_snr2_duration"): # FIXME the duration filter kernel is left as a guess rate.filter_array( lnpdf.array, rate.gaussian_window(snrsq_kernel_bins, 11, sigma=sigma)) # zero everything below the SNR cutoff. need to do the slicing # ourselves to avoid zero-ing the at-threshold bin lnpdf.array[:lnpdf.bins[0][self.snr_threshold], :] = 0. else: # shouldn't get here raise Exception lnpdf.normalize() self.mkinterps() # # never allow PDFs that have had the density estimation # transform applied to be written to disk: on-disk files # must only ever provide raw counts. also don't allow # density estimation to be applied twice # def to_xml(*args, **kwargs): raise NotImplementedError( "writing .finish()'ed LnLRDensity object to disk is forbidden") self.to_xml = to_xml def finish(*args, **kwargs): raise NotImplementedError( ".finish()ing a .finish()ed LnLRDensity object is forbidden") self.finish = finish
weeks = float(abs(segment)) / 86400.0 / 7.0 # FIXME: leep seconds? border = (0.5, 0.75, 0.125, 0.625) # inches width = max(10.0, weeks * 3.0 + border[0] + border[2]) # inches height = 8.0 # inches fig = figure.Figure() canvas = FigureCanvas(fig) fig.set_size_inches(width, height) fig.gca().set_position([border[0] / width, border[1] / height, (width - border[0] - border[2]) / width, (height - border[1] - border[3]) / height]) return fig fig = newfig(options.segment) axes = fig.gca() rate.filter_array(trigger_rate.array, rate.gaussian_window(bins_per_filterwidth)) axes.plot(trigger_rate.centres()[0], trigger_rate.at_centres()) axes.set_xlim(list(options.segment)) axes.grid(True) for seg in ~seglist & segments.segmentlist([options.segment]): axes.axvspan(seg[0], seg[1], facecolor = "k", alpha = 0.2) axes.set_title("%s Excess Power Trigger Rate vs. Time\n(%d Triggers, %g s Moving Average)" % (options.instrument, num_triggers, options.window))
def SNRPDF(instruments, horizon_distances, snr_cutoff, n_samples=200000, bins=rate.ATanLogarithmicBins(3.6, 1e3, 150)): """ Precomputed SNR PDF for each detector Returns a BinnedArray containing P(snr_{inst1}, snr_{inst2}, ... | signal seen in exactly {inst1, inst2, ...} in a network of instruments with a given set of horizon distances) i.e., the joint probability density of observing a set of SNRs conditional on them being the result of signal that has been recovered in a given subset of the instruments in a network of instruments with a given set of horizon distances. The axes of the PDF correspond to the instruments in alphabetical order. The binning used for all axes is set with the bins parameter. The n_samples parameter sets the number of iterations for the internal Monte Carlo sampling loop. """ if n_samples < 1: raise ValueError("n_samples=%d must be >= 1" % n_samples) # get instrument names instruments = sorted(instruments) if len(instruments) < 1: raise ValueError(instruments) # get the horizon distances in the same order DH_times_8 = 8. * numpy.array( [horizon_distances[inst] for inst in instruments]) # get detector responses in the same order resps = [ lalsimulation.DetectorPrefixToLALDetector(str(inst)).response for inst in instruments ] # get horizon distances and responses of remaining # instruments (order doesn't matter as long as they're in # the same order) DH_times_8_other = 8. * numpy.array([ dist for inst, dist in horizon_distances.items() if inst not in instruments ]) resps_other = tuple( lalsimulation.DetectorPrefixToLALDetector(str(inst)).response for inst in horizon_distances if inst not in instruments) # initialize the PDF array, and pre-construct the sequence of # snr, d(snr) tuples. since the last SNR bin probably has # infinite size, we remove it from the sequence # (meaning the PDF will be left 0 in that bin) pdf = rate.BinnedArray(rate.NDBins([bins] * len(instruments))) snr_sequence = rate.ATanLogarithmicBins(3.6, 1e3, 500) snr_snrlo_snrhi_sequence = numpy.array( zip(snr_sequence.centres(), snr_sequence.lower(), snr_sequence.upper())[:-1]) # compute the SNR at which to begin iterations over bins assert type(snr_cutoff) is float snr_min = snr_cutoff - 3.0 assert snr_min > 0.0 # we select random uniformly-distributed right assensions # so there's no point in also choosing random GMSTs and any # vlaue is as good as any other gmst = 0.0 # run the sample the requested # of iterations. save some # symbols to avoid doing module attribute look-ups in the # loop acos = math.acos random_uniform = random.uniform twopi = 2. * math.pi pi_2 = math.pi / 2. xlal_am_resp = lal.ComputeDetAMResponse # FIXME: scipy.stats.rice.rvs broken on reference OS. # switch to it when we can rely on a new-enough scipy #rice_rvs = stats.rice.rvs # broken on reference OS # the .reshape is needed in the event that x is a 1x1 # array: numpy returns a scalar from sqrt(), but we must # have something that we can iterate over rice_rvs = lambda x: numpy.sqrt(stats.ncx2.rvs(2., x**2.)).reshape(x.shape) for i in xrange(n_samples): # select random sky location and source orbital # plane inclination # the signal is linearly polaraized, and h_cross = 0 # is assumed, so we need only F+ (its absolute value). ra = random_uniform(0., twopi) dec = pi_2 - acos(random_uniform(-1., 1.)) psi = random_uniform(0., twopi) fplus = tuple( abs(xlal_am_resp(resp, ra, dec, psi, gmst)[0]) for resp in resps) # 1/8 ratio of inverse SNR to distance for each instrument # (1/8 because horizon distance is defined for an SNR of 8, # and we've omitted that factor for performance) snr_times_D = DH_times_8 * fplus # snr * D in instrument whose SNR grows fastest # with decreasing D max_snr_times_D = snr_times_D.max() # snr_times_D.min() / snr_min = the furthest a # source can be and still be above snr_min in all # instruments involved. max_snr_times_D / that # distance = the SNR that distance corresponds to # in the instrument whose SNR grows fastest with # decreasing distance --- the SNR the source has in # the most sensitive instrument when visible to all # instruments in the combo try: start_index = snr_sequence[max_snr_times_D / (snr_times_D.min() / snr_min)] except ZeroDivisionError: # one of the instruments that must be able # to see the event is blind to it continue # min_D_other is minimum distance at which source # becomes visible in an instrument that isn't # involved. max_snr_times_D / min_D_other gives # the SNR in the most sensitive instrument at which # the source becomes visible to one of the # instruments not allowed to participate if len(DH_times_8_other): min_D_other = (DH_times_8_other * fplus).min() / snr_cutoff try: end_index = snr_sequence[max_snr_times_D / min_D_other] + 1 except ZeroDivisionError: # all instruments that must not see # it are blind to it end_index = None else: # there are no other instruments end_index = None # if start_index >= end_index then in order for the # source to be close enough to be visible in all # the instruments that must see it it is already # visible to one or more instruments that must not. # don't need to check for this, the for loop that # comes next will simply not have any iterations. # iterate over the nominal SNRs (= noise-free SNR # in the most sensitive instrument) at which we # will add weight to the PDF. from the SNR in # most sensitive instrument, the distance to the # source is: # # D = max_snr_times_D / snr # # and the (noise-free) SNRs in all instruments are: # # snr_times_D / D # # scipy's Rice-distributed RV code is used to # add the effect of background noise, converting # the noise-free SNRs into simulated observed SNRs # # number of sources b/w Dlo and Dhi: # # d count \propto D^2 |dD| # count \propto Dhi^3 - Dlo**3 D_Dhi_Dlo_sequence = max_snr_times_D / snr_snrlo_snrhi_sequence[ start_index:end_index] for snr, weight in zip( rice_rvs(snr_times_D / numpy.reshape(D_Dhi_Dlo_sequence[:, 0], (len(D_Dhi_Dlo_sequence), 1))), D_Dhi_Dlo_sequence[:, 1]**3. - D_Dhi_Dlo_sequence[:, 2]**3.): pdf[tuple(snr)] += weight # check for divide-by-zeros that weren't caught. also # finds nans if they are there assert numpy.isfinite(pdf.array).all() # convolve samples with gaussian kernel rate.filter_array(pdf.array, rate.gaussian_window(*(1.875, ) * len(pdf.array.shape))) # protect against round-off in FFT convolution leading to # negative valuesin the PDF numpy.clip(pdf.array, 0., PosInf, pdf.array) # zero counts in bins that are below the trigger threshold. # have to convert SNRs to indexes ourselves and adjust so # that we don't zero the bin in which the SNR threshold # falls range_all = slice(None, None) range_low = slice(None, pdf.bins[0][snr_cutoff]) for i in xrange(len(instruments)): slices = [range_all] * len(instruments) slices[i] = range_low # convert bin counts to normalized PDF pdf.to_pdf() # one last sanity check assert numpy.isfinite(pdf.array).all() # done return pdf