Skip to content

Built API docs from the docstrings using Sphinx+autodocs#329

Merged
IgorTatarnikov merged 61 commits intobrainglobe:mainfrom
thisisrick25:api_docstr
Aug 27, 2025
Merged

Built API docs from the docstrings using Sphinx+autodocs#329
IgorTatarnikov merged 61 commits intobrainglobe:mainfrom
thisisrick25:api_docstr

Conversation

@thisisrick25
Copy link
Copy Markdown
Contributor

Introduce a script to fetch external repositories for the Brainglobe project, update Sphinx configuration for improved documentation paths and extensions, and enhance the API documentation for the brainglobe-atlasapi module. Include setup instructions for Python and external packages in the Sphinx build workflow.

closes #4

@thisisrick25
Copy link
Copy Markdown
Contributor Author

@adamltyson I have added docs only for brainglobe-atlasapi, for now.
Sphinx cannot directly import and document code from remote GitHub repositories. It requires the actual Python packages/source code to be present locally so it can import them and extract docstrings.
I wrote fetch_external.py, which will clone and install the BG repos and added it to the workflow.

@adamltyson
Copy link
Copy Markdown
Member

Thanks @thisisrick25. Do you need any feedback at this stage? Or shall we wait until all the repos are included in this PR?

@thisisrick25
Copy link
Copy Markdown
Contributor Author

thisisrick25 commented May 8, 2025

I am facing a problem @adamltyson. I can generate the docs in my local environment, but when I implement them in GA workflow, neuroinformatics-unit/actions/build_sphinx_docs interferes with my additional action setup. The action performs its own checkout potentially into a clean state/overwriting the workspace, not preserving the external dir.

One way I have yet to try is to write a completely different workflow action independent of neuroinformatics-unit/actions/build_sphinx_docs.

@adamltyson
Copy link
Copy Markdown
Member

