scverse / pertpy

Perturbation Analysis in the scverse ecosystem.
https://pertpy.readthedocs.io/en/latest/
MIT License
91 stars 19 forks source link

Name plotting functions last in the methods table of the classes #526

Closed Zethson closed 3 months ago

Zethson commented 4 months ago

Description of feature

The methods tables are ordered alphabetically by default which can result in plotting functions not being named last. They should be named last.

GPT4 suggests:

To ensure that methods with the prefix plot are listed last in the Sphinx documentation for your classes, you can customize the order in which methods are displayed using the autodoc_member_order option in your conf.py file for Sphinx. However, by default, Sphinx only allows ordering members by 'alphabetical', 'groupwise', or 'bysource'. Custom sorting, like prioritizing methods based on their names, requires a more tailored approach.

One strategy involves extending Sphinx with a custom extension that hooks into the process of generating documentation and explicitly reorders the methods. This could involve manipulating the autodoc process to change the order in which methods are documented.

Here's a script approach to create a Sphinx extension that sorts methods so that those with the prefix plot appear last. This script should be included as a Python module in your Sphinx project, and then referenced in the conf.py file.

from sphinx.application import Sphinx
from sphinx.ext.autodoc import MethodDocumenter

def setup(app: Sphinx):
    app.connect('autodoc-process-docstring', sort_plot_methods_last)

def sort_plot_methods_last(app: Sphinx, what: str, name: str, obj, options, lines):
    if what == "class":
        # Assuming obj is a class, get its methods
        methods = [method for method in dir(obj) if callable(getattr(obj, method))]
        # Separate plot methods from others
        plot_methods = [method for method in methods if method.startswith('plot')]
        other_methods = [method for method in methods if not method.startswith('plot')]
        # Sort other methods alphabetically, keeping plot methods at the end
        sorted_methods = sorted(other_methods) + sorted(plot_methods)

        # This part requires more in-depth integration to directly affect the order of documentation
        # For now, this highlights the concept of separating and sorting methods
        # Implementing the reordering in Sphinx documentation output would need deeper integration with Sphinx internals

Then, in your conf.py, after defining other settings, add:

# Add the path to the extension if it's not in the root directory
sys.path.insert(0, os.path.abspath('.')) # Adjust the path accordingly
# Load the custom extension
extensions.append('name_of_your_extension_module')

This script outlines the concept of sorting methods where those starting with plot are listed last. The implementation within the sort_plot_methods_last function is illustrative; integrating this logic into Sphinx's documentation process requires accessing and modifying the internal structure that Sphinx uses to generate method documentation, which is beyond the scope of simple event handlers like autodoc-process-docstring.

A fully integrated solution might involve subclassing existing Sphinx classes or deeply customizing the autodoc extension, which could be complex and require a thorough understanding of Sphinx internals. Always test custom extensions in a separate environment to ensure they work as expected without affecting your main documentation.