Esempio n. 1
0
    def to_coinc_xml_object(self, file_name):
        outdoc = ligolw.Document()
        outdoc.appendChild(ligolw.LIGO_LW())

        ifos = list(self.sngl_files.keys())
        proc_id = ligolw_process.register_to_xmldoc(
            outdoc,
            'pycbc', {},
            ifos=ifos,
            comment='',
            version=pycbc_version.git_hash,
            cvs_repository='pycbc/' + pycbc_version.git_branch,
            cvs_entry_time=pycbc_version.date).process_id

        search_summ_table = lsctables.New(lsctables.SearchSummaryTable)
        coinc_h5file = self.coinc_file.h5file
        try:
            start_time = coinc_h5file['segments']['coinc']['start'][:].min()
            end_time = coinc_h5file['segments']['coinc']['end'][:].max()
        except KeyError:
            start_times = []
            end_times = []
            for ifo_comb in coinc_h5file['segments']:
                if ifo_comb == 'foreground_veto':
                    continue
                seg_group = coinc_h5file['segments'][ifo_comb]
                start_times.append(seg_group['start'][:].min())
                end_times.append(seg_group['end'][:].max())
            start_time = min(start_times)
            end_time = max(end_times)
        num_trigs = len(self.sort_arr)
        search_summary = return_search_summary(start_time, end_time, num_trigs,
                                               ifos)
        search_summ_table.append(search_summary)
        outdoc.childNodes[0].appendChild(search_summ_table)

        sngl_inspiral_table = lsctables.New(lsctables.SnglInspiralTable)
        coinc_def_table = lsctables.New(lsctables.CoincDefTable)
        coinc_event_table = lsctables.New(lsctables.CoincTable)
        coinc_inspiral_table = lsctables.New(lsctables.CoincInspiralTable)
        coinc_event_map_table = lsctables.New(lsctables.CoincMapTable)
        time_slide_table = lsctables.New(lsctables.TimeSlideTable)

        # Set up time_slide table
        time_slide_id = lsctables.TimeSlideID(0)
        for ifo in ifos:
            time_slide_row = lsctables.TimeSlide()
            time_slide_row.instrument = ifo
            time_slide_row.time_slide_id = time_slide_id
            time_slide_row.offset = 0
            time_slide_row.process_id = proc_id
            time_slide_table.append(time_slide_row)

        # Set up coinc_definer table
        coinc_def_id = lsctables.CoincDefID(0)
        coinc_def_row = lsctables.CoincDef()
        coinc_def_row.search = "inspiral"
        coinc_def_row.description = \
            "sngl_inspiral<-->sngl_inspiral coincidences"
        coinc_def_row.coinc_def_id = coinc_def_id
        coinc_def_row.search_coinc_type = 0
        coinc_def_table.append(coinc_def_row)

        bank_col_names = ['mass1', 'mass2', 'spin1z', 'spin2z']
        bank_col_vals = {}
        for name in bank_col_names:
            bank_col_vals[name] = self.get_bankfile_array(name)

        coinc_event_names = ['ifar', 'time', 'fap', 'stat']
        coinc_event_vals = {}
        for name in coinc_event_names:
            if name == 'time':
                coinc_event_vals[name] = self.get_end_time()
            else:
                coinc_event_vals[name] = self.get_coincfile_array(name)

        sngl_col_names = [
            'snr', 'chisq', 'chisq_dof', 'bank_chisq', 'bank_chisq_dof',
            'cont_chisq', 'cont_chisq_dof', 'end_time', 'template_duration',
            'coa_phase', 'sigmasq'
        ]
        sngl_col_vals = {}
        for name in sngl_col_names:
            sngl_col_vals[name] = self.get_snglfile_array_dict(name)

        sngl_event_count = 0
        for idx in range(len(self.sort_arr)):
            # Set up IDs and mapping values
            coinc_id = lsctables.CoincID(idx)

            # Set up sngls
            # FIXME: As two-ifo is hardcoded loop over all ifos
            sngl_combined_mchirp = 0
            sngl_combined_mtot = 0
            net_snrsq = 0
            for ifo in ifos:
                # If this ifo is not participating in this coincidence then
                # ignore it and move on.
                if not sngl_col_vals['snr'][ifo][1][idx]:
                    continue
                event_id = lsctables.SnglInspiralID(sngl_event_count)
                sngl_event_count += 1
                sngl = return_empty_sngl()
                sngl.event_id = event_id
                sngl.ifo = ifo
                net_snrsq += sngl_col_vals['snr'][ifo][0][idx]**2
                for name in sngl_col_names:
                    val = sngl_col_vals[name][ifo][0][idx]
                    if name == 'end_time':
                        sngl.set_end(LIGOTimeGPS(val))
                    else:
                        setattr(sngl, name, val)
                for name in bank_col_names:
                    val = bank_col_vals[name][idx]
                    setattr(sngl, name, val)
                sngl.mtotal, sngl.eta = pnutils.mass1_mass2_to_mtotal_eta(
                    sngl.mass1, sngl.mass2)
                sngl.mchirp, _ = pnutils.mass1_mass2_to_mchirp_eta(
                    sngl.mass1, sngl.mass2)
                sngl.eff_distance = (sngl.sigmasq)**0.5 / sngl.snr
                sngl_combined_mchirp += sngl.mchirp
                sngl_combined_mtot += sngl.mtotal

                sngl_inspiral_table.append(sngl)

                # Set up coinc_map entry
                coinc_map_row = lsctables.CoincMap()
                coinc_map_row.table_name = 'sngl_inspiral'
                coinc_map_row.coinc_event_id = coinc_id
                coinc_map_row.event_id = event_id
                coinc_event_map_table.append(coinc_map_row)

            sngl_combined_mchirp = sngl_combined_mchirp / len(ifos)
            sngl_combined_mtot = sngl_combined_mtot / len(ifos)

            # Set up coinc inspiral and coinc event tables
            coinc_event_row = lsctables.Coinc()
            coinc_inspiral_row = lsctables.CoincInspiral()
            coinc_event_row.coinc_def_id = coinc_def_id
            coinc_event_row.nevents = len(ifos)
            coinc_event_row.instruments = ','.join(ifos)
            coinc_inspiral_row.set_ifos(ifos)
            coinc_event_row.time_slide_id = time_slide_id
            coinc_event_row.process_id = proc_id
            coinc_event_row.coinc_event_id = coinc_id
            coinc_inspiral_row.coinc_event_id = coinc_id
            coinc_inspiral_row.mchirp = sngl_combined_mchirp
            coinc_inspiral_row.mass = sngl_combined_mtot
            coinc_inspiral_row.set_end(
                LIGOTimeGPS(coinc_event_vals['time'][idx]))
            coinc_inspiral_row.snr = net_snrsq**0.5
            coinc_inspiral_row.false_alarm_rate = coinc_event_vals['fap'][idx]
            coinc_inspiral_row.combined_far = 1. / coinc_event_vals['ifar'][idx]
            # Transform to Hz
            coinc_inspiral_row.combined_far = \
                                    coinc_inspiral_row.combined_far / YRJUL_SI
            coinc_event_row.likelihood = coinc_event_vals['stat'][idx]
            coinc_inspiral_row.minimum_duration = 0.
            coinc_event_table.append(coinc_event_row)
            coinc_inspiral_table.append(coinc_inspiral_row)

        outdoc.childNodes[0].appendChild(coinc_def_table)
        outdoc.childNodes[0].appendChild(coinc_event_table)
        outdoc.childNodes[0].appendChild(coinc_event_map_table)
        outdoc.childNodes[0].appendChild(time_slide_table)
        outdoc.childNodes[0].appendChild(coinc_inspiral_table)
        outdoc.childNodes[0].appendChild(sngl_inspiral_table)

        ligolw_utils.write_filename(outdoc, file_name)
