如何通过gephi-toolkit导出csv表

时间:2017-12-08 08:43:57

标签: java export-to-csv gephi

最近我正在尝试使用gephi-toolkit为几个网络做一些统计,它可以很好地导入csv文件并进行一些度量计算,但是如果我想导出csv表就像我在下面做的那样Gephi,我没有找到任何教程,javadoc中没有描述。 export table by gephi

因为我想对不同的网络进行相同的操作,我想使用 gephi-toolkit 来实现相同的功能,但我已经检查了demos并谷歌为它但未能得到我想要的结果。

为了更清楚地描述我的问题,我在下面发布了我当前的代码,现在我尝试了两种方法,一种是使用 ExporterCSV 的方法,它只返回我的csv文件格式的矩阵我想要的是节点csv文件和边缘csv文件后计算每个网络的每个节点的中介中心性,亲密度中心性等几个指标。我试过的另一种方法是使用 DataTableControllerImpl ,但似乎没有创建文件,我想知道我的代码是否有问题,感谢任何帮助。

public class Transfer95 {
    public void script() {
        //Init a project - and therefore a workspace
        ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
        pc.newProject();
        Workspace workspace = pc.getCurrentWorkspace();

        //Get controllers and models
        ImportController importController = Lookup.getDefault().lookup(ImportController.class);

        //Get models and controllers for this new workspace - will be useful later
        GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();

        //Import file
        Container container,container2;
        try {
            File file_node = new File(getClass().getResource("/resource/season2/club_1_1995.csv").toURI());
            container = importController.importFile(file_node);
            container.getLoader().setEdgeDefault(EdgeDirectionDefault.DIRECTED);   //Force DIRECTED
            container.getLoader().setAllowAutoNode(true);  //create missing nodes
            container.getLoader().setEdgesMergeStrategy(EdgeMergeStrategy.SUM);
            container.getLoader().setAutoScale(true);

            File file_edge = new File(getClass().getResource("/resource/season2/transfer_1_1995.csv").toURI());
            container2 = importController.importFile(file_edge);
            container2.getLoader().setEdgeDefault(EdgeDirectionDefault.DIRECTED);   //Force DIRECTED
            container2.getLoader().setAllowAutoNode(true);  //create missing nodes
            container2.getLoader().setEdgesMergeStrategy(EdgeMergeStrategy.SUM);
            container2.getLoader().setAutoScale(true);

        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }

        //Append imported data to GraphAPI
        importController.process(container, new DefaultProcessor(), workspace);
        importController.process(container2, new AppendProcessor(), workspace); //Use AppendProcessor to append to current workspace


        //See if graph is well imported
        DirectedGraph graph = graphModel.getDirectedGraph();
        System.out.println("Nodes: " + graph.getNodeCount());
        System.out.println("Edges: " + graph.getEdgeCount());

        //count several metrics
        Degree degree=new Degree();
        degree.execute(graph.getModel());      
        System.out.println("Average Degree: "+degree.getAverageDegree());

        WeightedDegree weightedDegree=new WeightedDegree();
        weightedDegree.execute(graph.getModel());      
        System.out.println("Average Weighted Degree: "+weightedDegree.getAverageDegree());

        ClusteringCoefficient clusteringcoefficient=new ClusteringCoefficient();
        clusteringcoefficient.execute(graph.getModel());      
        System.out.println("Average Clustering Coefficient: "+clusteringcoefficient.getAverageClusteringCoefficient());

        GraphDistance graphDistance=new GraphDistance();
        graphDistance.execute(graph.getModel());      
        System.out.println("Average Path Length: "+graphDistance.getPathLength());
        System.out.println("Network Diameter: "+graphDistance.getDiameter());

        Modularity modularity=new Modularity();
        modularity.execute(graph.getModel());      
        System.out.println("Modularity: "+modularity.getModularity());

        GraphDensity graphDensity=new GraphDensity();
        graphDensity.execute(graph.getModel());      
        System.out.println("Graph Density: "+graphDensity.getDensity());


        //Export method 1
//        ExportController ec = Lookup.getDefault().lookup(ExportController.class);
//        ExporterCSV exporterCSV=(ExporterCSV)ec.getExporter("csv");
//        try {
////            ec.exportFile(new File("src/resource/output/test_95.csv"));
//            ec.exportFile(new File("src/resource/output/test_95.csv"), exporterCSV);
//        } catch (IOException ex) {
//            ex.printStackTrace();
//            return;
//        }

        //Export method 2
//        Lookup.getDefault().lookup(DataTablesController.class).setDataTablesEventListener(DataTableTopComponent.this);
        DataTablesControllerImpl csvExp=new DataTablesControllerImpl();        
//        Lookup.getDefault().lookup(DataTablesControllerImpl.class).setDataTablesEventListener(csvExp.getDataTablesEventListener());                
//        DataTablesControllerImpl dataTablesController = Lookup.getDefault().lookup(DataTablesController.class);
        csvExp.exportCurrentTable();

    }

    public static void main(String[] args){
        Transfer95 test=new Transfer95();
        test.script();
    }
}

1 个答案:

答案 0 :(得分:0)

这不是最好的解决方案,但就我而言,它奏效了。

从 Gephi 源代码 (GitHub link) 导入并在您的项目中使用 ExporterSpreadsheet.java

例如导出节点表:

        try {
            ExporterSpreadsheet exporter = new ExporterSpreadsheet();
            exporter.setWorkspace(workspace);
            exporter.setWriter(new FileWriter(new File("nodes.csv")));
            exporter.setTableToExport(ExporterSpreadsheet.ExportTable.NODES);
            exporter.execute();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

同理,对于边表:

        try {
            ExporterSpreadsheet exporter = new ExporterSpreadsheet();
            exporter.setWorkspace(workspace);
            exporter.setWriter(new FileWriter(new File("edges.csv")));
            exporter.setTableToExport(ExporterSpreadsheet.ExportTable.EDGES);
            exporter.execute();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

作为输出,您将获得与 Gephi 使用数据实验室的 Export table 按钮导出的相同的 CSV 文件。