1 def addNode(_name
,**_extra
):
2 _temp
= g
.addNode(_name
)
3 if not isinstance(_name
,Class
.forName("com.hp.hpl.guess.Node")):
5 for _toset
in _extra
.keys():
6 _temp
.__setattr
__(_toset
,_extra
[_toset
])
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
])
22 def addEdge(*d
,**_extra
):
23 return addUndirectedEdge(*d
,**_extra
)
25 def addDirectedEdge(*d
,**_extra
):
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
)
37 def addUndirectedEdge(*d
,**_extra
):
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
)
49 # Some demo functions that manipulate the graph in various ways
51 from java
.lang
import Math
53 # iteratively removes all nodes that have
55 # assumes self loops have been removed
57 g
.nodes
[0].totaldegree
58 _m
= (totaldegree
<= 1)
60 while _m
.__len
__() > 0:
62 _m
= (totaldegree
<= 1)
67 # iteratively removes nodes that have an outdegree
68 # of introduces a new edge
69 # assumes self loops have been removed
71 g
.nodes
[0].totaldegree
72 _m
= (totaldegree
== 2)
77 _pot
= _z
.getNeighbors()
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
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()
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
111 if _elem
not in _templist
:
114 if _elem
not in _templist
:
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
122 _maxangle
= 2 * Math
.PI
123 _ordering
= sortBy(_field
)
124 _increment
= _maxangle
/ len(_ordering
)
127 _maxdeg
= outdegree
.max + 1.0
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
)
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):
156 _values
+= [_z
.__getattr
__(_field
.getName())]
158 if (_field
.getType() == 1):
159 _xlabel
= "Rank Ordered Nodes"
161 _xlabel
= "Rank Ordered Edges"
163 _chart
= GChartFrame(_field
.getName(),_names
,_values
,_xlabel
,visible
,legend
)
165 _chart
.addToChart(_field
.getName(),_names
,_values
,_xlabel
)
169 return len(_b
) - len(_a
)
172 return len(_b
) - len(_a
)
174 def plotSizes(_field
,legend
=0,visible
=1):
175 _groups
= groupBy(_field
)
176 _groups
.sort(sizecmpd
)
182 _name
= _g
[0].__getattr
__(_field
.getName())
186 _colors
+= [_g
[0].color
]
187 _chart
= GHistoChartFrame(_field
.getName(),_groups
,_values
,_names
,_colors
,visible
,legend
)
190 def plotSizesPie(_field
,legend
=0,visible
=1):
191 _groups
= groupBy(_field
)
192 _groups
.sort(sizecmpd
)
198 _name
= _g
[0].__getattr
__(_field
.getName())
202 _colors
+= [_g
[0].color
]
203 _chart
= GPieChartFrame(_field
.getName(),_groups
,_values
,_names
,_colors
,visible
,legend
)
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
218 _val
= _e
.__getattr
__(_field
.getName())
220 _groups
+= [_curgroup
]
221 _values
+= [len(_curgroup
)]
222 _names
+= ["<="+str(_binend
)];
224 _binend
= _binend
+ _binsize
227 _groups
+= [_curgroup
]
228 _values
+= [len(_curgroup
)]
229 _names
+= ["<="+str(_max
)];
231 _chart
= GHistoChartFrame(_field
.getName(),_groups
,_values
,_names
,_colors
,visible
,legend
)
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
)))
255 def floatRange(a
, b
, inc
):
258 for i
in range(1, int(math
.ceil((b
- a
) / inc
))):
259 x
. append(a
+ i
* inc
)