1212#
1313# Initialize the context
1414#
15- def initialize ():
16- gtk_jar = Path ("./gephi-toolkit-all.jar" )
15+ def initialize (gephi_jar_path = "./gephi-toolkit-all.jar" ):
16+ gtk_jar = Path (gephi_jar_path )
1717 if not gtk_jar .is_file ():
1818 print ("Download the Gephi toolkit jar" )
19- urllib .request .urlretrieve (GTK_URL , "gephi-toolkit-all.jar" )
19+ urllib .request .urlretrieve (GTK_URL , gephi_jar_path )
2020 # Starts the JVM with the GTK in the classpath
2121 try :
22- jpype .startJVM (classpath = ["./gephi-toolkit-all.jar" ])
22+ jpype .startJVM (classpath = [gephi_jar_path ])
2323 except OSError as e :
2424 print (f"Warning: { e } " )
2525
2626#
27- # Function to load a NetworkX instance in Gephi
28- # This function takes a networkX instance and returns a Gephi graphModel
27+ # Create a new Gephi project in a fresh workspace and returns it associated graph model instance
2928#
30- def networkx_to_gephi ( graphX ):
29+ def gephi_create_graph ():
3130 # Create a Gephi workspace
3231 pc = Lookup .getDefault ().lookup (ProjectController )
3332 pc .newProject ()
3433 workspace = pc .getCurrentWorkspace ()
34+ # returns the graph model
35+ return Lookup .getDefault ().lookup (GraphController ).getGraphModel (workspace )
3536
37+ #
38+ # Function to load a NetworkX instance in Gephi
39+ # This function takes a networkX instance and returns a Gephi graphModel
40+ #
41+ def networkx_to_gephi (graphX ):
3642 # Get the Graph
37- graphModel = Lookup . getDefault (). lookup ( GraphController ). getGraphModel ( workspace )
43+ graphModel = gephi_create_graph ( )
3844 directedGraph = graphModel .getDirectedGraph ()
3945
4046 # Cast NetworkX graph to Gephi
@@ -43,6 +49,7 @@ def networkx_to_gephi(graphX):
4349 node = graphModel .factory ().newNode (f"{ node } " )
4450 if "label" in nodeAttributs :
4551 node .setLabel (nodeAttributs ["label" ])
52+ # TODO: add node attributes
4653 directedGraph .addNode (node )
4754
4855 for source , target in graphX .edges ():
@@ -51,12 +58,13 @@ def networkx_to_gephi(graphX):
5158 sourceNode = directedGraph .getNode (f"{ source } " )
5259 targetNode = directedGraph .getNode (f"{ target } " )
5360 edge = graphModel .factory ().newEdge (sourceNode , targetNode , 0 , edgeWeight , True )
61+ # TODO: add edge attributes
5462 directedGraph .addEdge (edge )
5563
5664 return graphModel
5765
5866#
59- # gephi to networkX
67+ # Gephi to NetworkX
6068#
6169def gephi_to_networkx (graphModel ):
6270 directedGraph = graphModel .getDirectedGraph ()
@@ -72,4 +80,36 @@ def gephi_to_networkx(graphModel):
7280 edge = edgeIter .next ()
7381 graphX .add_edge (edge .getSource ().getId (), edge .getTarget ().getId ())
7482
75- return graphX
83+ return graphX
84+
85+ #
86+ # Export the current Gephi graph to an GEXF file
87+ #
88+ def export_gexf (file_path = "./graph.gexf" ):
89+ export = Lookup .getDefault ().lookup (ExportController )
90+ export .exportFile (File (file_path ))
91+
92+
93+ #
94+ # Export the current Gephi graph to an PDF file
95+ #
96+ def export_pdf (file_path = "./graph.pdf" ):
97+ export = Lookup .getDefault ().lookup (ExportController )
98+ pdf = export .getExporter ("pdf" )
99+ export .exportFile (File (file_path ),pdf )
100+
101+ #
102+ # Export the current Gephi graph to an SVG file
103+ #
104+ def export_svg (file_path = "./graph.svg" ):
105+ export = Lookup .getDefault ().lookup (ExportController )
106+ svg = export .getExporter ("svg" )
107+ export .exportFile (File (file_path ), svg )
108+
109+ #
110+ # Export the current Gephi graph to an PNG file
111+ #
112+ def export_png (file_path = "./graph.png" ):
113+ export = Lookup .getDefault ().lookup (ExportController )
114+ png = export .getExporter ("png" )
115+ export .exportFile (File (file_path ), png )
0 commit comments