def _read_omicron_trigs(omicron_files, span):
    trigs1 = SnglBurstTable.read(omicron_files[0], format='ligolw')
    trigs = table.new_from_template(trigs1)
    for omicron_file in omicron_files:
        trigs2 = table.new_from_template(trigs1)
        trigs2 = SnglBurstTable.read(omicron_file, format='ligolw')
        trigs.extend(filter(lambda row: row.get_peak() in span, trigs2))
    times = []

    print('%d found' % len(trigs))
    times = trigs.get_peak()
    s = times.size
    return trigs, times, s
Exemple #2
0
def vetoed(self, seglist, time_slide_table=None):
    """Return a MultiInspiralTable with those row from self lying
    inside (i.e. vetoed by) any elements of seglist.

    If time_slide_table is not None, any time shifts will be undone and each
    detector checked individually
    """
    seglist = type(seglist)(map(float, seglist))
    vetoed = table.new_from_template(self)
    if time_slide_table:
        slides = time_slide_table.as_dict()
        for id_, vector in slides.iteritems():
            idx = str(id_).split(":")[-1]
            slides["multi_inspiral:time_slide_id:%s" % idx] = vector
            del slides[id_]
        slides = get_slide_vectors(time_slide_table)
        for row in self:
            ifos = row.get_ifos()
            for i, ifo in enumerate(ifos):
                ifo_time = (float(row.get_end()) -
                            slides[str(row.time_slide_id)][ifo])
                if ifo_time in seglist:
                    vetoed.append(row)
                    break
    else:
        for row in self:
            time = float(row.get_end())
            if time in seglist:
                vetoed.append(row)
    return vetoed
def apply_null_snr_veto(mi_table, null_snr=6.0, snr=20.0, grad=0.2,\
                        return_index=False):
    """Veto events in a MultiInspiralTable based on their null SNR.

    @param mi_table
        a MultiInspiralTable from which to veto events
    @param null_snr
        the value of null SNR on which to threshold
    @param snr
        the value of coherent SNR on above which to grade the null SNR
        threshold
    @param grad
        the rate at which to increase the null SNR threshold above snr
    @param return_index
        boolean to return the index array of non-vetoed elements rather
        than a new table containing the elements themselves

    @returns
        a new MultiInspiralTable with those events not vetoed OR
        the indices of the original mi_table not vetoed if return_index=True
    """
    mi_snr = mi_table.get_column("snr")
    mi_null_snr = mi_table.get_null_snr()
    # apply gradient to threshold for high SNR
    null_thresh = numpy.ones(len(mi_table)) * null_snr
    grade = mi_snr >= snr
    null_thresh[grade] += (mi_snr[grade] - snr)*5.0
    # apply veto
    keep = mi_null_snr < null_thresh
    if return_index:
        return keep
    else:
        out = table.new_from_template(mi_table)
        out.extend(numpy.asarray(mi_table)[keep])
        return out
def vetoed(self, seglist, time_slide_table=None):
    """Return a MultiInspiralTable with those row from self lying
    inside (i.e. vetoed by) any elements of seglist.

    If time_slide_table is not None, any time shifts will be undone and each
    detector checked individually
    """
    seglist = type(seglist)(map(float, seglist))
    vetoed = table.new_from_template(self)
    if time_slide_table:
        slides = time_slide_table.as_dict()
        for id_,vector in slides.iteritems():
            idx = str(id_).split(":")[-1]
            slides["multi_inspiral:time_slide_id:%s" % idx] = vector
            del slides[id_]
        slides = get_slide_vectors(time_slide_table)
        for row in self:
            ifos = row.get_ifos()
            for i,ifo in enumerate(ifos):
                ifo_time = (float(row.get_end()) -
                            slides[str(row.time_slide_id)][ifo])
                if ifo_time in seglist:
                    vetoed.append(row)
                    break
    else:
        for row in self:
            time = float(row.get_end())
            if time in seglist:
                vetoed.append(row)
    return vetoed
def apply_auto_veto(mi_table, snr=6.0, chisq_index=4.0, return_index=False):
    """Veto events in a MultiInspiralTable based on their auto chisq-
    weighted (new) coherent SNR.

    @param mi_table
        a MultiInspiralTable from which to veto events
    @param snr
        the value of coherent new SNR on which to threshold
    @param chisq_index
        the index \f$\iota\f$ used in the newSNR calculation:
        \f[\rho_{\mbox{new}} =
            \frac{\rho}{\left[\frac{1}{2}
                \left(1 + \left(\frac{\chi^2}{n_\mbox{dof}}\right)^{\iota/3}
                \right)\right]^{1/\iota}}
        \f]
    @param return_index
        boolean to return the index array of non-vetoed elements rather
        than a new table containing the elements themselves

    @returns
        a new MultiInspiralTable with those events not vetoed OR
        the indices of the original mi_table not vetoed if return_index=True
    """
    cont_new_snr = numpy.asarray(mi_table.get_new_snr(column="cont_chisq"))
    keep = cont_new_snr >= snr
    if return_index:
        return keep
    else:
        out = table.new_from_template(mi_table)
        out.extend(numpy.asarray(mi_table)[keep])
        return out
Exemple #6
0
def apply_null_snr_veto(mi_table, null_snr=6.0, snr=20.0, grad=0.2,\
                        return_index=False):
    """Veto events in a MultiInspiralTable based on their null SNR.

    @param mi_table
        a MultiInspiralTable from which to veto events
    @param null_snr
        the value of null SNR on which to threshold
    @param snr
        the value of coherent SNR on above which to grade the null SNR
        threshold
    @param grad
        the rate at which to increase the null SNR threshold above snr
    @param return_index
        boolean to return the index array of non-vetoed elements rather
        than a new table containing the elements themselves

    @returns
        a new MultiInspiralTable with those events not vetoed OR
        the indices of the original mi_table not vetoed if return_index=True
    """
    mi_snr = mi_table.get_column("snr")
    mi_null_snr = mi_table.get_null_snr()
    # apply gradient to threshold for high SNR
    null_thresh = numpy.ones(len(mi_table)) * null_snr
    grade = mi_snr >= snr
    null_thresh[grade] += (mi_snr[grade] - snr) * 5.0
    # apply veto
    keep = mi_null_snr < null_thresh
    if return_index:
        return keep
    else:
        out = table.new_from_template(mi_table)
        out.extend(numpy.asarray(mi_table)[keep])
        return out
Exemple #7
0
def apply_auto_veto(mi_table, snr=6.0, chisq_index=4.0, return_index=False):
    """Veto events in a MultiInspiralTable based on their auto chisq-
    weighted (new) coherent SNR.

    @param mi_table
        a MultiInspiralTable from which to veto events
    @param snr
        the value of coherent new SNR on which to threshold
    @param chisq_index
        the index \f$\iota\f$ used in the newSNR calculation:
        \f[\rho_{\mbox{new}} =
            \frac{\rho}{\left[\frac{1}{2}
                \left(1 + \left(\frac{\chi^2}{n_\mbox{dof}}\right)^{\iota/3}
                \right)\right]^{1/\iota}}
        \f]
    @param return_index
        boolean to return the index array of non-vetoed elements rather
        than a new table containing the elements themselves

    @returns
        a new MultiInspiralTable with those events not vetoed OR
        the indices of the original mi_table not vetoed if return_index=True
    """
    cont_new_snr = numpy.asarray(mi_table.get_new_snr(column="cont_chisq"))
    keep = cont_new_snr >= snr
    if return_index:
        return keep
    else:
        out = table.new_from_template(mi_table)
        out.extend(numpy.asarray(mi_table)[keep])
        return out
def plot_trig_rates(trigs, spans, channel, files=None, files_to_combine=None):
    if files is not None and files_to_combine is not None:
        final_trigs_to_plot = {}
        durations = {'combined': 0}
        combined_trigs = table.new_from_template(trigs[files[0]])
        for f in files_to_combine:
            combined_trigs.extend(trigs[f])
        for f in trigs.keys():
            if f not in files_to_combine:
                final_trigs_to_plot[f] = trigs[f]
                durations[f] = abs(spans[f])
            else:
                durations['combined'] += abs(spans[f])
        final_trigs_to_plot['combined'] = combined_trigs
        trigs = final_trigs_to_plot
    else:
        durations = {}
        for span in spans.keys():
            durations[span] = abs(spans[span])

    First = 1
    bins = numpy.logspace(0, 2, num=200)
    for key in durations.keys():
        # print(len(trigs[key]))
        try:
            label = str(spans[key][0]) + '-' + str(spans[key][-1])
        except KeyError:
            label = 'combined locks'
        if First:
            plot = trigs[key].hist('snr', logbins=True,
                                   log=True,
                                   histtype='step',
                                   weights=1. / abs(durations[key]),
                                   label=label,
                                   bins=bins,
                                   cumulative=-1
                                   )
            ax = plot.gca()
            First = 0
            continue
        snrs = trigs[key].get_column('snr')
        ax.hist(snrs, logbins=True, log=True, histtype='step',
                weights=1. / abs(durations[key]), label=label,
                bins=bins, cumulative=-1)
    ax.set_yscale('log')
    ax.set_ylabel('Trigger Rate [Hz]')
    ax.set_title(channel.replace('_', '\_'))
    ax.legend()
    ax.set_ylim(1e-4, 1)
    ax.set_xscale('log')
    ax.set_xlim(5, 100)
    plot.save(channel + 'LOCK-TRIG-RATES')
 def getsngls(self, ifo):
   """
   Return the sngls for a specific ifo.
   @param ifo: ifo for which to retrieve the single inspirals.
   """
   from glue.ligolw import table 
   try: ifoTrigs = table.new_from_template(self.sngl_table)
   except: ifoTrigs = lsctables.New(lsctables.SnglInspiralTable)
   for coinc in self:
     if hasattr(coinc,ifo): 
       ifoTrigs.append(getattr(coinc,ifo))
       
   return ifoTrigs
Exemple #10
0
 def getsngls(self, ifo):
   """
   Return the sngls for a specific ifo.
   @param ifo: ifo for which to retrieve the single inspirals.
   """
   from glue.ligolw import table 
   try: ifoTrigs = table.new_from_template(self.sngl_table)
   except: ifoTrigs = lsctables.New(lsctables.SnglInspiralTable)
   for coinc in self:
     if hasattr(coinc,ifo): 
       ifoTrigs.append(getattr(coinc,ifo))
       
   return ifoTrigs
