gstlal-inspiral  1.9.3
 All Classes Namespaces Files Functions
ilwdify.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2019 Kipp Cannon
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3 of the License, or (at your
8 # option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19 
20 #
21 # =============================================================================
22 #
23 # Preamble
24 #
25 # =============================================================================
26 #
27 
28 
29 """
30 Revert an XML document tree to "iwld:char" format for upload to gracedb.
31 This does not solve the generic problem of document conversion, it is
32 specifically written for the case of gstlal_inspiral's gracedb uploads.
33 """
34 
35 
36 from glue.ligolw import ilwd
37 from glue.ligolw import ligolw
38 from glue.ligolw import lsctables
39 from ligo.lw.lsctables import TableByName as ligo_lw_TableByName
40 from ligo.lw.param import Param as ligo_lw_Param
41 from ligo.lw.table import Column as ligo_lw_Column
42 
43 
44 #
45 # dictionary mapping lsctables table class to dictionary mapping column
46 # name to ilwd:char class. we only consider tables that are named in both
47 # glue.ligolw.lsctables and ligo.lw.lsctables, assuming we won't be
48 # converting documents that contain tables that have been removed from the
49 # latter.
50 #
51 
52 ilwdchar_tables = dict((tblname, dict((ligo_lw_Column.ColumnName(colname), None) for colname, coltype in tblcls.validcolumns.items() if coltype == u"ilwd:char")) for tblname, tblcls in lsctables.TableByName.items() if tblname in ligo_lw_TableByName and u"ilwd:char" in tblcls.validcolumns.values())
53 
54 destrip_column = dict((key, dict(value)) for key, value in ilwdchar_tables.items())
55 for tblname, colnamemap in destrip_column.items():
56  for colname in list(colnamemap):
57  colnamemap[colname], = (destripped for destripped in ligo_lw_TableByName[tblname].validcolumns if ligo_lw_Column.ColumnName(destripped) == colname)
58 
59 for tblname, colnamemap in ilwdchar_tables.items():
60  for colname in list(colnamemap):
61  destripped = destrip_column[tblname][colname]
62  try:
63  tblprefix = ligo_lw_Column.ColumnName.table_name(destripped)
64  except ValueError:
65  # columns whose names don't have a prefix are in
66  # their own table
67  tblprefix = tblname
68  colnamemap[colname] = ilwd.get_ilwdchar_class(tblprefix, ligo_lw_Column.ColumnName(colname))
69 
70 
71 #
72 # =============================================================================
73 #
74 # Main
75 #
76 # =============================================================================
77 #
78 
79 
80 def do_it_to(xmldoc):
81  """
82  NOTE: this performs an in-place transcription of the contents of
83  the XML document tree. This should be assumed to be a destructive
84  operation on the contents of the tree. If you wish to hold
85  references to any of the Table elements or other structures in the
86  tree and wish them to remain intact so they can be used afterwards,
87  make copies first
88  """
89  #
90  # walk the tree finding Table elements
91  #
92 
93  for table in list(xmldoc.getElementsByTagName(ligolw.Table.tagName)):
94  #
95  # this is not the table we're looking for
96  #
97 
98  if table.Name not in ilwdchar_tables:
99  continue
100 
101  #
102  # make a copy of the table with glue.ligolw's lsctables and
103  # replace the old table with the new table in the XML tree
104  #
105 
106  newtable = table.parentNode.replaceChild(lsctables.New(lsctables.TableByName[table.Name], table.columnnames), table)
107 
108  #
109  # build a row transcription function for this table
110  #
111 
112  if table.Name != "coinc_event_map":
113  ilwdclsmap = ilwdchar_tables[table.Name]
114  newrowtype = newtable.RowType
115  def newrow(row, nonilwdcharattrs = tuple(colname for colname in table.columnnames if colname not in ilwdclsmap), ilwdcharattrs = tuple(colname for colname in table.columnnames if colname in ilwdclsmap)):
116  kwargs = dict((attr, getattr(row, attr)) for attr in nonilwdcharattrs)
117  kwargs.update((attr, ilwdclsmap[attr](getattr(row, attr))) for attr in ilwdcharattrs)
118  return newrowtype(**kwargs)
119  else:
120  # event_id IDs obtain their table name prefix from
121  # the table_name column
122  newrowtype = newtable.RowType
123  def newrow(row, coinc_id_ilwdcls = ilwdchar_tables["coinc_event"]["coinc_event_id"]):
124  # FIXME this is probably a dumb way to do this,
125  # but it shouldn't matter once we have no
126  # reason to convert back to ilwdchar
127  if "event_id" in ilwdchar_tables[row.table_name]:
128  event_id = ilwdchar_tables[row.table_name]["event_id"](row.event_id)
129  elif "simulation_id" in ilwdchar_tables[row.table_name]:
130  event_id = ilwdchar_tables[row.table_name]["simulation_id"](row.event_id)
131  elif "coinc_event_id" in ilwdchar_tables[row.table_name]:
132  event_id = ilwdchar_tables[row.table_name]["coinc_event_id"](row.event_id)
133  else:
134  raise KeyError("event_id, simulation_id or coinc_event_id not in " + ilwdchar_tables[row.table_name])
135  return newrowtype(
136  table_name = row.table_name,
137  event_id = event_id,
138  coinc_event_id = coinc_id_ilwdcls(row.coinc_event_id)
139  )
140 
141  #
142  # transcribe rows from the old table into the new table
143  #
144 
145  newtable.extend(newrow(row) for row in table)
146 
147  #
148  # dispose of the old table
149  #
150 
151  table.unlink()
152 
153  #
154  # walk the tree looking for Param elements containing sngl_inspiral
155  # IDs and convert to ilwd:char
156  #
157 
158  ilwdcls = ilwdchar_tables["sngl_inspiral"]["event_id"]
159  for param in list(ligo_lw_Param.getParamsByName(xmldoc, "event_id")):
160  param.Type = u"ilwd:char"
161  param.pcdata = ilwdcls(param.pcdata)
162 
163  #
164  # done
165  #
166 
167  return xmldoc