Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big Graph visualization on a webpage : networkx, vivagraph

I have a graph of about 5000 nodes and 5000 links, that i can visualize in Chrome thanks to the vivagraph javascript library (webgl is faster than svg - in d3 for example).

My workflow is :

  • Building with the networkx python library and output the result as a json file.
  • Load the json and construct the graph with the vivagraph javascript library.
  • Nodes positions are computed by the js library

The problem is that it takes time to render the layout with well positionned nodes.

My approach is to pre-compute the nodes position in networkx for example. The really good point on this approach is that it minimize client work on the browser. But i can't achieve good positions on the webpage. I need help on this step.

The relevant python code for the node position computation is :

    ## positionning
    try:
        # Position nodes using Fruchterman-Reingold force-directed algorithm.
        pos=nx.spring_layout(G)
        for k,v in pos.iteritems():
            # scaling tentative
            # from small float like 0.5555 to higher values
            # casting to int because precision is not important
            pos[k] = [ int(i*1000) for i in v.tolist() ]
    except Exception, e:
        print "positionning failed"
        raise

    ## setting positions
    try:
        # set position of nodes as a node attribute
        # that will be used with the js library
        nx.set_node_attributes(G,'pos', pos)
    except Exception, e:
        print "getting positions failed"
        raise e

    # output all the stuff
    d = json_graph.node_link_data(G)
    with open(args.output,'w') as f:
        json.dump(d,f)

Then in my page, in javascript :

/*global Viva*/
function graph(file){
  var file = file;

  $.getJSON(file, function(data) {
      var graphGenerator = Viva.Graph.generator();
      graph = Viva.Graph.graph();

      # building the graph with the json data :

      data.nodes.forEach(function(n,i) {
         var node = graph.addNode(n.id,{d: n.d});

         # node position is defined in the json element attribute 'pos'
         node.position = {
             x : n.pos[0],
             y : n.pos[1]
         };
      })

      # adding links between nodes

      data.links.forEach(function(l,i) {
          graph.addLink(data.nodes[l.source].id, data.nodes[l.target].id);
      })


        var max_link = 55
        var min_link = 1

        var colors = d3.scale.linear().domain([min_link,max_link]).range(['#F0F0F0','#252525']);

      var layout = Viva.Graph.Layout.forceDirected(graph, {
         springLength : 80,
         springCoeff : 0.0008,
         dragCoeff : 0.001,
         gravity : -5.0,
         theta : 0.8
      });

      var graphics = Viva.Graph.View.webglGraphics();
      graphics
      .node(function(node){

        # color and size of nodes

        color = colors(node.links.length)
        if(node.id == "root"){
          // pin node on canvas, so no position update
          node.isPinned = true;
          size = 60;
        } else {
          size = 20+(7-node.id.length)*(7-node.id.length);
        }
        return Viva.Graph.View.webglSquare(size,color);
      })
      .link(function(link) {

        # color on links 

        fromId = link.fromId;
        toId = link.toId;
        if(toId == "root" || fromId == "root"){
          return Viva.Graph.View.webglLine("#252525");
        } else {
          if( fromId[0] == toId[0]){
            linkcolor = linkcolors(fromId[0])
            return Viva.Graph.View.webglLine(linkcolor);
          } else {
            linkcolor = averageRGB(linkcolors(fromId[0]),linkcolors(toId[0]))
            return Viva.Graph.View.webglLine('#'+linkcolor);
          }
        }
      }); 

      renderer = Viva.Graph.View.renderer(graph,
          {
              layout     : layout,
              graphics   : graphics,
              enableBlending: false,
              renderLinks : true,
              prerender  : true
          });

      renderer.run();
  });
}

I am now trying Gephi, but i don't want to use the gephi toolkit as i am not used to java.

If somebody got some hints on this, please avoid me hundred of trials and maybe failure ;)

like image 580
qdelettre Avatar asked Jun 10 '13 15:06

qdelettre


1 Answers

Spring Layout assumes that the edge weights uphold the metric property, i.e Weight(A,B)+Weight(A,C) > Weight(B,C). If this is not the case, then networkx tries to place them as realistic as possible.

You could try to adjust this by

    pos=nx.spring_layout(G,k=\alpha, iterations=\beta)
    # where 0.0<\alpha<1.0 and \beta>0 
    # k is the minimum distance between the nodes
    # iterations specify the simulated annealing runs
    # This code works only on Networkx 1.8 and not earlier versions
like image 96
Vikram Avatar answered Oct 10 '22 18:10

Vikram