Skip to content

Commit 0a48ab6

Browse files
committed
docs: document design decisions and building
1 parent 11caf6c commit 0a48ab6

File tree

5 files changed

+123
-10
lines changed

5 files changed

+123
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ __pycache__/
55
projects/
66
*/bayesflow.egg-info
77
docsrc/_build/
8+
docsrc/_build_polyversion
9+
docsrc/.bf_doc_gen_venv
810
docsrc/source/api
911
docsrc/source/_examples
1012
docsrc/source/contributing.md

CONTRIBUTING.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,27 @@ z = keras.ops.convert_to_numpy(x)
138138

139139
The documentation uses [sphinx](https://www.sphinx-doc.org/) and relies on [numpy style docstrings](https://numpydoc.readthedocs.io/en/latest/format.html) in classes and functions.
140140

141-
You need to install the following python packages for setting up documentation generation:
141+
Run the following command to install all necessary packages for setting up documentation generation:
142142

143143
```
144-
pip install sphinx numpydoc myst-nb sphinx_design sphinx-book-theme sphinxcontrib-bibtex
144+
pip install .[docs]
145145
```
146146

147-
The overall *structure* of the documentation is manually designed. This also applies to the API documentation. This has two implications for you:
147+
The overall *structure* of the documentation is manually designed, but the API documentation is auto-generated.
148148

149-
1. If you add to existing submodules, the documentation will update automatically (given that you use proper numpy docstrings).
150-
2. If you add a new submodule or subpackage, you need to add a reference to the new module to the appropriate section
151-
of `docsrc/source/api/bayesflow.rst`.
149+
You can re-build the current documentation with
152150

153-
You can re-build the documentation with
151+
```bash
152+
cd docsrc
153+
make clean && make dev
154+
# in case of issues, try `make clean-all`
155+
```
156+
157+
We also provide a multi-version documentation. To generate it, run
154158

155159
```bash
156160
cd docsrc
157161
make clean && make github
158-
# in case of issues, try `make clean-all`
159162
```
160163

161164
The entry point of the rendered documentation will be at `docs/index.html`.

docsrc/Makefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ dev:
2525
@cp .nojekyll ../docs/.nojekyll
2626

2727
github:
28-
@BF_DOCS_SYNCHRONOUS_BUILDS=1 sphinx-polyversion -vv poly.py
28+
@BF_DOCS_SEQUENTIAL_BUILDS=1 sphinx-polyversion poly.py
29+
@echo 'Copying docs to ../docs'
30+
@cp -a _build_polyversion/. ../docs
31+
@cp .nojekyll ../docs/.nojekyll
32+
33+
parallel:
34+
@BF_DOCS_SEQUENTIAL_BUILDS=0 sphinx-polyversion poly.py
2935
@echo 'Copying docs to ../docs'
3036
@cp -a _build_polyversion/. ../docs
3137
@cp .nojekyll ../docs/.nojekyll
@@ -38,6 +44,10 @@ clean:
3844
@rm -f source/installation.rst
3945
@rm -f source/contributing.md
4046

47+
clean-all:
48+
@make clean
49+
@rm -rf docsrc/.bf_doc_gen_venv
50+
4151
.PHONY: help Makefile
4252

4353
# Catch-all target: route all unknown targets to Sphinx using the new

docsrc/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Documentation
2+
3+
## Overview
4+
5+
To install the necessary dependencies, please run `pip install -e .[docs]`.
6+
You can then do the following:
7+
8+
1. `make dev`: Generate the docs for the current version
9+
2. `make github`: Build the docs for tagged versions, `master` and `dev` in a sequential fashion
10+
3. `make parallel`: As `make github`, but builds occur in parallel (see below for details)
11+
12+
The docs will be copied to `../docs`.
13+
14+
## Build process
15+
16+
In this section, the goals and constraints for the build process are described.
17+
18+
Goals:
19+
20+
- (semi-)automated documentation generation
21+
- multi-version documentation
22+
- runnable as a GitHub action
23+
24+
Constraints:
25+
26+
- GitHub actions have limited disk space (14GB)
27+
28+
### Considerations
29+
30+
For building the documentation, we need to install a given BayesFlow
31+
version/branch, its dependencies and the documentation dependencies into
32+
a virtual environment. As the dependencies differ, we cannot share the
33+
environments between versions.
34+
35+
[sphinx-polyversion](https://github.com/real-yfprojects/sphinx-polyversion/) is a compact standalone tool that handles this case in a customizable manner.
36+
37+
### Setup
38+
39+
Please refer to the [sphinx-polyversion documentation](https://real-yfprojects.github.io/sphinx-polyversion/1.0.0/index.html)
40+
for a getting started tutorial and general documentation.
41+
Important locations are the following:
42+
43+
- `poly.py`: Contains the polyversion-specific configuration.
44+
- `pre-build.py`: Build script to move files from other locations to `source`.
45+
Shared between all versions.
46+
- `source/conf.py`: Contains the sphinx-specific configuration. Will be copied
47+
from the currently checked-out branch, and shared between all versions.
48+
This enables a unified look and avoids having to add commits to old versions.
49+
- `polyversion/`: Polyversion-specific files, currently only redirect template.
50+
- `Makefile`/`make.bat`: Define commands to build different configurations.
51+
52+
### Building
53+
54+
For the multi-version docs, there are two ways to build them, which can be
55+
configured by setting the `BF_DOCS_SEQUENTIAL_BUILDS` environment variable.
56+
57+
#### Parallel Builds (Default)
58+
59+
This is the faster, but more resource intensive way. All builds run in parallel,
60+
in different virtual environments which are cached between runs.
61+
Therefore it needs a lot of space (around 20GB), some memory, and the runtime
62+
is determined by the slowest build.
63+
64+
#### Sequential Builds
65+
66+
By setting the environment variable `BF_DOCS_SEQUENTIAL_BUILDS=1`, a
67+
resource-constrained approach is chosen. Builds are sequential, and the
68+
virtual environment is deleted after the build. This overcomes the disk space
69+
limitations in the GitHub actions, at the cost of slightly higher built times
70+
(currently about 30 minutes). The variable can be set in the following way,
71+
which is used in `make github`:
72+
73+
```bash
74+
BF_DOCS_SEQUENTIAL_BUILDS=1 sphinx-polyversion -vv poly.py
75+
```
76+
77+
### Internals
78+
79+
We customize the creation and loading of the virtual environment to have
80+
one environment per revision (`DynamicPip`). We also create a variant that
81+
removes the environment after leaving it (`DestructingDynamicPip`). This
82+
enables freeing disk space in sequential builds.
83+
84+
As only the contents of a revision, but not the `.git` folder is copied
85+
for the build, we have to supply `SETUPTOOLS_SCM_PRETEND_VERSION_FOR_BAYESFLOW`
86+
with a version, otherwise `setuptools-scm` will fail when running
87+
`pip install -e .`. To enable this, we accept and set environment variables
88+
in `DynamicPip`.
89+
90+
The environments are created in `VENV_DIR_NAME`, and only removed if they are
91+
in this directory.
92+
93+
For sequential builds, define the `SynchronousDriver` class, which builds the
94+
revisions sequentially.
95+
96+
For all other details, please refer to `poly.py` and the code of `sphinx-polyversion`.
97+
98+
This text was written by @vpratz, if you have any questions feel free to reach out.

docsrc/poly.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
logger = getLogger(__name__)
2626

2727
#: Whether to build the docs in parallel
28-
PARALLEL_BUILDS = os.environ.get("BF_DOCS_SYNCHRONOUS_BUILDS", "0") != "1"
28+
PARALLEL_BUILDS = os.environ.get("BF_DOCS_SEQUENTIAL_BUILDS", "0") != "1"
2929
print(PARALLEL_BUILDS, "parallel")
3030

3131
#: Determine repository root directory

0 commit comments

Comments
 (0)