Saturday, September 10, 2011

Useful Jython scripts for GUESS, The Graph Exploration System

Often the data that we import in GUESS is in such a form that to look at every node and edge distinctly we need to do lot of repositioning and have to set many features so that the graph look elegant in terms of its visualization.

Here is a quick fix to this problem. In the following script i am going to set the node color, edge color, size of nodes,label visibility, label color,  the layout of the graph, and most importantly if you are working with the weighted graph, i am going to make the width of the edges of the graph proportional to their corresponding weight. Also in case of signed graph the color of the positive edges and the negative edges will be distinct which will make it easy for you to recognize them at once.



def modify_graph():
    edgeset=g.getEdges()
    if edgeset.size()>0:
        edgeset1=g.getEdges()
        maxweight=-1000
        minweight=1000
        i=0
        rescaleLayout(1280,800)
        for i in edgeset:
            i.labelvisible=false
            i.labelcolor=black
            if i.weight>0:
                i.color=limegreen
            else:
                i.color=red
                edgeset1.remove(i)
        for i in range (edgeset1.size()):
            if edgeset1[i].weight<minweight:
                minweight=edgeset1[i].weight
            if edgeset1[i].weight>maxweight:
                maxweight=edgeset1[i].weight
        diff=maxweight-minweight
        x=1.0
        max=0
        x=.01
        while max<=10:
            max=maxweight*x
            x=x+.01
        i=0
        for i in range (edgeset1.size()):
            edgeset1[i].width=(edgeset1[i].weight*x)
    nodeset=g.getNodes()
    for n in nodeset:
        n.width = 20
        n.height = 20
        n.labelvisible=true
        n.labelcolor=black
        n.color=black
        n.style = 2
    g.kkLayout()
modify_graph()   

useful script for GUESS, The Graph Exploration System

1) animhighlight.py
This script responds to your mouse positioning over a particular node. After running the script, as soon as you will take the mouse cursor to the node it will highlight the node and its corresponding neighbors with yellow color. Also the edges between the node and its neighbor will be highlighted in red. The nodes and edges will retain their previous color when you will leave that node. The Jython script for the same is as below

import java

class animhighlight(java.lang.Object):

    # so we can "unhighlight" nodes
    _toFix = {}
   
    def __init__(self):
        # add the listeners
        graphevents.mouseEnterNode = self.mouseEnter
        graphevents.mouseLeaveNode = self.mouseLeave
        graphevents.clickNode = self.mouseClick
       
        # remove default behaviors
        vf.defaultNodeHighlights(false)
        vf.defaultNodeZooming(false)

    def mouseEnter(self,_node):
        self._toFix[_node] = _node.color
        StatusBar.setStatus(str(_node))
        _node.color = yellow

        for _e in _node.getOutEdges():
            if not (_e in self._toFix.keys()):
                self._toFix[_e] = _e.color
                _e.color = orange
                _e.animate("arrows")

        for _e in _node.getInEdges():
            if not (_e in self._toFix.keys()):
                self._toFix[_e] = _e.color
                _e.color = green
                _e.animate("arrows")

        for _n in _node.getPredecessors():
            if (_n != _node):
                self._toFix[_n] = _n.color
                _n.color = green
                _n.animate("pulse")

        for _n in _node.getSuccessors():
            if (_n != _node):
                self._toFix[_n] = _n.color
                _n.color = red
                _n.animate("pulse")



    def mouseLeave(self,_node):
        # put back all the original colors
        # and stop animations
        for _elem in self._toFix.keys():
            _elem.color = self._toFix[_elem]
            _elem.animationStopAll()
        self._toFix.clear();

    def mouseClick(self,_node):
        # zoom to the node AND its neighbors
        _toCenter = [_node]
        _toCenter += _node.getNeighbors()
        center(_toCenter)

animhighlight()