Exemple #11
0
    def parseTimeDelayDegeneracy(self, ifos, gpstime=lal.GPSTimeNow(),\
                                 dt=0.0005):

        # get detectors
        detectors = [inject.cached_detector.get(inject.prefix_to_name[ifo])\
                         for ifo in ifos]

        timeDelays = []
        degenerate = []

        new = table.new_from_template(self)
        for n, row in enumerate(self):
            # get all time delays for this point
            timeDelays.append([])
            for i in xrange(len(ifos)):
                for j in xrange(i + 1, len(ifos)):
                    timeDelays[n].append(date.XLALArrivalTimeDiff(\
                                             detectors[i].location,\
                                             detectors[j].location,\
                                             row.longitude,\
                                             row.latitude,\
                                             LIGOTimeGPS(gpstime)))
            # skip the first point
            if n == 0:
                degenerate.append(False)
                new.append(row)
                continue
            else:
                degenerate.append(True)
            # test this point against all others
            for i in xrange(0, n):
                # if check point is degenerate, skip
                if degenerate[i]:
                    continue
                # check each time delay individually
                for j in xrange(0, len(timeDelays[n])):
                    # if this time delay is non-degenerate the point is valid
                    if np.fabs(timeDelays[i][j] - timeDelays[n][j]) >= dt:
                        degenerate[n] = False
                        break
                    else:
                        degenerate[n] = True
                if degenerate[n]:
                    break

            if not degenerate[n]:
                new.append(row)

        return new
Exemple #12
0
 def parseTimeDelayDegeneracy(self, ifos, gpstime=lal.GPSTimeNow(),\
                              dt=0.0005):
  
     # get detectors
     detectors = [inject.cached_detector.get(inject.prefix_to_name[ifo])\
                      for ifo in ifos]
 
     timeDelays = []
     degenerate = []
 
     new = table.new_from_template(self)
     for n,row in enumerate(self):
         # get all time delays for this point
         timeDelays.append([])
         for i in xrange(len(ifos)):
             for j in xrange(i+1, len(ifos)):
               timeDelays[n].append(date.XLALArrivalTimeDiff(\
                                        detectors[i].location,\
                                        detectors[j].location,\
                                        row.longitude,\
                                        row.latitude,\
                                        LIGOTimeGPS(gpstime)))
         # skip the first point
         if n==0:
             degenerate.append(False)
             new.append(row)
             continue
         else:
             degenerate.append(True)
         # test this point against all others
         for i in xrange(0,n):
             # if check point is degenerate, skip
             if degenerate[i]:
                 continue
             # check each time delay individually
             for j in xrange(0, len(timeDelays[n])):
                 # if this time delay is non-degenerate the point is valid
                 if np.fabs(timeDelays[i][j]-timeDelays[n][j]) >= dt:
                     degenerate[n] = False
                     break
                 else:
                     degenerate[n] = True
             if degenerate[n]:
                 break
 
         if not degenerate[n]:
             new.append(row)
 
     return new
def apply_sngl_snr_veto(mi_table, snrs=[4.0, 4.0], return_index=False):
    """Veto events in a MultiInspiralTable based on their single-detector
    snr in the most sensitive detectors.

    @param mi_table
        a MultiInspiralTable from which to veto events
    @param snrs
        an X-element list of single-detector SNRs on which to threshold
        for the X most sensitive detectors (in sensitivity order)
    @param return_index
        boolean to return the index array of non-vetoed elements rather
        than a new table containing the elements themselves

    @returns
        a new MultiInspiralTable with those events not vetoed OR
        the indices of the original mi_table not vetoed if return_index=True
    """
    if len(mi_table) == 0:
        if return_index:
            return numpy.zeros(0).astype(bool)
        else:
            return mi_table
    # parse table
    ifos = lsctables.instrument_set_from_ifos(mi_table[0].ifos)
    if "H1" in ifos and "H2" in ifos:
        ifos.remove("H2")
    mi_sngl_snr = numpy.asarray([numpy.asarray(mi_table.get_sngl_snr(ifo))
                                 for ifo in ifos])
    if len(snrs) > len(ifos):
        raise ValueError("%s single-detector thresholds given, but only %d "
                         "detectors found." % (len(snrs), len(ifos)))
    # set snrs
    snr_array = numpy.zeros(len(ifos))
    snr_array[:len(snrs)] = snrs
    snr_array.sort()

    # order sngl_snrs for each event
    mi_sngl_snr.sort(axis=0)

    # test thresholds
    keep = (mi_sngl_snr.T > snr_array).all(axis=1)
    if return_index:
        return keep
    else:
        out = table.new_from_template(mi_table)
        out.extend(numpy.asarray(mi_table)[keep])
        return out
Exemple #14
0
def get_all_vco_triggers(ifo,
                         seg,
                         frames=False,
                         fit=True,
                         channel='GDS-CALIB_STRAIN',
                         file=None):
    st = int(seg[0])
    et = int(seg[1])
    st2 = st
    First = 1
    # break into 1000s, run into memory issues otherwise
    amps = []
    while (et > st2):
        dur = min(et - st2, 1000)
        if file:
            vco = TimeSeries.read(file)
            vco = vco + 76e6
        else:
            vco = generate_fast_vco(ifo,
                                    Segment(st2, st2 + dur),
                                    frames=frames,
                                    fit=fit)

        trigs = SnglBurstTable.fetch(
            "%s:%s" % (ifo, channel),
            'omicron',
            st2,
            st2 + dur,
            filt=lambda x: st2 <= x.get_peak() < st2 + dur)
        if First:
            trigs2 = table.new_from_template(trigs)
            First = 0
        trigs2.extend(trigs)
        vtrigs = get_vco_trigs(vco, trigs, channel=channel)
        for vtrig in vtrigs:
            amps.append(vtrig)
        st2 += 1000

    central_freqs = trigs2.get_column('peak_frequency')
    snrs = trigs2.get_column('snr')
    sts = trigs2.get_column('time_peak')

    amps = np.asarray(amps)
    amps = amps[~(np.isnan(amps))]
    central_freqs = central_freqs[~(np.isnan(amps))]
    snrs = snrs[~(np.isnan(amps))]
    return amps, central_freqs, snrs, sts