That action is maintained by us (https://github.com/neuroinformatics-unit/actions/tree/main/build_sphinx_docs), so if you can work out what needs to be changed, you're welcome to submit a PR.

@thisisrick25
Copy link
Copy Markdown
Contributor Author

thisisrick25 commented May 9, 2025

I will open a new issue @neuroinformatics-unit/actions

edit: issue neuroinformatics-unit/actions#83

@thisisrick25
Copy link
Copy Markdown
Contributor Author

thisisrick25 commented May 14, 2025

@adamltyson I want to point out that the docstring syntax is inconsistent in brainglobe-atlasapi (have not checked in other repos). I got this warning locally

image
which also throws an error in GA because of this -W flag (see logs here)

I checked by fixing the syntax and can confirm it works as expected.

Do we need the -W flag to build Sphinx doc for the API workflow? My conscience says yes, but it will take some time to fix the docstrings.

I'm not very familiar with how pre-commit works, but I think it might be possible to create a hook with the numpydoc repo to check the docstring for numpydoc validation. This would be helpful in the future.

@alessandrofelder
Copy link
Copy Markdown
Member

alessandrofelder commented May 19, 2025

Thanks @thisisrick25 - you make some good points here.

I want to point out that the docstring syntax is inconsistent in brainglobe-atlasapi (have not checked in other repos).

Indeed, this is expected (a consequence of BrainGlobe's "organic" growth) and would be nice to fix.

Do we need the -W flag to build Sphinx doc for the API workflow? My conscience says yes, but it will take some time to fix the docstrings.

I agree.

I'm not very familiar with how pre-commit works, but I think it might be possible to create a hook with the numpydoc repo to check the docstring for numpydoc validation. This would be helpful in the future.

I don't know either, but agree it should be possible, and would be helpful.

To help keep things tidy and manageable, I would tackle each point in a separate PR, in the following order

  1. a PR that only fixes docstrings
  2. this PR (and make sure things work)
  3. a pre-commit PR (if possible)

Does that make sense, and would you have capacity to try (for this repo only)?

@thisisrick25
Copy link
Copy Markdown
Contributor Author

thisisrick25 commented May 19, 2025

@alessandrofelder Yes I will be happy to help.

@thisisrick25
Copy link
Copy Markdown
Contributor Author

I want to address a few more things:

  1. What should be the URL path to the API reference? i.e where should the api dir be located? Outside or embedded in the documentation dir? (Current config is outside)

    If outside, clicking on the API reference link will change the URL path from documentation/brainglobe-atlasapi/index.html to api/brainglobe_atlasapi.html. If embedded, then documentation/brainglobe-atlasapi/api/brainglobe_atlasapi.html.

    My recommendation would be to embed the API reference within the brainglobe-atlasapi documentation structure.

  2. I think we should be able to run the Sphinx makefile/bash script from the root directory instead of changing to the docs folder first

@IgorTatarnikov
Copy link
Copy Markdown
Member

  1. I think the embedded URL has a better structure
  2. I think we keep the make as is, https://github.com/neuroinformatics-unit/actions/blob/94f7334d4334c921e672a7ea48674f57ed7d0893/build_sphinx_docs/action.yml#L84C1-L85C29 the build action runs make html from inside the docs directory.

- Script now iterates through api.rst files nested within tool documentation directories.
- This aligns the generation process with the new documentation structure.
- Simplifies the generated toctree path to a relative api reference.
- Improves robustness by checking for the "API Reference" heading instead of a specific toctree path.
- Removes the dependency on a separate source/api directory.
- The make_api_index.py script now generates API documentation into package-specific subdirectories.
- Previously, all API documentation files were placed directly under source/api/.
- This change organizes the generated .rst files into source/documentation/<package_name>/api.rst.
- Improves the logical grouping and discoverability of API documentation for each package.
- Update .gitignore entries for Sphinx-generated API documentation.
- Adjust paths to match the current output structure of the documentation build.
- Ensure that generated API reference files are properly excluded from version control.
- Prevents committing transient build artifacts.
- Aligns with recent changes in the documentation generation process.
- The script now removes api.rst files and their corresponding api/ directories.
- It updates the regex pattern for removing API toctree entries in index.md files.
- This ensures a more comprehensive cleanup of generated API documentation artifacts.
- Prevents stale or orphaned API documentation files from remaining after builds.
- Add comprehensive docstrings to fetch_repos.py for better script overview.
- Improve inline comments across all API documentation generation scripts.
- Enhance readability and maintainability of the documentation build process.
- Provide clearer context for the logic involved in API documentation generation.
- Correct a logging message in remove_api_toctrees.py for non-directory paths.
- Added detailed print statements to all documentation generation scripts.
- Provides clearer feedback and progress indicators during the documentation build process.
- Improves user experience by showing current steps and aiding in debugging.
- Includes counts of processed repositories, API files, and packages.
- Removed trailing newlines from several script files for consistency.
- Removed redundant if tool_dir.is_dir(): check from the main loop.
- The tool_dirs list is already guaranteed to contain only directories.
- Streamlines the script's logic and improves readability.
- Eliminates an unused else branch for non-directory entries.
@thisisrick25
Copy link
Copy Markdown
Contributor Author

thisisrick25 commented Aug 10, 2025

  1. I think the embedded URL has a better structure

@IgorTatarnikov I have fixed the file structure, added some extra comments and print statements for better verbosity.

Are any of them in a decent state to go online other than the atlas API once brainglobe/brainglobe-atlasapi#584 is merged?

@adamltyson Unfortunately, no other repo other than brainglobe/brainglobe-atlasapi PR #584 has a correct docstring format.

Otherwise, this PR and brainglobe/brainglobe-atlasapi#584 are good to go.

Copy link
Copy Markdown
Member

@IgorTatarnikov IgorTatarnikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Now that brainglobe/brainglobe-atlasapi#584 is merged we should be good to merge this once the list of packages is restricted.

Comment thread docs/fetch_repos.py
- restrict this list to just brainglobe-atlasapi
@thisisrick25
Copy link
Copy Markdown
Contributor Author

Only brainglobe-atlasapi will be fetched, for now.

@IgorTatarnikov
Copy link
Copy Markdown
Member

Perfect, to avoid trying to resolve an environment where all packages required for atlas packing are present it might be best to not generate API docs for the atlas_generation scripts. I'll track this in brainglobe/brainglobe-atlasapi#624, once this is resolved, this PR can be merged as well.

@IgorTatarnikov
Copy link
Copy Markdown
Member

I've done some final local testing, now that brainglobe/brainglobe-atlasapi#624 is merged, we need to add atlas_scripts to the excluded directories in make_api_index.py.

Also, I'm seeing quite a few warnings thrown specifically for Class docstrings:

/path/to/repo/brainglobe.github.io/docs/downloads/brainglobe-atlasapi/brainglobe_atlasapi/bg_atlas.py:docstring of brainglobe_atlasapi.bg_atlas.BrainGlobeAtlas:79: WARNING: autosummary: stub file not found 'brainglobe_atlasapi.bg_atlas.BrainGlobeAtlas.check_latest_version'. Check your autosummary_generate setting.
image

I'm also seeing a duplicate method section in the class docs:

image

I was able to fix both of these issues by adding numpydoc_show_class_members = False to the conf.py file, but I'm not sure that's the best/only solution.

@IgorTatarnikov
Copy link
Copy Markdown
Member

Also, some of the mesh_generation functions remain undocumented due to failed imports. Perhaps we could install the dev extras for each package, and ensure that the dev extras for each package contain all versions of the install that need to be documented.

E.g. in brainglobe-atlasapi we could change pyproject.toml so that when dev is install atlasgen is also fetched:

dev = [
    "black",
    "check-manifest",
    "coverage",
    "mypy",
    "pre-commit",
    "pytest-cov",
    "pytest-mock",
    "pytest",
    "ruff",
    "setuptools_scm",
    "tox",
	"brainglobe-atlasapi["atlasgen"],
]

@thisisrick25 thisisrick25 marked this pull request as draft August 21, 2025 08:15
- Added a new attributes block to the class.rst autosummary template.
- This block automatically lists public attributes of classes.
- Attributes not starting with an underscore (_) are included.
- Improves the completeness and discoverability of API documentation for classes.
@thisisrick25
Copy link
Copy Markdown
Contributor Author

thisisrick25 commented Aug 21, 2025

I was able to fix both of these issues by adding numpydoc_show_class_members = False to the conf.py file, but I'm not sure that's the best/only solution.

Turns out these warnings happened because both sphinx.ext.napoleon and numpydoc Sphinx extensions are used.
Removing numpydoc extensions removes the class warnings.

I also added a new attributes section in the class template because numpydoc was responsible for it.

@thisisrick25 thisisrick25 marked this pull request as ready for review August 21, 2025 19:25
@thisisrick25
Copy link
Copy Markdown
Contributor Author

Also, some of the mesh_generation functions remain undocumented due to failed imports. Perhaps we could install the dev extras for each package, and ensure that the dev extras for each package contain all versions of the install that need to be documented.

This should be addressed in a new issue at brainglobe-atlasapi.

Copy link
Copy Markdown
Member

@IgorTatarnikov IgorTatarnikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good on my end, I've tested it again with a local build. LGTM 🎉

@IgorTatarnikov IgorTatarnikov merged commit 0a7d856 into brainglobe:main Aug 27, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Work out how to host docs for individual tools

5 participants