Home Wiki Blog Forum GEXF.net

Gephi forums

Community support

[SOLVED] ForceAtlas2 prevents script returning

Automated tasks on server

[SOLVED] ForceAtlas2 prevents script returning

Postby jthrilly » 17 Apr 2012 18:10

Hello everyone.

I have written a simple script which accepts a path to a graph file as a parameter, calculates modularity, colours the components based on this modularity, lays the graph out, and then writes back the file. Everything seems to work as expected.

My problem is that when I call the jar file from the command line, the script never returns me to the command prompt. This *only* happens when I attempt to use force atlas 2. The example documentation of Fruchterman-Reingold returns as expected, for example, and the script also returns when I don't do any layout at all. One strange observation is that if I open the gexf file, the layout seems to have completed as expected when using force atlas 2.

Here is the code I am using:
Code: Select all
        //Do layout
        ForceAtlas2 f2 = new ForceAtlas2Builder().buildLayout();
        f2.setGraphModel(graphModel);
        f2.setScalingRatio(2.0);
        f2.setGravity(7.0);
        //f2.setThreadsCount(12);
        f2.setLinLogMode(true);
        f2.setAdjustSizes(true);
        f2.initAlgo();
        for (int i = 0; i < 1 && f2.canAlgo(); i++) {
            f2.goAlgo();
        }

I have tried varying the number of iterations in the loop, but it has no effect.

------------------------------------------------------------------------------
SOLUTION:

In this case the solution was to make sure that endAlgo() is called after the for loop. The complete code therefore looks like this:
Code: Select all
//Do layout
        ForceAtlas2 f2 = new ForceAtlas2Builder().buildLayout();
        f2.setGraphModel(graphModel);
        f2.setScalingRatio(2.0);
        f2.setGravity(7.0);
        f2.setThreadsCount(12);
        f2.setLinLogMode(true);
        f2.setAdjustSizes(true);
        f2.initAlgo();
        for (int i = 0; i < 1000 && f2.canAlgo(); i++) {
            f2.goAlgo();
        }
       f2.endAlgo();


