This section deals with the sphinx documentation included in the library repository and built on top the Python interface.
This documentation is powered by the Sphinx documentation system. Hence, reading Sphinx’s own documentation might be a good idea for starters! You will also need to learn some basics about its main featured language: reStructuredText.
The gen_gallery extension from Sphinx-Gallery is used to generate pages from the examples directory.
Docstrings (in separate SWIG header files)¶
OpenTURNS main featured language is C++. We then use SWIG
in order to generate the Python interface. Hence, docstrings are defined
within dedicated SWIG header files (
and are then included in the main SWIG header files
For instance, the docstrings for the
are defined in
Arcsine_doc.i.in, and this docstring file is then
Arcsine.i using a %include Arcsine_doc.i.
Note the difference between the name of the docstring file in the source
Arcsine_doc.i.in) and its reference in
.in suffix disappeared because the docstring files are
preprocessed by CMake in order to escape LaTeX backslashes for SWIG and
Note also that the use of double quotes (”) in docstrings is forbidden. This is because SWIG uses them to delimit the docstrings.
Here are a few recommendations you’d better read in order to help us enhancing the docstrings coverage.
Please follow PEP257 and
guidelines for writing the docstrings as well as PEP8
recommendations for the Examples section (for instance, please don’t
from openturns import *, indent with 4 spaces, etc. …).
Using maths is highly recommended for illustrating the mathematical concepts
featured in OpenTURNS. Mathematical expression must use Sphinx
roles for inline maths, and
.. math:: directives for equations. These
equations will appear as plain LaTeX at prompt (using the
help command in
Python or the
? suffix in IPython) but Sphinx will render them as PNG images
in the User Manual.
Please use the math commands defined in our
Docstrings & inheritance¶
Good news! Docstrings are inherited so that we only need to document the methods of the parent objects (until we want to make them more specific).
An important number of objects use the bridge pattern.
For instance, the
object which is the interface class for all probability distributions
has an implementation class DistributionImplementation (that we don’t
need to expose). And the trick is that the interface class does not inherit from
its implementation object but the children do, so we need to
document them both.
In order to avoid docstrings duplicates though we decided to document the implementation class with defined blocks. Since we load the implementation first, we can then refer to the same defined blocks for documenting the object itself.
For instance the main docstring of the
object is defined and referred to in the
SWIG header file:
"Base class for probability distributions."
and it is then only being referred to in the
SWIG header file:
Integration to the build system¶
The separate docstring SWIG header files are included in the SWIG header files of the openturns repos, so this does not need any further integration steps (out of the backslashes escaper CMake script). A docstring test (python/test/t_docstring.py) has been added to the Python tests.
We added the following CMake variables:
Path to the sphinx-build command.
This is passed as the options of the sphinx-build command (see sphinx-build invocation).
All these targets depend on the rst files located in the sources
Example pages are generated from Python scripts in the examples directory
Each page allows one to display notebook-like code or text cells. Each cell is delimited by a specific marker and the text cells are written in rst format, for example:
# First cell (text) in *rst* format inside comments
# Some formula :math:`\lambda = 4`
import openturns as ot
print("Second cell (code)")
dist = ot.Normal()
Output cells are rendered at compilation time, so the script must not be too long to run.
Figures are automatically generated from the matplotlib handles, for example using the openturns.viewer module:
from openturns.viewer import View
graph = dist.drawPDF()
view = View(graph)
Note that sphinx runs the examples in the same Python process so you might want to reset the random generator at the beginning of your example for consistent results:
import openturns as ot
For the same reason you might want to reset the ResourceMap default values at the end if your example sets specific settings:
# Reset default settings
The special variable sphinx_gallery_thumbnail_number can be used to determine which figure is used as thumbnail in the examples gallery:
# sphinx_gallery_thumbnail_number = 3