gephi-tutorial-toolkit
gephi-tutorial-toolkit
The toolkit is just a single JAR that anyone could reuse in a Java program.
This tutorial aims to introduce the project, show possibilities and start
write some code.
The content of this tutorial, and more, can be found in the toolkit-demos archive.
The toolkit wraps core modules, without user interaces in a single JAR library. There is no additionnal
code in Gephi to allow this. The desktop application and the tookit relies exactly on the same modules.
Use-cases
Toolkit possible use-case:
* Create a layout program Receive a GEXF file, layout it, and return the resulting GEXF file.
* Complete chain From an imput graph file (DOT, GML, GEXF, ...) to the resulting PDF.
* Java applet Use Gephi modules to store, filter,layout and build a new visualization applet
on top of it.
Toolkit Javadoc
References
A project can have several workspaces, independent from each other. The workspace is often asked by modules,
so you need to create one at startup.
What is Lookup ?
Lookup is a very useful way to retrieve services and find instances in a modular application like Gephi. Controllers are
singletons and can be found by looking into the general Lookup: Lookup.getDefault(). So Lookup is just a container, where
modules put objects in it. Other modules get these objects from their class name.
Import Graph File
Importing data from files supported by Gephi.The import is done in two steps. First, all data are put in a
Container, which is separated from the main data structure. The second steps is “processing”, when data in
the container are appended to the graph structure and attributes.
The API also allows to import data from Reader and InputStream.
//Import file
ImportController importController = Lookup.getDefault().lookup(ImportController.class);
Container container;
try {
File file = new File(getClass().getResource(“/org/gephi/toolkit/demos/resources/test.gexf”).toURI());
container = importController.importFile(file);
container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED); //Force DIRECTED
container.setAllowAutoNode(false); //Don’t create missing nodes
} catch (Exception ex) {
ex.printStackTrace();
return;
}
//Find node by id
Node node2 = directedGraph.getNode(“n2”);
//Import database
ImportController importController = Lookup.getDefault().lookup(ImportController.class);
EdgeListDatabaseImpl db = new EdgeListDatabaseImpl();
db.setDBName(“test”);
db.setHost(“localhost”);
db.setUsername(“root”);
db.setPasswd(“”);
db.setSQLDriver(new MySQLDriver());
//db.setSQLDriver(new PostgreSQLDriver());
//db.setSQLDriver(new SQLServerDriver());
db.setPort(3306);
db.setNodeQuery(“SELECT nodes.id AS id, nodes.label AS label, nodes.url FROM nodes”);
db.setEdgeQuery(“SELECT edges.source AS source, edges.target AS target, edges.name AS label, edges.
weight AS weight FROM edges”);
Container container = importController.importDatabase(db, new ImporterEdgeList());
//Run YifanHuLayout for 100 passes - The layout always takes the current visible view
YifanHuLayout layout = new YifanHuLayout(null, new StepDisplacement(1f));
layout.setGraphModel(graphModel);
layout.resetPropertiesValues();
layout.setOptimalDistance(200f);
layout.initAlgo();
for (int i = 0; i < 100 && layout.canAlgo(); i++) {
layout.goAlgo();
}
Layout - Auto
With AutoLayout class, you can set a layout duration, and an execution ratio for several layouts. For instance
you set 0.8 for a Yifan Hu algorithm and 0.2 for Label Adjust. If execution time is 100 seconds, the first algorithm
runs for 80 seconds and the second for 20 seconds. It also allows to change property values dynamically, at a
certain ratio or interpolated if values are numerical.
autoLayout.addLayout(firstLayout, 0.5f);
autoLayout.addLayout(secondLayout, 0.5f, new AutoLayout.DynamicProperty[]{adjustBySizeProperty,
repulsionProperty});
autoLayout.execute();
Statistics and metrics
How to execute a metric algorithm and get the result for each node. The algorithm write values in a column it
creates. It is also possible to get the statistics report, as in Gephi.
//Get Centrality
GraphDistance distance = new GraphDistance();
distance.setDirected(true);
distance.execute(graphModel, attributeModel);
//Get Centrality
GraphDistance distance = new GraphDistance();
distance.setDirected(true);
distance.execute(graphModel, attributeModel);
The graph from the graph view can be obtained from the GraphModel by calling:
or, if the view has been set as the visible view like above:
//Filter, keep partition ‘Blogarama’. Build partition with ‘source’ column in the data
PartitionController pc = Lookup.getDefault().lookup(PartitionController.class);
Partition p = pc.buildPartition(attributeModel.getNodeTable().getColumn(“source”), graph);
//Ego filter
EgoFilter egoFilter = new EgoFilter();
egoFilter.setPattern(“obamablog.com”); //Regex accepted
egoFilter.setDepth(1);
Query queryEgo = filterController.createQuery(egoFilter);
GraphView viewEgo = filterController.filter(queryEgo);
graphModel.setVisibleView(viewEgo); //Set the filter result as the visible view
Filter (4/4)
It is possible to use Intersection and Union operators to create queries. Simply add other queries as sub-queries
of the operator.
//Combine two filters with AND - Set query1 and query2 as sub-query of AND
IntersectionOperator intersectionOperator = new IntersectionOperator();
Query andQuery = filterController.createQuery(intersectionOperator);
filterController.setSubQuery(andQuery, query1);
filterController.setSubQuery(andQuery, query2);
GraphView view = filterController.filter(andQuery);
graphModel.setVisibleView(view); //Set the filter result as the visible view
Preview
Preview is the last step before export and allows display customization and aesthetics refinements. It works
with the current workspace. The example below shows how to change edge coloring mode, edge thickness and
label font size.
//Preview
PreviewModel model = Lookup.getDefault().lookup(PreviewController.class).getModel();
model.getNodeSupervisor().setShowNodeLabels(Boolean.TRUE);
ColorizerFactory colorizerFactory = Lookup.getDefault().lookup(ColorizerFactory.class);
model.getUniEdgeSupervisor().setColorizer((EdgeColorizer) colorizerFactory.createCustomColorMode(Color.
LIGHT_GRAY)); //Set edges gray
model.getBiEdgeSupervisor().setColorizer((EdgeColorizer) colorizerFactory.createCustomColorMode(Color.
RED)); //Set mutual edges red
model.getUniEdgeSupervisor().setEdgeScale(0.1f);
model.getBiEdgeSupervisor().setEdgeScale(0.1f);
model.getNodeSupervisor().setBaseNodeLabelFont(model.getNodeSupervisor().getBaseNodeLabelFont().
deriveFont(8));
Export Graph File
Exports the current workspace to a file. For a simple export just call the exportFile() method with the right file
extension. For customizing the exporter, query it from the export controller and change settings.
Please also consider the gephi-toolkit is a young project and remains imperfect. We hope this project helps all
developers to use network visualization and analytics in their project and build great systems.
We are looking for your help, join the network and become a contributor!