Because ForceAtlas2 is a continuous layout algorithm, it must somehow "stay alive" even after the iterations have been completed. This was not documented anywhere, so I think a note on the wiki (perhaps on this page: http://wiki.gephi.org/index.php/Toolkit_-_Layout_graph) about this specific algorithm would be a good idea.
Last edited by jthrilly on 17 Apr 2012 23:05, edited 1 time in total.
jthrilly
 
Posts: 7
Joined: 13 Mar 2012 19:54

Re: ForceAtlas2 prevents script returning

Postby eduramiba » 17 Apr 2012 20:52

Hi,
Some layouts such as force atlas 2 don't auto-stop even when it is stabilized. That means canAlgo will always return true, so you should set a maximum number of iterations in your for loop.

Edit: I see you put 1 iteration. I don't see anything wrong with the code. How are you loading your graph?

Eduardo
Life... is like a grapefruit. It's orange and squishy, and has a few pips in it, and some folks have half a one for breakfast.
User avatar
eduramiba
Gephi Code Manager
 
Posts: 579
Joined: 22 Mar 2010 16:30
Location: Zaragoza, Spain

Re: ForceAtlas2 prevents script returning

Postby jthrilly » 17 Apr 2012 20:59

Thank you for your reply - makes perfect sense.

Is there a reason that forceatlas 2 does not stop even though it is stabalised? Is there something I can use to check if it has stabalised? The reason I ask is that the graphs will be of different sizes, and it is unclear to me how I should decide an appropriate number of iterations based solely on node/edge count.
jthrilly
 
Posts: 7
Joined: 13 Mar 2012 19:54

Re: ForceAtlas2 prevents script returning

Postby eduramiba » 17 Apr 2012 21:11

Hi,
I guess it never stops because it is difficult to determine when the layout is done for any graph for sure.
Also I think Force Atlas 2 converges quickly at the beginning and slowly when it is more stable ir order to reach very good quality. You can see an example of the slow convergence here https://gephi.org/2011/forceatlas2-the- ... rew-layout

Did you manage to make that code work? If you execute that just 1 iteration it should return soon. Is the graph really big?
Does the layout work for that gexf file in Gephi 0.8.1-beta?

And if you don't want to manually set the number of iterations, you can execute the layout for an amount of time using AutoLayout http://wiki.gephi.org/index.php/Toolkit_-_Layout_graph

Eduardo
Life... is like a grapefruit. It's orange and squishy, and has a few pips in it, and some folks have half a one for breakfast.
User avatar
eduramiba
Gephi Code Manager
 
Posts: 579
Joined: 22 Mar 2010 16:30
Location: Zaragoza, Spain

Re: ForceAtlas2 prevents script returning

Postby jthrilly » 17 Apr 2012 21:26

eduramiba wrote:Edit: I see you put 1 iteration. I don't see anything wrong with the code. How are you loading your graph?


Yes, I did that when I was debugging to check it wasn't just happening because of too many iterations. The graph is loaded by:

Code: Select all
//Import file       
        Container container;
        try {
            File file = new File(args[0]);
            InputStream is = new FileInputStream(file);
            container = importController.importFile(file);
            container.getLoader().setEdgeDefault(EdgeDefault.UNDIRECTED);   //Force UNDIRECTED
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }


Why would the way I load the graph affect ForceAtlas 2 but not other layout routines?

In response to your questions, yes the layout renders fine in standalone gephi - I am attempting to recreate the layout I get there. The networks are mostly small (<1000 nodes) with a mean of ~140.

I have considered using the automatic layout approach, but I don't wish to set a specific amount of time given the variation in the size of the networks. I really just want to understand why this approach isn't working.

EDIT: and no, with one iteration I am not sent back to the command prompt.
jthrilly
 
Posts: 7
Joined: 13 Mar 2012 19:54

Re: ForceAtlas2 prevents script returning

Postby eduramiba » 17 Apr 2012 21:41

Oh, I think I know what is happening. It must be this bug https://github.com/gephi/gephi/issues/487.

It should be fixed with the latest toolkit. Make sure you are using it https://launchpad.net/gephi/toolkit/too ... .5-all.zip

Eduardo
Life... is like a grapefruit. It's orange and squishy, and has a few pips in it, and some folks have half a one for breakfast.
User avatar
eduramiba
Gephi Code Manager
 
Posts: 579
Joined: 22 Mar 2010 16:30
Location: Zaragoza, Spain

Re: ForceAtlas2 prevents script returning

Postby jthrilly » 17 Apr 2012 22:10

I tried the link you suggested, but even though it says it is version 8.5 it unpacks as 8.1 and seems to contain the same gephi-toolkit.jar as I am already using. Either way, it does not solve my problem :cry:

Aren't the newer sources on GitHub? Can anyone try to reproduce my issue?

Thank you very much for helping me by the way!
jthrilly
 
Posts: 7
Joined: 13 Mar 2012 19:54

Re: ForceAtlas2 prevents script returning

Postby eduramiba » 17 Apr 2012 22:58

I am going to check the file to see if it is the correct version.
Meanwhile you can try to add System.exit(0); at the end of your code as a workaround to fix it.

Eduardo
Life... is like a grapefruit. It's orange and squishy, and has a few pips in it, and some folks have half a one for breakfast.
User avatar
eduramiba
Gephi Code Manager
 
Posts: 579
Joined: 22 Mar 2010 16:30
Location: Zaragoza, Spain

Re: ForceAtlas2 prevents script returning

Postby jthrilly » 17 Apr 2012 23:00

I figured out the problem. I didn't call endAlgo() after the for loop. It seems so obvious in retrospect, but it isn't documented anywhere that it is needed. I will update the OP and the issue thread on GitHub with this information.

Thank you Eduardo.
jthrilly
 
Posts: 7
Joined: 13 Mar 2012 19:54

Re: ForceAtlas2 prevents script returning

Postby eduramiba » 17 Apr 2012 23:10

Right, call to endAlgo is missing in the toolkit examples. I will update them.
In the case of force atlas 2 it closes a thread pool.

Eduardo
Life... is like a grapefruit. It's orange and squishy, and has a few pips in it, and some folks have half a one for breakfast.
User avatar
eduramiba
Gephi Code Manager
 
Posts: 579
Joined: 22 Mar 2010 16:30
Location: Zaragoza, Spain


Return to Toolkit

Who is online

Users browsing this forum: No registered users and 0 guests