Search in sources :

Example 6 with NodeIterable

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

the class ExporterCSV method exportData.

private void exportData(Graph graph) throws Exception {
    int max = graph.getNodeCount();
    Progress.start(progressTicket, max);
    if (!list) {
        if (header) {
            writer.append(SEPARATOR);
            int i = 0;
            NodeIterable itr = graph.getNodes();
            for (Node node : itr) {
                writeMatrixNode(node, i++ < max - 1);
                if (cancel) {
                    itr.doBreak();
                    return;
                }
            }
            writer.append(EOL);
        }
    }
    if (list) {
        NodeIterable itr = graph.getNodes();
        for (Node n : itr) {
            List<Node> neighbours = new ArrayList<>();
            for (Edge e : graph.getEdges(n)) {
                if (!e.isDirected() || (e.isDirected() && n == e.getSource())) {
                    Node m = graph.getOpposite(n, e);
                    neighbours.add(m);
                }
            }
            writeListNode(n, !neighbours.isEmpty());
            for (int j = 0; j < neighbours.size(); j++) {
                writeListNode(neighbours.get(j), j < neighbours.size() - 1);
            }
            writer.append(EOL);
            if (cancel) {
                itr.doBreak();
                return;
            }
        }
    } else {
        Node[] nodes = graph.getNodes().toArray();
        for (Node n : nodes) {
            if (cancel) {
                return;
            }
            writeMatrixNode(n, true);
            for (int j = 0; j < nodes.length; j++) {
                Node m = nodes[j];
                EdgeIterable edges = graph.getEdges(n, m);
                writeEdge(edges, j < nodes.length - 1);
            }
            Progress.progress(progressTicket);
            writer.append(EOL);
        }
    }
    Progress.finish(progressTicket);
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) EdgeIterable(org.gephi.graph.api.EdgeIterable) Node(org.gephi.graph.api.Node) ArrayList(java.util.ArrayList) Edge(org.gephi.graph.api.Edge)

Example 7 with NodeIterable

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

the class ExporterVNA method exportNodeData.

/*
     * prints node data in format "id (attributes)*
     */
private void exportNodeData(Graph graph) throws IOException {
    // header
    writer.append("*Node data\n");
    writer.append("ID");
    for (Column column : graph.getModel().getNodeTable()) {
        if (!column.isProperty()) {
            writer.append(" ").append(column.getTitle().replace(' ', '_'));
        // replace spaces because importer can't read attributes titles in quotes
        }
    }
    writer.append("\n");
    // body
    NodeIterable nodeIterable = graph.getNodes();
    for (Node node : nodeIterable) {
        writer.append(printParameter(node.getId()));
        for (Column column : node.getAttributeColumns()) {
            if (!column.isProperty()) {
                Object value = node.getAttribute(column, graph.getView());
                if (value != null) {
                    writer.append(" ").append(printParameter(value));
                } else {
                    writer.append(" ").append(valueForEmptyAttributes);
                }
            }
        }
        writer.append("\n");
        Progress.progress(progressTicket);
        if (cancel) {
            nodeIterable.doBreak();
            return;
        }
    }
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) Column(org.gephi.graph.api.Column) Node(org.gephi.graph.api.Node)

Example 8 with NodeIterable

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

the class ClusteringCoefficient method bruteForce.

private double bruteForce(Graph graph) {
    // The atrributes computed by the statistics
    Column clusteringColumn = initializeAttributeColunms(graph.getModel());
    float totalCC = 0;
    graph.readLock();
    try {
        Progress.start(progress, graph.getNodeCount());
        int node_count = 0;
        NodeIterable nodesIterable = graph.getNodes();
        for (Node node : nodesIterable) {
            float nodeClusteringCoefficient = computeNodeClusteringCoefficient(graph, node, isDirected);
            if (nodeClusteringCoefficient > -1) {
                saveCalculatedValue(node, clusteringColumn, nodeClusteringCoefficient);
                totalCC += nodeClusteringCoefficient;
            }
            if (isCanceled) {
                nodesIterable.doBreak();
                break;
            }
            node_count++;
            Progress.progress(progress, node_count);
        }
        double clusteringCoeff = totalCC / graph.getNodeCount();
        return clusteringCoeff;
    } finally {
        graph.readUnlockAll();
    }
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) Column(org.gephi.graph.api.Column) Node(org.gephi.graph.api.Node)