def ReadMultiInspiralTimeSlidesFromFiles(fileList,
                                         generate_output_tables=False):
    """
  Read time-slid multiInspiral tables from a list of files
  @param fileList: list of input files
  """
    if not fileList:
        return multiInspiralTable(), None

    multis = None
    timeSlides = []

    segmentDict = {}
    for thisFile in fileList:

        doc = utils.load_filename(thisFile,
                                  gz=(thisFile or "stdin").endswith(".gz"),
                                  contenthandler=lsctables.use_in(
                                      ligolw.LIGOLWContentHandler))
        # Extract the time slide table
        timeSlideTable = table.get_table(doc,
                                         lsctables.TimeSlideTable.tableName)
        slideMapping = {}
        currSlides = {}
        # NOTE: I think some of this is duplicated in the glue definition of the
        # time slide table. Probably should move over to that
        for slide in timeSlideTable:
            currID = int(slide.time_slide_id)
            if currID not in currSlides.keys():
                currSlides[currID] = {}
                currSlides[currID][slide.instrument] = slide.offset
            elif slide.instrument not in currSlides[currID].keys():
                currSlides[currID][slide.instrument] = slide.offset

        for slideID, offsetDict in currSlides.items():
            try:
                # Is the slide already in the list and where?
                offsetIndex = timeSlides.index(offsetDict)
                slideMapping[slideID] = offsetIndex
            except ValueError:
                # If not then add it
                timeSlides.append(offsetDict)
                slideMapping[slideID] = len(timeSlides) - 1

        # Get the mapping table
        segmentMap = {}
        timeSlideMapTable = table.get_table(
            doc, lsctables.TimeSlideSegmentMapTable.tableName)
        for entry in timeSlideMapTable:
            segmentMap[int(entry.segment_def_id)] = int(entry.time_slide_id)

        # Extract the segment table
        segmentTable = table.get_table(doc, lsctables.SegmentTable.tableName)
        for entry in segmentTable:
            currSlidId = segmentMap[int(entry.segment_def_id)]
            currSeg = entry.get()
            if not segmentDict.has_key(slideMapping[currSlidId]):
                segmentDict[slideMapping[currSlidId]] = segments.segmentlist()
            segmentDict[slideMapping[currSlidId]].append(currSeg)
            segmentDict[slideMapping[currSlidId]].coalesce()

        # extract the multi inspiral table
        try:
            multiInspiralTable = table.get_table(
                doc, lsctables.MultiInspiralTable.tableName)
            # Remap the time slide IDs
            for multi in multiInspiralTable:
                newID = slideMapping[int(multi.time_slide_id)]
                multi.time_slide_id = ilwd.ilwdchar(\
                                      "time_slide:time_slide_id:%d" % (newID))
            if multis: multis.extend(multiInspiralTable)
            else: multis = multiInspiralTable
