Skip to content

Instantly share code, notes, and snippets.

@shibacow
Created March 1, 2017 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shibacow/9e1497c260c2294c851a59e49ee8a475 to your computer and use it in GitHub Desktop.
Save shibacow/9e1497c260c2294c851a59e49ee8a475 to your computer and use it in GitHub Desktop.
gephi-toolkit sample
/**
*
*/
package hello_world;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import org.gephi.appearance.api.AppearanceController;
import org.gephi.appearance.api.AppearanceModel;
import org.gephi.appearance.api.Function;
import org.gephi.appearance.plugin.RankingElementColorTransformer;
import org.gephi.appearance.plugin.RankingNodeSizeTransformer;
import org.gephi.filters.api.FilterController;
import org.gephi.filters.api.Query;
import org.gephi.filters.api.Range;
import org.gephi.filters.plugin.graph.DegreeRangeBuilder.DegreeRangeFilter;
import org.gephi.graph.api.Column;
import org.gephi.graph.api.DirectedGraph;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.GraphView;
import org.gephi.graph.api.UndirectedGraph;
import org.gephi.io.exporter.api.ExportController;
import org.gephi.io.exporter.spi.GraphExporter;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.EdgeDirectionDefault;
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.statistics.plugin.GraphDistance;
import org.openide.util.Lookup;
/**
* @author shibacow
*
*/
public class HeadlessSimple {
public void script(){
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
pc.newProject();
Workspace workspace = pc.getCurrentWorkspace();
System.out.println(workspace);
//Get models and controllers for this new workspace - will be useful later
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
PreviewModel model = Lookup.getDefault().lookup(PreviewController.class).getModel();
ImportController importController = Lookup.getDefault().lookup(ImportController.class);
FilterController filterController = Lookup.getDefault().lookup(FilterController.class);
AppearanceController appearanceController = Lookup.getDefault().lookup(AppearanceController.class);
AppearanceModel appearanceModel = appearanceController.getModel();
Container container;
try {
System.out.println("hoge");
// File file = new File(getClass().getResource("polblogs.gml").toURI());
File file = new File("src/polblogs.gml");
container = importController.importFile(file);
container.getLoader().setEdgeDefault(EdgeDirectionDefault.DIRECTED); //Force DIRECTED
} catch (Exception ex) {
ex.printStackTrace();
return;
}
System.out.println(container);
//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());
//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 < 101 && layout.canAlgo(); i++) {
if(i%50==0){
System.out.println(i);
}
layout.goAlgo();
}
layout.endAlgo();
//Get Centrality
System.out.println("start distance for centarlity");
GraphDistance distance = new GraphDistance();
distance.setDirected(true);
distance.execute(graphModel);
System.out.println("end distance for centarlity");
//Rank color by Degree
System.out.println("start color by Degree");
Function degreeRanking = appearanceModel.getNodeFunction(graph, AppearanceModel.GraphFunction.NODE_DEGREE, RankingElementColorTransformer.class);
RankingElementColorTransformer degreeTransformer = (RankingElementColorTransformer) degreeRanking.getTransformer();
degreeTransformer.setColors(new Color[]{new Color(0xFEF0D9), new Color(0xB30000)});
degreeTransformer.setColorPositions(new float[]{0f, 1f});
appearanceController.transform(degreeRanking);
System.out.println("end color by Degree");
//Rank size by centrality
System.out.println("start Rank Size");
Column centralityColumn = graphModel.getNodeTable().getColumn(GraphDistance.BETWEENNESS);
Function centralityRanking = appearanceModel.getNodeFunction(graph, centralityColumn, RankingNodeSizeTransformer.class);
RankingNodeSizeTransformer centralityTransformer = (RankingNodeSizeTransformer) centralityRanking.getTransformer();
centralityTransformer.setMinSize(3);
centralityTransformer.setMaxSize(10);
appearanceController.transform(centralityRanking);
System.out.println("end Rank Size");
//Preview
System.out.println("start set Preview Properties");
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("end set Preview Properties");
//Export
System.out.println("start export");
ExportController ec = Lookup.getDefault().lookup(ExportController.class);
try {
//ec.exportFile(new File("src/headless_simple.pdf"));
ec.exportFile(new File("src/headless_simple.gexf"));
} catch (IOException ex) {
ex.printStackTrace();
return;
}
GraphExporter exporter = (GraphExporter) ec.getExporter("gexf"); //Get GEXF exporter
exporter.setExportVisible(true); //Only exports the visible (filtered) graph
exporter.setWorkspace(workspace);
try {
ec.exportFile(new File("src/io_gexf.gexf"), exporter);
} catch (IOException ex) {
ex.printStackTrace();
return;
}
System.out.println("end export");
System.out.println("process end");
}
}
/**
*
*/
package hello_world;
/**
* @author shibacow
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
System.out.println("Hello, World");
HeadlessSimple headlessSimple = new HeadlessSimple();
headlessSimple.script();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment