2
Copyright 2008 WebAtlas
3
Authors : Patrick J. McSweeney (pjmcswee@syr.edu)
4
Website : http://www.gephi.org
6
This file is part of Gephi.
8
Gephi is free software: you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation, either version 3 of the License, or
11
(at your option) any later version.
13
Gephi is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with Gephi. If not, see <http://www.gnu.org/licenses/>.
21
package org.gephi.statistics.plugin;
24
import java.io.IOException;
25
import java.util.Hashtable;
26
import org.gephi.data.attributes.api.AttributeTable;
27
import org.gephi.data.attributes.api.AttributeColumn;
28
import org.gephi.data.attributes.api.AttributeController;
29
import org.gephi.data.attributes.api.AttributeModel;
30
import org.gephi.data.attributes.api.AttributeOrigin;
31
import org.gephi.data.attributes.api.AttributeRow;
32
import org.gephi.data.attributes.api.AttributeType;
33
import org.gephi.graph.api.DirectedGraph;
34
import org.gephi.graph.api.Edge;
35
import org.gephi.graph.api.EdgeIterable;
36
import org.gephi.graph.api.Graph;
37
import org.gephi.graph.api.GraphModel;
38
import org.gephi.graph.api.Node;
39
import org.gephi.graph.api.UndirectedGraph;
40
import org.gephi.statistics.spi.Statistics;
41
import org.gephi.ui.utils.TempDirUtils;
42
import org.gephi.ui.utils.TempDirUtils.TempDir;
43
import org.gephi.utils.longtask.LongTask;
44
import org.gephi.utils.progress.Progress;
45
import org.gephi.utils.progress.ProgressTicket;
46
import org.jfree.chart.ChartFactory;
47
import org.jfree.chart.ChartRenderingInfo;
48
import org.jfree.chart.ChartUtilities;
49
import org.jfree.chart.JFreeChart;
50
import org.jfree.chart.entity.StandardEntityCollection;
51
import org.jfree.chart.plot.PlotOrientation;
52
import org.jfree.chart.plot.XYPlot;
53
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
54
import org.jfree.data.xy.XYSeries;
55
import org.jfree.data.xy.XYSeriesCollection;
56
import org.openide.util.Lookup;
62
public class PageRank implements Statistics, LongTask {
65
private ProgressTicket mProgress;
67
private boolean mIsCanceled;
69
private double mEpsilon = 0.001;
71
private double mProbability = 0.85;
73
private double[] mPageranks;
75
private boolean mDirected;
77
private String mGraphRevision;
83
public void setUndirected(boolean pUndirected) {
84
mDirected = pUndirected;
91
public boolean getUndirected() {
99
public void execute(GraphModel graphModel, AttributeModel attributeModel) {
104
graph = graphModel.getUndirectedGraph();
106
graph = graphModel.getDirectedGraph();
110
this.mGraphRevision = "(" + graph.getNodeVersion() + ", " + graph.getEdgeVersion() + ")";
111
//DirectedGraph digraph = graphController.getDirectedGraph();
112
int N = graph.getNodeCount();
113
mPageranks = new double[N];
114
double[] temp = new double[N];
115
Hashtable<Node, Integer> indicies = new Hashtable<Node, Integer>();
118
Progress.start(mProgress);
119
for (Node s : graph.getNodes()) {
120
indicies.put(s, index);
121
mPageranks[index] = 1.0f / N;
127
for (Node s : graph.getNodes()) {
128
int s_index = indicies.get(s);
131
out = graph.getDegree(s) > 0;
133
out = ((DirectedGraph) graph).getOutDegree(s) > 0;
137
r += (1.0 - mProbability) * (mPageranks[s_index] / N);
139
r += (mPageranks[s_index] / N);
147
for (Node s : graph.getNodes()) {
148
int s_index = indicies.get(s);
153
eIter = ((UndirectedGraph) graph).getEdges(s);
155
eIter = ((DirectedGraph) graph).getInEdges(s);
158
for (Edge edge : eIter) {
159
Node neighbor = graph.getOpposite(s, edge);
160
int neigh_index = indicies.get(neighbor);
163
normalize = ((UndirectedGraph) graph).getDegree(neighbor);
165
normalize = ((DirectedGraph) graph).getOutDegree(neighbor);
168
temp[s_index] += mProbability * (mPageranks[neigh_index] / normalize);
171
if ((temp[s_index] - mPageranks[s_index]) / mPageranks[s_index] >= mEpsilon) {
181
temp = new double[N];
182
if ((done) || (mIsCanceled)) {
188
AttributeTable nodeClass = attributeModel.getNodeTable();
189
AttributeColumn pangeRanksCol = nodeClass.addColumn("pageranks", "Page Ranks", AttributeType.DOUBLE, AttributeOrigin.COMPUTED, new Double(0));
191
for (Node s : graph.getNodes()) {
192
int s_index = indicies.get(s);
193
AttributeRow row = (AttributeRow) s.getNodeData().getAttributes();
194
row.setValue(pangeRanksCol, mPageranks[s_index]);
204
public String getReport() {
207
XYSeries series = new XYSeries("Series 2");
208
for (int i = 0; i < mPageranks.length; i++) {
209
series.add(i, mPageranks[i]);
214
XYSeriesCollection dataset = new XYSeriesCollection();
215
dataset.addSeries(series);
217
JFreeChart chart = ChartFactory.createXYLineChart(
222
PlotOrientation.VERTICAL,
226
XYPlot plot = (XYPlot) chart.getPlot();
227
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
228
renderer.setSeriesLinesVisible(0, true);
229
renderer.setSeriesShapesVisible(0, false);
230
renderer.setSeriesLinesVisible(1, false);
231
renderer.setSeriesShapesVisible(1, true);
232
renderer.setSeriesShape(1, new java.awt.geom.Ellipse2D.Double(0, 0, 1, 1));
233
plot.setBackgroundPaint(java.awt.Color.WHITE);
234
plot.setDomainGridlinePaint(java.awt.Color.GRAY);
235
plot.setRangeGridlinePaint(java.awt.Color.GRAY);
237
plot.setRenderer(renderer);
240
String imageFile = "";
242
final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
243
TempDir tempDir = TempDirUtils.createTempDir();
244
String fileName = "pageranks.png";
245
File file1 = tempDir.createFile(fileName);
246
imageFile = "<IMG SRC=\"file:" + file1.getAbsolutePath() + "\" " + "WIDTH=\"600\" HEIGHT=\"400\" BORDER=\"0\" USEMAP=\"#chart\"></IMG>";
247
ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
248
} catch (IOException e) {
249
System.out.println(e.toString());
251
String report = new String("<HTML> <BODY> <h1>PageRank Report </h1> "
252
+ "<hr> <br> <h2>Network Revision Number:</h2>"
254
+ "<h2> Parameters: </h2>"
255
+ "Epsilon = " + this.mEpsilon + "<br>"
256
+ "Probability = " + this.mProbability
257
+ "<br> <h2> Results: </h2>"
269
public boolean cancel() {
276
* @param progressTicket
278
public void setProgressTicket(ProgressTicket progressTicket) {
279
mProgress = progressTicket;
286
public void setProbability(double prob) {
294
public void setEpsilon(double eps) {
302
public double getProbability() {
310
public double getEpsilon() {