Search in sources :

Example 21 with Edge

use of org.gephi.graph.api.Edge in project gephi by gephi.

the class AttributeTableCSVExporter method writeCSVFile.

/**
     * <p>Export a AttributeTable to the specified file.</p>
     *
     * @param graph Graph containing the table and rows
     * @param table Table to export. Cannot be null
     * @param out Ouput stream to write. Cannot be null.
     * @param separator Separator to use for separating values of a row in the CSV file. If null ',' will be used.
     * @param charset Charset encoding for the file. If null, UTF-8 will be used
     * @param columnIndexesToExport Indicates the indexes of the columns to export. All columns will be exported if null. For special columns in edges table, use {@code FAKE_COLUMN_EDGE_} values in this class.
     * @param rows Elements (table rows: nodes/edges) to include in the exported file. Cannot be null. If null, all nodes/edges will be exported.
     * @throws IOException When an error happens while writing the file
     */
public static void writeCSVFile(Graph graph, Table table, OutputStream out, Character separator, Charset charset, Integer[] columnIndexesToExport, Element[] rows) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("out cannot be null");
    }
    if (separator == null) {
        separator = DEFAULT_SEPARATOR;
    }
    if (charset == null) {
        charset = Charset.forName("UTF-8");
    }
    AttributeColumnsController ac = Lookup.getDefault().lookup(AttributeColumnsController.class);
    boolean isEdgeTable = ac.isEdgeTable(table);
    if (rows == null) {
        if (isEdgeTable) {
            rows = graph.getEdges().toArray();
        } else {
            rows = graph.getNodes().toArray();
        }
    }
    TimeFormat timeFormat = graph.getModel().getTimeFormat();
    DateTimeZone timeZone = graph.getModel().getTimeZone();
    if (columnIndexesToExport == null) {
        List<Integer> columnIndexesToExportList = new ArrayList<>();
        //Add special columns for edges table:
        if (isEdgeTable) {
            columnIndexesToExportList.add(FAKE_COLUMN_EDGE_SOURCE);
            columnIndexesToExportList.add(FAKE_COLUMN_EDGE_TARGET);
            columnIndexesToExportList.add(FAKE_COLUMN_EDGE_TYPE);
        }
        for (Column column : table) {
            columnIndexesToExportList.add(column.getIndex());
        }
        columnIndexesToExport = columnIndexesToExportList.toArray(new Integer[0]);
    }
    CsvWriter writer = new CsvWriter(out, separator, charset);
    //Write column headers:
    for (int column = 0; column < columnIndexesToExport.length; column++) {
        int columnIndex = columnIndexesToExport[column];
        if (columnIndex == FAKE_COLUMN_EDGE_SOURCE) {
            writer.write("Source");
        } else if (columnIndex == FAKE_COLUMN_EDGE_TARGET) {
            writer.write("Target");
        } else if (columnIndex == FAKE_COLUMN_EDGE_TYPE) {
            writer.write("Type");
        } else {
            //Use the title only if it's the same as the id (case insensitive):
            String columnId = table.getColumn(columnIndex).getId();
            String columnTitle = table.getColumn(columnIndex).getId();
            String columnHeader = columnId.equalsIgnoreCase(columnTitle) ? columnTitle : columnId;
            writer.write(columnHeader, true);
        }
    }
    writer.endRecord();
    //Write rows:
    Object value;
    String text;
    for (int row = 0; row < rows.length; row++) {
        for (int i = 0; i < columnIndexesToExport.length; i++) {
            int columnIndex = columnIndexesToExport[i];
            if (columnIndex == FAKE_COLUMN_EDGE_SOURCE) {
                value = ((Edge) rows[row]).getSource().getId();
            } else if (columnIndex == FAKE_COLUMN_EDGE_TARGET) {
                value = ((Edge) rows[row]).getTarget().getId();
            } else if (columnIndex == FAKE_COLUMN_EDGE_TYPE) {
                value = ((Edge) rows[row]).isDirected() ? "Directed" : "Undirected";
            } else {
                value = rows[row].getAttribute(table.getColumn(columnIndex));
            }
            if (value != null) {
                text = AttributeUtils.print(value, timeFormat, timeZone);
            } else {
                text = "";
            }
            writer.write(text, true);
        }
        writer.endRecord();
    }
    writer.close();
}
Also used : TimeFormat(org.gephi.graph.api.TimeFormat) CsvWriter(com.csvreader.CsvWriter) ArrayList(java.util.ArrayList) DateTimeZone(org.joda.time.DateTimeZone) Column(org.gephi.graph.api.Column) AttributeColumnsController(org.gephi.datalab.api.AttributeColumnsController) Edge(org.gephi.graph.api.Edge)

