From 7c49e9d8a72e04a8a2740663aa2b92cd9aef3d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Louf?= Date: Wed, 25 Jan 2023 13:43:16 +0100 Subject: [PATCH] Dynamically generate RVTransform list in docs --- docs/source/api/transforms.rst | 27 ++--------------------- docs/source/conf.py | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/docs/source/api/transforms.rst b/docs/source/api/transforms.rst index 5dc9dad2..e0a4ff1a 100644 --- a/docs/source/api/transforms.rst +++ b/docs/source/api/transforms.rst @@ -56,37 +56,14 @@ associated variables and their values: .. autoclass:: aeppl.transforms.TransformValuesRewrite + --------------------- Invertible transforms --------------------- AePPL currently supports transforms using the following (invertible) Aesara operators. This means that AePPL can compute the log-probability of a random variable that is the result of one of the following transformations applied to another random variable: -- `aesara.tensor.add` -- `aesara.tensor.sub` -- `aesara.tensor.mul` -- `aesara.tensor.true_div` -- `aesara.tensor.exponential` -- `aesara.tensor.exp` -- `aesara.tensor.log` - -One can also apply the following transforms directly: - -.. autoclass:: aeppl.transforms.LocTransform -.. autoclass:: aeppl.transforms.ScaleTransform -.. autoclass:: aeppl.transforms.LogTransform -.. autoclass:: aeppl.transforms.ExpTransform -.. autoclass:: aeppl.transforms.ReciprocalTransform -.. autoclass:: aeppl.transforms.IntervalTransform -.. autoclass:: aeppl.transforms.LogOddsTransform -.. autoclass:: aeppl.transforms.SimplexTransform -.. autoclass:: aeppl.transforms.CircularTransform - -These transformations can be chained using: - - -.. autoclass:: aeppl.transforms.ChainedTransform - +.. print-invertible-transforms:: --------- Censoring diff --git a/docs/source/conf.py b/docs/source/conf.py index 630a770f..6b1442cc 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -3,6 +3,9 @@ import sphinx.addnodes import sphinx.directives from docutils import nodes +from docutils.frontend import OptionParser +from docutils.utils import new_document +from sphinx.parsers import RSTParser from sphinx.util.docutils import SphinxDirective import aeppl @@ -110,5 +113,41 @@ def run(self): return [res] +class InvertibleTransformationsDirective(SphinxDirective): + def run(self): + import inspect + import sys + + import aeppl.transforms as transforms + + invertible_transforms = ( + mname + for mname, mtype in inspect.getmembers(sys.modules["aeppl.transforms"]) + if inspect.isclass(mtype) + and issubclass(mtype, transforms.RVTransform) + and not mtype == transforms.RVTransform + ) + + rst = ".. autosummary::\n" + rst += " :toctree: _generated\n\n" + for transform_name in invertible_transforms: + rst += f" aeppl.transforms.{transform_name}\n" + + return self.parse_rst(rst) + + def parse_rst(self, text: str): + parser = RSTParser() + parser.set_application(self.env.app) + + settings = OptionParser( + defaults=self.env.settings, + components=(RSTParser,), + read_config_files=True, + ).get_default_values() + document = new_document("", settings=settings) + parser.parse(text, document) + return document.children + def setup(app): app.add_directive("print-supported-dists", SupportedDistributionsDirective) + app.add_directive("print-invertible-transforms", InvertibleTransformationsDirective)