From eb8c12a938c97dc2d0511cf182366bb899a96531 Mon Sep 17 00:00:00 2001 From: Nabil Date: Fri, 29 Mar 2024 18:57:14 +0600 Subject: [PATCH 1/4] Fix #25032 for plot_types --- doc/sphinxext/gallery_order.py | 68 ++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/doc/sphinxext/gallery_order.py b/doc/sphinxext/gallery_order.py index 70a018750537..1cecd186760c 100644 --- a/doc/sphinxext/gallery_order.py +++ b/doc/sphinxext/gallery_order.py @@ -4,12 +4,36 @@ """ from sphinx_gallery.sorting import ExplicitOrder +import os + + +# Utility functions +def get_ordering(dir): + """Read gallery_order.txt in dir and return content of the file as a List""" + file_path = os.path.join(dir, 'gallery_order.txt') + f = open(file_path, "r") + lines = [line.replace('\n', '') for line in f.readlines()] + ordered_list = [] + for line in lines: + if line == "unsorted": + ordered_list.append(UNSORTED) + else: + ordered_list.append(line) + + return ordered_list + + +def list_directory(parent_dir): + """Return list of sub directories at a directory""" + root, dirs, files = next(os.walk(parent_dir)) + return [os.path.join(root, dir) for dir in dirs] # Gallery sections shall be displayed in the following order. # Non-matching sections are inserted at the unsorted position UNSORTED = "unsorted" + examples_order = [ '../galleries/examples/lines_bars_and_markers', '../galleries/examples/images_contours_and_fields', @@ -36,17 +60,20 @@ '../galleries/tutorials/provisional' ] -plot_types_order = [ - '../galleries/plot_types/basic', - '../galleries/plot_types/stats', - '../galleries/plot_types/arrays', - '../galleries/plot_types/unstructured', - '../galleries/plot_types/3D', - UNSORTED -] +# plot_types_order = [ +# '../galleries/plot_types/basic', +# '../galleries/plot_types/stats', +# '../galleries/plot_types/arrays', +# '../galleries/plot_types/unstructured', +# '../galleries/plot_types/3D', +# UNSORTED +# ] -folder_lists = [examples_order, tutorials_order, plot_types_order] +plot_types_directory = "../galleries/plot_types/" +plot_types_order = get_ordering(plot_types_directory) + +folder_lists = [examples_order, tutorials_order, plot_types_order] explicit_order_folders = [fd for folders in folder_lists for fd in folders[:folders.index(UNSORTED)]] explicit_order_folders.append(UNSORTED) @@ -89,19 +116,30 @@ def __call__(self, item): # **Plot Types # Basic - "plot", "scatter_plot", "bar", "stem", "step", "fill_between", + # "plot", "scatter_plot", "bar", "stem", "step", "fill_between", # Arrays - "imshow", "pcolormesh", "contour", "contourf", - "barbs", "quiver", "streamplot", + # "imshow", "pcolormesh", "contour", "contourf", + # "barbs", "quiver", "streamplot", # Stats - "hist_plot", "boxplot_plot", "errorbar_plot", "violin", - "eventplot", "hist2d", "hexbin", "pie", + # "hist_plot", "boxplot_plot", "errorbar_plot", "violin", + # "eventplot", "hist2d", "hexbin", "pie", # Unstructured - "tricontour", "tricontourf", "tripcolor", "triplot", + # "tricontour", "tricontourf", "tripcolor", "triplot", # Spines "spines", "spine_placement_demo", "spines_dropped", "multiple_yaxis_with_spines", "centered_spines_with_arrows", ] + + +for dir in list_directory(plot_types_directory): + try: + ordered_subdirs = get_ordering(dir) + list_all.extend(ordered_subdirs) + except FileNotFoundError: + # Fallback to ordering already defined in list_all + pass + + explicit_subsection_order = [item + ".py" for item in list_all] From 509c5bf0e7c97652a9b525ab5a225e563fba18a8 Mon Sep 17 00:00:00 2001 From: Nabil Date: Fri, 29 Mar 2024 21:49:22 +0600 Subject: [PATCH 2/4] Add gallery_order.txt files for plot_types --- galleries/plot_types/arrays/gallery_order.txt | 6 ++++++ galleries/plot_types/basic/gallery_order.txt | 6 ++++++ galleries/plot_types/gallery_order.txt | 6 ++++++ galleries/plot_types/stats/gallery_order.txt | 8 ++++++++ galleries/plot_types/unstructured/gallery_order.txt | 4 ++++ 5 files changed, 30 insertions(+) create mode 100644 galleries/plot_types/arrays/gallery_order.txt create mode 100644 galleries/plot_types/basic/gallery_order.txt create mode 100644 galleries/plot_types/gallery_order.txt create mode 100644 galleries/plot_types/stats/gallery_order.txt create mode 100644 galleries/plot_types/unstructured/gallery_order.txt diff --git a/galleries/plot_types/arrays/gallery_order.txt b/galleries/plot_types/arrays/gallery_order.txt new file mode 100644 index 000000000000..9a495653a0b9 --- /dev/null +++ b/galleries/plot_types/arrays/gallery_order.txt @@ -0,0 +1,6 @@ +imshow +pcolormesh +contourcontourf +barbs +quiver +streamplot \ No newline at end of file diff --git a/galleries/plot_types/basic/gallery_order.txt b/galleries/plot_types/basic/gallery_order.txt new file mode 100644 index 000000000000..15d7b778695e --- /dev/null +++ b/galleries/plot_types/basic/gallery_order.txt @@ -0,0 +1,6 @@ +plot +scatter_plot +bar +stem +step +fill_between \ No newline at end of file diff --git a/galleries/plot_types/gallery_order.txt b/galleries/plot_types/gallery_order.txt new file mode 100644 index 000000000000..1941d38d4b12 --- /dev/null +++ b/galleries/plot_types/gallery_order.txt @@ -0,0 +1,6 @@ +basic +stats +arrays +unstructured +3D +unsorted \ No newline at end of file diff --git a/galleries/plot_types/stats/gallery_order.txt b/galleries/plot_types/stats/gallery_order.txt new file mode 100644 index 000000000000..93f40cc690c8 --- /dev/null +++ b/galleries/plot_types/stats/gallery_order.txt @@ -0,0 +1,8 @@ +hist_plot +boxplot_plot +errorbar_plot +violin +eventplot +hist2d +hexbin +pie \ No newline at end of file diff --git a/galleries/plot_types/unstructured/gallery_order.txt b/galleries/plot_types/unstructured/gallery_order.txt new file mode 100644 index 000000000000..0e170ba29d12 --- /dev/null +++ b/galleries/plot_types/unstructured/gallery_order.txt @@ -0,0 +1,4 @@ +tricontour +tricontourf +tripcolor +triplot \ No newline at end of file From 3c031db76db6d495d9f5921774048a4d09d78999 Mon Sep 17 00:00:00 2001 From: Nabil Date: Fri, 29 Mar 2024 22:04:23 +0600 Subject: [PATCH 3/4] Fix bug: plot_types subdirectory ordering logic was faulty --- doc/sphinxext/gallery_order.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/doc/sphinxext/gallery_order.py b/doc/sphinxext/gallery_order.py index 1cecd186760c..ebbabca3cb71 100644 --- a/doc/sphinxext/gallery_order.py +++ b/doc/sphinxext/gallery_order.py @@ -8,7 +8,7 @@ # Utility functions -def get_ordering(dir): +def get_ordering(dir, include_directory_path=False): """Read gallery_order.txt in dir and return content of the file as a List""" file_path = os.path.join(dir, 'gallery_order.txt') f = open(file_path, "r") @@ -18,7 +18,10 @@ def get_ordering(dir): if line == "unsorted": ordered_list.append(UNSORTED) else: - ordered_list.append(line) + if include_directory_path: + ordered_list.append(os.path.join(dir, line)) + else: + ordered_list.append(line) return ordered_list @@ -69,9 +72,9 @@ def list_directory(parent_dir): # UNSORTED # ] -plot_types_directory = "../galleries/plot_types/" -plot_types_order = get_ordering(plot_types_directory) +plot_types_directory = "../galleries/plot_types/" +plot_types_order = get_ordering(plot_types_directory, include_directory_path=True) folder_lists = [examples_order, tutorials_order, plot_types_order] explicit_order_folders = [fd for folders in folder_lists @@ -133,7 +136,7 @@ def __call__(self, item): for dir in list_directory(plot_types_directory): try: - ordered_subdirs = get_ordering(dir) + ordered_subdirs = get_ordering(dir, include_directory_path=False) list_all.extend(ordered_subdirs) except FileNotFoundError: # Fallback to ordering already defined in list_all From 8201f0e0c7ad80c19eea5fddadb74d8c6187a002 Mon Sep 17 00:00:00 2001 From: Nabil Date: Sat, 30 Mar 2024 03:09:46 +0600 Subject: [PATCH 4/4] Add gallery_order.txt files for examples folder and tidy up gallery_order.py --- doc/sphinxext/gallery_order.py | 77 ++----------------- galleries/examples/color/gallery_order.txt | 2 + galleries/examples/gallery_order.txt | 15 ++++ .../pie_and_polar_charts/gallery_order.txt | 2 + galleries/examples/spines/gallery_order.txt | 5 ++ 5 files changed, 31 insertions(+), 70 deletions(-) create mode 100644 galleries/examples/color/gallery_order.txt create mode 100644 galleries/examples/gallery_order.txt create mode 100644 galleries/examples/pie_and_polar_charts/gallery_order.txt create mode 100644 galleries/examples/spines/gallery_order.txt diff --git a/doc/sphinxext/gallery_order.py b/doc/sphinxext/gallery_order.py index ebbabca3cb71..a57c87675d4e 100644 --- a/doc/sphinxext/gallery_order.py +++ b/doc/sphinxext/gallery_order.py @@ -37,46 +37,13 @@ def list_directory(parent_dir): UNSORTED = "unsorted" -examples_order = [ - '../galleries/examples/lines_bars_and_markers', - '../galleries/examples/images_contours_and_fields', - '../galleries/examples/subplots_axes_and_figures', - '../galleries/examples/statistics', - '../galleries/examples/pie_and_polar_charts', - '../galleries/examples/text_labels_and_annotations', - '../galleries/examples/color', - '../galleries/examples/shapes_and_collections', - '../galleries/examples/style_sheets', - '../galleries/examples/pyplots', - '../galleries/examples/axes_grid1', - '../galleries/examples/axisartist', - '../galleries/examples/showcase', - UNSORTED, - '../galleries/examples/userdemo', -] - -tutorials_order = [ - '../galleries/tutorials/introductory', - '../galleries/tutorials/intermediate', - '../galleries/tutorials/advanced', - UNSORTED, - '../galleries/tutorials/provisional' -] - -# plot_types_order = [ -# '../galleries/plot_types/basic', -# '../galleries/plot_types/stats', -# '../galleries/plot_types/arrays', -# '../galleries/plot_types/unstructured', -# '../galleries/plot_types/3D', -# UNSORTED -# ] - - plot_types_directory = "../galleries/plot_types/" plot_types_order = get_ordering(plot_types_directory, include_directory_path=True) -folder_lists = [examples_order, tutorials_order, plot_types_order] +examples_directory = "../galleries/examples/" +examples_order = get_ordering(examples_directory, include_directory_path=True) + +folder_lists = [examples_order, plot_types_order] explicit_order_folders = [fd for folders in folder_lists for fd in folders[:folders.index(UNSORTED)]] explicit_order_folders.append(UNSORTED) @@ -99,39 +66,9 @@ def __call__(self, item): # Examples/tutorials that do not appear in a list will be appended. list_all = [ - # **Tutorials** - # introductory - "quick_start", "pyplot", "images", "lifecycle", "customizing", - # intermediate - "artists", "legend_guide", "color_cycle", - "constrainedlayout_guide", "tight_layout_guide", - # advanced - # text - "text_intro", "text_props", - # colors - "colors", - - # **Examples** - # color - "color_demo", - # pies - "pie_features", "pie_demo2", - - # **Plot Types - # Basic - # "plot", "scatter_plot", "bar", "stem", "step", "fill_between", - # Arrays - # "imshow", "pcolormesh", "contour", "contourf", - # "barbs", "quiver", "streamplot", - # Stats - # "hist_plot", "boxplot_plot", "errorbar_plot", "violin", - # "eventplot", "hist2d", "hexbin", "pie", - # Unstructured - # "tricontour", "tricontourf", "tripcolor", "triplot", - # Spines - "spines", "spine_placement_demo", "spines_dropped", - "multiple_yaxis_with_spines", "centered_spines_with_arrows", - ] + # folders that don't contain gallery_order.txt file can + # list their file orderings here +] for dir in list_directory(plot_types_directory): diff --git a/galleries/examples/color/gallery_order.txt b/galleries/examples/color/gallery_order.txt new file mode 100644 index 000000000000..75ad290be1f2 --- /dev/null +++ b/galleries/examples/color/gallery_order.txt @@ -0,0 +1,2 @@ +color_demo +unsorted \ No newline at end of file diff --git a/galleries/examples/gallery_order.txt b/galleries/examples/gallery_order.txt new file mode 100644 index 000000000000..d2079c0c24b2 --- /dev/null +++ b/galleries/examples/gallery_order.txt @@ -0,0 +1,15 @@ +lines_bars_and_markers +images_contours_and_fields +subplots_axes_and_figures +statistics +pie_and_polar_charts +text_labels_and_annotations +color +shapes_and_collections +style_sheets +pyplots +axes_grid1 +axisartist +showcase +unsorted +userdemo \ No newline at end of file diff --git a/galleries/examples/pie_and_polar_charts/gallery_order.txt b/galleries/examples/pie_and_polar_charts/gallery_order.txt new file mode 100644 index 000000000000..5372ad562536 --- /dev/null +++ b/galleries/examples/pie_and_polar_charts/gallery_order.txt @@ -0,0 +1,2 @@ +pie_features +unsorted \ No newline at end of file diff --git a/galleries/examples/spines/gallery_order.txt b/galleries/examples/spines/gallery_order.txt new file mode 100644 index 000000000000..c48bfbb9f4df --- /dev/null +++ b/galleries/examples/spines/gallery_order.txt @@ -0,0 +1,5 @@ +spines +spine_placement_demo +spines_dropped +multiple_yaxis_with_spines +centered_spines_with_arrows \ No newline at end of file