Example 22 with Edge

use of org.gephi.graph.api.Edge in project gephi by gephi.

the class PageRankNGTest method testUndirectedWeightedGraphPageRank.

@Test
public void testUndirectedWeightedGraphPageRank() {
    pc.newProject();
    GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
    UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
    Node node1 = graphModel.factory().newNode("0");
    Node node2 = graphModel.factory().newNode("1");
    Node node3 = graphModel.factory().newNode("2");
    Node node4 = graphModel.factory().newNode("3");
    Node node5 = graphModel.factory().newNode("4");
    Node node6 = graphModel.factory().newNode("5");
    undirectedGraph.addNode(node1);
    undirectedGraph.addNode(node2);
    undirectedGraph.addNode(node3);
    undirectedGraph.addNode(node4);
    undirectedGraph.addNode(node5);
    undirectedGraph.addNode(node6);
    Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
    Edge edge23 = graphModel.factory().newEdge(node2, node3, 0, 10, false);
    Edge edge34 = graphModel.factory().newEdge(node3, node4, false);
    Edge edge45 = graphModel.factory().newEdge(node4, node5, false);
    Edge edge56 = graphModel.factory().newEdge(node5, node6, false);
    Edge edge61 = graphModel.factory().newEdge(node6, node1, false);
    undirectedGraph.addEdge(edge12);
    undirectedGraph.addEdge(edge23);
    undirectedGraph.addEdge(edge34);
    undirectedGraph.addEdge(edge45);
    undirectedGraph.addEdge(edge56);
    undirectedGraph.addEdge(edge61);
    UndirectedGraph graph = graphModel.getUndirectedGraph();
    PageRank pr = new PageRank();
    double[] pageRank;
    HashMap<Node, Integer> indicies = pr.createIndiciesMap(graph);
    pageRank = pr.calculatePagerank(graph, indicies, false, true, 0.001, 0.85);
    int index1 = indicies.get(node1);
    int index2 = indicies.get(node2);
    int index3 = indicies.get(node3);
    int index6 = indicies.get(node6);
    double diff = 0.01;
    double pr1 = pageRank[index1];
    double pr2 = pageRank[index2];
    double pr3 = pageRank[index3];
    double pr6 = pageRank[index6];
    assertTrue(Math.abs(pr2 - pr3) < diff);
    assertTrue(pr1 < pr2);
    assertTrue(pr1 < pr6);
}
Also used : GraphModel(org.gephi.graph.api.GraphModel) UndirectedGraph(org.gephi.graph.api.UndirectedGraph) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController) Test(org.testng.annotations.Test)

Example 23 with Edge

use of org.gephi.graph.api.Edge in project gephi by gephi.

the class WeightedDegreeNGTest method testDirectedStarOutGraphDegree.

@Test
public void testDirectedStarOutGraphDegree() {
    GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
    DirectedGraph directedGraph = graphModel.getDirectedGraph();
    Node firstNode = graphModel.factory().newNode("0");
    directedGraph.addNode(firstNode);
    for (int i = 1; i <= 5; i++) {
        Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
        directedGraph.addNode(currentNode);
        Edge currentEdge = graphModel.factory().newEdge(firstNode, currentNode);
        directedGraph.addEdge(currentEdge);
    }
    DirectedGraph graph = graphModel.getDirectedGraph();
    Node n1 = graph.getNode("0");
    Node n3 = graph.getNode("2");
    WeightedDegree d = new WeightedDegree();
    d.execute(graph);
    double inDegree1 = (Double) n1.getAttribute(WeightedDegree.WINDEGREE);
    double outDegree1 = (Double) n1.getAttribute(WeightedDegree.WOUTDEGREE);
    double degree3 = (Double) n3.getAttribute(WeightedDegree.WDEGREE);
    assertEquals(inDegree1, 0.0);
    assertEquals(outDegree1, 5.0);
    assertEquals(degree3, 1.0);
}
Also used : DirectedGraph(org.gephi.graph.api.DirectedGraph) GraphModel(org.gephi.graph.api.GraphModel) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController) Test(org.testng.annotations.Test)

Example 24 with Edge

use of org.gephi.graph.api.Edge in project gephi by gephi.

the class ModularityNGTest method testComputeBarbellGraphModularityHasHighWeight.