Exemple #15
0
def apply_sngl_snr_veto(mi_table, snrs=[4.0, 4.0], return_index=False):
    """Veto events in a MultiInspiralTable based on their single-detector
    snr in the most sensitive detectors.

    @param mi_table
        a MultiInspiralTable from which to veto events
    @param snrs
        an X-element list of single-detector SNRs on which to threshold
        for the X most sensitive detectors (in sensitivity order)
    @param return_index
        boolean to return the index array of non-vetoed elements rather
        than a new table containing the elements themselves

    @returns
        a new MultiInspiralTable with those events not vetoed OR
        the indices of the original mi_table not vetoed if return_index=True
    """
    if len(mi_table) == 0:
        if return_index:
            return numpy.zeros(0).astype(bool)
        else:
            return mi_table
    # parse table
    ifos = lsctables.instrument_set_from_ifos(mi_table[0].ifos)
    if "H1" in ifos and "H2" in ifos:
        ifos.remove("H2")
    mi_sngl_snr = numpy.asarray(
        [numpy.asarray(mi_table.get_sngl_snr(ifo)) for ifo in ifos])
    if len(snrs) > len(ifos):
        raise ValueError("%s single-detector thresholds given, but only %d "
                         "detectors found." % (len(snrs), len(ifos)))
    # set snrs
    snr_array = numpy.zeros(len(ifos))
    snr_array[:len(snrs)] = snrs
    snr_array.sort()

    # order sngl_snrs for each event
    mi_sngl_snr.sort(axis=0)

    # test thresholds
    keep = (mi_sngl_snr.T > snr_array).all(axis=1)
    if return_index:
        return keep
    else:
        out = table.new_from_template(mi_table)
        out.extend(numpy.asarray(mi_table)[keep])
        return out
Exemple #16
0
  def return_sim_inspirals(self,statistic = None,thresh = 0):
    """
    Method to return the sim_inspiral table associated to the coincs.
    If thresh is specified, only return sims from those coincs whose stat
    exceeds thresh (or is under thresh if statistic == far).

    @param statistic: the statistic to use
    @param thresh: the threshold on the statistic
    """
    from glue.ligolw import table 
    try: simInspirals = table.new_from_template(self.sim_table)
    except: simInspirals = lsctables.New(lsctables.SimInspiralTable)
    for coinc in self:
      if statistic == 'far':
        if (hasattr(coinc,"sim")) and \
            (((coinc.stat <= thresh) and (coinc.stat >= 0)) or (thresh < 0)):
          simInspirals.append(coinc.sim)
      else:
        if (hasattr(coinc,"sim")) and (coinc.stat >= thresh):
          simInspirals.append(coinc.sim)
    
    return simInspirals
  def return_sim_inspirals(self,statistic = None,thresh = 0):
    """
    Method to return the sim_inspiral table associated to the coincs.
    If thresh is specified, only return sims from those coincs whose stat
    exceeds thresh (or is under thresh if statistic == far).

    @param statistic: the statistic to use
    @param thresh: the threshold on the statistic
    """
    from glue.ligolw import table 
    try: simInspirals = table.new_from_template(self.sim_table)
    except: simInspirals = lsctables.New(lsctables.SimInspiralTable)
    for coinc in self:
      if statistic == 'far':
        if (hasattr(coinc,"sim")) and \
            (((coinc.stat <= thresh) and (coinc.stat >= 0)) or (thresh < 0)):
          simInspirals.append(coinc.sim)
      else:
        if (hasattr(coinc,"sim")) and (coinc.stat >= thresh):
          simInspirals.append(coinc.sim)
    
    return simInspirals
def _hist_triggers(trigs, amp, times, nbins=100, bin_low=1, bin_high=1):
    bin_size = (amp.max() - amp.min()) / float(nbins)
    bins = numpy.arange(amp.min(), amp.max() + bin_size, bin_size)
    hist = numpy.zeros(bins.size)
    hist_indices = {}
    for i in range(bins.size - 1):
        hist_indices[str(i)] = []
        for j in range(times.size):
            if amp[j] > bins[i] and amp[j] < bins[i + 1]:
                hist[i] += 1
                hist_indices[str(i)].append(j)
    idx = numpy.where(hist == hist.max())[0]
    if len(idx) > 1:
        idx = idx[0]
    high = 79 + bins[idx + bin_high] / 1e3
    low = 79 + bins[idx - bin_low] / 1e3
    print('we expect whistles occur between %s and %s MHz'
          % (low, high))

    new_trigs = table.new_from_template(trigs)
    for i in range(len(trigs)):
        if amp[i] > bins[idx - bin_low] and amp[i] < bins[idx + bin_high]:
            new_trigs.extend(trigs[i:i + 1])
    times = []
    snrs = []
    freqs = []
    for trig in new_trigs:
        peak = trig.get_peak()
    snrs.append(trig.snr)
    freqs.append(trig.peak_frequency)
    times.append(peak.seconds + peak.nanoseconds * 1e-9)
    f = open(
        'L1-TRIG-TIMES-' + str(span[0]) + '-' + str(span[-1]) + '.txt', 'w')
    for time, snr, freq in zip(times, snrs, freqs):
        f.write('%f\t%f\t%f\n' % (time, snr, freq))
    f.close()
    return new_trigs, hist
