Skip to content

Commit

Permalink
Adding documentation for ArviZ support (#146)
Browse files Browse the repository at this point in the history
* refactoring docs

* rtds version

* invalid key

* adding tutorial for arviz
  • Loading branch information
dfm committed Mar 7, 2021
1 parent b224f0c commit 21647ea
Show file tree
Hide file tree
Showing 26 changed files with 443 additions and 832 deletions.
6 changes: 5 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ repos:
hooks:
- id: isort
additional_dependencies: [toml]
exclude: docs/tutorials
exclude: docs/pages
- repo: https://github.com/psf/black
rev: "20.8b1"
hooks:
- id: black
- repo: https://github.com/kynan/nbstripout
rev: "0.3.9"
hooks:
- id: nbstripout
5 changes: 0 additions & 5 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
# the i18n builder cannot share the environment and doctrees with the others
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .

PAGES = pages/sigmas.rst pages/quickstart.rst pages/custom.rst

pages/%.rst: _static/notebooks/%.ipynb
cd pages;jupyter nbconvert --template page_rst --to rst --output-dir . ../$<

.PHONY: help
help:
@echo "Please use \`make <target>' where <target> is one of"
Expand Down
169 changes: 0 additions & 169 deletions docs/_static/notebooks/custom.ipynb

This file was deleted.

Binary file removed docs/_static/notebooks/demo.png
Binary file not shown.
144 changes: 0 additions & 144 deletions docs/_static/notebooks/quickstart.ipynb

This file was deleted.

160 changes: 0 additions & 160 deletions docs/_static/notebooks/sigmas.ipynb

This file was deleted.

67 changes: 23 additions & 44 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,42 @@
# -*- coding: utf-8 -*-

import os
import sys

d = os.path.dirname
sys.path.insert(0, d(d(os.path.abspath(__file__))))
import corner # NOQA
import corner

extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.mathjax",
"sphinx.ext.napoleon",
"myst_nb",
]
templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"

# General information about the project.
project = u"corner.py"
copyright = u"2013-2016 Dan Foreman-Mackey & contributors"
copyright = u"2013-2021 Dan Foreman-Mackey & contributors"

version = corner.__version__
release = corner.__version__

exclude_patterns = ["_build", "_static/notebooks/profile"]
pygments_style = "sphinx"

# Sphinx-gallery
sphinx_gallery_conf = dict(
examples_dirs="../examples",
gallery_dirs="examples",
)

# Readthedocs.
on_rtd = os.environ.get("READTHEDOCS", None) == "True"
if not on_rtd:
import sphinx_rtd_theme

html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]

# htmp_theme_options = dict(
# analytics_id="analytics_id",
# )
# html_context = dict(
# display_github=True,
# github_user="dfm",
# github_repo="corner.py",
# github_version="corner.py",
# conf_py_path="/docs/",
# favicon="favicon.png",
# script_files=[
# "_static/jquery.js",
# "_static/underscore.js",
# "_static/doctools.js",
# "//cdn.mathjax.org/mathjax/latest/MathJax.js"
# "?config=TeX-AMS-MML_HTMLorMML",
# "_static/js/analytics.js",
# ],
# )
exclude_patterns = ["_build"]
html_theme = "sphinx_book_theme"
html_title = "corner.py"
html_static_path = ["_static"]
html_show_sourcelink = False
html_theme_options = {
"path_to_docs": "docs",
"repository_url": "https://github.com/dfm/corner.py",
"repository_branch": "main",
"launch_buttons": {
"binderhub_url": "https://mybinder.org",
"colab_url": "https://colab.research.google.com/",
"notebook_interface": "jupyterlab",
},
"use_edit_page_button": True,
"use_issues_button": True,
"use_repository_button": True,
"use_download_button": True,
}
html_baseurl = "https://corner.readthedocs.io/en/latest/"

jupyter_execute_notebooks = "force"
3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Documentation
pages/quickstart
pages/sigmas
pages/custom
pages/arviz
api


Expand Down Expand Up @@ -76,7 +77,7 @@ If you make use of this code, please cite `the JOSS paper
Authors & License
-----------------

Copyright 2013-2020 Dan Foreman-Mackey
Copyright 2013-2021 Dan Foreman-Mackey

Built by `Dan Foreman-Mackey <https://github.com/dfm>`_ and contributors (see
`the contribution graph <https://github.com/dfm/corner.py/graphs/contributors>`_ for the most
Expand Down
10 changes: 5 additions & 5 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ with `pip <http://www.pip-installer.org/>`_:

.. code-block:: bash
pip install corner
python -m pip install corner
From source
Expand All @@ -38,18 +38,18 @@ directory and run:

