CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
bookConverter.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import xml.dom.minidom as dom
3 import sys, os, optparse
4 
5 class OptionParser(optparse.OptionParser):
6  """
7  OptionParser is main class to parse options.
8  """
9  def __init__(self):
10  optparse.OptionParser.__init__(self, usage="%prog --help or %prog [options] file", version="%prog 0.0.1", conflict_handler="resolve")
11  self.add_option("--src", action="store", type="string", dest="src", help="specify source XML file")
12  self.add_option("--min", action="store", type="int", dest="min", help="Minimum length to measure")
13  self.add_option("--max", action="store", type="int", dest="max", help="Maximum length to measure")
14  self.add_option("--cid", action="store", type="int", dest="cid", help="Apply combination ID")
15  self.add_option("--xsd", action="store_true", dest="xsd", help="Create XML Schema fragment")
16 
17 def read_data():
18  print("Reading histogram file")
19  n = 0
20  histos = srcdoc.getElementsByTagName("Histogram")
21  for histo in histos:
22  h = []
23  for key in histo.childNodes:
24  if key.nodeType == key.ELEMENT_NODE:
25  name = key.localName
26  value = key.childNodes[0].nodeValue
27  found = 0
28 
29  if name not in elements:
30  elements[name] = {'type': '', 'count': 0}
31  elements[name]['count'] = elements[name]['count'] + 1
32 
33  try:
34  i = int(value)
35  if elements[name]['type'] == '':
36  elements[name]['type'] = 'xs:integer'
37  except ValueError:
38  try:
39  i = float(value)
40  if elements[name]['type'] in ('', 'xs:integer'):
41  elements[name]['type'] = 'xs:double'
42  except ValueError:
43  elements[name]['type'] = 'xs:string'
44 
45  for k in keys.keys():
46  if keys[k]['name'] == name and keys[k]['value'] == value:
47  keys[k]['count'] = keys[k]['count'] + 1
48  h.append(k)
49  found = 1
50  break
51  if found == 0:
52  keys[n] = {'name': name, 'value': value, 'count': 1}
53  h.append(n)
54  n += 1
55  h.sort()
56  histograms.append(h)
57 
58 def create_xsd():
59  for k in keys.keys():
60  name = keys[k]['name']
61 
62  root = resdoc.createElement("xs:complexType")
63  root.setAttribute("name", "HistogramType")
64  resdoc.appendChild(root)
65  seq = resdoc.createElement("xs:all")
66  root.appendChild(seq)
67  for e in sorted(elements.keys()):
68  el = resdoc.createElement("xs:element")
69  el.setAttribute("name", e)
70  el.setAttribute("type", elements[e]['type'])
71  if elements[e]['count'] < len(histograms):
72  el.setAttribute("minOccurs", '0')
73  el.setAttribute("maxOccurs", '1')
74  seq.appendChild(el)
75 
77  co = comb[cid]
78  print("Declaration to apply:", co)
79  for k in comb[cid]:
80  print(keys[k]['name'], '=', keys[k]['value'])
81 
82 def cexists(s, c):
83  d = len(c)
84  for v1 in s:
85  for v2 in c:
86  if v1 == v2:
87  d = d - 1
88  return (d == 0)
89 
90 def ccopy(a):
91  r = []
92  for v in a:
93  r.append(v)
94  return r
95 
96 def kpermutation(vfrom, vto, min, max):
97  vto = vto + 1
98  queue = []
99  for i in range(vfrom, vto):
100  for j in range(i, vto):
101  queue.append(j)
102  if len(queue) >= min and len(queue) <= max:
103  yield queue
104  queue = []
105 
106 def compute(min, max):
107  print("Computing permutations")
108  for v in kpermutation(0, len(keys), min, max):
109  ci = -1
110  for h in histograms:
111  if cexists(h, v):
112  if ci == -1:
113  ci = len(comb)
114  comb[ci] = ccopy(v)
115  results[ci] = [h]
116  else:
117  results[ci].append(h)
118 
120  for ci in comb.keys():
121  l = len(results[ci])
122  if l == 1:
123  continue
124  if l not in prior:
125  prior[l] = [ci]
126  else:
127  prior[l].append(ci)
128 
129 if __name__ == "__main__":
130 
131  optManager = OptionParser()
132  (opts, args) = optManager.parse_args()
133  opts = opts.__dict__
134 
135  if opts['src'] in ('', None):
136  print("You must specify a valid source xml file")
137  sys.exit(0)
138 
139  resdoc = dom.Document()
140  srcdoc = dom.parse(opts['src'])
141 
142  histograms = []
143  keys = {}
144  results = {}
145  comb = {}
146  prior = {}
147  elements = {}
148  len_min = 1000000
149  len_max = 0
150 
151  read_data()
152 
153  if opts['xsd'] != None:
154 
155  create_xsd()
156  print(resdoc.toprettyxml())
157 
158  else:
159 
160  for h in histograms:
161  if len(h) > len_max: len_max = len(h)
162  if len(h) < len_min: len_min = len(h)
163  print("Computed len: min = ", len_min, ", max = ", len_max)
164 
165  min = 2
166  if opts['min'] not in (0, None): min = opts['min']
167  max = len_max
168  if opts['max'] not in (0, None): max = opts['max']
169  print("Computing lens from", min, " to ", max)
170 
171  compute(min, max)
172  priorities()
173 
174  for pi in sorted(prior.keys()):
175  print(pi, "=", prior[pi])
176 
177  if opts['cid'] != None:
178  create_declaration(opts['cid'])
def create_declaration
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
const uint16_t range(const Frame &aFrame)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47