#    except: multiInspiralTable = None
        except:
            raise

    if not generate_output_tables:
        return multis, timeSlides, segmentDict
    else:
        # Make a new time slide table
        timeSlideTab = lsctables.New(lsctables.TimeSlideTable)

        for slideID, offsetDict in enumerate(timeSlides):
            for instrument in offsetDict.keys():
                currTimeSlide = lsctables.TimeSlide()
                currTimeSlide.instrument = instrument
                currTimeSlide.offset = offsetDict[instrument]
                currTimeSlide.time_slide_id = ilwd.ilwdchar(\
                                        "time_slide:time_slide_id:%d" % (slideID))
                currTimeSlide.process_id = ilwd.ilwdchar(\
                                        "process:process_id:%d" % (0))
                timeSlideTab.append(currTimeSlide)

        # Make a new mapping table
        timeSlideSegMapTab = lsctables.New(lsctables.TimeSlideSegmentMapTable)

        for i in range(len(timeSlides)):
            currMapEntry = lsctables.TimeSlideSegmentMap()
            currMapEntry.time_slide_id = ilwd.ilwdchar(\
                                      "time_slide:time_slide_id:%d" % (i))
            currMapEntry.segment_def_id = ilwd.ilwdchar(\
                                      "segment_def:segment_def_id:%d" % (i))
            timeSlideSegMapTab.append(currMapEntry)

        # Make a new segment table
        newSegmentTable = lsctables.New(lsctables.SegmentTable)

        segmentIDCount = 0
        for i in range(len(timeSlides)):
            currSegList = segmentDict[i]
            for seg in currSegList:
                currSegment = lsctables.Segment()
                currSegment.segment_id = ilwd.ilwdchar(\
                                      "segment:segment_id:%d" %(segmentIDCount))
                segmentIDCount += 1
                currSegment.segment_def_id = ilwd.ilwdchar(\
                                        "segment_def:segment_def_id:%d" % (i))
                currSegment.process_id = ilwd.ilwdchar(\
                                        "process:process_id:%d" % (0))
                currSegment.set(seg)
                currSegment.creator_db = -1
                currSegment.segment_def_cdb = -1
                newSegmentTable.append(currSegment)
        return multis,timeSlides,segmentDict,timeSlideTab,newSegmentTable,\
               timeSlideSegMapTab