def apply_snr_veto(mi_table, snr=6.0, return_index=False):
    """Veto events in a MultiInspiralTable based on their (coherent) SNR
    value.

    @param mi_table
        a MultiInspiralTable from which to veto events
    @param snr
        the value of coherent SNR on which to threshold
    @param return_index
        boolean to return the index array of non-vetoed elements rather
        than a new table containing the elements themselves

    @returns
        a new MultiInspiralTable with those events not vetoed OR
        the indices of the original mi_table not vetoed if return_index=True
    """
    mi_snr = numpy.asarray(mi_table.get_column("snr"))
    keep = mi_snr >= snr
    if return_index:
        return keep
    else:
        out = table.new_from_template(mi_table)
        out.extend(numpy.asarray(mi_table)[keep])
        return out
Exemple #20
0
def apply_snr_veto(mi_table, snr=6.0, return_index=False):
    """Veto events in a MultiInspiralTable based on their (coherent) SNR
    value.

    @param mi_table
        a MultiInspiralTable from which to veto events
    @param snr
        the value of coherent SNR on which to threshold
    @param return_index
        boolean to return the index array of non-vetoed elements rather
        than a new table containing the elements themselves

    @returns
        a new MultiInspiralTable with those events not vetoed OR
        the indices of the original mi_table not vetoed if return_index=True
    """
    mi_snr = numpy.asarray(mi_table.get_column("snr"))
    keep = mi_snr >= snr
    if return_index:
        return keep
    else:
        out = table.new_from_template(mi_table)
        out.extend(numpy.asarray(mi_table)[keep])
        return out
def get_all_vco_triggers(ifo, seg, frames=False, fit=True, channel="GDS-CALIB_STRAIN", file=None):
    st = int(seg[0])
    et = int(seg[1])
    st2 = st
    First = 1
    # break into 1000s, run into memory issues otherwise
    amps = []
    while et > st2:
        dur = min(et - st2, 1000)
        if file:
            vco = TimeSeries.read(file)
            vco = vco + 76e6
        else:
            vco = generate_fast_vco(ifo, Segment(st2, st2 + dur), frames=frames, fit=fit)

        trigs = SnglBurstTable.fetch(
            "%s:%s" % (ifo, channel), "omicron", st2, st2 + dur, filt=lambda x: st2 <= x.get_peak() < st2 + dur
        )
        if First:
            trigs2 = table.new_from_template(trigs)
            First = 0
        trigs2.extend(trigs)
        vtrigs = get_vco_trigs(vco, trigs, channel=channel)
        for vtrig in vtrigs:
            amps.append(vtrig)
        st2 += 1000

    central_freqs = trigs2.get_column("peak_frequency")
    snrs = trigs2.get_column("snr")
    sts = trigs2.get_column("time_peak")

    amps = np.asarray(amps)
    amps = amps[~(np.isnan(amps))]
    central_freqs = central_freqs[~(np.isnan(amps))]
    snrs = snrs[~(np.isnan(amps))]
    return amps, central_freqs, snrs, sts
Exemple #22
0
def table_from_file(f,
                    tablename,
                    columns=None,
                    filt=None,
                    contenthandler=None,
                    nproc=1,
                    verbose=False):
    """Read a `~glue.ligolw.table.Table` from a LIGO_LW file.

    Parameters
    ----------
    f : `file`, `str`, `CacheEntry`, `list`, `Cache`
        object representing one or more files. One of

        - an open `file`
        - a `str` pointing to a file path on disk
        - a formatted `~lal.utils.CacheEntry` representing one file
        - a `list` of `str` file paths
        - a formatted `~glue.lal.Cache` representing many files

    tablename : `str`
        name of the table to read.
    columns : `list`, optional
        list of column name strings to read, default all.
    filt : `function`, optional
        function by which to `filter` events. The callable must accept as
        input a row of the table event and return `True`/`False`.
    contenthandler : `~glue.ligolw.ligolw.LIGOLWContentHandler`
        SAX content handler for parsing LIGO_LW documents.

    Returns
    -------
    table : `~glue.ligolw.table.Table`
        `Table` of data with given columns filled
    """
    from glue.ligolw.ligolw import Document
    from glue.ligolw import (table, lsctables)
    from glue.ligolw.utils.ligolw_add import ligolw_add

    # find table class
    tableclass = lsctables.TableByName[table.Table.TableName(tablename)]

    # get content handler
    if contenthandler is None:
        contenthandler = get_partial_contenthandler(tableclass)

    # allow cache multiprocessing
    if nproc != 1:
        return tableclass.read(f,
                               columns=columns,
                               contenthandler=contenthandler,
                               nproc=nproc,
                               format='cache')

    lsctables.use_in(contenthandler)

    # set columns to read
    if columns is not None:
        _oldcols = tableclass.loadcolumns
        tableclass.loadcolumns = columns

    # generate Document and populate
    files = file_list(f)
    xmldoc = Document()
    ligolw_add(xmldoc,
               files,
               non_lsc_tables_ok=True,
               contenthandler=contenthandler,
               verbose=verbose)

    # extract table
    out = tableclass.get_table(xmldoc)
    if verbose:
        gprint('%d rows found in %s table' % (len(out), out.tableName))

    # filter output
    if filt:
        if verbose:
            gprint('filtering rows ...', end=' ')
        try:
            out_ = out.copy()
        except AttributeError:
            out_ = table.new_from_template(out)
        out_.extend(filter(filt, out))
        out = out_
        if verbose:
            gprint('%d rows remaining\n' % len(out))

    # reset loadcolumns and return
    if columns is not None:
        tableclass.loadcolumns = _oldcols

    return out
