Created
April 21, 2019 00:39
-
-
Save igorbrigadir/bfacf23c9ca6b8f2b4d63fdc565c0d3a to your computer and use it in GitHub Desktop.
Use Gephi in Java without GUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.Color; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import org.gephi.data.attributes.api.AttributeColumn; | |
| import org.gephi.data.attributes.api.AttributeController; | |
| import org.gephi.data.attributes.api.AttributeModel; | |
| import org.gephi.filters.api.FilterController; | |
| import org.gephi.graph.api.DirectedGraph; | |
| import org.gephi.graph.api.GraphController; | |
| import org.gephi.graph.api.GraphModel; | |
| import org.gephi.graph.api.UndirectedGraph; | |
| import org.gephi.io.exporter.api.ExportController; | |
| import org.gephi.io.importer.api.Container; | |
| import org.gephi.io.importer.api.EdgeDefault; | |
| import org.gephi.io.importer.api.ImportController; | |
| import org.gephi.io.processor.plugin.DefaultProcessor; | |
| import org.gephi.layout.plugin.force.StepDisplacement; | |
| import org.gephi.layout.plugin.force.yifanHu.YifanHuLayout; | |
| import org.gephi.preview.api.PreviewController; | |
| import org.gephi.preview.api.PreviewModel; | |
| import org.gephi.preview.api.PreviewProperty; | |
| import org.gephi.preview.types.EdgeColor; | |
| import org.gephi.project.api.ProjectController; | |
| import org.gephi.project.api.Workspace; | |
| import org.gephi.ranking.api.Ranking; | |
| import org.gephi.ranking.api.RankingController; | |
| import org.gephi.ranking.api.Transformer; | |
| import org.gephi.ranking.plugin.transformer.AbstractColorTransformer; | |
| import org.gephi.ranking.plugin.transformer.AbstractSizeTransformer; | |
| import org.gephi.statistics.plugin.GraphDistance; | |
| import org.openide.util.Lookup; | |
| public class GephiMaker { | |
| public static void main(String[] args) { | |
| //Init a project - and therefore a workspace | |
| ProjectController pc = Lookup.getDefault().lookup(ProjectController.class); | |
| pc.newProject(); | |
| Workspace workspace = pc.getCurrentWorkspace(); | |
| //Get models and controllers for this new workspace - will be useful later | |
| AttributeModel attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel(); | |
| GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel(); | |
| PreviewModel model = Lookup.getDefault().lookup(PreviewController.class).getModel(); | |
| ImportController importController = Lookup.getDefault().lookup(ImportController.class); | |
| FilterController filterController = Lookup.getDefault().lookup(FilterController.class); | |
| RankingController rankingController = Lookup.getDefault().lookup(RankingController.class); | |
| //Import file | |
| Container container; | |
| try { | |
| // GEXF File here: | |
| File file = new File("example.gexf"); | |
| container = importController.importFile(file); | |
| container.getLoader().setEdgeDefault(EdgeDefault.UNDIRECTED); | |
| } catch (Exception ex) { | |
| ex.printStackTrace(); | |
| return; | |
| } | |
| //Append imported data to GraphAPI | |
| importController.process(container, new DefaultProcessor(), workspace); | |
| //See if graph is well imported | |
| DirectedGraph graph = graphModel.getDirectedGraph(); | |
| System.out.println("Nodes: " + graph.getNodeCount()); | |
| System.out.println("Edges: " + graph.getEdgeCount()); | |
| //Filter | |
| /* | |
| DegreeRangeFilter degreeFilter = new DegreeRangeFilter(); | |
| degreeFilter.init(graph); | |
| degreeFilter.setRange(new Range(30, Integer.MAX_VALUE)); //Remove nodes with degree < 30 | |
| Query query = filterController.createQuery(degreeFilter); | |
| GraphView view = filterController.filter(query); | |
| graphModel.setVisibleView(view); //Set the filter result as the visible view | |
| */ | |
| //See visible graph stats | |
| UndirectedGraph graphVisible = graphModel.getUndirectedGraphVisible(); | |
| System.out.println("Nodes: " + graphVisible.getNodeCount()); | |
| System.out.println("Edges: " + graphVisible.getEdgeCount()); | |
| System.out.println("Now calculate centrality..."); | |
| //Get Centrality | |
| GraphDistance distance = new GraphDistance(); | |
| distance.setDirected(true); | |
| distance.execute(graphModel, attributeModel); | |
| //Rank color by Degree | |
| Ranking degreeRanking = rankingController.getModel().getRanking(Ranking.NODE_ELEMENT, Ranking.DEGREE_RANKING); | |
| AbstractColorTransformer colorTransformer = | |
| (AbstractColorTransformer) rankingController.getModel().getTransformer(Ranking.NODE_ELEMENT, Transformer.RENDERABLE_COLOR); | |
| colorTransformer.setColors(new Color[] { new Color(0xFEF0D9), new Color(0xB30000) }); | |
| rankingController.transform(degreeRanking, colorTransformer); | |
| //Rank size by centrality | |
| AttributeColumn centralityColumn = attributeModel.getNodeTable().getColumn(GraphDistance.BETWEENNESS); | |
| Ranking centralityRanking = rankingController.getModel().getRanking(Ranking.NODE_ELEMENT, centralityColumn.getId()); | |
| AbstractSizeTransformer sizeTransformer = | |
| (AbstractSizeTransformer) rankingController.getModel().getTransformer(Ranking.NODE_ELEMENT, Transformer.RENDERABLE_SIZE); | |
| sizeTransformer.setMinSize(10); | |
| sizeTransformer.setMaxSize(15); | |
| rankingController.transform(centralityRanking, sizeTransformer); | |
| // Layout | |
| System.out.println("Layout...:"); | |
| //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.endAlgo(); | |
| // Preview Styles | |
| model.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE); | |
| model.getProperties().putValue(PreviewProperty.EDGE_COLOR, new EdgeColor(Color.GRAY)); | |
| model.getProperties().putValue(PreviewProperty.EDGE_THICKNESS, new Float(0.1f)); | |
| model.getProperties().putValue(PreviewProperty.NODE_LABEL_FONT, model.getProperties().getFontValue(PreviewProperty.NODE_LABEL_FONT).deriveFont(8)); | |
| System.out.println("Save Layout"); | |
| //Export | |
| ExportController ec = Lookup.getDefault().lookup(ExportController.class); | |
| try { | |
| ec.exportFile(new File("viz.pdf")); | |
| } catch (IOException ex) { | |
| ex.printStackTrace(); | |
| return; | |
| } | |
| System.out.println("Finished."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment