def check(self, triggers, data_reader, **kwds): """ Look for a single detector trigger that passes the thresholds in the current data. """ if len(triggers['snr']) == 0: return None i = triggers['snr'].argmax() # This uses the pycbc live convention of chisq always meaning the # reduced chisq. rchisq = triggers['chisq'][i] nsnr = newsnr(triggers['snr'][i], rchisq) dur = triggers['template_duration'][i] if (nsnr > self.newsnr_threshold and rchisq < self.reduced_chisq_threshold and dur > self.duration_threshold): d = {key: triggers[key][i] for key in triggers} d['stat'] = nsnr d['ifar'] = self.fixed_ifar return SingleForGraceDB( self.ifo, d, hardware_injection=data_reader.near_hwinj(), **kwds) else: return None
def check(self, triggers, data_reader, **kwds): """ Look for a single detector trigger that passes the thresholds in the current data. """ if len(triggers['snr']) == 0: return None i = triggers['snr'].argmax() # This uses the pycbc live convention of chisq always meaning the # reduced chisq. rchisq = triggers['chisq'][i] nsnr = newsnr(triggers['snr'][i], rchisq) dur = triggers['template_duration'][i] if nsnr > self.newsnr_threshold and \ rchisq < self.reduced_chisq_threshold and \ dur > self.duration_threshold: d = {key: triggers[key][i] for key in triggers} d['stat'] = nsnr d['ifar'] = self.fixed_ifar return SingleForGraceDB(self.ifo, d, hardware_injection=data_reader.near_hwinj(), **kwds) else: return None
def xis(snr, template, strain, psd_o, fc, grafica, time_max, duration, hc, shift, time_trigger): nbins = 16 chisq = power_chisq(template, strain, nbins, psd_o, low_frequency_cutoff=fc, high_frequency_cutoff=hc) chisq = chisq.crop(4, 4) dof = nbins * 2 - 2 chisq /= dof nsnr = newsnr(snr, chisq) maximo = max(nsnr) print 'maximo ', abs(maximo) MaxInd = np.argmax(nsnr) time = snr.sample_times[MaxInd] if grafica == 1: pylab.figure('Superposition') pylab.plot(snr.sample_times, abs(nsnr), label='Re-weighted SNR') #pylab.plot(snr.sample_times,abs(snr),label='Original SNR') #pylab.title('Re-weighted SNR') #pylab.ylabel('Signal-to-noise ratio') pylab.xlabel('Time(s)') pylab.legend() #pylab.xlim(time_max-0.05, time_max+0.05) pylab.grid() pylab.show() pylab.figure('Re-weighted SNR') pylab.plot(snr.sample_times, abs(nsnr)) pylab.grid() pylab.show() return maximo, time, nsnr
def check(self, triggers, data_reader): """ Look for a single detector trigger that passes the thresholds in the current data. """ if len(triggers['snr']) == 0: return None i = triggers['snr'].argmax() # This uses the pycbc live convention of chisq always meaning the # reduced chisq. rchisq = triggers['chisq'][i] nsnr = newsnr(triggers['snr'][i], rchisq) dur = triggers['template_duration'][i] if nsnr > self.newsnr_threshold and \ rchisq < self.reduced_chisq_threshold and \ dur > self.duration_threshold: fake_coinc = {'foreground/%s/%s' % (self.ifo, k): triggers[k][i] for k in triggers} fake_coinc['foreground/stat'] = nsnr fake_coinc['foreground/ifar'] = self.fixed_ifar fake_coinc['HWINJ'] = data_reader.near_hwinj() return fake_coinc return None
def newsnr(self): return events.newsnr(self.snr, self.rchisq)
def values(self, stilde, template, psd, snrv, snr_norm, bchisq, bchisq_dof, indices): """ Calculate sine-Gaussian chisq Parameters ---------- stilde: pycbc.types.Frequencyseries The overwhitened strain template: pycbc.types.Frequencyseries The waveform template being analyzed psd: pycbc.types.Frequencyseries The power spectral density of the data snrv: numpy.ndarray The peak unnormalized complex SNR values snr_norm: float The normalization factor for the snr bchisq: numpy.ndarray The Bruce Allen power chisq values for these triggers bchisq_dof: numpy.ndarray The degrees of freedom of the Bruce chisq indics: numpy.ndarray The indices of the snr peaks. Returns ------- chisq: Array Chisq values, one for each sample index """ if not self.do: return None if template.params.template_hash not in self.params: return numpy.ones(len(snrv)) values = self.params[template.params.template_hash].split(',') # Get the chisq bins to use as the frequency reference point bins = self.cached_chisq_bins(template, psd) # This is implemented slowly, so let's not call it often, OK? chisq = numpy.ones(len(snrv)) for i, snrvi in enumerate(snrv): #Skip if newsnr too low snr = abs(snrvi * snr_norm) nsnr = newsnr(snr, bchisq[i] / bchisq_dof[i]) if nsnr < self.snr_threshold: continue N = (len(template) - 1) * 2 dt = 1.0 / (N * template.delta_f) kmin = int(template.f_lower / psd.delta_f) time = float(template.epoch) + dt * indices[i] # Shift the time of interest to be centered on 0 stilde_shift = apply_fseries_time_shift(stilde, -time) # Only apply the sine-Gaussian in a +-50 Hz range around the # central frequency qwindow = 50 chisq[i] = 0 # Estimate the maximum frequency up to which the waveform has # power by approximating power per frequency # as constant over the last 2 chisq bins. We cannot use the final # chisq bin edge as it does not have to be where the waveform # terminates. fstep = (bins[-2] - bins[-3]) fpeak = (bins[-2] + fstep) * template.delta_f # This is 90% of the Nyquist frequency of the data # This allows us to avoid issues near Nyquist due to resample # Filtering fstop = len(stilde) * stilde.delta_f * 0.9 dof = 0 # Calculate the sum of SNR^2 for the sine-Gaussians specified for descr in values: # Get the q and frequency offset from the descriptor q, offset = descr.split('-') q, offset = float(q), float(offset) fcen = fpeak + offset flow = max(kmin * template.delta_f, fcen - qwindow) fhigh = fcen + qwindow # If any sine-gaussian tile has an upper frequency near # nyquist return 1 instead. if fhigh > fstop: return numpy.ones(len(snrv)) kmin = int(flow / template.delta_f) kmax = int(fhigh / template.delta_f) #Calculate sine-gaussian tile gtem = sinegauss.fd_sine_gaussian(1.0, q, fcen, flow, len(template) * template.delta_f, template.delta_f).astype(numpy.complex64) gsigma = sigma(gtem, psd=psd, low_frequency_cutoff=flow, high_frequency_cutoff=fhigh) #Calculate the SNR of the tile gsnr = (gtem[kmin:kmax] * stilde_shift[kmin:kmax]).sum() gsnr *= 4.0 * gtem.delta_f / gsigma chisq[i] += abs(gsnr)**2.0 dof += 2 if dof == 0: chisq[i] = 1 else: chisq[i] /= dof return chisq
def xis(snr,template,strain,psd_o,fc,grafica,time_max,duration,hc,shift,time_trigger): nbins = 32 start_time = strain.start_time end_time = strain.start_time+32 chisq = power_chisq(template, strain, nbins, psd_o, low_frequency_cutoff=fc,high_frequency_cutoff=hc) if grafica == 1: print(start_time) print(end_time) print(time_max) print(strain.delta_f) print(template.delta_f) print(psd_o.delta_f) crop_left = abs(start_time - time_max)-1.98 crop_right = abs(end_time - time_max)-1.98 print(crop_left) print(crop_right) chisq = chisq.crop(crop_left, crop_right) dof = 2*nbins-2 chisq /= dof low_chisq = abs(chisq).numpy().argmin() if grafica == 0: chisq = chisq.crop(4,4) dof = 2*nbins-2 chisq /= dof low_chisq = chisq[time_trigger] print('Lower value of Chi') print(low_chisq) if grafica == 1: nsnr=newsnr(snr,chisq) pylab.plot(chisq.sample_times,chisq) pylab.ylabel('$chi^2_r$') pylab.xlabel('Time(s)') pylab.xlim(time_max-0.1, time_max+0.1) pylab.grid() pylab.show() pylab.figure('Superposition',figsize=[15,5]) pylab.plot(snr.sample_times,abs(nsnr),label='Re-weighted SNR') pylab.plot(snr.sample_times,abs(snr),label='Original SNR') pylab.plot(chisq.sample_times,chisq,label='$Chi^2$') #pylab.title('Re-weighted SNR') #pylab.ylabel('Signal-to-noise ratio') pylab.xlabel('Time(s)') pylab.legend() #pylab.xlim(time_max-0.05, time_max+0.05) pylab.grid() pylab.show() pylab.plot(snr.sample_times,abs(snr)) pylab.title('Matched filter output') pylab.ylabel('Signal-to-noise ratio') pylab.xlabel('Time(s)') pylab.xlim(time_max-0.1, time_max+0.1) pylab.grid() pylab.show() return chisq, low_chisq
def values(self, stilde, template, psd, snrv, snr_norm, bchisq, bchisq_dof, indices): """ Calculate sine-Gaussian chisq Parameters ---------- stilde: pycbc.types.Frequencyseries The overwhitened strain template: pycbc.types.Frequencyseries The waveform template being analyzed psd: pycbc.types.Frequencyseries The power spectral density of the data snrv: numpy.ndarray The peak unnormalized complex SNR values snr_norm: float The normalization factor for the snr bchisq: numpy.ndarray The Bruce Allen power chisq values for these triggers bchisq_dof: numpy.ndarray The degrees of freedom of the Bruce chisq indics: numpy.ndarray The indices of the snr peaks. Returns ------- chisq: Array Chisq values, one for each sample index """ if not self.do: return None if template.params.template_hash not in self.params: return numpy.ones(len(snrv)) values = self.params[template.params.template_hash].split(',') # Get the chisq bins to use as the frequency reference point bins = self.cached_chisq_bins(template, psd) # This is implemented slowly, so let's not call it often, OK? chisq = numpy.ones(len(snrv)) for i, snrvi in enumerate(snrv): #Skip if newsnr too low snr = abs(snrvi * snr_norm) nsnr = newsnr(snr, bchisq[i] / bchisq_dof[i]) if nsnr < self.snr_threshold: continue N = (len(template) - 1) * 2 dt = 1.0 / (N * template.delta_f) kmin = int(template.f_lower / psd.delta_f) time = float(template.epoch) + dt * indices[i] # Shift the time of interest to be centered on 0 stilde_shift = apply_fseries_time_shift(stilde, -time) # Only apply the sine-Gaussian in a +-50 Hz range around the # central frequency qwindow = 50 chisq[i] = 0 # Estimate the maximum frequency up to which the waveform has # power by approximating power per frequency # as constant over the last 2 chisq bins. We cannot use the final # chisq bin edge as it does not have to be where the waveform # terminates. fstep = (bins[-2] - bins[-3]) fpeak = (bins[-2] + fstep) * template.delta_f # This is 90% of the Nyquist frequency of the data # This allows us to avoid issues near Nyquist due to resample # Filtering fstop = len(stilde) * stilde.delta_f * 0.9 dof = 0 # Calculate the sum of SNR^2 for the sine-Gaussians specified for descr in values: # Get the q and frequency offset from the descriptor q, offset = descr.split('-') q, offset = float(q), float(offset) fcen = fpeak + offset flow = max(kmin * template.delta_f, fcen - qwindow) fhigh = fcen + qwindow # If any sine-gaussian tile has an upper frequency near # nyquist return 1 instead. if fhigh > fstop: return numpy.ones(len(snrv)) kmin = int(flow / template.delta_f) kmax = int(fhigh / template.delta_f) #Calculate sine-gaussian tile gtem = sinegauss.fd_sine_gaussian( 1.0, q, fcen, flow, len(template) * template.delta_f, template.delta_f).astype(numpy.complex64) gsigma = sigma(gtem, psd=psd, low_frequency_cutoff=flow, high_frequency_cutoff=fhigh) #Calculate the SNR of the tile gsnr = (gtem[kmin:kmax] * stilde_shift[kmin:kmax]).sum() gsnr *= 4.0 * gtem.delta_f / gsigma chisq[i] += abs(gsnr)**2.0 dof += 2 if dof == 0: chisq[i] = 1 else: chisq[i] /= dof logging.info('Found chisq %s', chisq[i]) return chisq
gs2 = t2['L1/sigmasq'][:][tid2] ** 0.5 vs1 = t1['H1/snr'][:][tid1] vs2 = t2['L1/snr'][:][tid2] #pd = numpy.append(pd, (t1['H1/coa_phase'][:][tid1] - t2['L1/coa_phase'][:][tid2]) % (numpy.pi * 2.0)) #td = numpy.append(td, (t1['H1/end_time'][:][tid1] - t2['L1/end_time'][:][tid2])) rd = numpy.append(rd, ifar/ifar2) md = numpy.append(md, (vs1**2.0 + vs2**2.0) ** 0.5) g1 = numpy.append(g1, gs1 / vs1) g2 = numpy.append(g2, gs2 / vs2) v1 = numpy.append(v1, vs1) v2 = numpy.append(v2, vs2) pv = numpy.append(pv, stat**2.0 - newsnr(vs1, c1)**2.0 - newsnr(vs2, c2)**2.0) stat_diff = numpy.append(stat_diff, abs(stat2 - stat)) #print pv #print v1[0:3], v2[0:3], o1[0:3], o2[0:3] print "SIZE", len(o1) print "Total number found with reference IFAR >1 and comparison IFAR <1000: {0}".format(total_found) print "Total number found (at all) in reference but missed in comparison: {0}".format(total_missed) df = ifs / ifs2 s = df.argsort()[::-1] #o1 = o1[s] #o2 = o2[s] df = df[s]
opt_snr_final = 1. / (opt_snr[injection_index_injc]**2) dist_injc = dist_injc[injection_index_injc] del ifar_injc f_inj = hf_inj[ifo] time_inj = f_inj['end_time'][:] snr_inj = f_inj['snr'][:] template_dur_inj = f_inj['template_duration'][:] template_id_inj = f_inj['template_id'][:] chisq_inj = f_inj['chisq'][:] chisq_dof_inj = f_inj['chisq_dof'][:] rchisq_inj = chisq_inj / (2 * chisq_dof_inj - 2) del chisq_inj del chisq_dof_inj newsnr_inj = events.newsnr(snr_inj, rchisq_inj) del rchisq_inj time = f['end_time'][:] if args.just_inj == 'False': snr = f['snr'][:] template_dur = f['template_duration'][:] template_id = f['template_id'][:] chisq = f['chisq'][:] chisq_dof = f['chisq_dof'][:] rchisq = chisq / (2 * chisq_dof - 2) del chisq del chisq_dof newsnr = events.newsnr(snr, rchisq) del rchisq
ifo = args.ifo tag = args.user_tag #Downloading all triggers from pycbc import events, init_logging, pnutils hf_all = h5py.File('H1-HDF_TRIGGER_MERGE_NSBH02_INJ-1128299417-1083600.hdf', 'r') f_all = hf_all['H1'] time_all = f_all['end_time'][:] snr_all = f_all['snr'][:] chisq = f_all['chisq'][:] chisq_dof = f_all['chisq_dof'][:] rchisq = chisq / (2 * chisq_dof - 2) del chisq del chisq_dof newsnr_all = events.newsnr(snr_all, rchisq) del rchisq f = h5py.File(args.stat_file, 'r') newsnr = f['%s/maxnewsnr' % ifo][:] maxsnr = f['%s/maxsnr' % ifo][:] time = f['%s/time' % ifo][:] time_inj = f['%s/time_inj' % ifo][:] count = f['%s/count' % ifo][:] margl = f['%s/marg_l' % ifo][:] delT = f['%s/delT' % ifo][:] delta_chirp = f['%s/delta_chirp' % ifo][:] newsnr_inj = f['%s/maxnewsnr_inj' % ifo][:] maxsnr_inj = f['%s/maxsnr_inj' % ifo][:] count_inj = f['%s/count_inj' % ifo][:] margl_inj = f['%s/marg_l_inj' % ifo][:]
def xis(snr, template, strain, psd, fc, grafica, time_max, duration, hc, shift): nbins = 32 start_time = strain.start_time end_time = strain.start_time + 32 #crop_left = abs(start_time - time_max-5) #crop_right = abs(end_time - time_max-5) #strain = strain.crop(crop_left,crop_right) #psd = psd.crop(crop_left,crop_right) chisq = power_chisq(template, strain, nbins, psd, low_frequency_cutoff=fc, high_frequency_cutoff=hc) chisq = chisq.crop(4, 4) dof = 2 * nbins - 2 chisq /= dof low_chisq = min(chisq) #print 'Lower value of Chi' #print low_chisq if grafica == 1: nsnr = newsnr(snr, chisq) pylab.plot(chisq.sample_times, chisq) pylab.ylabel('$chi^2_r$') pylab.xlabel('Time(s)') pylab.xlim(time_max - 0.1, time_max + 0.1) pylab.grid() pylab.show() pylab.figure('Superposition', figsize=[15, 5]) pylab.plot(snr.sample_times, abs(nsnr), label='Re-weighted SNR') pylab.plot(snr.sample_times, abs(snr), label='Original SNR') pylab.plot(chisq.sample_times, chisq, label='$Chi^2$') #pylab.title('Re-weighted SNR') #pylab.ylabel('Signal-to-noise ratio') pylab.xlabel('Time(s)') pylab.legend() pylab.xlim(time_max - 0.05, time_max + 0.05) pylab.grid() pylab.show() pylab.plot(snr.sample_times, abs(snr)) pylab.title('Matched filter output') pylab.ylabel('Signal-to-noise ratio') pylab.xlabel('Time(s)') pylab.xlim(time_max - 0.1, time_max + 0.1) pylab.grid() pylab.show() return chisq, low_chisq