use of org.gephi.graph.api.EdgeIterable in project gephi by gephi.
the class PageRank method setInitialValues.
private void setInitialValues(Graph graph, Map<Node, Integer> indicies, double[] pagerankValues, double[] weights, boolean directed, boolean useWeights) {
final int N = graph.getNodeCount();
for (Node s : graph.getNodes()) {
final int index = indicies.get(s);
pagerankValues[index] = 1.0 / N;
if (useWeights) {
double sum = 0;
EdgeIterable eIter;
if (directed) {
eIter = ((DirectedGraph) graph).getOutEdges(s);
} else {
eIter = ((UndirectedGraph) graph).getEdges(s);
}
for (Edge edge : eIter) {
if (!edge.isSelfLoop()) {
sum += edge.getWeight();
}
}
weights[index] = sum;
}
}
}
use of org.gephi.graph.api.EdgeIterable in project gephi by gephi.
the class ExporterGDF method exportData.
private void exportData(Graph graph, GraphModel graphModel) throws Exception {
Progress.start(progressTicket);
defaultNodeColumns(graph);
defaultEdgeColumns(graph);
Column[] nodeColumns = attributesNodeColumns(graphModel);
Column[] edgeColumns = attributesEdgeColumns(graphModel);
StringBuilder stringBuilder = new StringBuilder();
// Node intro
stringBuilder.append("nodedef> name VARCHAR,");
// Default Node columns title
for (NodeColumnsGDF c : defaultNodeColumnsGDFs) {
if (c.isEnable()) {
stringBuilder.append(c.getTitle());
stringBuilder.append(" ");
stringBuilder.append(c.getType().toString().toUpperCase());
if (c.getDefaultValue() != null) {
stringBuilder.append(" default ");
stringBuilder.append(c.getDefaultValue().toString());
}
stringBuilder.append(",");
}
}
// Attributes Node columns
for (Column c : nodeColumns) {
if (!c.isProperty()) {
stringBuilder.append(c.getTitle());
stringBuilder.append(" ");
DataTypeGDF dataTypeGDF = getDataTypeGDF(c.getTypeClass());
stringBuilder.append(dataTypeGDF.toString().toUpperCase());
if (c.getDefaultValue() != null) {
stringBuilder.append(" default ");
stringBuilder.append(c.getDefaultValue().toString());
}
stringBuilder.append(",");
}
}
// Remove last coma
stringBuilder.setLength(stringBuilder.length() - 1);
stringBuilder.append("\n");
// Options
if (normalize) {
calculateMinMax(graph);
}
// Calculate progress units count
int max = graph.getNodeCount() + graph.getEdgeCount();
Progress.switchToDeterminate(progressTicket, max);
// Node lines
NodeIterable itr = graph.getNodes();
for (Node node : itr) {
if (cancel) {
itr.doBreak();
return;
}
// Id
stringBuilder.append(node.getId());
stringBuilder.append(",");
// Default columns
for (NodeColumnsGDF c : defaultNodeColumnsGDFs) {
if (c.isEnable()) {
c.writeData(stringBuilder, node);
stringBuilder.append(",");
}
}
// Attributes columns
for (Column c : nodeColumns) {
if (!c.isProperty()) {
Object val = node.getAttribute(c, graph.getView());
if (val != null) {
if (c.getTypeClass().equals(String.class) || c.getTypeClass().equals(String[].class)) {
String quote = !useQuotes ? "" : simpleQuotes ? "'" : "\"";
stringBuilder.append(quote);
stringBuilder.append(val.toString());
stringBuilder.append(quote);
} else {
stringBuilder.append(val.toString());
}
}
stringBuilder.append(",");
}
}
// Remove last coma
stringBuilder.setLength(stringBuilder.length() - 1);
stringBuilder.append("\n");
Progress.progress(progressTicket);
}
// Edge intro
stringBuilder.append("edgedef> node1,node2,");
// Edge settings helper
for (Edge e : graph.getEdges()) {
edgeColors = edgeColors || e.alpha() != 0;
edgeLabels = edgeLabels || (e.getLabel() != null && !e.getLabel().isEmpty());
}
// Edge columns title
for (EdgeColumnsGDF c : defaultEdgeColumnsGDFs) {
if (c.isEnable()) {
stringBuilder.append(c.getTitle());
stringBuilder.append(" ");
stringBuilder.append(c.getType().toString().toUpperCase());
if (c.getDefaultValue() != null) {
stringBuilder.append(" default ");
stringBuilder.append(c.getDefaultValue().toString());
}
stringBuilder.append(",");
}
}
// Attributes Edge columns
for (Column c : edgeColumns) {
if (!c.isProperty()) {
stringBuilder.append(c.getTitle());
stringBuilder.append(" ");
DataTypeGDF dataTypeGDF = getDataTypeGDF(c.getTypeClass());
stringBuilder.append(dataTypeGDF.toString().toUpperCase());
if (c.getDefaultValue() != null) {
stringBuilder.append(" default ");
stringBuilder.append(c.getDefaultValue().toString());
}
stringBuilder.append(",");
}
}
// Remove last coma
stringBuilder.setLength(stringBuilder.length() - 1);
stringBuilder.append("\n");
// Edge lines
EdgeIterable itrEdges = graph.getEdges();
for (Edge edge : itrEdges) {
if (cancel) {
itrEdges.doBreak();
return;
}
// Source & Target
stringBuilder.append(edge.getSource().getId());
stringBuilder.append(",");
stringBuilder.append(edge.getTarget().getId());
stringBuilder.append(",");
// Default columns
for (EdgeColumnsGDF c : defaultEdgeColumnsGDFs) {
if (c.isEnable()) {
c.writeData(stringBuilder, edge);
stringBuilder.append(",");
}
}
// Attributes columns
for (Column c : edgeColumns) {
if (!c.isProperty()) {
Object val = edge.getAttribute(c, graph.getView());
if (val != null) {
if (c.getTypeClass().equals(String.class) || c.getTypeClass().equals(String[].class)) {
String quote = !useQuotes ? "" : simpleQuotes ? "'" : "\"";
stringBuilder.append(quote);
stringBuilder.append(val.toString());
stringBuilder.append(quote);
} else {
stringBuilder.append(val.toString());
}
}
stringBuilder.append(",");
}
}
// Remove last coma
stringBuilder.setLength(stringBuilder.length() - 1);
stringBuilder.append("\n");
Progress.progress(progressTicket);
}
// Write StringBuilder
if (!cancel) {
writer.append(stringBuilder);
}
Progress.finish(progressTicket);
}
use of org.gephi.graph.api.EdgeIterable in project gephi by gephi.
the class ExporterVNA method exportEdgeData.
/*
* prints edge data as "from to (strength)? (attributes)*"
*/
private void exportEdgeData(Graph graph) throws IOException {
writer.append("*Tie data\n");
writer.append("from to");
if (exportEdgeWeight) {
writer.append(" strength");
}
if (exportAttributes) {
for (Column col : graph.getModel().getEdgeTable()) {
if (!col.isProperty()) {
writer.append(" ").append(printParameter(col.getTitle()).replace(' ', '_'));
// replace spaces because importer can't read attributes titles in quotes
}
}
}
writer.append("\n");
EdgeIterable edgeIterable = graph.getEdges();
for (Edge edge : edgeIterable) {
// all edges in vna are directed, so make clone
printEdgeData(edge, edge.getSource(), edge.getTarget(), graph);
if (!edge.isDirected() && !edge.isSelfLoop()) {
printEdgeData(edge, edge.getTarget(), edge.getSource(), graph);
}
Progress.progress(progressTicket);
if (cancel) {
edgeIterable.doBreak();
return;
}
}
}
Aggregations