def test_get_ligolw_element(): from glue.ligolw.ligolw import (Document, LIGO_LW) xmldoc = Document() llw = xmldoc.appendChild(LIGO_LW()) assert io_ligolw.get_ligolw_element(llw) is llw assert io_ligolw.get_ligolw_element(xmldoc) is llw with pytest.raises(ValueError): io_ligolw.get_ligolw_element(Document())
def test_open_xmldoc(self): from glue.ligolw.ligolw import (Document, LIGO_LW) assert isinstance(io_ligolw.open_xmldoc(tempfile.mktemp()), Document) with tempfile.TemporaryFile(mode='w') as f: xmldoc = Document() xmldoc.appendChild(LIGO_LW()) xmldoc.write(f) f.seek(0) assert isinstance(io_ligolw.open_xmldoc(f), Document)
def llwtable(): from glue.ligolw.ligolw import (Document, LIGO_LW) # build dummy document with two tables xmldoc = Document() llw = xmldoc.appendChild(LIGO_LW()) tables = [new_table('process'), new_table('sngl_ringdown')] for t in tables: llw.appendChild(t) return xmldoc
def open_xmldoc(fobj, **kwargs): """Try and open an existing LIGO_LW-format file, or create a new Document Parameters ---------- fobj : `str`, `file` file path or open file object to read **kwargs other keyword arguments to pass to :func:`~glue.ligolw.utils.load_filename`, or :func:`~glue.ligolw.utils.load_fileobj` as appropriate Returns -------- xmldoc : :class:`~glue.ligolw.ligolw.Document` either the `Document` as parsed from an existing file, or a new, empty `Document` """ from glue.ligolw.lsctables import use_in from glue.ligolw.ligolw import (Document, LIGOLWContentHandler) from glue.ligolw.utils import load_filename, load_fileobj try: # try and load existing file if isinstance(fobj, string_types): kwargs.setdefault('contenthandler', use_in(LIGOLWContentHandler)) return load_filename(fobj, **kwargs) if isinstance(fobj, FILE_LIKE): kwargs.setdefault('contenthandler', use_in(LIGOLWContentHandler)) return load_fileobj(fobj, **kwargs)[0] except (OSError, IOError): # or just create a new Document return Document()
def write_ligolw(flag, fobj, **kwargs): """Write this `DataQualityFlag` to XML in LIGO_LW format """ # if given a Document, just add data if isinstance(fobj, Document): return write_to_xmldoc(flag, fobj, **kwargs) # otherwise build a new Document xmldoc = Document() xmldoc.appendChild(LIGO_LW()) # TODO: add process information write_to_xmldoc(flag, xmldoc) # and write if isinstance(fobj, string_types): return write_filename(xmldoc, fobj, gz=fobj.endswith('.gz')) else: return write_fileobj(xmldoc, fobj, gz=fobj.name.endswith('.gz'))
def write_ligolw(flag, fobj, **kwargs): """Write this `DataQualityFlag` to XML in LIGO_LW format """ if isinstance(fobj, Document): return write_to_xmldoc(flag, fobj, **kwargs) elif isinstance(fobj, (str, unicode)): fobj = open(fobj, 'w') close = True else: close = False xmldoc = Document() xmldoc.appendChild(LIGO_LW()) # TODO: add process information write_to_xmldoc(flag, xmldoc) xmldoc.write(fobj) if close: fobj.close()
def test_list_tables(self): from glue.ligolw import lsctables from glue.ligolw.ligolw import (Document, LIGO_LW) # build dummy document with two tables xmldoc = Document() llw = xmldoc.appendChild(LIGO_LW()) tables = [lsctables.New(lsctables.ProcessTable), lsctables.New(lsctables.SnglRingdownTable)] names = [t.TableName(t.Name) for t in tables] [llw.appendChild(t) for t in tables] # add tables to xmldoc # check that tables are listed properly assert io_ligolw.list_tables(xmldoc) == names # check that we can list from files with tempfile.NamedTemporaryFile(mode='w') as f: xmldoc.write(f) f.seek(0) assert io_ligolw.list_tables(f) == names
def write_tables(target, tables, append=False, overwrite=False, **kwargs): """Write an LIGO_LW table to file Parameters ---------- target : `str`, `file`, :class:`~glue.ligolw.ligolw.Document` the file or document to write into tables : `list`, `tuple` of :class:`~glue.ligolw.table.Table` the tables to write append : `bool`, optional, default: `False` if `True`, append to an existing file/table, otherwise `overwrite` overwrite : `bool`, optional, default: `False` if `True`, delete an existing instance of the table type, otherwise append new rows **kwargs other keyword arguments to pass to :func:`~glue.ligolw.utils.load_filename`, or :func:`~glue.ligolw.utils.load_fileobj` as appropriate """ from glue.ligolw.ligolw import (Document, LIGO_LW, LIGOLWContentHandler) from glue.ligolw import utils as ligolw_utils # allow writing directly to XML if isinstance(target, (Document, LIGO_LW)): xmldoc = target # open existing document, if possible elif append and not overwrite: xmldoc = open_xmldoc(target, contenthandler=kwargs.pop('contenthandler', LIGOLWContentHandler)) # fail on existing document and not overwriting elif (not overwrite and isinstance(target, string_types) and os.path.isfile(target)): raise IOError("File exists: {}".format(target)) else: # or create a new document xmldoc = Document() # convert table to format write_tables_to_document(xmldoc, tables, overwrite=overwrite) # write file if isinstance(target, string_types): kwargs.setdefault('gz', target.endswith('.gz')) ligolw_utils.write_filename(xmldoc, target, **kwargs) elif isinstance(target, FILE_LIKE): kwargs.setdefault('gz', target.name.endswith('.gz')) ligolw_utils.write_fileobj(xmldoc, target, **kwargs)
def open_xmldoc(f, **kwargs): """Try and open an existing LIGO_LW-format file, or create a new Document """ from glue.ligolw.lsctables import use_in from glue.ligolw.ligolw import Document from glue.ligolw.utils import load_filename, load_fileobj use_in(kwargs['contenthandler']) try: # try and load existing file if isinstance(f, string_types): return load_filename(f, **kwargs) if isinstance(f, FILE_LIKE): return load_fileobj(f, **kwargs)[0] except (OSError, IOError): # or just create a new Document return Document()
def test_open_xmldoc(): from glue.ligolw.ligolw import (Document, LIGO_LW) assert isinstance(io_ligolw.open_xmldoc(tempfile.mktemp()), Document) with tempfile.TemporaryFile(mode='w') as f: xmldoc = Document() xmldoc.appendChild(LIGO_LW()) xmldoc.write(f) f.seek(0) assert isinstance(io_ligolw.open_xmldoc(f), Document)
def read_ligolw(source, contenthandler=None, verbose=False, non_lsc_tables_ok=True): """Read one or more LIGO_LW format files Parameters ---------- source : `str`, `file`, `list` one or more open files or file paths to read contenthandler : `~xml.sax.handler.ContentHandler`, optional content handler used to parse document verbose : `bool`, optional be verbose when reading files, default: `False` non_lsc_tables_ok : `bool`, optional if `False` error on unrecognised tables in documents, default: `True` Returns ------- xmldoc : :class:`~glue.ligolw.ligolw.Document` the document object as parsed from the file(s) """ from glue.ligolw.ligolw import (Document, LIGOLWContentHandler) from glue.ligolw import types from glue.ligolw.lsctables import use_in from glue.ligolw.utils.ligolw_add import ligolw_add # mock ToPyType to link to numpy dtypes topytype = types.ToPyType.copy() for key in types.ToPyType: if key in types.ToNumPyType: types.ToPyType[key] = numpy.dtype(types.ToNumPyType[key]).type # set default content handler if contenthandler is None: contenthandler = use_in(LIGOLWContentHandler) # read one or more files into a single Document try: return ligolw_add(Document(), file_list(source), contenthandler=contenthandler, verbose=verbose, non_lsc_tables_ok=non_lsc_tables_ok) finally: # replace ToPyType types.ToPyType = topytype
def write_tables(f, tables, append=False, overwrite=False, **kwargs): """Write an LIGO_LW table to file Parameters ---------- f : `str`, `file`, :class:`~glue.ligolw.ligolw.Document` the file or document to write into tables : `list` of :class:`~glue.ligolw.table.Table` the tables to write append : `bool`, optional, default: `False` if `True`, append to an existing file/table, otherwise `overwrite` overwrite : `bool`, optional, default: `False` if `True`, delete an existing instance of the table type, otherwise append new rows """ from glue.ligolw.ligolw import (Document, LIGO_LW, LIGOLWContentHandler) from glue.ligolw import utils as ligolw_utils # allow writing directly to XML if isinstance(f, (Document, LIGO_LW)): xmldoc = f # open existing document, if possible elif append and not overwrite: xmldoc = open_xmldoc(f, contenthandler=kwargs.pop('contenthandler', LIGOLWContentHandler)) # fail on existing document and not overwriting elif not overwrite and isinstance(f, string_types) and os.path.isfile(f): raise IOError("File exists: %s" % f) else: # or create a new document xmldoc = Document() # convert table to format write_tables_to_document(xmldoc, tables, overwrite=overwrite) # write file if isinstance(f, string_types): kwargs.setdefault('gz', f.endswith('.gz')) ligolw_utils.write_filename(xmldoc, f, **kwargs) elif not isinstance(f, Document): kwargs.setdefault('gz', f.name.endswith('.gz')) ligolw_utils.write_fileobj(xmldoc, f, **kwargs)
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
import sys, os import glob import re from pylal.dq.dqTriggerUtils import fromkwfile from glue.ligolw.utils import write_filename, write_fileobj from glue.ligolw.ligolw import LIGO_LW, Document from glue.ligolw import lsctables from glue.ligolw import ilwd if len(glob.glob("%s/*.trg" % sys.argv[1])) == 0: print "No .trg files found. Nothing to convert." sys.exit(0) for f in glob.glob("%s/*.trg" % sys.argv[1]): #print f xmldoc = Document() xmldoc.appendChild(LIGO_LW()) sbt = lsctables.New(lsctables.SnglBurstTable, [ "ifo", "peak_time", "peak_time_ns", "event_id", "process_id", "start_time", "start_time_ns", "confidence", "chisq", "chisq_dof", "amplitude", "duration", "search", "central_freq", "channel", "snr", "bandwidth" ]) #H1_TCS-ITMY_PD_ISS_OUT_AC_1_1024.xml fspl = os.path.basename(f).split("_") ifo = fspl[0] channel = "_".join(fspl[1:-2]) sbt += fromkwfile(f, ifo=ifo, channel=channel, columns=[
def read_flag_dict(f, flags=None, gpstype=LIGOTimeGPS, coalesce=False, contenthandler=GWpyContentHandler, nproc=1): """Read segments for the given flag from the LIGO_LW XML file. Parameters ---------- fp : `str` path of XML file to read. flags : `list`, `None`, optional list of flags to read or `None` to read all into a single `DataQualityFlag`. Returns ------- flagdict : :class:`~gwpy.segments.flag.DataQualityDict` a new `DataQualityDict` of `DataQualityFlag` entries with ``active`` and ``known`` segments seeded from the XML tables in the given file ``fp``. """ if nproc != 1: return DataQualityDict.read(f, flags, coalesce=coalesce, gpstype=gpstype, contenthandler=contenthandler, format='cache', nproc=nproc) # generate Document and populate xmldoc = Document() files = [fp.name if isinstance(fp, (file, GzipFile)) else fp for fp in file_list(f)] ligolw_add(xmldoc, files, non_lsc_tables_ok=True, contenthandler=contenthandler) # read segment definers and generate DataQualityFlag object seg_def_table = lsctables.SegmentDefTable.get_table(xmldoc) # find flags if isinstance(flags, (unicode, str)): flags = flags.split(',') out = DataQualityDict() id_ = dict() if flags is not None and len(flags) == 1 and flags[0] is None: out[None] = DataQualityFlag() id_[None] = [] for row in seg_def_table: ifos = row.get_ifos() name = row.name if ifos and name: name = ':'.join([''.join(row.get_ifos()), row.name]) if row.version is not None: name += ':%d' % row.version else: name = None if flags is None or name in flags: out[name] = DataQualityFlag(name) try: id_[name].append(row.segment_def_id) except (AttributeError, KeyError): id_[name] = [row.segment_def_id] if flags is None and not len(out.keys()): raise RuntimeError("No segment definitions found in file.") elif flags is not None and len(out.keys()) != len(flags): for flag in flags: if flag not in out: raise ValueError("No segment definition found for flag=%r " "in file." % flag) # read segment summary table as 'known' seg_sum_table = lsctables.SegmentSumTable.get_table(xmldoc) for row in seg_sum_table: for flag in out: if not id_[flag] or row.segment_def_id in id_[flag]: try: s = row.get() except AttributeError: s = row.start_time, row.end_time out[flag].known.append(Segment(gpstype(s[0]), gpstype(s[1]))) for dqf in out: if coalesce: out[dqf].coalesce() # read segment table as 'active' seg_table = lsctables.SegmentTable.get_table(xmldoc) for row in seg_table: for flag in out: if not id_[flag] or row.segment_def_id in id_[flag]: try: s = row.get() except AttributeError: s = row.start_time, row.end_time out[flag].active.append(Segment(gpstype(s[0]), gpstype(s[1]))) for dqf in out: if coalesce: out[dqf].coalesce() return out
import sys, os import glob import re from pylal.dq.dqTriggerUtils import fromkwfile from glue.ligolw.utils import write_filename, write_fileobj from glue.ligolw.ligolw import LIGO_LW, Document from glue.ligolw import lsctables from glue.ligolw import ilwd if len(glob.glob("%s/*.trg" % sys.argv[1])) == 0: print "No .trg files found. Nothing to convert." sys.exit(0) for f in glob.glob("%s/*.trg" % sys.argv[1]): #print f xmldoc = Document() xmldoc.appendChild( LIGO_LW() ) sbt = lsctables.New(lsctables.SnglBurstTable, ["ifo", "peak_time", "peak_time_ns", "event_id", "process_id", "start_time", "start_time_ns", "confidence", "chisq", "chisq_dof", "amplitude", "duration", "search", "central_freq", "channel", "snr", "bandwidth"]) #H1_TCS-ITMY_PD_ISS_OUT_AC_1_1024.xml fspl = os.path.basename(f).split("_") ifo = fspl[0] channel = "_".join( fspl[1:-2] ) sbt += fromkwfile( f, ifo=ifo, channel=channel, columns = ["duration", "start_time", "peak_time", "central_freq", "bandwidth", "snr", "confidence"] ) for i, sb in enumerate(sbt): sb.search = "KleineWelle" sb.process_id = ilwd.ilwdchar("process:process_id:0")