Add sphinx docs

This commit is contained in:
Tom Hodson 2022-06-16 17:02:32 +02:00
parent 6961575276
commit 8fd35ba0bd
10 changed files with 336 additions and 1 deletions

1
.gitignore vendored
View File

@ -128,3 +128,4 @@ dmypy.json
# Pyre type checker # Pyre type checker
.pyre/ .pyre/
.DS_Store .DS_Store
_build/

20
code/docs/Makefile Normal file
View File

@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

17
code/docs/_static/custom.css vendored Normal file
View File

@ -0,0 +1,17 @@
/* This code comes from Yevhen Kuzmovych : https://github.com/sphinx-doc/sphinx/issues/1514#issuecomment-742703082 */
/* Newlines (\a) and spaces (\20) before each parameter */
.sig-param::before {
content: "\a\20\20\20\20\20\20\20\20\20\20\20\20\20\20\20\20";
white-space: pre;
}
/* Newline after the last parameter (so the closing bracket is on a new line) */
dt em.sig-param:last-of-type::after {
content: "\a";
white-space: pre;
}
/* To have blue background of width of the block (instead of width of content) */
dl.class > dt:first-of-type {
display: block !important;
}

BIN
code/docs/_static/states.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

64
code/docs/conf.py Normal file
View File

@ -0,0 +1,64 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath("../src/"))
# -- Project information -----------------------------------------------------
project = "MCFF"
copyright = "2022, Thomas Hodson"
author = "Thomas Hodson"
# The full version, including alpha/beta/rc tags
release = "1.0"
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = "classic"
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
# These paths are either relative to html_static_path
# or fully qualified paths (eg. https://...)
html_css_files = [
"custom.css",
]

66
code/docs/index.rst Normal file
View File

@ -0,0 +1,66 @@
Imperial College London ReCoDE : Monte Carlo for Fun
====================================================
This is an exemplar project designed to showcase best practices in developing scientific software as part of the ReCoDE Project at Imperial College London. These docs have been generated automatically with sphinx.
You can find the source code and main landing page for this project on `GitHub <https://github.com/TomHodson/ReCoDE_MCMCFF>`_
There is a `Jupyter notebook <https://github.com/TomHodson/ReCoDE_MCMCFF>`_ detailing how this page was generated in there.
Quickstart
----------
.. code-block:: python
from MCFF.mcmc import mcmc_generator
from MCFF.ising_model import show_state
### Simulation Inputs ###
N = 500 # Use an NxN system
initial_state = np.random.choice(
np.array([-1, 1], dtype=np.int8), size=(N, N)
) # the intial state to use
### Simulation Code ###
critical_states = [
s for s in mcmc_generator(initial_state, steps=5, stepsize=5*N**2, T= 3.5)
]
fig, axes = plt.subplots(
ncols=len(critical_states), figsize=(5 * len(critical_states), 5)
)
for s, ax in zip(critical_states, axes):
show_state(s, ax=ax)
.. image:: _static/states.png
:width: 100%
:alt: 5 grids showing black and white Ising Model states
.. toctree::
:maxdepth: 2
:caption: Contents:
mcmc : Markov Chain Monte Carlo Routines
---------------------------------------------
.. automodule:: MCFF.mcmc
:members:
ising_model : Ising Model Routines
---------------------------------------------
.. automodule:: MCFF.ising_model
:members:
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

35
code/docs/make.bat Normal file
View File

@ -0,0 +1,35 @@
@ECHO OFF
pushd %~dp0
REM Command file for Sphinx documentation
if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build
%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)
if "%1" == "" goto help
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end
:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
:end
popd

View File

View File

@ -67,7 +67,7 @@ def mcmc_generator(
energy_difference : function(state, site), default = ising_model.energy_difference energy_difference : function(state, site), default = ising_model.energy_difference
A function that gives the energy change from flippings `site = (i, j)`of `state`. A function that gives the energy change from flippings `site = (i, j)`of `state`.
Returns Yields
------- -------
np.array np.array
The final state. The final state.

View File

@ -0,0 +1,132 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d3df99d5-0fd1-4912-aef2-26fc189917ec",
"metadata": {},
"source": [
"<h1 align=\"center\">Markov Chain Monte Carlo for fun and profit</h1>\n",
"<h1 align=\"center\"> 🎲 ⛓️ 👉 🧪 </h1>\n",
"\n",
"# Adding Documentation \n",
"\n",
"[Intro to basic Rst](https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html)\n",
"\n",
"[Getting started with Sphinx](https://www.sphinx-doc.org/en/master/usage/quickstart.html)"
]
},
{
"cell_type": "markdown",
"id": "e2ecf10d-0025-4ce0-938a-807dbf0b6d1f",
"metadata": {},
"source": [
"Make a folder called `docs/` inside `code/`, cd into it and run sphinx\n",
"```sh\n",
"% mkdir docs\n",
"% cd docs\n",
"% sphinx-quickstart #just go with the defaults\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "7da17bd4-4072-4dd8-9680-dfd2a982de6f",
"metadata": {},
"source": [
"To tell sphinx where to find our code we add some lines to conf.py:\n",
"```python\n",
"import os\n",
"import sys\n",
"sys.path.insert(0, os.path.abspath('../src'))\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "64d55530-c21a-4bd3-b25a-9c7109541038",
"metadata": {},
"source": [
"We also add two extensions:\n",
"```python\n",
"extensions = [\n",
" 'sphinx.ext.autodoc',\n",
" 'sphinx.ext.napoleon',\n",
"]\n",
"```\n",
"\n",
"Autodoc allows us to generate documentation automatically from the docstrings in our source code, while [napoleon][napoleon] allows us to use [NUMPYDOC][numpydoc] and Google formats for the docstrings in addition to [reStructuredText][rst]\n",
"\n",
"[napoleon]: https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html\n",
"[NUMPYDOC]: https://numpydoc.readthedocs.io/en/latest/format.html\n",
"[googledoc]: https://google.github.io/styleguide/pyguide.html\n",
"[rst]: https://docutils.sourceforge.io/rst.html"
]
},
{
"cell_type": "markdown",
"id": "6d2d4a0f-adf2-4cc7-b220-8243635658d5",
"metadata": {},
"source": [
"Numpy style docstrings look like this:\n",
"```python\n",
"def func(arg1, arg2):\n",
" \"\"\"Summary line.\n",
"\n",
" Extended description of function.\n",
"\n",
" Parameters\n",
" ----------\n",
" arg1 : int\n",
" Description of arg1\n",
" arg2 : str\n",
" Description of arg2\n",
"\n",
" Returns\n",
" -------\n",
" bool\n",
" Description of return value\n",
"\n",
" \"\"\"\n",
" return True\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "167d08ec-4c55-4050-929c-0ca5d4c03fd6",
"metadata": {},
"source": [
"## Making the function declarations a bit nicer\n",
"Longer function names in the generated documentation currently generate with no line break, I found a fix for that buried [inside a bug report on sphinx](https://github.com/sphinx-doc/sphinx/issues/1514#issuecomment-742703082) \n",
"\n",
"It involves adding some custom css and an extra line to `conf.py`:\n",
"```python\n",
"html_css_files = [\n",
" 'custom.css',\n",
"]\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:recode]",
"language": "python",
"name": "conda-env-recode-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}