Exemple #23
0
 def rotate(self, R):
     rotated = table.new_from_template(self)
     for row in self:
         rotated.append(row.rotate(R))
     return rotated
def cluster_multi_inspirals(mi_table, dt, loudest_by="snr"):
    """Cluster a MultiInspiralTable with a given ranking statistic and
    clustering window.

    This method separates the table rows into time bins, returning those
    row that are
        * loudest in their own bin, and
        * louder than those events in the preceeding and following bins
          that are within the clustering time window

    @return: a new MultiInspiralTable containing those clustered events

    @param mi_table:
        MultiInspiralTable to cluster
    @param dt:
        width (seconds) of clustering window
    @keyword loudest_by:
        column by which to rank events, default: 'snr'

    @type mi_table: glue.ligolw.lsctables.MultiInspiralTable
    @type dt: float
    @type loudest_by: string
    @rtype: glue.ligolw.lsctables.MultiInspiralTable
    """
    cluster_table = table.new_from_template(mi_table)
    if not len(mi_table):
        return cluster_table

    # get data
    end_time = numpy.asarray(mi_table.get_end()).astype(float)
    if hasattr(mi_table, "get_%s" % loudest_by):
        stat = numpy.asarray(getattr(mi_table, "get_%s" % loudest_by)())
    else:
        stat = numpy.asarray(mi_table.get_column(loudest_by))

    # get times
    start = round(end_time.min())
    end = round(end_time.max() + 1)

    # generate bins
    num_bins = int((end - start) // dt + 1)
    time_bins = []
    loudest_stat = numpy.zeros(num_bins)
    loudest_time = numpy.zeros(num_bins)
    for n in range(num_bins):
        time_bins.append([])

    # bin triggers
    for i, (t, s) in enumerate(itertools.izip(end_time, stat)):
        bin_ = int(float(t - start) // dt)
        time_bins[bin_].append(i)
        if s > loudest_stat[bin_]:
            loudest_stat[bin_] = s
            loudest_time[bin_] = t

    # loop over all bins
    for i, bin_ in enumerate(time_bins):
        if len(bin_) < 1:
            continue
        first = i == 0
        last = i == (num_bins - 1)

        prev = i - 1
        next_ = i + 1
        check_prev = (not first and len(time_bins[prev]) > 0)
        check_next = (not last and len(time_bins[next_]) > 0)

        # pick loudest event in bin
        idx = bin_[stat[bin_].argmax()]
        s = stat[idx]
        t = end_time[idx]

        # trigger was loudest in it's bin, search loudest event
        # in previous bin
        if (check_prev and (t - loudest_time[prev]) < dt
                and s < loudest_stat[prev]):
            continue

        # Same for the next bin
        if (check_next and (loudest_time[next_] - t) < dt
                and s < loudest_stat[next_]):
            continue

        loudest = True

        # trigger was loudest in it's bin, search previous bin
        if check_prev and not (t - loudest_time[prev]) < dt:
            for idx2 in time_bins[prev]:
                t2 = end_time[idx2]
                if (t - end_time[idx2]) < dt and s < stat[idx2]:
                    loudest = False
                    break
        if not loudest:
            continue

        # if still loudest, check the next bin
        if check_next and not (loudest_time[next_] - t) < dt:
            for idx2 in time_bins[next_]:
                if (end_time[idx2] - t) < dt and s < stat[idx2]:
                    loudest = False
                    break
        if not loudest:
            continue

        # this was the loudest trigger in its vicinity,
        # keep it and move to the next bin
        cluster_table.append(mi_table[idx])

    return cluster_table
def cluster_multi_inspirals(mi_table, dt, loudest_by="snr"):
    """Cluster a MultiInspiralTable with a given ranking statistic and
    clustering window.

    This method separates the table rows into time bins, returning those
    row that are
        * loudest in their own bin, and
        * louder than those events in the preceeding and following bins
          that are within the clustering time window

    @return: a new MultiInspiralTable containing those clustered events

    @param mi_table:
        MultiInspiralTable to cluster
    @param dt:
        width (seconds) of clustering window
    @keyword loudest_by:
        column by which to rank events, default: 'snr'

    @type mi_table: glue.ligolw.lsctables.MultiInspiralTable
    @type dt: float
    @type loudest_by: string
    @rtype: glue.ligolw.lsctables.MultiInspiralTable
    """
    cluster_table = table.new_from_template(mi_table)
    if not len(mi_table):
        return cluster_table

    # get data
    end_time = numpy.asarray(mi_table.get_end()).astype(float)
    if hasattr(mi_table, "get_%s" % loudest_by):
        stat = numpy.asarray(getattr(mi_table, "get_%s" % loudest_by)())
    else:
        stat = numpy.asarray(mi_table.get_column(loudest_by))

    # get times
    start = round(end_time.min())
    end = round(end_time.max()+1)

    # generate bins
    num_bins  = int((end-start)//dt + 1)
    time_bins = []
    loudest_stat = numpy.zeros(num_bins)
    loudest_time = numpy.zeros(num_bins)
    for n in range(num_bins):
        time_bins.append([])

    # bin triggers
    for i,(t,s) in enumerate(itertools.izip(end_time, stat)):
        bin_ = int(float(t-start)//dt)
        time_bins[bin_].append(i)
        if s > loudest_stat[bin_]:
            loudest_stat[bin_] = s
            loudest_time[bin_] = t

    # loop over all bins
    for i,bin_ in enumerate(time_bins):
        if len(bin_)<1:
            continue
        first = i==0
        last = i==(num_bins-1)

        prev = i-1
        next_ = i+1
        check_prev = (not first and len(time_bins[prev]) > 0)
        check_next = (not last and len(time_bins[next_]) > 0)

        # pick loudest event in bin
        idx = bin_[stat[bin_].argmax()]
        s = stat[idx]
        t = end_time[idx]

        # trigger was loudest in it's bin, search loudest event
        # in previous bin
        if (check_prev and (t - loudest_time[prev]) < dt and
                s < loudest_stat[prev]):
            continue

        # Same for the next bin
        if (check_next and (loudest_time[next_] - t) < dt and
                s < loudest_stat[next_]):
            continue

        loudest=True

        # trigger was loudest in it's bin, search previous bin
        if check_prev and not (t - loudest_time[prev]) < dt:
            for idx2 in time_bins[prev]:
                t2 = end_time[idx2]
                if (t - end_time[idx2]) < dt and s < stat[idx2]:
                    loudest = False
                    break
        if not loudest:
            continue

        # if still loudest, check the next bin
        if check_next and not (loudest_time[next_] - t) < dt:
            for idx2 in time_bins[next_]:
                if (end_time[idx2] - t) < dt and s < stat[idx2]:
                    loudest = False
                    break
        if not loudest:
            continue

        # this was the loudest trigger in its vicinity,
        # keep it and move to the next bin
        cluster_table.append(mi_table[idx])

    return cluster_table
Exemple #26
0
def table_from_file(f, tablename, columns=None, filt=None,
                    contenthandler=None, nproc=1, verbose=False):
    """Read a `~glue.ligolw.table.Table` from a LIGO_LW file.

    Parameters
    ----------
    f : `file`, `str`, `CacheEntry`, `list`, `Cache`
        object representing one or more files. One of

        - an open `file`
        - a `str` pointing to a file path on disk
        - a formatted `~glue.lal.CacheEntry` representing one file
        - a `list` of `str` file paths
        - a formatted `~glue.lal.Cache` representing many files

    tablename : `str`
        name of the table to read.
    columns : `list`, optional
        list of column name strings to read, default all.
    filt : `function`, optional
        function by which to `filter` events. The callable must accept as
        input a row of the table event and return `True`/`False`.
    contenthandler : `~glue.ligolw.ligolw.LIGOLWContentHandler`
        SAX content handler for parsing LIGO_LW documents.

    Returns
    -------
    table : `~glue.ligolw.table.Table`
        `Table` of data with given columns filled
    """
    # find table class
    tableclass = lsctables.TableByName[table.StripTableName(tablename)]

    # get content handler
    if contenthandler is None:
        contenthandler = get_partial_contenthandler(tableclass)

    # allow cache multiprocessing
    if nproc != 1:
        return tableclass.read(f, columns=columns,
                               contenthandler=contenthandler,
                               nproc=nproc, format='cache')

    # set columns to read
    if columns is not None:
        _oldcols = tableclass.loadcolumns
        tableclass.loadcolumns = columns

    # generate Document and populate
    files = [fp.name if isinstance(fp, (file, GzipFile)) else
             fp for fp in file_list(f)]
    xmldoc = Document()
    ligolw_add(xmldoc, files, non_lsc_tables_ok=True,
               contenthandler=contenthandler, verbose=verbose)

    # extract table
    try:
        out = tableclass.get_table(xmldoc)
    except ValueError:
        out = lsctables.New(tableclass, columns=columns)
    if verbose:
        gprint('%d rows found in %s table' % (len(out), out.tableName))

    if filt:
        if verbose:
            gprint('filtering rows ...', end=' ')
        try:
            out_ = out.copy()
        except AttributeError:
            out_ = table.new_from_template(out)
        out_.extend(filter(filt, out))
        out = out_
        if verbose:
            gprint('%d rows remaining\n' % len(out))
    if columns is not None:
        tableclass.loadcolumns = _oldcols
    return out
Exemple #27
0
 def rotate(self, R):
     rotated = table.new_from_template(self)
     for row in self:
         rotated.append(row.rotate(R))
     return rotated
Exemple #28
0
exttrig_config_file, grb_ifolist, onSourceSegment, offSourceSegment = exttrig_dataquery.exttrig_dataquery(grb_name, grb_time, grb_ra, grb_dec, opts.offset, opts.config_file, opts.extend, opts.useold, opts.make_plots, opts.make_xml)

##############################################################################
# create the config parser object and exttrig_dataquery ini file
cp = dcConfigParser()
cp.read(exttrig_config_file)

ext_trigs = grbsummary.load_external_triggers('grb%s.xml'%opts.name[0])

if ext_trigs is None:
  print("No external triggers found.  Nothing to do.", file=sys.stderr)
  sys.exit()

if len(opts.name) > 0:
  temp_list = filter(lambda row: row.event_number_grb in opts.name, ext_trigs)
  ext_trigs = table.new_from_template(ext_trigs)
  ext_trigs.extend(temp_list)

  if len(ext_trigs) != len(opts.name):
    missing = set(opts.name) - set([row.event_number_grb for row in ext_trigs])
    raise ValueError("could not find the following requested GRBs: " \
      + "".join(missing))

if opts.verbose:
  print("Will construct workflows to analyze:", \
    ", ".join(["GRB" + trig.event_number_grb for trig in ext_trigs]))

if opts.do_coh_PTF:
  for program in ['coh_PTF_inspiral','coh_PTF_spin_checker']:
    if not opts.ipn:
      cp.set(program,'right-ascension',str(ext_trigs[0].event_ra))
def frominjectionfile(file, type, ifo=None, start=None, end=None):
  
  """
    Read generic injection file object file containing injections of the given
    type string. Returns an 'Sim' lsctable of the corresponding type.

    Arguments:
   
      file : file object
      type : [ "inspiral" | "burst" | "ringdown" ]

    Keyword arguments:

      ifo : [ "G1" | "H1" | "H2" | "L1" | "V1" ]
  """

  # read type
  type = type.lower()

  # read injection xml
  xml = re.compile('(xml$|xml.gz$)')
  if re.search(xml,file.name):
    xmldoc,digest = utils.load_fileobj(file)
    injtable = table.get_table(xmldoc,'sim_%s:table' % (type))

  # read injection txt
  else:
    cchar = re.compile('[#%<!()_\[\]{}:;\'\"]+')

    #== construct new Sim{Burst,Inspiral,Ringdown}Table
    injtable = lsctables.New(lsctables.__dict__['Sim%sTable' % (type.title())])
    if type=='inspiral':
      columns = ['geocent_end_time.geocent_end_time_ns',\
                 'h_end_time.h_end_time_ns',\
                 'l_end_time.l_end_time_ns',\
                 'v_end_time.v_end_time_ns',\
                 'distance'] 
      for line in file.readlines():
        if re.match(cchar,line):
          continue
        # set up siminspiral object
        inj = lsctables.SimInspiral()
        # split data
        sep = re.compile('[\s,=]+')
        data = sep.split(line)
        # set attributes
        inj.geocent_end_time    = int(data[0].split('.')[0])
        inj.geocent_end_time_ns = int(data[0].split('.')[1])
        inj.h_end_time          = int(data[1].split('.')[0])
        inj.h_end_time_ns       = int(data[1].split('.')[1])
        inj.l_end_time          = int(data[2].split('.')[0])
        inj.l_end_time_ns       = int(data[2].split('.')[1])
        inj.v_end_time          = int(data[3].split('.')[0])
        inj.v_end_time_ns       = int(data[3].split('.')[1])
        inj.distance            = float(data[4])

        injtable.append(inj)

    if type=='burst':
      if file.readlines()[0].startswith('filestart'):
        # if given parsed burst file
        file.seek(0)

        snrcol = { 'G1':23, 'H1':19, 'L1':21, 'V1':25 }

        for line in file.readlines():
          inj = lsctables.SimBurst()
          # split data
          sep = re.compile('[\s,=]+')
          data = sep.split(line)
          # set attributes

          # gps time
          if 'burstgps' in data:
            idx = data.index('burstgps')+1
            geocent = LIGOTimeGPS(data[idx])

            inj.time_geocent_gps    = geocent.seconds
            inj.time_geocent_gps_ns = geocent.nanoseconds
          else:
            continue


          #inj.waveform            = data[4]
          #inj.waveform_number     = int(data[5])

          # frequency
          if 'freq' in data:
            idx = data.index('freq')+1
            inj.frequency = float(data[idx])
          else:
            continue

          # SNR a.k.a. amplitude
          if ifo and 'snr%s' % ifo in data:
            idx = data.index('snr%s' % ifo)+1
            inj.amplitude = float(data[idx])
          elif 'rmsSNR' in data:
            idx = data.index('rmsSNR')+1
            inj.amplitude = float(data[idx])
          else:
            continue

          if 'phi' in data:
            idx = data.index('phi' )+1
            inj.ra = float(data[idx])*24/(2*math.pi)       

          if 'theta' in data:
            idx = data.index('theta' )+1 
            inj.ra = 90-(float(data[idx])*180/math.pi)

          if ifo and 'hrss%s' % ifo in data:
            idx = data.index('hrss%s' % ifo)+1
            inj.hrss = float(data[idx])
          elif 'hrss' in data:
            idx = data.index('hrss')+1
            inj.hrss = float(data[idx])

          # extra columns to be added when I know how
          #inj.q = 0
          #inj.q                   = float(data[11])
          #h_delay = LIGOTimeGPS(data[41])
          #inj.h_peak_time         = inj.time_geocent_gps+h_delay.seconds
          #inj.h_peak_time_ns      = inj.time_geocent_gps_ns+h_delay.nanoseconds
          #l_delay = LIGOTimeGPS(data[43])
          #inj.l_peak_time         = inj.time_geocent_gps+l_delay.seconds
          #inj.l_peak_time_ns      = inj.time_geocent_gps_ns+l_delay.nanoseconds
          #v_delay = LIGOTimeGPS(data[43])
          #inj.v_peak_time         = inj.time_geocent_gps+v_delay.seconds
          #inj.v_peak_time_ns      = inj.time_geocent_gps_ns+v_delay.nanoseconds

          injtable.append(inj)

      else:
        # if given parsed burst file
        file.seek(0)
        for line in file.readlines():
          inj = lsctables.SimBurst()
          # split data
          sep = re.compile('[\s,]+')
          data = sep.split(line)
          # set attributes
          geocent = LIGOTimeGPS(data[0])
          inj.time_geocent_gps    = geocent.seconds
          inj.time_geocent_gps_ns = geocent.nanoseconds

          injtable.append(inj)

  injections = table.new_from_template(injtable)
  if not start:  start = 0
  if not end:    end   = 9999999999
  span = segments.segmentlist([ segments.segment(start, end) ])
  get_time = dqTriggerUtils.def_get_time(injections.tableName)
  injections.extend(inj for inj in injtable if get_time(inj) in span)

  return injections