.. code-block:: bash
python setup.py install
python -m pip install .
Tests
-----

If you installed from source, you can run the unit tests using `nose
<http://nose.readthedocs.org/>`_. From the root of the source directory, run:
If you installed from source, you can run the unit tests. From the root of the
source directory, run:

.. code-block:: bash
nosetests corner
python -m pip install pytest -v tests
This might take a few minutes but you shouldn't get any errors if all went
as planned.
80 changes: 80 additions & 0 deletions docs/pages/arviz.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ArviZ support\n",
"\n",
"As of version 2.2, corner supports plotting [ArviZ](https://arviz-devs.github.io/arviz/) samples, providing a consistent interface to [many MCMC samplers](https://arviz-devs.github.io/arviz/getting_started/CreatingInferenceData.html).\n",
"\n",
"Here is a simple example of this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import corner\n",
"import numpy as np\n",
"import arviz as az\n",
"\n",
"np.random.seed(11234)\n",
"\n",
"x = np.random.randn(2, 2000)\n",
"data = az.from_dict(\n",
" posterior={\n",
" \"x\": x,\n",
" \"y\": np.random.randn(2, 2000, 2)\n",
" },\n",
" sample_stats={\n",
" \"diverging\": x < -1.2\n",
" }\n",
")\n",
"\n",
"figure = corner.corner(data, divergences=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When using this interface, it is probably best to provide the `truths` parameter using a dictionary keyed on the variable names:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"figure = corner.corner(data, truths={\"x\": 1.5, \"y\": [-0.3, 0.1]})"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
132 changes: 132 additions & 0 deletions docs/pages/custom.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Custom plotting\n",
"\n",
"It can sometimes be useful to add custom annotations or plot elements to the output of `corner.corner`.\n",
"This tutorial shows how you can do this.\n",
"\n",
"For example, let's say that we want to overplot a few different values for comparison.\n",
"The `truths` interface isn't sufficient for supporting this use case, so here's how you can do it.\n",
"\n",
"First, let's generate some fake data with a mode at the origin and another randomly sampled mode:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import corner\n",
"import numpy as np\n",
"\n",
"ndim, nsamples = 4, 50000\n",
"np.random.seed(1234)\n",
"data1 = np.random.randn(ndim * 4 * nsamples // 5).reshape([4 * nsamples // 5, ndim])\n",
"mean = 4*np.random.rand(ndim)\n",
"data2 = (mean[None, :] + np.random.randn(ndim * nsamples // 5).reshape([nsamples // 5, ndim]))\n",
"samples = np.vstack([data1, data2])\n",
"\n",
"figure = corner.corner(samples)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's overplot the empirical mean of the samples and the true mean of the second mode."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This is the true mean of the second mode that we used above:\n",
"value1 = mean\n",
"\n",
"# This is the empirical mean of the sample:\n",
"value2 = np.mean(samples, axis=0)\n",
"\n",
"# Make the base corner plot\n",
"figure = corner.corner(samples)\n",
"\n",
"# Extract the axes\n",
"axes = np.array(figure.axes).reshape((ndim, ndim))\n",
"\n",
"# Loop over the diagonal\n",
"for i in range(ndim):\n",
" ax = axes[i, i]\n",
" ax.axvline(value1[i], color=\"g\")\n",
" ax.axvline(value2[i], color=\"r\")\n",
" \n",
"# Loop over the histograms\n",
"for yi in range(ndim):\n",
" for xi in range(yi):\n",
" ax = axes[yi, xi]\n",
" ax.axvline(value1[xi], color=\"g\")\n",
" ax.axvline(value2[xi], color=\"r\")\n",
" ax.axhline(value1[yi], color=\"g\")\n",
" ax.axhline(value2[yi], color=\"r\")\n",
" ax.plot(value1[xi], value1[yi], \"sg\")\n",
" ax.plot(value2[xi], value2[yi], \"sr\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A similar procedure could be used to add anything to the axes that you can normally do with matplotlib.\n",
"\n",
"This being said, there is actually an even easier way to do this using the `overplot_lines` and `overplot_points` functions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This is the true mean of the second mode that we used above:\n",
"value1 = mean\n",
"\n",
"# This is the empirical mean of the sample:\n",
"value2 = np.mean(samples, axis=0)\n",
"\n",
"# Make the base corner plot\n",
"figure = corner.corner(samples)\n",
"\n",
"corner.overplot_lines(figure, value1, color=\"C1\")\n",
"corner.overplot_points(figure, value1[None], marker=\"s\", color=\"C1\")\n",
"corner.overplot_lines(figure, value2, color=\"C2\")\n",
"corner.overplot_points(figure, value2[None], marker=\"s\", color=\"C2\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit 21647ea

Please sign in to comment.