From 1f94d75846300f5db6b64acb4c754e003d99ce01 Mon Sep 17 00:00:00 2001 From: MIBea13 Date: Wed, 26 Mar 2025 22:24:10 +0200 Subject: [PATCH 1/3] added personalized PageRank example --- .../personalized_pagerank.py | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 doc/examples_sphinx-gallery/personalized_pagerank.py diff --git a/doc/examples_sphinx-gallery/personalized_pagerank.py b/doc/examples_sphinx-gallery/personalized_pagerank.py new file mode 100644 index 000000000..7b6edc447 --- /dev/null +++ b/doc/examples_sphinx-gallery/personalized_pagerank.py @@ -0,0 +1,81 @@ +""" +.. _tutorials-personalized_pagerank: + +=============================== +Personalized PageRank on a grid +=============================== + +This example demonstrates how to calculate and visualize personalized PageRank on a grid. We use the :meth:`igraph.GraphBase.personalized_pagerank` method, and demonstrate the effects on a grid graph. +""" + +import matplotlib.cm as cm +import matplotlib.pyplot as plt +import numpy as np + +import igraph as ig + + +# %% +# We define a function that plots the graph on a Matplotlib axis, along with +# its personalized PageRank values. The function also generates a +# color bar on the side to see how the values change. +# We use `Matplotlib's Normalize class `_ +# to set the colors and ensure that our color bar range is correct. +def plot_pagerank(graph: ig.Graph, p_pagerank: list[float]): + """Plots personalized PageRank values on a grid graph with a colorbar. + + Parameters + ---------- + graph : ig.Graph + graph to plot + p_pagerank : list[float] + calculated personalized PageRank values + """ + # Create the axis for matplotlib + _, ax = plt.subplots(figsize=(8, 8)) + + # Create a matplotlib colormap + # coolwarm goes from blue (lowest value) to red (highest value) + cmap = cm.coolwarm + + # Normalize the PageRank values for colormap + normalized_pagerank = ig.rescale(p_pagerank) + + graph.vs["color"] = [cmap(pr) for pr in normalized_pagerank] + graph.vs["size"] = ig.rescale(p_pagerank, (20, 40)) + graph.es["color"] = "gray" + graph.es["width"] = 1.5 + + # Plot the graph + ig.plot(graph, target=ax, layout=graph.layout_grid()) + + # Add a colorbar + sm = cm.ScalarMappable(norm=plt.Normalize(min(p_pagerank), max(p_pagerank)), cmap=cmap) + plt.colorbar(sm, ax=ax, label="Personalized PageRank") + + plt.title("Graph with Personalized PageRank") + plt.show() + + +# %% +# First, we generate a graph, e.g. a Lattice Graph, which basically is a ``dim x dim`` grid: +dim = 5 +grid_size = (dim, dim) # dim rows, dim columns +g = ig.Graph.Lattice(dim=grid_size, circular=False) + +# %% +# Then we initialize the ``reset_vector`` (it's length should be equal to the number of vertices in the graph): +reset_vector = np.zeros(g.vcount()) + +# %% +# Then we set the nodes to prioritize, for example nodes with indices ``0`` and ``6``: +reset_vector[0] = 1 +reset_vector[6] = 1 + +# %% +# Then we calculate the personalized PageRank: +personalized_page_rank = g.personalized_pagerank(weights=None, damping=0.85, reset=reset_vector) + +# %% +# Finally, we plot the graph with the personalized PageRank values: +plot_pagerank(g, personalized_page_rank) From b1b4efaf2d59cc0f4e5b2170cf426ceff17196bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bea=20M=C3=A1rton?= Date: Fri, 28 Mar 2025 12:05:51 +0200 Subject: [PATCH 2/3] Update doc/examples_sphinx-gallery/personalized_pagerank.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Szabolcs Horvát --- doc/examples_sphinx-gallery/personalized_pagerank.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/examples_sphinx-gallery/personalized_pagerank.py b/doc/examples_sphinx-gallery/personalized_pagerank.py index 7b6edc447..d2b329a32 100644 --- a/doc/examples_sphinx-gallery/personalized_pagerank.py +++ b/doc/examples_sphinx-gallery/personalized_pagerank.py @@ -5,7 +5,7 @@ Personalized PageRank on a grid =============================== -This example demonstrates how to calculate and visualize personalized PageRank on a grid. We use the :meth:`igraph.GraphBase.personalized_pagerank` method, and demonstrate the effects on a grid graph. +This example demonstrates how to calculate and visualize personalized PageRank on a grid. We use the :meth:`igraph.Graph.personalized_pagerank` method, and demonstrate the effects on a grid graph. """ import matplotlib.cm as cm From 8a7a1aa400f76a50cfc632b0d0ce94ce90cb0cec Mon Sep 17 00:00:00 2001 From: MIBea13 Date: Fri, 28 Mar 2025 13:23:01 +0200 Subject: [PATCH 3/3] added changes requested in PR --- .../personalized_pagerank.py | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/doc/examples_sphinx-gallery/personalized_pagerank.py b/doc/examples_sphinx-gallery/personalized_pagerank.py index d2b329a32..2fd044612 100644 --- a/doc/examples_sphinx-gallery/personalized_pagerank.py +++ b/doc/examples_sphinx-gallery/personalized_pagerank.py @@ -8,19 +8,24 @@ This example demonstrates how to calculate and visualize personalized PageRank on a grid. We use the :meth:`igraph.Graph.personalized_pagerank` method, and demonstrate the effects on a grid graph. """ +# %% +# .. note:: +# +# The PageRank score of a vertex reflects the probability that a random walker will be at that vertex over the long run. At each step the walker has a 1 - damping chance to restart the walk and pick a starting vertex according to the probabilities defined in the reset vector. + +import igraph as ig import matplotlib.cm as cm import matplotlib.pyplot as plt import numpy as np -import igraph as ig - - # %% # We define a function that plots the graph on a Matplotlib axis, along with # its personalized PageRank values. The function also generates a # color bar on the side to see how the values change. # We use `Matplotlib's Normalize class `_ # to set the colors and ensure that our color bar range is correct. + + def plot_pagerank(graph: ig.Graph, p_pagerank: list[float]): """Plots personalized PageRank values on a grid graph with a colorbar. @@ -54,6 +59,7 @@ def plot_pagerank(graph: ig.Graph, p_pagerank: list[float]): plt.colorbar(sm, ax=ax, label="Personalized PageRank") plt.title("Graph with Personalized PageRank") + plt.axis("equal") plt.show() @@ -68,14 +74,23 @@ def plot_pagerank(graph: ig.Graph, p_pagerank: list[float]): reset_vector = np.zeros(g.vcount()) # %% -# Then we set the nodes to prioritize, for example nodes with indices ``0`` and ``6``: +# Then we set the nodes to prioritize, for example nodes with indices ``0`` and ``18``: reset_vector[0] = 1 -reset_vector[6] = 1 +reset_vector[18] = 0.65 # %% # Then we calculate the personalized PageRank: -personalized_page_rank = g.personalized_pagerank(weights=None, damping=0.85, reset=reset_vector) +personalized_page_rank = g.personalized_pagerank(damping=0.85, reset=reset_vector) # %% # Finally, we plot the graph with the personalized PageRank values: plot_pagerank(g, personalized_page_rank) + + +# %% +# Alternatively, we can play around with the ``damping`` parameter: +personalized_page_rank = g.personalized_pagerank(damping=0.45, reset=reset_vector) + +# %% +# Here we can see the same plot with the new damping parameter: +plot_pagerank(g, personalized_page_rank)