def grand_tour(gps, trgs, trgtype, windows, conf=0.68, plot=False): """ a function that compute the grand tour statistics """ if trgtype == "kw": col = event.col_kw snrkey = 'signif' elif trgtype == "Omicron": col = event.col_snglBurst snrkey = 'snr' else: raise ValueError("do not understand trgtype=%s" % (trgtype)) figax = [] for win in windows: print "window=%.6f" % win ### downelect to only this window trgs = [ trg for trg in trgs if (trg[col['tcent']] >= gps - win) and ( trg[col['tcent']] <= gps + win) ] Pvalue = np.infty Snr = None Dt = None Frq = None for snrThr in sorted(list(set([trg[col[snrkey]] for trg in trgs ]))): ### iterate over all snrs present print "snrThr=%.6f" % snrThr ctrgs = [trg for trg in trgs if trg[snrkey] >= snrThr] n = len(ctrgs) ### number of triggers r = 0.5 * n / opts.window ### point estimate of the rate if n: dt = np.array([ctrgs[col['tcent']] for trg in ctrgs]) - gps arg = np.argmin(np.abs(dt)) snr = ctrgs[arg][col[snrkey]] frq = ctrgs[arg][col['fcent']] min_dt = dt[arg] absmin_dt = abs(min_dt) else: min_dt = win snr = None frq = None if r > 0: pvalue = 1 - np.exp( -r * 2 * absmin_dt ) ### cumulative probability of observing min_dt <= observed min_dt | estimated rate, the factor of 2 comes from looking on either side of the specified gps time else: pvalue = 1 ### limit is not great here...need to add CI print "\n\tchannel=%s\n\t-> Ntrg=%d\n\t-> rate=%.9e Hz\n\t-> min_dt=%.9e sec\n\t-> pvalue=%.9e" % ( chan, n, r, min_dt, pvalue) r_l, r_h = np.array(gci.poisson_bs(conf, n)) * 0.5 / win pvalue_l = 1 - np.exp(-r_l * 2 * absmin_dt) pvalue_h = 1 - np.exp(-r_h * 2 * absmin_dt) print "\t-> %.5e confidence:\n\t\tlow rate =%.9e Hz\n\t\thigh rate=%.9e Hz\n\t\tlow pvalue =%.9e\n\t\thigh pvalue=%.9e" % ( conf, r_l, r_h, pvalue_l, pvalue_h) if pvalue_h < Pvalue: pvalue = pvalue_h Snr = snr Dt = dt Frq = frq ### separate plot for each window ### xaxis : time (relative to gps) ### yaxis SNR ### color = pvalue (upper limit?) if plot: fig = plt.figure() ax = plt.subplot(1, 1, 1) figax.append((fig, ax)) ### plot all triggers # dts = np.array([ trg[col['tcent'] for trg in trgs ]) - gps # snrs = [ trg[col[snrkey]] for trg in trgs ] # frqs = [ trg[col['fcent']] for trg in trgs ] for trg in trgs: dt = trg[col['tcent']] - gps snr = trg[col[snrkey]] frq = trg[col['fcent']] color = snr_map(snr) ax.plot(dt, frq, markerfacecolor=color, markeredgecolor='none', marker='o', linestyle='none', alpha=0.50, markersize=2) ax.set_xlabel('time relative to %.6f [sec]' % gps) ax.set_ylabel('frequency [Hz]') if Dt: color = snr_map(Snr) ax.plot(Dt, Frq, markerfacecolor='none', markeredgecolor=color, marker='o', linestyle='none', alpha=1.00, markersize=5) ax.text(Dt, Frq, '%.3e' % Pvalue, ha='left', va='center') ax.set_xlim(xmin=-win, xmax=win) ymax = 1 maxfrq = max([trg[col['fcent']] for trg in trgs]) while maxfrq > ymax: ymax *= 2 ax.set_ylim(ymin=0, ymax=ymax) return figax
min_dt = dt[arg] else: min_dt = opts.window arg = None absmin_dt = abs(min_dt) if r > 0: pvalue = 1 - np.exp( -r * 2 * absmin_dt ) ### cumulative probability of observing min_dt <= observed min_dt | estimated rate, the factor of 2 comes from looking on either side of the specified gps time else: pvalue = 1 ### limit is not great here...need to add CI if opts.confidence_intervals: if n < opts.nmax: r_l, r_h = np.array(gci.poisson_bs(conf, n)) * 0.5 / (opts.window - opts.exclude) else: s = n ** 0.5 * ( scipy.special.erfinv(conf) * 2 ** 0.5 ) ### the size of the standard deviation times the number of standard deviations needed to cover conf r_l = max(0, (n - s) * 0.5 / (opts.window - opts.exclude)) r_h = (n + s) * 0.5 / (opts.window - opts.exclude) pvalue_l = 1 - np.exp(-r_l * 2 * absmin_dt) pvalue_h = 1 - np.exp(-r_h * 2 * absmin_dt) if pvalue <= opts.pvalue_print_thr: print "\n\tchannel=%s\n\t-> Ntrg=%d\n\t-> rate=%.9e Hz\n\t-> min_dt=%.9e sec" % (chan, n, r, min_dt) if arg != None: trg = trgdict[chan][arg]
min_dt = dt[arg] else: min_dt = opts.window arg = None absmin_dt = abs(min_dt) if r > 0: pvalue = 1 - np.exp( -r * 2 * absmin_dt ) ### cumulative probability of observing min_dt <= observed min_dt | estimated rate, the factor of 2 comes from looking on either side of the specified gps time else: pvalue = 1 ### limit is not great here...need to add CI if opts.confidence_intervals: if n < opts.nmax: r_l, r_h = np.array(gci.poisson_bs( conf, n)) * 0.5 / (opts.window - opts.exclude) else: s = n**0.5 * ( scipy.special.erfinv(conf) * 2**0.5 ) ### the size of the standard deviation times the number of standard deviations needed to cover conf r_l = max(0, (n - s) * 0.5 / (opts.window - opts.exclude)) r_h = (n + s) * 0.5 / (opts.window - opts.exclude) pvalue_l = 1 - np.exp(-r_l * 2 * absmin_dt) pvalue_h = 1 - np.exp(-r_h * 2 * absmin_dt) if (pvalue <= opts.pvalue_print_thr): print "\n\tchannel=%s\n\t-> Ntrg=%d\n\t-> rate=%.9e Hz\n\t-> min_dt=%.9e sec" % ( chan, n, r, min_dt) if arg != None:
def grand_tour(gps, trgs, trgtype, windows, conf=0.68, plot=False): """ a function that compute the grand tour statistics """ if trgtype == "kw": col = event.col_kw snrkey = 'signif' elif trgtype == "Omicron": col = event.col_snglBurst snrkey = 'snr' else: raise ValueError("do not understand trgtype=%s"%(trgtype)) figax = [] for win in windows: print "window=%.6f"%win ### downelect to only this window trgs = [ trg for trg in trgs if (trg[col['tcent']] >= gps-win) and (trg[col['tcent']] <= gps+win) ] Pvalue = np.infty Snr = None Dt = None Frq = None for snrThr in sorted(list(set([ trg[col[snrkey]] for trg in trgs ]))): ### iterate over all snrs present print "snrThr=%.6f"%snrThr ctrgs = [ trg for trg in trgs if trg[snrkey] >= snrThr ] n = len( ctrgs ) ### number of triggers r = 0.5*n/opts.window ### point estimate of the rate if n: dt = np.array([ctrgs[col['tcent']] for trg in ctrgs]) - gps arg = np.argmin(np.abs(dt)) snr = ctrgs[arg][col[snrkey]] frq = ctrgs[arg][col['fcent']] min_dt = dt[arg] absmin_dt = abs(min_dt) else: min_dt = win snr = None frq = None if r > 0: pvalue = 1 - np.exp(-r*2*absmin_dt) ### cumulative probability of observing min_dt <= observed min_dt | estimated rate, the factor of 2 comes from looking on either side of the specified gps time else: pvalue = 1 ### limit is not great here...need to add CI print "\n\tchannel=%s\n\t-> Ntrg=%d\n\t-> rate=%.9e Hz\n\t-> min_dt=%.9e sec\n\t-> pvalue=%.9e"%(chan, n, r, min_dt, pvalue) r_l, r_h = np.array( gci.poisson_bs(conf, n) ) * 0.5 / win pvalue_l = 1 - np.exp(-r_l*2*absmin_dt) pvalue_h = 1 - np.exp(-r_h*2*absmin_dt) print "\t-> %.5e confidence:\n\t\tlow rate =%.9e Hz\n\t\thigh rate=%.9e Hz\n\t\tlow pvalue =%.9e\n\t\thigh pvalue=%.9e"%(conf, r_l, r_h, pvalue_l, pvalue_h) if pvalue_h < Pvalue: pvalue = pvalue_h Snr = snr Dt = dt Frq = frq ### separate plot for each window ### xaxis : time (relative to gps) ### yaxis SNR ### color = pvalue (upper limit?) if plot: fig = plt.figure() ax = plt.subplot(1,1,1) figax.append( (fig, ax) ) ### plot all triggers # dts = np.array([ trg[col['tcent'] for trg in trgs ]) - gps # snrs = [ trg[col[snrkey]] for trg in trgs ] # frqs = [ trg[col['fcent']] for trg in trgs ] for trg in trgs: dt = trg[col['tcent']] - gps snr = trg[col[snrkey]] frq = trg[col['fcent']] color = snr_map( snr ) ax.plot( dt, frq, markerfacecolor=color, markeredgecolor='none', marker='o', linestyle='none', alpha=0.50, markersize=2 ) ax.set_xlabel( 'time relative to %.6f [sec]'%gps ) ax.set_ylabel( 'frequency [Hz]' ) if Dt: color = snr_map( Snr ) ax.plot( Dt, Frq, markerfacecolor='none', markeredgecolor=color, marker='o', linestyle='none', alpha=1.00, markersize=5 ) ax.text( Dt, Frq, '%.3e'%Pvalue, ha='left', va='center' ) ax.set_xlim(xmin=-win, xmax=win) ymax = 1 maxfrq = max( [ trg[col['fcent']] for trg in trgs ] ) while maxfrq > ymax: ymax *= 2 ax.set_ylim(ymin=0, ymax=ymax) return figax