Example 9 with NodeIterable

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

the class Degree method calculateAverageDegree.

protected double calculateAverageDegree(Graph graph, boolean isDirected, boolean updateAttributes) {
    double averageDegree = 0;
    DirectedGraph directedGraph = null;
    if (isDirected) {
        directedGraph = (DirectedGraph) graph;
    }
    Progress.start(progress, graph.getNodeCount());
    NodeIterable nodesIterable = graph.getNodes();
    for (Node n : nodesIterable) {
        int inDegree = 0;
        int outDegree = 0;
        int degree = 0;
        if (isDirected) {
            inDegree = calculateInDegree(directedGraph, n);
            outDegree = calculateOutDegree(directedGraph, n);
        }
        degree = calculateDegree(graph, n);
        if (updateAttributes) {
            n.setAttribute(DEGREE, degree);
            if (isDirected) {
                n.setAttribute(INDEGREE, inDegree);
                n.setAttribute(OUTDEGREE, outDegree);
                updateDegreeDists(inDegree, outDegree, degree);
            } else {
                updateDegreeDists(degree);
            }
        }
        averageDegree += degree;
        if (isCanceled) {
            nodesIterable.doBreak();
            break;
        }
        Progress.progress(progress);
    }
    averageDegree /= (isDirected ? 2.0 : 1.0) * graph.getNodeCount();
    return averageDegree;
}
Also used : DirectedGraph(org.gephi.graph.api.DirectedGraph) NodeIterable(org.gephi.graph.api.NodeIterable) Node(org.gephi.graph.api.Node)

Example 10 with NodeIterable

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

the class PageRank method calculateR.

private double calculateR(Graph graph, double[] pagerankValues, HashMap<Node, Integer> indicies, boolean directed, double prob) {
    int N = graph.getNodeCount();
    // Initialize to damping factor
    double r = (1.0 - prob) / N;
    // Calculate dangling nodes (nodes without out edges) contribution to all other nodes.
    // Necessary for all nodes page rank values sum to be 1
    NodeIterable nodesIterable = graph.getNodes();
    double danglingNodesRankContrib = 0;
    for (Node s : graph.getNodes()) {
        int s_index = indicies.get(s);
        int outDegree;
        if (directed) {
            outDegree = ((DirectedGraph) graph).getOutDegree(s);
        } else {
            outDegree = graph.getDegree(s);
        }
        if (outDegree == 0) {
            danglingNodesRankContrib += pagerankValues[s_index];
        }
        if (isCanceled) {
            nodesIterable.doBreak();
            break;
        }
    }
    danglingNodesRankContrib *= prob / N;
    r += danglingNodesRankContrib;
    return r;
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) Node(org.gephi.graph.api.Node)

Aggregations

Node (org.gephi.graph.api.Node)23 NodeIterable (org.gephi.graph.api.NodeIterable)23 Edge (org.gephi.graph.api.Edge)9 EdgeIterable (org.gephi.graph.api.EdgeIterable)8 LinkedList (java.util.LinkedList)3 Column (org.gephi.graph.api.Column)3 Object2DoubleOpenHashMap (it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap)2 Object2ObjectOpenHashMap (it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap)2 ObjectOpenHashSet (it.unimi.dsi.fastutil.objects.ObjectOpenHashSet)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 DirectedGraph (org.gephi.graph.api.DirectedGraph)2 ArrayList (java.util.ArrayList)1 Graph (org.gephi.graph.api.Graph)1 Element (org.w3c.dom.Element)1