In [1]:
from networkit import *
NetworKit provides an easy interface to Gephi that uses the Gephi graph streaming plugin. To be able to use it, install the Graph Streaming plugin using the Gephi plugin manager. Afterwards, open the Streaming window by selecting Windows/Streaming in the menu.
Once the plugin is installed in gephi, create a new project and start the Master Server in the Streaming tab within gephi. The running server will be indicated by a green dot.
As an example, we generate a random graph...
In [2]:
G = generators.ErdosRenyiGenerator(300, 0.2).generate()
G.addEdge(0, 1) #We want to make sure this specific edge exists, for usage in an example later.
... and export it directly export it into the active gephi workspace. After executing the following code, the graph should be available in the first gephi workspace. Attention: Any graph currently contained in that workspace will be overriden.
In [3]:
client = gephi.streaming.GephiStreamingClient()
client.exportGraph(G)
We now apply a community detection algorithm to the generated random graph and export the community as a node attribute to gephi. Any python list or Partition object can be exported. Please note that only the attribute itself is transfered, so make sure you called exportGraph(graph) first.
In [4]:
communities = community.detectCommunities(G)
In [5]:
client.exportNodeValues(G, communities, "community")
The node attribute can now be selected and used in gephi, for partitioning or any other desired scenario.
Just like node values, we can export edge values. After graph creation, each edge is assigned an integer id that is then used to access arbitrary attribute vectors, so any python list can be exported to gephi. In the following example, we assign an even number edge and export that score to gephi.
In [6]:
edgeScore = [2*x for x in range(0, G.upperEdgeIdBound())]
client.exportEdgeValues(G, edgeScore, "myEdgeScore")
By default, the streaming client in NetworKit connects to http://localhost:8080/workspace0, i.e. the first workspace of the local gephi instance. One might want to connect to a gephi instance running on a remote host or change the used port (this can be done in gephi by selecting Settings within the Streaming tab). To change the url in NetworKit, simply pass it upon the creation of the client object:
In [7]:
client = gephi.streaming.GephiStreamingClient(url='http://localhost:8080/workspace0')