I hope I understood correctly what is this: it's automatically changed by the build...
[guess.git] / scripts / MainPre.py
blob1668c718708091d64b3cfa702f9f9be87f2cfa00
1 def addNode(_name,**_extra):
2 _temp = g.addNode(_name)
3 if not isinstance(_name,Class.forName("com.hp.hpl.guess.Node")):
4 _temp.label = _name
5 for _toset in _extra.keys():
6 _temp.__setattr__(_toset,_extra[_toset])
7 return _temp
9 def copyNode(_template,_name,**_extra):
10 if not isinstance(_template,Class.forName("com.hp.hpl.guess.Node")):
11 raise TypeError,'First argument nust be a template Node'
12 if not isinstance(_name,Class.forName("org.python.core.PyString")):
13 raise TypeError,'Second argument must be a name'
14 _temp = g.addNode(_name)
15 for _field in Node.fieldNames():
16 if (_field != "name"):
17 _temp.__setattr__(_field,_template.__getattr__(_field))
18 for _toset in _extra.keys():
19 _temp.__setattr__(_toset,_extra[_toset])
20 return(_temp)
22 def addEdge(*d,**_extra):
23 return addUndirectedEdge(*d,**_extra)
25 def addDirectedEdge(*d,**_extra):
26 _temp = None
27 if d.__len__() == 1:
28 _temp = g.addEdge(d[0])
29 elif d.__len__() == 2:
30 return g.addDirectedEdge(d[0],d[1])
31 for _toset in _extra.keys():
32 _temp.__setattr__(_toset,_extra[_toset])
33 if not _extra.has_key("label"):
34 _temp.label = str(_temp.weight)
35 return(_temp)
37 def addUndirectedEdge(*d,**_extra):
38 _temp = None
39 if d.__len__() == 1:
40 _temp = g.addEdge(d[0])
41 elif d.__len__() == 2:
42 return g.addUndirectedEdge(d[0],d[1])
43 for _toset in _extra.keys():
44 _temp.__setattr__(_toset,_extra[_toset])
45 if not _extra.has_key("label"):
46 _temp.label = str(_temp.weight)
47 return(_temp)
49 # Some demo functions that manipulate the graph in various ways
51 from java.lang import Math
53 # iteratively removes all nodes that have
54 # a total degree of 1
55 # assumes self loops have been removed
56 def removeLeaves():
57 g.nodes[0].totaldegree
58 _m = (totaldegree <= 1)
59 _toRet = []
60 while _m.__len__() > 0:
61 _toRet += remove(_m)
62 _m = (totaldegree <= 1)
63 return _toRet
67 # iteratively removes nodes that have an outdegree
68 # of introduces a new edge
69 # assumes self loops have been removed
70 def shortcutNodes():
71 g.nodes[0].totaldegree
72 _m = (totaldegree == 2)
73 _toRet = []
74 _added = []
75 while len(_m) > 0:
76 for _z in _m:
77 _pot = _z.getNeighbors()
78 if len(_pot) == 2:
79 if len(_pot[0]-_pot[1]) == 0:
80 _added += [addEdge(_pot[0],_pot[1])]
81 _toRet += removeNode(_z)
82 _m = (totaldegree == 2)
83 _temp = intersectionHelper(_toRet,_added)
84 return [_temp[0],_temp[1]]
86 # iteratively call removeLeaves and shortcutNodes to
87 # condense the graph
88 def condenseGraph():
89 _toRet = []
90 _added = []
91 _toRet += removeDisconnected()
92 _toRet += removeSelfLoops()
93 g.nodes[0].totaldegree
94 _t1 = (totaldegree <= 1)
95 _t2 = (totaldegree == 2)
96 while (_t1.__len__() > 0) | (_t2.__len__() > 0):
97 _toRet += removeLeaves()
98 _scr = shortcutNodes()
99 _toRet += _scr[0]
100 _added += _scr[1]
101 _t1 = (totaldegree <= 1)
102 _t2 = (totaldegree == 2)
103 _temp = intersectionHelper(_toRet,_added)
104 return [_temp[0],_temp[1]]
106 def intersectionHelper(_l1,_l2):
107 _templist = _l1 & _l2
108 _ln1 = []
109 _ln2 = []
110 for _elem in _l1:
111 if _elem not in _templist:
112 _ln1 += [_elem]
113 for _elem in _l2:
114 if _elem not in _templist:
115 _ln2 += [_elem]
116 return [_ln1,_ln2]
118 # does a "skitter" plot which lays out nodes around a circle based on the
119 # ordering given an input field (lexical for text, numerical for numbers).
120 # The radius is defined by the totaldegree of the node
121 def skitter(_field):
122 _maxangle = 2 * Math.PI
123 _ordering = sortBy(_field)
124 _increment = _maxangle / len(_ordering)
125 _curangle = 0
126 g.nodes[0].outdegree
127 _maxdeg = outdegree.max + 1.0
128 for _n in _ordering:
129 _radius = 1 - Math.log((_n.outdegree + 1.0) / _maxdeg)
130 _radius = _radius * 500.0
131 _x = 500.0 + _radius * Math.cos(_curangle)
132 _y = 500.0 + _radius * Math.sin(_curangle)
133 _n.setX(_x)
134 _n.setY(_y)
135 _curangle += _increment
137 # colors a given edge the "average" of it's two endpoints
138 def averageEdgeColor(_edge):
139 _n1 = _edge.getNode1()
140 _n2 = _edge.getNode2()
141 _edge.color = averageColor(_n1.color,_n2.color)
143 from com.hp.hpl.guess.jfreechart import GChartFrame
144 from com.hp.hpl.guess.jfreechart import GHistoChartFrame
145 from com.hp.hpl.guess.jfreechart import GPieChartFrame
147 # plots the distribution of values for a given field in a chart
148 def plotDistrib(_field,_chart = None, _sortBy = None, legend = 1, visible = 1):
149 if _sortBy == None:
150 _sortBy = _field
151 _l = sortBy(_sortBy)
152 _names = []
153 _values = []
154 for _z in _l:
155 _names += [_z]
156 _values += [_z.__getattr__(_field.getName())]
157 _xlabel = ""
158 if (_field.getType() == 1):
159 _xlabel = "Rank Ordered Nodes"
160 else:
161 _xlabel = "Rank Ordered Edges"
162 if _chart == None:
163 _chart = GChartFrame(_field.getName(),_names,_values,_xlabel,visible,legend)
164 else:
165 _chart.addToChart(_field.getName(),_names,_values,_xlabel)
166 return _chart
168 def sizecmpd(_a,_b):
169 return len(_b) - len(_a)
171 def sizecmpi(_a,_b):
172 return len(_b) - len(_a)
174 def plotSizes(_field,legend=0,visible=1):
175 _groups = groupBy(_field)
176 _groups.sort(sizecmpd)
177 _values = []
178 _names = []
179 _colors = []
180 for _g in _groups:
181 _values += [len(_g)]
182 _name = _g[0].__getattr__(_field.getName())
183 if _g == None:
184 _name = "X"
185 _names += [_name]
186 _colors += [_g[0].color]
187 _chart = GHistoChartFrame(_field.getName(),_groups,_values,_names,_colors,visible,legend)
188 return _chart
190 def plotSizesPie(_field,legend=0,visible=1):
191 _groups = groupBy(_field)
192 _groups.sort(sizecmpd)
193 _values = []
194 _names = []
195 _colors = []
196 for _g in _groups:
197 _values += [len(_g)]
198 _name = _g[0].__getattr__(_field.getName())
199 if _g == None:
200 _name = "X"
201 _names += [_name]
202 _colors += [_g[0].color]
203 _chart = GPieChartFrame(_field.getName(),_groups,_values,_names,_colors,visible,legend)
204 return _chart
206 def plotHistogram(_field,bins=10,legend=0,visible=1):
207 _sorted = sortBy(_field)
208 _min = float(_field.min)
209 _max = float(_field.max)
210 _binsize = (_max - _min)/float(bins)
211 _binend = _min + _binsize
212 _curgroup = []
213 _values = []
214 _names = []
215 _colors = []
216 _groups = []
217 for _e in _sorted:
218 _val = _e.__getattr__(_field.getName())
219 if _val > _binend:
220 _groups += [_curgroup]
221 _values += [len(_curgroup)]
222 _names += ["<="+str(_binend)];
223 _colors += [blue]
224 _binend = _binend + _binsize
225 _curgroup = []
226 _curgroup += [_e]
227 _groups += [_curgroup]
228 _values += [len(_curgroup)]
229 _names += ["<="+str(_max)];
230 _colors += [blue]
231 _chart = GHistoChartFrame(_field.getName(),_groups,_values,_names,_colors,visible,legend)
232 return _chart
235 # calculates the distance between two nodes
236 def distance(_node1,_node2):
237 return Math.sqrt(((_node1.x - _node2.x) * (_node1.x - _node2.x)) + ((_node1.y - _node2.y) * (_node1.y - _node2.y)))
239 def ninverse(_set):
240 _toret = []
241 for _t in g.nodes:
242 if _t not in _set:
243 _toret += [_t]
244 return _toret
246 def einverse(_set):
247 _toret = []
248 for _t in g.edges:
249 if _t not in _set:
250 _toret += [_t]
251 return _toret
253 import math
255 def floatRange(a, b, inc):
256 try: x = [float(a)]
257 except: return False
258 for i in range(1, int(math.ceil((b - a ) / inc))):
259 x. append(a + i * inc)
260 return x