Repository: networkx/networkx

Issue:
Gallery: spectral graph partition very slow
Just opening this issue to track a performance issue I noticed with the docs.

The image segmentation and spectral graph example added in #7040 is extremely slow - it takes about 100s to run on a fast machine (ryzen 5950X) and is even worse in CI, taking nearly 4 minutes per doc build to run the example.

This should be investigated ASAP to reduce CI burden. There are multiple 3D animations in the example - I suspect switching them to static images would resolve the issue, though there will have to be a bit of profiling to verify that the animation(s) are the bottleneck and not the analysis itself.


Discussion:

Resolving patch:
diff --git a/doc/conf.py b/doc/conf.py
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -54,6 +54,7 @@
     "image_scrapers": ("matplotlib",),
     "matplotlib_animations": True,
     "plot_gallery": "True",
+    "reference_url": {"sphinx_gallery": None},
 }
 # Add pygraphviz png scraper, if available
 try:
diff --git a/examples/algorithms/plot_image_segmentation_spectral_graph_partiion.py b/examples/algorithms/plot_image_segmentation_spectral_graph_partiion.py
--- a/examples/algorithms/plot_image_segmentation_spectral_graph_partiion.py
+++ b/examples/algorithms/plot_image_segmentation_spectral_graph_partiion.py
@@ -1,12 +1,16 @@
 """
-=====================================================
+==================================================
 Image Segmentation via Spectral Graph Partitioning
-=====================================================
-Example of partitioning a undirected graph obtained by `k-neighbors`
+==================================================
+
+Example of partitioning a undirected graph obtained by ``k-neighbors``
 from an RGB image into two subgraphs using spectral clustering
 illustrated by 3D plots of the original labeled data points in RGB 3D space
 vs the bi-partition marking performed by graph partitioning via spectral clustering.
-All 3D plots and animations use the 3D spectral layout.
+All 3D plots use the 3D spectral layout.
+
+See :ref:`sphx_glr_auto_examples_3d_drawing` for recipes to create 3D animations
+from these visualizations.
 """
 import numpy as np
 import networkx as nx
@@ -15,7 +19,7 @@
 from matplotlib.lines import Line2D
 from sklearn.cluster import SpectralClustering
 
-# sphinx_gallery_thumbnail_number = 4
+# sphinx_gallery_thumbnail_number = 3
 
 ###############################################################################
 # Create an example 3D dataset "The Rings".
@@ -129,40 +133,6 @@ def _scatter_plot(ax, X, array_of_markers, axis_plot=True):
 
 plt.show()
 
-###############################################################################
-# Generate the rotating animation of the clustered data.
-# ------------------------------------------------------
-# The data points are marked according to clustering and rotated
-# in the 3D animation.
-
-
-def _init():
-    ax.clear()
-    _scatter_plot(ax, X, array_of_markers)
-    ax.grid(False)
-    ax.set_axis_off()
-    ax.view_init(elev=6.0, azim=-22.0)
-
-
-def _frame_update(index):
-    ax.view_init(6.0 + index * 0.2, -22.0 + index * 0.5)
-
-
-fig = plt.figure(layout="tight")
-ax = fig.add_subplot(111, projection="3d")
-ax.grid(False)
-ax.set_axis_off()
-ani = animation.FuncAnimation(
-    fig,
-    _frame_update,
-    init_func=_init,
-    interval=50,
-    cache_frame_data=False,
-    frames=100,
-)
-
-plt.show()
-
 
 ###############################################################################
 # Generate the plots of the graph.
@@ -217,29 +187,3 @@ def _3d_graph_plot(ax):
 _3d_graph_plot(ax1)
 plt.tight_layout()
 plt.show()
-
-###############################################################################
-# Generate the rotating 3D animation of the graph.
-# ------------------------------------------------
-# The nodes of the graph are marked according to clustering.
-# The graph is rotated in the 3D animation.
-
-
-def _frame_update(index):
-    ax.view_init(100.0 + index * 0.7, -100.0 + index * 0.5)
-
-
-fig = plt.figure(layout="tight")
-ax = fig.add_subplot(111, projection="3d")
-ax.grid(False)
-ax.set_axis_off()
-_3d_graph_plot(ax)
-ani = animation.FuncAnimation(
-    fig,
-    _frame_update,
-    interval=50,
-    cache_frame_data=False,
-    frames=100,
-)
-
-plt.show()


Test patch: