def get_segment_summary_times(scienceFile, segmentName): """ This function will find the times for which the segment_summary is set for the flag given by segmentName. Parameters ----------- scienceFile : SegFile The segment file that we want to use to determine this. segmentName : string The DQ flag to search for times in the segment_summary table. Returns --------- summSegList : glue.segments.segmentlist The times that are covered in the segment summary table. """ # Parse the segmentName segmentName = segmentName.split(':') if not len(segmentName) in [2,3]: raise ValueError("Invalid channel name %s." %(segmentName)) ifo = segmentName[0] channel = segmentName[1] version = '' if len(segmentName) == 3: version = int(segmentName[2]) # Load the filename xmldoc = utils.load_filename(scienceFile.cache_entry.path, gz=scienceFile.cache_entry.path.endswith("gz"), contenthandler=ContentHandler) # Get the segment_def_id for the segmentName segmentDefTable = table.get_table(xmldoc, "segment_definer") for entry in segmentDefTable: if (entry.ifos == ifo) and (entry.name == channel): if len(segmentName) == 2 or (entry.version==version): segDefID = entry.segment_def_id break else: raise ValueError("Cannot find channel %s in segment_definer table."\ %(segmentName)) # Get the segmentlist corresponding to this segmentName in segment_summary segmentSummTable = table.get_table(xmldoc, "segment_summary") summSegList = segments.segmentlist([]) for entry in segmentSummTable: if entry.segment_def_id == segDefID: segment = segments.segment(entry.start_time, entry.end_time) summSegList.append(segment) summSegList.coalesce() return summSegList
def get_segment_definer_comments(xml_file, include_version=True): """Returns a dict with the comment column as the value for each segment""" from pycbc.ligolw.ligolw import LIGOLWContentHandler as h lsctables.use_in(h) # read segment definer table xmldoc, _ = ligolw_utils.load_fileobj(xml_file, gz=xml_file.name.endswith(".gz"), contenthandler=h) seg_def_table = table.get_table(xmldoc, lsctables.SegmentDefTable.tableName) # put comment column into a dict comment_dict = {} for seg_def in seg_def_table: if include_version: full_channel_name = ':'.join( [str(seg_def.ifos), str(seg_def.name), str(seg_def.version)]) else: full_channel_name = ':'.join( [str(seg_def.ifos), str(seg_def.name)]) comment_dict[full_channel_name] = seg_def.comment return comment_dict
def get_segment_definer_comments(xml_file, include_version=True): """Returns a dict with the comment column as the value for each segment""" from pycbc.ligolw.ligolw import LIGOLWContentHandler as h lsctables.use_in(h) # read segment definer table xmldoc, _ = ligolw_utils.load_fileobj(xml_file, gz=xml_file.name.endswith(".gz"), contenthandler=h) seg_def_table = table.get_table(xmldoc, lsctables.SegmentDefTable.tableName) # put comment column into a dict comment_dict = {} for seg_def in seg_def_table: if include_version: full_channel_name = ':'.join([str(seg_def.ifos), str(seg_def.name), str(seg_def.version)]) else: full_channel_name = ':'.join([str(seg_def.ifos), str(seg_def.name)]) comment_dict[full_channel_name] = seg_def.comment return comment_dict
def select_segments_by_definer(segment_file, segment_name=None, ifo=None): """ Return the list of segments that match the segment name Parameters ---------- segment_file: str path to segment xml file segment_name: str Name of segment ifo: str, optional Returns ------- seg: list of segments """ from pycbc.ligolw.ligolw import LIGOLWContentHandler as h lsctables.use_in(h) indoc = ligolw_utils.load_filename(segment_file, False, contenthandler=h) segment_table = table.get_table(indoc, 'segment') seg_def_table = table.get_table(indoc, 'segment_definer') def_ifos = seg_def_table.getColumnByName('ifos') def_names = seg_def_table.getColumnByName('name') def_ids = seg_def_table.getColumnByName('segment_def_id') valid_id = [] for def_ifo, def_name, def_id in zip(def_ifos, def_names, def_ids): if ifo and ifo != def_ifo: continue if segment_name and segment_name != def_name: continue valid_id += [def_id] start = numpy.array(segment_table.getColumnByName('start_time')) start_ns = numpy.array(segment_table.getColumnByName('start_time_ns')) end = numpy.array(segment_table.getColumnByName('end_time')) end_ns = numpy.array(segment_table.getColumnByName('end_time_ns')) start, end = start + 1e-9 * start_ns, end + 1e-9 * end_ns did = segment_table.getColumnByName('segment_def_id') keep = numpy.array([d in valid_id for d in did]) if sum(keep) > 0: return start_end_to_segments(start[keep], end[keep]) else: return segmentlist([])
def select_segments_by_definer(segment_file, segment_name=None, ifo=None): """ Return the list of segments that match the segment name Parameters ---------- segment_file: str path to segment xml file segment_name: str Name of segment ifo: str, optional Returns ------- seg: list of segments """ from pycbc.ligolw.ligolw import LIGOLWContentHandler as h; lsctables.use_in(h) indoc = ligolw_utils.load_filename(segment_file, False, contenthandler=h) segment_table = table.get_table(indoc, 'segment') seg_def_table = table.get_table(indoc, 'segment_definer') def_ifos = seg_def_table.getColumnByName('ifos') def_names = seg_def_table.getColumnByName('name') def_ids = seg_def_table.getColumnByName('segment_def_id') valid_id = [] for def_ifo, def_name, def_id in zip(def_ifos, def_names, def_ids): if ifo and ifo != def_ifo: continue if segment_name and segment_name != def_name: continue valid_id += [def_id] start = numpy.array(segment_table.getColumnByName('start_time')) start_ns = numpy.array(segment_table.getColumnByName('start_time_ns')) end = numpy.array(segment_table.getColumnByName('end_time')) end_ns = numpy.array(segment_table.getColumnByName('end_time_ns')) start, end = start + 1e-9 * start_ns, end + 1e-9 * end_ns did = segment_table.getColumnByName('segment_def_id') keep = numpy.array([d in valid_id for d in did]) if sum(keep) > 0: return start_end_to_segments(start[keep], end[keep]) else: return segmentlist([])
def start_end_from_segments(segment_file): """ Return the start and end time arrays from a segment file. Parameters ---------- segment_file: xml segment file Returns ------- start: numpy.ndarray end: numpy.ndarray """ from pycbc.ligolw.ligolw import LIGOLWContentHandler as h; lsctables.use_in(h) indoc = ligolw_utils.load_filename(segment_file, False, contenthandler=h) segment_table = table.get_table(indoc, lsctables.SegmentTable.tableName) start = numpy.array(segment_table.getColumnByName('start_time')) start_ns = numpy.array(segment_table.getColumnByName('start_time_ns')) end = numpy.array(segment_table.getColumnByName('end_time')) end_ns = numpy.array(segment_table.getColumnByName('end_time_ns')) return start + start_ns * 1e-9, end + end_ns * 1e-9
def start_end_from_segments(segment_file): """ Return the start and end time arrays from a segment file. Parameters ---------- segment_file: xml segment file Returns ------- start: numpy.ndarray end: numpy.ndarray """ from pycbc.ligolw.ligolw import LIGOLWContentHandler as h lsctables.use_in(h) indoc = ligolw_utils.load_filename(segment_file, False, contenthandler=h) segment_table = table.get_table(indoc, lsctables.SegmentTable.tableName) start = numpy.array(segment_table.getColumnByName('start_time')) start_ns = numpy.array(segment_table.getColumnByName('start_time_ns')) end = numpy.array(segment_table.getColumnByName('end_time')) end_ns = numpy.array(segment_table.getColumnByName('end_time_ns')) return start + start_ns * 1e-9, end + end_ns * 1e-9
def __init__(self, filename, approximant=None, parameters=None, **kwds): self.has_compressed_waveforms = False ext = os.path.basename(filename) if ext.endswith(('.xml', '.xml.gz', '.xmlgz')): self.filehandler = None self.indoc = ligolw_utils.load_filename( filename, False, contenthandler=LIGOLWContentHandler) self.table = table.get_table( self.indoc, lsctables.SnglInspiralTable.tableName) self.table = pycbc.io.WaveformArray.from_ligolw_table(self.table, columns=parameters) # inclination stored in xml alpha3 column names = list(self.table.dtype.names) names = tuple([n if n != 'alpha3' else 'inclination' for n in names]) # low frequency cutoff in xml alpha6 column names = tuple([n if n!= 'alpha6' else 'f_lower' for n in names]) self.table.dtype.names = names elif ext.endswith(('hdf', '.h5')): self.indoc = None f = h5py.File(filename, 'r') self.filehandler = f try: fileparams = map(str, f.attrs['parameters']) except KeyError: # just assume all of the top-level groups are the parameters fileparams = map(str, f.keys()) logging.info("WARNING: no parameters attribute found. " "Assuming that %s " %(', '.join(fileparams)) + "are the parameters.") # use WaveformArray's syntax parser to figure out what fields # need to be loaded if parameters is None: parameters = fileparams common_fields = list(pycbc.io.WaveformArray(1, names=parameters).fieldnames) add_fields = list(set(parameters) & (set(fileparams) - set(common_fields))) # load dtype = [] data = {} for key in common_fields+add_fields: data[str(key)] = f[key][:] dtype.append((str(key), data[key].dtype)) num = f[fileparams[0]].size self.table = pycbc.io.WaveformArray(num, dtype=dtype) for key in data: self.table[key] = data[key] # add the compressed waveforms, if they exist self.has_compressed_waveforms = 'compressed_waveforms' in f else: raise ValueError("Unsupported template bank file extension %s" %( ext)) # if approximant is specified, override whatever was in the file # (if anything was in the file) if approximant is not None: # get the approximant for each template apprxs = self.parse_approximant(approximant) if 'approximant' not in self.table.fieldnames: self.table = self.table.add_fields(apprxs, 'approximant') else: self.table['approximant'] = apprxs self.extra_args = kwds self.ensure_hash()
def __init__(self, sim_file, **kwds): self.indoc = ligolw_utils.load_filename( sim_file, False, contenthandler=LIGOLWContentHandler) self.table = table.get_table( self.indoc, lsctables.SimBurstTable.tableName) self.extra_args = kwds
def __init__(self, filename, approximant=None, parameters=None, **kwds): self.has_compressed_waveforms = False ext = os.path.basename(filename) if ext.endswith(('.xml', '.xml.gz', '.xmlgz')): self.filehandler = None self.indoc = ligolw_utils.load_filename( filename, False, contenthandler=LIGOLWContentHandler) self.table = table.get_table(self.indoc, lsctables.SnglInspiralTable.tableName) self.table = pycbc.io.WaveformArray.from_ligolw_table( self.table, columns=parameters) # inclination stored in xml alpha3 column names = list(self.table.dtype.names) names = tuple( [n if n != 'alpha3' else 'inclination' for n in names]) # low frequency cutoff in xml alpha6 column names = tuple([n if n != 'alpha6' else 'f_lower' for n in names]) self.table.dtype.names = names elif ext.endswith(('hdf', '.h5')): self.indoc = None f = h5py.File(filename, 'r') self.filehandler = f try: fileparams = map(str, f.attrs['parameters']) except KeyError: # just assume all of the top-level groups are the parameters fileparams = map(str, f.keys()) logging.info("WARNING: no parameters attribute found. " "Assuming that %s " % (', '.join(fileparams)) + "are the parameters.") # use WaveformArray's syntax parser to figure out what fields # need to be loaded if parameters is None: parameters = fileparams common_fields = list( pycbc.io.WaveformArray(1, names=parameters).fieldnames) add_fields = list( set(parameters) & (set(fileparams) - set(common_fields))) # load dtype = [] data = {} for key in common_fields + add_fields: data[str(key)] = f[key][:] dtype.append((str(key), data[key].dtype)) num = f[fileparams[0]].size self.table = pycbc.io.WaveformArray(num, dtype=dtype) for key in data: self.table[key] = data[key] # add the compressed waveforms, if they exist self.has_compressed_waveforms = 'compressed_waveforms' in f else: raise ValueError("Unsupported template bank file extension %s" % (ext)) # if approximant is specified, override whatever was in the file # (if anything was in the file) if approximant is not None: # get the approximant for each template apprxs = self.parse_approximant(approximant) if 'approximant' not in self.table.fieldnames: self.table = self.table.add_fields(apprxs, 'approximant') else: self.table['approximant'] = apprxs self.extra_args = kwds self.ensure_hash()
def __init__(self, sim_file, **kwds): self.indoc = ligolw_utils.load_filename( sim_file, False, contenthandler=LIGOLWContentHandler) self.table = table.get_table(self.indoc, lsctables.SimBurstTable.tableName) self.extra_args = kwds
# Grab all relevant Omicron trigger files omicron_times = [] omicron_snr = [] omicron_freq = [] for era in eras: # Generate list of all Omicron SnglBurst xml trigger files file_list = glob.glob(args.omicron_dir + '/%s/%s_Omicron/%s/%s-%s_Omicron-*.xml.gz' %(args.ifo,args.omicron_channel,era,args.ifo,args.omicron_channel.replace('-','_'))) # Parse trigger files into SNR, time, and frequency for Omicron triggers for file_name in file_list: omicron_xml = utils.load_filename(file_name, contenthandler=DefaultContentHandler) snglburst_table = table.get_table(omicron_xml, lsctables.SnglBurstTable.tableName) for row in snglburst_table: if (row.snr > args.omicron_snr_thresh and omicron_start_time < row.peak_time < omicron_end_time): omicron_times.append(row.peak_time + row.peak_time_ns * 10**(-9)) omicron_snr.append(row.snr) omicron_freq.append(row.peak_frequency) # Generate inspiral waveform and calculate f(t) to plot on top of Omicron triggers hp, hc = get_td_waveform(approximant='SEOBNRv2', mass1=m1, mass2=m2, spin1x=0, spin1y=0, spin1z=s1z, spin2x=0, spin2y=0, spin2z=s2z, delta_t=(1./32768.), f_lower=30)