def from_recarray(cls, array, columns=None): """Create a new table from a `numpy.recarray` Parameters ---------- array : `numpy.recarray` an array of data column : `list` of `str`, optional the columns to populate, if not given, all columns present in the `~numpy.recarray` are mapped Notes ----- The columns populated in the `numpy.recarray` must all map exactly to valid columns of the target `~glue.ligolw.table.Table`. """ if columns is None: columns = list(array.dtype.names) out = New(cls, columns=columns) tblname = strip_table_name(out.tableName) ilwdchar = dict((col, get_ilwdchar_class(tblname, col)) for (col, llwtype) in zip(out.columnnames, out.columntypes) if llwtype == 'ilwd:char') for rec in array: row = out.RowType() for col, llwtype in zip(out.columnnames, out.columntypes): if llwtype == 'ilwd:char': setattr(row, col, ilwdchar[col](rec[col])) else: setattr(row, col, rec[col]) out.append(row) return out
def table_to_ligolw(table, tablename): """Convert a `astropy.table.Table` to a :class:`glue.ligolw.table.Table` """ from glue.ligolw import (lsctables, types) from glue.ligolw.ilwd import get_ilwdchar_class # create new LIGO_LW table columns = table.columns.keys() table_class = lsctables.TableByName[tablename] llwtable = lsctables.New(table_class, columns=columns) # map rows across for row in table: llwrow = llwtable.RowType() for name in columns: llwtype = llwtable.validcolumns[name] if row[name] is None: val = None elif llwtype == 'ilwd:char': val = get_ilwdchar_class(tablename, name)(row[name]) else: val = types.ToPyType[llwtype](row[name]) setattr(llwrow, name, val) llwtable.append(llwrow) return llwtable
def _to_ilwd(value, tablename, colname): from glue.ligolw.ilwd import (ilwdchar, get_ilwdchar_class) from glue.ligolw._ilwd import ilwdchar as IlwdChar if isinstance(value, IlwdChar) and value.column_name != colname: raise ValueError("ilwdchar '{0!s}' doesn't match column " "{1!r}".format(value, colname)) if isinstance(value, IlwdChar): return value if isinstance(value, int): return get_ilwdchar_class(tablename, colname)(value) return ilwdchar(value)
def test_read_write_ligolw_ilwdchar_compat(self): from glue.ligolw.ilwd import get_ilwdchar_class from glue.ligolw.lsctables import SnglBurstTable eid_type = get_ilwdchar_class("sngl_burst", "event_id") table = self.create( 100, ["peak", "snr", "central_freq", "event_id"], ["f8", "f4", "f4", "i8"], ) with tempfile.NamedTemporaryFile(suffix=".xml") as tmp: # write table with ilwdchar_compat=True table.write(tmp, format="ligolw", tablename="sngl_burst", ilwdchar_compat=True) # read raw ligolw and check type is correct llw = io_ligolw.read_table(tmp, tablename="sngl_burst", ilwdchar_compat=True) assert type(llw.getColumnByName("event_id")[0]) is eid_type # reset IDs to 0 SnglBurstTable.reset_next_id() # read without explicit use of ilwdchar_compat t2 = self.TABLE.read(tmp, columns=table.colnames) assert type(t2[0]["event_id"]) is eid_type # read again with explicit use of ilwdchar_compat SnglBurstTable.reset_next_id() utils.assert_table_equal( self.TABLE.read(tmp, columns=table.colnames, ilwdchar_compat=True), t2, ) # and check that ilwdchar_compat=True, use_numpy_dtypes=True works SnglBurstTable.reset_next_id() utils.assert_table_equal( self.TABLE.read(tmp, columns=table.colnames, ilwdchar_compat=True, use_numpy_dtypes=True), table, almost_equal=True, )
for colname in list(colnamemap): colnamemap[colname], = ( destripped for destripped in ligo_lw_TableByName[tblname].validcolumns if ligo_lw_Column.ColumnName(destripped) == colname) for tblname, colnamemap in ilwdchar_tables.items(): for colname in list(colnamemap): destripped = destrip_column[tblname][colname] try: tblprefix = ligo_lw_Column.ColumnName.table_name(destripped) except ValueError: # columns whose names don't have a prefix are in # their own table tblprefix = tblname colnamemap[colname] = ilwd.get_ilwdchar_class( tblprefix, ligo_lw_Column.ColumnName(colname)) # # ============================================================================= # # Main # # ============================================================================= # def do_it_to(xmldoc): """ NOTE: this performs an in-place transcription of the contents of the XML document tree. This should be assumed to be a destructive operation on the contents of the tree. If you wish to hold
__date__ = git_version.date ## \addtogroup laldetchar_py_idq_idq_tables # @{ # contains definitions for iDQ xml table classes and objects # # ============================================================================= # # idq_glitch:table # # ============================================================================= # IDQGlitchID = ilwd.get_ilwdchar_class(u"idq_glitch", u"event_id") class IDQGlitchTable(table.Table): tableName = 'idq_glitch:table' validcolumns = { 'event_id': 'ilwd:char', 'ifo': 'lstring', 'gps': 'int_4s', 'gps_ns': 'int_4s', 'rank': 'real_4', 'fap': 'real_4', 'likelihood': 'real_4', } constraints = "PRIMARY KEY (event_id)"
# @{ # contains definitions for iDQ xml table classes and objects # # ============================================================================= # # idq_glitch:table # # ============================================================================= # IDQGlitchID = ilwd.get_ilwdchar_class(u"idq_glitch", u"event_id") class IDQGlitchTable(table.Table): tableName = 'idq_glitch:table' validcolumns = { 'event_id': 'ilwd:char', 'ifo': 'lstring', 'gps': 'int_4s', 'gps_ns': 'int_4s', 'rank': 'real_4', 'fap': 'real_4', 'likelihood': 'real_4', } constraints = "PRIMARY KEY (event_id)" next_id = IDQGlitchID(0)