@Test
public void testComputeBarbellGraphModularityHasHighWeight() {
    GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(4);
    UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
    Node[] nodes = new Node[4];
    for (int i = 0; i < 4; i++) {
        Node currentNode = graphModel.factory().newNode(((Integer) (i + 4)).toString());
        nodes[i] = currentNode;
        undirectedGraph.addNode(currentNode);
    }
    for (int i = 0; i < 3; i++) {
        for (int j = i + 1; j < 4; j++) {
            Edge currentEdge = graphModel.factory().newEdge(nodes[i], nodes[j], false);
            undirectedGraph.addEdge(currentEdge);
        }
    }
    Edge currentEdge = graphModel.factory().newEdge(undirectedGraph.getNode("0"), undirectedGraph.getNode("5"), 0, 100.f, false);
    undirectedGraph.addEdge(currentEdge);
    UndirectedGraph graph = graphModel.getUndirectedGraph();
    Modularity mod = new Modularity();
    Modularity.CommunityStructure theStructure = mod.new CommunityStructure(graph);
    int[] comStructure = new int[graph.getNodeCount()];
    HashMap<String, Double> modularityValues = mod.computeModularity(graph, theStructure, comStructure, 1., true, true);
    int class4 = comStructure[0];
    int class5 = comStructure[5];
    assertEquals(class4, class5);
}
Also used : Node(org.gephi.graph.api.Node) GraphModel(org.gephi.graph.api.GraphModel) UndirectedGraph(org.gephi.graph.api.UndirectedGraph) Edge(org.gephi.graph.api.Edge) Test(org.testng.annotations.Test)

Example 25 with Edge

use of org.gephi.graph.api.Edge in project gephi by gephi.

the class PageRankNGTest method testDirectedStarOutGraphPageRank.

@Test
public void testDirectedStarOutGraphPageRank() {
    pc.newProject();
    GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
    DirectedGraph directedGraph = graphModel.getDirectedGraph();
    Node firstNode = graphModel.factory().newNode("0");
    directedGraph.addNode(firstNode);
    for (int i = 1; i <= 5; i++) {
        Node currentNode = graphModel.factory().newNode(((Integer) i).toString());
        directedGraph.addNode(currentNode);
        Edge currentEdge = graphModel.factory().newEdge(firstNode, currentNode);
        directedGraph.addEdge(currentEdge);
    }
    DirectedGraph graph = graphModel.getDirectedGraph();
    PageRank pr = new PageRank();
    double[] pageRank;
    HashMap<Node, Integer> indicies = pr.createIndiciesMap(graph);
    pageRank = pr.calculatePagerank(graph, indicies, true, false, 0.001, 0.85);
    Node n1 = graph.getNode("0");
    Node n2 = graph.getNode("1");
    Node n3 = graph.getNode("2");
    Node n5 = graph.getNode("4");
    int index1 = indicies.get(n1);
    int index2 = indicies.get(n2);
    int index3 = indicies.get(n3);
    int index5 = indicies.get(n5);
    double pr1 = pageRank[index1];
    double pr2 = pageRank[index2];
    double pr3 = pageRank[index3];
    double pr5 = pageRank[index5];
    double res = 0.146;
    double diff = 0.01;
    assertTrue(pr1 < pr3);
    assertEquals(pr2, pr5);
    assertTrue(Math.abs(pr1 - res) < diff);
}
Also used : DirectedGraph(org.gephi.graph.api.DirectedGraph) GraphModel(org.gephi.graph.api.GraphModel) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController) Test(org.testng.annotations.Test)

Aggregations

Edge (org.gephi.graph.api.Edge)151 Node (org.gephi.graph.api.Node)122 GraphModel (org.gephi.graph.api.GraphModel)84 GraphController (org.gephi.graph.api.GraphController)79 Test (org.testng.annotations.Test)67 UndirectedGraph (org.gephi.graph.api.UndirectedGraph)46 DirectedGraph (org.gephi.graph.api.DirectedGraph)44 Graph (org.gephi.graph.api.Graph)31 HashMap (java.util.HashMap)26 EdgeIterable (org.gephi.graph.api.EdgeIterable)18 LinkedList (java.util.LinkedList)13 Column (org.gephi.graph.api.Column)11 NodeIterable (org.gephi.graph.api.NodeIterable)9 ArrayList (java.util.ArrayList)8 Table (org.gephi.graph.api.Table)6 GraphView (org.gephi.graph.api.GraphView)5 HashSet (java.util.HashSet)4 Interval (org.gephi.graph.api.Interval)4 TimeFormat (org.gephi.graph.api.TimeFormat)4 DateTimeZone (org.joda.time.DateTimeZone)4