gephi / gephi-toolkit-demos

Quick demos using the Toolkit
95 stars 88 forks source link

how to export csv table by gephi-toolkit #7

Open LancelotHolmes opened 6 years ago

LancelotHolmes commented 6 years ago

recently I'm trying to use gephi-toolkit to do some statistics for several networks,it works fine to import the csv file and do some metrics computation, but if I want to export the csv table like what I do below in the Gephi, I do not find any tutorials and there is not description in the javadoc.

test

since I want to do the same operations for different networks,I want to use gephi-toolkit to implement the same function,but I've checked the demos and google for it but failed to get the result I want.


To describe my question clearer,I post my current code below, now I've tried two methods, one is using the methods of ExporterCSV which only returned me with the matrix in csv file format while what I want is a node csv file and an edge csv file after computing several metrics like betweenness centrality, closeness centrality and so on for each node of each network. And the other method I've tried is using DataTableControllerImpl but it seems that no files has been created, I want to know if there is something wrong with my code, any help is appreciated.

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();
    }
}
loveneet29 commented 6 years ago

Hey have you found the solution for csv export . Thanks in adavance

matthewnoell commented 6 years ago

Here is what I came up with to start with. In my case I wanted to dump PageRank. I probably need to clean this up.

   ExportController ec = Lookup.getDefault().lookup(ExportController.class);
    ExporterSpreadsheet.ExportTable currentTable = ExporterSpreadsheet.ExportTable.NODES;

    for (GraphFileExporterBuilder builder : Lookup.getDefault().lookupAll(GraphFileExporterBuilder.class)) {
        if (builder.getName().toLowerCase().startsWith("spreadsheet")) {
            GraphExporter exporter = builder.buildExporter();
            ((ExporterSpreadsheet) exporter).setTableToExport(currentTable);
            try {
                ec.exportFile(new File("ranking.csv"), exporter);
            } catch (IOException ex) {
                ex.printStackTrace();
                return;                  
            }
        }
    }
loveneet29 commented 5 years ago

Here is what I came up with to start with. In my case I wanted to dump PageRank. I probably need to clean this up.

   ExportController ec = Lookup.getDefault().lookup(ExportController.class);
    ExporterSpreadsheet.ExportTable currentTable = ExporterSpreadsheet.ExportTable.NODES;

    for (GraphFileExporterBuilder builder : Lookup.getDefault().lookupAll(GraphFileExporterBuilder.class)) {
        if (builder.getName().toLowerCase().startsWith("spreadsheet")) {
            GraphExporter exporter = builder.buildExporter();
            ((ExporterSpreadsheet) exporter).setTableToExport(currentTable);
            try {
                ec.exportFile(new File("ranking.csv"), exporter);
            } catch (IOException ex) {
                ex.printStackTrace();
                return;                  
            }
        }
    }

Thanks Matthew....it works ...you save my day :+1: